From patchwork Wed Dec 20 22:44:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13500616 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 C1A91C46CD4 for ; Wed, 20 Dec 2023 22:45:22 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6E3286B0088; Wed, 20 Dec 2023 17:45:21 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 61C876B0089; Wed, 20 Dec 2023 17:45:21 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 47A6F6B008A; Wed, 20 Dec 2023 17:45:21 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 2F49A6B0088 for ; Wed, 20 Dec 2023 17:45:21 -0500 (EST) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 031C0A05D7 for ; Wed, 20 Dec 2023 22:45:20 +0000 (UTC) X-FDA: 81588679242.11.C27F55E Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf21.hostedemail.com (Postfix) with ESMTP id 685E51C0028 for ; Wed, 20 Dec 2023 22:45:19 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=glVxp5dg; spf=pass (imf21.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=1703112319; 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=MXqiJu2pun8vKoCf/nv4oO60M9YSAaLT+Ujt3IpxQcI=; b=8HHydAleIDEXz7Pa4QT5+k2kPAoPz9kZ3HN0Et8hKcSroq5qmFoX/aUi8ykMSU8fCO8gDO 191+5sfgZj9j1Jg5Mo2nRhmPfliRO2gs6ylXVJc1HHY1ZukyG/ChJLkfUfo4oyebHBgBos zYaE4EH6N1k2mdJHkT1q7UsnFNT0y1Q= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=glVxp5dg; spf=pass (imf21.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-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1703112319; a=rsa-sha256; cv=none; b=4srYIt39mo5nsdXdcyTMbytckJnc1vN+XBHogdLZqE5y1c505vP2aJ0i5jXJcC/kpgOrpF Pu6NwhuZhAYCG++k5JAqCxXKBdEkwl4KghwVX9zBsmS44RFA33Z/HjhEGmPLXdcjUlfh/k TQAQbPmT00JmdTIMm/DndIo7G5RojNE= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1703112318; 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=MXqiJu2pun8vKoCf/nv4oO60M9YSAaLT+Ujt3IpxQcI=; b=glVxp5dg4JhN9zWpgtzVPuPlrg8UUr6k9kMzEch47LaHVpdMcOGr8OrStE5R8kOy0sk4xE 6UjfIeQg2xEXJsTsa2C7Vp9q1DmXrXJsX/WNHyfxH8LAlsQQnu6oOgfePXuKsmN0GoolQ7 /dwr8hw7jLegI4Pegvu2tO2HW5urWrA= 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-615-YB96pkyOPFqEud271oQ8LQ-1; Wed, 20 Dec 2023 17:45:15 -0500 X-MC-Unique: YB96pkyOPFqEud271oQ8LQ-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (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 36C35101A551; Wed, 20 Dec 2023 22:45:15 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.192.101]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7185940C6EB9; Wed, 20 Dec 2023 22:45:13 +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 v2 03/40] mm/rmap: introduce and use hugetlb_add_file_rmap() Date: Wed, 20 Dec 2023 23:44:27 +0100 Message-ID: <20231220224504.646757-4-david@redhat.com> In-Reply-To: <20231220224504.646757-1-david@redhat.com> References: <20231220224504.646757-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.2 X-Rspamd-Queue-Id: 685E51C0028 X-Rspam-User: X-Stat-Signature: 447eirt8mp9pi53hh81doj638ifckn1q X-Rspamd-Server: rspam01 X-HE-Tag: 1703112319-65726 X-HE-Meta: U2FsdGVkX1/fMhwmqxsr7tnbKmxw8dqRslMRDiC1bkOiflA31+X+kiHa807k6L7hzJELuKySScKZEnU9NEXspkEmrBwiNL7yNm/AykbSCIGHBIySL8chV6pquKiP732qmPM2E7JZ8mD7sZiC+xvCWGVM/bmJ0iIcjtEiAm8cQ8zdNw3GBleO9hII10ZrkWBKcb5cYpDA1BrI9l5Hiwv/8SWaGS0rgSkLREHgHFrshF6WPPYjeo6MZVedOFYesLAH26rTlt6Tn/ihTkrZhr7T8wHBBsZxCmceixRwWhH7iof3F9TSrzQGj7HzQHcnowluT9Zo1LpI4v6q0uT45Xb/Sn2hRAjgfTA7T3biL368lZ8whFEsv7v6AXzN8J0wYYbobMtUmdGXDvMIQF3jZm++EB70qsYMilbPwp7iUHbnjwJDEDmiBpyZxoVoaH1nTk6OPLbyGZzLo/rV1TD7J2gtyLJxJIgBbhSG64J1fc4ytGlxvt9vCRE/rNLIMVe3d1IjwCp478mpdKDvWnIKv38Af+5f4Nw9EUE2FiUhqjEOQInNTC2x2lm3VFkZV/5FH69gTnHzuRC8l31/VV3YKbl8AfCveSyvSyC1mMqzJLIc///JB/WeO3oDyG7WsWqbkhkcTGShV6iw4VS9Jybs0aj0gJVhCB4ERCZrwZws99H+g9wjB6BjVEcJfA8Jc9Xfh1sgtFPLzeppaifRPPJNOrvEUJAFxCCiD42+yIiSJNJ/pOMoH36iKVC3sdru/3Wf+2gR6Jaf1i+5ExBXZZ8761qJrZj+j/opwTA7fe8vXLq8h0C5fM2NGJJCKNnGodzCu9wZmdbPp6+yCYs5OjtF4T3NZXhRbdIb9t5pjSmZ+bFUActv/LOUnOEFkuwftuov/UFC+S22cMzr/yiCTMshEL0R4ivnYqTYg+FYkipFksqC2X2izw66GLAI78YBB3JHWy9r 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. Right now we're using page_dup_file_rmap() in some cases where "ordinary" rmap code would have used page_add_file_rmap(). So let's introduce and use hugetlb_add_file_rmap() instead. We won't be adding a "hugetlb_dup_file_rmap()" functon for the fork() case, as it would be doing the same: "dup" is just an optimization for "add". What remains is a single page_dup_file_rmap() call in fork() code. Add sanity checks that we end up with the right folios in the right functions. Reviewed-by: Yin Fengwei Reviewed-by: Ryan Roberts Signed-off-by: David Hildenbrand Reviewed-by: Muchun Song Reviewed-by: Matthew Wilcox (Oracle) --- include/linux/rmap.h | 8 ++++++++ mm/hugetlb.c | 6 +++--- mm/migrate.c | 2 +- mm/rmap.c | 1 + 4 files changed, 13 insertions(+), 4 deletions(-) diff --git a/include/linux/rmap.h b/include/linux/rmap.h index 64ae6c4d72720..56900a16f41a6 100644 --- a/include/linux/rmap.h +++ b/include/linux/rmap.h @@ -211,6 +211,14 @@ void hugetlb_add_anon_rmap(struct folio *, struct vm_area_struct *, void hugetlb_add_new_anon_rmap(struct folio *, struct vm_area_struct *, unsigned long address); +static inline void hugetlb_add_file_rmap(struct folio *folio) +{ + VM_WARN_ON_FOLIO(!folio_test_hugetlb(folio), folio); + VM_WARN_ON_FOLIO(folio_test_anon(folio), folio); + + atomic_inc(&folio->_entire_mapcount); +} + static inline void hugetlb_remove_rmap(struct folio *folio) { VM_WARN_ON_FOLIO(!folio_test_hugetlb(folio), folio); diff --git a/mm/hugetlb.c b/mm/hugetlb.c index ef48ae6738909..57e8981879314 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -5408,7 +5408,7 @@ int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src, * sleep during the process. */ if (!folio_test_anon(pte_folio)) { - page_dup_file_rmap(&pte_folio->page, true); + hugetlb_add_file_rmap(pte_folio); } else if (page_try_dup_anon_rmap(&pte_folio->page, true, src_vma)) { pte_t src_pte_old = entry; @@ -6279,7 +6279,7 @@ static vm_fault_t hugetlb_no_page(struct mm_struct *mm, if (anon_rmap) hugetlb_add_new_anon_rmap(folio, vma, haddr); else - page_dup_file_rmap(&folio->page, true); + hugetlb_add_file_rmap(folio); new_pte = make_huge_pte(vma, &folio->page, ((vma->vm_flags & VM_WRITE) && (vma->vm_flags & VM_SHARED))); /* @@ -6730,7 +6730,7 @@ int hugetlb_mfill_atomic_pte(pte_t *dst_pte, goto out_release_unlock; if (folio_in_pagecache) - page_dup_file_rmap(&folio->page, true); + hugetlb_add_file_rmap(folio); else hugetlb_add_new_anon_rmap(folio, dst_vma, dst_addr); diff --git a/mm/migrate.c b/mm/migrate.c index 7d1c3f292d24d..0e912443a18c3 100644 --- a/mm/migrate.c +++ b/mm/migrate.c @@ -252,7 +252,7 @@ static bool remove_migration_pte(struct folio *folio, hugetlb_add_anon_rmap(folio, vma, pvmw.address, rmap_flags); else - page_dup_file_rmap(new, true); + hugetlb_add_file_rmap(folio); set_huge_pte_at(vma->vm_mm, pvmw.address, pvmw.pte, pte, psize); } else diff --git a/mm/rmap.c b/mm/rmap.c index 261e1af0d254f..a57ec926daf0c 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -1395,6 +1395,7 @@ void folio_add_file_rmap_range(struct folio *folio, struct page *page, unsigned int nr_pmdmapped = 0, first; int nr = 0; + VM_WARN_ON_FOLIO(folio_test_hugetlb(folio), folio); VM_WARN_ON_FOLIO(compound && !folio_test_pmd_mappable(folio), folio); /* Is page being mapped by PTE? Is this its first map to be added? */