From patchwork Wed Feb 17 23:35:47 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 12092605 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id AC79BC433E6 for ; Wed, 17 Feb 2021 23:36:06 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 48209600EF for ; Wed, 17 Feb 2021 23:36:06 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 48209600EF Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 7F7608D0002; Wed, 17 Feb 2021 18:36:02 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 783538D0001; Wed, 17 Feb 2021 18:36:02 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5FFDA8D0002; Wed, 17 Feb 2021 18:36:02 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0108.hostedemail.com [216.40.44.108]) by kanga.kvack.org (Postfix) with ESMTP id 38F158D0001 for ; Wed, 17 Feb 2021 18:36:02 -0500 (EST) Received: from smtpin03.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 0265B180AD802 for ; Wed, 17 Feb 2021 23:36:02 +0000 (UTC) X-FDA: 77829370164.03.teeth99_0f0883227650 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin03.hostedemail.com (Postfix) with ESMTP id D2B6628A4EA for ; Wed, 17 Feb 2021 23:36:01 +0000 (UTC) X-HE-Tag: teeth99_0f0883227650 X-Filterd-Recvd-Size: 8733 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [63.128.21.124]) by imf28.hostedemail.com (Postfix) with ESMTP for ; Wed, 17 Feb 2021 23:36:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1613604960; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=WSh1ft3TkUZQw5lT26Ir4z6+mrJgP1tvshvl4KSiDTY=; b=T0K/bEwDMGJRGa+35SEQRfBSwvgdEuZbph4Dj6i/33kAwHrMUNOEFb6gDDP06yVQuSOcRG mHoMHCTurHt2yXe+95rSMMp88AHe98768EGoHEFRFtUzeRJQW02DrYvO7RX8IaZgLBqFAS lBMV+JS+k2rAYoQFhnI5847VuMtY9r8= Received: from mail-qv1-f70.google.com (mail-qv1-f70.google.com [209.85.219.70]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-446-zuvg3Jb9OyCHEmTE1xjz5Q-1; Wed, 17 Feb 2021 18:35:59 -0500 X-MC-Unique: zuvg3Jb9OyCHEmTE1xjz5Q-1 Received: by mail-qv1-f70.google.com with SMTP id u8so81099qvm.5 for ; Wed, 17 Feb 2021 15:35:59 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=WSh1ft3TkUZQw5lT26Ir4z6+mrJgP1tvshvl4KSiDTY=; b=WpzbqlPdv8JXaN2ie5Lz56cZTRtHZqY91qLcaNNx3d0Ou4zuyZC+Ihxo77rDbyaInG oQ7L2j7sl7HmmEEmrDvDtwhOeDOxxRzadoIxDQjGnlWIrL7mb5E1gmyOH9qJZjIPzr/k OTDSIXxtZcZAaxtL6ws02/1YDrWUVlka25syOPU2ljBAZr4Rn7N5fk13wpdDTxGFon8c c4icS6peBrGyU0MaLOsZUMr9VOpmrl1D4p7NWg3WfR8Vg5XqcGtInVF87LUELklq58lR VfLKSBYavpGTsgseHyTLNo6a/Bu+GR4r2KkzfeVJMUgkFDHw1jSCV22V8AOEArsqcSL6 ojzA== X-Gm-Message-State: AOAM5303mGACDh2TKdKYD/aGwusj57OzYiXnd+ay3/CSB8i/ZkVGX27r Z7Esn2BPUx7/M/f6Yc73m5Dp+fjcsqFhFfQV/F9miKCDrKtnVb7PFKNY6/DE6DYzY4Tld9sab4/ bHOPmwPlYRZj7ZGJYFbHzB6zT9jylF6VRSfqgL9fjk9XgRQYjkgD8Fyb+XMIo X-Received: by 2002:ac8:4c8e:: with SMTP id j14mr1827741qtv.92.1613604958281; Wed, 17 Feb 2021 15:35:58 -0800 (PST) X-Google-Smtp-Source: ABdhPJzZ/yEV04MLom7yxkWLFWTI5mn1lD6GWwzpCnVnvrYHJZXeaz6IYDZJzbezFjqMM5XCtZhhUA== X-Received: by 2002:ac8:4c8e:: with SMTP id j14mr1827704qtv.92.1613604957958; Wed, 17 Feb 2021 15:35:57 -0800 (PST) Received: from xz-x1.redhat.com (bras-vprn-toroon474qw-lp130-20-174-93-89-182.dsl.bell.ca. [174.93.89.182]) by smtp.gmail.com with ESMTPSA id o5sm2739622qkh.59.2021.02.17.15.35.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Feb 2021 15:35:57 -0800 (PST) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Gal Pressman , Matthew Wilcox , Wei Zhang , peterx@redhat.com, Mike Kravetz , Mike Rapoport , Christoph Hellwig , Andrew Morton , Linus Torvalds , David Gibson , Jason Gunthorpe , Jann Horn , Kirill Tkhai , Kirill Shutemov , Miaohe Lin , Andrea Arcangeli , Jan Kara Subject: [PATCH v5 5/5] hugetlb: Do early cow when page pinned on src mm Date: Wed, 17 Feb 2021 18:35:47 -0500 Message-Id: <20210217233547.93892-6-peterx@redhat.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210217233547.93892-1-peterx@redhat.com> References: <20210217233547.93892-1-peterx@redhat.com> MIME-Version: 1.0 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=peterx@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com 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: This is the last missing piece of the COW-during-fork effort when there're pinned pages found. One can reference 70e806e4e645 ("mm: Do early cow for pinned pages during fork() for ptes", 2020-09-27) for more information, since we do similar things here rather than pte this time, but just for hugetlb. Note that after Jason's recent work on 57efa1fe5957 ("mm/gup: prevent gup_fast from racing with COW during fork", 2020-12-15) which is safer and easier to understand, we're safe now within the whole copy_page_range() against gup-fast, we don't need the wr-protect trick that proposed in 70e806e4e645 anymore. Reviewed-by: Mike Kravetz Reviewed-by: Jason Gunthorpe Signed-off-by: Peter Xu --- mm/hugetlb.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 62 insertions(+), 4 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 8379224e1d43..0b45ff7df708 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -3725,6 +3725,18 @@ static bool is_hugetlb_entry_hwpoisoned(pte_t pte) return false; } +static void +hugetlb_install_page(struct vm_area_struct *vma, pte_t *ptep, unsigned long addr, + struct page *new_page) +{ + __SetPageUptodate(new_page); + set_huge_pte_at(vma->vm_mm, addr, ptep, make_huge_pte(vma, new_page, 1)); + hugepage_add_new_anon_rmap(new_page, vma, addr); + hugetlb_count_add(pages_per_huge_page(hstate_vma(vma)), vma->vm_mm); + ClearHPageRestoreReserve(new_page); + SetHPageMigratable(new_page); +} + int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src, struct vm_area_struct *vma) { @@ -3734,6 +3746,7 @@ int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src, bool cow = is_cow_mapping(vma->vm_flags); struct hstate *h = hstate_vma(vma); unsigned long sz = huge_page_size(h); + unsigned long npages = pages_per_huge_page(h); struct address_space *mapping = vma->vm_file->f_mapping; struct mmu_notifier_range range; int ret = 0; @@ -3782,6 +3795,7 @@ int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src, spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING); entry = huge_ptep_get(src_pte); dst_entry = huge_ptep_get(dst_pte); +again: if (huge_pte_none(entry) || !huge_pte_none(dst_entry)) { /* * Skip if src entry none. Also, skip in the @@ -3805,6 +3819,52 @@ int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src, } set_huge_swap_pte_at(dst, addr, dst_pte, entry, sz); } else { + entry = huge_ptep_get(src_pte); + ptepage = pte_page(entry); + get_page(ptepage); + + /* + * This is a rare case where we see pinned hugetlb + * pages while they're prone to COW. We need to do the + * COW earlier during fork. + * + * When pre-allocating the page or copying data, we + * need to be without the pgtable locks since we could + * sleep during the process. + */ + if (unlikely(page_needs_cow_for_dma(vma, ptepage))) { + pte_t src_pte_old = entry; + struct page *new; + + spin_unlock(src_ptl); + spin_unlock(dst_ptl); + /* Do not use reserve as it's private owned */ + new = alloc_huge_page(vma, addr, 1); + if (IS_ERR(new)) { + put_page(ptepage); + ret = PTR_ERR(new); + break; + } + copy_user_huge_page(new, ptepage, addr, vma, + npages); + put_page(ptepage); + + /* Install the new huge page if src pte stable */ + dst_ptl = huge_pte_lock(h, dst, dst_pte); + src_ptl = huge_pte_lockptr(h, src, src_pte); + spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING); + entry = huge_ptep_get(src_pte); + if (!pte_same(src_pte_old, entry)) { + put_page(new); + /* dst_entry won't change as in child */ + goto again; + } + hugetlb_install_page(vma, dst_pte, addr, new); + spin_unlock(src_ptl); + spin_unlock(dst_ptl); + continue; + } + if (cow) { /* * No need to notify as we are downgrading page @@ -3815,12 +3875,10 @@ int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src, */ huge_ptep_set_wrprotect(src, addr, src_pte); } - entry = huge_ptep_get(src_pte); - ptepage = pte_page(entry); - get_page(ptepage); + page_dup_rmap(ptepage, true); set_huge_pte_at(dst, addr, dst_pte, entry); - hugetlb_count_add(pages_per_huge_page(h), dst); + hugetlb_count_add(npages, dst); } spin_unlock(src_ptl); spin_unlock(dst_ptl);