From patchwork Tue Jan 14 02:19:18 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexei Starovoitov X-Patchwork-Id: 13938320 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 3D515C02180 for ; Tue, 14 Jan 2025 02:19:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C5E56280003; Mon, 13 Jan 2025 21:19:44 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C0F7E280002; Mon, 13 Jan 2025 21:19:44 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A89BE280003; Mon, 13 Jan 2025 21:19:44 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 85E3E280002 for ; Mon, 13 Jan 2025 21:19:44 -0500 (EST) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 3D618C04B3 for ; Tue, 14 Jan 2025 02:19:44 +0000 (UTC) X-FDA: 83004451488.18.D672BCE Received: from mail-pl1-f175.google.com (mail-pl1-f175.google.com [209.85.214.175]) by imf28.hostedemail.com (Postfix) with ESMTP id 44B5CC0012 for ; Tue, 14 Jan 2025 02:19:42 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=JCyamgJi; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf28.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.214.175 as permitted sender) smtp.mailfrom=alexei.starovoitov@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1736821182; a=rsa-sha256; cv=none; b=aRl0UaZkVrDAXp8Fx+90QeBFpJTO7s9Cce3+YAIa6XS60izJgZ9CW/obCakjQgsHt9VYoc AJf+F6emneyAqfIObzbCeYv4fTfDKn45vYP1lQ7sbj2NtiLAmYA6D8xypq5aTdcbanJ0fe 90StZAZDfNsjkdfcMi2DBj3vAlNQXkI= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=JCyamgJi; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf28.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.214.175 as permitted sender) smtp.mailfrom=alexei.starovoitov@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1736821182; 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:in-reply-to:references:references:dkim-signature; bh=SQPsX2DHIButJsUpdhJcEvvwgfeMxBo/GFMw0XDovgw=; b=m+VK8ksrpOYd60UJs4l36kNznwEsBnuTMhZIPgW7RTrhiu8j94+cBtU5t6GR1zLrHs/XmC 3aBfXWbM8aJwIxI0teVeQFLO1onbxUFVNzGprd4hfhdS5h6JMa/wr74qx7SYc6MUTTStH5 +o6DceAMAYfbIZrtKKlyJ15tHFhkyzY= Received: by mail-pl1-f175.google.com with SMTP id d9443c01a7336-2166651f752so110785145ad.3 for ; Mon, 13 Jan 2025 18:19:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736821181; x=1737425981; darn=kvack.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=SQPsX2DHIButJsUpdhJcEvvwgfeMxBo/GFMw0XDovgw=; b=JCyamgJi+HcLSmxYsr36Yn/+dxqNGiqfqfeutX+jykH1NlnATve35UaDfE6vQ3BN+K ZJ2UhlA8TmgKqOMF7Ke55dBnOMZC3yDs8WU5BNgHg6FMDXTjOP+cHN1TvNjz9GoavKR1 Nc2t0R8wrPLqkJvTOAXOOMsbgVW2c5JLnPKbZnB8Z3NrznNtWKlbodkaKxhjDrnDQHLs wblzbTCGXXjzLFQWe+dkGxfgK8hjMsFzbxgFwNrMCYg558A+TaTnXoRpKx5a70liPU01 k3YsAsFKSqu2t7LVzcnvjqcMezJ7Lot9fPmn6e4QdkRx7LUJ9qLFGArplEYzMv5xgiKi +fbg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736821181; x=1737425981; 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=SQPsX2DHIButJsUpdhJcEvvwgfeMxBo/GFMw0XDovgw=; b=fXRjyJ7m8LVk5F7ePwhvE3Jwe55lEBtfxS6AQ7e0cSY7tHaP+ATOKK1l5lFQMC7vIv LMzxotmgm7b5mYlCL4Kp88s+COsYwkhHavXL66rN+FjpyXnD4NMrZ4SK0uUdUsBRQzcP RTW8GMLmwr+CYATtcEDYe2Fy/Ch8lf7cIHD9DNkflO/qU+p6B+he+ytD6PpUSnYUWz/T U6aYvwJcCqcMU6OMiL33LLoXvrXNlzti7k7/w5yrm03Ti6y28zX72MV3K2lREM9buQMZ 3tbKCphKlFhigGvAFI3tZ+yGMdtOWbCTJAih7dirxdaK4wk76V2PTUNvRbVxo+G7x/wP UQTA== X-Forwarded-Encrypted: i=1; AJvYcCVhs3s2wG0lo/n9bzA2PlaaeR4x8uFLx1w3vMImxEdfo8+R2qfcwEViGjuHpNnJ6iTkToHZGSVC7w==@kvack.org X-Gm-Message-State: AOJu0Yx173gbFNsos48aEUi6tECIOZTv12dkkk8HZD8Syog7epNiSKKV YzR4XViM9EIGOQ/sKJ22tqWxVdPSmMQKLv5h73Nq0HKo/5/9Kkj1 X-Gm-Gg: ASbGncsUYPfYu8nIwFlL+aKdwQNdIINDcvCL8LoslJ8N1sxKhGtcw9QWVAoL7HQanpr uwA0+Dg/pHF81WudfsnMiDA8A4i1i0ZmcZX0CTQta1eARxxrGt2cIxn6VjPo7MZoj3wNMH4PqWS IRaXZliis0EQVXyfF9xDh5Av6WM64G4wdduaRmMaCnKSfftswib31DkhF+UdwKbQybJDdMmHarf rHmZF5/DquE9O+gaWyKQLg5JkvCSKm85sGACGqM5ppgCNXml8Hzr3G7ai14dW4h4ZcZCVX/dpBt YbRb7IZQ X-Google-Smtp-Source: AGHT+IH4C8oQxYrxEc0hMPntr3TFj0/HQ3tRiTFBDtPEqYrgkCC5XatENgLtg7g5dRzWvlp6uSkr4w== X-Received: by 2002:a17:903:1245:b0:216:48f4:4f20 with SMTP id d9443c01a7336-21a83f56f58mr324043955ad.16.1736821180973; Mon, 13 Jan 2025 18:19:40 -0800 (PST) Received: from localhost.localdomain ([2620:10d:c090:400::5:4043]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21a9f10e011sm59713495ad.3.2025.01.13.18.19.38 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 13 Jan 2025 18:19:40 -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 v4 2/6] mm, bpf: Introduce free_pages_nolock() Date: Mon, 13 Jan 2025 18:19:18 -0800 Message-Id: <20250114021922.92609-3-alexei.starovoitov@gmail.com> X-Mailer: git-send-email 2.39.5 (Apple Git-154) In-Reply-To: <20250114021922.92609-1-alexei.starovoitov@gmail.com> References: <20250114021922.92609-1-alexei.starovoitov@gmail.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 44B5CC0012 X-Stat-Signature: ocbpik8tz4fyf81yjsa3o861prss4j7y X-HE-Tag: 1736821182-334403 X-HE-Meta: U2FsdGVkX18uFAoSWaZCesVqc6mnIpH2UWhmCIYmPZcMmmIfZyQy4SoOAV6JcTB7cm59zQpCZ63XmsRwDpzqYksWl0X2GE6oNruPXjhj2KNA62i3mnI1ZwS2koHf6TxxPxrA5uOrzMnRZ4ymZCGtm2qyWTIN6xf7+/Ujf+ZmQt0ic/4PsdXvx16iaAwiP71UM76Lbaug5MeqpZo9eft0wRfOK5SgFKpebBezfyDiQeJOi8/y4xc8W062KOnlm0Ff9T43L4zWhEmqHA0+KYiv6JNwwwvpZaPzMx3o/L4ReOTqhqqA1La6wDse4hEls0SAWx/l1pMVs6SA7bkt3cos697FnPGRrmN6Sqc1TR35GFe0vXUs6nzzUhbETZxzu2wr/bcJOwl160jNtEmYOwMaHsbuFHXb2e1gQILap+OK48G+1zeBksQ3j5I6VM19MfbbdtnlWjxnaUr6IaYtzJ2a8PXFBOuYEeGQPVTUvF10XnEb28A3KswPrG/G9pyDDGl+GIeVUhlx1dUGnsZb/yA2Cth+2GbhSd0GtS5KITMuPcf2NIfM5lNTY4cT2DvgjtVZ+NTaHB+NnAKHy/GmjMM9Qj9G6KesSIf7MLogN1dCtNGJYPyDfxsY6FhGNddIHC0fKq2uBtWyfKGv5NcLNi33rearFYijCk9EsZN6jXIuADPKubzE6trIcL+CSFjMmNpDiKXiSZJiz/SMYFbjp+JWCRBxeMT9XEeP4kiZaRUwkRIaLpxj/kVoiaSqHrAbqkuvHaoGtkONXZl7fvK5rWYSB5fkBtfSb+Oyo1B+tsnAum/2ELlwoYwSnBT7G6H0wMhe+CjhiTIIGvhCidc0vEdliR1GK6HnEz/aNyye9rONcvZ0dbgWRIpLLFw2rFDlBJPQaiAsi3mF1MrqYbZLwe5xZUmjhDCOq9BHZ+5sPqINrn+aU5QjhBxic9/fNX2eKkxJv9i4RnRgDYM8TATR446 C3tHXucc P/FRxuH4tOEPbaPSKnEL71MCp/tRW/Ltj5A0PoHDDyiQI1IVthyVIQDAsR8w0ZqXNhKWgJ2tUEvpYRsQ8QwNGBphIu6hhS1FajnumkBOkc78AVyZbf+Qu03ffJV/BkfOzqO7LRKKcCR1+0tOhWJlGCM3gqvfuRkrvc2kbtmamf2pMWAKWrZemRJEzCequmAjGykmPzW5kr+VTNyt8CWnxMCnqK4ckwaI/YGPuBJfV0s40ihXB/isgwyJIaczl9tcQV4nfQzWyCCY0kJDCkBCBRpE57ChaRbu0uLIrYLb7Xpv0QlGYqYv+O+dQIJhHi+VBJMr3XomcvzHHFR4pQ2a+YP2Hd3TYpNChYch/57VB+mWP42rIZLS4q6xciINzlVS/ZxeDrY7HJpIVvLZ4xR2j6vzsQDpuIT+JfaP7Y5WajwEie9Z0oSUT60gJ2vM7k+VLRpdWHii5pdJ5UVaefdvtP/QI5lPmsSWAi42zlSvgGtb6qpc+J2DcEeBsBw== 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 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 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. Signed-off-by: Alexei Starovoitov --- include/linux/gfp.h | 1 + include/linux/mm_types.h | 4 ++ include/linux/mmzone.h | 3 ++ mm/page_alloc.c | 79 ++++++++++++++++++++++++++++++++++++---- 4 files changed, 79 insertions(+), 8 deletions(-) diff --git a/include/linux/gfp.h b/include/linux/gfp.h index b41bb6e01781..6eba2d80feb8 100644 --- a/include/linux/gfp.h +++ b/include/linux/gfp.h @@ -391,6 +391,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 7361a8f3ab68..52547b3e5fd8 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 b36124145a16..1a854e0a9e3b 100644 --- a/include/linux/mmzone.h +++ b/include/linux/mmzone.h @@ -953,6 +953,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/mm/page_alloc.c b/mm/page_alloc.c index 0f4be88ff131..f967725898be 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) @@ -1247,13 +1250,44 @@ static void split_large_buddy(struct zone *zone, struct page *page, } } +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); @@ -2596,7 +2630,7 @@ static int nr_pcp_high(struct per_cpu_pages *pcp, struct zone *zone, static void free_unref_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; @@ -2631,6 +2665,14 @@ static void free_unref_page_commit(struct zone *zone, struct per_cpu_pages *pcp, } 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), @@ -2645,7 +2687,8 @@ static void free_unref_page_commit(struct zone *zone, struct per_cpu_pages *pcp, /* * Free a pcp page */ -void free_unref_page(struct page *page, unsigned int order) +static void __free_unref_page(struct page *page, unsigned int order, + fpi_t fpi_flags) { unsigned long __maybe_unused UP_flags; struct per_cpu_pages *pcp; @@ -2654,7 +2697,7 @@ void free_unref_page(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; } @@ -2671,24 +2714,33 @@ void free_unref_page(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(page_zone(page), page, pfn, order, FPI_NONE); + free_one_page(page_zone(page), page, pfn, order, fpi_flags); return; } migratetype = MIGRATE_MOVABLE; } zone = page_zone(page); + if (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_unref_page_commit(zone, pcp, page, migratetype, order); + free_unref_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_unref_page(struct page *page, unsigned int order) +{ + __free_unref_page(page, order, FPI_NONE); +} + /* * Free a batch of folios */ @@ -2777,7 +2829,7 @@ void free_unref_folios(struct folio_batch *folios) trace_mm_page_free_batched(&folio->page); free_unref_page_commit(zone, pcp, &folio->page, migratetype, - order); + order, FPI_NONE); } if (pcp) { @@ -4853,6 +4905,17 @@ void __free_pages(struct page *page, unsigned int order) } EXPORT_SYMBOL(__free_pages); +/* + * Can be called while holding raw_spin_lock or from IRQ and NMI, + * but only for pages that came from try_alloc_pages(): + * order <= 3, !folio, etc + */ +void free_pages_nolock(struct page *page, unsigned int order) +{ + if (put_page_testzero(page)) + __free_unref_page(page, order, FPI_TRYLOCK); +} + void free_pages(unsigned long addr, unsigned int order) { if (addr != 0) {