From patchwork Wed Jan 29 17:23:10 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13953966 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 E2709C02193 for ; Wed, 29 Jan 2025 17:23:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F057328007F; Wed, 29 Jan 2025 12:23:30 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E8B5C280067; Wed, 29 Jan 2025 12:23:30 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C9215280080; Wed, 29 Jan 2025 12:23:30 -0500 (EST) 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 A04A9280067 for ; Wed, 29 Jan 2025 12:23:30 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id B77F4A0FD1 for ; Wed, 29 Jan 2025 17:23:27 +0000 (UTC) X-FDA: 83061160854.14.5ED393B Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) by imf01.hostedemail.com (Postfix) with ESMTP id E1DE640015 for ; Wed, 29 Jan 2025 17:23:25 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=dPoqhIZT; spf=pass (imf01.hostedemail.com: domain of 3DGSaZwUKCIEyfggflttlqj.htrqnsz2-rrp0fhp.twl@flex--tabba.bounces.google.com designates 209.85.128.74 as permitted sender) smtp.mailfrom=3DGSaZwUKCIEyfggflttlqj.htrqnsz2-rrp0fhp.twl@flex--tabba.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1738171406; 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-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=iDaVGDLi8mgRhdRmBEo1g3AHoA0NuWvKpi8mcLq23xA=; b=MKMpHBGKJteFHvIM6N2AwoPxRYMZyfEh9KCT+Z8oVOszC46EL4C6okq8/vit2DRvBGjR4u JQTJfvn0R4tLrOi8dgpCgftEdjO5U7XHmdCQ10EWN5yrOzVAaB26A6lzy1Ep4zjWi0bqQd u+FuvPaXjnLn9zzuTdwmKp6Xd4FvUEU= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=dPoqhIZT; spf=pass (imf01.hostedemail.com: domain of 3DGSaZwUKCIEyfggflttlqj.htrqnsz2-rrp0fhp.twl@flex--tabba.bounces.google.com designates 209.85.128.74 as permitted sender) smtp.mailfrom=3DGSaZwUKCIEyfggflttlqj.htrqnsz2-rrp0fhp.twl@flex--tabba.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1738171406; a=rsa-sha256; cv=none; b=ucSJhOqvl5sqmeesAtA/QXjWIsletfsa44QpZq9KGrf8ak2cnbTCAxjI5bIx7MKnb59MDQ UQ4kXVo3B5x7yGpecvvRlZ6GjZligk4LgbPXJ0yznwG/TZKVl7iU6O1N5AV10Bw+V4meYv 75U0cjMC+m3gLucpLSKLMuGVC3fVE2o= Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-438da39bb69so12012775e9.0 for ; Wed, 29 Jan 2025 09:23:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738171404; x=1738776204; darn=kvack.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=iDaVGDLi8mgRhdRmBEo1g3AHoA0NuWvKpi8mcLq23xA=; b=dPoqhIZT9SCp0D1B9W6eVW7Eh1cswWwGM43eLz/oMys7KBgXuruAWJZk4sEXvD4Gww W72nCnx7hsrhECWDdUO/VTXg1IFxgGCM3BFfiZH3syWnhZnIgG2rUkTJo9Of6LG6rE+P CfA4iGPDmQKFle7tGk4wUBS66RP6MrA7wyt3vi9qznf6MywB+U7ehPr33/qSIx+MJd5y r1IXA6nIBsVKWMITWRGdN9Y/9YUxgYD3ydFhXsVoPWvDpFtVolLdUCBw4H+eXHS1HnrH lUFxnimsBmTIRBH7pR/bqcVH+2KOZiAJne+FR/mrcYwWsWmhkxbPCBujMy7wOtuNNyMw BLtw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738171404; x=1738776204; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=iDaVGDLi8mgRhdRmBEo1g3AHoA0NuWvKpi8mcLq23xA=; b=BFBkP1pPyfCcwHiyJo89FFcJosFI2MGzGiNjfOYwifeIHQXLGYHZnpJdxvQ/8Z1PxV e9V0nBkr8qlfg6giRlGyTv14qCQOGzsFoh5emWR3udIyKCwq8WBOaDjOOMv/9PjAq9d2 aLBg0f9e8hGesii/62CvYXoHYYU6++1LE6prqFhIRzQ6w1JpO/rURXZ1ZBAgF2PgyLS0 nLA54LmLTxxDrAHXkoCBYXF16FR9pWk2Ge88/tV86NZ9+WuI8hthH26fm8cGINP2BGIf tr3Hc9tkgphiU/Vc2PK58DBIuLnGOuGfI29A0vaDginCZ8LrUUWNTEVO06+rla8lcBzf 6ObA== X-Forwarded-Encrypted: i=1; AJvYcCUSL87Nv6+cY3qwjXxFdl0danpS4MVp7+PiulwKmnWgV1DR3OjhVoNU44MOlDmtTe3N9/PWMDe1Fw==@kvack.org X-Gm-Message-State: AOJu0YzukoicTRUSFpD8uI5H3h31RsoXWrKyidKwv3iX9ZOY44rSJ5IQ x7UqHeZwC7dMYLREtOOrZaaaVed7DWuOpMk/EYfdYbbu6xWkMV2iEI2ncglIi5x634t1oF9fkQ= = X-Google-Smtp-Source: AGHT+IG7AAxXO9uUL/TV8lWRjQ0ckYe36dATBMk94k2BjGqrWcfbwhKyc4W7DSJHTwQDQb5yqHpv0ii9Sw== X-Received: from wmbbh25.prod.google.com ([2002:a05:600c:3d19:b0:434:f173:a51]) (user=tabba job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:1d26:b0:42c:b9c8:2bb0 with SMTP id 5b1f17b1804b1-438dc3aa595mr34124635e9.4.1738171404499; Wed, 29 Jan 2025 09:23:24 -0800 (PST) Date: Wed, 29 Jan 2025 17:23:10 +0000 In-Reply-To: <20250129172320.950523-1-tabba@google.com> Mime-Version: 1.0 References: <20250129172320.950523-1-tabba@google.com> X-Mailer: git-send-email 2.48.1.262.g85cc9f2d1e-goog Message-ID: <20250129172320.950523-2-tabba@google.com> Subject: [RFC PATCH v2 01/11] mm: Consolidate freeing of typed folios on final folio_put() From: Fuad Tabba To: kvm@vger.kernel.org, linux-arm-msm@vger.kernel.org, linux-mm@kvack.org Cc: pbonzini@redhat.com, chenhuacai@kernel.org, mpe@ellerman.id.au, anup@brainfault.org, paul.walmsley@sifive.com, palmer@dabbelt.com, aou@eecs.berkeley.edu, seanjc@google.com, viro@zeniv.linux.org.uk, brauner@kernel.org, willy@infradead.org, akpm@linux-foundation.org, xiaoyao.li@intel.com, yilun.xu@intel.com, chao.p.peng@linux.intel.com, jarkko@kernel.org, amoorthy@google.com, dmatlack@google.com, yu.c.zhang@linux.intel.com, isaku.yamahata@intel.com, mic@digikod.net, vbabka@suse.cz, vannapurve@google.com, ackerleytng@google.com, mail@maciej.szmigiero.name, david@redhat.com, michael.roth@amd.com, wei.w.wang@intel.com, liam.merwick@oracle.com, isaku.yamahata@gmail.com, kirill.shutemov@linux.intel.com, suzuki.poulose@arm.com, steven.price@arm.com, quic_eberman@quicinc.com, quic_mnalajal@quicinc.com, quic_tsoni@quicinc.com, quic_svaddagi@quicinc.com, quic_cvanscha@quicinc.com, quic_pderrin@quicinc.com, quic_pheragu@quicinc.com, catalin.marinas@arm.com, james.morse@arm.com, yuzenghui@huawei.com, oliver.upton@linux.dev, maz@kernel.org, will@kernel.org, qperret@google.com, keirf@google.com, roypat@amazon.co.uk, shuah@kernel.org, hch@infradead.org, jgg@nvidia.com, rientjes@google.com, jhubbard@nvidia.com, fvdl@google.com, hughd@google.com, jthoughton@google.com, tabba@google.com X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: E1DE640015 X-Stat-Signature: j5bamm15zcy5ag8ujhzf7eqaquwe8ikb X-Rspam-User: X-HE-Tag: 1738171405-660170 X-HE-Meta: U2FsdGVkX1+F/BCbJOgUUMGnaty2EoFtWI3m8m87QOw/+n2TDRTCKtnTgUjllR5G2Af5WNYqvUiD33RhS533aETsJHYflOO/RV0G6IIX4SEtSf41ulLqIz//K4/r3zReonN/6kixxBrItkbJhAhAC4q+bfjQLGTK79TmJK9AaXqB6leJqmmBSp/qqw5Qhxth/OHr9fPWNMS85wuYMDnvrC/6fL8BN0l9IcDYypLJ7AndHVuqXgZYfPlK94fL/ntpFjzwu98Uydlxu29Z66r5dKcS65YIQAub0YfSPNGBfBCbj+4QTXqQ0NLqBu4Lzv0XmLk8eosHGseDDk65zjaJk2Uv5rmBfIylHms67ip7nlmj/d1fZHnNBk69XH3RZ+SMjAfBuQYGYwbsS46war9tZNbsJkTwCcjeVD3FaBK/qmWry7Eo/4bRrVJjH4PtX5W5b+/NugKIXnWdjkhb1GjUFDdpS/I42cZ1hJeF/y6LI2cKXDF4/j+O2SVIHRZlU4doTGioLXlaU/NPlHEQXM5iL/HEwjlh7ohMqVYkh17N3kPWsGV+oTOshhUlXgu/xR4mMGND/iXOT3SexmWVG2W0HRmpLe8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ests=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: Some folio types, such as hugetlb, handle freeing their own folios. Moreover, guest_memfd will require being notified once a folio's reference count reaches 0 to facilitate shared to private folio conversion, without the folio actually being freed at that point. As a first step towards that, this patch consolidates freeing folios that have a type. The first user is hugetlb folios. Later in this patch series, guest_memfd will become the second user of this. Suggested-by: David Hildenbrand Signed-off-by: Fuad Tabba --- include/linux/page-flags.h | 15 +++++++++++++++ mm/swap.c | 22 +++++++++++++++++----- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/include/linux/page-flags.h b/include/linux/page-flags.h index 691506bdf2c5..6615f2f59144 100644 --- a/include/linux/page-flags.h +++ b/include/linux/page-flags.h @@ -962,6 +962,21 @@ static inline bool page_has_type(const struct page *page) return page_mapcount_is_type(data_race(page->page_type)); } +static inline int page_get_type(const struct page *page) +{ + return page->page_type >> 24; +} + +static inline bool folio_has_type(const struct folio *folio) +{ + return page_has_type(&folio->page); +} + +static inline int folio_get_type(const struct folio *folio) +{ + return page_get_type(&folio->page); +} + #define FOLIO_TYPE_OPS(lname, fname) \ static __always_inline bool folio_test_##fname(const struct folio *folio) \ { \ diff --git a/mm/swap.c b/mm/swap.c index 10decd9dffa1..8a66cd9cb9da 100644 --- a/mm/swap.c +++ b/mm/swap.c @@ -94,6 +94,18 @@ static void page_cache_release(struct folio *folio) unlock_page_lruvec_irqrestore(lruvec, flags); } +static void free_typed_folio(struct folio *folio) +{ + switch (folio_get_type(folio)) { + case PGTY_hugetlb: + if (IS_ENABLED(CONFIG_HUGETLBFS)) + free_huge_folio(folio); + return; + default: + WARN_ON_ONCE(1); + } +} + void __folio_put(struct folio *folio) { if (unlikely(folio_is_zone_device(folio))) { @@ -101,8 +113,8 @@ void __folio_put(struct folio *folio) return; } - if (folio_test_hugetlb(folio)) { - free_huge_folio(folio); + if (unlikely(folio_has_type(folio))) { + free_typed_folio(folio); return; } @@ -934,13 +946,13 @@ void folios_put_refs(struct folio_batch *folios, unsigned int *refs) if (!folio_ref_sub_and_test(folio, nr_refs)) continue; - /* hugetlb has its own memcg */ - if (folio_test_hugetlb(folio)) { + if (unlikely(folio_has_type(folio))) { + /* typed folios have their own memcg, if any */ if (lruvec) { unlock_page_lruvec_irqrestore(lruvec, flags); lruvec = NULL; } - free_huge_folio(folio); + free_typed_folio(folio); continue; } folio_unqueue_deferred_split(folio);