From nobody Sat Feb 7 18:45:59 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 86911EDEC49 for ; Wed, 13 Sep 2023 12:52:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240643AbjIMMwO (ORCPT ); Wed, 13 Sep 2023 08:52:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48092 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238125AbjIMMwN (ORCPT ); Wed, 13 Sep 2023 08:52:13 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id C783219B6 for ; Wed, 13 Sep 2023 05:51:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1694609482; 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=fcwyuEyPhUjibl+UPXTZ+YZfX1YncqC+LcRBkk4rhVQ=; b=TcwgLUjcSt8KyaNcTRWSDsebjVmcB6IeEn6C4e003MHugNCvu3vXw0ws9oc7VOC1GX3vBX o77ublDLqWL9ARchHN3xVKGkmgiQO2kIXkbKaRnvIPvzWf4wrB/qClW/X8GXKQhcwyRzJq k6Zkr9k2rXm+yT7JH2Z7hSE/AUiJ+Zc= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-664-skkLD7dfNDCSC0zIh01gyA-1; Wed, 13 Sep 2023 08:51:19 -0400 X-MC-Unique: skkLD7dfNDCSC0zIh01gyA-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 3CA79857A9A; Wed, 13 Sep 2023 12:51:19 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.193.225]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8FF2D2156721; Wed, 13 Sep 2023 12:51:17 +0000 (UTC) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, David Hildenbrand , Andrew Morton , Mike Kravetz , Muchun Song Subject: [PATCH v1 1/6] mm/rmap: drop stale comment in page_add_anon_rmap and hugepage_add_anon_rmap() Date: Wed, 13 Sep 2023 14:51:08 +0200 Message-ID: <20230913125113.313322-2-david@redhat.com> In-Reply-To: <20230913125113.313322-1-david@redhat.com> References: <20230913125113.313322-1-david@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.6 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" That comment was added in commit 5dbe0af47f8a ("mm: fix kernel BUG at mm/rmap.c:1017!") to document why we can see vma->vm_end getting adjusted concurrently due to a VMA split. However, the optimized locking code was changed again in bf181b9f9d8 ("mm anon rmap: replace same_anon_vma linked list with an interval tree."). ... and later, the comment was changed in commit 0503ea8f5ba7 ("mm/mmap: remove __vma_adjust()") to talk about "vma_merge" although the original issue was with VMA splitting. Let's just remove that comment. Nowadays, it's outdated, imprecise and confusing. Signed-off-by: David Hildenbrand --- mm/rmap.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/mm/rmap.c b/mm/rmap.c index ec7f8e6c9e48..ca2787c1fe05 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -1245,7 +1245,6 @@ void page_add_anon_rmap(struct page *page, struct vm_= area_struct *vma, __lruvec_stat_mod_folio(folio, NR_ANON_MAPPED, nr); =20 if (likely(!folio_test_ksm(folio))) { - /* address might be in next vma when migration races vma_merge */ if (first) __page_set_anon_rmap(folio, page, vma, address, !!(flags & RMAP_EXCLUSIVE)); @@ -2536,7 +2535,6 @@ void hugepage_add_anon_rmap(struct page *page, struct= vm_area_struct *vma, =20 BUG_ON(!folio_test_locked(folio)); BUG_ON(!anon_vma); - /* address might be in next vma when migration races vma_merge */ first =3D atomic_inc_and_test(&folio->_entire_mapcount); VM_BUG_ON_PAGE(!first && (flags & RMAP_EXCLUSIVE), page); VM_BUG_ON_PAGE(!first && PageAnonExclusive(page), page); --=20 2.41.0 From nobody Sat Feb 7 18:45:59 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 9F89AEDEC49 for ; Wed, 13 Sep 2023 12:52:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240689AbjIMMwT (ORCPT ); Wed, 13 Sep 2023 08:52:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48108 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240665AbjIMMwR (ORCPT ); Wed, 13 Sep 2023 08:52:17 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id CC0D519B9 for ; Wed, 13 Sep 2023 05:51:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1694609485; 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=Fi954WhfJTlnxfu/yTjvI+IMpqyRN+5DQHmUbjacTd0=; b=N9QboO8pqvkY2+aoMzgt2I2CD2heVX7NfV38zGIuWDZcC8B8jxNciyuHG1ETRXEA1l8tlN gP+wVhD7YMoTCwK//+fpeQ4bqJGv5oO8fAregzrbLFjKpLrP0MEWFrdwI26qBhlJd87L4C fg4kREN+oQXMSME2MVcHMZlKLP2vlvk= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-60-IQGL421PNvK9ZlO5bNgMvQ-1; Wed, 13 Sep 2023 08:51:21 -0400 X-MC-Unique: IQGL421PNvK9ZlO5bNgMvQ-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 4CBBA101A550; Wed, 13 Sep 2023 12:51:21 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.193.225]) by smtp.corp.redhat.com (Postfix) with ESMTP id A48BE2156722; Wed, 13 Sep 2023 12:51:19 +0000 (UTC) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, David Hildenbrand , Andrew Morton , Mike Kravetz , Muchun Song Subject: [PATCH v1 2/6] mm/rmap: move SetPageAnonExclusive out of __page_set_anon_rmap() Date: Wed, 13 Sep 2023 14:51:09 +0200 Message-ID: <20230913125113.313322-3-david@redhat.com> In-Reply-To: <20230913125113.313322-1-david@redhat.com> References: <20230913125113.313322-1-david@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.6 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Let's handle it in the caller. No need to pass the page. While at it, rename the function to __folio_set_anon() and pass "bool exclusive" instead of "int exclusive". Signed-off-by: David Hildenbrand --- mm/rmap.c | 41 +++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/mm/rmap.c b/mm/rmap.c index ca2787c1fe05..ab16baa0fcb3 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -1122,27 +1122,25 @@ void page_move_anon_rmap(struct page *page, struct = vm_area_struct *vma) } =20 /** - * __page_set_anon_rmap - set up new anonymous rmap - * @folio: Folio which contains page. - * @page: Page to add to rmap. - * @vma: VM area to add page to. + * __folio_set_anon - set up a new anonymous rmap for a folio + * @folio: The folio to set up the new anonymous rmap for. + * @vma: VM area to add the folio to. * @address: User virtual address of the mapping - * @exclusive: the page is exclusively owned by the current process + * @exclusive: Whether the folio is exclusive to the process. */ -static void __page_set_anon_rmap(struct folio *folio, struct page *page, - struct vm_area_struct *vma, unsigned long address, int exclusive) +static void __folio_set_anon(struct folio *folio, struct vm_area_struct *v= ma, + unsigned long address, bool exclusive) { struct anon_vma *anon_vma =3D vma->anon_vma; =20 BUG_ON(!anon_vma); =20 if (folio_test_anon(folio)) - goto out; + return; =20 /* - * If the page isn't exclusively mapped into this vma, - * we must use the _oldest_ possible anon_vma for the - * page mapping! + * If the folio isn't exclusive to this vma, we must use the _oldest_ + * possible anon_vma for the folio mapping! */ if (!exclusive) anon_vma =3D anon_vma->root; @@ -1156,9 +1154,6 @@ static void __page_set_anon_rmap(struct folio *folio,= struct page *page, anon_vma =3D (void *) anon_vma + PAGE_MAPPING_ANON; WRITE_ONCE(folio->mapping, (struct address_space *) anon_vma); folio->index =3D linear_page_index(vma, address); -out: - if (exclusive) - SetPageAnonExclusive(page); } =20 /** @@ -1246,11 +1241,13 @@ void page_add_anon_rmap(struct page *page, struct v= m_area_struct *vma, =20 if (likely(!folio_test_ksm(folio))) { if (first) - __page_set_anon_rmap(folio, page, vma, address, - !!(flags & RMAP_EXCLUSIVE)); + __folio_set_anon(folio, vma, address, + !!(flags & RMAP_EXCLUSIVE)); else __page_check_anon_rmap(folio, page, vma, address); } + if (flags & RMAP_EXCLUSIVE) + SetPageAnonExclusive(page); =20 mlock_vma_folio(folio, vma, compound); } @@ -1289,7 +1286,8 @@ void folio_add_new_anon_rmap(struct folio *folio, str= uct vm_area_struct *vma, } =20 __lruvec_stat_mod_folio(folio, NR_ANON_MAPPED, nr); - __page_set_anon_rmap(folio, &folio->page, vma, address, 1); + __folio_set_anon(folio, vma, address, true); + SetPageAnonExclusive(&folio->page); } =20 /** @@ -2539,8 +2537,10 @@ void hugepage_add_anon_rmap(struct page *page, struc= t vm_area_struct *vma, VM_BUG_ON_PAGE(!first && (flags & RMAP_EXCLUSIVE), page); VM_BUG_ON_PAGE(!first && PageAnonExclusive(page), page); if (first) - __page_set_anon_rmap(folio, page, vma, address, - !!(flags & RMAP_EXCLUSIVE)); + __folio_set_anon(folio, vma, address, + !!(flags & RMAP_EXCLUSIVE)); + if (flags & RMAP_EXCLUSIVE) + SetPageAnonExclusive(page); } =20 void hugepage_add_new_anon_rmap(struct folio *folio, @@ -2550,6 +2550,7 @@ void hugepage_add_new_anon_rmap(struct folio *folio, /* increment count (starts at -1) */ atomic_set(&folio->_entire_mapcount, 0); folio_clear_hugetlb_restore_reserve(folio); - __page_set_anon_rmap(folio, &folio->page, vma, address, 1); + __folio_set_anon(folio, vma, address, true); + SetPageAnonExclusive(&folio->page); } #endif /* CONFIG_HUGETLB_PAGE */ --=20 2.41.0 From nobody Sat Feb 7 18:45:59 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 0B611EDEC4D for ; Wed, 13 Sep 2023 12:52:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240738AbjIMMw0 (ORCPT ); Wed, 13 Sep 2023 08:52:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58000 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240668AbjIMMwT (ORCPT ); Wed, 13 Sep 2023 08:52:19 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 5861919B3 for ; Wed, 13 Sep 2023 05:51:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1694609487; 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=TPywS+lMvCdRAxxYldVBzyiy9uXPhmFWj9jbUK5aT08=; b=bFBycnAQdVCR9gfq9lxyHgaN+RZQMxyvjX0f2ec/zjFvKMyrK8SmnqbNmTu39mKt1bGDlj ufG6IG7KB1slESFqH0Xwbs3bj6NTbz2DsVJrisC6KzZ03K41pEtaoqlAUrRbhecEJmAHHz A8/NoXugx219alLFAuxK1vpnpOIQwbQ= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-41-FbYiyCTvMFu_-aiiWy3VKA-1; Wed, 13 Sep 2023 08:51:24 -0400 X-MC-Unique: FbYiyCTvMFu_-aiiWy3VKA-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id BD850800045; Wed, 13 Sep 2023 12:51:23 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.193.225]) by smtp.corp.redhat.com (Postfix) with ESMTP id A749F21B2414; Wed, 13 Sep 2023 12:51:21 +0000 (UTC) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, David Hildenbrand , Andrew Morton , Mike Kravetz , Muchun Song Subject: [PATCH v1 3/6] mm/rmap: move folio_test_anon() check out of __folio_set_anon() Date: Wed, 13 Sep 2023 14:51:10 +0200 Message-ID: <20230913125113.313322-4-david@redhat.com> In-Reply-To: <20230913125113.313322-1-david@redhat.com> References: <20230913125113.313322-1-david@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.6 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Let's handle it in the caller; no need for the "first" check based on the mapcount. We really only end up with !anon pages in page_add_anon_rmap() via do_swap_page(), where we hold the folio lock. So races are not possible. Add a VM_WARN_ON_FOLIO() to make sure that we really hold the folio lock. In the future, we might want to let do_swap_page() use folio_add_new_anon_rmap() on new pages instead: however, we might have to pass then whether the folio is exclusive or not. So keep it in there for now. For hugetlb we never expect to have a non-anon page in hugepage_add_anon_rmap(). Remove that code, along with some other checks that are either not required or were checked in hugepage_add_new_anon_rmap() already. Signed-off-by: David Hildenbrand --- mm/rmap.c | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/mm/rmap.c b/mm/rmap.c index ab16baa0fcb3..1ac5bd1b8169 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -1135,9 +1135,6 @@ static void __folio_set_anon(struct folio *folio, str= uct vm_area_struct *vma, =20 BUG_ON(!anon_vma); =20 - if (folio_test_anon(folio)) - return; - /* * If the folio isn't exclusive to this vma, we must use the _oldest_ * possible anon_vma for the folio mapping! @@ -1239,12 +1236,12 @@ void page_add_anon_rmap(struct page *page, struct v= m_area_struct *vma, if (nr) __lruvec_stat_mod_folio(folio, NR_ANON_MAPPED, nr); =20 - if (likely(!folio_test_ksm(folio))) { - if (first) - __folio_set_anon(folio, vma, address, - !!(flags & RMAP_EXCLUSIVE)); - else - __page_check_anon_rmap(folio, page, vma, address); + if (unlikely(!folio_test_anon(folio))) { + VM_WARN_ON_FOLIO(!folio_test_locked(folio), folio); + __folio_set_anon(folio, vma, address, + !!(flags & RMAP_EXCLUSIVE)); + } else if (likely(!folio_test_ksm(folio))) { + __page_check_anon_rmap(folio, page, vma, address); } if (flags & RMAP_EXCLUSIVE) SetPageAnonExclusive(page); @@ -2528,17 +2525,13 @@ void hugepage_add_anon_rmap(struct page *page, stru= ct vm_area_struct *vma, unsigned long address, rmap_t flags) { struct folio *folio =3D page_folio(page); - struct anon_vma *anon_vma =3D vma->anon_vma; int first; =20 - BUG_ON(!folio_test_locked(folio)); - BUG_ON(!anon_vma); + VM_WARN_ON_FOLIO(!folio_test_anon(folio), folio); + first =3D atomic_inc_and_test(&folio->_entire_mapcount); VM_BUG_ON_PAGE(!first && (flags & RMAP_EXCLUSIVE), page); VM_BUG_ON_PAGE(!first && PageAnonExclusive(page), page); - if (first) - __folio_set_anon(folio, vma, address, - !!(flags & RMAP_EXCLUSIVE)); if (flags & RMAP_EXCLUSIVE) SetPageAnonExclusive(page); } --=20 2.41.0 From nobody Sat Feb 7 18:45:59 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 8035EEDEC4F for ; Wed, 13 Sep 2023 12:52:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240795AbjIMMwf (ORCPT ); Wed, 13 Sep 2023 08:52:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48122 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240691AbjIMMwX (ORCPT ); Wed, 13 Sep 2023 08:52:23 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 3694119BD for ; Wed, 13 Sep 2023 05:51:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1694609489; 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=iURFDA2RdJWU1H9IedgFZuHwYBnEAwvtokGRQic4P+w=; b=bK8NUHwZbHB1MYsGiOEMfonkJW9PoUyEEQtuBqPNIHv7LxjKQxof7JtYfeKB0dxS65AGZ/ fkT3oai9/QHPWrhCoIN26aBquRvCOLwjUTbOwjczo8C4OS3jWuXeFL9z9glT/1XAfQG/eJ alNKnBpMfq0aH/bNjveBRBw/peTCP/s= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-480-uy6DAjTwN3CYPzjjmN_0TQ-1; Wed, 13 Sep 2023 08:51:26 -0400 X-MC-Unique: uy6DAjTwN3CYPzjjmN_0TQ-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id CB2A58030CB; Wed, 13 Sep 2023 12:51:25 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.193.225]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2A94721B20B1; Wed, 13 Sep 2023 12:51:23 +0000 (UTC) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, David Hildenbrand , Andrew Morton , Mike Kravetz , Muchun Song Subject: [PATCH v1 4/6] mm/rmap: warn on new PTE-mapped folios in page_add_anon_rmap() Date: Wed, 13 Sep 2023 14:51:11 +0200 Message-ID: <20230913125113.313322-5-david@redhat.com> In-Reply-To: <20230913125113.313322-1-david@redhat.com> References: <20230913125113.313322-1-david@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.6 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" If swapin code would ever decide to not use order-0 pages and supply a PTE-mapped large folio, we will have to change how we call __folio_set_anon() -- eventually with exclusive=3Dfalse and an adjusted address. For now, let's add a VM_WARN_ON_FOLIO() with a comment about the situation. Signed-off-by: David Hildenbrand --- mm/rmap.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/mm/rmap.c b/mm/rmap.c index 1ac5bd1b8169..489c142d073b 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -1238,6 +1238,13 @@ void page_add_anon_rmap(struct page *page, struct vm= _area_struct *vma, =20 if (unlikely(!folio_test_anon(folio))) { VM_WARN_ON_FOLIO(!folio_test_locked(folio), folio); + /* + * For a PTE-mapped large folio, we only know that the single + * PTE is exclusive. Further, __folio_set_anon() might not get + * folio->index right when not given the address of the head + * page. + */ + VM_WARN_ON_FOLIO(folio_test_large(folio) && !compound, folio); __folio_set_anon(folio, vma, address, !!(flags & RMAP_EXCLUSIVE)); } else if (likely(!folio_test_ksm(folio))) { --=20 2.41.0 From nobody Sat Feb 7 18:45:59 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 BC864EDEC4F for ; Wed, 13 Sep 2023 12:52:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240712AbjIMMwb (ORCPT ); Wed, 13 Sep 2023 08:52:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57992 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240648AbjIMMwW (ORCPT ); Wed, 13 Sep 2023 08:52:22 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id F2DB41BC8 for ; Wed, 13 Sep 2023 05:51:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1694609489; 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=Z+qQSwZU5O6LoNzg+Y0KcgzyFROGp1piGCQLbieshoU=; b=TDI9gnm7p5CsZag3dSNEos0XRJbYI9BFE07jjbQZU4fl2m26IOFN8D/sslG+yZzbSOh2r2 WSHILd1vDnfCO7fpNzNnjAPiQ3kJuIhE/Iidwyz/qmTGVcvoA4YrhbTIJPTvfE0b/CgQTm XGcewk4cmosbvnJzQt2XwD7hRxr8K3I= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-18-4zJ54y7iMkaiv39wEbMctA-1; Wed, 13 Sep 2023 08:51:27 -0400 X-MC-Unique: 4zJ54y7iMkaiv39wEbMctA-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 6E9678011AD; Wed, 13 Sep 2023 12:51:27 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.193.225]) by smtp.corp.redhat.com (Postfix) with ESMTP id 1F56421B20B1; Wed, 13 Sep 2023 12:51:26 +0000 (UTC) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, David Hildenbrand , Andrew Morton , Mike Kravetz , Muchun Song Subject: [PATCH v1 5/6] mm/rmap: simplify PageAnonExclusive sanity checks when adding anon rmap Date: Wed, 13 Sep 2023 14:51:12 +0200 Message-ID: <20230913125113.313322-6-david@redhat.com> In-Reply-To: <20230913125113.313322-1-david@redhat.com> References: <20230913125113.313322-1-david@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.6 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Let's sanity-check PageAnonExclusive vs. mapcount in page_add_anon_rmap() and hugepage_add_anon_rmap() after setting PageAnonExclusive simply by re-reading the mapcounts. We can stop initializing the "first" variable in page_add_anon_rmap() and no longer need an atomic_inc_and_test() in hugepage_add_anon_rmap(). While at it, switch to VM_WARN_ON_FOLIO(). Signed-off-by: David Hildenbrand Reported-by: syzbot+6e4f59235036c3c2e296@syzkaller.appspotmail.com --- mm/rmap.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/mm/rmap.c b/mm/rmap.c index 489c142d073b..10d477a0991f 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -1199,7 +1199,7 @@ void page_add_anon_rmap(struct page *page, struct vm_= area_struct *vma, atomic_t *mapped =3D &folio->_nr_pages_mapped; int nr =3D 0, nr_pmdmapped =3D 0; bool compound =3D flags & RMAP_COMPOUND; - bool first =3D true; + bool first; =20 /* Is page being mapped by PTE? Is this its first map to be added? */ if (likely(!compound)) { @@ -1228,9 +1228,6 @@ void page_add_anon_rmap(struct page *page, struct vm_= area_struct *vma, } } =20 - VM_BUG_ON_PAGE(!first && (flags & RMAP_EXCLUSIVE), page); - VM_BUG_ON_PAGE(!first && PageAnonExclusive(page), page); - if (nr_pmdmapped) __lruvec_stat_mod_folio(folio, NR_ANON_THPS, nr_pmdmapped); if (nr) @@ -1252,6 +1249,8 @@ void page_add_anon_rmap(struct page *page, struct vm_= area_struct *vma, } if (flags & RMAP_EXCLUSIVE) SetPageAnonExclusive(page); + VM_WARN_ON_FOLIO(page_mapcount(page) > 1 && PageAnonExclusive(page), + folio); =20 mlock_vma_folio(folio, vma, compound); } @@ -2532,15 +2531,14 @@ void hugepage_add_anon_rmap(struct page *page, stru= ct vm_area_struct *vma, unsigned long address, rmap_t flags) { struct folio *folio =3D page_folio(page); - int first; =20 VM_WARN_ON_FOLIO(!folio_test_anon(folio), folio); =20 - first =3D atomic_inc_and_test(&folio->_entire_mapcount); - VM_BUG_ON_PAGE(!first && (flags & RMAP_EXCLUSIVE), page); - VM_BUG_ON_PAGE(!first && PageAnonExclusive(page), page); + atomic_inc(&folio->_entire_mapcount); if (flags & RMAP_EXCLUSIVE) SetPageAnonExclusive(page); + VM_WARN_ON_FOLIO(folio_entire_mapcount(folio) > 1 && + PageAnonExclusive(page), folio); } =20 void hugepage_add_new_anon_rmap(struct folio *folio, --=20 2.41.0 From nobody Sat Feb 7 18:45:59 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 784A2EDEC4D for ; Wed, 13 Sep 2023 12:52:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240778AbjIMMwc (ORCPT ); Wed, 13 Sep 2023 08:52:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58028 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240587AbjIMMwV (ORCPT ); Wed, 13 Sep 2023 08:52:21 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 787C719BB for ; Wed, 13 Sep 2023 05:51:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1694609492; 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=t7xwwUMm6Z1sTtxTNKBbcilCnHU1Bmn/mrplLkXOKnQ=; b=XjRh1VaHkxphM4qrtl5pZCD5YZrVeBkTtuXJ2mFaj7A0cLGZEDtzoflWtxzEYyJ95oxgxb sYWJJoIslHmcWZcvuF6wWJhHpPS/q0H5uScJqP2hesramQf7thyUMIUT9iHgfqupYBcucn KpQku2qKfrQD+M/yRcYGJvtu5G7cPZU= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-389-tgr1yN5WMfuxKuox-CRlxA-1; Wed, 13 Sep 2023 08:51:29 -0400 X-MC-Unique: tgr1yN5WMfuxKuox-CRlxA-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 06B9A857A9E; Wed, 13 Sep 2023 12:51:29 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.193.225]) by smtp.corp.redhat.com (Postfix) with ESMTP id BD4432156722; Wed, 13 Sep 2023 12:51:27 +0000 (UTC) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, David Hildenbrand , Andrew Morton , Mike Kravetz , Muchun Song Subject: [PATCH v1 6/6] mm/rmap: pass folio to hugepage_add_anon_rmap() Date: Wed, 13 Sep 2023 14:51:13 +0200 Message-ID: <20230913125113.313322-7-david@redhat.com> In-Reply-To: <20230913125113.313322-1-david@redhat.com> References: <20230913125113.313322-1-david@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.6 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Let's pass a folio; we are always mapping the entire thing. Signed-off-by: David Hildenbrand --- include/linux/rmap.h | 2 +- mm/migrate.c | 2 +- mm/rmap.c | 8 +++----- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/include/linux/rmap.h b/include/linux/rmap.h index 51cc21ebb568..d22f4d21a11c 100644 --- a/include/linux/rmap.h +++ b/include/linux/rmap.h @@ -203,7 +203,7 @@ void folio_add_file_rmap_range(struct folio *, struct p= age *, unsigned int nr, void page_remove_rmap(struct page *, struct vm_area_struct *, bool compound); =20 -void hugepage_add_anon_rmap(struct page *, struct vm_area_struct *, +void hugepage_add_anon_rmap(struct folio *, struct vm_area_struct *, unsigned long address, rmap_t flags); void hugepage_add_new_anon_rmap(struct folio *, struct vm_area_struct *, unsigned long address); diff --git a/mm/migrate.c b/mm/migrate.c index 123fc4dc0bc4..d4e8cbf0a704 100644 --- a/mm/migrate.c +++ b/mm/migrate.c @@ -247,7 +247,7 @@ static bool remove_migration_pte(struct folio *folio, =20 pte =3D arch_make_huge_pte(pte, shift, vma->vm_flags); if (folio_test_anon(folio)) - hugepage_add_anon_rmap(new, vma, pvmw.address, + hugepage_add_anon_rmap(folio, vma, pvmw.address, rmap_flags); else page_dup_file_rmap(new, true); diff --git a/mm/rmap.c b/mm/rmap.c index 10d477a0991f..789a2beb8b3a 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -2527,18 +2527,16 @@ void rmap_walk_locked(struct folio *folio, struct r= map_walk_control *rwc) * * RMAP_COMPOUND is ignored. */ -void hugepage_add_anon_rmap(struct page *page, struct vm_area_struct *vma, +void hugepage_add_anon_rmap(struct folio *folio, struct vm_area_struct *vm= a, unsigned long address, rmap_t flags) { - struct folio *folio =3D page_folio(page); - VM_WARN_ON_FOLIO(!folio_test_anon(folio), folio); =20 atomic_inc(&folio->_entire_mapcount); if (flags & RMAP_EXCLUSIVE) - SetPageAnonExclusive(page); + SetPageAnonExclusive(&folio->page); VM_WARN_ON_FOLIO(folio_entire_mapcount(folio) > 1 && - PageAnonExclusive(page), folio); + PageAnonExclusive(&folio->page), folio); } =20 void hugepage_add_new_anon_rmap(struct folio *folio, --=20 2.41.0