From patchwork Mon Dec 4 14:21:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13478496 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 278D0C10F07 for ; Mon, 4 Dec 2023 14:22:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DC63F6B00F3; Mon, 4 Dec 2023 09:22:05 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id CD6976B00F5; Mon, 4 Dec 2023 09:22:05 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AD71F6B00F9; Mon, 4 Dec 2023 09:22:05 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 97A856B00F3 for ; Mon, 4 Dec 2023 09:22:05 -0500 (EST) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 683F11C04E3 for ; Mon, 4 Dec 2023 14:22:05 +0000 (UTC) X-FDA: 81529350210.22.4177CF6 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf28.hostedemail.com (Postfix) with ESMTP id 9FE87C0009 for ; Mon, 4 Dec 2023 14:22:03 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=EU4P7Nc4; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf28.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1701699723; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=4nUQXJdzLDxn5x/X9wL3ukpxzJ+76ZeroIzXMEVl8GM=; b=rNq7WgSAhwJv+HDWi2bKPlnLlsn1Fi0ZwcLih6aVsYjNGLOQpJln5XBQfFQYJOleGdBwiZ AgPs8erwVhJugy2tSeHX4oB13OWhLdWMUq+YB4lr7xRNnsToTYmD35439SAvrojJaMcs9M gS/LUMzgvrdusZc4Ol+DgkZuYYy36AU= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=EU4P7Nc4; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf28.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1701699723; a=rsa-sha256; cv=none; b=VEeClG+938+FYDLAzD93izMVLvnhXUHdPE28obFU/YLDw0P5WMZoHhamcsCHR2P/1FbR97 J6KyLEOS0Q3NozzXC7xTCTf4/ZdNKDPtolUFIru6aCPt7l1qfHmsBAuqB1fqAOgjb0y19s j/r3Zv6Xvd/8HGTPmZQVCXaH7usfCq8= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1701699722; 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=4nUQXJdzLDxn5x/X9wL3ukpxzJ+76ZeroIzXMEVl8GM=; b=EU4P7Nc48r1Q/s7gIAIcakDFW5eCGHdgSDQTiToa7KJQXevbAYDD0ms31fu+rE4g5i/DaA W0vThFJDsvukHGK3/Uj+POzq1z5WI73tge+cgukFgA+K0AucXrYUQwC3q7b5bNIEXTuVsj 5AFr8cHlLMDDRHzb/+Um7TV8rwZCznQ= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-9-ssjzReYIPVSCXays4yclSw-1; Mon, 04 Dec 2023 09:21:58 -0500 X-MC-Unique: ssjzReYIPVSCXays4yclSw-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (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 mimecast-mx02.redhat.com (Postfix) with ESMTPS id EA9F3101A590; Mon, 4 Dec 2023 14:21:57 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.195.87]) by smtp.corp.redhat.com (Postfix) with ESMTP id 572442026D4C; Mon, 4 Dec 2023 14:21:56 +0000 (UTC) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, David Hildenbrand , Andrew Morton , "Matthew Wilcox (Oracle)" , Hugh Dickins , Ryan Roberts , Yin Fengwei , Mike Kravetz , Muchun Song , Peter Xu Subject: [PATCH RFC 05/39] mm/rmap: introduce and use hugetlb_try_share_anon_rmap() Date: Mon, 4 Dec 2023 15:21:12 +0100 Message-ID: <20231204142146.91437-6-david@redhat.com> In-Reply-To: <20231204142146.91437-1-david@redhat.com> References: <20231204142146.91437-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.4 X-Rspam-User: X-Stat-Signature: pr6jcxnhfuqntdb67q9s9tp8zcduwhxh X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 9FE87C0009 X-HE-Tag: 1701699723-710990 X-HE-Meta: U2FsdGVkX18+OHpmwfg7heNGrU34c8h0I9fCcdZuqOG6mWk5KGAu0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if6Yp8OgDuJAjKwrVeFloG9rcGRrrau3VFPIZKH2Iq6yBtxpFh1tvPRquOQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: hugetlb rmap handling differs quite a lot from "ordinary" rmap code. For example, hugetlb currently only supports entire mappings, and treats any mapping as mapped using a single "logical PTE". Let's move it out of the way so we can overhaul our "ordinary" rmap. implementation/interface. So let's introduce and use hugetlb_try_dup_anon_rmap() to make all hugetlb handling use dedicated hugetlb_* rmap functions. Note that try_to_unmap_one() does not need care. Easy to spot because among all that nasty hugetlb special-casing in that function, we're not using set_huge_pte_at() on the anon path -- well, and that code assumes that we we would want to swapout. Signed-off-by: David Hildenbrand Reviewed-by: Yin Fengwei --- include/linux/rmap.h | 20 ++++++++++++++++++++ mm/rmap.c | 15 ++++++++++----- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/include/linux/rmap.h b/include/linux/rmap.h index 8068c332e2ce5..3f38141b53b9d 100644 --- a/include/linux/rmap.h +++ b/include/linux/rmap.h @@ -223,6 +223,26 @@ static inline int hugetlb_try_dup_anon_rmap(struct folio *folio, return 0; } +/* See page_try_share_anon_rmap() */ +static inline int hugetlb_try_share_anon_rmap(struct folio *folio) +{ + VM_WARN_ON_FOLIO(!folio_test_anon(folio), folio); + VM_WARN_ON_FOLIO(!PageAnonExclusive(&folio->page), folio); + + /* See page_try_share_anon_rmap() */ + if (IS_ENABLED(CONFIG_HAVE_FAST_GUP)) + smp_mb(); + + if (unlikely(folio_maybe_dma_pinned(folio))) + return -EBUSY; + ClearPageAnonExclusive(&folio->page); + + /* See page_try_share_anon_rmap() */ + if (IS_ENABLED(CONFIG_HAVE_FAST_GUP)) + smp_mb__after_atomic(); + return 0; +} + static inline void hugetlb_add_file_rmap(struct folio *folio) { VM_WARN_ON_FOLIO(folio_test_anon(folio), folio); diff --git a/mm/rmap.c b/mm/rmap.c index 5037581b79ec6..2f1af3958e687 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -2105,13 +2105,18 @@ static bool try_to_migrate_one(struct folio *folio, struct vm_area_struct *vma, !anon_exclusive, subpage); /* See page_try_share_anon_rmap(): clear PTE first. */ - if (anon_exclusive && - page_try_share_anon_rmap(subpage)) { - if (folio_test_hugetlb(folio)) + if (folio_test_hugetlb(folio)) { + if (anon_exclusive && + hugetlb_try_share_anon_rmap(folio)) { set_huge_pte_at(mm, address, pvmw.pte, pteval, hsz); - else - set_pte_at(mm, address, pvmw.pte, pteval); + ret = false; + page_vma_mapped_walk_done(&pvmw); + break; + } + } else if (anon_exclusive && + page_try_share_anon_rmap(page)) { + set_pte_at(mm, address, pvmw.pte, pteval); ret = false; page_vma_mapped_walk_done(&pvmw); break;