Received: by 2002:a05:7412:a9a8:b0:f9:92ae:e617 with SMTP id o40csp23867rdh; Wed, 20 Dec 2023 14:47:21 -0800 (PST) X-Google-Smtp-Source: AGHT+IFjhgoPtMjhHImqSiKL6cxG5UGm+MGlOw4q+5l7BqpzAJCOfyKaP/hjUzi//NRUiPiZYdNY X-Received: by 2002:a05:6a20:748b:b0:194:e518:14af with SMTP id p11-20020a056a20748b00b00194e51814afmr454247pzd.60.1703112441027; Wed, 20 Dec 2023 14:47:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1703112441; cv=none; d=google.com; s=arc-20160816; b=sEY7WB4e864OgmU4wIBn0aWInXl5tfpMq7Is+OD3MWPLnNIh3pK9ASCHiIj1GOw16/ Huc9TkQCao4b3vILvCMgBujSVJV7Z9v7FxVSlOIppaFVabkxMtZNBZ48WQ6FDjo+WYwv IG++gXaVKtlVdkns2VoQm3KyiqZXYXWQ8XyZR6uTjN/QkUdACI16piuToF89nQztYS04 xI3FisSTjrTaemnzCEPfukUgcAoxRcOZZbgfsF2+pgg5S+R1lOgDoc2TiSm1iyBYkkJa oOzpcR4i81hiMtZKp4p0tnKy9b4S/CXiI5K12VePa0IPwiJzNcW87az3LSd+EdQ4qZGZ +d3w== 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=mTgg+xMXQdacmBIURVO4NXuqVVzQK4rkW7J9Pz1Od1g=; fh=UlFXU+hKOMcm6idRzNfe3Pu+B5L1f7aeir3C6i/NWQA=; b=wCPxR01Ta62MM0qidy2b1SAe4hYkQRHyj6PWtkEunGWXBnNi6iAOy1yXysg6hwVHPP jJrml2EXlmFucEQLRArp0Tnz3Xk5oM+hAK62kN5JTsAJwt+l5f5oRX09+NC1ks17Qr6e qjDBZ9bOG7h2O6jsRIMohpOjb4S7RLfqHN7b5pM+GXPV+/piepQ7MOKw3GZNBfLlnpQZ J1X0oWUaoANfSOW0KRjo0YCDHGxsSSCQmi9LBYK0uc2L/qGRHuA3I1uyLiDOBp//+TUx aBehTWDhqQSIpA43zrhrDdMCP9uxDvvwQadx18/w7uTcfSXyI9wATv5zLnSXqzLDBW29 VQLw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=OMguaNlZ; spf=pass (google.com: domain of linux-kernel+bounces-7530-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-7530-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id t5-20020a170902b20500b001d3a6ebf739si347773plr.570.2023.12.20.14.47.20 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Dec 2023 14:47:21 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-7530-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=OMguaNlZ; spf=pass (google.com: domain of linux-kernel+bounces-7530-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-7530-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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 3849DB22C16 for ; Wed, 20 Dec 2023 22:46:33 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 611C64C3CC; Wed, 20 Dec 2023 22:45:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="OMguaNlZ" 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 2AF644C3B6 for ; Wed, 20 Dec 2023 22:45:25 +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=1703112324; 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=mTgg+xMXQdacmBIURVO4NXuqVVzQK4rkW7J9Pz1Od1g=; b=OMguaNlZPxU0XYBLdbuBPw1r7WH+hbxY08SPeSPIDoFz2NQ+UBsrQOIUWQ+hcKf37aZrVp T3LH8urMgbir08uwJz7ZNrVwVCW6fJwQQjyhWSyLoVb2cnxDFWfwDo+Cxuze+TKYImrULD QS46K8v99PPdNlps6XqXBn0j3P3I2ag= 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-131-WcUjTUD5MXKhfmT8SK5HuA-1; Wed, 20 Dec 2023 17:45:21 -0500 X-MC-Unique: WcUjTUD5MXKhfmT8SK5HuA-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 793E986EB22; Wed, 20 Dec 2023 22:45:20 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.192.101]) by smtp.corp.redhat.com (Postfix) with ESMTP id 1E03C40C6EB9; Wed, 20 Dec 2023 22:45:17 +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 05/40] mm/rmap: introduce and use hugetlb_try_share_anon_rmap() Date: Wed, 20 Dec 2023 23:44:29 +0100 Message-ID: <20231220224504.646757-6-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. So let's introduce and use hugetlb_try_dup_anon_rmap() to make all hugetlb handling use dedicated hugetlb_* rmap functions. Add sanity checks that we end up with the right folios in the right 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 Reviewed-by: Ryan Roberts Signed-off-by: David Hildenbrand --- include/linux/rmap.h | 25 +++++++++++++++++++++++++ mm/rmap.c | 15 ++++++++++----- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/include/linux/rmap.h b/include/linux/rmap.h index 5f26752de945c..d6fefa0f04105 100644 --- a/include/linux/rmap.h +++ b/include/linux/rmap.h @@ -227,6 +227,30 @@ 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_hugetlb(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_hugetlb(folio), folio); @@ -331,6 +355,7 @@ static inline int page_try_dup_anon_rmap(struct page *page, bool compound, */ static inline int page_try_share_anon_rmap(struct page *page) { + VM_WARN_ON(folio_test_hugetlb(page_folio(page))); VM_BUG_ON_PAGE(!PageAnon(page) || !PageAnonExclusive(page), page); /* device private pages cannot get pinned via GUP. */ diff --git a/mm/rmap.c b/mm/rmap.c index a57ec926daf0c..c229e48cf5a9e 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -2149,13 +2149,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; -- 2.43.0