From patchwork Mon Aug 21 20:44:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthew Wilcox X-Patchwork-Id: 13359798 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 3C25BEE49A5 for ; Mon, 21 Aug 2023 20:44:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D679C8E0012; Mon, 21 Aug 2023 16:44:39 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CBC44940012; Mon, 21 Aug 2023 16:44:39 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A1F8E8E0012; Mon, 21 Aug 2023 16:44:39 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 8CF72940010 for ; Mon, 21 Aug 2023 16:44:39 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 4D555140883 for ; Mon, 21 Aug 2023 20:44:39 +0000 (UTC) X-FDA: 81149290278.06.23D19B6 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) by imf23.hostedemail.com (Postfix) with ESMTP id A9449140017 for ; Mon, 21 Aug 2023 20:44:37 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=cX48794h; dmarc=none; spf=none (imf23.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1692650677; 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=RORJVnZ1xjomMTeygi3JJ1E6/B1esghHOmLJuCDuVL8=; b=Xn0A4Dc0k6y9R+TNPAnjYoGZE2NSRCbNkA/1JYh9Khz2BNtrPs0NdDuIbw9Phuyju42Cga e0sBy/F21DpuWRXYf63yJGWgzQavqKT8v1GSI9RKpBG1kU+4Gi6VLt1zJVcFaO1DoH5vWk PQULN4EPIWIMVeJ08bd6aCPuZ+MH/fI= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=cX48794h; dmarc=none; spf=none (imf23.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1692650677; a=rsa-sha256; cv=none; b=ZzGxlns9MAnOXx86QPDL6dsmQZ/gbPbR07ZEBp7xvH+MElSXCLLHTXolgFutF9o4GdVRO5 qNJTztZ7nx0X1UyDYjj4lLI/Qi5JWRTlZc6IilEgQ+GbmukM/gtVVCJQBpEnuPK6wiXXiq rLfMqqhQNXhR6WMTzAOZsGEMOoAgsHA= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description; bh=RORJVnZ1xjomMTeygi3JJ1E6/B1esghHOmLJuCDuVL8=; b=cX48794hHeiyAzu9fcQmsTqm/r aqdYpWMbkTSg7sdRenY7DY184Sjqx0L5TE1lRgR1Rv/aCmpiho/0J8NfdRRgDvgFHaQ9sh85P0saE Ey1GDns++lTylnBp8OVqiMfqhFMzLA0fpZGEodGibshcHZBSYp3j6aPLIDFwP1ocmxTHjfQXSr6px xr/QshlBPusGF0zTRlEZH8lHdsFiiY9YmUKnFUHdiJr3VPUBOpZfpiAhZnQ8FyqDrjkrlCHWc+dgm 7+hR/W6e6bBmn7K8tUqHbHnagtZH6cUOlrhdg4Auy7hyJ8bNsFbqaLdZ3mv0bZKbpg7B9GN6tG/LJ C4P1H2Sg==; Received: from willy by casper.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1qYBlE-00CL08-JJ; Mon, 21 Aug 2023 20:44:28 +0000 From: "Matthew Wilcox (Oracle)" To: Mateusz Guzik , linux-kernel@vger.kernel.org, dennis@kernel.org, tj@kernel.org, cl@linux.com, akpm@linux-foundation.org, shakeelb@google.com, linux-mm@kvack.org Cc: "Matthew Wilcox (Oracle)" Subject: [PATCH 3/7] mm: Add free_unref_folios() Date: Mon, 21 Aug 2023 21:44:21 +0100 Message-Id: <20230821204425.2940496-3-willy@infradead.org> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20230821204425.2940496-1-willy@infradead.org> References: <20230821204425.2940496-1-willy@infradead.org> MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: 9waj7ewjnxjeq8wz1nakpabjhuwqy5wx X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: A9449140017 X-HE-Tag: 1692650677-931918 X-HE-Meta: U2FsdGVkX1+GsoJjLvKuCW2X6CXAnp4QO9GgjaEE+fDEV2TSJ/vXoQwwp++n0kSOFneJe88TVc88K5H9F51JhmG8ciSWpYX7xcHN4sDzabLs41cyzYP7ZZr5tn1VkYGeFuoD/6B9uAn+LQeFHb3nFaLuKSW7keST9KXYQJl/5TkqWPoqRmIKH245FPcl9JYit19zcyG29TU+ggFFw2Oix8JO6vOijA1j7zwbiwLvJ7txhzQIt2TBF/SdmfQQPge3hc2A1KCZ+LcX92RH+ZEFrQ6KoriIVL/8B3HQam/2BA1AtkykyScNuFhu0BbOwVcHjMn0b9dGN1Rk9YGCdzNEziwGSN7AxPMJ2ZaIUHYOiLKHWYMUMg9ztQxDiDn+ILRKCYExKZ0g5vPVszi9JeO7bTRc/RVIwPSi+tDXpOosGOjYZEFmOJwWnQHaX56OKbd/JbRH52iBYHE5SQbPODzRcRF0JEaulHp4O4mLwBP0P8dvkannkl0wb1Jlk8ClW2cah0rVuF/4VRzGLO3ut0yyIJIFgRoo4hqd4LQpIll768st36MJJ/T3dm7TMR7d/ikfoIOy030ipAYpJkEeqh8jMqDrhfPTtoKNX8bob1xSvT/bJf8vGlXaaJT1A0mOitaabK5llsOSlr3y+xlDmZE5ANoXJoLW1TbyPrHtroMCvLxD69H0hByAZ83a2QOr1eiDidwcZ34EB0y1ECBUh/58AMCCWiZe+RDuaDe7HGbIrV/U9QrbXvoHWjF0HNz0luEC2OwjulvxWe2sm7cNrr+aQOBghUaWPontv+Mmfrt+WaCYIArAC0hGrUJuPJa18O8BkXgV6R57AYaHpDeMuo0/RDC7aNJHsawn0vycZNVMPxdI8tbn/tl6YXhbv/Gi+OMlSZkkXdvaE9GTLEpVHBhy58zzpBfrtOvarvfxTHAva+/3zghhreAJs1JWUHm/9UOBJ9sL4H6myDWU1nkzMaA SvQ6p4xq zWYdNdt/8S6XNb/7pSfH2FeeEGrYdAVxWU5gWEHCIZ1sAFPZuu82C6g1xtHHipo6EDoEXzJAfd9Kfn2bFAqgFnBU7xqFR7IY2Jz5YzgibIWkqeNEBHs6z3xj32dYkpZGvbMbpbNkesMlc7WDIPTyrvMEFiuILsNTEAsFzxmewJSM1CHsSLohYSmoP9Oj/Bsq9DMYVEsW7DfQZlnjaTtoRTclOa9v5l71vgBUf 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: Iterate over a folio_batch rather than a linked list. This is easier for the CPU to prefetch and has a batch count naturally built in so we don't need to track it. Signed-off-by: Matthew Wilcox (Oracle) --- mm/internal.h | 5 +++-- mm/page_alloc.c | 59 ++++++++++++++++++++++++++++++------------------- 2 files changed, 39 insertions(+), 25 deletions(-) diff --git a/mm/internal.h b/mm/internal.h index 5c777b6779fa..3e6b448e7d63 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -440,8 +440,9 @@ extern void post_alloc_hook(struct page *page, unsigned int order, gfp_t gfp_flags); extern int user_min_free_kbytes; -extern void free_unref_page(struct page *page, unsigned int order); -extern void free_unref_page_list(struct list_head *list); +void free_unref_page(struct page *page, unsigned int order); +void free_unref_folios(struct folio_batch *fbatch); +void free_unref_page_list(struct list_head *list); extern void zone_pcp_reset(struct zone *zone); extern void zone_pcp_disable(struct zone *zone); diff --git a/mm/page_alloc.c b/mm/page_alloc.c index 2f2185929fcb..4354938ca3b0 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -32,6 +32,7 @@ #include #include #include +#include #include #include #include @@ -2493,24 +2494,21 @@ void free_unref_page(struct page *page, unsigned int order) } /* - * Free a list of 0-order pages + * Free a batch of 0-order pages */ -void free_unref_page_list(struct list_head *list) +void free_unref_folios(struct folio_batch *folios) { unsigned long __maybe_unused UP_flags; - struct folio *folio, *next; struct per_cpu_pages *pcp = NULL; struct zone *locked_zone = NULL; - int batch_count = 0; - int migratetype; + int i, j, migratetype; - /* Prepare pages for freeing */ - list_for_each_entry_safe(folio, next, list, lru) { + /* Prepare folios for freeing */ + for (i = 0, j = 0; i < folios->nr; i++) { + struct folio *folio = folios->folios[i]; unsigned long pfn = folio_pfn(folio); - if (!free_unref_page_prepare(&folio->page, pfn, 0)) { - list_del(&folio->lru); + if (!free_unref_page_prepare(&folio->page, pfn, 0)) continue; - } /* * Free isolated pages directly to the allocator, see @@ -2518,34 +2516,31 @@ void free_unref_page_list(struct list_head *list) */ migratetype = get_pcppage_migratetype(&folio->page); if (unlikely(is_migrate_isolate(migratetype))) { - list_del(&folio->lru); free_one_page(folio_zone(folio), &folio->page, pfn, 0, migratetype, FPI_NONE); continue; } + if (j != i) + folios->folios[j] = folio; + j++; } + folios->nr = j; - list_for_each_entry_safe(folio, next, list, lru) { + for (i = 0; i < folios->nr; i++) { + struct folio *folio = folios->folios[i]; struct zone *zone = folio_zone(folio); - list_del(&folio->lru); migratetype = get_pcppage_migratetype(&folio->page); - /* - * Either different zone requiring a different pcp lock or - * excessive lock hold times when freeing a large list of - * folios. - */ - if (zone != locked_zone || batch_count == SWAP_CLUSTER_MAX) { + /* Different zone requires a different pcp lock */ + if (zone != locked_zone) { if (pcp) { pcp_spin_unlock(pcp); pcp_trylock_finish(UP_flags); } - batch_count = 0; - /* - * trylock is necessary as folios may be getting freed + * trylock is necessary as pages may be getting freed * from IRQ or SoftIRQ context after an IO completion. */ pcp_trylock_prepare(UP_flags); @@ -2570,7 +2565,6 @@ void free_unref_page_list(struct list_head *list) trace_mm_page_free_batched(&folio->page); free_unref_page_commit(zone, pcp, &folio->page, migratetype, 0); - batch_count++; } if (pcp) { @@ -2579,6 +2573,25 @@ void free_unref_page_list(struct list_head *list) } } +void free_unref_page_list(struct list_head *list) +{ + struct folio_batch fbatch; + + folio_batch_init(&fbatch); + while (!list_empty(list)) { + struct folio *folio = list_first_entry(list, struct folio, lru); + + list_del(&folio->lru); + if (folio_batch_add(&fbatch, folio) > 0) + continue; + free_unref_folios(&fbatch); + fbatch.nr = 0; + } + + if (fbatch.nr) + free_unref_folios(&fbatch); +} + /* * split_page takes a non-compound higher-order page, and splits it into * n (1<