From patchwork Mon Nov 15 08:01:32 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 12618869 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 50D33C433EF for ; Mon, 15 Nov 2021 08:01:51 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id EE91260E8D for ; Mon, 15 Nov 2021 08:01:50 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org EE91260E8D Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id 8AB686B008C; Mon, 15 Nov 2021 03:01:50 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 85A356B0092; Mon, 15 Nov 2021 03:01:50 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 749E86B0093; Mon, 15 Nov 2021 03:01:50 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0027.hostedemail.com [216.40.44.27]) by kanga.kvack.org (Postfix) with ESMTP id 6801D6B008C for ; Mon, 15 Nov 2021 03:01:50 -0500 (EST) Received: from smtpin04.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 2618F180E936C for ; Mon, 15 Nov 2021 08:01:50 +0000 (UTC) X-FDA: 78810420780.04.901AFAF Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf25.hostedemail.com (Postfix) with ESMTP id 87F87B00018D for ; Mon, 15 Nov 2021 08:01:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1636963309; 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=3JN/qHWTDj3qXyIKJcMZC7BazoBUZifDcHVBgdG1ZHs=; b=CfANeJZ3v2hiBeqeRpIoBikbJu+JbRoPi63LvufvKv/zSjdwEmYNudRuBEGMhaH/p+PBb+ cIaoinMPgbfN3WPVgnFB4iCnYCsR7BNJ+dAURkai0/TvxBMZa8ZJZ14/rq+wd84Z1QDypW 9Y1fo3PsrMo+jLwtQUlcdDKO5d/vcWA= Received: from mail-pg1-f197.google.com (mail-pg1-f197.google.com [209.85.215.197]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-202-9ob3jgkAMpiQQ1PVBW9Phg-1; Mon, 15 Nov 2021 03:01:48 -0500 X-MC-Unique: 9ob3jgkAMpiQQ1PVBW9Phg-1 Received: by mail-pg1-f197.google.com with SMTP id h35-20020a63f923000000b002d5262fdfc4so8779534pgi.2 for ; Mon, 15 Nov 2021 00:01:48 -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=3JN/qHWTDj3qXyIKJcMZC7BazoBUZifDcHVBgdG1ZHs=; b=TyHx1ULsMypXGIdur1zHi7uKRq4J+4sgM9FDuzkcpFYZWdBkfOvFF+vYzlLOuoUj33 myukxkO4Cqz2xGY4v+tkgaM0KKgLwJF2n1fiRm6i1TC21cBZzSLNmhmJrpHco1cQz9WS IsBfat44IbImjHQBga1knXLnHHJvPE88nfTf6Cqd1qmyK6W7UTg2cxE4/s/j6j2tftT0 Dc8HLl0GMY7WG/RrIW6IDQ5+YDcBzOYSWUEKWx8+So/EoWENpM+QlUGIx7JybO5nVVhl PUC0GUuvhN6e+ePsK3pJFjR0dDjStycQ7smzhihzixcplYBEbhzVm/QSHqeS8qVnRcXJ uG+Q== X-Gm-Message-State: AOAM533jkqAlFcXuM7FLtU2R4jKQcIzU+oERm7cRx2ErfIlm3ubF49ve IoLWF/0UYtmuzx8DaZFVoeGEQkcmUHK7OSDbi0Fayb7Z6oZCXi/LMQMvZL0cC0heCL9SvwXYeoY DPWm14h5SoV318ciCVM5Q01shlvOLX4E8fCCoMrLCRxEihr3g5CN6/6UuI1ff X-Received: by 2002:a65:4c01:: with SMTP id u1mr23373808pgq.151.1636963306643; Mon, 15 Nov 2021 00:01:46 -0800 (PST) X-Google-Smtp-Source: ABdhPJxAqokYq6ooU4nDXInwG5sxx6pe6ho32Uv0rSbhETu3lnvghB2QdRC5YvCrwwuikrQwoQlTpg== X-Received: by 2002:a65:4c01:: with SMTP id u1mr23373775pgq.151.1636963306345; Mon, 15 Nov 2021 00:01:46 -0800 (PST) Received: from localhost.localdomain ([94.177.118.89]) by smtp.gmail.com with ESMTPSA id f130sm14450402pfa.81.2021.11.15.00.01.38 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 15 Nov 2021 00:01:45 -0800 (PST) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Nadav Amit , peterx@redhat.com, Alistair Popple , Andrew Morton , Mike Kravetz , Mike Rapoport , Matthew Wilcox , Jerome Glisse , Axel Rasmussen , "Kirill A . Shutemov" , David Hildenbrand , Andrea Arcangeli , Hugh Dickins Subject: [PATCH v6 12/23] mm/hugetlb: Hook page faults for uffd write protection Date: Mon, 15 Nov 2021 16:01:32 +0800 Message-Id: <20211115080132.74754-1-peterx@redhat.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20211115075522.73795-1-peterx@redhat.com> References: <20211115075522.73795-1-peterx@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: 87F87B00018D X-Stat-Signature: 7czadj4rfokbit4w5yfr9x9hhyjigmcy Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=CfANeJZ3; spf=none (imf25.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-HE-Tag: 1636963296-402301 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 | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index e09159c957e3..3a10274b2e39 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -5658,6 +5658,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_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, + .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