From patchwork Thu Jul 15 20:16:22 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 12380957 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 44F9EC636C8 for ; Thu, 15 Jul 2021 20:16:32 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id E5C7D613C9 for ; Thu, 15 Jul 2021 20:16:31 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org E5C7D613C9 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 548038D0106; Thu, 15 Jul 2021 16:16:32 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4F7B38D00EC; Thu, 15 Jul 2021 16:16:32 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 372C48D0106; Thu, 15 Jul 2021 16:16:32 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0048.hostedemail.com [216.40.44.48]) by kanga.kvack.org (Postfix) with ESMTP id 171328D00EC for ; Thu, 15 Jul 2021 16:16:32 -0400 (EDT) Received: from smtpin30.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 01F8F1BCAB for ; Thu, 15 Jul 2021 20:16:31 +0000 (UTC) X-FDA: 78365929782.30.0D9F051 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by imf17.hostedemail.com (Postfix) with ESMTP id A2FFFF00038C for ; Thu, 15 Jul 2021 20:16:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1626380190; 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=J821CEDH1gjfs0CBrZTj21MlvfuuPmBJQg+S3giuJZs=; b=H0IIRxM84oye3p3EOgS0v6acJJxkWFQEZ2b4Y6NT+1LosV4tdSOI3cS+3lLMHwzQwqKm61 AshxvWvaChFrFwsd7v7y7kdwN68vEOdu8wVCAphr48ArYRHvVD4iX0yrU0t3Lb4UvSrYAK 5qiEDyQUtdRjR84CLptlIOzqQknZxEo= Received: from mail-qv1-f69.google.com (mail-qv1-f69.google.com [209.85.219.69]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-449-bi5bsxRMOCOCUPN7u1k3ow-1; Thu, 15 Jul 2021 16:16:26 -0400 X-MC-Unique: bi5bsxRMOCOCUPN7u1k3ow-1 Received: by mail-qv1-f69.google.com with SMTP id d10-20020a0ce44a0000b02902c99dfad03fso5042810qvm.8 for ; Thu, 15 Jul 2021 13:16:26 -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=J821CEDH1gjfs0CBrZTj21MlvfuuPmBJQg+S3giuJZs=; b=gNSZGH/wg5TwSoPcjEk7wLVOzMsBhVoA/x+fxD0vfnBUVnfc4kYBwmW4DvM8bS4s+y Mq1kRkZUA5HISAZZuDiDpFTpYWz0Pt75J1kGNLaqu6d6UD0q2KmWO5+XHtrz1sNDra9v QKQC3jd029D9FswHxx/XF8ffU8+8NbiXIggIkOmeEW4LRU6c5ER/tI+3PBgwD2n4QSVM y9JE6yyH27JfJdOyS+7PyAhtOHiZtbi4yxGi7mmsuJobPs+js8vJmRpOV7MO6tvtuLzO Ot/VLUzlZAUE+iooU712EDv0TkuaBmb2xvd11ZoAZ3E69+gS3WdrFZe68QD6pU0yXU4d 1qrg== X-Gm-Message-State: AOAM530aqmZ+Jk/iwM88Lcg2JRUCZjSoJhYRXvzQ+ESsH4bWVrlwcqyz 6Ig0OTjHJ6mTM+UPtfmarzkLjQ1AfobbNht89pdME49cIwYCfFn4XEo+7LT6x1zqbLc1PzrfEub aslEwIURo4Hc= X-Received: by 2002:ac8:7a98:: with SMTP id x24mr5652695qtr.42.1626380186444; Thu, 15 Jul 2021 13:16:26 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyqx/JKx983VIK40kL+tOEfKDUFx+QG/B/b/7cM+8atn/7wXP3AG7MvbKd5YDYnZ2YGLzaxaQ== X-Received: by 2002:ac8:7a98:: with SMTP id x24mr5652668qtr.42.1626380186235; Thu, 15 Jul 2021 13:16:26 -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 d2sm2385704qto.91.2021.07.15.13.16.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Jul 2021 13:16:25 -0700 (PDT) From: Peter Xu To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Axel Rasmussen , Nadav Amit , Jerome Glisse , "Kirill A . Shutemov" , Jason Gunthorpe , Alistair Popple , Andrew Morton , David Hildenbrand , peterx@redhat.com, Andrea Arcangeli , Matthew Wilcox , Mike Kravetz , Tiberiu Georgescu , Hugh Dickins , Miaohe Lin , Mike Rapoport Subject: [PATCH v5 17/26] hugetlb/userfaultfd: Hook page faults for uffd write protection Date: Thu, 15 Jul 2021 16:16:22 -0400 Message-Id: <20210715201622.211762-1-peterx@redhat.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210715201422.211004-1-peterx@redhat.com> References: <20210715201422.211004-1-peterx@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: A2FFFF00038C X-Stat-Signature: eof7jg1ygr88ya5teiat75mcf16bfmfg Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=H0IIRxM8; spf=none (imf17.hostedemail.com: domain of peterx@redhat.com has no SPF policy when checking 216.205.24.124) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-HE-Tag: 1626380190-138402 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 Reported-by: kernel test robot --- mm/hugetlb.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 4bdd637b0c29..d34636085eaf 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