From patchwork Tue Nov 28 14:52:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13471280 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 7214AC07CA9 for ; Tue, 28 Nov 2023 14:52:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E4B3A6B0307; Tue, 28 Nov 2023 09:52:21 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id DD33F6B030D; Tue, 28 Nov 2023 09:52:21 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B14DB6B030F; Tue, 28 Nov 2023 09:52:21 -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 84AEE6B0307 for ; Tue, 28 Nov 2023 09:52:21 -0500 (EST) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 52A6A40265 for ; Tue, 28 Nov 2023 14:52:21 +0000 (UTC) X-FDA: 81507653682.28.F25242A Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf29.hostedemail.com (Postfix) with ESMTP id 6CF0F120008 for ; Tue, 28 Nov 2023 14:52:19 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Lajk4O61; spf=pass (imf29.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=1701183139; 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=OTmuEGR3gJmZpzV6zcBNIBMMhBTdNFHIsA/oNnAKXzk=; b=ExzLCOB4nuhnFQ55Pr/s8qiXsS/meJJ+cW0jUVZU+NW+WVWZGEKsOl3kg+Lcq/9I6vA+Ao N+nuupe6mLf+I5C2+10tBwz8XOrVb2LejrH72wLZ2nZR+7N9e5Br6Gy9P4qJeNOKMIc6Uq 3R9lIYseBiJWD2EDX+nmB4Ofy4IGMMA= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1701183139; a=rsa-sha256; cv=none; b=ZgBe4V8U/cb767oHFmaHfb3AsITG4n8yL7CuJGhJ6DJ039rbsKVadNmv3HAwmhEoNW+LTX DRNOhQB9zTnVJTnBGwe1v+Ww1EISzZTGNJjjM6CQM8sxh8+q/gNbf9koNKGPOUeyFY/EUB DEjXbbwHJVtZ1nKRykkinS5aAMiSPiI= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Lajk4O61; spf=pass (imf29.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=1701183138; 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=OTmuEGR3gJmZpzV6zcBNIBMMhBTdNFHIsA/oNnAKXzk=; b=Lajk4O61sCSZ46Eo29/44tmEoAt02sWZYeveNXPg5vfLdv3xKlkd5iVPdYadeteDBT4eMW xVBHxBsgZwFOp4jusnARHDNeNPsDKLj+zrzCJLkdgHh/HCW0nwXrStRoIa5PHj+6HMgmzY dT3mFtjuKc1NE66qp7eGOe/F926OqlQ= 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-111-vnhnDkNXPWC8poKFInuLGw-1; Tue, 28 Nov 2023 09:52:12 -0500 X-MC-Unique: vnhnDkNXPWC8poKFInuLGw-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 DDC0C2825E9F; Tue, 28 Nov 2023 14:52:11 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.193.189]) by smtp.corp.redhat.com (Postfix) with ESMTP id D8D0D5028; Tue, 28 Nov 2023 14:52:10 +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 4/5] mm/rmap: introduce and use hugetlb_try_dup_anon_rmap() Date: Tue, 28 Nov 2023 15:52:04 +0100 Message-ID: <20231128145205.215026-5-david@redhat.com> In-Reply-To: <20231128145205.215026-1-david@redhat.com> References: <20231128145205.215026-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.5 X-Rspamd-Queue-Id: 6CF0F120008 X-Rspam-User: X-Stat-Signature: qytjip7tt6rfmdd7zu3resjgayddhsji X-Rspamd-Server: rspam03 X-HE-Tag: 1701183139-984605 X-HE-Meta: U2FsdGVkX19pq9etjqe5r7+KNNQlivxqPt8R/EvsZ2f2B7Jeq8scpKhAsZAPINnIMc6rel+N9t044ZSGmIw0FHZsn2LJgwRMnJUyyb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myxuP1G3WtS0oa4faaEx+b5f8GwLO9+DkqZupNw== 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, and we already have dedicated functions for adding anon hugetlb folios and removing hugetlb folios. So let's introduce and use hugetlb_try_dup_anon_rmap() to make all hugetlb handling use dedicated hugetlb_* rmap functions. While this is a cleanup, this will also make it easier to change rmap handling for partially-mappable folios. Note that is_device_private_page() does not apply to hugetlb. Signed-off-by: David Hildenbrand --- include/linux/mm.h | 12 +++++++++--- include/linux/rmap.h | 15 +++++++++++++++ mm/hugetlb.c | 3 +-- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index 418d26608ece..24c1c7c5a99c 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -1953,15 +1953,21 @@ static inline bool page_maybe_dma_pinned(struct page *page) * * The caller has to hold the PT lock and the vma->vm_mm->->write_protect_seq. */ -static inline bool page_needs_cow_for_dma(struct vm_area_struct *vma, - struct page *page) +static inline bool folio_needs_cow_for_dma(struct vm_area_struct *vma, + struct folio *folio) { VM_BUG_ON(!(raw_read_seqcount(&vma->vm_mm->write_protect_seq) & 1)); if (!test_bit(MMF_HAS_PINNED, &vma->vm_mm->flags)) return false; - return page_maybe_dma_pinned(page); + return folio_maybe_dma_pinned(folio); +} + +static inline bool page_needs_cow_for_dma(struct vm_area_struct *vma, + struct page *page) +{ + return folio_needs_cow_for_dma(vma, page_folio(page)); } /** diff --git a/include/linux/rmap.h b/include/linux/rmap.h index 0a81e8420a96..8068c332e2ce 100644 --- a/include/linux/rmap.h +++ b/include/linux/rmap.h @@ -208,6 +208,21 @@ 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); +/* See page_try_dup_anon_rmap() */ +static inline int hugetlb_try_dup_anon_rmap(struct folio *folio, + struct vm_area_struct *vma) +{ + VM_WARN_ON_FOLIO(!folio_test_anon(folio), folio); + + if (PageAnonExclusive(&folio->page)) { + if (unlikely(folio_needs_cow_for_dma(vma, folio))) + return -EBUSY; + ClearPageAnonExclusive(&folio->page); + } + atomic_inc(&folio->_entire_mapcount); + 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/hugetlb.c b/mm/hugetlb.c index 541a8f38cfdc..d927f8b2893c 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -5402,8 +5402,7 @@ int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src, */ if (!folio_test_anon(pte_folio)) { hugetlb_add_file_rmap(pte_folio); - } else if (page_try_dup_anon_rmap(&pte_folio->page, - true, src_vma)) { + } else if (hugetlb_try_dup_anon_rmap(pte_folio, src_vma)) { pte_t src_pte_old = entry; struct folio *new_folio;