From patchwork Tue Mar 23 00:49:04 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 12156503 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 10DD7C433DB for ; Tue, 23 Mar 2021 00:49:56 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id A1E6E6196C for ; Tue, 23 Mar 2021 00:49:55 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org A1E6E6196C 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 1AE936B0130; Mon, 22 Mar 2021 20:49:44 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 160826B0132; Mon, 22 Mar 2021 20:49:44 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E8A246B0133; Mon, 22 Mar 2021 20:49:43 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0120.hostedemail.com [216.40.44.120]) by kanga.kvack.org (Postfix) with ESMTP id C64336B0130 for ; Mon, 22 Mar 2021 20:49:43 -0400 (EDT) Received: from smtpin39.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 8A29A8249980 for ; Tue, 23 Mar 2021 00:49:43 +0000 (UTC) X-FDA: 77949306246.39.7B6F93C Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by imf24.hostedemail.com (Postfix) with ESMTP id EB951A0009E2 for ; Tue, 23 Mar 2021 00:49:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1616460582; 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=LwDOET0+t9FVkgq293BQMlQJW4s7rxaX3ykZpO1NCQ8=; b=T3JJr4TjZgXIAauGUHzlMgVPV/1p1YOpNrgVpd9ZbNJ2iWkEg26Pq1K580hchp8dlhca+N Exrqw/J1eyTnigW+dJV0L3KjRdMwKLpSTWZWXK/gAYWiYjEMbpIgVLMtuPVBMdjowzFxbH gp+yrpujGhwNNfudjsBEToKKzch9BPw= Received: from mail-qv1-f72.google.com (mail-qv1-f72.google.com [209.85.219.72]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-437-Ec2ynQsNN1qzTS9tLftHEQ-1; Mon, 22 Mar 2021 20:49:41 -0400 X-MC-Unique: Ec2ynQsNN1qzTS9tLftHEQ-1 Received: by mail-qv1-f72.google.com with SMTP id k92so540840qva.20 for ; Mon, 22 Mar 2021 17:49:40 -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=LwDOET0+t9FVkgq293BQMlQJW4s7rxaX3ykZpO1NCQ8=; b=REZ2+BSoY+t+crrE05gOuCeE07vXL4q+lJnVFHTioKy800lJEScYlf9csEjB2NO1rl 3CEMxgsXTSuVcF0m76fIWRMACgE7QeA+n4tu+L1hb0sj/wrhfjhYl/+ho6UOTMsuRBKY 3NRBmiXGiFBX9xLEsqKEewPnxpAY4YRpeUKBYVEcCgo7TsdBQaNlYDO2L/N1fLfeg51E BvkqbOC6uUW5iA8Bn3h1iufn1HvB3pzfG6CwT2w4Gi4aJglFbpMYWv7uB+PXOM3DMwv7 BPK3P2Ut1WZNd4+ZztojQUxAZu/PB2l84AX3PYRYM/UWVomUGlNFMSqW0hxWkk7rBflL 8pug== X-Gm-Message-State: AOAM533tK9dQM1RF++KKHFDoiwTsFaTervdUP1nZHWweoK5Vh3//dHmv uJeNhu5AD9AAB/L0MnUTGCgIiFaa4KaaKVgkcDwNU+/quJ9SEvC//NxSBlTDXLanCd5UjxqYzgH oykEM+n3VM70= X-Received: by 2002:a37:e315:: with SMTP id y21mr2972106qki.418.1616460580486; Mon, 22 Mar 2021 17:49:40 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzGhlvWbuuhEk+ZKMiW+MQgkfAcTKCZx9vxyblCGaELts7EA6fuF6kIJ/7TdB+zCUR+L3XDeA== X-Received: by 2002:a37:e315:: with SMTP id y21mr2972089qki.418.1616460580286; Mon, 22 Mar 2021 17:49:40 -0700 (PDT) Received: from localhost.localdomain (bras-base-toroon474qw-grc-82-174-91-135-175.dsl.bell.ca. [174.91.135.175]) by smtp.gmail.com with ESMTPSA id n6sm5031793qtx.22.2021.03.22.17.49.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Mar 2021 17:49:39 -0700 (PDT) From: Peter Xu To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: "Kirill A . Shutemov" , Jerome Glisse , Mike Kravetz , Matthew Wilcox , Andrew Morton , Axel Rasmussen , Hugh Dickins , peterx@redhat.com, Nadav Amit , Andrea Arcangeli , Mike Rapoport Subject: [PATCH 15/23] hugetlb/userfaultfd: Hook page faults for uffd write protection Date: Mon, 22 Mar 2021 20:49:04 -0400 Message-Id: <20210323004912.35132-16-peterx@redhat.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210323004912.35132-1-peterx@redhat.com> References: <20210323004912.35132-1-peterx@redhat.com> MIME-Version: 1.0 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 X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: EB951A0009E2 X-Stat-Signature: 4qgst1x4canfmya5ur8kozsd9roj6u4i Received-SPF: none (redhat.com>: No applicable sender policy available) receiver=imf24; 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: 1616460581-514381 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. Signed-off-by: Peter Xu Reviewed-by: Mike Kravetz --- mm/hugetlb.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 56b78a206913..def2c7ddf3ae 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -4643,6 +4643,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)) && + (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