From nobody Tue Oct 7 05:19:48 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 652B02F22 for ; Mon, 14 Jul 2025 13:17:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752499038; cv=none; b=flN7nb5yXcaduU2C+tYu6xYkCL1p2TPi9eK/HGI1/ceJ+8IlDROZPmtySnfzCBZqtOtRrhiBhF75Z+jn1iolPVes+9nKFDjTCDgLlW0Iwu66hvbONM8r/vyNgSEbMiVt+MzIDJmsck6kF7f28eWSetzrJAICjnElsF/+XBQDw2I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752499038; c=relaxed/simple; bh=e9V2W1eJzVV6zWm35TffsJfx2D9Kv0OhKkXnyzMbG7U=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=I9JNZqunm2Yo2o8lazIzJwPoaiMzEf5xOSY+t+6aC46p6n7t1ZNYsxUiC3fTOVjRJ+0Mm8Dh/ILiA5bkcxirR2i2R5Q3l3sNjkZmPmMR6GUtVVx/kyUTlMwIVueBHhRApZK282o1iJ0pr6DVcsDU9Zkn7/emi4zmCGC0kocTRlk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=cMzjG29r; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="cMzjG29r" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1752499036; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=9nsd6AfgXSp7nEfOg4lyDom0iMtgQzhUt3vvT3lAeFQ=; b=cMzjG29rn06U5vF12dMGFALf2UQ/S5/rvsHB7mXSQZXSwcBVvYyH977SijUIMrIn0+hzOl rMiL7ZnjHAGweoNTXXZPl48SwlWNjUBtQvaAUmzp32tLTyymjm6XMk5IiJzIPzvj89AFK9 8qQvQJy3RqFbLWkfcyUyR9Mgdjaaz+g= Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-582-X42RbnBUM9mJmbfIthzxuw-1; Mon, 14 Jul 2025 09:17:12 -0400 X-MC-Unique: X42RbnBUM9mJmbfIthzxuw-1 X-Mimecast-MFC-AGG-ID: X42RbnBUM9mJmbfIthzxuw_1752499030 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 540BA1801210; Mon, 14 Jul 2025 13:17:10 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.22.80.96]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 9041F18002B5; Mon, 14 Jul 2025 13:17:08 +0000 (UTC) From: Luiz Capitulino To: david@redhat.com, willy@infradead.org, akpm@linux-foundation.org Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, shivankg@amd.com, sj@kernel.org, harry.yoo@oracle.com Subject: [PATCH v3 1/4] mm/memory: introduce is_huge_zero_pfn() and use it in vm_normal_page_pmd() Date: Mon, 14 Jul 2025 09:16:51 -0400 Message-ID: <4940826e99f0c709a7cf7beb94f53288320aea5a.1752499009.git.luizcap@redhat.com> In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 Content-Type: text/plain; charset="utf-8" From: David Hildenbrand Let's avoid working with the PMD when not required. If vm_normal_page_pmd() would be called on something that is not a present pmd, it would already be a bug (pfn possibly garbage). While at it, let's support passing in any pfn covered by the huge zero folio by masking off PFN bits -- which should be rather cheap. Signed-off-by: David Hildenbrand Reviewed-by: Oscar Salvador Signed-off-by: Luiz Capitulino Reviewed-by: Shivank Garg Tested-by: Harry Yoo --- include/linux/huge_mm.h | 12 +++++++++++- mm/memory.c | 2 +- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/include/linux/huge_mm.h b/include/linux/huge_mm.h index 2f190c90192d..59e93fba15f4 100644 --- a/include/linux/huge_mm.h +++ b/include/linux/huge_mm.h @@ -486,9 +486,14 @@ static inline bool is_huge_zero_folio(const struct fol= io *folio) return READ_ONCE(huge_zero_folio) =3D=3D folio; } =20 +static inline bool is_huge_zero_pfn(unsigned long pfn) +{ + return READ_ONCE(huge_zero_pfn) =3D=3D (pfn & ~(HPAGE_PMD_NR - 1)); +} + static inline bool is_huge_zero_pmd(pmd_t pmd) { - return pmd_present(pmd) && READ_ONCE(huge_zero_pfn) =3D=3D pmd_pfn(pmd); + return pmd_present(pmd) && is_huge_zero_pfn(pmd_pfn(pmd)); } =20 struct folio *mm_get_huge_zero_folio(struct mm_struct *mm); @@ -636,6 +641,11 @@ static inline bool is_huge_zero_folio(const struct fol= io *folio) return false; } =20 +static inline bool is_huge_zero_pfn(unsigned long pfn) +{ + return false; +} + static inline bool is_huge_zero_pmd(pmd_t pmd) { return false; diff --git a/mm/memory.c b/mm/memory.c index b0cda5aab398..3a765553bacb 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -687,7 +687,7 @@ struct page *vm_normal_page_pmd(struct vm_area_struct *= vma, unsigned long addr, =20 if (pmd_devmap(pmd)) return NULL; - if (is_huge_zero_pmd(pmd)) + if (is_huge_zero_pfn(pfn)) return NULL; if (unlikely(pfn > highest_memmap_pfn)) return NULL; --=20 2.50.0 From nobody Tue Oct 7 05:19:48 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 35BCB24DD0A for ; Mon, 14 Jul 2025 13:17:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752499041; cv=none; b=uKMzjnLLRjZg+fEeNDxDOIeMvUVfK8h9AXd9TFPAQWkcuAKeo7XSZ2+Lq3GKm7eGuyOo5PsfKKkU1oE5Q9dSmKPyJRObpVrlkLY/dqpLsBFH/PtNpbZhkSJ9BjGKVhoH1egdumritmyjaCXZhIkKhejp+aGzVQWV+MkqckDd0Sk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752499041; c=relaxed/simple; bh=OU3p7ZD7Je4BWV16C8j4MBKjwcozASiSqaaRu4MZpx4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=orn3D+u6/tmgn4veYkDRxiJqhnFQZWvxUPpfcCDtwH6hzRx176eUhRFpyXk6J9C64pT/SX1LNA833yNFCWn+E+9x+COFlBZIXo15NwGij271lg1BQnCxMaphuvJvaISP1dsY0KGSltUllFBo2ycHg9NnDQpMg1HK6Nl637H2P20= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=XHH17d5x; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="XHH17d5x" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1752499039; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ynXGS3UbV4776sA/q7fw/bdeOIQTeuFUbD3iRS8icfM=; b=XHH17d5x2FQBG4TSiKRNPHmxkHfMq6o14nrSFoSKlE0wneSkNT47y2k4hVSC0N6Al4G6TK 3pwTOpDu4IFox7Mmvr2ajbOxVqxMDyzy0qYK6u/wkg+Sy4S1I+pGjpxvdXx/qdDxsZm8tU mroFpNMriMxpphrMHm14pp6TCNyWbLY= Received: from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-296-V_U7AeCoMcyllFn2sWRvcQ-1; Mon, 14 Jul 2025 09:17:14 -0400 X-MC-Unique: V_U7AeCoMcyllFn2sWRvcQ-1 X-Mimecast-MFC-AGG-ID: V_U7AeCoMcyllFn2sWRvcQ_1752499032 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 2ECCE18001D6; Mon, 14 Jul 2025 13:17:12 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.22.80.96]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 97035180035E; Mon, 14 Jul 2025 13:17:10 +0000 (UTC) From: Luiz Capitulino To: david@redhat.com, willy@infradead.org, akpm@linux-foundation.org Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, shivankg@amd.com, sj@kernel.org, harry.yoo@oracle.com Subject: [PATCH v3 2/4] mm/util: introduce snapshot_page() Date: Mon, 14 Jul 2025 09:16:52 -0400 Message-ID: <637a03a05cb2e3df88f84ff9e9f9642374ef813a.1752499009.git.luizcap@redhat.com> In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 Content-Type: text/plain; charset="utf-8" This commit refactors __dump_page() into snapshot_page(). snapshot_page() tries to take a faithful snapshot of a page and its folio representation. The snapshot is returned in the struct page_snapshot parameter along with additional flags that are best retrieved at snapshot creation time to reduce race windows. This function is intended to be used by callers that need a stable representation of a struct page and struct folio so that pointers or page information doesn't change while working on a page. The idea and original implementation of snapshot_page() comes from Matthew Wilcox with suggestions for improvements from David Hildenbrand. All bugs and misconceptions are mine. Signed-off-by: Luiz Capitulino Acked-by: David Hildenbrand Reviewed-by: Shivank Garg Tested-by: Harry Yoo --- include/linux/mm.h | 19 +++++++++++ mm/debug.c | 42 +++--------------------- mm/util.c | 79 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 102 insertions(+), 38 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index 0ef2ba0c667a..929e2330af13 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -4184,4 +4184,23 @@ static inline bool page_pool_page_is_pp(struct page = *page) } #endif =20 +#define PAGE_SNAPSHOT_FAITHFUL (1 << 0) +#define PAGE_SNAPSHOT_PG_BUDDY (1 << 1) +#define PAGE_SNAPSHOT_PG_IDLE (1 << 2) + +struct page_snapshot { + struct folio folio_snapshot; + struct page page_snapshot; + unsigned long pfn; + unsigned long idx; + unsigned long flags; +}; + +static inline bool snapshot_page_is_faithful(const struct page_snapshot *p= s) +{ + return ps->flags & PAGE_SNAPSHOT_FAITHFUL; +} + +void snapshot_page(struct page_snapshot *ps, const struct page *page); + #endif /* _LINUX_MM_H */ diff --git a/mm/debug.c b/mm/debug.c index 907382257062..7349330ea506 100644 --- a/mm/debug.c +++ b/mm/debug.c @@ -129,47 +129,13 @@ static void __dump_folio(struct folio *folio, struct = page *page, =20 static void __dump_page(const struct page *page) { - struct folio *foliop, folio; - struct page precise; - unsigned long head; - unsigned long pfn =3D page_to_pfn(page); - unsigned long idx, nr_pages =3D 1; - int loops =3D 5; - -again: - memcpy(&precise, page, sizeof(*page)); - head =3D precise.compound_head; - if ((head & 1) =3D=3D 0) { - foliop =3D (struct folio *)&precise; - idx =3D 0; - if (!folio_test_large(foliop)) - goto dump; - foliop =3D (struct folio *)page; - } else { - foliop =3D (struct folio *)(head - 1); - idx =3D folio_page_idx(foliop, page); - } + struct page_snapshot ps; =20 - if (idx < MAX_FOLIO_NR_PAGES) { - memcpy(&folio, foliop, 2 * sizeof(struct page)); - nr_pages =3D folio_nr_pages(&folio); - if (nr_pages > 1) - memcpy(&folio.__page_2, &foliop->__page_2, - sizeof(struct page)); - foliop =3D &folio; - } - - if (idx > nr_pages) { - if (loops-- > 0) - goto again; + snapshot_page(&ps, page); + if (!snapshot_page_is_faithful(&ps)) pr_warn("page does not match folio\n"); - precise.compound_head &=3D ~1UL; - foliop =3D (struct folio *)&precise; - idx =3D 0; - } =20 -dump: - __dump_folio(foliop, &precise, pfn, idx); + __dump_folio(&ps.folio_snapshot, &ps.page_snapshot, ps.pfn, ps.idx); } =20 void dump_page(const struct page *page, const char *reason) diff --git a/mm/util.c b/mm/util.c index 0b270c43d7d1..f270bf42465b 100644 --- a/mm/util.c +++ b/mm/util.c @@ -25,6 +25,7 @@ #include #include #include +#include =20 #include =20 @@ -1171,3 +1172,81 @@ int compat_vma_mmap_prepare(struct file *file, struc= t vm_area_struct *vma) return 0; } EXPORT_SYMBOL(compat_vma_mmap_prepare); + +static void set_ps_flags(struct page_snapshot *ps, const struct folio *fol= io, + const struct page *page) +{ + /* + * Only the first page of a high-order buddy page has PageBuddy() set. + * So we have to check manually whether this page is part of a high- + * order buddy page. + */ + if (PageBuddy(page)) + ps->flags |=3D PAGE_SNAPSHOT_PG_BUDDY; + else if (page_count(page) =3D=3D 0 && is_free_buddy_page(page)) + ps->flags |=3D PAGE_SNAPSHOT_PG_BUDDY; + + if (folio_test_idle(folio)) + ps->flags |=3D PAGE_SNAPSHOT_PG_IDLE; +} + +/** + * snapshot_page() - Create a snapshot of a struct page + * @ps: Pointer to a struct page_snapshot to store the page snapshot + * @page: The page to snapshot + * + * Create a snapshot of the page and store both its struct page and struct + * folio representations in @ps. + * + * Note that creating a faithful snapshot may fail if the compound + * state of the page keeps changing (e.g., due to a folio split). In + * this case, ps->faithful is set to false, and the snapshot assumes + * that @page refers to a single page. + */ +void snapshot_page(struct page_snapshot *ps, const struct page *page) +{ + unsigned long head, nr_pages =3D 1; + struct folio *foliop; + int loops =3D 5; + + ps->pfn =3D page_to_pfn(page); + ps->flags =3D PAGE_SNAPSHOT_FAITHFUL; + +again: + memset(&ps->folio_snapshot, 0, sizeof(struct folio)); + memcpy(&ps->page_snapshot, page, sizeof(*page)); + head =3D ps->page_snapshot.compound_head; + if ((head & 1) =3D=3D 0) { + ps->idx =3D 0; + foliop =3D (struct folio *)&ps->page_snapshot; + if (!folio_test_large(foliop)) { + set_ps_flags(ps, page_folio(page), page); + memcpy(&ps->folio_snapshot, foliop, + sizeof(struct page)); + return; + } + foliop =3D (struct folio *)page; + } else { + foliop =3D (struct folio *)(head - 1); + ps->idx =3D folio_page_idx(foliop, page); + } + + if (ps->idx < MAX_FOLIO_NR_PAGES) { + memcpy(&ps->folio_snapshot, foliop, 2 * sizeof(struct page)); + nr_pages =3D folio_nr_pages(&ps->folio_snapshot); + if (nr_pages > 1) + memcpy(&ps->folio_snapshot.__page_2, &foliop->__page_2, + sizeof(struct page)); + set_ps_flags(ps, foliop, page); + } + + if (ps->idx > nr_pages) { + if (loops-- > 0) + goto again; + clear_compound_head(&ps->page_snapshot); + foliop =3D (struct folio *)&ps->page_snapshot; + memcpy(&ps->folio_snapshot, foliop, sizeof(struct page)); + ps->flags =3D 0; + ps->idx =3D 0; + } +} --=20 2.50.0 From nobody Tue Oct 7 05:19:48 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2CA2B42065 for ; Mon, 14 Jul 2025 13:17:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752499039; cv=none; b=qUvauvUhq7eQPZeCj7zIXRZsi+ic8gYnHX6Miqe48cb9IyXbW/8XS6LZKhqfWhaqsntBnmoRhj+USgvC7E/vTcXz7gNlnY4USi61478KJ1NTZAGwPUNiC6JBN2O2EOeHlkIkJd4wfg2EK5WcV13VYWaXKNb8KJGaXiL2ZpGtZx4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752499039; c=relaxed/simple; bh=NQlEC12/PkcNog6rAzPE2sHwLIdyRd0D6p4x1t2tCAY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=WUcpNVl/lbPqI/8bxEinAqRU7VmXN6uzy3xAeJKEpzxg96m437lnuRan5Hv7C2V4aT3/4Kh6XXaGJ4etJeqg5kxp23a9eEgDXjHsXXkPGuhRhGmDb8xZZrHPtO/Elr7Xw+PoxgyqYX/CklJOSvYuF5V1XNlSGaAT1u5geVRbQaE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=O3FjRptO; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="O3FjRptO" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1752499037; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=FTJc9bWtRtDIBfHbDRnLppWXtJs9xo9JkdcLfIZxo/I=; b=O3FjRptOftNiUEhhBdrihSHo4nfmfDTZ6xnqsYvTz7j7bIEjYEOqZvNyrBpVqPn7jmOSkU q68PUgrJBvk2JNLTNbAlzMF15NN8PO9+oUbBFjiJTvhlW68Mv49fnJLbkPqdD2G95Bhrm/ hpm+mMO+luTp2M5lL1Qok2QyCyWa6Yg= Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-418-Hz-OqUGIOJylB5NjZspXLQ-1; Mon, 14 Jul 2025 09:17:15 -0400 X-MC-Unique: Hz-OqUGIOJylB5NjZspXLQ-1 X-Mimecast-MFC-AGG-ID: Hz-OqUGIOJylB5NjZspXLQ_1752499034 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id DDF8C1800268; Mon, 14 Jul 2025 13:17:13 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.22.80.96]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 71FCF18002B5; Mon, 14 Jul 2025 13:17:12 +0000 (UTC) From: Luiz Capitulino To: david@redhat.com, willy@infradead.org, akpm@linux-foundation.org Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, shivankg@amd.com, sj@kernel.org, harry.yoo@oracle.com Subject: [PATCH v3 3/4] proc: kpagecount: use snapshot_page() Date: Mon, 14 Jul 2025 09:16:53 -0400 Message-ID: <1c05cc725b90962d56323ff2e28e9cc3ae397b68.1752499009.git.luizcap@redhat.com> In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 Content-Type: text/plain; charset="utf-8" Currently, the call to folio_precise_page_mapcount() from kpage_read() can race with a folio split. When the race happens we trigger a VM_BUG_ON_FOLIO() in folio_entire_mapcount() (see splat below). This commit fixes this race by using snapshot_page() so that we retrieve the folio mapcount using a folio snapshot. [ 2356.558576] page: refcount:1 mapcount:1 mapping:0000000000000000 index:0= xffff85200 pfn:0x6f7c00 [ 2356.558748] memcg:ffff000651775780 [ 2356.558763] anon flags: 0xafffff60020838(uptodate|dirty|lru|owner_2|swap= backed|node=3D1|zone=3D2|lastcpupid=3D0xfffff) [ 2356.558796] raw: 00afffff60020838 fffffdffdb5d0048 fffffdffdadf7fc8 ffff= 00064c1629c1 [ 2356.558817] raw: 0000000ffff85200 0000000000000000 0000000100000000 ffff= 000651775780 [ 2356.558839] page dumped because: VM_BUG_ON_FOLIO(!folio_test_large(folio= )) [ 2356.558882] ------------[ cut here ]------------ [ 2356.558897] kernel BUG at ./include/linux/mm.h:1103! [ 2356.558982] Internal error: Oops - BUG: 00000000f2000800 [#1] SMP [ 2356.564729] CPU: 8 UID: 0 PID: 1864 Comm: folio-split-rac Tainted: G S = W 6.15.0+ #3 PREEMPT(voluntary) [ 2356.566196] Tainted: [S]=3DCPU_OUT_OF_SPEC, [W]=3DWARN [ 2356.566814] Hardware name: Red Hat KVM, BIOS edk2-20241117-3.el9 11/17/2= 024 [ 2356.567684] pstate: 60400005 (nZCv daif +PAN -UAO -TCO -DIT -SSBS BTYPE= =3D--) [ 2356.568563] pc : kpage_read.constprop.0+0x26c/0x290 [ 2356.569605] lr : kpage_read.constprop.0+0x26c/0x290 [ 2356.569992] sp : ffff80008fb739b0 [ 2356.570263] x29: ffff80008fb739b0 x28: ffff00064aa69580 x27: 00000000ff0= 00000 [ 2356.570842] x26: 0000fffffffffff8 x25: ffff00064aa69580 x24: ffff80008fb= 73ae0 [ 2356.571411] x23: 0000000000000001 x22: 0000ffff86c6e8b8 x21: 00000000000= 00008 [ 2356.571978] x20: 00000000006f7c00 x19: 0000ffff86c6e8b8 x18: 00000000000= 00000 [ 2356.572581] x17: 3630303066666666 x16: 0000000000000003 x15: 00000000000= 01000 [ 2356.573217] x14: 00000000ffffffff x13: 0000000000000004 x12: 00aaaaaa00a= aaaaa [ 2356.577674] x11: 0000000000000000 x10: 00aaaaaa00aaaaaa x9 : ffffbf3afca= 6c300 [ 2356.578332] x8 : 0000000000000002 x7 : 0000000000000001 x6 : 00000000000= 00001 [ 2356.578984] x5 : ffff000c79812408 x4 : 0000000000000000 x3 : 00000000000= 00000 [ 2356.579635] x2 : 0000000000000000 x1 : ffff00064aa69580 x0 : 00000000000= 0003e [ 2356.580286] Call trace: [ 2356.580524] kpage_read.constprop.0+0x26c/0x290 (P) [ 2356.580982] kpagecount_read+0x28/0x40 [ 2356.581336] proc_reg_read+0x38/0x100 [ 2356.581681] vfs_read+0xcc/0x320 [ 2356.581992] ksys_read+0x74/0x118 [ 2356.582306] __arm64_sys_read+0x24/0x38 [ 2356.582668] invoke_syscall+0x70/0x100 [ 2356.583022] el0_svc_common.constprop.0+0x48/0xf8 [ 2356.583456] do_el0_svc+0x28/0x40 [ 2356.583930] el0_svc+0x38/0x118 [ 2356.584328] el0t_64_sync_handler+0x144/0x168 [ 2356.584883] el0t_64_sync+0x19c/0x1a0 [ 2356.585350] Code: aa0103e0 9003a541 91082021 97f813fc (d4210000) [ 2356.586130] ---[ end trace 0000000000000000 ]--- [ 2356.587377] note: folio-split-rac[1864] exited with irqs disabled [ 2356.588050] note: folio-split-rac[1864] exited with preempt_count 1 Reported-by: syzbot+3d7dc5eaba6b932f8535@syzkaller.appspotmail.com Closes: https://lore.kernel.org/all/67812fbd.050a0220.d0267.0030.GAE@google= .com/ Signed-off-by: Luiz Capitulino Acked-by: David Hildenbrand Reviewed-by: Shivank Garg Tested-by: Harry Yoo --- fs/proc/page.c | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/fs/proc/page.c b/fs/proc/page.c index 999af26c7298..936f8bbe5a6f 100644 --- a/fs/proc/page.c +++ b/fs/proc/page.c @@ -43,6 +43,22 @@ static inline unsigned long get_max_dump_pfn(void) #endif } =20 +static u64 get_kpage_count(const struct page *page) +{ + struct page_snapshot ps; + u64 ret; + + snapshot_page(&ps, page); + + if (IS_ENABLED(CONFIG_PAGE_MAPCOUNT)) + ret =3D folio_precise_page_mapcount(&ps.folio_snapshot, + &ps.page_snapshot); + else + ret =3D folio_average_page_mapcount(&ps.folio_snapshot); + + return ret; +} + static ssize_t kpage_read(struct file *file, char __user *buf, size_t count, loff_t *ppos, enum kpage_operation op) @@ -75,10 +91,7 @@ static ssize_t kpage_read(struct file *file, char __user= *buf, info =3D stable_page_flags(page); break; case KPAGE_COUNT: - if (IS_ENABLED(CONFIG_PAGE_MAPCOUNT)) - info =3D folio_precise_page_mapcount(page_folio(page), page); - else - info =3D folio_average_page_mapcount(page_folio(page)); + info =3D get_kpage_count(page); break; case KPAGE_CGROUP: info =3D page_cgroup_ino(page); --=20 2.50.0 From nobody Tue Oct 7 05:19:48 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2C62325393C for ; Mon, 14 Jul 2025 13:17:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752499044; cv=none; b=QuuOxjY9ZtotuUSQAWvOYSjM0Mrp/oU73d3CTS4+R3AFisMe7it3wPOdlYWOF1sMIx35XQtjigtEy3bqSTcD8Oa6bub+YJ0h55xEhC+vKpr72GQY5Kv0dUEb7HMBm1dTfx/13JECk4gCZ4LrKDBFADlC43TDevsiK5CckCc+v94= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752499044; c=relaxed/simple; bh=HI+ls1uhcjl2xzKioEZcsu9yaVQyGvMvx+q4n/3XbI8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=c6s9I38tChO/aV0BKpsR5PpKKDjyquKLWFADYFfeiLJhk+QCLGJhgu2zL7rU+DxAXDMxvrB51h8rxsgWVZHKBvxWSJBZUUr+VajeU5PGTIZXc4mBLGIhqIZkSu2DYsdVNIS7pcDrOcXWH2cSAdCTPc0L1kZkQg37ObOgVedQcOs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=FCPw2gXe; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="FCPw2gXe" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1752499042; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=EPBx35LRg/lHdTXAHcUxuRahtl/Vjvqe5PsZWwJTjtI=; b=FCPw2gXe75uA9Njqdkh8vcgWUNjRMBM1J1JOsV3OaDHarDrGblcfDUnqg6ogxgMNVVx/FT YLApLemTRVYsTALon0jjLcR9ABLu6DDZAJYwM3LXjkS81Eq03UrTm6aKAy3t/yPtv+IBVC BoxQdQ+mOcodjqboLzgcI9pq/ra+qAs= Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-620-wxL77xLTOK6D7_5OwZo9DA-1; Mon, 14 Jul 2025 09:17:17 -0400 X-MC-Unique: wxL77xLTOK6D7_5OwZo9DA-1 X-Mimecast-MFC-AGG-ID: wxL77xLTOK6D7_5OwZo9DA_1752499036 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id D0595180028A; Mon, 14 Jul 2025 13:17:15 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.22.80.96]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 2D21F1803AF2; Mon, 14 Jul 2025 13:17:14 +0000 (UTC) From: Luiz Capitulino To: david@redhat.com, willy@infradead.org, akpm@linux-foundation.org Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, shivankg@amd.com, sj@kernel.org, harry.yoo@oracle.com Subject: [PATCH v3 4/4] fs: stable_page_flags(): use snapshot_page() Date: Mon, 14 Jul 2025 09:16:54 -0400 Message-ID: <52c16c0f00995a812a55980c2f26848a999a34ab.1752499009.git.luizcap@redhat.com> In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 Content-Type: text/plain; charset="utf-8" A race condition is possible in stable_page_flags() where user-space is reading /proc/kpageflags concurrently to a folio split. This may lead to oopses or BUG_ON()s being triggered. To fix this, this commit uses snapshot_page() in stable_page_flags() so that stable_page_flags() works with a stable page and folio snapshots instead. Note that stable_page_flags() makes use of some functions that require the original page or folio pointer to work properly (eg. is_free_budy_page() and folio_test_idle()). Since those functions can't be used on the page snapshot, we replace their usage with flags that were set by snapshot_page() for this purpose. Signed-off-by: Luiz Capitulino Acked-by: David Hildenbrand Reviewed-by: Shivank Garg Tested-by: Harry Yoo --- fs/proc/page.c | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/fs/proc/page.c b/fs/proc/page.c index 936f8bbe5a6f..52e4f49c0638 100644 --- a/fs/proc/page.c +++ b/fs/proc/page.c @@ -147,6 +147,7 @@ static inline u64 kpf_copy_bit(u64 kflags, int ubit, in= t kbit) u64 stable_page_flags(const struct page *page) { const struct folio *folio; + struct page_snapshot ps; unsigned long k; unsigned long mapping; bool is_anon; @@ -158,7 +159,9 @@ u64 stable_page_flags(const struct page *page) */ if (!page) return 1 << KPF_NOPAGE; - folio =3D page_folio(page); + + snapshot_page(&ps, page); + folio =3D &ps.folio_snapshot; =20 k =3D folio->flags; mapping =3D (unsigned long)folio->mapping; @@ -167,7 +170,7 @@ u64 stable_page_flags(const struct page *page) /* * pseudo flags for the well known (anonymous) memory mapped pages */ - if (page_mapped(page)) + if (folio_mapped(folio)) u |=3D 1 << KPF_MMAP; if (is_anon) { u |=3D 1 << KPF_ANON; @@ -179,7 +182,7 @@ u64 stable_page_flags(const struct page *page) * compound pages: export both head/tail info * they together define a compound page's start/end pos and order */ - if (page =3D=3D &folio->page) + if (ps.idx =3D=3D 0) u |=3D kpf_copy_bit(k, KPF_COMPOUND_HEAD, PG_head); else u |=3D 1 << KPF_COMPOUND_TAIL; @@ -189,25 +192,19 @@ u64 stable_page_flags(const struct page *page) folio_test_large_rmappable(folio)) { /* Note: we indicate any THPs here, not just PMD-sized ones */ u |=3D 1 << KPF_THP; - } else if (is_huge_zero_folio(folio)) { + } else if (is_huge_zero_pfn(ps.pfn)) { u |=3D 1 << KPF_ZERO_PAGE; u |=3D 1 << KPF_THP; - } else if (is_zero_folio(folio)) { + } else if (is_zero_pfn(ps.pfn)) { u |=3D 1 << KPF_ZERO_PAGE; } =20 - /* - * Caveats on high order pages: PG_buddy and PG_slab will only be set - * on the head page. - */ - if (PageBuddy(page)) - u |=3D 1 << KPF_BUDDY; - else if (page_count(page) =3D=3D 0 && is_free_buddy_page(page)) + if (ps.flags & PAGE_SNAPSHOT_PG_BUDDY) u |=3D 1 << KPF_BUDDY; =20 - if (PageOffline(page)) + if (folio_test_offline(folio)) u |=3D 1 << KPF_OFFLINE; - if (PageTable(page)) + if (folio_test_pgtable(folio)) u |=3D 1 << KPF_PGTABLE; if (folio_test_slab(folio)) u |=3D 1 << KPF_SLAB; @@ -215,7 +212,7 @@ u64 stable_page_flags(const struct page *page) #if defined(CONFIG_PAGE_IDLE_FLAG) && defined(CONFIG_64BIT) u |=3D kpf_copy_bit(k, KPF_IDLE, PG_idle); #else - if (folio_test_idle(folio)) + if (ps.flags & PAGE_SNAPSHOT_PG_IDLE) u |=3D 1 << KPF_IDLE; #endif =20 @@ -241,7 +238,7 @@ u64 stable_page_flags(const struct page *page) if (u & (1 << KPF_HUGE)) u |=3D kpf_copy_bit(k, KPF_HWPOISON, PG_hwpoison); else - u |=3D kpf_copy_bit(page->flags, KPF_HWPOISON, PG_hwpoison); + u |=3D kpf_copy_bit(ps.page_snapshot.flags, KPF_HWPOISON, PG_hwpoison); #endif =20 u |=3D kpf_copy_bit(k, KPF_RESERVED, PG_reserved); --=20 2.50.0