From patchwork Tue Apr 5 01:49:01 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 12800968 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 C75F6C433F5 for ; Tue, 5 Apr 2022 01:54:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5C3936B0082; Mon, 4 Apr 2022 21:49:19 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 571C96B0083; Mon, 4 Apr 2022 21:49:19 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 43A566B0085; Mon, 4 Apr 2022 21:49:19 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (relay.hostedemail.com [64.99.140.26]) by kanga.kvack.org (Postfix) with ESMTP id 370C76B0082 for ; Mon, 4 Apr 2022 21:49:19 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 1576B6099C for ; Tue, 5 Apr 2022 01:49:09 +0000 (UTC) X-FDA: 79321142418.14.81CB9E3 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf18.hostedemail.com (Postfix) with ESMTP id 889C31C001F for ; Tue, 5 Apr 2022 01:49:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1649123348; 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=cMJ3LqPsnbXMwjHxk9MsuTKHubQJUVkMzfk9UWY8MQ4=; b=ZCYCYzskOGH+8fh1m+cRfwZeN7yh6RfpPX1dJlgWp4dn0ciwcwao91g2Uxmq8U7xRHQWgc FRsi5r+3QdxGAqF/RjeU7Riyu5e0Nstp8jzSrQoD47XBswgIrbHif5D3deXeTXP/c5xHTG aSJhPCxcfdDDkTBdRJpXQwE647Myq2A= Received: from mail-io1-f71.google.com (mail-io1-f71.google.com [209.85.166.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-422-_s34HcUGPXO26RM2Wc0XVQ-1; Mon, 04 Apr 2022 21:49:04 -0400 X-MC-Unique: _s34HcUGPXO26RM2Wc0XVQ-1 Received: by mail-io1-f71.google.com with SMTP id h10-20020a05660224ca00b0064c77aa4477so7426585ioe.17 for ; Mon, 04 Apr 2022 18:49:04 -0700 (PDT) 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=cMJ3LqPsnbXMwjHxk9MsuTKHubQJUVkMzfk9UWY8MQ4=; b=VK6igoFQKoDJpMAHr4f+DH6BZ7eRWXTCXuYUAeirAsr3hEFo4Oh4eRR+zb/EEqGxwN 3qYmc3Q3tfnihYGBnIYV2QtKLQxjpwzgCjKZI1C5YqCly0ddDxpD29wjwUpkXy2lF8jx VsllgeyWeJTi6cstn4Cvw2xTmymEiOEiBdfZxn91qtKhqUE0KCkGPlUGHNrIgCaLADQT RHyZ0b4ZdvRfen2s/ePP53OdXVn+ACUFTUFlgELxFwV5Z9q882JpGKoaoKufEPqyD8u0 S0rTar0E9H4832tXrRGXFQxPUB9mN4LeDSSQDdUJRa+IgcCRLi22vwyHT8j1eZ+uPQWD d9hg== X-Gm-Message-State: AOAM531+GcROElGL6WsIHyaoZK7dGq0uu8EygZWSsOuhQNDoctm2gIzf 3F7N6UCoY6x7Hzn0KOKF0u4Q+Rq7WyhcI7LI4ksmEQU0xw5PdRSYZllYfxqhdAcLyqe0LgNTdHd N+YoQ7xYFFZA= X-Received: by 2002:a05:6602:2e10:b0:649:e2d4:3334 with SMTP id o16-20020a0566022e1000b00649e2d43334mr586221iow.210.1649123343972; Mon, 04 Apr 2022 18:49:03 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzbt1ibJTi0gvyMJwC5hy/Js4ILQPnWU9UGGKs5bKIIkDs9NeLq7C5AVpY51XCUy10wipducA== X-Received: by 2002:a05:6602:2e10:b0:649:e2d4:3334 with SMTP id o16-20020a0566022e1000b00649e2d43334mr586205iow.210.1649123343719; Mon, 04 Apr 2022 18:49:03 -0700 (PDT) Received: from localhost.localdomain (cpec09435e3e0ee-cmc09435e3e0ec.cpe.net.cable.rogers.com. [99.241.198.116]) by smtp.gmail.com with ESMTPSA id y8-20020a920908000000b002ca38acaa60sm2917919ilg.81.2022.04.04.18.49.02 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 04 Apr 2022 18:49:03 -0700 (PDT) From: Peter Xu To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Mike Kravetz , Nadav Amit , Matthew Wilcox , Mike Rapoport , David Hildenbrand , Hugh Dickins , Jerome Glisse , "Kirill A . Shutemov" , Andrea Arcangeli , Andrew Morton , Axel Rasmussen , Alistair Popple , peterx@redhat.com Subject: [PATCH v8 12/23] mm/hugetlb: Hook page faults for uffd write protection Date: Mon, 4 Apr 2022 21:49:01 -0400 Message-Id: <20220405014901.14590-1-peterx@redhat.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220405014646.13522-1-peterx@redhat.com> References: <20220405014646.13522-1-peterx@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=ZCYCYzsk; spf=none (imf18.hostedemail.com: domain of peterx@redhat.com has no SPF policy when checking 170.10.133.124) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-Rspam-User: X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 889C31C001F X-Stat-Signature: mjj77ny3oqxzi78mx79zywroc53euzjp X-HE-Tag: 1649123348-624313 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: 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 | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index dd642cfc538b..82df0fcfedf9 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -5711,6 +5711,26 @@ 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_wp(vma) && huge_pte_uffd_wp(huge_ptep_get(ptep)) && + (flags & FAULT_FLAG_WRITE) && !huge_pte_write(entry)) { + struct vm_fault vmf = { + .vma = vma, + .address = haddr, + .real_address = address, + .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_wp() requires page locks of pte_page(entry) and * pagecache_page, so here we need take the former one