From mboxrd@z Thu Jan 1 00:00:00 1970 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 40012C433DB for ; Fri, 5 Feb 2021 16:53:46 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id BE70264F06 for ; Fri, 5 Feb 2021 16:53:45 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org BE70264F06 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 DCFE26B0078; Fri, 5 Feb 2021 11:53:43 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D381B6B007B; Fri, 5 Feb 2021 11:53:43 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B62636B007D; Fri, 5 Feb 2021 11:53:43 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0252.hostedemail.com [216.40.44.252]) by kanga.kvack.org (Postfix) with ESMTP id 9617B6B0078 for ; Fri, 5 Feb 2021 11:53:43 -0500 (EST) Received: from smtpin09.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 54ED2180AD822 for ; Fri, 5 Feb 2021 16:53:43 +0000 (UTC) X-FDA: 77784810726.09.laugh02_2d0892a275e6 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin09.hostedemail.com (Postfix) with ESMTP id 3A25A180AD80F for ; Fri, 5 Feb 2021 16:53:43 +0000 (UTC) X-HE-Tag: laugh02_2d0892a275e6 X-Filterd-Recvd-Size: 8320 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [63.128.21.124]) by imf20.hostedemail.com (Postfix) with ESMTP for ; Fri, 5 Feb 2021 16:53:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1612544022; 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=NBcsouZewuvk8KZODy/tJjYpoCFexLf8th1hdmd2fdU=; b=YAZJwhm0fXlDQSOhj2imMPl9RHKpUzrFTCgvxFi9/UTuFrwWGMm8LMhKwTnwN0xP5phoA2 dzMmuLFU/H/WBj6h+2YJxM8ZkCinQqTeZBe1N1wT+YBEaxImsOZUrHf83D+HvmnGJP/yIo zw9R7hhrwrEFwmAq9RdxMI2VjTQjhe4= Received: from mail-qv1-f72.google.com (mail-qv1-f72.google.com [209.85.219.72]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-584-sab53n-CO0qxaxo-tAEOMg-1; Fri, 05 Feb 2021 11:53:40 -0500 X-MC-Unique: sab53n-CO0qxaxo-tAEOMg-1 Received: by mail-qv1-f72.google.com with SMTP id j4so5453931qvi.8 for ; Fri, 05 Feb 2021 08:53:40 -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=NBcsouZewuvk8KZODy/tJjYpoCFexLf8th1hdmd2fdU=; b=hjfhnoNgv+mY7wEXZs+6kL7adPb8tNjMRVLjFoaIEh8uPo6IrftTr5WFnNyPhvSA6B 2O90GE9iYXRB1OP7qBjVoa/Wy6fZ2awzwtck4z3wSfw2uTxZ276RjPZ/uCdXstHt6xN8 Zm3kLBlIEBZZDncdLMmkPUNs5y7PVvXYEur69qDHculZdpWIxIhKMolgPWLCv3weQZNU TpY0lxt9vWCPNMnWs17u7rk7c0fPGjWQxA4alroM5NBGXaz+BVQGs6zhIJ3zmNPDytWf /72tYm8V0xyq1eztLaxYYU3nsnpvnsv8M0zyQl135ySaIJZkN+vMwr5KNwFhI1ybXHI4 VJJA== X-Gm-Message-State: AOAM533VmO1VzYtk+pwu4j8o06x1D00I7TyPF9HM8UZrIeMWo+HVimY1 M43fvfsSkjQngMRe3kLb3yldSj4JbGqCN9gGf0az7TtYlC304++xjWFvNJlF1ZVksCR+oWQK5Pi W5+I1nap1FmfhV9WLaepGj1YhVg/PJtYxuK4S/czipbVO+OMuRqOpQpwO8w6i X-Received: by 2002:ac8:5c0a:: with SMTP id i10mr4900107qti.239.1612544019970; Fri, 05 Feb 2021 08:53:39 -0800 (PST) X-Google-Smtp-Source: ABdhPJzFpCiS7vlmmziN/8ahxv+juOeWp1Z1zPJrlPr5nMSaR9n55md55zNusgN1cZRy2ABzzKQMqw== X-Received: by 2002:ac8:5c0a:: with SMTP id i10mr4900071qti.239.1612544019658; Fri, 05 Feb 2021 08:53:39 -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 12sm9618893qkg.39.2021.02.05.08.53.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Feb 2021 08:53:39 -0800 (PST) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Gal Pressman , Andrea Arcangeli , Christoph Hellwig , Miaohe Lin , Kirill Shutemov , Jann Horn , Matthew Wilcox , Jan Kara , Jason Gunthorpe , Linus Torvalds , Mike Rapoport , David Gibson , Mike Kravetz , peterx@redhat.com, Kirill Tkhai , Wei Zhang , Andrew Morton Subject: [PATCH v3 5/5] hugetlb: Do early cow when page pinned on src mm Date: Fri, 5 Feb 2021 11:54:06 -0500 Message-Id: <20210205165406.4655-6-peterx@redhat.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210205165406.4655-1-peterx@redhat.com> References: <20210205165406.4655-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 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: quoted-printable 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'r= e pinned pages found. One can reference 70e806e4e645 ("mm: Do early cow fo= r pinned pages during fork() for ptes", 2020-09-27) for more information, s= ince we do similar things here rather than pte this time, but just for hugetlb= . 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 620700f05ff4..7c1a0ecc130e 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -3727,6 +3727,18 @@ static bool is_hugetlb_entry_hwpoisoned(pte_t pte) return false; } =20 +static void +hugetlb_install_page(struct vm_area_struct *vma, pte_t *ptep, unsigned l= ong 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) { @@ -3736,6 +3748,7 @@ int copy_hugetlb_page_range(struct mm_struct *dst, = struct mm_struct *src, int cow =3D is_cow_mapping(vma->vm_flags); struct hstate *h =3D hstate_vma(vma); unsigned long sz =3D huge_page_size(h); + unsigned long npages =3D pages_per_huge_page(h); struct address_space *mapping =3D vma->vm_file->f_mapping; struct mmu_notifier_range range; int ret =3D 0; @@ -3784,6 +3797,7 @@ int copy_hugetlb_page_range(struct mm_struct *dst, = struct mm_struct *src, spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING); entry =3D huge_ptep_get(src_pte); dst_entry =3D 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 @@ -3807,6 +3821,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 =3D huge_ptep_get(src_pte); + ptepage =3D 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 =3D entry; + struct page *new; + + spin_unlock(src_ptl); + spin_unlock(dst_ptl); + /* Do not use reserve as it's private owned */ + new =3D alloc_huge_page(vma, addr, 1); + if (IS_ERR(new)) { + put_page(ptepage); + ret =3D 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 =3D huge_pte_lock(h, dst, dst_pte); + src_ptl =3D huge_pte_lockptr(h, src, src_pte); + spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING); + entry =3D 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 @@ -3817,12 +3877,10 @@ int copy_hugetlb_page_range(struct mm_struct *dst= , struct mm_struct *src, */ huge_ptep_set_wrprotect(src, addr, src_pte); } - entry =3D huge_ptep_get(src_pte); - ptepage =3D 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); --=20 2.26.2