From patchwork Fri Mar 4 05:16:55 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 12768465 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 EDEAAC433EF for ; Fri, 4 Mar 2022 05:18:47 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8BB658D0003; Fri, 4 Mar 2022 00:18:47 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 86A748D0001; Fri, 4 Mar 2022 00:18:47 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 732308D0003; Fri, 4 Mar 2022 00:18:47 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (relay.hostedemail.com [64.99.140.25]) by kanga.kvack.org (Postfix) with ESMTP id 658BF8D0001 for ; Fri, 4 Mar 2022 00:18:47 -0500 (EST) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay13.hostedemail.com (Postfix) with ESMTP id 440A5608E3 for ; Fri, 4 Mar 2022 05:18:47 +0000 (UTC) X-FDA: 79205549094.01.11FF03C Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf07.hostedemail.com (Postfix) with ESMTP id B20FC40008 for ; Fri, 4 Mar 2022 05:18:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1646371126; 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=87YY6m3X7TCIFcgpIMmGMt9yOOTi6qvz7lf8TeZlPS8=; b=KFpkXEgAn+yhHFJB5z2XI+Ki305phj/nfRIY/IADo/7Hvs6eIh44s8LjZlIJjyIxzZglqj RIAQ5H1WPncoj8QqI33sxP82NzSVrLc2LZXJCoHnzqIGX4GW9ZDejdiiyx+e3Qh8GdO+LK tHoWw94dJjpibvy0q/sykb4i0ghgZB8= Received: from mail-pg1-f200.google.com (mail-pg1-f200.google.com [209.85.215.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-437-KD4WbscKMbys_fYVecJKGw-1; Fri, 04 Mar 2022 00:18:44 -0500 X-MC-Unique: KD4WbscKMbys_fYVecJKGw-1 Received: by mail-pg1-f200.google.com with SMTP id n8-20020a654508000000b003783b1e9834so3941536pgq.0 for ; Thu, 03 Mar 2022 21:18:44 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=87YY6m3X7TCIFcgpIMmGMt9yOOTi6qvz7lf8TeZlPS8=; b=JjS8M9dByAvjJJpyNJhgip1mMqm2bYhhtIJdydXwCpVc3BkyefIWZeEBMFlmh1axKJ UM93dWCZM+LQtjkc6b6P2n+0bJJ6oyuA9/WxtTwiuJ8p4QPhGhUWMgzELS6HwYrYwila HPY+8ZXohNXjFurQgDa/UD2eFFFsWJxZCSXDL/3FHAEl2t5OL5BxwQG7aXJQXKhvx+2j Ddo3Gvl4Cg/ivZGKl5UIV1zV8mt91dMEihC0RUrVtT64Ubqi5Kpxes1mO/VhZ3zy/igL Oode/6U8w6lwPC0/A4X61STjh5c8IES1xXF1B7S/lkvLmYnVHiVUIfLqTF6tKJ7Er0lN hr0w== X-Gm-Message-State: AOAM530+cgRvmnoBM9jnwkeNAGCyu40yVh5v482fz3/XCsENaEXLokkK 34nFY/KN2tAwbtPyD8mvHzl21QVQ8HCHJWqjgJEr8QXAa3mmgCyoznI+eltEsv7uYv1U+Qn879c HK7UKOolQY2skHpGMQW51wQIthvv1cjjiIWz+RUHoJNT7yD7S57BMLO7XIrqh X-Received: by 2002:a63:5525:0:b0:372:c376:74f1 with SMTP id j37-20020a635525000000b00372c37674f1mr32598175pgb.433.1646371123487; Thu, 03 Mar 2022 21:18:43 -0800 (PST) X-Google-Smtp-Source: ABdhPJxvoeKLrN4o7hnThDJS0UIUxVik/k+WtOZ+b4Gnnk3c8Fk0c2piAJVffr6/tNq+9lzTQITaSA== X-Received: by 2002:a63:5525:0:b0:372:c376:74f1 with SMTP id j37-20020a635525000000b00372c37674f1mr32598143pgb.433.1646371123117; Thu, 03 Mar 2022 21:18:43 -0800 (PST) Received: from localhost.localdomain ([94.177.118.59]) by smtp.gmail.com with ESMTPSA id p16-20020a056a000b5000b004f669806cd9sm4323865pfo.87.2022.03.03.21.18.35 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Thu, 03 Mar 2022 21:18:42 -0800 (PST) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: peterx@redhat.com, Nadav Amit , Hugh Dickins , David Hildenbrand , Axel Rasmussen , Matthew Wilcox , Alistair Popple , Mike Rapoport , Andrew Morton , Jerome Glisse , Mike Kravetz , "Kirill A . Shutemov" , Andrea Arcangeli Subject: [PATCH v7 10/23] mm/shmem: Handle uffd-wp during fork() Date: Fri, 4 Mar 2022 13:16:55 +0800 Message-Id: <20220304051708.86193-11-peterx@redhat.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220304051708.86193-1-peterx@redhat.com> References: <20220304051708.86193-1-peterx@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Rspamd-Server: rspam10 X-Rspam-User: X-Stat-Signature: mf1763smk36bgiypdawwmhwzt5xu4k46 Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=KFpkXEgA; spf=none (imf07.hostedemail.com: domain of peterx@redhat.com has no SPF policy when checking 170.10.129.124) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-Rspamd-Queue-Id: B20FC40008 X-HE-Tag: 1646371126-432023 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: Normally we skip copy page when fork() for VM_SHARED shmem, but we can't skip it anymore if uffd-wp is enabled on dst vma. This should only happen when the src uffd has UFFD_FEATURE_EVENT_FORK enabled on uffd-wp shmem vma, so that VM_UFFD_WP will be propagated onto dst vma too, then we should copy the pgtables with uffd-wp bit and pte markers, because these information will be lost otherwise. Since the condition checks will become even more complicated for deciding "whether a vma needs to copy the pgtable during fork()", introduce a helper vma_needs_copy() for it, so everything will be clearer. Signed-off-by: Peter Xu --- mm/memory.c | 49 +++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 41 insertions(+), 8 deletions(-) diff --git a/mm/memory.c b/mm/memory.c index e3e67e32eb8a..e9e335ecb5dc 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -857,6 +857,14 @@ copy_nonpresent_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm, if (try_restore_exclusive_pte(src_pte, src_vma, addr)) return -EBUSY; return -ENOENT; + } else if (is_pte_marker_entry(entry)) { + /* + * We're copying the pgtable should only because dst_vma has + * uffd-wp enabled, do sanity check. + */ + WARN_ON_ONCE(!userfaultfd_wp(dst_vma)); + set_pte_at(dst_mm, addr, dst_pte, pte); + return 0; } if (!userfaultfd_wp(dst_vma)) pte = pte_swp_clear_uffd_wp(pte); @@ -1225,6 +1233,38 @@ copy_p4d_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma, return 0; } +/* + * Return true if the vma needs to copy the pgtable during this fork(). Return + * false when we can speed up fork() by allowing lazy page faults later until + * when the child accesses the memory range. + */ +bool +vma_needs_copy(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma) +{ + /* + * Always copy pgtables when dst_vma has uffd-wp enabled even if it's + * file-backed (e.g. shmem). Because when uffd-wp is enabled, pgtable + * contains uffd-wp protection information, that's something we can't + * retrieve from page cache, and skip copying will lose those info. + */ + if (userfaultfd_wp(dst_vma)) + return true; + + if (src_vma->vm_flags & (VM_HUGETLB | VM_PFNMAP | VM_MIXEDMAP)) + return true; + + if (src_vma->anon_vma) + return true; + + /* + * Don't copy ptes where a page fault will fill them correctly. Fork + * becomes much lighter when there are big shared or private readonly + * mappings. The tradeoff is that copy_page_range is more efficient + * than faulting. + */ + return false; +} + int copy_page_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma) { @@ -1238,14 +1278,7 @@ copy_page_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma) bool is_cow; int ret; - /* - * Don't copy ptes where a page fault will fill them correctly. - * Fork becomes much lighter when there are big shared or private - * readonly mappings. The tradeoff is that copy_page_range is more - * efficient than faulting. - */ - if (!(src_vma->vm_flags & (VM_HUGETLB | VM_PFNMAP | VM_MIXEDMAP)) && - !src_vma->anon_vma) + if (!vma_needs_copy(dst_vma, src_vma)) return 0; if (is_vm_hugetlb_page(src_vma))