From nobody Fri Jun 19 19:08:07 2026 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 44F55C4332F for ; Wed, 30 Mar 2022 10:06:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245195AbiC3KIa (ORCPT ); Wed, 30 Mar 2022 06:08:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45780 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244732AbiC3KI1 (ORCPT ); Wed, 30 Mar 2022 06:08:27 -0400 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7A56413F8CC for ; Wed, 30 Mar 2022 03:06:42 -0700 (PDT) Received: from dggpemm500020.china.huawei.com (unknown [172.30.72.57]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4KT28q5fk6zDq9W; Wed, 30 Mar 2022 18:04:27 +0800 (CST) Received: from dggpemm500002.china.huawei.com (7.185.36.229) by dggpemm500020.china.huawei.com (7.185.36.49) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2308.21; Wed, 30 Mar 2022 18:06:40 +0800 Received: from localhost.localdomain (10.175.112.125) by dggpemm500002.china.huawei.com (7.185.36.229) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2308.21; Wed, 30 Mar 2022 18:06:40 +0800 From: Chen Wandun To: , , , Subject: [PATCH v2 1/2] mm: fix contiguous memmap assumptions about split page Date: Wed, 30 Mar 2022 18:25:33 +0800 Message-ID: <20220330102534.1053240-2-chenwandun@huawei.com> X-Mailer: git-send-email 2.18.0.huawei.25 In-Reply-To: <20220330102534.1053240-1-chenwandun@huawei.com> References: <20220330102534.1053240-1-chenwandun@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.175.112.125] X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To dggpemm500002.china.huawei.com (7.185.36.229) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" It isn't true for only SPARSEMEM configs to assume that a compound page has virtually contiguous page structs, so use nth_page to iterate each page. Inspired by: https://lore.kernel.org/linux-mm/20220204195852.1751729-8-willy@infradead.o= rg/ Signed-off-by: Chen Wandun --- mm/compaction.c | 6 +++--- mm/huge_memory.c | 2 +- mm/page_alloc.c | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/mm/compaction.c b/mm/compaction.c index c3e37aa9ff9e..ddff13b968a2 100644 --- a/mm/compaction.c +++ b/mm/compaction.c @@ -87,7 +87,7 @@ static unsigned long release_freepages(struct list_head *= freelist) static void split_map_pages(struct list_head *list) { unsigned int i, order, nr_pages; - struct page *page, *next; + struct page *page, *next, *tmp; LIST_HEAD(tmp_list); =20 list_for_each_entry_safe(page, next, list, lru) { @@ -101,8 +101,8 @@ static void split_map_pages(struct list_head *list) split_page(page, order); =20 for (i =3D 0; i < nr_pages; i++) { - list_add(&page->lru, &tmp_list); - page++; + tmp =3D nth_page(page, i); + list_add(&tmp->lru, &tmp_list); } } =20 diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 2fe38212e07c..d77fc2ad581d 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -2297,7 +2297,7 @@ static void lru_add_page_tail(struct page *head, stru= ct page *tail, static void __split_huge_page_tail(struct page *head, int tail, struct lruvec *lruvec, struct list_head *list) { - struct page *page_tail =3D head + tail; + struct page *page_tail =3D nth_page(head, tail); =20 VM_BUG_ON_PAGE(atomic_read(&page_tail->_mapcount) !=3D -1, page_tail); =20 diff --git a/mm/page_alloc.c b/mm/page_alloc.c index f648decfe39d..855211dea13e 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -3513,7 +3513,7 @@ void split_page(struct page *page, unsigned int order) VM_BUG_ON_PAGE(!page_count(page), page); =20 for (i =3D 1; i < (1 << order); i++) - set_page_refcounted(page + i); + set_page_refcounted(nth_page(page, i)); split_page_owner(page, 1 << order); split_page_memcg(page, 1 << order); } --=20 2.18.0.huawei.25 From nobody Fri Jun 19 19:08:07 2026 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 9202FC43217 for ; Wed, 30 Mar 2022 10:06:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245203AbiC3KIb (ORCPT ); Wed, 30 Mar 2022 06:08:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45898 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242007AbiC3KI2 (ORCPT ); Wed, 30 Mar 2022 06:08:28 -0400 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1D00013F8CC for ; Wed, 30 Mar 2022 03:06:44 -0700 (PDT) Received: from dggpemm500024.china.huawei.com (unknown [172.30.72.55]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4KT29W0RhCzgY96; Wed, 30 Mar 2022 18:05:03 +0800 (CST) Received: from dggpemm500002.china.huawei.com (7.185.36.229) by dggpemm500024.china.huawei.com (7.185.36.203) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2308.21; Wed, 30 Mar 2022 18:06:42 +0800 Received: from localhost.localdomain (10.175.112.125) by dggpemm500002.china.huawei.com (7.185.36.229) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2308.21; Wed, 30 Mar 2022 18:06:41 +0800 From: Chen Wandun To: , , , Subject: [PATCH v2 2/2] mm: fix contiguous memmap assumptions about alloc/free pages Date: Wed, 30 Mar 2022 18:25:34 +0800 Message-ID: <20220330102534.1053240-3-chenwandun@huawei.com> X-Mailer: git-send-email 2.18.0.huawei.25 In-Reply-To: <20220330102534.1053240-1-chenwandun@huawei.com> References: <20220330102534.1053240-1-chenwandun@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.175.112.125] X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To dggpemm500002.china.huawei.com (7.185.36.229) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" It isn't true for only SPARSEMEM configs to assume that a compound page has virtually contiguous page structs, so use nth_page to iterate each page. Signed-off-by: Chen Wandun --- mm/page_alloc.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/mm/page_alloc.c b/mm/page_alloc.c index 855211dea13e..758d8f069b32 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -721,7 +721,7 @@ static void prep_compound_head(struct page *page, unsig= ned int order) =20 static void prep_compound_tail(struct page *head, int tail_idx) { - struct page *p =3D head + tail_idx; + struct page *p =3D nth_page(head, tail_idx); =20 p->mapping =3D TAIL_MAPPING; set_compound_head(p, head); @@ -1199,10 +1199,10 @@ static inline int check_free_page(struct page *page) return 1; } =20 -static int free_tail_pages_check(struct page *head_page, struct page *page) +static int free_tail_pages_check(struct page *head_page, int index) { + struct page *page =3D nth_page(head_page, index); int ret =3D 1; - /* * We rely page->lru.next never has bit 0 set, unless the page * is PageTail(). Let's make sure that's true even for poisoned ->lru. @@ -1213,7 +1213,7 @@ static int free_tail_pages_check(struct page *head_pa= ge, struct page *page) ret =3D 0; goto out; } - switch (page - head_page) { + switch (index) { case 1: /* the first tail page: ->mapping may be compound_mapcount() */ if (unlikely(compound_mapcount(page))) { @@ -1322,6 +1322,7 @@ static __always_inline bool free_pages_prepare(struct= page *page, if (unlikely(order)) { bool compound =3D PageCompound(page); int i; + struct page *tail_page; =20 VM_BUG_ON_PAGE(compound && compound_order(page) !=3D order, page); =20 @@ -1330,13 +1331,14 @@ static __always_inline bool free_pages_prepare(stru= ct page *page, ClearPageHasHWPoisoned(page); } for (i =3D 1; i < (1 << order); i++) { + tail_page =3D nth_page(page, i); if (compound) - bad +=3D free_tail_pages_check(page, page + i); - if (unlikely(check_free_page(page + i))) { + bad +=3D free_tail_pages_check(page, i); + if (unlikely(check_free_page(tail_page))) { bad++; continue; } - (page + i)->flags &=3D ~PAGE_FLAGS_CHECK_AT_PREP; + tail_page->flags &=3D ~PAGE_FLAGS_CHECK_AT_PREP; } } if (PageMappingFlags(page)) --=20 2.18.0.huawei.25