From patchwork Mon Dec 11 15:56:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13487438 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 295B8C4167B for ; Mon, 11 Dec 2023 15:57:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B34626B0100; Mon, 11 Dec 2023 10:57:18 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id AE4296B0102; Mon, 11 Dec 2023 10:57:18 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9AD506B0103; Mon, 11 Dec 2023 10:57:18 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 883C26B0100 for ; Mon, 11 Dec 2023 10:57:18 -0500 (EST) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 39B03120790 for ; Mon, 11 Dec 2023 15:57:18 +0000 (UTC) X-FDA: 81554991756.27.243CEBF Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf12.hostedemail.com (Postfix) with ESMTP id 710C040009 for ; Mon, 11 Dec 2023 15:57:16 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=bEMFaQXf; spf=pass (imf12.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1702310236; 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=NEVRdWv5MmhzBcZ7w5tNkRp7uojKujUjmq26NniU0lw=; b=y27BZaQgmOJOXw2ZVppwbjMwiIZ/BeWwpk+U1QrYpUpNMmmLyjjMbpSKTxbcR71QM10flR 6U+Xz/wepn0r3lljdmmPwpbpduAJm8pZZfIcxDky3HdhloCB2/dpJT/AkPf7U5tFRrgZWv AW5R8RqyE/uY3tmstRU6u0zGpnDW3qA= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1702310236; a=rsa-sha256; cv=none; b=6By/6zpjlMdsVmwmuDscmAUnLjBG5DKJHOpsSTQ89MtmbwEBHZsH6KcO71oUURLmHLm+1R 8+tRv5juiz37HmZzFJqqoFuxdaH/1GQhKQ2TyXtnj+VCwMeOK2I4ocMYV2qiZU2Aigdh7j 3H2hiKHEH/c/jgGsOPOhsbcw+efJM+k= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=bEMFaQXf; spf=pass (imf12.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1702310235; 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=NEVRdWv5MmhzBcZ7w5tNkRp7uojKujUjmq26NniU0lw=; b=bEMFaQXfk0hztuuzstaaJvONUWUpQgUpBaijog4k4rH4OtdQTi4AMKwa+xzwuCEKreJVvR 1FyCLgS5Rz85I1SDOQ+0n/YDbXe3HXnngsZ2YGDc+DEJCJF80MUttMVC5x/JYXOECsUwD5 q+VmeCIylVb/jts0COGyhPB3cCUL4M0= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-173-qBz2N7eWPmuxhrmCuHZ-Dw-1; Mon, 11 Dec 2023 10:57:12 -0500 X-MC-Unique: qBz2N7eWPmuxhrmCuHZ-Dw-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (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 3226A28078D6; Mon, 11 Dec 2023 15:57:10 +0000 (UTC) Received: from t14s.redhat.com (unknown [10.39.192.166]) by smtp.corp.redhat.com (Postfix) with ESMTP id 071301121306; Mon, 11 Dec 2023 15:57:07 +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 v1 05/39] mm/rmap: introduce and use hugetlb_try_share_anon_rmap() Date: Mon, 11 Dec 2023 16:56:18 +0100 Message-ID: <20231211155652.131054-6-david@redhat.com> In-Reply-To: <20231211155652.131054-1-david@redhat.com> References: <20231211155652.131054-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.3 X-Stat-Signature: 3psrtjt3ck1zse59oa9qp4zdgwqx4xpo X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 710C040009 X-Rspam-User: X-HE-Tag: 1702310236-287143 X-HE-Meta: U2FsdGVkX1/D2aN5vnBeKkG9d9FiiGKngYgBtdAFIaN0RTRT2mRC4NGBbPp5anY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RWFRziT6bRdWuhSyivjgP0P6L1wwCr6fVv65OsDYcGKGbjOIFviGnGAQBRH+PTWt3pzKDUG1hXOClmw== 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 would want to swapout. Reviewed-by: Yin Fengwei Signed-off-by: David Hildenbrand Reviewed-by: Ryan Roberts --- include/linux/rmap.h | 23 +++++++++++++++++++++++ mm/rmap.c | 15 ++++++++++----- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/include/linux/rmap.h b/include/linux/rmap.h index ca42b3db5688..4c0650e9f6db 100644 --- a/include/linux/rmap.h +++ b/include/linux/rmap.h @@ -228,6 +228,29 @@ 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); + + /* Paired with the memory barrier in try_grab_folio(). */ + if (IS_ENABLED(CONFIG_HAVE_FAST_GUP)) + smp_mb(); + + if (unlikely(folio_maybe_dma_pinned(folio))) + return -EBUSY; + ClearPageAnonExclusive(&folio->page); + + /* + * This is conceptually a smp_wmb() paired with the smp_rmb() in + * gup_must_unshare(). + */ + 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 4e60c1f38eaa..e210ac1b73de 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -2147,13 +2147,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(subpage)) { + set_pte_at(mm, address, pvmw.pte, pteval); ret = false; page_vma_mapped_walk_done(&pvmw); break;