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.0 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS 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 A5F53C433B4 for ; Wed, 28 Apr 2021 00:07:40 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 2A1E0613BF for ; Wed, 28 Apr 2021 00:07:40 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 2A1E0613BF 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 97F0C6B0036; Tue, 27 Apr 2021 20:07:39 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 956C66B0070; Tue, 27 Apr 2021 20:07:39 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7F6C36B0071; Tue, 27 Apr 2021 20:07:39 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0140.hostedemail.com [216.40.44.140]) by kanga.kvack.org (Postfix) with ESMTP id 6266A6B0036 for ; Tue, 27 Apr 2021 20:07:39 -0400 (EDT) Received: from smtpin08.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 18099364C for ; Wed, 28 Apr 2021 00:07:39 +0000 (UTC) X-FDA: 78079837038.08.A5C2B7B Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by imf14.hostedemail.com (Postfix) with ESMTP id 402A3C0001FA for ; Wed, 28 Apr 2021 00:07:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1619568458; 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: in-reply-to:in-reply-to:references:references; bh=4HPRjF6IefTTXi7+TlIzyDGOJbUTZ8HmAi1R2WG08BM=; b=Z/yVow2uedZ4kcXw3WiuP8QAvlmCxIAr4j7cuXwhkoz7wuawC7L8YW7L//FRE25MoT0JHK MOCEZIrdQrZQ4Caop8TQqSext4H2M5LmngUCdKOl+yNT4I5t6ViDBsemKjJMBDZDpjenPR u734/OGAjumZTKDbbHLtNugruegIB44= Received: from mail-qt1-f197.google.com (mail-qt1-f197.google.com [209.85.160.197]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-205-d5rCHpWBNmCQd7jpPkjYRA-1; Tue, 27 Apr 2021 20:07:36 -0400 X-MC-Unique: d5rCHpWBNmCQd7jpPkjYRA-1 Received: by mail-qt1-f197.google.com with SMTP id r20-20020ac85c940000b02901bac34fa2eeso2428027qta.11 for ; Tue, 27 Apr 2021 17:07:36 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=4HPRjF6IefTTXi7+TlIzyDGOJbUTZ8HmAi1R2WG08BM=; b=jOvNyJH+pcnbN1EKjx7x5mpl76KUI8QxgWu6+PyJxhIuLx7ci/vEMswU0XPcRC2COI g582G5VKjQui2lR/ckDfEbVxMJtbHOm5B3apvUfX061mpX0/MoDeyCqElTcxdVBLqujd qogClEvEzxsIu138El5D5tnf2qtS1iA7lxMZXlF/6nmueCbGCVedwoPD5NdSSlBNHulQ Fg02MHM+HXt102piNwexsu/7f7zjQYVtJ+AOqTuqB2nwD45M98H0Wr18/RKPc6P1k4nS QDz6UytYlfYNMm0GMUOfl1Q9AMEj5uPwMOqhBUkHv/bsE47AVYSKVTwJgW1BrBXqhMAW qc4Q== X-Gm-Message-State: AOAM532trmaAIORGsedxwBjMoLm5npTaG8hIhgIULggc0Miexmbdk5lb LQk7GuQ/E1jvlv6wB+93j3O4NCahbS01WzDWvVIe8R8lM76DI/b7hU2SLlcDXQWyom7XZIlFrX1 LxTSHyJyaSNWq1D/H3sEgzTCVxF9SLsHuTCDRHB9WpQo9pGRstu4pWxXfbvM1 X-Received: by 2002:a37:e10b:: with SMTP id c11mr25020933qkm.152.1619568455606; Tue, 27 Apr 2021 17:07:35 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxrU7unTybMnPl+jzN6o83Adzyp5LycZ9z6cmzVkA6kB5d8ANGZMGkCqfiCqW7EQQQzXgxILw== X-Received: by 2002:a37:e10b:: with SMTP id c11mr25020899qkm.152.1619568455237; Tue, 27 Apr 2021 17:07:35 -0700 (PDT) Received: from xz-x1 (bras-base-toroon474qw-grc-77-184-145-104-227.dsl.bell.ca. [184.145.104.227]) by smtp.gmail.com with ESMTPSA id d3sm1052773qtm.56.2021.04.27.17.07.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Apr 2021 17:07:34 -0700 (PDT) Date: Tue, 27 Apr 2021 20:07:32 -0400 From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Nadav Amit , Miaohe Lin , Mike Rapoport , Andrea Arcangeli , Hugh Dickins , Jerome Glisse , Mike Kravetz , Jason Gunthorpe , Matthew Wilcox , Andrew Morton , Axel Rasmussen , "Kirill A . Shutemov" Subject: Re: [PATCH v2 16/24] hugetlb/userfaultfd: Hook page faults for uffd write protection Message-ID: <20210428000732.GA6584@xz-x1> References: <20210427161317.50682-1-peterx@redhat.com> <20210427161317.50682-17-peterx@redhat.com> MIME-Version: 1.0 In-Reply-To: <20210427161317.50682-17-peterx@redhat.com> 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=utf-8 Content-Disposition: inline X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: 402A3C0001FA X-Stat-Signature: 1nk8c6x4ek53t4yf4j1csdurppyiuiis Received-SPF: none (redhat.com>: No applicable sender policy available) receiver=imf14; identity=mailfrom; envelope-from=""; helo=us-smtp-delivery-124.mimecast.com; client-ip=216.205.24.124 X-HE-DKIM-Result: pass/pass X-HE-Tag: 1619568443-101243 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: On Tue, Apr 27, 2021 at 12:13:09PM -0400, Peter Xu wrote: > Hook up hugetlbfs_fault() with the capability to handle userfaultfd-wp faults. > > We do this slightly earlier than hugetlb_cow() so that we can avoid taking some > extra locks that we definitely don't need. > > Reviewed-by: Mike Kravetz > Signed-off-by: Peter Xu > --- > mm/hugetlb.c | 19 +++++++++++++++++++ > 1 file changed, 19 insertions(+) > > diff --git a/mm/hugetlb.c b/mm/hugetlb.c > index 629aa4c2259c8..8e234ee9a15e2 100644 > --- a/mm/hugetlb.c > +++ b/mm/hugetlb.c > @@ -4802,6 +4802,25 @@ vm_fault_t hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma, > if (unlikely(!pte_same(entry, huge_ptep_get(ptep)))) > goto out_ptl; > > + /* Handle userfault-wp first, before trying to lock more pages */ > + if (userfaultfd_pte_wp(vma, huge_ptep_get(ptep)) && I'm going to change this to: - if (userfaultfd_pte_wp(vma, huge_ptep_get(ptep)) && + if (userfaultfd_wp(vma) && huge_pte_uffd_wp(huge_ptep_get(ptep)) && As strictly speaking it's not right to use userfaultfd_pte_wp() directly on a huge pte... Currently huge_pte_uffd_wp() is missing in this version (which is still pte_uffd_wp() anyways), but I'll add a separate patch to introduce all these helpers (also mention about what arch should do when the huge pte is not handled the same way as small pte). I'll try to keep Mike's R-b as this change should be trivial, or please shoot. Thanks, > + (flags & FAULT_FLAG_WRITE) && !huge_pte_write(entry)) { > + struct vm_fault vmf = { > + .vma = vma, > + .address = haddr, > + .flags = flags, > + }; > + > + spin_unlock(ptl); > + if (pagecache_page) { > + unlock_page(pagecache_page); > + put_page(pagecache_page); > + } > + mutex_unlock(&hugetlb_fault_mutex_table[hash]); > + i_mmap_unlock_read(mapping); > + return handle_userfault(&vmf, VM_UFFD_WP); > + } > + > /* > * hugetlb_cow() requires page locks of pte_page(entry) and > * pagecache_page, so here we need take the former one > -- > 2.26.2 > -- Peter Xu