From patchwork Mon Mar 31 00:28:09 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexei Starovoitov X-Patchwork-Id: 14033728 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id BD2A8C3600C for ; Mon, 31 Mar 2025 15:46:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E8DCD280004; Mon, 31 Mar 2025 11:46:22 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E3E7D280001; Mon, 31 Mar 2025 11:46:22 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D051E280004; Mon, 31 Mar 2025 11:46:22 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id B39CA280001 for ; Mon, 31 Mar 2025 11:46:22 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 2A6B4552C0 for ; Mon, 31 Mar 2025 00:28:18 +0000 (UTC) X-FDA: 83279959476.18.F9E0129 Received: from mail-pj1-f42.google.com (mail-pj1-f42.google.com [209.85.216.42]) by imf05.hostedemail.com (Postfix) with ESMTP id 60CE7100007 for ; Mon, 31 Mar 2025 00:28:16 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=Hbm+bo6J; spf=pass (imf05.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.216.42 as permitted sender) smtp.mailfrom=alexei.starovoitov@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1743380896; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=rFi5AJSWI2YMMuD5etUG01CTNnkiusxKc3AgS1Fz8Ig=; b=r23YQ8EUsO+Pd9MrpNo5kbuApksBMW9+Ly9L/Vx/5uYC/9Tsj/h1PoGQL8QZF8u/l3jgkJ fellHPvNaP5sQCQ0TeAZctPMZ+U0VS0/lUT0EAMLW3L/+uMn46SfEvIIr3q/eqh19cLL2E BQKS2eppkNQ5bkbUr07S9N7PjwdYS/s= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=Hbm+bo6J; spf=pass (imf05.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.216.42 as permitted sender) smtp.mailfrom=alexei.starovoitov@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1743380896; a=rsa-sha256; cv=none; b=tLopj4KrWih49XF8YEbyFbt4Pb4mjWVC7a5tGDfmvbS2v+VhJ5ZpEUFkyemv17PEysoZcI ZmqyFv37+KzjUd36aGmox7Nylz/b9wvW95Z7U4F1bYmB3AoQhCVeiskB3c2cLl01UrOdVz pQIAHOab8WfOJVNYpmGriE64Oqa9K6w= Received: by mail-pj1-f42.google.com with SMTP id 98e67ed59e1d1-303a66af07eso5409341a91.2 for ; Sun, 30 Mar 2025 17:28:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1743380895; x=1743985695; darn=kvack.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=rFi5AJSWI2YMMuD5etUG01CTNnkiusxKc3AgS1Fz8Ig=; b=Hbm+bo6JCH1TxS8JX6CAaZuZVh+UGw9EQ3giSvAaAQ0kD9HpzGB/jzHhPV8AysJ6SP fGreYPt1Qc/4Fqb0/7VZ4+FyJ9ttcIiowx/xDy3Xhd0vZ4jl2GwN8f/AdJMYHzAHPps3 KcDsDMIZTWadPZ7M1sn+2sz8TznFHFZobveWevwOqj7VEpqjLfCEPXGau+XzeFZxUvMH BKgJXvK2Aoimx/WbB4i/mqI1nKa6zCgj3qawDAMub3m1IFOcsPDoBjKgGCzvzMgGvvJS 7N8beouOKMDQOby7ePVzGMMYe061OqBmEzzVyZJp3qrh1OfwOeE27DX/jAnLcg99JR1Z b1uw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743380895; x=1743985695; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=rFi5AJSWI2YMMuD5etUG01CTNnkiusxKc3AgS1Fz8Ig=; b=DEDT8y6NCVCjU5utiVNDgz3YHApE+rVOI48gCpX1lR8SRHXNVY0o/0exfkFMX0Hz1w 0g6bi4r0tjYVeDIA0NXCg0LBeDj0OfSkAzAdNyZbnZpQfnJ/qvGY3YpJjUpP5bR1gsx6 gZf3zDmk2EdiUZ38Dp1gIpQBc0mVZ+97eypmOQo4+y58oNNXjIdg4BpksANpDfow1L5b HJjLUN2mjBFRMnb0bXiTj2sdzYK+aDXeNIjpjhnkN9o1i25JDBUaIOKp45VB7swKrMMp KWwBQxrBgn1c1kaonM1C+Ia7htP3YpV7BDriHlzLkr4zh5C5/yZ65VfDidLcWmCV68Kg Y2lw== X-Forwarded-Encrypted: i=1; AJvYcCUMUIPgjFa6gqmBswI20IXmm7xfBWL7J3IT1V8vWEd2V+igK3HhSDOxIQtPtIenuYjgdjnHJ4s6Zw==@kvack.org X-Gm-Message-State: AOJu0YzfucuFNnyRzX9eD6oq4QGa8q8QNY+hL/Rcr3Dy+PcZAgWa4/Df cIx31YW0G5aOgL8dCMsTBt1tsgc1fVXMGeVetp/PbqdsdRF4koVQ X-Gm-Gg: ASbGncti4xhjTUy/NsSXvnJZObm4Y8+mm5oyXeK1w3k37+ykW59SUPolydYgHSVKtXn r6OO9CwRniJNwwQqbgnRhpBH+81jb50s7oECQ/+bvs+ZKhnznTZmgNC1ne4FHVQwkS5Gq5ljhco LFbCacjI6VqBml8sDkOSku/0TNMzTbCoSgvH08YWAybIl3dMj/UW+M4/HqmSXigTliECuKhHCDc bXp5yFOy7M1PqwCSDw13Sw/367+rwwOrl3yHXNc+doFgO1/4JvUCq1sRqbF0VBolb13s7cMpqZN qBc/RAyDNjn1aDfZJnOsmNDs24SHrH1j3TRkC9p4QHVebweZNKTSdlZ9A/cB0kdLS/rhw0ij X-Google-Smtp-Source: AGHT+IGd9xVHYNN5LAtu0WVsviR9zrZOcbiQrZ0CixultBjriE+r9Er3YZeWI0M2GMQRlBvx3P1H7g== X-Received: by 2002:a17:90b:384d:b0:2ff:7b28:a519 with SMTP id 98e67ed59e1d1-30532158ee8mr10273885a91.30.1743380894198; Sun, 30 Mar 2025 17:28:14 -0700 (PDT) Received: from localhost.localdomain ([2620:10d:c090:400::5:22d1]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3039dfd4ab0sm8385636a91.6.2025.03.30.17.28.12 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Sun, 30 Mar 2025 17:28:13 -0700 (PDT) From: Alexei Starovoitov To: Linus Torvalds Cc: bpf@vger.kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@kernel.org, akpm@linux-foundation.org, peterz@infradead.org, vbabka@suse.cz, bigeasy@linutronix.de, rostedt@goodmis.org, shakeel.butt@linux.dev, mhocko@suse.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH mm] mm/page_alloc: Avoid second trylock of zone->lock Date: Sun, 30 Mar 2025 17:28:09 -0700 Message-Id: <20250331002809.94758-1-alexei.starovoitov@gmail.com> X-Mailer: git-send-email 2.39.5 (Apple Git-154) MIME-Version: 1.0 X-Rspamd-Queue-Id: 60CE7100007 X-Rspamd-Server: rspam05 X-Rspam-User: X-Stat-Signature: x4qsua15f67ucf7dqd6qbd5jmharpuxi X-HE-Tag: 1743380896-423719 X-HE-Meta: U2FsdGVkX19jqXAbG2ovnLihW1mwQoFJXrbJhgAgiCkWTljgP+LSxRj19TfoOz8kMEhRpL3SsPHAoUM9Zn+bO1RRHmPOTS4LNr/AH4l8aBG+w7KjmGOl9okNjq8UB2UqbYVyZXiIdRP8O1pULP1rMlXyewBSo1M2RED983Socz/nrD+Ip+ltdoaoYwBw+0Nco+u66LrK9xBwDqXtawf46ySn1XW+S6oc++6az8LpQFgrUn4NxadoXsKabvNi96/Ui6uVEoe/IFJD77GJLeoHw6OxhPQoeJJCNpWQm58ta1Y3hGQD1/yQi5qK9uzmBCddMIdk3RkYSpmKXKaj7sVIK4ib8mU8spm7bJHy2j7hgUc2mKEY0jvJ4AO0UxqL8m05D+YC1iSJwuN+enECMyQ994wjhUU1a+/X2/c3ihNWJtgy4BU+t+LHZ0a7TUef6wUMmjvoXz8O8aWqvh76FZwLa4KMwxgdydRpXMkMbOi2TtzUItzzICNh9KdulRMs9EU3/cDVlHvU33EPyJ8kcIOe+LTEq0UEiG80C1algSYjfOLvNpAYTafP9q2ZkcvrzK72VBgBya2zY5ABLR2R5CLDnMrzDJ3O1EFXuUTKGAkXGKKnPAoaPse7DOT7diBReXpF04RoSOKBPOcjOa4vPgkxBY0JbdFot3wm0Kv3HR6D+ZSTVXytoCtiY7RhvcdK0Mp0OSCmwOMhnPkWKQg8B731lu0Kp9TgioIT17evpGjvbVhhFN5ThYN403RwcPcFBCGigAuc2po5u/NLk/wMnOvRXPKzVTH4AF1dPQbTLdeBszmokxedAj0Fzb5zuRCI1SKfC2zcIu8gUifq85e0FJCfeE/luRdugXDLlz8hov4Xm1+MjoLGhXvS5vGgvaolBEkfszA78XTHmGLvpcIwulueduH2/rawrYqTQJ85ftbqXe6BQ/iLemPp4cRs1faHhu+L5FVXVPOjP/rbcw/EvBu z+EvXa// RmvPioNKDxdcSke/QBAQrEYXBIL/8HjEcaYINgB7tA1OiXRE+nTQv5DYANbDWaEcCbUI/HOQlW2F9UiHoTGZIWiHDtl7DyDEGM6dy+0hrhOVHQf1Pbt8H3gqEn6Hj86QUTdOSI7zIjuV+Uxl8FOuvICJbMNdIHIlmfmb5Nbk+6HUAS3JoX4Ei21bNncrh0YKF9KMZtrUiMVvzNI6QcyAgqk/Zz+/MNx4/wpenFX7TMMQRUlJWaerXZah3x0fOKMeHBacFtgSWLs84cWgSJFNAZFy4BDsO9kumNxc5X8XftPmwPMx4sifSQwzvEfSB8J5fcxZfAfutuDLudl9c4la9nyabM/pv8NounneI0sJbHt/5j7xO6+c58cen3EXu6azY+cLsBufDk6WYAAzTIqULVOmR48qhH0VHiHgHdRZ0W1fJwEAYf/gFL68hedWXki83uKSiC7xjIt2Nryf7GGJuXc3u9BjgiYZkPKdX3cEmyu0Cuogp6ZjYVcvNYJFfcvmMGSxYA5m7wiDemUlaFSvywzWngM3rKpX8aXnyn7JSwolniXvA8l+PQzDJCjLU5gLfdO8q/+YoLlpngnyRPYIa/T5RbKkimBLqG94t X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Alexei Starovoitov spin_trylock followed by spin_lock will cause extra write cache access. If the lock is contended it may cause unnecessary cache line bouncing and will execute redundant irq restore/save pair. Therefore, check alloc/fpi_flags first and use spin_trylock or spin_lock. Suggested-by: Linus Torvalds Fixes: 97769a53f117 ("mm, bpf: Introduce try_alloc_pages() for opportunistic page allocation") Signed-off-by: Alexei Starovoitov Reviewed-by: Sebastian Andrzej Siewior Acked-by: Vlastimil Babka Reviewed-by: Harry Yoo Reviewed-by: Shakeel Butt --- mm/page_alloc.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/mm/page_alloc.c b/mm/page_alloc.c index e3ea5bf5c459..ffbb5678bc2f 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -1268,11 +1268,12 @@ static void free_one_page(struct zone *zone, struct page *page, struct llist_head *llhead; unsigned long flags; - if (!spin_trylock_irqsave(&zone->lock, flags)) { - if (unlikely(fpi_flags & FPI_TRYLOCK)) { + if (unlikely(fpi_flags & FPI_TRYLOCK)) { + if (!spin_trylock_irqsave(&zone->lock, flags)) { add_page_to_zone_llist(zone, page, order); return; } + } else { spin_lock_irqsave(&zone->lock, flags); } @@ -2341,9 +2342,10 @@ static int rmqueue_bulk(struct zone *zone, unsigned int order, unsigned long flags; int i; - if (!spin_trylock_irqsave(&zone->lock, flags)) { - if (unlikely(alloc_flags & ALLOC_TRYLOCK)) + if (unlikely(alloc_flags & ALLOC_TRYLOCK)) { + if (!spin_trylock_irqsave(&zone->lock, flags)) return 0; + } else { spin_lock_irqsave(&zone->lock, flags); } for (i = 0; i < count; ++i) { @@ -2964,9 +2966,10 @@ struct page *rmqueue_buddy(struct zone *preferred_zone, struct zone *zone, do { page = NULL; - if (!spin_trylock_irqsave(&zone->lock, flags)) { - if (unlikely(alloc_flags & ALLOC_TRYLOCK)) + if (unlikely(alloc_flags & ALLOC_TRYLOCK)) { + if (!spin_trylock_irqsave(&zone->lock, flags)) return NULL; + } else { spin_lock_irqsave(&zone->lock, flags); } if (alloc_flags & ALLOC_HIGHATOMIC)