From patchwork Fri Jan 17 16:29:47 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13943548 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 D7CBBC02188 for ; Fri, 17 Jan 2025 16:30:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 66EAB6B008A; Fri, 17 Jan 2025 11:30:09 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 646316B008C; Fri, 17 Jan 2025 11:30:09 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4724D6B0092; Fri, 17 Jan 2025 11:30:09 -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 2CBB86B008A for ; Fri, 17 Jan 2025 11:30:09 -0500 (EST) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id DBD71160600 for ; Fri, 17 Jan 2025 16:30:08 +0000 (UTC) X-FDA: 83017480896.29.71C6DA0 Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) by imf21.hostedemail.com (Postfix) with ESMTP id E9F6A1C001B for ; Fri, 17 Jan 2025 16:30:06 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=umF23x0T; spf=pass (imf21.hostedemail.com: domain of 3jYWKZwUKCAYzghhgmuumrk.iusrot03-ssq1giq.uxm@flex--tabba.bounces.google.com designates 209.85.128.74 as permitted sender) smtp.mailfrom=3jYWKZwUKCAYzghhgmuumrk.iusrot03-ssq1giq.uxm@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=1737131407; 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=MygWVLHFA/Ogghouk9TRsaDRRHDK/dBL9FKQqgrSGb0=; b=hXuoMgDrUSATHrcaGl0aHSnA19fQCWs5FlIFGvKpTMYPmuWOXWUv7Mj0DCsATPDp9XcdbM /AImDleUfnBU7Fn+4bBrtXXJf4RkxR9vGKcAKY9tR1spBD8VxMoUPqZKQc2CcM/TheX7Qb zjEL1tFpYkSjdXS+QPjJ6ZR1qxPpWuI= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=umF23x0T; spf=pass (imf21.hostedemail.com: domain of 3jYWKZwUKCAYzghhgmuumrk.iusrot03-ssq1giq.uxm@flex--tabba.bounces.google.com designates 209.85.128.74 as permitted sender) smtp.mailfrom=3jYWKZwUKCAYzghhgmuumrk.iusrot03-ssq1giq.uxm@flex--tabba.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737131407; a=rsa-sha256; cv=none; b=rqyJ3UmI9SJ82H7FvJb0DqwriTl22zmjWHvoua0Gjhxg8gqE77SXa8k3+VMVHkSrj0weXy 2TuRrErBoPpzHN1oyT1vcOP91KYHEia8+wMzx7YlMx00XwgpJ5T+N9PORvz8Ht1FuiWkSk I+PRV813mMb/79lHP7Od05hp7P2lgYs= Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-4361efc9d1fso16507265e9.2 for ; Fri, 17 Jan 2025 08:30:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1737131405; x=1737736205; 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=MygWVLHFA/Ogghouk9TRsaDRRHDK/dBL9FKQqgrSGb0=; b=umF23x0Tk3a85+gQtmzN+3aFnpW3JcQ3N6Zd8d/BLWdBNMDIdoXp8QmlQzBixVUODC HcLtbxN5Ns02nNcRqYXTGsiVSt0cuqNr/nPtjtVEJjq/AgYYVA2/7uNjSNn7RFLv6K1J KAvdNMxseKnRh2+4gvhe32C4Wat1Y3hz8fG8bmt1jkfwc11bu9nwoG7fqK01JgnS1sVl 8DDuMgb5pCgc5Yo4lXfnPMpgBTwa5VyCLAaUnuIYiknMJi0Rn7cVWSVttAcZW8rqU6NU b4dhyuQxYZnbjuYaakBXhzLUWabuoiVbsSS4ZPgsS/2zFMTWyCUyJ0GjefcKFVlYtIya HD5g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737131405; x=1737736205; 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=MygWVLHFA/Ogghouk9TRsaDRRHDK/dBL9FKQqgrSGb0=; b=kGClyYNPuu1RCpQqZDquJJWXYdGeWffHeW8dixhqhwcGXRyOu+wI3DNZYyXeZB+GLM mbA0kwrBPedi0jq3BQrNruJp9z2wzicTJ9S9fpolDSLssKxqqyqm9SNLCSDh/SrLaQmr aGqXOCdqVIoMpgtSDJL2iHhWpIYthzhZdw8aVqjPQAAf1JNFWq85bfzVqo3QLaUB4Q4s 0vj0WMnd59vZE5OuBmgqI+CDtj0/DxepoJcG6IQB4FhMs6JW582L+n6zv0p9BqbicMdq mAnC7wK9K7VV14P9XkufsjJc9fmaNewy+9iQBhhCSdYXLtwDYLA878fATTTPCUM/UgqS 3ZOw== X-Forwarded-Encrypted: i=1; AJvYcCWtJfn9/F7wCm/BpIXSic4svhozxuGpDsVBXO+3PX3vx2eIRPTF4P7xuSxHh9IK3ai7hBT6D1jE3Q==@kvack.org X-Gm-Message-State: AOJu0YyGDxflAWKnMgEkM/DJ9y8LcOsrpxye4C88YG30d/zLw9wDZwjN E1JEPz6j9jR8Xk/fS79wmaEAHCVxUnQ37LMbavjE5NyWmDnx5Cg2YOwkM3PqlUkHcyiu5UlIzg= = X-Google-Smtp-Source: AGHT+IEHuE0O1Pmu3qZGQgThvn78WhmP89neBbw/jiEtDzguRVKTsO5hOjuvNpTFKXWiuoSlKkap9d2uOA== X-Received: from wmsp9.prod.google.com ([2002:a05:600c:1d89:b0:434:9da4:2fa5]) (user=tabba job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:524c:b0:432:7c08:d0ff with SMTP id 5b1f17b1804b1-4389143768fmr35144945e9.23.1737131405542; Fri, 17 Jan 2025 08:30:05 -0800 (PST) Date: Fri, 17 Jan 2025 16:29:47 +0000 In-Reply-To: <20250117163001.2326672-1-tabba@google.com> Mime-Version: 1.0 References: <20250117163001.2326672-1-tabba@google.com> X-Mailer: git-send-email 2.48.0.rc2.279.g1de40edade-goog Message-ID: <20250117163001.2326672-2-tabba@google.com> Subject: [RFC PATCH v5 01/15] 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-Queue-Id: E9F6A1C001B X-Stat-Signature: 5ccdb4ijisb5scj6n7hbbwtzxbo7ay3n X-Rspamd-Server: rspam08 X-Rspam-User: X-HE-Tag: 1737131406-26934 X-HE-Meta: U2FsdGVkX19Sj8nmEH/UbRoK60kmGWtrUqmAGsbk7DYNtp8IW+3oWiy42Sa3KmpmoLrA+Hv5zvP0IRWTnZiVg0BGzsWG5PJIPbl1Xd5HczEOyxhaUVwsm5V7AJMSgF+Mdo2Vocj9o8lUg3yInkd5Zy+80aAQp90wcyy+AD0QHlkwuhtFk01KEzVm0IvFr78IlHQZhXQPN3XJdhI9K4nD9Tlkm/yuJdRs/L8IZZNz1Xnsg8Euj38WYNVruNiynt0TZjnPBqvcCUppwmlQldyBzaEbHzrJP523hJGO72u1B4GL8dRb0Hv3KrfIEfGquB9HN87bN/POXZboSN+3mklM6Dih0ewitntlZnqo8k2LjPeGatw2U1quWeJ2KUH/KaFHorQd3h06xolOghEUq3c6i1pov/Ro66tL8W2+tFh0wAVOM1G4bkgjRU9tOFgtgoNyVuyI4sA95oBU/Ai53UmUKHJ7U3Du2pSNJ2xpy082cU5vJKrUN3up6B3Hwa/d4Q7L3VUbboer6y6rnTeDCGqFDbJqjKBMOuma8tTb6ORNc0U0aOD2FwBmPejoY3vdowJ2zdbucE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nJJtvk3MX0UCI2Qul7p9bVBQQXPnCs= 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: 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 | 24 +++++++++++++++++++----- 2 files changed, 34 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..6f01b56bce13 100644 --- a/mm/swap.c +++ b/mm/swap.c @@ -94,6 +94,20 @@ 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: + free_huge_folio(folio); + return; + case PGTY_offline: + /* Nothing to do, it's offline. */ + return; + default: + WARN_ON_ONCE(1); + } +} + void __folio_put(struct folio *folio) { if (unlikely(folio_is_zone_device(folio))) { @@ -101,8 +115,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 +948,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);