Received: by 2002:a05:7412:419a:b0:f3:1519:9f41 with SMTP id i26csp3900705rdh; Tue, 28 Nov 2023 06:53:18 -0800 (PST) X-Google-Smtp-Source: AGHT+IHIqZ5sBH/2pzDF1lm0IzkW5867fS/xPeNN4jKTEBHnhVmWcGSNHkhPv9gwZN/JVMOmINii X-Received: by 2002:a05:6a20:7f98:b0:189:baaa:23e7 with SMTP id d24-20020a056a207f9800b00189baaa23e7mr22153568pzj.40.1701183198685; Tue, 28 Nov 2023 06:53:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701183198; cv=none; d=google.com; s=arc-20160816; b=peUvq1SEBtZVYIz0zOJc4vhnzJFvybeMe76xxtvS4Zv6mUksqZubnsbCe0mTQFUb2b EYTvNrGIzsaIwrbH8dLFWP7kVRXZQzbc+kcGZwXMqdMIZy6A+1FmYB7QZ8EqLluQFeUp xst+ZQA3+309WI9tAJbkgQwI6A2BAgC74vjBkRufRSVmYETGdyNO20MOQNDwHxJb8ZnZ 7xv8UdY+fpYwH+dScyzg9pJN/SocneK+QWhv0YZL8K8OW44xz2nhLg/szvRxWphR6KoR /SAT0hEReQGo1lY7hXtnvWuHJydxrpDZZJLhqNCR8ssvIUtBd4gUkvcOZPss+rRqwEO9 KI9Q== 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=PmPjXh3Vh8+yuItVsdShx0BHlebvdn/GvnE6+gvw2TQ=; fh=iXyIHEqHOYLHaHkg4AmQBb0b8vPARyWexIbS110BxH8=; b=kZNLspQi3UovQQq1cXP/M69EEhpqNrjYc96szjTJia2i+neH+HiQ3w59ZkQPS4meHM qS2WecAf5Ws7raJvnHli7YUiZKmcnog7v3qYibqqwz6jE4gL1RWq5ZUMGnp5LP8+4o2D lGYU1+hhUVRDnUor/WwZK+J2JSfQKktsgvO8vvJsc+IBK0XqZ8DBABIqfiFBdtsobtdl dWsCmiP9CjDIBnkmSF97QXA9mOzB3+Vv54Xq3VsgLBdGr8gA8CvHDEiTXOfqSb8g4g6l XWIqF/WUL2jG2Oqu24c4ZU4P91JqMw1JbZAXZHU3gugazV+xciuPqqf9AN3RwnV+O00O 5NKw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=KLu+zqHf; 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 Return-Path: Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id b30-20020a631b5e000000b005c278ba0fe2si12632731pgm.175.2023.11.28.06.53.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Nov 2023 06:53:18 -0800 (PST) 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=KLu+zqHf; 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 DB762804707C; Tue, 28 Nov 2023 06:52:58 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346402AbjK1Ow1 (ORCPT + 99 others); Tue, 28 Nov 2023 09:52:27 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35522 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346389AbjK1OwP (ORCPT ); Tue, 28 Nov 2023 09:52:15 -0500 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 685C510E4 for ; Tue, 28 Nov 2023 06:52:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1701183140; 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=PmPjXh3Vh8+yuItVsdShx0BHlebvdn/GvnE6+gvw2TQ=; b=KLu+zqHf5Q+HGbZEwSFGpWi/mHTELEO2zQxjZOJVyiO9zIfmrX3UyG90H8NUAFQeL90Rrr TY4tn7wtrN+yjT2S4be4U3P2M/IjtHjhLuBgEGjGorLYATklRH3BqF+iEtL945a2YFK3kl EvsAlhMm0mb/+/hsHB4KT/iIKLQwJ9I= 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-623-3AskqEHWOzuit0KMJT14zw-1; Tue, 28 Nov 2023 09:52:12 -0500 X-MC-Unique: 3AskqEHWOzuit0KMJT14zw-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (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 665EB101A52D; Tue, 28 Nov 2023 14:52:09 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.193.189]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6B2E35028; Tue, 28 Nov 2023 14:52:08 +0000 (UTC) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, David Hildenbrand , Andrew Morton , Mike Kravetz , Muchun Song Subject: [PATCH v1 2/5] mm/rmap: introduce and use hugetlb_remove_rmap() Date: Tue, 28 Nov 2023 15:52:02 +0100 Message-ID: <20231128145205.215026-3-david@redhat.com> In-Reply-To: <20231128145205.215026-1-david@redhat.com> References: <20231128145205.215026-1-david@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.5 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_BLOCKED,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE 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 (snail.vger.email [0.0.0.0]); Tue, 28 Nov 2023 06:52:59 -0800 (PST) hugetlb rmap handling differs quite a lot from "ordinary" rmap code. We don't want this hugetlb special-casing in the rmap functions, as we're special casing the callers already. Let's simply use a separate function for hugetlb. 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. While this is a cleanup, this will also make it easier to change rmap handling for partially-mappable folios. Note: all possible candidates that need care are page_remove_rmap() that pass compound=true. Signed-off-by: David Hildenbrand --- include/linux/rmap.h | 5 +++++ mm/hugetlb.c | 4 ++-- mm/rmap.c | 17 ++++++++--------- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/include/linux/rmap.h b/include/linux/rmap.h index 4c5bfeb05463..e8d1dc1d5361 100644 --- a/include/linux/rmap.h +++ b/include/linux/rmap.h @@ -208,6 +208,11 @@ 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) +{ + 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 4cfa0679661e..d17bb53b19ff 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -5669,7 +5669,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)); @@ -5980,7 +5980,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 112467c30b2c..5037581b79ec 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -1440,13 +1440,6 @@ void page_remove_rmap(struct page *page, struct vm_area_struct *vma, 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); @@ -1804,7 +1797,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); @@ -2157,7 +2153,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.41.0