Received: by 2002:a05:7412:a9a8:b0:f9:92ae:e617 with SMTP id o40csp23285rdh; Wed, 20 Dec 2023 14:45:45 -0800 (PST) X-Google-Smtp-Source: AGHT+IGvlBV+5FYPLcw/JiHqxA/JifNW8EFEkeF53Avoxf+o6TH93r/YyP1lXUy7cSr2PWEZd4mC X-Received: by 2002:ad4:5e87:0:b0:67f:3640:47ed with SMTP id jl7-20020ad45e87000000b0067f364047edmr8788885qvb.0.1703112344824; Wed, 20 Dec 2023 14:45:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1703112344; cv=none; d=google.com; s=arc-20160816; b=bevgOaBezj7q7EDYyqkpA9zbE40f6H62qX3c75ONcdL15+8yzMQUStLR0+8GUNJEI8 N8YPWG4/laUHKY9649WfNOEkpSfrY7seTYMOMV7gXV+D6Ty+C1Da7O626rULypYDYYUk dZzcg4PP1NrGbibH5EENSmMJh6rYC5H9HyiNoLLOhRJO2bfC5YLSiyfPIVH8midKTjNu dEajvQ4p5anV4IqX/CeAkn0iOZ/c1gdWzf2CwiqO8yl52CYIPzxfsf7cwQnqWRh60yz0 mJ1Fo8iVRoY/Hd9QVDt2N1Pb/jUpyMWALsqJd81DQuYFT7Rd1k7q0EO1iARc9/2oQ6ww Aqkw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=Zb5u0xx47K41JJXfyNoHDQEukBbwbPO9Q+a2q/uOjFE=; fh=UlFXU+hKOMcm6idRzNfe3Pu+B5L1f7aeir3C6i/NWQA=; b=g3a3v3jNs+AZC1t+J6dpbGTwHw51g/05iAVhXGb+Fbt25NBU6GzQ3M6MBzlkCV7nmn G/WMXbzX78Gs5i3YUMp8uDxNOUANVCLzrmQ6I9CSE43xDLLluiUwjyk4ozuBYwOyI4Rx ccn1C3nq4apIrcr0pW538rBNoNQr22AlbzNm1OjuByzMIdDCWUJbHJokhkD3KcgJuKBq GyLBe9QFD0HBGFH0ktuXItCYr9WmY0O9sbO4Nv+tnJ1Pz+WR4xBpeDjDT8vj25WzE/8A Czdd8B7WaNBqvLKzycSaBIz0Prg3T3PTE0+rb8oTb6pPzP/pq2djSgbrNW+cYea4zjZe WggQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=VurQXmUA; spf=pass (google.com: domain of linux-kernel+bounces-7527-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-7527-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id q3-20020a0cf5c3000000b0067f69df6712si715423qvm.488.2023.12.20.14.45.44 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Dec 2023 14:45:44 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-7527-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=VurQXmUA; spf=pass (google.com: domain of linux-kernel+bounces-7527-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-7527-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 92BE11C2262E for ; Wed, 20 Dec 2023 22:45:44 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8F04E4B5A6; Wed, 20 Dec 2023 22:45:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="VurQXmUA" X-Original-To: linux-kernel@vger.kernel.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3DF5D4B133 for ; Wed, 20 Dec 2023 22:45:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com 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=Zb5u0xx47K41JJXfyNoHDQEukBbwbPO9Q+a2q/uOjFE=; b=VurQXmUAASwMygg+NZJy+r8hpV029Fsj/be65Qc3/CfO0OVg/Tk0tZgDj3TMWpx/wN/fi6 fbZQmHd8fODnVrQXf9N8E9tCAMJufJsYGMpbVoJV+kpIZfc339fq9pdyipuByv0fisgGtd NNoTBL/cmDBm3isdq4UFes8TCqGvDzs= 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-1-JUWNMJ1_NEm_REskD0WbEQ-1; Wed, 20 Dec 2023 17:45:13 -0500 X-MC-Unique: JUWNMJ1_NEm_REskD0WbEQ-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 3710986EB20; Wed, 20 Dec 2023 22:45:13 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.192.101]) by smtp.corp.redhat.com (Postfix) with ESMTP id 273FD40C6EB9; Wed, 20 Dec 2023 22:45:10 +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 02/40] mm/rmap: introduce and use hugetlb_remove_rmap() Date: Wed, 20 Dec 2023 23:44:26 +0100 Message-ID: <20231220224504.646757-3-david@redhat.com> In-Reply-To: <20231220224504.646757-1-david@redhat.com> References: <20231220224504.646757-1-david@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.2 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. Let's introduce and use hugetlb_remove_rmap() and remove the hugetlb code from page_remove_rmap(). This effectively removes one check on the small-folio path as well. Add sanity checks that we end up with the right folios in the right functions. Note: all possible candidates that need care are page_remove_rmap() that pass compound=true. Reviewed-by: Yin Fengwei Reviewed-by: Ryan Roberts Reviewed-by: Matthew Wilcox (Oracle) Signed-off-by: David Hildenbrand --- include/linux/rmap.h | 7 +++++++ mm/hugetlb.c | 4 ++-- mm/rmap.c | 18 +++++++++--------- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/include/linux/rmap.h b/include/linux/rmap.h index 36096ba69bdcd..64ae6c4d72720 100644 --- a/include/linux/rmap.h +++ b/include/linux/rmap.h @@ -211,6 +211,13 @@ 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_remove_rmap(struct folio *folio) +{ + VM_WARN_ON_FOLIO(!folio_test_hugetlb(folio), folio); + + atomic_dec(&folio->_entire_mapcount); +} + static inline void __page_dup_rmap(struct page *page, bool compound) { if (compound) { diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 305f3ca1dee62..ef48ae6738909 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -5676,7 +5676,7 @@ void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma, make_pte_marker(PTE_MARKER_UFFD_WP), sz); hugetlb_count_sub(pages_per_huge_page(h), mm); - page_remove_rmap(page, vma, true); + hugetlb_remove_rmap(page_folio(page)); spin_unlock(ptl); tlb_remove_page_size(tlb, page, huge_page_size(h)); @@ -5987,7 +5987,7 @@ static vm_fault_t hugetlb_wp(struct mm_struct *mm, struct vm_area_struct *vma, /* Break COW or unshare */ huge_ptep_clear_flush(vma, haddr, ptep); - page_remove_rmap(&old_folio->page, vma, true); + hugetlb_remove_rmap(old_folio); hugetlb_add_new_anon_rmap(new_folio, vma, haddr); if (huge_pte_uffd_wp(pte)) newpte = huge_pte_mkuffd_wp(newpte); diff --git a/mm/rmap.c b/mm/rmap.c index 9845499b22f8f..261e1af0d254f 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -1480,15 +1480,9 @@ void page_remove_rmap(struct page *page, struct vm_area_struct *vma, bool last; enum node_stat_item idx; + VM_WARN_ON_FOLIO(folio_test_hugetlb(folio), folio); VM_BUG_ON_PAGE(compound && !PageHead(page), page); - /* Hugetlb pages are not counted in NR_*MAPPED */ - if (unlikely(folio_test_hugetlb(folio))) { - /* hugetlb pages are always mapped with pmds */ - atomic_dec(&folio->_entire_mapcount); - return; - } - /* Is page being unmapped by PTE? Is this its last map to be removed? */ if (likely(!compound)) { last = atomic_add_negative(-1, &page->_mapcount); @@ -1846,7 +1840,10 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, dec_mm_counter(mm, mm_counter_file(&folio->page)); } discard: - page_remove_rmap(subpage, vma, folio_test_hugetlb(folio)); + if (unlikely(folio_test_hugetlb(folio))) + hugetlb_remove_rmap(folio); + else + page_remove_rmap(subpage, vma, false); if (vma->vm_flags & VM_LOCKED) mlock_drain_local(); folio_put(folio); @@ -2199,7 +2196,10 @@ static bool try_to_migrate_one(struct folio *folio, struct vm_area_struct *vma, */ } - page_remove_rmap(subpage, vma, folio_test_hugetlb(folio)); + if (unlikely(folio_test_hugetlb(folio))) + hugetlb_remove_rmap(folio); + else + page_remove_rmap(subpage, vma, false); if (vma->vm_flags & VM_LOCKED) mlock_drain_local(); folio_put(folio); -- 2.43.0