From patchwork Thu Feb 13 03:35:51 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexei Starovoitov X-Patchwork-Id: 13972741 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-pj1-f45.google.com (mail-pj1-f45.google.com [209.85.216.45]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 685479476 for ; Thu, 13 Feb 2025 03:36:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.45 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739417766; cv=none; b=tIPp5FpPvCV/9FF1Ao2jG4jn6Lj/xgmpLBeoXwlskXN09/38K+xllktIE2eHkUk7c/O9lNN3N2TJhQJvn0GK/em0lQ+91ttB0MTDjSMfdtNnqotjLnzLqVuU6sWm0ekHROOpHCxSoxlx+i44rB3Hu25Zky1FKRF+820WzKFUymw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739417766; c=relaxed/simple; bh=iYGhUljMqRO2DRd3TBcochxe0N1xSyUPCmGFy7eR+eM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=l0kyJK94fXz0VLfeUicIILDLdkXdRbhRBnXjStK3dEYyqQBCmDFeJkLtfyAMFUVOfDXGh0jCYl7uxP/YNREXwV3WHqU+Syfn8JJLYq2bGbXEX77B5O0Xq6Huu0swGsMzJtH0xHVjUdRKORvxVlHPjQ7Jzi4D62Rme5idJcv0SCY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=K58U8uN7; arc=none smtp.client-ip=209.85.216.45 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="K58U8uN7" Received: by mail-pj1-f45.google.com with SMTP id 98e67ed59e1d1-2fc11834404so330535a91.0 for ; Wed, 12 Feb 2025 19:36:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1739417763; x=1740022563; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=neE48xwS02SE7GqbOWiJ7ZnS1/Lsz/8j6zs7IbGprYg=; b=K58U8uN7HjCJRyQKvtr84NpgDLcE7VwJMbhej1f17wYmCgE/81YCK6KOzWuFbtckdB UUffq7CnjNjysyPpEgyapNiwmy98JSmPfsGPKbXNqW9P6DyA83yJdytMvuJstGl7B2jK a9i54A8qJgjEMovpRFHghgznli1FFk7QYb07tZ8M87My9VltbYd7a953CqrgmjSuSHV9 G1B+vKoN22h+92ypCqmPc+XO46o1FrC8GrKw3tamTn2zA9s25koLT3V049XvjBuewKBn +srOZFLtbLnkkoCc/FzTIbfwHHp8JwZ/c/T8EKjMt4UZ4M3NnrdWxagWkI1rMrPo3cl6 S2Jg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739417763; x=1740022563; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=neE48xwS02SE7GqbOWiJ7ZnS1/Lsz/8j6zs7IbGprYg=; b=grs7WBfWYtunHnu0L+yJTMnaZ/AjmNPZhafGSA9OMFle3iiyoNwSphu8ZSyeJHIDvf Txj9wo5iE5rMp9SC0bcWnj3mw8xJkOY9CcLwxVwOXZPsHA1vNhgHup24pspwahvtQvcC wjaUPtdopFnNxRwwmh2iZorW6H76JaZt/lNYe0BlKL/2+c0qy31AkTEijMyhXpHDQYIC F6LxQOG/Gvpf1zGRtA4KGddak4y6r2ZO1xF/5D5MN+YSZcCgSRI98G9X+BXEsiMvbdQN R41+HMtlfQhvBnmaAEmtF0bDj78VRUdi5Inll8HUOQamE/oppH0VnNgULtIuFf9WLANs YSTQ== X-Gm-Message-State: AOJu0YywLq8EXcWzqavlHewhZY+q0pZ485e8+DwvMh6PwaxCdQoS7U93 VeoqGty0CoJ5mF5HQ6+2EaAKwXom0YvuXXDRr5MAyb1MD7sqp7us4zCxjA== X-Gm-Gg: ASbGncvbKiN8k5u6rl/sJfazt4qG35WzzMyrpzaWmCkEkAxiuLYDpId0rESMSWrgO2V bKViup8OMlT16Zlso+Nhn6oLZ8l597yez8BwwA2fJiKgP5PvDIw9t6xv5uSPHzTEkaqAL2SJHjJ HM4bxv6Yujkq/aOvRRSRiw+gaTzlzc1cxyQw0xvh2+q/oytVNVies7NfXbWY5uCcKRG4lmp5UxQ +UK5lBYW/DdGOizQoxTff0u+M7pNZdWm5iPACkc2JKE5a7kD4N5XGLcuB+AI9V9L21DT7pvyZtB PtavCweN9khM7Gprr4dhRfzrapL+uSsaD3TKFT61hpRJdGKM2Q== X-Google-Smtp-Source: AGHT+IG/OKe0k23HRwq6uyNTvVhZ1LwcohTQ4NHabBVvA7giq717pAjLvBtV5aaQaGpRA45ZMtAmaw== X-Received: by 2002:a17:90b:54c4:b0:2ee:ab29:1a65 with SMTP id 98e67ed59e1d1-2fbf5bc07e4mr8801377a91.4.1739417762744; Wed, 12 Feb 2025 19:36:02 -0800 (PST) Received: from macbookpro.lan ([2603:3023:16e:5000:8af:ecd2:44cd:8027]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2fc13b91232sm198511a91.34.2025.02.12.19.36.01 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Wed, 12 Feb 2025 19:36:02 -0800 (PST) From: Alexei Starovoitov To: bpf@vger.kernel.org Cc: andrii@kernel.org, memxor@gmail.com, akpm@linux-foundation.org, peterz@infradead.org, vbabka@suse.cz, bigeasy@linutronix.de, rostedt@goodmis.org, houtao1@huawei.com, hannes@cmpxchg.org, shakeel.butt@linux.dev, mhocko@suse.com, willy@infradead.org, tglx@linutronix.de, jannh@google.com, tj@kernel.org, linux-mm@kvack.org, kernel-team@fb.com Subject: [PATCH bpf-next v8 1/6] mm, bpf: Introduce try_alloc_pages() for opportunistic page allocation Date: Wed, 12 Feb 2025 19:35:51 -0800 Message-Id: <20250213033556.9534-2-alexei.starovoitov@gmail.com> X-Mailer: git-send-email 2.39.5 (Apple Git-154) In-Reply-To: <20250213033556.9534-1-alexei.starovoitov@gmail.com> References: <20250213033556.9534-1-alexei.starovoitov@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net From: Alexei Starovoitov Tracing BPF programs execute from tracepoints and kprobes where running context is unknown, but they need to request additional memory. The prior workarounds were using pre-allocated memory and BPF specific freelists to satisfy such allocation requests. Instead, introduce gfpflags_allow_spinning() condition that signals to the allocator that running context is unknown. Then rely on percpu free list of pages to allocate a page. try_alloc_pages() -> get_page_from_freelist() -> rmqueue() -> rmqueue_pcplist() will spin_trylock to grab the page from percpu free list. If it fails (due to re-entrancy or list being empty) then rmqueue_bulk()/rmqueue_buddy() will attempt to spin_trylock zone->lock and grab the page from there. spin_trylock() is not safe in PREEMPT_RT when in NMI or in hard IRQ. Bailout early in such case. The support for gfpflags_allow_spinning() mode for free_page and memcg comes in the next patches. This is a first step towards supporting BPF requirements in SLUB and getting rid of bpf_mem_alloc. That goal was discussed at LSFMM: https://lwn.net/Articles/974138/ Acked-by: Michal Hocko Acked-by: Vlastimil Babka Acked-by: Sebastian Andrzej Siewior Reviewed-by: Shakeel Butt Signed-off-by: Alexei Starovoitov --- include/linux/gfp.h | 22 ++++++++++ lib/stackdepot.c | 5 ++- mm/internal.h | 1 + mm/page_alloc.c | 104 ++++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 127 insertions(+), 5 deletions(-) diff --git a/include/linux/gfp.h b/include/linux/gfp.h index 6bb1a5a7a4ae..5d9ee78c74e4 100644 --- a/include/linux/gfp.h +++ b/include/linux/gfp.h @@ -39,6 +39,25 @@ static inline bool gfpflags_allow_blocking(const gfp_t gfp_flags) return !!(gfp_flags & __GFP_DIRECT_RECLAIM); } +static inline bool gfpflags_allow_spinning(const gfp_t gfp_flags) +{ + /* + * !__GFP_DIRECT_RECLAIM -> direct claim is not allowed. + * !__GFP_KSWAPD_RECLAIM -> it's not safe to wake up kswapd. + * All GFP_* flags including GFP_NOWAIT use one or both flags. + * try_alloc_pages() is the only API that doesn't specify either flag. + * + * This is stronger than GFP_NOWAIT or GFP_ATOMIC because + * those are guaranteed to never block on a sleeping lock. + * Here we are enforcing that the allocation doesn't ever spin + * on any locks (i.e. only trylocks). There is no high level + * GFP_$FOO flag for this use in try_alloc_pages() as the + * regular page allocator doesn't fully support this + * allocation mode. + */ + return !(gfp_flags & __GFP_RECLAIM); +} + #ifdef CONFIG_HIGHMEM #define OPT_ZONE_HIGHMEM ZONE_HIGHMEM #else @@ -335,6 +354,9 @@ static inline struct page *alloc_page_vma_noprof(gfp_t gfp, } #define alloc_page_vma(...) alloc_hooks(alloc_page_vma_noprof(__VA_ARGS__)) +struct page *try_alloc_pages_noprof(int nid, unsigned int order); +#define try_alloc_pages(...) alloc_hooks(try_alloc_pages_noprof(__VA_ARGS__)) + extern unsigned long get_free_pages_noprof(gfp_t gfp_mask, unsigned int order); #define __get_free_pages(...) alloc_hooks(get_free_pages_noprof(__VA_ARGS__)) diff --git a/lib/stackdepot.c b/lib/stackdepot.c index 245d5b416699..377194969e61 100644 --- a/lib/stackdepot.c +++ b/lib/stackdepot.c @@ -591,7 +591,8 @@ depot_stack_handle_t stack_depot_save_flags(unsigned long *entries, depot_stack_handle_t handle = 0; struct page *page = NULL; void *prealloc = NULL; - bool can_alloc = depot_flags & STACK_DEPOT_FLAG_CAN_ALLOC; + bool allow_spin = gfpflags_allow_spinning(alloc_flags); + bool can_alloc = (depot_flags & STACK_DEPOT_FLAG_CAN_ALLOC) && allow_spin; unsigned long flags; u32 hash; @@ -630,7 +631,7 @@ depot_stack_handle_t stack_depot_save_flags(unsigned long *entries, prealloc = page_address(page); } - if (in_nmi()) { + if (in_nmi() || !allow_spin) { /* We can never allocate in NMI context. */ WARN_ON_ONCE(can_alloc); /* Best effort; bail if we fail to take the lock. */ diff --git a/mm/internal.h b/mm/internal.h index 109ef30fee11..10a8b4b3b86e 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -1187,6 +1187,7 @@ unsigned int reclaim_clean_pages_from_list(struct zone *zone, #define ALLOC_NOFRAGMENT 0x0 #endif #define ALLOC_HIGHATOMIC 0x200 /* Allows access to MIGRATE_HIGHATOMIC */ +#define ALLOC_TRYLOCK 0x400 /* Only use spin_trylock in allocation path */ #define ALLOC_KSWAPD 0x800 /* allow waking of kswapd, __GFP_KSWAPD_RECLAIM set */ /* Flags that allow allocations below the min watermark. */ diff --git a/mm/page_alloc.c b/mm/page_alloc.c index 579789600a3c..0404c4c0dfc7 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -2307,7 +2307,11 @@ static int rmqueue_bulk(struct zone *zone, unsigned int order, unsigned long flags; int i; - spin_lock_irqsave(&zone->lock, flags); + if (!spin_trylock_irqsave(&zone->lock, flags)) { + if (unlikely(alloc_flags & ALLOC_TRYLOCK)) + return 0; + spin_lock_irqsave(&zone->lock, flags); + } for (i = 0; i < count; ++i) { struct page *page = __rmqueue(zone, order, migratetype, alloc_flags); @@ -2907,7 +2911,11 @@ struct page *rmqueue_buddy(struct zone *preferred_zone, struct zone *zone, do { page = NULL; - spin_lock_irqsave(&zone->lock, flags); + if (!spin_trylock_irqsave(&zone->lock, flags)) { + if (unlikely(alloc_flags & ALLOC_TRYLOCK)) + return NULL; + spin_lock_irqsave(&zone->lock, flags); + } if (alloc_flags & ALLOC_HIGHATOMIC) page = __rmqueue_smallest(zone, order, MIGRATE_HIGHATOMIC); if (!page) { @@ -4511,7 +4519,12 @@ static inline bool prepare_alloc_pages(gfp_t gfp_mask, unsigned int order, might_alloc(gfp_mask); - if (should_fail_alloc_page(gfp_mask, order)) + /* + * Don't invoke should_fail logic, since it may call + * get_random_u32() and printk() which need to spin_lock. + */ + if (!(*alloc_flags & ALLOC_TRYLOCK) && + should_fail_alloc_page(gfp_mask, order)) return false; *alloc_flags = gfp_to_alloc_flags_cma(gfp_mask, *alloc_flags); @@ -7071,3 +7084,88 @@ static bool __free_unaccepted(struct page *page) } #endif /* CONFIG_UNACCEPTED_MEMORY */ + +/** + * try_alloc_pages_noprof - opportunistic reentrant allocation from any context + * @nid - node to allocate from + * @order - allocation order size + * + * Allocates pages of a given order from the given node. This is safe to + * call from any context (from atomic, NMI, and also reentrant + * allocator -> tracepoint -> try_alloc_pages_noprof). + * Allocation is best effort and to be expected to fail easily so nobody should + * rely on the success. Failures are not reported via warn_alloc(). + * See always fail conditions below. + * + * Return: allocated page or NULL on failure. + */ +struct page *try_alloc_pages_noprof(int nid, unsigned int order) +{ + /* + * Do not specify __GFP_DIRECT_RECLAIM, since direct claim is not allowed. + * Do not specify __GFP_KSWAPD_RECLAIM either, since wake up of kswapd + * is not safe in arbitrary context. + * + * These two are the conditions for gfpflags_allow_spinning() being true. + * + * Specify __GFP_NOWARN since failing try_alloc_pages() is not a reason + * to warn. Also warn would trigger printk() which is unsafe from + * various contexts. We cannot use printk_deferred_enter() to mitigate, + * since the running context is unknown. + * + * Specify __GFP_ZERO to make sure that call to kmsan_alloc_page() below + * is safe in any context. Also zeroing the page is mandatory for + * BPF use cases. + * + * Though __GFP_NOMEMALLOC is not checked in the code path below, + * specify it here to highlight that try_alloc_pages() + * doesn't want to deplete reserves. + */ + gfp_t alloc_gfp = __GFP_NOWARN | __GFP_ZERO | __GFP_NOMEMALLOC; + unsigned int alloc_flags = ALLOC_TRYLOCK; + struct alloc_context ac = { }; + struct page *page; + + /* + * In PREEMPT_RT spin_trylock() will call raw_spin_lock() which is + * unsafe in NMI. If spin_trylock() is called from hard IRQ the current + * task may be waiting for one rt_spin_lock, but rt_spin_trylock() will + * mark the task as the owner of another rt_spin_lock which will + * confuse PI logic, so return immediately if called form hard IRQ or + * NMI. + * + * Note, irqs_disabled() case is ok. This function can be called + * from raw_spin_lock_irqsave region. + */ + if (IS_ENABLED(CONFIG_PREEMPT_RT) && (in_nmi() || in_hardirq())) + return NULL; + if (!pcp_allowed_order(order)) + return NULL; + +#ifdef CONFIG_UNACCEPTED_MEMORY + /* Bailout, since try_to_accept_memory_one() needs to take a lock */ + if (has_unaccepted_memory()) + return NULL; +#endif + /* Bailout, since _deferred_grow_zone() needs to take a lock */ + if (deferred_pages_enabled()) + return NULL; + + if (nid == NUMA_NO_NODE) + nid = numa_node_id(); + + prepare_alloc_pages(alloc_gfp, order, nid, NULL, &ac, + &alloc_gfp, &alloc_flags); + + /* + * Best effort allocation from percpu free list. + * If it's empty attempt to spin_trylock zone->lock. + */ + page = get_page_from_freelist(alloc_gfp, order, alloc_flags, &ac); + + /* Unlike regular alloc_pages() there is no __alloc_pages_slowpath(). */ + + trace_mm_page_alloc(page, order, alloc_gfp, ac.migratetype); + kmsan_alloc_page(page, order, alloc_gfp); + return page; +} From patchwork Thu Feb 13 03:35:52 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexei Starovoitov X-Patchwork-Id: 13972742 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-pj1-f52.google.com (mail-pj1-f52.google.com [209.85.216.52]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C067F9476 for ; Thu, 13 Feb 2025 03:36:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739417769; cv=none; b=e55ufMJXY0pJ9UDUxKPuwLamG71R5GfLVDvulZPoJTGjjNSVuPrj7WLR9LRHbz6wLfiPRIvndEV0YMwT+bSg5nFN8Tj8BKoIQjkN9/hBDHS7RD7GMgJw7pvUQZkiaq+t0kcqZfn6HI/nayL1QYR/SMQq+gy1DnddKqCvjP19W1E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739417769; c=relaxed/simple; bh=OMlqbXvLNTOITMc0brgNOKHtyzI37JuMHtdWinMtXKw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=YMDaD1J9dYGhblDuaugSGs2ctDygD+CsL6eICDfxzI/pEPomC/EMPEpqnrG7SEkz42K9WIGlWbnsKV+hjfB8dtvv0DZv04VtFU0eY0hKi72NstJ0MRb6p6m2zOV8Ga5WVDOqvF1ozFE1hkFTGr6F2VswcjZX0S+jPLYSOS+bb3A= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=UhBnrfD3; arc=none smtp.client-ip=209.85.216.52 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="UhBnrfD3" Received: by mail-pj1-f52.google.com with SMTP id 98e67ed59e1d1-2f44353649aso713236a91.0 for ; Wed, 12 Feb 2025 19:36:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1739417766; x=1740022566; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=lzTu73HDNMqdVTVLzxRBLMiERoGgzud+lLSX9AZjyIs=; b=UhBnrfD3Ijqkkt15uQVVpq2P8qF3kwkapDDgw+Im+RplWhUvUOb5/Zgfv3eqv1JAA5 Jh7c5GRB5+9zRqPefvasSTwl0Du5z/xIG/JfZd8KtwODoJllB54b2FTPMFoNR1kWTR6W yBhUzz+K+VbVqGdFLZNRB8Z4KoLBuebenj2etF3wcHQ/xoeZBkhNTG0KBJ10ysvvfWsV AW78qOPjITzyOj8Ji93WHfiXBxFNxTveQTMPvE2CC1FuJt7VK/Q66+ElYVa31nw2oqXk vaDsdGWjnbgQehejVJgueT8DbCjjQ/+RbQOMklR9RIhsLEZrNetPXzjNh4zOOFCtfIi1 LxKA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739417766; x=1740022566; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=lzTu73HDNMqdVTVLzxRBLMiERoGgzud+lLSX9AZjyIs=; b=ctIUpmwIcdiPUloBLNHSBzHTmWnGTwnvRi0wlMbTFbK5SzU32WZjdNkQPObuBR2Ybe RZC6LbhR+V6J5FuXS0EamWXZpWoIETqNoGbzEkt2xsvFcJV3MSanAn7ag/VrTg0bAaNO r+bbsftt+XKXhfNibwOcNIUEwKtGaw0Qa/MASrQ0YH65/Ko5S6Hgnz/5xCuxpXYBjRuR TG183Y8w5GVXbWls0nwZcd95f4Mcgd9Zmrc570G3r7wHZPwwfsClvSzPSaqyPVQ3xuQ/ LBQqwopu9aTfpaTrploH5aiVfEtWIW93OucdbE6Dheb725mql+aJPuPaMy5yNZwSsTvM Fsyg== X-Gm-Message-State: AOJu0YzTysiXzzgxBsI8nPzFpelAFntxfouObBBZJTDBZJL2P8JEM8hN 5TtqRm3PIXiyWrVdyfEnG6dlvshk+rOgVunH9NCO6D+t90p/lV3hyuKYrA== X-Gm-Gg: ASbGnctQTDu3SLPA5QzuFp5/awYr4fBI2MH+DD7McqtzAwpyd7MidrTaiT7boONmrty jEebCaT6j4N6IQzuJdcB4oYgDXyJaEza4pKJ9OnbmIn3WLq4Wcve4iOlkG5z3Vj2Iea1c0LNa5O +oyuDXMCqudY57T0DPNkcUs08kz8Nfy5PqctVz+S5emzO4h4PDEBkK1pM0djGpiO82VWU8U3JZ0 cSNJxqKBDty8S9zLw3AqPrNauYDYyFTGVRbyG6fEmLrQY2TzpN1tIfzFL1qBxDi73jgi7EeOb9M L+O5jk8nMGRQodIdWdaOJQ/8JjimXCBMVbv0xBCD3kSbHl7MUA== X-Google-Smtp-Source: AGHT+IHwW0mWciqg0xYIxqI4O6t3z/9NXB3Y7q2juLoIWFRHSivyRpyZDeMZECumFXMCEhZFV6jUHg== X-Received: by 2002:a17:90a:d404:b0:2ea:3f34:f190 with SMTP id 98e67ed59e1d1-2fbf91067abmr7466225a91.25.1739417766159; Wed, 12 Feb 2025 19:36:06 -0800 (PST) Received: from macbookpro.lan ([2603:3023:16e:5000:8af:ecd2:44cd:8027]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2fbf9ab0233sm2256043a91.44.2025.02.12.19.36.04 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Wed, 12 Feb 2025 19:36:05 -0800 (PST) From: Alexei Starovoitov To: bpf@vger.kernel.org Cc: andrii@kernel.org, memxor@gmail.com, akpm@linux-foundation.org, peterz@infradead.org, vbabka@suse.cz, bigeasy@linutronix.de, rostedt@goodmis.org, houtao1@huawei.com, hannes@cmpxchg.org, shakeel.butt@linux.dev, mhocko@suse.com, willy@infradead.org, tglx@linutronix.de, jannh@google.com, tj@kernel.org, linux-mm@kvack.org, kernel-team@fb.com Subject: [PATCH bpf-next v8 2/6] mm, bpf: Introduce free_pages_nolock() Date: Wed, 12 Feb 2025 19:35:52 -0800 Message-Id: <20250213033556.9534-3-alexei.starovoitov@gmail.com> X-Mailer: git-send-email 2.39.5 (Apple Git-154) In-Reply-To: <20250213033556.9534-1-alexei.starovoitov@gmail.com> References: <20250213033556.9534-1-alexei.starovoitov@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net From: Alexei Starovoitov Introduce free_pages_nolock() that can free pages without taking locks. It relies on trylock and can be called from any context. Since spin_trylock() cannot be used in PREEMPT_RT from hard IRQ or NMI it uses lockless link list to stash the pages which will be freed by subsequent free_pages() from good context. Do not use llist unconditionally. BPF maps continuously allocate/free, so we cannot unconditionally delay the freeing to llist. When the memory becomes free make it available to the kernel and BPF users right away if possible, and fallback to llist as the last resort. Acked-by: Vlastimil Babka Acked-by: Sebastian Andrzej Siewior Reviewed-by: Shakeel Butt Signed-off-by: Alexei Starovoitov --- include/linux/gfp.h | 1 + include/linux/mm_types.h | 4 ++ include/linux/mmzone.h | 3 ++ lib/stackdepot.c | 5 ++- mm/page_alloc.c | 90 +++++++++++++++++++++++++++++++++++----- mm/page_owner.c | 8 +++- 6 files changed, 98 insertions(+), 13 deletions(-) diff --git a/include/linux/gfp.h b/include/linux/gfp.h index 5d9ee78c74e4..ceb226c2e25c 100644 --- a/include/linux/gfp.h +++ b/include/linux/gfp.h @@ -379,6 +379,7 @@ __meminit void *alloc_pages_exact_nid_noprof(int nid, size_t size, gfp_t gfp_mas __get_free_pages((gfp_mask) | GFP_DMA, (order)) extern void __free_pages(struct page *page, unsigned int order); +extern void free_pages_nolock(struct page *page, unsigned int order); extern void free_pages(unsigned long addr, unsigned int order); #define __free_page(page) __free_pages((page), 0) diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h index 6b27db7f9496..483aa90242cd 100644 --- a/include/linux/mm_types.h +++ b/include/linux/mm_types.h @@ -99,6 +99,10 @@ struct page { /* Or, free page */ struct list_head buddy_list; struct list_head pcp_list; + struct { + struct llist_node pcp_llist; + unsigned int order; + }; }; /* See page-flags.h for PAGE_MAPPING_FLAGS */ struct address_space *mapping; diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h index 9540b41894da..e16939553930 100644 --- a/include/linux/mmzone.h +++ b/include/linux/mmzone.h @@ -972,6 +972,9 @@ struct zone { /* Primarily protects free_area */ spinlock_t lock; + /* Pages to be freed when next trylock succeeds */ + struct llist_head trylock_free_pages; + /* Write-intensive fields used by compaction and vmstats. */ CACHELINE_PADDING(_pad2_); diff --git a/lib/stackdepot.c b/lib/stackdepot.c index 377194969e61..73d7b50924ef 100644 --- a/lib/stackdepot.c +++ b/lib/stackdepot.c @@ -672,7 +672,10 @@ depot_stack_handle_t stack_depot_save_flags(unsigned long *entries, exit: if (prealloc) { /* Stack depot didn't use this memory, free it. */ - free_pages((unsigned long)prealloc, DEPOT_POOL_ORDER); + if (!allow_spin) + free_pages_nolock(virt_to_page(prealloc), DEPOT_POOL_ORDER); + else + free_pages((unsigned long)prealloc, DEPOT_POOL_ORDER); } if (found) handle = found->handle.handle; diff --git a/mm/page_alloc.c b/mm/page_alloc.c index 0404c4c0dfc7..3fbcbeb7de8e 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -88,6 +88,9 @@ typedef int __bitwise fpi_t; */ #define FPI_TO_TAIL ((__force fpi_t)BIT(1)) +/* Free the page without taking locks. Rely on trylock only. */ +#define FPI_TRYLOCK ((__force fpi_t)BIT(2)) + /* prevent >1 _updater_ of zone percpu pageset ->high and ->batch fields */ static DEFINE_MUTEX(pcp_batch_high_lock); #define MIN_PERCPU_PAGELIST_HIGH_FRACTION (8) @@ -1249,13 +1252,44 @@ static void split_large_buddy(struct zone *zone, struct page *page, } while (1); } +static void add_page_to_zone_llist(struct zone *zone, struct page *page, + unsigned int order) +{ + /* Remember the order */ + page->order = order; + /* Add the page to the free list */ + llist_add(&page->pcp_llist, &zone->trylock_free_pages); +} + static void free_one_page(struct zone *zone, struct page *page, unsigned long pfn, unsigned int order, fpi_t fpi_flags) { + struct llist_head *llhead; unsigned long flags; - spin_lock_irqsave(&zone->lock, flags); + if (!spin_trylock_irqsave(&zone->lock, flags)) { + if (unlikely(fpi_flags & FPI_TRYLOCK)) { + add_page_to_zone_llist(zone, page, order); + return; + } + spin_lock_irqsave(&zone->lock, flags); + } + + /* The lock succeeded. Process deferred pages. */ + llhead = &zone->trylock_free_pages; + if (unlikely(!llist_empty(llhead) && !(fpi_flags & FPI_TRYLOCK))) { + struct llist_node *llnode; + struct page *p, *tmp; + + llnode = llist_del_all(llhead); + llist_for_each_entry_safe(p, tmp, llnode, pcp_llist) { + unsigned int p_order = p->order; + + split_large_buddy(zone, p, page_to_pfn(p), p_order, fpi_flags); + __count_vm_events(PGFREE, 1 << p_order); + } + } split_large_buddy(zone, page, pfn, order, fpi_flags); spin_unlock_irqrestore(&zone->lock, flags); @@ -2599,7 +2633,7 @@ static int nr_pcp_high(struct per_cpu_pages *pcp, struct zone *zone, static void free_frozen_page_commit(struct zone *zone, struct per_cpu_pages *pcp, struct page *page, int migratetype, - unsigned int order) + unsigned int order, fpi_t fpi_flags) { int high, batch; int pindex; @@ -2634,6 +2668,14 @@ static void free_frozen_page_commit(struct zone *zone, } if (pcp->free_count < (batch << CONFIG_PCP_BATCH_SCALE_MAX)) pcp->free_count += (1 << order); + + if (unlikely(fpi_flags & FPI_TRYLOCK)) { + /* + * Do not attempt to take a zone lock. Let pcp->count get + * over high mark temporarily. + */ + return; + } high = nr_pcp_high(pcp, zone, batch, free_high); if (pcp->count >= high) { free_pcppages_bulk(zone, nr_pcp_free(pcp, batch, high, free_high), @@ -2648,7 +2690,8 @@ static void free_frozen_page_commit(struct zone *zone, /* * Free a pcp page */ -void free_frozen_pages(struct page *page, unsigned int order) +static void __free_frozen_pages(struct page *page, unsigned int order, + fpi_t fpi_flags) { unsigned long __maybe_unused UP_flags; struct per_cpu_pages *pcp; @@ -2657,7 +2700,7 @@ void free_frozen_pages(struct page *page, unsigned int order) int migratetype; if (!pcp_allowed_order(order)) { - __free_pages_ok(page, order, FPI_NONE); + __free_pages_ok(page, order, fpi_flags); return; } @@ -2675,23 +2718,33 @@ void free_frozen_pages(struct page *page, unsigned int order) migratetype = get_pfnblock_migratetype(page, pfn); if (unlikely(migratetype >= MIGRATE_PCPTYPES)) { if (unlikely(is_migrate_isolate(migratetype))) { - free_one_page(zone, page, pfn, order, FPI_NONE); + free_one_page(zone, page, pfn, order, fpi_flags); return; } migratetype = MIGRATE_MOVABLE; } + if (unlikely((fpi_flags & FPI_TRYLOCK) && IS_ENABLED(CONFIG_PREEMPT_RT) + && (in_nmi() || in_hardirq()))) { + add_page_to_zone_llist(zone, page, order); + return; + } pcp_trylock_prepare(UP_flags); pcp = pcp_spin_trylock(zone->per_cpu_pageset); if (pcp) { - free_frozen_page_commit(zone, pcp, page, migratetype, order); + free_frozen_page_commit(zone, pcp, page, migratetype, order, fpi_flags); pcp_spin_unlock(pcp); } else { - free_one_page(zone, page, pfn, order, FPI_NONE); + free_one_page(zone, page, pfn, order, fpi_flags); } pcp_trylock_finish(UP_flags); } +void free_frozen_pages(struct page *page, unsigned int order) +{ + __free_frozen_pages(page, order, FPI_NONE); +} + /* * Free a batch of folios */ @@ -2780,7 +2833,7 @@ void free_unref_folios(struct folio_batch *folios) trace_mm_page_free_batched(&folio->page); free_frozen_page_commit(zone, pcp, &folio->page, migratetype, - order); + order, FPI_NONE); } if (pcp) { @@ -4841,22 +4894,37 @@ EXPORT_SYMBOL(get_zeroed_page_noprof); * Context: May be called in interrupt context or while holding a normal * spinlock, but not in NMI context or while holding a raw spinlock. */ -void __free_pages(struct page *page, unsigned int order) +static void ___free_pages(struct page *page, unsigned int order, + fpi_t fpi_flags) { /* get PageHead before we drop reference */ int head = PageHead(page); struct alloc_tag *tag = pgalloc_tag_get(page); if (put_page_testzero(page)) - free_frozen_pages(page, order); + __free_frozen_pages(page, order, fpi_flags); else if (!head) { pgalloc_tag_sub_pages(tag, (1 << order) - 1); while (order-- > 0) - free_frozen_pages(page + (1 << order), order); + __free_frozen_pages(page + (1 << order), order, + fpi_flags); } } +void __free_pages(struct page *page, unsigned int order) +{ + ___free_pages(page, order, FPI_NONE); +} EXPORT_SYMBOL(__free_pages); +/* + * Can be called while holding raw_spin_lock or from IRQ and NMI for any + * page type (not only those that came from try_alloc_pages) + */ +void free_pages_nolock(struct page *page, unsigned int order) +{ + ___free_pages(page, order, FPI_TRYLOCK); +} + void free_pages(unsigned long addr, unsigned int order) { if (addr != 0) { diff --git a/mm/page_owner.c b/mm/page_owner.c index 2d6360eaccbb..90e31d0e3ed7 100644 --- a/mm/page_owner.c +++ b/mm/page_owner.c @@ -294,7 +294,13 @@ void __reset_page_owner(struct page *page, unsigned short order) page_owner = get_page_owner(page_ext); alloc_handle = page_owner->handle; - handle = save_stack(GFP_NOWAIT | __GFP_NOWARN); + /* + * Do not specify GFP_NOWAIT to make gfpflags_allow_spinning() == false + * to prevent issues in stack_depot_save(). + * This is similar to try_alloc_pages() gfp flags, but only used + * to signal stack_depot to avoid spin_locks. + */ + handle = save_stack(__GFP_NOWARN); __update_page_owner_free_handle(page_ext, handle, order, current->pid, current->tgid, free_ts_nsec); page_ext_put(page_ext); From patchwork Thu Feb 13 03:35:53 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexei Starovoitov X-Patchwork-Id: 13972743 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-pl1-f173.google.com (mail-pl1-f173.google.com [209.85.214.173]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EB129204F72 for ; Thu, 13 Feb 2025 03:36:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739417772; cv=none; b=mqHzAaBqcdke0ZCy7dtoxO8yl8NW/WVDo3L5CyL4/d2Riy6pBVr258yjaBuVBioilcQm0kaTG7opeLouS35IXoG84JOUZ9NYgx9GFQEBF8xmkGAJHdPJKIrTGM5oEBMgghftX3X3g70jJAeVJuvc18HMkzoQGqiyjWbioRUbPto= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739417772; c=relaxed/simple; bh=U90v5Pk93VeiqRlGaWd3w3TfoItyJk0Pr1aSpT1G4VI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=HGg/qEbgOCQfQoZuwcknPrI59AlKpgKtSRx5Z+HA0opHF+ab81bDewngj3+CmDIxJPlIp8bnddJR1mefgUeZSYQv9XT4kncZQ6uQr1WyIkaGsUeBjnD/GwgD+F7yL6EeXo+IPMNMnVAPGB16hspboIWwzz+J00+TBlCqNkokyPM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Vis1ppbf; arc=none smtp.client-ip=209.85.214.173 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Vis1ppbf" Received: by mail-pl1-f173.google.com with SMTP id d9443c01a7336-220c8f38febso6286095ad.2 for ; Wed, 12 Feb 2025 19:36:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1739417769; x=1740022569; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Dxu1Rc7/sI72JB4NYxxk9elaSjOiJ9Td89bVV/zMYyc=; b=Vis1ppbfARabHZAvNLFq9MM/+3FV38KUwbcOxPsLIw0E0bOil6y09ecYBWb4bn7hov JlXJW66LQjrepLqtmu+POOcHpW79nVgoiGOQOvSFK0blBKrUaJ+hG8NqsHDB6zAW/lqW ke9b8t/DkiPkqRYV/IVm/qNrVz+eaWMby4QrBQtNGvNqMBeSDg0Pgfx4w0UWeFtEq9UL H/5KWZr0APl209vnJl8o8qJ8WxaLJ0rYFTJzu+KHKV6+eDCHNh2/P+JmnugYm4KAEGyl HK1VJCJo2OwFdrpdtTeDTNSJBAW7MCCVVZj3faykjZqulhQo27Qj6qzBIp/KA0gkvAlD tVnA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739417769; x=1740022569; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Dxu1Rc7/sI72JB4NYxxk9elaSjOiJ9Td89bVV/zMYyc=; b=BuepztY1F87YOEe5QEvtkW/Tmush3g71ghsn3mK2s5vB4d/HxKNj3LUFW4JCAh8a51 nPb3Xlyb0AkxYQQzq1f75/QTp153SdCRYQOimsi88brnjKbnrfsOXyeKX0BtR1JqVBTB n4M7zvIBfOgwsSQZqTfwjsvw7K7U9aP1uNVIBTHIR/2VxonuSIG8MwL8XHHAPLAngwmg x027L+lURa4309mgZD4pZ8kx2kVcPsfVJZ7W7zludPcfRISSniKA+y00psUesmNJmHbm 41S4QRdw/RwjLUvg9h38SW9wOdtqDwnBsKprz+5K/i/vGYd3crC3tZD3JfB57Ad72lrU d4JA== X-Gm-Message-State: AOJu0YxpT4qgX41xyjGJGITNIwdjteedlf2FNqiVBMEGmgHugWikiQeU LQiWRHwdZfYy9uFzGY3PJmvW/+zzX9cwU5cWhaxIIcReYIhPXjYGqQxjpg== X-Gm-Gg: ASbGncsN6aEdS6WKJoQaGjrcnk3Kp+ZanE3o/2CrIcPdriv3da2JpdQTr1XFhseM/lX UFAiW0ya+UfyY5ch4dFVFxr+PTU0gAPadUXjZIADH7L9sc7ej7S7PA7CL3TcSeBVQYDiy3CnlEc 7iBrx3zI2DcXpji5WIGliDpWjyl3utI5s6uNVzA62vzSOZJ7J7ifE1Keh57H2qdA61thZ3rB0My SB8i1IxUXrsnkwY1F5YNuWiHP4MFVseCj5+vq38INGb9dU9FE6/TfkLhV8TTFW/mcYzFWZgVcRn V7vvnUJ2jRz2yzuOFnoO0IyrcJZ7d4PT15NNwnrvi7jkSy0NjQ== X-Google-Smtp-Source: AGHT+IF0oyIrki3EN7feLFgHfxItX3wzuThKb7cJwiTjjSoOQ/uGuke17pktF4EceH+U1SvptjTlvw== X-Received: by 2002:a17:902:c94b:b0:220:d1c3:2511 with SMTP id d9443c01a7336-220d1c327f5mr24489425ad.26.1739417769475; Wed, 12 Feb 2025 19:36:09 -0800 (PST) Received: from macbookpro.lan ([2603:3023:16e:5000:8af:ecd2:44cd:8027]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-220d556d6d2sm2693595ad.177.2025.02.12.19.36.08 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Wed, 12 Feb 2025 19:36:09 -0800 (PST) From: Alexei Starovoitov To: bpf@vger.kernel.org Cc: andrii@kernel.org, memxor@gmail.com, akpm@linux-foundation.org, peterz@infradead.org, vbabka@suse.cz, bigeasy@linutronix.de, rostedt@goodmis.org, houtao1@huawei.com, hannes@cmpxchg.org, shakeel.butt@linux.dev, mhocko@suse.com, willy@infradead.org, tglx@linutronix.de, jannh@google.com, tj@kernel.org, linux-mm@kvack.org, kernel-team@fb.com Subject: [PATCH bpf-next v8 3/6] locking/local_lock: Introduce localtry_lock_t Date: Wed, 12 Feb 2025 19:35:53 -0800 Message-Id: <20250213033556.9534-4-alexei.starovoitov@gmail.com> X-Mailer: git-send-email 2.39.5 (Apple Git-154) In-Reply-To: <20250213033556.9534-1-alexei.starovoitov@gmail.com> References: <20250213033556.9534-1-alexei.starovoitov@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net From: Sebastian Andrzej Siewior In !PREEMPT_RT local_lock_irqsave() disables interrupts to protect critical section, but it doesn't prevent NMI, so the fully reentrant code cannot use local_lock_irqsave() for exclusive access. Introduce localtry_lock_t and localtry_lock_irqsave() that disables interrupts and sets acquired=1, so localtry_lock_irqsave() from NMI attempting to acquire the same lock will return false. In PREEMPT_RT local_lock_irqsave() maps to preemptible spin_lock(). Map localtry_lock_irqsave() to preemptible spin_trylock(). When in hard IRQ or NMI return false right away, since spin_trylock() is not safe due to PI issues. Note there is no need to use local_inc for acquired variable, since it's a percpu variable with strict nesting scopes. Signed-off-by: Sebastian Andrzej Siewior Signed-off-by: Alexei Starovoitov Signed-off-by: Vlastimil Babka Acked-by: Vlastimil Babka --- include/linux/local_lock.h | 59 +++++++++++++ include/linux/local_lock_internal.h | 123 ++++++++++++++++++++++++++++ 2 files changed, 182 insertions(+) diff --git a/include/linux/local_lock.h b/include/linux/local_lock.h index 091dc0b6bdfb..05c254a5d7d3 100644 --- a/include/linux/local_lock.h +++ b/include/linux/local_lock.h @@ -51,6 +51,65 @@ #define local_unlock_irqrestore(lock, flags) \ __local_unlock_irqrestore(lock, flags) +/** + * localtry_lock_init - Runtime initialize a lock instance + */ +#define localtry_lock_init(lock) __localtry_lock_init(lock) + +/** + * localtry_lock - Acquire a per CPU local lock + * @lock: The lock variable + */ +#define localtry_lock(lock) __localtry_lock(lock) + +/** + * localtry_lock_irq - Acquire a per CPU local lock and disable interrupts + * @lock: The lock variable + */ +#define localtry_lock_irq(lock) __localtry_lock_irq(lock) + +/** + * localtry_lock_irqsave - Acquire a per CPU local lock, save and disable + * interrupts + * @lock: The lock variable + * @flags: Storage for interrupt flags + */ +#define localtry_lock_irqsave(lock, flags) \ + __localtry_lock_irqsave(lock, flags) + +/** + * localtry_trylock_irqsave - Try to acquire a per CPU local lock, save and disable + * interrupts if acquired + * @lock: The lock variable + * @flags: Storage for interrupt flags + * + * The function can be used in any context such as NMI or HARDIRQ. Due to + * locking constrains it will _always_ fail to acquire the lock on PREEMPT_RT. + */ +#define localtry_trylock_irqsave(lock, flags) \ + __localtry_trylock_irqsave(lock, flags) + +/** + * local_unlock - Release a per CPU local lock + * @lock: The lock variable + */ +#define localtry_unlock(lock) __localtry_unlock(lock) + +/** + * local_unlock_irq - Release a per CPU local lock and enable interrupts + * @lock: The lock variable + */ +#define localtry_unlock_irq(lock) __localtry_unlock_irq(lock) + +/** + * localtry_unlock_irqrestore - Release a per CPU local lock and restore + * interrupt flags + * @lock: The lock variable + * @flags: Interrupt flags to restore + */ +#define localtry_unlock_irqrestore(lock, flags) \ + __localtry_unlock_irqrestore(lock, flags) + DEFINE_GUARD(local_lock, local_lock_t __percpu*, local_lock(_T), local_unlock(_T)) diff --git a/include/linux/local_lock_internal.h b/include/linux/local_lock_internal.h index 8dd71fbbb6d2..c1369b300777 100644 --- a/include/linux/local_lock_internal.h +++ b/include/linux/local_lock_internal.h @@ -15,6 +15,11 @@ typedef struct { #endif } local_lock_t; +typedef struct { + local_lock_t llock; + unsigned int acquired; +} localtry_lock_t; + #ifdef CONFIG_DEBUG_LOCK_ALLOC # define LOCAL_LOCK_DEBUG_INIT(lockname) \ .dep_map = { \ @@ -31,6 +36,13 @@ static inline void local_lock_acquire(local_lock_t *l) l->owner = current; } +static inline void local_trylock_acquire(local_lock_t *l) +{ + lock_map_acquire_try(&l->dep_map); + DEBUG_LOCKS_WARN_ON(l->owner); + l->owner = current; +} + static inline void local_lock_release(local_lock_t *l) { DEBUG_LOCKS_WARN_ON(l->owner != current); @@ -45,11 +57,13 @@ static inline void local_lock_debug_init(local_lock_t *l) #else /* CONFIG_DEBUG_LOCK_ALLOC */ # define LOCAL_LOCK_DEBUG_INIT(lockname) static inline void local_lock_acquire(local_lock_t *l) { } +static inline void local_trylock_acquire(local_lock_t *l) { } static inline void local_lock_release(local_lock_t *l) { } static inline void local_lock_debug_init(local_lock_t *l) { } #endif /* !CONFIG_DEBUG_LOCK_ALLOC */ #define INIT_LOCAL_LOCK(lockname) { LOCAL_LOCK_DEBUG_INIT(lockname) } +#define INIT_LOCALTRY_LOCK(lockname) { .llock = { LOCAL_LOCK_DEBUG_INIT(lockname.llock) }} #define __local_lock_init(lock) \ do { \ @@ -118,6 +132,86 @@ do { \ #define __local_unlock_nested_bh(lock) \ local_lock_release(this_cpu_ptr(lock)) +/* localtry_lock_t variants */ + +#define __localtry_lock_init(lock) \ +do { \ + __local_lock_init(&(lock)->llock); \ + WRITE_ONCE(&(lock)->acquired, 0); \ +} while (0) + +#define __localtry_lock(lock) \ + do { \ + localtry_lock_t *lt; \ + preempt_disable(); \ + lt = this_cpu_ptr(lock); \ + local_lock_acquire(<->llock); \ + WRITE_ONCE(lt->acquired, 1); \ + } while (0) + +#define __localtry_lock_irq(lock) \ + do { \ + localtry_lock_t *lt; \ + local_irq_disable(); \ + lt = this_cpu_ptr(lock); \ + local_lock_acquire(<->llock); \ + WRITE_ONCE(lt->acquired, 1); \ + } while (0) + +#define __localtry_lock_irqsave(lock, flags) \ + do { \ + localtry_lock_t *lt; \ + local_irq_save(flags); \ + lt = this_cpu_ptr(lock); \ + local_lock_acquire(<->llock); \ + WRITE_ONCE(lt->acquired, 1); \ + } while (0) + +#define __localtry_trylock_irqsave(lock, flags) \ + ({ \ + localtry_lock_t *lt; \ + bool _ret; \ + \ + local_irq_save(flags); \ + lt = this_cpu_ptr(lock); \ + if (!READ_ONCE(lt->acquired)) { \ + WRITE_ONCE(lt->acquired, 1); \ + local_trylock_acquire(<->llock); \ + _ret = true; \ + } else { \ + _ret = false; \ + local_irq_restore(flags); \ + } \ + _ret; \ + }) + +#define __localtry_unlock(lock) \ + do { \ + localtry_lock_t *lt; \ + lt = this_cpu_ptr(lock); \ + WRITE_ONCE(lt->acquired, 0); \ + local_lock_release(<->llock); \ + preempt_enable(); \ + } while (0) + +#define __localtry_unlock_irq(lock) \ + do { \ + localtry_lock_t *lt; \ + lt = this_cpu_ptr(lock); \ + WRITE_ONCE(lt->acquired, 0); \ + local_lock_release(<->llock); \ + local_irq_enable(); \ + } while (0) + +#define __localtry_unlock_irqrestore(lock, flags) \ + do { \ + localtry_lock_t *lt; \ + lt = this_cpu_ptr(lock); \ + WRITE_ONCE(lt->acquired, 0); \ + local_lock_release(<->llock); \ + local_irq_restore(flags); \ + } while (0) + #else /* !CONFIG_PREEMPT_RT */ /* @@ -125,8 +219,10 @@ do { \ * critical section while staying preemptible. */ typedef spinlock_t local_lock_t; +typedef spinlock_t localtry_lock_t; #define INIT_LOCAL_LOCK(lockname) __LOCAL_SPIN_LOCK_UNLOCKED((lockname)) +#define INIT_LOCALTRY_LOCK(lockname) INIT_LOCAL_LOCK(lockname) #define __local_lock_init(l) \ do { \ @@ -169,4 +265,31 @@ do { \ spin_unlock(this_cpu_ptr((lock))); \ } while (0) +/* localtry_lock_t variants */ + +#define __localtry_lock_init(lock) __local_lock_init(lock) +#define __localtry_lock(lock) __local_lock(lock) +#define __localtry_lock_irq(lock) __local_lock(lock) +#define __localtry_lock_irqsave(lock, flags) __local_lock_irqsave(lock, flags) +#define __localtry_unlock(lock) __local_unlock(lock) +#define __localtry_unlock_irq(lock) __local_unlock(lock) +#define __localtry_unlock_irqrestore(lock, flags) __local_unlock_irqrestore(lock, flags) + +#define __localtry_trylock_irqsave(lock, flags) \ + ({ \ + int __locked; \ + \ + typecheck(unsigned long, flags); \ + flags = 0; \ + if (in_nmi() | in_hardirq()) { \ + __locked = 0; \ + } else { \ + migrate_disable(); \ + __locked = spin_trylock(this_cpu_ptr((lock))); \ + if (!__locked) \ + migrate_enable(); \ + } \ + __locked; \ + }) + #endif /* CONFIG_PREEMPT_RT */ From patchwork Thu Feb 13 03:35:54 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexei Starovoitov X-Patchwork-Id: 13972744 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-pl1-f179.google.com (mail-pl1-f179.google.com [209.85.214.179]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5D381204C30 for ; Thu, 13 Feb 2025 03:36:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739417776; cv=none; b=DHK6OzEpdFEc62y+4DY2nq7vaQzT6tOlsw16M1tHDTTicwX04y8XBtywOUuEXB2M56JfTgg8qxokIVSqTDBf8pi3d/gHNYI6de0YBC7FKkZ3dNTNvMCz+Yl9xQP/q+eTgBZciHOyPOgERo7fjbREc4H5j8Av7OXdAU5oLoB+FPY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739417776; c=relaxed/simple; bh=ZC2g15cWBHl4xKK3l6VY+Bv903v1M/rGvWQlFD6bb8Y=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=jOh0mNKhd9LDnoOPUyLL7sYDgggczbSMdnhBxvco64FVmYZ8JuatL7h9j2SDM9nhHjZ3w1/TL0I29Ls3EX/t7jVd9gYVlSf+IqhEg7gwyrUA/D85hqt9a1pWUjH5wx0D8Kwkq40I1CP/XOm2RW1Fxe1MrkL6ufFgI+XVWYOfrj4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=kep3VY5Q; arc=none smtp.client-ip=209.85.214.179 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="kep3VY5Q" Received: by mail-pl1-f179.google.com with SMTP id d9443c01a7336-21f818a980cso5066475ad.3 for ; Wed, 12 Feb 2025 19:36:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1739417773; x=1740022573; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=UVEemv3fH2Y13Oyfgyz54ddroh/hFIT8TR6MrFMiG5U=; b=kep3VY5QH5G0XU6BUicy7bhB4GYVo5XkAnhnB2uaOmLTHyDrxYyVkZ6bprQ06aDjxQ klUhn5wb4GFJIXwRm2rAOnIdcwauC6lPulNBFMOrGFtmTfpXcI943pDRUtHlEn/tyufb KLjQGGDbOlw4UcnhMEY2RfUzq8TJajmAZvQwVcciEDNW7IiGWu3TUimWyGDc2C/q40gC nXE1EP4A5sO51JgnZdjMp6D/Q67nUZB1orrYxev0eil0yxfR8UUGOlMhncPi+uoMA/Y6 GjM5JeqU3AvE09LmrElSQY5/BF6/GfUpnRji3GbWrhmCgJs9xcmG2nStf+OzQWA+7bAW onog== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739417773; x=1740022573; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=UVEemv3fH2Y13Oyfgyz54ddroh/hFIT8TR6MrFMiG5U=; b=NzhpraGZqOnbHrKXgfuV7g4dsUjHoV0ohg6tVrxAynd+/qctAvfmt5DPtMx1TNth7Z C1y2PwJg/pI+e1BGuGJYGzXAnVitVJQQV9gpQdmIMRDEmWPFMv3QfceBeZKqA94qQNbU gbAX5v10L9M65fqZ31/phwCIu8ua8FoPsccH8QbBpoCFOtfwXS7zpaZkPKWGH2VqYZ6f H0EL8pxSVnuUYR0N82yVHddiof3anFxL+qIrdEo1hiCgleSwnFIMx98pkd3YxPtJFldv bjySGAJFmO2M+HG8yog+TCP+qfx67u5upX0xcV5Z17vGzD3x3LnWI970oC/SaL71yvSg dKAA== X-Gm-Message-State: AOJu0YxtVQd0+A6CeLUxawKQGC23oWi0Zb63uYLN5M+WnQbSiCJ/7ZjH sflHdZfam3dTzQACtkSIIf1dFCFLVA72SB3Na1yXRygn9qQd9zhQMElXIA== X-Gm-Gg: ASbGnctQ+iGyUQAXxgYzHcqt13wNLwarF20RA0IoElfL8v5Z9iwULN1zyGDgvJOkaKx Vdb/xIerQigUodZTUg3LPkNTfqmukuno6ytZlCzwjBzDKa56rbiZfkJWJVzzvv4iLtQuqJKPYUv LA7FRLFmBLwvx0yCjQG4DNbpqAV8x9hjoaPPFRQLabpsvLZcG2pCw4eP/wcQwtZp28bjl9n12Ri B5biEa37X8LD6KvoaGXqgIh8Qgv8HpdK4B4MCYJxWX1kLxs2T71Z3I9S28m7AczJqbjP1bHFo6j CI66KA9kiv00oeZnH53E8GPUJYUx++VGavhufjgDPoMDIqRiCw== X-Google-Smtp-Source: AGHT+IFVpGjNj/vzK1bi6WsgsmwnS7KOtENZ1Nu6FIVShyFexL6xf+cWsUdEjky1AdkIY4WdMWXaMw== X-Received: by 2002:a17:902:c942:b0:216:386e:dbc with SMTP id d9443c01a7336-220bbacbb1amr78015995ad.13.1739417772747; Wed, 12 Feb 2025 19:36:12 -0800 (PST) Received: from macbookpro.lan ([2603:3023:16e:5000:8af:ecd2:44cd:8027]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-220d556d5e6sm2691755ad.173.2025.02.12.19.36.11 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Wed, 12 Feb 2025 19:36:12 -0800 (PST) From: Alexei Starovoitov To: bpf@vger.kernel.org Cc: andrii@kernel.org, memxor@gmail.com, akpm@linux-foundation.org, peterz@infradead.org, vbabka@suse.cz, bigeasy@linutronix.de, rostedt@goodmis.org, houtao1@huawei.com, hannes@cmpxchg.org, shakeel.butt@linux.dev, mhocko@suse.com, willy@infradead.org, tglx@linutronix.de, jannh@google.com, tj@kernel.org, linux-mm@kvack.org, kernel-team@fb.com Subject: [PATCH bpf-next v8 4/6] memcg: Use trylock to access memcg stock_lock. Date: Wed, 12 Feb 2025 19:35:54 -0800 Message-Id: <20250213033556.9534-5-alexei.starovoitov@gmail.com> X-Mailer: git-send-email 2.39.5 (Apple Git-154) In-Reply-To: <20250213033556.9534-1-alexei.starovoitov@gmail.com> References: <20250213033556.9534-1-alexei.starovoitov@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net From: Alexei Starovoitov Teach memcg to operate under trylock conditions when spinning locks cannot be used. localtry_trylock might fail and this would lead to charge cache bypass if the calling context doesn't allow spinning (gfpflags_allow_spinning). In those cases charge the memcg counter directly and fail early if that is not possible. This might cause a pre-mature charge failing but it will allow an opportunistic charging that is safe from try_alloc_pages path. Acked-by: Michal Hocko Acked-by: Vlastimil Babka Acked-by: Shakeel Butt Signed-off-by: Alexei Starovoitov --- mm/memcontrol.c | 52 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 36 insertions(+), 16 deletions(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 46f8b372d212..7587511b92cc 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -1739,7 +1739,7 @@ void mem_cgroup_print_oom_group(struct mem_cgroup *memcg) } struct memcg_stock_pcp { - local_lock_t stock_lock; + localtry_lock_t stock_lock; struct mem_cgroup *cached; /* this never be root cgroup */ unsigned int nr_pages; @@ -1754,7 +1754,7 @@ struct memcg_stock_pcp { #define FLUSHING_CACHED_CHARGE 0 }; static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock) = { - .stock_lock = INIT_LOCAL_LOCK(stock_lock), + .stock_lock = INIT_LOCALTRY_LOCK(stock_lock), }; static DEFINE_MUTEX(percpu_charge_mutex); @@ -1773,7 +1773,8 @@ static bool obj_stock_flush_required(struct memcg_stock_pcp *stock, * * returns true if successful, false otherwise. */ -static bool consume_stock(struct mem_cgroup *memcg, unsigned int nr_pages) +static bool consume_stock(struct mem_cgroup *memcg, unsigned int nr_pages, + gfp_t gfp_mask) { struct memcg_stock_pcp *stock; unsigned int stock_pages; @@ -1783,7 +1784,11 @@ static bool consume_stock(struct mem_cgroup *memcg, unsigned int nr_pages) if (nr_pages > MEMCG_CHARGE_BATCH) return ret; - local_lock_irqsave(&memcg_stock.stock_lock, flags); + if (!localtry_trylock_irqsave(&memcg_stock.stock_lock, flags)) { + if (!gfpflags_allow_spinning(gfp_mask)) + return ret; + localtry_lock_irqsave(&memcg_stock.stock_lock, flags); + } stock = this_cpu_ptr(&memcg_stock); stock_pages = READ_ONCE(stock->nr_pages); @@ -1792,7 +1797,7 @@ static bool consume_stock(struct mem_cgroup *memcg, unsigned int nr_pages) ret = true; } - local_unlock_irqrestore(&memcg_stock.stock_lock, flags); + localtry_unlock_irqrestore(&memcg_stock.stock_lock, flags); return ret; } @@ -1831,14 +1836,14 @@ static void drain_local_stock(struct work_struct *dummy) * drain_stock races is that we always operate on local CPU stock * here with IRQ disabled */ - local_lock_irqsave(&memcg_stock.stock_lock, flags); + localtry_lock_irqsave(&memcg_stock.stock_lock, flags); stock = this_cpu_ptr(&memcg_stock); old = drain_obj_stock(stock); drain_stock(stock); clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags); - local_unlock_irqrestore(&memcg_stock.stock_lock, flags); + localtry_unlock_irqrestore(&memcg_stock.stock_lock, flags); obj_cgroup_put(old); } @@ -1868,9 +1873,20 @@ static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages) { unsigned long flags; - local_lock_irqsave(&memcg_stock.stock_lock, flags); + if (!localtry_trylock_irqsave(&memcg_stock.stock_lock, flags)) { + /* + * In case of unlikely failure to lock percpu stock_lock + * uncharge memcg directly. + */ + if (mem_cgroup_is_root(memcg)) + return; + page_counter_uncharge(&memcg->memory, nr_pages); + if (do_memsw_account()) + page_counter_uncharge(&memcg->memsw, nr_pages); + return; + } __refill_stock(memcg, nr_pages); - local_unlock_irqrestore(&memcg_stock.stock_lock, flags); + localtry_unlock_irqrestore(&memcg_stock.stock_lock, flags); } /* @@ -2213,9 +2229,13 @@ int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask, unsigned long pflags; retry: - if (consume_stock(memcg, nr_pages)) + if (consume_stock(memcg, nr_pages, gfp_mask)) return 0; + if (!gfpflags_allow_spinning(gfp_mask)) + /* Avoid the refill and flush of the older stock */ + batch = nr_pages; + if (!do_memsw_account() || page_counter_try_charge(&memcg->memsw, batch, &counter)) { if (page_counter_try_charge(&memcg->memory, batch, &counter)) @@ -2699,7 +2719,7 @@ static void mod_objcg_state(struct obj_cgroup *objcg, struct pglist_data *pgdat, unsigned long flags; int *bytes; - local_lock_irqsave(&memcg_stock.stock_lock, flags); + localtry_lock_irqsave(&memcg_stock.stock_lock, flags); stock = this_cpu_ptr(&memcg_stock); /* @@ -2752,7 +2772,7 @@ static void mod_objcg_state(struct obj_cgroup *objcg, struct pglist_data *pgdat, if (nr) __mod_objcg_mlstate(objcg, pgdat, idx, nr); - local_unlock_irqrestore(&memcg_stock.stock_lock, flags); + localtry_unlock_irqrestore(&memcg_stock.stock_lock, flags); obj_cgroup_put(old); } @@ -2762,7 +2782,7 @@ static bool consume_obj_stock(struct obj_cgroup *objcg, unsigned int nr_bytes) unsigned long flags; bool ret = false; - local_lock_irqsave(&memcg_stock.stock_lock, flags); + localtry_lock_irqsave(&memcg_stock.stock_lock, flags); stock = this_cpu_ptr(&memcg_stock); if (objcg == READ_ONCE(stock->cached_objcg) && stock->nr_bytes >= nr_bytes) { @@ -2770,7 +2790,7 @@ static bool consume_obj_stock(struct obj_cgroup *objcg, unsigned int nr_bytes) ret = true; } - local_unlock_irqrestore(&memcg_stock.stock_lock, flags); + localtry_unlock_irqrestore(&memcg_stock.stock_lock, flags); return ret; } @@ -2862,7 +2882,7 @@ static void refill_obj_stock(struct obj_cgroup *objcg, unsigned int nr_bytes, unsigned long flags; unsigned int nr_pages = 0; - local_lock_irqsave(&memcg_stock.stock_lock, flags); + localtry_lock_irqsave(&memcg_stock.stock_lock, flags); stock = this_cpu_ptr(&memcg_stock); if (READ_ONCE(stock->cached_objcg) != objcg) { /* reset if necessary */ @@ -2880,7 +2900,7 @@ static void refill_obj_stock(struct obj_cgroup *objcg, unsigned int nr_bytes, stock->nr_bytes &= (PAGE_SIZE - 1); } - local_unlock_irqrestore(&memcg_stock.stock_lock, flags); + localtry_unlock_irqrestore(&memcg_stock.stock_lock, flags); obj_cgroup_put(old); if (nr_pages) From patchwork Thu Feb 13 03:35:55 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexei Starovoitov X-Patchwork-Id: 13972745 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E665A2046A0 for ; Thu, 13 Feb 2025 03:36:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739417778; cv=none; b=feHZqjKQVlWxyOEnsz2fmWSoZ/bSqQPGlVBCzNqfWIAUTpQ5lFWLtVWyaWYA1mRzsGkQi0VMCZzfv2307ahEI/WuAJoS1ci/mEuXL2cb1w/ElASYMhRRQo16DG0jKZ12XksTbcAqwoeZqf7HCOTAhiTJ/1+FAPClayjhcj0CWpg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739417778; c=relaxed/simple; bh=OXvxRRQ0ldJk6tf+xlfHzw5zM51xSwL/MeJ06bSRpIM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Lo+e28yaJ8Ze3cdpNA5HFHyyA6gykJ6tD4CM/NSj5+5bTRmk2wqBr8siL9fPn19PZX3cuSKSFPkhZtBJyre/J8LOyuxWUBCfblrAImG6dSzEy0BOcHyRGycr7hgI4Taqafgy7Lx85/PWttD82jL/XJAB9R+NW1h88Nn0SV/rzsc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=TenZdNtw; arc=none smtp.client-ip=209.85.214.174 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="TenZdNtw" Received: by mail-pl1-f174.google.com with SMTP id d9443c01a7336-21f49bd087cso5581195ad.0 for ; Wed, 12 Feb 2025 19:36:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1739417776; x=1740022576; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=3/v8WJgHJaSQyAqUJ963JkPt7Jj2HKJav5PT9g4jqQk=; b=TenZdNtwAO2kqAzMYvAiV1OquEFziIZ49gD+LTV5o6nZlS4cz3OQiCty26Hgw+vFtb /kCwouPaOh3CVNnvPdfb22MjqyBPBiNyRoBVsGJZMfQjrqDz1kSR+LDYRW2KNcT352me l4bPia4dL2FixxlD3kbWAjpRShSHl5cSlqrrS90lrdDwkcdyEo9Bi0o0OHrL2Bw+pHNn 5W3l9ZRu5xoNdImCjkH01ATH/OrQjXczwcbhBTAcAFGt2bytJ0M/9TvVTr1KIs3uQbwy SdCn7+/qWpel3FBW2GTbd3oHKb0NWQoBxYfK7KHe1I/d6DnnpKgslv0MJJzjRTb+Xb1e NxAg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739417776; x=1740022576; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=3/v8WJgHJaSQyAqUJ963JkPt7Jj2HKJav5PT9g4jqQk=; b=WvT6IQJZTGfH4xh0j4V1VmY/72dUTBKgL4TiR1Z+KjayBFx7HK5yCOjpZkvnIpH1yX OosfsfJx6RFRwDkuBuPO/MH9zoOeUDDoIpitfanLtSS7W09m9gE/2slhub0HC1WiYtu3 npc5wD4OSbc1/wKo0c4Q/AHTg/BUhz+cFS8UXRlECC/2ARX5T5DzF8ZSznWVzqhVzlUy udWf1BmNA+jf/HdWMkPiy49B4mZRvuF07k6htL/m3yrVUZF5XbMXcPbvGoFFKk+67skl TdXZsMtAhUJuwoznfAzqAdUodqvF1kz7pZqxBBISHBIHW8GpKuhMUHFgw0gkKkQM/rxL ysFQ== X-Gm-Message-State: AOJu0YzqDQwlq0kdMmu6mzUl+vvtazLNOzA58UBNGg13PHDretuuqgMi pmoqXVDPrMEcd8vsyuZXI+VH8vCkLUuU6x3OwPRTMvpHG5/Gn7fouK9sqg== X-Gm-Gg: ASbGncvwcrCXohOQzLvpTd0hUsZnG9S0KVDMQGDCsha34q3a4oz3kZ/ZCE6f/PzEaNn HXiRZKedduaATHxLPaKtrsgNZLjcY/dl8kd9EtjBTgzpMtjuRsTPQ6qY6v0Lu4X+1eL9kVpJ9tq CbWVUWpLM/mO6nUmOSRgOeAJdqIDDjeu1IELQaApX9t/PdkrnqlVCwRpbwD5zh0PhkCindPcw+J d7fh7zOYQsutRv1nQVXqIBqNc49tbI4OhEyTHp7bSfOsWGG1JZ7egtYmGuqDLnAm9LysKT8OR0G QDd3wP9vD3CjxszZlGeya3dYh22OQCQxYYU9N3Nl85AYH8DWnQ== X-Google-Smtp-Source: AGHT+IHexcsWqcq7pb6cql8+KDie7vvimoWi/+7jiQiVYLknknAkaUVO0a4xFfFcJbi5waug/L/YFQ== X-Received: by 2002:a05:6a20:4311:b0:1eb:48e2:2c2f with SMTP id adf61e73a8af0-1ee6b399c55mr3608181637.30.1739417775940; Wed, 12 Feb 2025 19:36:15 -0800 (PST) Received: from macbookpro.lan ([2603:3023:16e:5000:8af:ecd2:44cd:8027]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7324256aa6asm227563b3a.60.2025.02.12.19.36.14 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Wed, 12 Feb 2025 19:36:15 -0800 (PST) From: Alexei Starovoitov To: bpf@vger.kernel.org Cc: andrii@kernel.org, memxor@gmail.com, akpm@linux-foundation.org, peterz@infradead.org, vbabka@suse.cz, bigeasy@linutronix.de, rostedt@goodmis.org, houtao1@huawei.com, hannes@cmpxchg.org, shakeel.butt@linux.dev, mhocko@suse.com, willy@infradead.org, tglx@linutronix.de, jannh@google.com, tj@kernel.org, linux-mm@kvack.org, kernel-team@fb.com Subject: [PATCH bpf-next v8 5/6] mm, bpf: Use memcg in try_alloc_pages(). Date: Wed, 12 Feb 2025 19:35:55 -0800 Message-Id: <20250213033556.9534-6-alexei.starovoitov@gmail.com> X-Mailer: git-send-email 2.39.5 (Apple Git-154) In-Reply-To: <20250213033556.9534-1-alexei.starovoitov@gmail.com> References: <20250213033556.9534-1-alexei.starovoitov@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net From: Alexei Starovoitov Unconditionally use __GFP_ACCOUNT in try_alloc_pages(). The caller is responsible to setup memcg correctly. All BPF memory accounting is memcg based. Acked-by: Vlastimil Babka Acked-by: Shakeel Butt Signed-off-by: Alexei Starovoitov --- mm/page_alloc.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/mm/page_alloc.c b/mm/page_alloc.c index 3fbcbeb7de8e..c8068fd2da42 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -7189,7 +7189,8 @@ struct page *try_alloc_pages_noprof(int nid, unsigned int order) * specify it here to highlight that try_alloc_pages() * doesn't want to deplete reserves. */ - gfp_t alloc_gfp = __GFP_NOWARN | __GFP_ZERO | __GFP_NOMEMALLOC; + gfp_t alloc_gfp = __GFP_NOWARN | __GFP_ZERO | __GFP_NOMEMALLOC + | __GFP_ACCOUNT; unsigned int alloc_flags = ALLOC_TRYLOCK; struct alloc_context ac = { }; struct page *page; @@ -7233,6 +7234,11 @@ struct page *try_alloc_pages_noprof(int nid, unsigned int order) /* Unlike regular alloc_pages() there is no __alloc_pages_slowpath(). */ + if (memcg_kmem_online() && page && + unlikely(__memcg_kmem_charge_page(page, alloc_gfp, order) != 0)) { + free_pages_nolock(page, order); + page = NULL; + } trace_mm_page_alloc(page, order, alloc_gfp, ac.migratetype); kmsan_alloc_page(page, order, alloc_gfp); return page; From patchwork Thu Feb 13 03:35:56 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexei Starovoitov X-Patchwork-Id: 13972746 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-pj1-f52.google.com (mail-pj1-f52.google.com [209.85.216.52]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AE4A6204C15 for ; Thu, 13 Feb 2025 03:36:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739417782; cv=none; b=qaLnlBSoweKwaSGenJzaGKBL1t+xigql5uP+LqUX+TnRFXLj1Ew1V5TxytXev4EfEo+G2pQkHzBWllY4Raqjpo6Zzy6Tyr0TMzc3pdtBrDscUqKGrpvZvzfgXuP8vHbx3dizoRwJ3NiIl/UkS1jjHG2s8iBL2XpN80YncgC9w8k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739417782; c=relaxed/simple; bh=TSxROkvhAG5IYqi3Cx7cKZkWqXvbU4Yr4NE0XE2gaU4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=JBjdri0oQ1pLMv+F6NKcVYHf4LkL6hkFsB2mWIriXwxMkKj7lzXDI9VEzkMtjt04SD9KU7bOdjpSz73+ubOcaTdFnS74Uu0xClcm2gQgvbfXO8aRB3N3g4KXBoefMB6bPs4ZM8Aj8Cz5mOlfdexcVELuRtFfonC/hvZBAvT4KvA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=jVZ/4SOR; arc=none smtp.client-ip=209.85.216.52 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="jVZ/4SOR" Received: by mail-pj1-f52.google.com with SMTP id 98e67ed59e1d1-2f9d3d0f55dso668583a91.1 for ; Wed, 12 Feb 2025 19:36:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1739417779; x=1740022579; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=1bG2TcWcZQz5k+Gqwwy79wI8/0pax/QFELT9STfGtKY=; b=jVZ/4SORs2iLH7NdWYnMFktweVrtl55YrSTaC8oyxCznNY+1CJ9qQVlXmGR7czbROs iBQz/k6/bwI+Sa7zudzuzUf/D01K3/MF7ZkppC6ZMYYBedlmz91YwEoiY0QNl9j3PIEY S/fDwVzRj48MUbxgDysSc2ffd6VC4aNirSG6Pj2/jiAnLj0gm/oQyz+8UfDRHVdkyQkG E+8rS0ffQCFYG5sf/jYgiMzHv74iv6mAmFvSzB3tvFjhBdhOGG2xzyGPX6INXWXtZRGa dfjX+0psjF6L0Lh+yDX6NUv3Jy0e36m3NxFRHcKE+e6Gz/mt5LZkN4tDgQJMZym6nMSg qiDg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739417779; x=1740022579; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=1bG2TcWcZQz5k+Gqwwy79wI8/0pax/QFELT9STfGtKY=; b=EUS90k1FpeKYHeOnkwsQvwPXL07gC+Mmaz4guXIFs7eb5he8/D5asnneoWOqfiuEIi V04cnMcWBsLXEOTEiAnyYOMaly3SybTJoq6AqO05tZrQtNL27D8AuPwVQxc6rX8rtfZg EM7Bg/snhUcJfhjnK5JRh11dopVxBGI5lI4MQ085cWgJjzN01Z6o55Xd0NMVtiK89r7g fSLnZZrY9OmeNCaSIe49sQM9KnehLZRUl1jrL67cHKKuB1OBS25WeKl3atwBhDV0AEpf rU686G3tDepq3+p2Gm1K2hRj0clFr69IED+1rZRnEjR5fumqdCltTsX1QVXXPBbEBX2I GOZg== X-Gm-Message-State: AOJu0YzOB9dFy/C410Cs/qsv+A4ThAp9IwsTyVKI3SFbkxpdMi3Srfda QCGd6dcB4utjGBbNaP4jQLhhEpTt4iP7HqCHkBK8wAAnMC743D8Y1yv9KQ== X-Gm-Gg: ASbGncte41jgKMDRwVx7JlKxkQz9bIxrfmhzzZtXTDkkkJrFZeHTjNARmj8oi+NfMI8 +A253krnE/1es6bCuRvz8iF6jln3nQB/lBs6uURtamla79S3U3SWkEXVtvydozcmmNCEcF3ImCG 7XDfikPJM8X0EHR6aIvkxQibRDNUNSrNuwRUukYPYgQIQMJfVn9zxbtMSVq2jad8v+vxugtExav l3pHvvnJIEkXQRa80VYxzdFA9mk9hQpE6OLwJPq2VLA0hIlARaJ7zgyBYU9WictWnMo02P6ynJT xdBuHxr6IfctGW7GA58pvZ4oiNYZU1DAK39Jfvjta0bzxeXAMQ== X-Google-Smtp-Source: AGHT+IFXy/P5J+sCx9sY/U/MAEMVw0FrBRnsYEuISK2pqOweK9ST2IkQe2QsXaxkx9P9LFDey130mw== X-Received: by 2002:a05:6a00:2294:b0:730:9768:ccdf with SMTP id d2e1a72fcca58-7322c3a5ef2mr7987610b3a.14.1739417779128; Wed, 12 Feb 2025 19:36:19 -0800 (PST) Received: from macbookpro.lan ([2603:3023:16e:5000:8af:ecd2:44cd:8027]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-732425468bdsm222056b3a.31.2025.02.12.19.36.17 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Wed, 12 Feb 2025 19:36:18 -0800 (PST) From: Alexei Starovoitov To: bpf@vger.kernel.org Cc: andrii@kernel.org, memxor@gmail.com, akpm@linux-foundation.org, peterz@infradead.org, vbabka@suse.cz, bigeasy@linutronix.de, rostedt@goodmis.org, houtao1@huawei.com, hannes@cmpxchg.org, shakeel.butt@linux.dev, mhocko@suse.com, willy@infradead.org, tglx@linutronix.de, jannh@google.com, tj@kernel.org, linux-mm@kvack.org, kernel-team@fb.com Subject: [PATCH bpf-next v8 6/6] bpf: Use try_alloc_pages() to allocate pages for bpf needs. Date: Wed, 12 Feb 2025 19:35:56 -0800 Message-Id: <20250213033556.9534-7-alexei.starovoitov@gmail.com> X-Mailer: git-send-email 2.39.5 (Apple Git-154) In-Reply-To: <20250213033556.9534-1-alexei.starovoitov@gmail.com> References: <20250213033556.9534-1-alexei.starovoitov@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net From: Alexei Starovoitov Use try_alloc_pages() and free_pages_nolock() for BPF needs when context doesn't allow using normal alloc_pages. This is a prerequisite for further work. Signed-off-by: Alexei Starovoitov --- include/linux/bpf.h | 2 +- kernel/bpf/arena.c | 5 ++--- kernel/bpf/syscall.c | 23 ++++++++++++++++++++--- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index f3f50e29d639..e1838a341817 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -2348,7 +2348,7 @@ int generic_map_delete_batch(struct bpf_map *map, struct bpf_map *bpf_map_get_curr_or_next(u32 *id); struct bpf_prog *bpf_prog_get_curr_or_next(u32 *id); -int bpf_map_alloc_pages(const struct bpf_map *map, gfp_t gfp, int nid, +int bpf_map_alloc_pages(const struct bpf_map *map, int nid, unsigned long nr_pages, struct page **page_array); #ifdef CONFIG_MEMCG void *bpf_map_kmalloc_node(const struct bpf_map *map, size_t size, gfp_t flags, diff --git a/kernel/bpf/arena.c b/kernel/bpf/arena.c index 0975d7f22544..8ecc62e6b1a2 100644 --- a/kernel/bpf/arena.c +++ b/kernel/bpf/arena.c @@ -287,7 +287,7 @@ static vm_fault_t arena_vm_fault(struct vm_fault *vmf) return VM_FAULT_SIGSEGV; /* Account into memcg of the process that created bpf_arena */ - ret = bpf_map_alloc_pages(map, GFP_KERNEL | __GFP_ZERO, NUMA_NO_NODE, 1, &page); + ret = bpf_map_alloc_pages(map, NUMA_NO_NODE, 1, &page); if (ret) { range_tree_set(&arena->rt, vmf->pgoff, 1); return VM_FAULT_SIGSEGV; @@ -465,8 +465,7 @@ static long arena_alloc_pages(struct bpf_arena *arena, long uaddr, long page_cnt if (ret) goto out_free_pages; - ret = bpf_map_alloc_pages(&arena->map, GFP_KERNEL | __GFP_ZERO, - node_id, page_cnt, pages); + ret = bpf_map_alloc_pages(&arena->map, node_id, page_cnt, pages); if (ret) goto out; diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index c420edbfb7c8..a7af8d0185d0 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -569,7 +569,24 @@ static void bpf_map_release_memcg(struct bpf_map *map) } #endif -int bpf_map_alloc_pages(const struct bpf_map *map, gfp_t gfp, int nid, +static bool can_alloc_pages(void) +{ + return preempt_count() == 0 && !irqs_disabled() && + !IS_ENABLED(CONFIG_PREEMPT_RT); +} + +static struct page *__bpf_alloc_page(int nid) +{ + if (!can_alloc_pages()) + return try_alloc_pages(nid, 0); + + return alloc_pages_node(nid, + GFP_KERNEL | __GFP_ZERO | __GFP_ACCOUNT + | __GFP_NOWARN, + 0); +} + +int bpf_map_alloc_pages(const struct bpf_map *map, int nid, unsigned long nr_pages, struct page **pages) { unsigned long i, j; @@ -582,14 +599,14 @@ int bpf_map_alloc_pages(const struct bpf_map *map, gfp_t gfp, int nid, old_memcg = set_active_memcg(memcg); #endif for (i = 0; i < nr_pages; i++) { - pg = alloc_pages_node(nid, gfp | __GFP_ACCOUNT, 0); + pg = __bpf_alloc_page(nid); if (pg) { pages[i] = pg; continue; } for (j = 0; j < i; j++) - __free_page(pages[j]); + free_pages_nolock(pages[j], 0); ret = -ENOMEM; break; }