From patchwork Wed Jul 14 22:24:58 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 12377973 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 4F9F2C12002 for ; Wed, 14 Jul 2021 22:25:06 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id E0015613C9 for ; Wed, 14 Jul 2021 22:25:05 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org E0015613C9 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 3ABB76B00C8; Wed, 14 Jul 2021 18:25:06 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 35D178D0003; Wed, 14 Jul 2021 18:25:06 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1D6D56B00CA; Wed, 14 Jul 2021 18:25:06 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id EE0B36B00C8 for ; Wed, 14 Jul 2021 18:25:05 -0400 (EDT) Received: from smtpin06.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id DE2A9231D8 for ; Wed, 14 Jul 2021 22:25:04 +0000 (UTC) X-FDA: 78362624928.06.268265F Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf02.hostedemail.com (Postfix) with ESMTP id 9AB89700177D for ; Wed, 14 Jul 2021 22:25:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1626301504; 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=pgOZ+9IjwVfKb5tKhAxRusG1z8GoeGAv34cmBph/B3s=; b=W8baDqN/jefgG6jJBd53gMdPIp354ntaXo/f8eTCjhydJMqFF5d/IrCD7iP0bZEnCKR36m o6VX7j2Bhv/g2ArTHN+u9xXocrNGGiWoCxmx1w9jy6K71rIeF3DkhiN3ZmIq9ADoSW3jfI nkhB5CARv7zFis+XraQELk/r8nnRnuc= Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-406-Ki_ouCYJMte_E-xk4Wf9cg-1; Wed, 14 Jul 2021 18:25:03 -0400 X-MC-Unique: Ki_ouCYJMte_E-xk4Wf9cg-1 Received: by mail-qk1-f199.google.com with SMTP id i190-20020a3786c70000b02903b54f40b442so2360771qkd.0 for ; Wed, 14 Jul 2021 15:25:03 -0700 (PDT) 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=pgOZ+9IjwVfKb5tKhAxRusG1z8GoeGAv34cmBph/B3s=; b=Sq3ukUZlr9YqMM41JEiM0QOCKkaSpe18mMyrNWmpprm8r64UbfkG7CACDLTspI1mxy pXmJTZcFWfLyZ2lLM9u2eoV/0cmZjYVjyKID6eydQ0ndIhkV5LSyguO6fQAZ8pnUqePf bW6+KTYsEDcpb445PkWRBzNx6FWfevceS84kumFc/7E4F5t/GBmkNKcmp9LevGQ/JYx0 i+GN0JOoI8RMa0hqM5gsM/kBnpG1/lSUnBRw4gSGa+b005DATJbX9AttaiABGDUh78+l /Mtq6Tp3O11sssTLMrjbJQmbkW/qREZe5pioxsFtbQ5D8BeDw9KrLgjVEPPomE46ObPx gVhg== X-Gm-Message-State: AOAM531JfV9x7nRVvyMR4ShVTtgwhL6n2h7r9XadBsz20LthyqdaV5mc dbJpMvRrh1abnLAXgmH/yq8hbUXqcWm2RXpNW3nkZnhbcKnWbFa65B2UU+qsK0xDKwDCF2wF0qP 1Sf3xGcLDfYkFkcX231TFLuSIoeTSzeJ7trfbQj7XqUnZNt5hP7bLLgiQDu2X X-Received: by 2002:ac8:5ac3:: with SMTP id d3mr318904qtd.257.1626301502438; Wed, 14 Jul 2021 15:25:02 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwmUP5TjPEpwzMNS3GOKkC/oUMalgKy2Zc3e2y7pAB8MiTJNFhofDrLUpO66BNt/9fBL+Yu3A== X-Received: by 2002:ac8:5ac3:: with SMTP id d3mr318869qtd.257.1626301502188; Wed, 14 Jul 2021 15:25:02 -0700 (PDT) Received: from localhost.localdomain (bras-base-toroon474qw-grc-65-184-144-111-238.dsl.bell.ca. [184.144.111.238]) by smtp.gmail.com with ESMTPSA id o126sm1606702qka.74.2021.07.14.15.25.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Jul 2021 15:25:01 -0700 (PDT) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Andrew Morton , Mike Kravetz , Axel Rasmussen , Miaohe Lin , "Kirill A . Shutemov" , Hugh Dickins , Jason Gunthorpe , Alistair Popple , Matthew Wilcox , peterx@redhat.com, Jerome Glisse , Andrea Arcangeli , Mike Rapoport , Nadav Amit , David Hildenbrand Subject: [PATCH v4 17/26] hugetlb/userfaultfd: Hook page faults for uffd write protection Date: Wed, 14 Jul 2021 18:24:58 -0400 Message-Id: <20210714222458.48942-1-peterx@redhat.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210714222117.47648-1-peterx@redhat.com> References: <20210714222117.47648-1-peterx@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="W8baDqN/"; spf=none (imf02.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-Stat-Signature: tk4bfzq6sahp6pgrkn5mw68ugqwzabm4 X-Rspamd-Queue-Id: 9AB89700177D X-Rspamd-Server: rspam01 X-HE-Tag: 1626301504-224691 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 924553aa8f78..8559b8bb7fa5 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -5059,6 +5059,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