From nobody Wed Sep 17 01:34:39 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id EEB62C4708D for ; Mon, 26 Dec 2022 07:09:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231696AbiLZHJD (ORCPT ); Mon, 26 Dec 2022 02:09:03 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51186 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229556AbiLZHI6 (ORCPT ); Mon, 26 Dec 2022 02:08:58 -0500 Received: from mail-wm1-x331.google.com (mail-wm1-x331.google.com [IPv6:2a00:1450:4864:20::331]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7DAE521AA for ; Sun, 25 Dec 2022 23:08:57 -0800 (PST) Received: by mail-wm1-x331.google.com with SMTP id m26-20020a05600c3b1a00b003d9811fcaafso572872wms.5 for ; Sun, 25 Dec 2022 23:08:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; 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=WF0czWG0z2T4oq6TY2ZkLRE6dWmClGYr0kuRjGhgjlc=; b=fpdkSJF/JqUlKexT21mW8e+jSTKUNfkGzMkXrfOfL8HIFYx/Vq1hHyPBMjknJ2WtBK WrIcuPAHPepaz2AHevrqxKaipS445kAnZPURMVNYZHTNkuacttJ2UiXq3RvY2/Gt3Lyw bqgPnNwcChpmNMyk0XKDnTB16KN6xSc6zms8dljaclOqZVhCPVDsbRisayFao3u42G9V OVqutqKiT99JxTmxQaPT1PaWfI6Tg/hRRf1UYwL+F4dLe7ZBRYtVxulB8bGPlDUuIKMM 0qftQCz9/GBw2P5y4J4/TvoPqdAqIMJXl1s/DMbHk6YaN5YZ3Q30KzMr41ELzyYZuCpf K/tw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=WF0czWG0z2T4oq6TY2ZkLRE6dWmClGYr0kuRjGhgjlc=; b=jgFty1oqyu8+59JkD3q8VsvPnKX0tD7Q5W7vXf4lDGCIhKX8r+zAsLrli+teQnqZAO s1I8tKgP9ddZfC/A3f6vqtkYeCCJYM9gTiqduyNZgUisS+FvY71d9tqlAmRbIuLu30gH EXHokd5pUw9lnm0AxANlET9Kd2iha6H49EIydYe1qtpFiUfVMkuv7XprVHOo/QYf3CPe Rp0/+avJw9+r41fDgbByCbU4z8yTcHwzBfZtfrgV5PSmA9XJfm5S3Iz/ArcqoDFk01gu 8h/TWUxipS2eRP4pbuoaYQyDZfEjAlpmo8PYAY5tt0ddcgOgSJPYmA6yrftHzWQFSDUC l7lQ== X-Gm-Message-State: AFqh2kqM0YDNKnH5fpcpVzFI4c/zfCrvvwBLFnuFwvkCRNTUKoHRvcK/ XUQ7lQpNeRyN5loxu5gXB4w= X-Google-Smtp-Source: AMrXdXsSiUJBjB/FhDmSCuSkDKJgGEs8hWcATc7EdSFhVVbhZyX7hNNQ4+aMSZDrUlPWscxg5IgwEg== X-Received: by 2002:a05:600c:3589:b0:3d0:6c60:b4d1 with SMTP id p9-20020a05600c358900b003d06c60b4d1mr14506892wmq.6.1672038536088; Sun, 25 Dec 2022 23:08:56 -0800 (PST) Received: from lucifer.home ([2a00:23c5:dc8c:8701:1663:9a35:5a7b:1d76]) by smtp.googlemail.com with ESMTPSA id e16-20020a05600c4e5000b003c21ba7d7d6sm13191456wmq.44.2022.12.25.23.08.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 25 Dec 2022 23:08:55 -0800 (PST) From: Lorenzo Stoakes To: linux-mm@kvack.org, Andrew Morton , linux-kernel@vger.kernel.org Cc: Matthew Wilcox , Hugh Dickins , Vlastimil Babka , Liam Howlett , William Kucharski , Christian Brauner , Jonathan Corbet , Mike Rapoport , Joel Fernandes , Lorenzo Stoakes Subject: [PATCH v2 1/4] mm: pagevec: add folio_batch_reinit() Date: Mon, 26 Dec 2022 07:08:44 +0000 Message-Id: X-Mailer: git-send-email 2.39.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This performs the same task as pagevec_reinit(), only modifying a folio batch rather than a pagevec. Signed-off-by: Lorenzo Stoakes --- include/linux/pagevec.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/include/linux/pagevec.h b/include/linux/pagevec.h index 215eb6c3bdc9..2a6f61a0c10a 100644 --- a/include/linux/pagevec.h +++ b/include/linux/pagevec.h @@ -103,6 +103,11 @@ static inline void folio_batch_init(struct folio_batch= *fbatch) fbatch->percpu_pvec_drained =3D false; } =20 +static inline void folio_batch_reinit(struct folio_batch *fbatch) +{ + fbatch->nr =3D 0; +} + static inline unsigned int folio_batch_count(struct folio_batch *fbatch) { return fbatch->nr; --=20 2.39.0 From nobody Wed Sep 17 01:34:39 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 33CCEC4332F for ; Mon, 26 Dec 2022 07:09:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231492AbiLZHJJ (ORCPT ); Mon, 26 Dec 2022 02:09:09 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51196 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231684AbiLZHJA (ORCPT ); Mon, 26 Dec 2022 02:09:00 -0500 Received: from mail-wm1-x32d.google.com (mail-wm1-x32d.google.com [IPv6:2a00:1450:4864:20::32d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E50ED2626 for ; Sun, 25 Dec 2022 23:08:58 -0800 (PST) Received: by mail-wm1-x32d.google.com with SMTP id c65-20020a1c3544000000b003cfffd00fc0so9970527wma.1 for ; Sun, 25 Dec 2022 23:08:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; 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=VLkOKeT4uIbBG6ss89Hd1tlt6NrDYEFm7lc6id39hK0=; b=COIP09nzy7+WUYXb67bPQBEcKY4WAGn4puhgDZEh04apbH19KfM9H1dsABye7DhxTw FcWexp+CZ1yPBvzB3gThuuvGnsUcxWJETv9ECDBq5SQueQBppS/y5bp8dLPVpHIRPh3u OzTvikrsG+tqvQVA47m3QWGzo51Z13zp17Q1VU1y11ZTdBZyVg2TBquWulrrm1vmSK4w cMxegFalPOk25gfuMt7Amed0iIr27PXL220rYePTzze2cak5eR7QaMygByv8H60iozZP of3oxLkhX2kimQnE0T7Emyam4xJhzo07UxWThIyyxJIzM2X/2MKnNbeGDvZxqOFL7Pze DKQg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=VLkOKeT4uIbBG6ss89Hd1tlt6NrDYEFm7lc6id39hK0=; b=ZxfFW95eaYwtYKxIJPfI8Ea8+wdLYXAu+KplRfnMp7wCkk6Bkf+qMgT8IQWbkiMDPO EygfK6eLUfQSPce4oQkuiQbNdp7LLnjHnzf+91px9Gj+k/mJZBzKHbCiY2WV+sqqJjb6 jhOEhvC7j1Kl0nKMw1GpVixPGHik4qOB5CguzhNjE/W0EPMX+2Av0ENgLSn2NJcmzwFJ wDmcWbXpnCjSsCqHOam1aY+4ubITLIZXoH+xpApofjoGTZJvLTFrOAM4bd5bgm+/44WM UuqhNezsG40Gq8chRo5I7VCbYFpRMMNGRovYhfQUnYUBT8cEomH1wHD3cIG9dbi6EJB5 cLzA== X-Gm-Message-State: AFqh2krlEKyM+ACOAAuPCf2xx1+A0MPyEXCMOvYn4/sl96wl/osG99aY xyiCIEVIhWneOBOc2VoFQMU= X-Google-Smtp-Source: AMrXdXvidvUPEzQDnVzwN9qFcNFQbsFT1dSF8jp/geRaOFD/4l6Jf0gcoX25KRTXIAC1rvlKHefHzw== X-Received: by 2002:a05:600c:3ba7:b0:3d3:4dac:aa69 with SMTP id n39-20020a05600c3ba700b003d34dacaa69mr12243687wms.36.1672038537308; Sun, 25 Dec 2022 23:08:57 -0800 (PST) Received: from lucifer.home ([2a00:23c5:dc8c:8701:1663:9a35:5a7b:1d76]) by smtp.googlemail.com with ESMTPSA id e16-20020a05600c4e5000b003c21ba7d7d6sm13191456wmq.44.2022.12.25.23.08.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 25 Dec 2022 23:08:56 -0800 (PST) From: Lorenzo Stoakes To: linux-mm@kvack.org, Andrew Morton , linux-kernel@vger.kernel.org Cc: Matthew Wilcox , Hugh Dickins , Vlastimil Babka , Liam Howlett , William Kucharski , Christian Brauner , Jonathan Corbet , Mike Rapoport , Joel Fernandes , Lorenzo Stoakes Subject: [PATCH v2 2/4] mm: mlock: use folios and a folio batch internally Date: Mon, 26 Dec 2022 07:08:45 +0000 Message-Id: <03ac78b416be5a361b79464acc3da7f93b9c37e8.1672038314.git.lstoakes@gmail.com> X-Mailer: git-send-email 2.39.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This brings mlock in line with the folio batches declared in mm/swap.c and makes the code more consistent across the two. The existing mechanism for identifying which operation each folio in the batch is undergoing is maintained, i.e. using the lower 2 bits of the struct folio address (previously struct page address). This should continue to function correctly as folios remain at least system word-aligned. All invoctions of mlock() pass either a non-compound page or the head of a THP-compound page and no tail pages need updating so this functionality works with struct folios being used internally rather than struct pages. In this patch the external interface is kept identical to before in order to maintain separation between patches in the series, using a rather awkward conversion from struct page to struct folio in relevant functions. However, this maintenance of the existing interface is intended to be temporary - the next patch in the series will update the interfaces to accept folios directly. Signed-off-by: Lorenzo Stoakes --- mm/mlock.c | 238 +++++++++++++++++++++++++++-------------------------- 1 file changed, 120 insertions(+), 118 deletions(-) diff --git a/mm/mlock.c b/mm/mlock.c index 7032f6dd0ce1..e9ba47fe67ed 100644 --- a/mm/mlock.c +++ b/mm/mlock.c @@ -28,12 +28,12 @@ =20 #include "internal.h" =20 -struct mlock_pvec { +struct mlock_fbatch { local_lock_t lock; - struct pagevec vec; + struct folio_batch fbatch; }; =20 -static DEFINE_PER_CPU(struct mlock_pvec, mlock_pvec) =3D { +static DEFINE_PER_CPU(struct mlock_fbatch, mlock_fbatch) =3D { .lock =3D INIT_LOCAL_LOCK(lock), }; =20 @@ -48,192 +48,192 @@ bool can_do_mlock(void) EXPORT_SYMBOL(can_do_mlock); =20 /* - * Mlocked pages are marked with PageMlocked() flag for efficient testing + * Mlocked folios are marked with the PG_mlocked flag for efficient testing * in vmscan and, possibly, the fault path; and to support semi-accurate * statistics. * - * An mlocked page [PageMlocked(page)] is unevictable. As such, it will - * be placed on the LRU "unevictable" list, rather than the [in]active lis= ts. - * The unevictable list is an LRU sibling list to the [in]active lists. - * PageUnevictable is set to indicate the unevictable state. + * An mlocked folio [folio_test_mlocked(folio)] is unevictable. As such, = it + * will be ostensibly placed on the LRU "unevictable" list (actually no su= ch + * list exists), rather than the [in]active lists. PG_unevictable is set to + * indicate the unevictable state. */ =20 -static struct lruvec *__mlock_page(struct page *page, struct lruvec *lruve= c) +static struct lruvec *__mlock_folio(struct folio *folio, struct lruvec *lr= uvec) { /* There is nothing more we can do while it's off LRU */ - if (!TestClearPageLRU(page)) + if (!folio_test_clear_lru(folio)) return lruvec; =20 - lruvec =3D folio_lruvec_relock_irq(page_folio(page), lruvec); + lruvec =3D folio_lruvec_relock_irq(folio, lruvec); =20 - if (unlikely(page_evictable(page))) { + if (unlikely(folio_evictable(folio))) { /* - * This is a little surprising, but quite possible: - * PageMlocked must have got cleared already by another CPU. - * Could this page be on the Unevictable LRU? I'm not sure, - * but move it now if so. + * This is a little surprising, but quite possible: PG_mlocked + * must have got cleared already by another CPU. Could this + * folio be unevictable? I'm not sure, but move it now if so. */ - if (PageUnevictable(page)) { - del_page_from_lru_list(page, lruvec); - ClearPageUnevictable(page); - add_page_to_lru_list(page, lruvec); + if (folio_test_unevictable(folio)) { + lruvec_del_folio(lruvec, folio); + folio_clear_unevictable(folio); + lruvec_add_folio(lruvec, folio); + __count_vm_events(UNEVICTABLE_PGRESCUED, - thp_nr_pages(page)); + folio_nr_pages(folio)); } goto out; } =20 - if (PageUnevictable(page)) { - if (PageMlocked(page)) - page->mlock_count++; + if (folio_test_unevictable(folio)) { + if (folio_test_mlocked(folio)) + folio->mlock_count++; goto out; } =20 - del_page_from_lru_list(page, lruvec); - ClearPageActive(page); - SetPageUnevictable(page); - page->mlock_count =3D !!PageMlocked(page); - add_page_to_lru_list(page, lruvec); - __count_vm_events(UNEVICTABLE_PGCULLED, thp_nr_pages(page)); + lruvec_del_folio(lruvec, folio); + folio_clear_active(folio); + folio_set_unevictable(folio); + folio->mlock_count =3D !!folio_test_mlocked(folio); + lruvec_add_folio(lruvec, folio); + __count_vm_events(UNEVICTABLE_PGCULLED, folio_nr_pages(folio)); out: - SetPageLRU(page); + folio_set_lru(folio); return lruvec; } =20 -static struct lruvec *__mlock_new_page(struct page *page, struct lruvec *l= ruvec) +static struct lruvec *__mlock_new_folio(struct folio *folio, struct lruvec= *lruvec) { - VM_BUG_ON_PAGE(PageLRU(page), page); + VM_BUG_ON_FOLIO(folio_test_lru(folio), folio); =20 - lruvec =3D folio_lruvec_relock_irq(page_folio(page), lruvec); + lruvec =3D folio_lruvec_relock_irq(folio, lruvec); =20 /* As above, this is a little surprising, but possible */ - if (unlikely(page_evictable(page))) + if (unlikely(folio_evictable(folio))) goto out; =20 - SetPageUnevictable(page); - page->mlock_count =3D !!PageMlocked(page); - __count_vm_events(UNEVICTABLE_PGCULLED, thp_nr_pages(page)); + folio_set_unevictable(folio); + folio->mlock_count =3D !!folio_test_mlocked(folio); + __count_vm_events(UNEVICTABLE_PGCULLED, folio_nr_pages(folio)); out: - add_page_to_lru_list(page, lruvec); - SetPageLRU(page); + lruvec_add_folio(lruvec, folio); + folio_set_lru(folio); return lruvec; } =20 -static struct lruvec *__munlock_page(struct page *page, struct lruvec *lru= vec) +static struct lruvec *__munlock_folio(struct folio *folio, struct lruvec *= lruvec) { - int nr_pages =3D thp_nr_pages(page); + int nr_pages =3D folio_nr_pages(folio); bool isolated =3D false; =20 - if (!TestClearPageLRU(page)) + if (!folio_test_clear_lru(folio)) goto munlock; =20 isolated =3D true; - lruvec =3D folio_lruvec_relock_irq(page_folio(page), lruvec); + lruvec =3D folio_lruvec_relock_irq(folio, lruvec); =20 - if (PageUnevictable(page)) { + if (folio_test_unevictable(folio)) { /* Then mlock_count is maintained, but might undercount */ - if (page->mlock_count) - page->mlock_count--; - if (page->mlock_count) + if (folio->mlock_count) + folio->mlock_count--; + if (folio->mlock_count) goto out; } /* else assume that was the last mlock: reclaim will fix it if not */ =20 munlock: - if (TestClearPageMlocked(page)) { - __mod_zone_page_state(page_zone(page), NR_MLOCK, -nr_pages); - if (isolated || !PageUnevictable(page)) + if (folio_test_clear_mlocked(folio)) { + zone_stat_mod_folio(folio, NR_MLOCK, -nr_pages); + if (isolated || !folio_test_unevictable(folio)) __count_vm_events(UNEVICTABLE_PGMUNLOCKED, nr_pages); else __count_vm_events(UNEVICTABLE_PGSTRANDED, nr_pages); } =20 - /* page_evictable() has to be checked *after* clearing Mlocked */ - if (isolated && PageUnevictable(page) && page_evictable(page)) { - del_page_from_lru_list(page, lruvec); - ClearPageUnevictable(page); - add_page_to_lru_list(page, lruvec); + /* folio_evictable() has to be checked *after* clearing Mlocked */ + if (isolated && folio_test_unevictable(folio) && folio_evictable(folio)) { + lruvec_del_folio(lruvec, folio); + folio_clear_unevictable(folio); + lruvec_add_folio(lruvec, folio); __count_vm_events(UNEVICTABLE_PGRESCUED, nr_pages); } out: if (isolated) - SetPageLRU(page); + folio_set_lru(folio); return lruvec; } =20 /* - * Flags held in the low bits of a struct page pointer on the mlock_pvec. + * Flags held in the low bits of a struct folio pointer on the mlock_fbatc= h. */ #define LRU_PAGE 0x1 #define NEW_PAGE 0x2 -static inline struct page *mlock_lru(struct page *page) +static inline struct folio *mlock_lru(struct folio *folio) { - return (struct page *)((unsigned long)page + LRU_PAGE); + return (struct folio *)((unsigned long)folio + LRU_PAGE); } =20 -static inline struct page *mlock_new(struct page *page) +static inline struct folio *mlock_new(struct folio *folio) { - return (struct page *)((unsigned long)page + NEW_PAGE); + return (struct folio *)((unsigned long)folio + NEW_PAGE); } =20 /* - * mlock_pagevec() is derived from pagevec_lru_move_fn(): - * perhaps that can make use of such page pointer flags in future, - * but for now just keep it for mlock. We could use three separate - * pagevecs instead, but one feels better (munlocking a full pagevec - * does not need to drain mlocking pagevecs first). + * mlock_folio_batch() is derived from folio_batch_move_lru(): perhaps tha= t can + * make use of such page pointer flags in future, but for now just keep it= for + * mlock. We could use three separate folio batches instead, but one feels + * better (munlocking a full folio batch does not need to drain mlocking f= olio + * batches first). */ -static void mlock_pagevec(struct pagevec *pvec) +static void mlock_folio_batch(struct folio_batch *fbatch) { struct lruvec *lruvec =3D NULL; unsigned long mlock; - struct page *page; + struct folio *folio; int i; =20 - for (i =3D 0; i < pagevec_count(pvec); i++) { - page =3D pvec->pages[i]; - mlock =3D (unsigned long)page & (LRU_PAGE | NEW_PAGE); - page =3D (struct page *)((unsigned long)page - mlock); - pvec->pages[i] =3D page; + for (i =3D 0; i < folio_batch_count(fbatch); i++) { + folio =3D fbatch->folios[i]; + mlock =3D (unsigned long)folio & (LRU_PAGE | NEW_PAGE); + folio =3D (struct folio *)((unsigned long)folio - mlock); + fbatch->folios[i] =3D folio; =20 if (mlock & LRU_PAGE) - lruvec =3D __mlock_page(page, lruvec); + lruvec =3D __mlock_folio(folio, lruvec); else if (mlock & NEW_PAGE) - lruvec =3D __mlock_new_page(page, lruvec); + lruvec =3D __mlock_new_folio(folio, lruvec); else - lruvec =3D __munlock_page(page, lruvec); + lruvec =3D __munlock_folio(folio, lruvec); } =20 if (lruvec) unlock_page_lruvec_irq(lruvec); - release_pages(pvec->pages, pvec->nr); - pagevec_reinit(pvec); + release_pages(fbatch->folios, fbatch->nr); + folio_batch_reinit(fbatch); } =20 void mlock_page_drain_local(void) { - struct pagevec *pvec; + struct folio_batch *fbatch; =20 - local_lock(&mlock_pvec.lock); - pvec =3D this_cpu_ptr(&mlock_pvec.vec); - if (pagevec_count(pvec)) - mlock_pagevec(pvec); - local_unlock(&mlock_pvec.lock); + local_lock(&mlock_fbatch.lock); + fbatch =3D this_cpu_ptr(&mlock_fbatch.fbatch); + if (folio_batch_count(fbatch)) + mlock_folio_batch(fbatch); + local_unlock(&mlock_fbatch.lock); } =20 void mlock_page_drain_remote(int cpu) { - struct pagevec *pvec; + struct folio_batch *fbatch; =20 WARN_ON_ONCE(cpu_online(cpu)); - pvec =3D &per_cpu(mlock_pvec.vec, cpu); - if (pagevec_count(pvec)) - mlock_pagevec(pvec); + fbatch =3D &per_cpu(mlock_fbatch.fbatch, cpu); + if (folio_batch_count(fbatch)) + mlock_folio_batch(fbatch); } =20 bool need_mlock_page_drain(int cpu) { - return pagevec_count(&per_cpu(mlock_pvec.vec, cpu)); + return folio_batch_count(&per_cpu(mlock_fbatch.fbatch, cpu)); } =20 /** @@ -242,10 +242,10 @@ bool need_mlock_page_drain(int cpu) */ void mlock_folio(struct folio *folio) { - struct pagevec *pvec; + struct folio_batch *fbatch; =20 - local_lock(&mlock_pvec.lock); - pvec =3D this_cpu_ptr(&mlock_pvec.vec); + local_lock(&mlock_fbatch.lock); + fbatch =3D this_cpu_ptr(&mlock_fbatch.fbatch); =20 if (!folio_test_set_mlocked(folio)) { int nr_pages =3D folio_nr_pages(folio); @@ -255,10 +255,10 @@ void mlock_folio(struct folio *folio) } =20 folio_get(folio); - if (!pagevec_add(pvec, mlock_lru(&folio->page)) || + if (!folio_batch_add(fbatch, mlock_lru(folio)) || folio_test_large(folio) || lru_cache_disabled()) - mlock_pagevec(pvec); - local_unlock(&mlock_pvec.lock); + mlock_folio_batch(fbatch); + local_unlock(&mlock_fbatch.lock); } =20 /** @@ -267,20 +267,22 @@ void mlock_folio(struct folio *folio) */ void mlock_new_page(struct page *page) { - struct pagevec *pvec; - int nr_pages =3D thp_nr_pages(page); + struct folio_batch *fbatch; + struct folio *folio =3D page_folio(page); + int nr_pages =3D folio_nr_pages(folio); =20 - local_lock(&mlock_pvec.lock); - pvec =3D this_cpu_ptr(&mlock_pvec.vec); - SetPageMlocked(page); - mod_zone_page_state(page_zone(page), NR_MLOCK, nr_pages); + local_lock(&mlock_fbatch.lock); + fbatch =3D this_cpu_ptr(&mlock_fbatch.fbatch); + folio_set_mlocked(folio); + + zone_stat_mod_folio(folio, NR_MLOCK, nr_pages); __count_vm_events(UNEVICTABLE_PGMLOCKED, nr_pages); =20 - get_page(page); - if (!pagevec_add(pvec, mlock_new(page)) || - PageHead(page) || lru_cache_disabled()) - mlock_pagevec(pvec); - local_unlock(&mlock_pvec.lock); + folio_get(folio); + if (!folio_batch_add(fbatch, mlock_new(folio)) || + folio_test_large(folio) || lru_cache_disabled()) + mlock_folio_batch(fbatch); + local_unlock(&mlock_fbatch.lock); } =20 /** @@ -289,20 +291,20 @@ void mlock_new_page(struct page *page) */ void munlock_page(struct page *page) { - struct pagevec *pvec; + struct folio_batch *fbatch; + struct folio *folio =3D page_folio(page); =20 - local_lock(&mlock_pvec.lock); - pvec =3D this_cpu_ptr(&mlock_pvec.vec); + local_lock(&mlock_fbatch.lock); + fbatch =3D this_cpu_ptr(&mlock_fbatch.fbatch); /* - * TestClearPageMlocked(page) must be left to __munlock_page(), - * which will check whether the page is multiply mlocked. + * folio_test_clear_mlocked(folio) must be left to __munlock_folio(), + * which will check whether the folio is multiply mlocked. */ - - get_page(page); - if (!pagevec_add(pvec, page) || - PageHead(page) || lru_cache_disabled()) - mlock_pagevec(pvec); - local_unlock(&mlock_pvec.lock); + folio_get(folio); + if (!folio_batch_add(fbatch, folio) || + folio_test_large(folio) || lru_cache_disabled()) + mlock_folio_batch(fbatch); + local_unlock(&mlock_fbatch.lock); } =20 static int mlock_pte_range(pmd_t *pmd, unsigned long addr, --=20 2.39.0 From nobody Wed Sep 17 01:34:39 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6F473C4332F for ; Mon, 26 Dec 2022 07:09:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230059AbiLZHJS (ORCPT ); Mon, 26 Dec 2022 02:09:18 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51212 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231685AbiLZHJB (ORCPT ); Mon, 26 Dec 2022 02:09:01 -0500 Received: from mail-wm1-x32a.google.com (mail-wm1-x32a.google.com [IPv6:2a00:1450:4864:20::32a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0EC7321AA for ; Sun, 25 Dec 2022 23:09:00 -0800 (PST) Received: by mail-wm1-x32a.google.com with SMTP id l26so5558264wme.5 for ; Sun, 25 Dec 2022 23:08:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; 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=PvP9A6XKAl/v1o7+nXXmkZgCax5Ofzh4VaB88gRKyTM=; b=ky8mj0aMYqAH26amqbof509vhFcQfx/R9JD++pBJfS9LkQ5h7Ku50RfP+Qwr6uqUNV N6jemG0/tTfyufN/1FVlI2FBfR/4NQES9dCn1oBqKrt4hJDGUijI5JwJAsfPa6PZiHze 8taysu/+hfy9TDEcqWUKrxtRdn1IaAA5UBZjBgEErAA4K9QvRPdGwQ/THSI5w+g5Q28o H86M3tygigaBd1IuDGha4O5hp6AbLl0034NdnOvrY8o9ciDPToSU1t3kPp5zPgi9Nu18 7boxfKn+etohB+OD9Kz/HBTbZOmQeX/6V+/Bhvo7KUJ4VUWuzpRUDGrJk2mPHjZ61p9n +Ynw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=PvP9A6XKAl/v1o7+nXXmkZgCax5Ofzh4VaB88gRKyTM=; b=iYLFnLmSgih0fa1NEZunINR4GjUzYDmdL9EWolCd0KxBBr0hg38YDNC/9THSAJ0wM4 H15EjA9uoKbII+7ezY2Cz/tYOR430qePbz9lmt+hfg+m9xiadgCQYZX5Od2kXz2/PySn q9IJg9l2qa2yC9K0Qr7hPOpBxCcNrFrxDGlOfqWU49arfehUZWtT3PnCYe8m1xKZckD3 c6gZhxYSEPPK1HTrXeZm3hhXf0649UMepYadNL43bw7V27i04H5bqUxfSIe0OsWtXdkb S9L3etI6QdQ/62MCdJCrrQ08ggER13Ne1DE9xybj5Q+FpaN/M7WupTgjMZ3OqyxeCTOx T5wQ== X-Gm-Message-State: AFqh2krhhWN7EHvoryEefYO20HNPZYXn2JzkeSmN3ZuHKZayqJXTasYv b+U6zUAHfwFgMtculOcXgBM= X-Google-Smtp-Source: AMrXdXvP/oOJeqG/K19E0NDhr+3RSqXJeDQAjARpuQZuKzrvSV8HjWUvLNnrxYVZJF148QZamrvUcg== X-Received: by 2002:a05:600c:4f55:b0:3d3:5166:2da4 with SMTP id m21-20020a05600c4f5500b003d351662da4mr12246298wmq.8.1672038538554; Sun, 25 Dec 2022 23:08:58 -0800 (PST) Received: from lucifer.home ([2a00:23c5:dc8c:8701:1663:9a35:5a7b:1d76]) by smtp.googlemail.com with ESMTPSA id e16-20020a05600c4e5000b003c21ba7d7d6sm13191456wmq.44.2022.12.25.23.08.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 25 Dec 2022 23:08:57 -0800 (PST) From: Lorenzo Stoakes To: linux-mm@kvack.org, Andrew Morton , linux-kernel@vger.kernel.org Cc: Matthew Wilcox , Hugh Dickins , Vlastimil Babka , Liam Howlett , William Kucharski , Christian Brauner , Jonathan Corbet , Mike Rapoport , Joel Fernandes , Lorenzo Stoakes Subject: [PATCH v2 3/4] mm: mlock: update the interface to use folios Date: Mon, 26 Dec 2022 07:08:46 +0000 Message-Id: <555c36b91c4b34a5972f2614395e3c3831e8102f.1672038314.git.lstoakes@gmail.com> X-Mailer: git-send-email 2.39.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This patch updates the mlock interface to accept folios rather than pages, bringing the interface in line with the internal implementation. munlock_vma_page() still requires a page_folio() conversion, however this is consistent with the existent mlock_vma_page() implementation and a product of rmap still dealing in pages rather than folios. Signed-off-by: Lorenzo Stoakes --- mm/internal.h | 26 ++++++++++++++++---------- mm/mlock.c | 32 +++++++++++++++----------------- mm/swap.c | 2 +- 3 files changed, 32 insertions(+), 28 deletions(-) diff --git a/mm/internal.h b/mm/internal.h index 1d6f4e168510..8a6e83315369 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -515,10 +515,9 @@ extern int mlock_future_check(struct mm_struct *mm, un= signed long flags, * should be called with vma's mmap_lock held for read or write, * under page table lock for the pte/pmd being added or removed. * - * mlock is usually called at the end of page_add_*_rmap(), - * munlock at the end of page_remove_rmap(); but new anon - * pages are managed by lru_cache_add_inactive_or_unevictable() - * calling mlock_new_page(). + * mlock is usually called at the end of page_add_*_rmap(), munlock at + * the end of page_remove_rmap(); but new anon folios are managed by + * folio_add_lru_vma() calling mlock_new_folio(). * * @compound is used to include pmd mappings of THPs, but filter out * pte mappings of THPs, which cannot be consistently counted: a pte @@ -547,15 +546,22 @@ static inline void mlock_vma_page(struct page *page, mlock_vma_folio(page_folio(page), vma, compound); } =20 -void munlock_page(struct page *page); -static inline void munlock_vma_page(struct page *page, +void munlock_folio(struct folio *folio); + +static inline void munlock_vma_folio(struct folio *folio, struct vm_area_struct *vma, bool compound) { if (unlikely(vma->vm_flags & VM_LOCKED) && - (compound || !PageTransCompound(page))) - munlock_page(page); + (compound || !folio_test_large(folio))) + munlock_folio(folio); +} + +static inline void munlock_vma_page(struct page *page, + struct vm_area_struct *vma, bool compound) +{ + munlock_vma_folio(page_folio(page), vma, compound); } -void mlock_new_page(struct page *page); +void mlock_new_folio(struct folio *folio); bool need_mlock_page_drain(int cpu); void mlock_page_drain_local(void); void mlock_page_drain_remote(int cpu); @@ -647,7 +653,7 @@ static inline void mlock_vma_page(struct page *page, struct vm_area_struct *vma, bool compound) { } static inline void munlock_vma_page(struct page *page, struct vm_area_struct *vma, bool compound) { } -static inline void mlock_new_page(struct page *page) { } +static inline void mlock_new_folio(struct folio *folio) { } static inline bool need_mlock_page_drain(int cpu) { return false; } static inline void mlock_page_drain_local(void) { } static inline void mlock_page_drain_remote(int cpu) { } diff --git a/mm/mlock.c b/mm/mlock.c index e9ba47fe67ed..0317b33c727f 100644 --- a/mm/mlock.c +++ b/mm/mlock.c @@ -262,13 +262,12 @@ void mlock_folio(struct folio *folio) } =20 /** - * mlock_new_page - mlock a newly allocated page not yet on LRU - * @page: page to be mlocked, either a normal page or a THP head. + * mlock_new_folio - mlock a newly allocated folio not yet on LRU + * @folio: folio to be mlocked, either normal or a THP head. */ -void mlock_new_page(struct page *page) +void mlock_new_folio(struct folio *folio) { struct folio_batch *fbatch; - struct folio *folio =3D page_folio(page); int nr_pages =3D folio_nr_pages(folio); =20 local_lock(&mlock_fbatch.lock); @@ -286,13 +285,12 @@ void mlock_new_page(struct page *page) } =20 /** - * munlock_page - munlock a page - * @page: page to be munlocked, either a normal page or a THP head. + * munlock_folio - munlock a folio + * @folio: folio to be munlocked, either normal or a THP head. */ -void munlock_page(struct page *page) +void munlock_folio(struct folio *folio) { struct folio_batch *fbatch; - struct folio *folio =3D page_folio(page); =20 local_lock(&mlock_fbatch.lock); fbatch =3D this_cpu_ptr(&mlock_fbatch.fbatch); @@ -314,7 +312,7 @@ static int mlock_pte_range(pmd_t *pmd, unsigned long ad= dr, struct vm_area_struct *vma =3D walk->vma; spinlock_t *ptl; pte_t *start_pte, *pte; - struct page *page; + struct folio *folio; =20 ptl =3D pmd_trans_huge_lock(pmd, vma); if (ptl) { @@ -322,11 +320,11 @@ static int mlock_pte_range(pmd_t *pmd, unsigned long = addr, goto out; if (is_huge_zero_pmd(*pmd)) goto out; - page =3D pmd_page(*pmd); + folio =3D page_folio((struct page *)pmd_page(*pmd)); if (vma->vm_flags & VM_LOCKED) - mlock_folio(page_folio(page)); + mlock_folio(folio); else - munlock_page(page); + munlock_folio(folio); goto out; } =20 @@ -334,15 +332,15 @@ static int mlock_pte_range(pmd_t *pmd, unsigned long = addr, for (pte =3D start_pte; addr !=3D end; pte++, addr +=3D PAGE_SIZE) { if (!pte_present(*pte)) continue; - page =3D vm_normal_page(vma, addr, *pte); - if (!page || is_zone_device_page(page)) + folio =3D vm_normal_folio(vma, addr, *pte); + if (!folio || folio_is_zone_device(folio)) continue; - if (PageTransCompound(page)) + if (folio_test_large(folio)) continue; if (vma->vm_flags & VM_LOCKED) - mlock_folio(page_folio(page)); + mlock_folio(folio); else - munlock_page(page); + munlock_folio(folio); } pte_unmap(start_pte); out: diff --git a/mm/swap.c b/mm/swap.c index e54e2a252e27..7df297b143f9 100644 --- a/mm/swap.c +++ b/mm/swap.c @@ -562,7 +562,7 @@ void folio_add_lru_vma(struct folio *folio, struct vm_a= rea_struct *vma) VM_BUG_ON_FOLIO(folio_test_lru(folio), folio); =20 if (unlikely((vma->vm_flags & (VM_LOCKED | VM_SPECIAL)) =3D=3D VM_LOCKED)) - mlock_new_page(&folio->page); + mlock_new_folio(folio); else folio_add_lru(folio); } --=20 2.39.0 From nobody Wed Sep 17 01:34:39 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 511DCC4332F for ; Mon, 26 Dec 2022 07:09:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231733AbiLZHJX (ORCPT ); Mon, 26 Dec 2022 02:09:23 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51248 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231691AbiLZHJC (ORCPT ); Mon, 26 Dec 2022 02:09:02 -0500 Received: from mail-wm1-x334.google.com (mail-wm1-x334.google.com [IPv6:2a00:1450:4864:20::334]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 739EE2626 for ; Sun, 25 Dec 2022 23:09:01 -0800 (PST) Received: by mail-wm1-x334.google.com with SMTP id p13-20020a05600c468d00b003cf8859ed1bso7116368wmo.1 for ; Sun, 25 Dec 2022 23:09:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; 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=r2F81c9AgLyck1wSShCjUojXRC7u44cFLH6IR6oWmN4=; b=F4i0E9Wu0NYud28tdJXiEWfYSh10GD22IJXdzIrA/XakhY5Iy9YwrPJDCCKplhFIJM UpKqvP2hYOs4H2MRo+xIrANw3FVpLZB3h1Zs2ur8QtHr4IOhP9pD/eMWEgPjUFr/oC2e NiQ6eHvO8KfOOv2KdvU44EiLEpNwn1Bx698xA316j4lVjoXs9QuraRsCpkJx/TmYyjKL b5qKRX5nyf2FaaPrbtRz/Gl64Pfc3BGxJyU0JncF5xHxdqGlfhGMBpAF4CmeODFz8nRB Ub8b07H0VeAnLMipDxeLl7XNrk4T45Qqcf7J/nbXJs8CpDlxBlRlFDgt8pwbT+X3XJvP kl3g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=r2F81c9AgLyck1wSShCjUojXRC7u44cFLH6IR6oWmN4=; b=OvGLaIng9C7VHPfFFOx8I+qjQ740gqFavBG8Uza6NTTixqFMjuu1jkM2/oF+KneLmf S3ExUs9BK+eMnw10KQma27ejitoT939qkRcvHw8OncIGaaKMLO1L15luC4pCsTptCdRZ /DRrAVc1bDeetoVmifFE5eMhJvwyeXklm3nyzisKJ1OdAMPrr3dn9BQatQ/U8O93jUKm +qrF+F/cPDW93XMWd+sqFMb2YtVvS+Xl2XeAgUGU+bSxncklZaFBey1HeRpDeHLaE8o/ jAo5xDD5XpFp0LTRiWQ0TGuyLjI8TtSGida5JGUWSDqhDKe/AqIFBc1gLoRCPAz1VtpQ tTKA== X-Gm-Message-State: AFqh2kpZj+cjZmrUbq1FYrj/GTUiCJMM7TNEsOT4DFvCaYN/6/VO8nFT cqYNi6shogqG/68UcXMsnKc= X-Google-Smtp-Source: AMrXdXvZCSVjc3Yf5QlYu7hAnys5nckvEYVgPo1bv5CypB6+nBC4XDwiD7UyHjoPk9yO9Qq54Pid/g== X-Received: by 2002:a05:600c:378c:b0:3d2:1f10:3318 with SMTP id o12-20020a05600c378c00b003d21f103318mr15193927wmr.31.1672038539911; Sun, 25 Dec 2022 23:08:59 -0800 (PST) Received: from lucifer.home ([2a00:23c5:dc8c:8701:1663:9a35:5a7b:1d76]) by smtp.googlemail.com with ESMTPSA id e16-20020a05600c4e5000b003c21ba7d7d6sm13191456wmq.44.2022.12.25.23.08.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 25 Dec 2022 23:08:59 -0800 (PST) From: Lorenzo Stoakes To: linux-mm@kvack.org, Andrew Morton , linux-kernel@vger.kernel.org Cc: Matthew Wilcox , Hugh Dickins , Vlastimil Babka , Liam Howlett , William Kucharski , Christian Brauner , Jonathan Corbet , Mike Rapoport , Joel Fernandes , Lorenzo Stoakes Subject: [PATCH v2 4/4] Documentation/mm: Update references to __m[un]lock_page() to *_folio() Date: Mon, 26 Dec 2022 07:08:47 +0000 Message-Id: <54006f75cb3c03b98e5a3d0968294db8c6889089.1672038314.git.lstoakes@gmail.com> X-Mailer: git-send-email 2.39.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" We now pass folios to these functions, so update the documentation accordingly. Additionally, correct the outdated reference to __pagevec_lru_add_fn(), the referenced action occurs in __munlock_folio() directly now. Signed-off-by: Lorenzo Stoakes --- Documentation/mm/unevictable-lru.rst | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Documentation/mm/unevictable-lru.rst b/Documentation/mm/unevic= table-lru.rst index 4a0e158aa9ce..153629e0c100 100644 --- a/Documentation/mm/unevictable-lru.rst +++ b/Documentation/mm/unevictable-lru.rst @@ -308,22 +308,22 @@ do end up getting faulted into this VM_LOCKED VMA, th= ey will be handled in the fault path - which is also how mlock2()'s MLOCK_ONFAULT areas are handled. =20 For each PTE (or PMD) being faulted into a VMA, the page add rmap function -calls mlock_vma_page(), which calls mlock_page() when the VMA is VM_LOCKED +calls mlock_vma_page(), which calls mlock_folio() when the VMA is VM_LOCKED (unless it is a PTE mapping of a part of a transparent huge page). Or when it is a newly allocated anonymous page, lru_cache_add_inactive_or_unevicta= ble() -calls mlock_new_page() instead: similar to mlock_page(), but can make bett= er +calls mlock_new_folio() instead: similar to mlock_folio(), but can make be= tter judgments, since this page is held exclusively and known not to be on LRU = yet. =20 -mlock_page() sets PageMlocked immediately, then places the page on the CPU= 's -mlock pagevec, to batch up the rest of the work to be done under lru_lock = by -__mlock_page(). __mlock_page() sets PageUnevictable, initializes mlock_co= unt +mlock_folio() sets PageMlocked immediately, then places the page on the CP= U's +mlock folio batch, to batch up the rest of the work to be done under lru_l= ock by +__mlock_folio(). __mlock_folio() sets PageUnevictable, initializes mlock_= count and moves the page to unevictable state ("the unevictable LRU", but with mlock_count in place of LRU threading). Or if the page was already PageLRU and PageUnevictable and PageMlocked, it simply increments the mlock_count. =20 But in practice that may not work ideally: the page may not yet be on an L= RU, or it may have been temporarily isolated from LRU. In such cases the mlock_c= ount -field cannot be touched, but will be set to 0 later when __pagevec_lru_add= _fn() +field cannot be touched, but will be set to 0 later when __munlock_folio() returns the page to "LRU". Races prohibit mlock_count from being set to 1= then: rather than risk stranding a page indefinitely as unevictable, always err = with mlock_count on the low side, so that when munlocked the page will be rescu= ed to --=20 2.39.0