From patchwork Mon Oct 2 14:29:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 147489 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2a8e:b0:403:3b70:6f57 with SMTP id in14csp1712362vqb; Mon, 2 Oct 2023 14:58:10 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGltp/vf7XvoQIJ8kVvih8bT1nwqr4P+HFCbfFbQWJ/m8bGokYADPgTEmp0iIhSnGbp8dEA X-Received: by 2002:a05:6830:c8b:b0:6c6:473c:9ca0 with SMTP id bn11-20020a0568300c8b00b006c6473c9ca0mr8388567otb.3.1696283889828; Mon, 02 Oct 2023 14:58:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696283889; cv=none; d=google.com; s=arc-20160816; b=EOJE+Ti+u1xN1fZ9C3C91dFFHstv87QI/FyzlDqVTjiDHiSJ6jV7cBKyEJ5jA1SURB dGVYqyoMaW7IjuMwq5BeuN+X1Nk2ioziq/4eH/W7Tmy1R0HyGIXuJs2Zw+HauAmcf9I8 JJ9lKz9yGdBSNb8yPNFD42pCg1YGtPtiG7qgzWL7sHh09tTvt/uTfPyJEPortugNCvkU 5ZVbGPw3p6xvC33Edav8vAD5E8Med4yRxf9XO5mjuAoKUSvhpUrO1pnYWk9tVhVMlZF0 LLU0fgnomtHqAFa8SbyoyXnkzOfolJOIsUpIJgIzIq5J1gpMPqcJsPBCi0MZbwKnkFpz 0VBg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=74/4kguVt+JNeClEzh2bgjVFXMUXyl4kPsimySG+lfk=; fh=9zMraF9xqB+ji/y8otbFQF1V52x00H8qsdWu3Nrm/O0=; b=rmChMkVcOfMrDTxQhBmCLhC6A06eTfsZkIELatrzNbSKtGYilYKvE72gpiH+YgHNih s3kzzwTqRIJgy5dWjuPou6YoiyBLJoXnz+vCifruCK377KgXmnltr8v/y060oCQiCot9 6vx8HfLNpn4a6714rKyacHRGGjxyMStCXQHZ2pNSrZUdgzeSEqp7/TR2cigxdfg34r07 +rZaVLyJRP7WYSJX38fJQb+ylruovSah42BTBl8gU40UWNJWWJ6HaPH8IvUvGZwW88Q+ 0nw8XKfujJsYV/7HvDNiNlAs5ccPAaz0tFF11Hfh5ORyLf/v5FrPG3IpRFfxyAsI1c+L gPsw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Y5U0Y6mU; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from howler.vger.email (howler.vger.email. [23.128.96.34]) by mx.google.com with ESMTPS id l72-20020a63914b000000b005859b2d8d7asi9261261pge.4.2023.10.02.14.58.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Oct 2023 14:58:09 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) client-ip=23.128.96.34; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Y5U0Y6mU; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id 47F21802EAC9; Mon, 2 Oct 2023 07:31:09 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237786AbjJBOau (ORCPT + 18 others); Mon, 2 Oct 2023 10:30:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37364 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237778AbjJBOaq (ORCPT ); Mon, 2 Oct 2023 10:30:46 -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 ESMTPS id 8C8E0A4 for ; Mon, 2 Oct 2023 07:29:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1696256997; 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=74/4kguVt+JNeClEzh2bgjVFXMUXyl4kPsimySG+lfk=; b=Y5U0Y6mUvc8tOXPyv+L+x1UMw0c1OVT8QsrdyGbyM1M/50aKRRxm6XtRpswBuwazG1AzP7 lV8tLpNd/idC0q8/4mPNDBCJTLSbwbVei6gZ1VJjwg5HYTXSeCmwzEfP0Cw7hYv5EZpVI8 pIkD6uJfBxaJmPygfwPQdY3+Cd/aZmA= 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-237-MXePQoblNP-G9nwx88mC8A-1; Mon, 02 Oct 2023 10:29:53 -0400 X-MC-Unique: MXePQoblNP-G9nwx88mC8A-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (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 C5A84801779; Mon, 2 Oct 2023 14:29:52 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.194.19]) by smtp.corp.redhat.com (Postfix) with ESMTP id A52811005B90; Mon, 2 Oct 2023 14:29:51 +0000 (UTC) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, David Hildenbrand , Andrew Morton , Mike Kravetz , Muchun Song , Suren Baghdasaryan Subject: [PATCH v1 1/3] mm/rmap: move SetPageAnonExclusive() out of page_move_anon_rmap() Date: Mon, 2 Oct 2023 16:29:47 +0200 Message-ID: <20231002142949.235104-2-david@redhat.com> In-Reply-To: <20231002142949.235104-1-david@redhat.com> References: <20231002142949.235104-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Mon, 02 Oct 2023 07:31:09 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1778682576012067715 X-GMAIL-MSGID: 1778682576012067715 Let's move it into the caller: there is a difference between whether an anon folio can only be mapped by one process (e.g., into one VMA), and whether it is truly exclusive (e.g., no references -- including GUP -- from other processes). Further, for large folios the page might not actually be pointing at the head page of the folio, so it better be handled in the caller. This is a preparation for converting page_move_anon_rmap() to consume a folio. Signed-off-by: David Hildenbrand Reviewed-by: Suren Baghdasaryan Reviewed-by: Vishal Moola (Oracle) --- mm/huge_memory.c | 1 + mm/hugetlb.c | 4 +++- mm/memory.c | 1 + mm/rmap.c | 1 - 4 files changed, 5 insertions(+), 2 deletions(-) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index e54fb9c542bb..01d0d65ece13 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -1506,6 +1506,7 @@ vm_fault_t do_huge_pmd_wp_page(struct vm_fault *vmf) pmd_t entry; page_move_anon_rmap(page, vma); + SetPageAnonExclusive(page); folio_unlock(folio); reuse: if (unlikely(unshare)) { diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 9c22297d9c57..24591fc145ff 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -5460,8 +5460,10 @@ static vm_fault_t hugetlb_wp(struct mm_struct *mm, struct vm_area_struct *vma, * owner and can reuse this page. */ if (folio_mapcount(old_folio) == 1 && folio_test_anon(old_folio)) { - if (!PageAnonExclusive(&old_folio->page)) + if (!PageAnonExclusive(&old_folio->page)) { page_move_anon_rmap(&old_folio->page, vma); + SetPageAnonExclusive(&old_folio->page); + } if (likely(!unshare)) set_huge_ptep_writable(vma, haddr, ptep); diff --git a/mm/memory.c b/mm/memory.c index d4820802b01b..9de231c92769 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -3484,6 +3484,7 @@ static vm_fault_t do_wp_page(struct vm_fault *vmf) * sunglasses. Hit it. */ page_move_anon_rmap(vmf->page, vma); + SetPageAnonExclusive(vmf->page); folio_unlock(folio); reuse: if (unlikely(unshare)) { diff --git a/mm/rmap.c b/mm/rmap.c index 77222adccda1..854ccbd66954 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -1165,7 +1165,6 @@ void page_move_anon_rmap(struct page *page, struct vm_area_struct *vma) * folio_test_anon()) will not see one without the other. */ WRITE_ONCE(folio->mapping, anon_vma); - SetPageAnonExclusive(page); } /** From patchwork Mon Oct 2 14:29:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 147324 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2a8e:b0:403:3b70:6f57 with SMTP id in14csp1475044vqb; Mon, 2 Oct 2023 07:44:19 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHncDTlK0PXwx/MO+ndyFzo5Clvk50MIr5nqnWww54MWa19GWErmAAPL9It0WkRmaDFpamc X-Received: by 2002:a05:6a00:8015:b0:68e:2fd4:288a with SMTP id eg21-20020a056a00801500b0068e2fd4288amr12153706pfb.3.1696257859709; Mon, 02 Oct 2023 07:44:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696257859; cv=none; d=google.com; s=arc-20160816; b=CQNA7UhfSMV3xS/5MgdMt1ffRTEAQtJ17O5HdzVA7XmijGmXeLsqL8jOuk6QN9/7bP YHG++h4qTyFQuQaLWSjgxYBocdfkzpW7N6wm8vld7TFknfQG3E4F7H3bP3cttSzIlCkB dH0KlRglFs+LKLnV5om2Okm9WwhyIBB4rAXSpW0i1zSXqB9CKn0zhmdnrNq9tDW4rdlm rFhhbzD0arDfICrCSmDsK7cMTUfdvu1ycIkRumCHNJ5f4zW+krYHyq6b73Dto+wMhFcA ZEPx1nYELDGzywgUK2ff2OiL2IrB09kb3uM/usujAjcv11d5JH7ia5ShKJqirb5hs2eE rMAQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=iuOLWxV07WMAFicgIRp5L49gcu/vw0Qb9ZJxOn72iWI=; fh=9zMraF9xqB+ji/y8otbFQF1V52x00H8qsdWu3Nrm/O0=; b=wlKsrde181I3wvHEy/guT6bwdnbwgdLrmKRNU1HxurcDfk+MWfD8FXwD8QwbbZOph9 R9Mgkquh0tec2TEQ8HERCuQalEnf1+QbNiv1OjhThld7RBpwb4Y6elqCrcdR/6hLy1G8 f0QEqJP4Ii03xQcR7X4oiD4NHfQS0sT5e/1Y/H6MXAHxAJsZToEmvp9uJybQFabGVNxU MSmeL6+OhucTQl/d/igImfEl2Ve8gBGcRS2ZCD9HTKKTVvvKBtYF1QT5+KsEsGMov4W6 wxxVb7RbKH6IImFTrCh1Zqw6MHNrjQyY4q1xiJY/D+GgE0zgUsaNIuiqCK10AAAbki2w Qhrw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Rp5mwb27; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id s140-20020a632c92000000b005859c221c6dsi8545477pgs.221.2023.10.02.07.44.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Oct 2023 07:44:19 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Rp5mwb27; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id CC3FF808D219; Mon, 2 Oct 2023 07:30:52 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237753AbjJBOah (ORCPT + 18 others); Mon, 2 Oct 2023 10:30:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37382 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237639AbjJBOag (ORCPT ); Mon, 2 Oct 2023 10:30:36 -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 ESMTPS id B32A2B4 for ; Mon, 2 Oct 2023 07:29:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1696256998; 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=iuOLWxV07WMAFicgIRp5L49gcu/vw0Qb9ZJxOn72iWI=; b=Rp5mwb27nYLeg/ebKD8hUPPEEqGAh7M8PnrgvsHCCLasadeMTcKs+opWXHRbmeIVaRSgdE RKoOeLcW4Wvjsr/nufhf8jb+7isNJrCw3GksEfIQJKl9v/bZc2O/8y1hX3dLHXApReTaX4 cJ6sRzEiboU/KZOpilrmTWHJOy6m0eQ= 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-564-A63WA2viPemAtiRYluLjVw-1; Mon, 02 Oct 2023 10:29:54 -0400 X-MC-Unique: A63WA2viPemAtiRYluLjVw-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (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 26B71802C1A; Mon, 2 Oct 2023 14:29:54 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.194.19]) by smtp.corp.redhat.com (Postfix) with ESMTP id 09C2510EE6CE; Mon, 2 Oct 2023 14:29:52 +0000 (UTC) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, David Hildenbrand , Andrew Morton , Mike Kravetz , Muchun Song , Suren Baghdasaryan Subject: [PATCH v1 2/3] mm/rmap: convert page_move_anon_rmap() to folio_move_anon_rmap() Date: Mon, 2 Oct 2023 16:29:48 +0200 Message-ID: <20231002142949.235104-3-david@redhat.com> In-Reply-To: <20231002142949.235104-1-david@redhat.com> References: <20231002142949.235104-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Mon, 02 Oct 2023 07:30:52 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1778655281609936748 X-GMAIL-MSGID: 1778655281609936748 Let's convert it to consume a folio. Signed-off-by: David Hildenbrand Reviewed-by: Suren Baghdasaryan Reviewed-by: Vishal Moola (Oracle) --- include/linux/rmap.h | 2 +- mm/huge_memory.c | 2 +- mm/hugetlb.c | 2 +- mm/memory.c | 2 +- mm/rmap.c | 16 +++++++--------- 5 files changed, 11 insertions(+), 13 deletions(-) diff --git a/include/linux/rmap.h b/include/linux/rmap.h index 064b432a4033..8034eda972e5 100644 --- a/include/linux/rmap.h +++ b/include/linux/rmap.h @@ -194,7 +194,7 @@ typedef int __bitwise rmap_t; /* * rmap interfaces called when adding or removing pte of page */ -void page_move_anon_rmap(struct page *, struct vm_area_struct *); +void folio_move_anon_rmap(struct folio *, struct vm_area_struct *); void page_add_anon_rmap(struct page *, struct vm_area_struct *, unsigned long address, rmap_t flags); void page_add_new_anon_rmap(struct page *, struct vm_area_struct *, diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 01d0d65ece13..08245226ccb8 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -1505,7 +1505,7 @@ vm_fault_t do_huge_pmd_wp_page(struct vm_fault *vmf) if (folio_ref_count(folio) == 1) { pmd_t entry; - page_move_anon_rmap(page, vma); + folio_move_anon_rmap(folio, vma); SetPageAnonExclusive(page); folio_unlock(folio); reuse: diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 24591fc145ff..e52c6048e74f 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -5461,7 +5461,7 @@ static vm_fault_t hugetlb_wp(struct mm_struct *mm, struct vm_area_struct *vma, */ if (folio_mapcount(old_folio) == 1 && folio_test_anon(old_folio)) { if (!PageAnonExclusive(&old_folio->page)) { - page_move_anon_rmap(&old_folio->page, vma); + folio_move_anon_rmap(old_folio, vma); SetPageAnonExclusive(&old_folio->page); } if (likely(!unshare)) diff --git a/mm/memory.c b/mm/memory.c index 9de231c92769..1f0e3317cbdd 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -3483,7 +3483,7 @@ static vm_fault_t do_wp_page(struct vm_fault *vmf) * and the folio is locked, it's dark out, and we're wearing * sunglasses. Hit it. */ - page_move_anon_rmap(vmf->page, vma); + folio_move_anon_rmap(folio, vma); SetPageAnonExclusive(vmf->page); folio_unlock(folio); reuse: diff --git a/mm/rmap.c b/mm/rmap.c index 854ccbd66954..37f05f33559b 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -1141,19 +1141,17 @@ int folio_total_mapcount(struct folio *folio) } /** - * page_move_anon_rmap - move a page to our anon_vma - * @page: the page to move to our anon_vma - * @vma: the vma the page belongs to + * folio_move_anon_rmap - move a folio to our anon_vma + * @page: The folio to move to our anon_vma + * @vma: The vma the folio belongs to * - * When a page belongs exclusively to one process after a COW event, - * that page can be moved into the anon_vma that belongs to just that - * process, so the rmap code will not search the parent or sibling - * processes. + * When a folio belongs exclusively to one process after a COW event, + * that folio can be moved into the anon_vma that belongs to just that + * process, so the rmap code will not search the parent or sibling processes. */ -void page_move_anon_rmap(struct page *page, struct vm_area_struct *vma) +void folio_move_anon_rmap(struct folio *folio, struct vm_area_struct *vma) { void *anon_vma = vma->anon_vma; - struct folio *folio = page_folio(page); VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio); VM_BUG_ON_VMA(!anon_vma, vma); From patchwork Mon Oct 2 14:29:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 147457 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2a8e:b0:403:3b70:6f57 with SMTP id in14csp1668058vqb; Mon, 2 Oct 2023 13:16:56 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHuytOibm2bbKIgFzBEwQs3RK0ynX5Uo8WRo+YeRfx7LZ/b+O6jf6pdE7qavUKViLc7gXlg X-Received: by 2002:a05:6358:7e84:b0:13a:4855:d885 with SMTP id o4-20020a0563587e8400b0013a4855d885mr14181541rwn.10.1696277815786; Mon, 02 Oct 2023 13:16:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696277815; cv=none; d=google.com; s=arc-20160816; b=Cw5d12RJ/fFQWCmOsekoAcXhUDwrBQyAiMwAoO54YwfXCObAZdlOOK6IWctIDvorXk z/Rnf6lgp9Z8KSiEnvucyxVrf/jYUbEf3221zosO318381/gwvDGiXvsfFygQutAoXAT G2/Nf9V9knCx/wRco4G0OGqx3f+Sidamv9SZh9IrB2B+Oup/GZm/Gi62x1PnHiN4/DoF SBAgdPiQjQlB7ozHHhhU1X71qFomDe2sP8EAPpkoOkE+E93qUP8qMU27YhlQ9Emy4XsV To38/xtCM2T8OGSSvzWBUUropg065Bp7jsgVfPngpO6G8KpzzQVhIXJi3SMC5HF+5i2U cnTQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=Tnnd1i96YoBpg2Wk1+SUJX4WepSITGLgtW2LVCbh8ow=; fh=9zMraF9xqB+ji/y8otbFQF1V52x00H8qsdWu3Nrm/O0=; b=prz8vhy9SwfFWjtHxm+6y/YcWJxSJryd37hssY/EDqP2822ZmbCzZ+0mCoGMtGFpbK 2M+PFYKyrTz2VPv5JlNWWjsCzKE8Ti2uJRmSdYNJT1yFF8WMbJC1V6k4O72Y+/ZDUQQe gND2J96RFW471hQQDWPR154nYY0SgNvPERq/oENQhChje1ifDNlFIaTvwxDIiXXqFRcg VPWNX89E0aZT7gGiS6cBtOhVam0pVXxOTbjx/PefspTSAIOqdsVUGd/+2j2gAyIf9g04 Fx1KDbbg56jZ53TscgaGIqzyuKLhCc3HadHl6AH8fXfO1woJaXTjwAoBGGc4UyggSfkW oQNA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=gdxffqR5; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id j64-20020a638043000000b00573f6eafd25si27998656pgd.572.2023.10.02.13.16.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Oct 2023 13:16:55 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=gdxffqR5; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 7E5C8802C7C9; Mon, 2 Oct 2023 07:31:29 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237773AbjJBOay (ORCPT + 18 others); Mon, 2 Oct 2023 10:30:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37390 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237779AbjJBOaq (ORCPT ); Mon, 2 Oct 2023 10:30:46 -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 ESMTPS id 382BCB0 for ; Mon, 2 Oct 2023 07:30:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1696257002; 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=Tnnd1i96YoBpg2Wk1+SUJX4WepSITGLgtW2LVCbh8ow=; b=gdxffqR5WYhgRxe/ZADdLcraC3CbWZeOZUdt8MfBQM7Gd+1makCMtW+CtEKPY3yxgK3iXc RpxEaNA7Q0G0Gqxmz8v455aPxEJlexwNM2v3giSWL4GiS6hj2fM1Zuamd1k3OQ7yjX4OD7 1FMN2ToaymwixrqypLYGjPphpvIcY/U= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-596-SD06yepTPyaOIWqDKjRAKw-1; Mon, 02 Oct 2023 10:29:56 -0400 X-MC-Unique: SD06yepTPyaOIWqDKjRAKw-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (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 805873826D2C; Mon, 2 Oct 2023 14:29:55 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.194.19]) by smtp.corp.redhat.com (Postfix) with ESMTP id 5F47B1005B90; Mon, 2 Oct 2023 14:29:54 +0000 (UTC) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, David Hildenbrand , Andrew Morton , Mike Kravetz , Muchun Song , Suren Baghdasaryan Subject: [PATCH v1 3/3] memory: move exclusivity detection in do_wp_page() into wp_can_reuse_anon_folio() Date: Mon, 2 Oct 2023 16:29:49 +0200 Message-ID: <20231002142949.235104-4-david@redhat.com> In-Reply-To: <20231002142949.235104-1-david@redhat.com> References: <20231002142949.235104-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 X-Spam-Status: No, score=1.2 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL, RCVD_IN_SBL_CSS,SPF_HELO_NONE,SPF_NONE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Level: * X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Mon, 02 Oct 2023 07:31:29 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1778676206626428114 X-GMAIL-MSGID: 1778676206626428114 Let's clean up do_wp_page() a bit, removing two labels and making it a easier to read. wp_can_reuse_anon_folio() now only operates on the whole folio. Move the SetPageAnonExclusive() out into do_wp_page(). No need to do this under page lock -- the page table lock is sufficient. Signed-off-by: David Hildenbrand --- mm/memory.c | 88 +++++++++++++++++++++++++++-------------------------- 1 file changed, 45 insertions(+), 43 deletions(-) diff --git a/mm/memory.c b/mm/memory.c index 1f0e3317cbdd..512f6f05620e 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -3358,6 +3358,44 @@ static vm_fault_t wp_page_shared(struct vm_fault *vmf, struct folio *folio) return ret; } +static bool wp_can_reuse_anon_folio(struct folio *folio, + struct vm_area_struct *vma) +{ + /* + * We have to verify under folio lock: these early checks are + * just an optimization to avoid locking the folio and freeing + * the swapcache if there is little hope that we can reuse. + * + * KSM doesn't necessarily raise the folio refcount. + */ + if (folio_test_ksm(folio) || folio_ref_count(folio) > 3) + return false; + if (!folio_test_lru(folio)) + /* + * We cannot easily detect+handle references from + * remote LRU caches or references to LRU folios. + */ + lru_add_drain(); + if (folio_ref_count(folio) > 1 + folio_test_swapcache(folio)) + return false; + if (!folio_trylock(folio)) + return false; + if (folio_test_swapcache(folio)) + folio_free_swap(folio); + if (folio_test_ksm(folio) || folio_ref_count(folio) != 1) { + folio_unlock(folio); + return false; + } + /* + * Ok, we've got the only folio reference from our mapping + * and the folio is locked, it's dark out, and we're wearing + * sunglasses. Hit it. + */ + folio_move_anon_rmap(folio, vma); + folio_unlock(folio); + return true; +} + /* * This routine handles present pages, when * * users try to write to a shared page (FAULT_FLAG_WRITE) @@ -3444,49 +3482,14 @@ static vm_fault_t do_wp_page(struct vm_fault *vmf) /* * Private mapping: create an exclusive anonymous page copy if reuse * is impossible. We might miss VM_WRITE for FOLL_FORCE handling. + * + * If we encounter a page that is marked exclusive, we must reuse + * the page without further checks. */ - if (folio && folio_test_anon(folio)) { - /* - * If the page is exclusive to this process we must reuse the - * page without further checks. - */ - if (PageAnonExclusive(vmf->page)) - goto reuse; - - /* - * We have to verify under folio lock: these early checks are - * just an optimization to avoid locking the folio and freeing - * the swapcache if there is little hope that we can reuse. - * - * KSM doesn't necessarily raise the folio refcount. - */ - if (folio_test_ksm(folio) || folio_ref_count(folio) > 3) - goto copy; - if (!folio_test_lru(folio)) - /* - * We cannot easily detect+handle references from - * remote LRU caches or references to LRU folios. - */ - lru_add_drain(); - if (folio_ref_count(folio) > 1 + folio_test_swapcache(folio)) - goto copy; - if (!folio_trylock(folio)) - goto copy; - if (folio_test_swapcache(folio)) - folio_free_swap(folio); - if (folio_test_ksm(folio) || folio_ref_count(folio) != 1) { - folio_unlock(folio); - goto copy; - } - /* - * Ok, we've got the only folio reference from our mapping - * and the folio is locked, it's dark out, and we're wearing - * sunglasses. Hit it. - */ - folio_move_anon_rmap(folio, vma); - SetPageAnonExclusive(vmf->page); - folio_unlock(folio); -reuse: + if (folio && folio_test_anon(folio) && + (PageAnonExclusive(vmf->page) || wp_can_reuse_anon_folio(folio, vma))) { + if (!PageAnonExclusive(vmf->page)) + SetPageAnonExclusive(vmf->page); if (unlikely(unshare)) { pte_unmap_unlock(vmf->pte, vmf->ptl); return 0; @@ -3494,7 +3497,6 @@ static vm_fault_t do_wp_page(struct vm_fault *vmf) wp_page_reuse(vmf); return 0; } -copy: /* * Ok, we need to copy. Oh, well.. */