From patchwork Mon Nov 15 08:02:14 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 12618875 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 705E2C433F5 for ; Mon, 15 Nov 2021 08:02:32 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 0F16C61B97 for ; Mon, 15 Nov 2021 08:02:32 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 0F16C61B97 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 A09D76B007E; Mon, 15 Nov 2021 03:02:31 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 9B9EC6B0093; Mon, 15 Nov 2021 03:02:31 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 881A26B0095; Mon, 15 Nov 2021 03:02:31 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0004.hostedemail.com [216.40.44.4]) by kanga.kvack.org (Postfix) with ESMTP id 7728A6B007E for ; Mon, 15 Nov 2021 03:02:31 -0500 (EST) Received: from smtpin21.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 40EDE8249980 for ; Mon, 15 Nov 2021 08:02:31 +0000 (UTC) X-FDA: 78810422502.21.3865C66 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 D33EEF00039B for ; Mon, 15 Nov 2021 08:02:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1636963350; 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=juo8CnrknUVx2CU+cPYbVJwKflQnt9kXKdPVkj+rnzk=; b=Y9DeJv48eKe/LYmwDk5MNDecn0k0DFZxdmS4o8l2uvoNkfGojOGF0LOessvamUiXGvQi9L lGEpaGftViRqo144K5oLNmG+ke2YavP96JdWPMUQY/fjUFW28iO1UaCc4Ru+R1T9cpFWRr anD1sfQ0J1nfQRgbma8vPpL3jAHi58A= Received: from mail-pg1-f200.google.com (mail-pg1-f200.google.com [209.85.215.200]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-401-I-7Ci8rmMwyY1hp7cUY7Dg-1; Mon, 15 Nov 2021 03:02:29 -0500 X-MC-Unique: I-7Ci8rmMwyY1hp7cUY7Dg-1 Received: by mail-pg1-f200.google.com with SMTP id e4-20020a630f04000000b002cc40fe16afso8733860pgl.23 for ; Mon, 15 Nov 2021 00:02:29 -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=juo8CnrknUVx2CU+cPYbVJwKflQnt9kXKdPVkj+rnzk=; b=3+tyY+2YKph6ozwGBikeAjE46NtUg7u5Dl1QWaip290JwmOL0uDc+g3OSIdS7HTtaL Bvhd0dnPJXmyeeKrKfFyd+1m/QjNJ0L6fGQdx7dwkDe7OgYzSDSMIg34NSF0LHbO/x/d r1J/vYbtYSqzcAqSOe5OTPY5SYoPKGIYI+3imvlTCpVbDZbPG4ffL8C/I0RxECY7gczc VW19yt8CjZPZ4DMma5riwbQfXE8sCuCFeTdxK8STqOLzSMsG4xmVsxZs/XCQq587PmnH OJfoiE0sp7DqaglH2NZZl58VNpnIlzcIzhFNWIEE3x9bNqXtsLLTPwf4DVh3YcldfK2M tZjA== X-Gm-Message-State: AOAM532Xv67p4TVNkR2cGGlTm/6S4L5Y4s/X+tG8AQVKtORcEr67Q8m+ EHpgqx+rgY6ZZoffekHm6zCa+fo1sRPyhQS3ZkdlwkMudeWjsoXluZGoDkgsgzZHtgbokM7gM9Y bA23Wtld23QSO7fjtCuidW+2jUe5y9gqHK0Pl4ADyo6oQhNN8opXlmsGWsZZ7 X-Received: by 2002:a17:902:e804:b0:142:1c0b:c2a6 with SMTP id u4-20020a170902e80400b001421c0bc2a6mr32648884plg.23.1636963347946; Mon, 15 Nov 2021 00:02:27 -0800 (PST) X-Google-Smtp-Source: ABdhPJwDNuRUr56R2HgkYgfQqjH6+BcRz0mtYQUWUhFx5NvN9CbgJnvkwVUYkq/DiyMnk9FcFK3PfQ== X-Received: by 2002:a17:902:e804:b0:142:1c0b:c2a6 with SMTP id u4-20020a170902e80400b001421c0bc2a6mr32648832plg.23.1636963347598; Mon, 15 Nov 2021 00:02:27 -0800 (PST) Received: from localhost.localdomain ([94.177.118.89]) by smtp.gmail.com with ESMTPSA id b4sm14912250pfl.60.2021.11.15.00.02.19 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 15 Nov 2021 00:02:27 -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 15/23] mm/hugetlb: Handle pte markers in page faults Date: Mon, 15 Nov 2021 16:02:14 +0800 Message-Id: <20211115080214.74926-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: D33EEF00039B X-Stat-Signature: bbm6bo3uipk6b68agurni43x4tmei7tt Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Y9DeJv48; 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: 1636963350-360407 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: Allow hugetlb code to handle pte markers just like none ptes. It's mostly there, we just need to make sure we don't assume hugetlb_no_page() only handles none pte, so when detecting pte change we should use pte_same() rather than pte_none(). We need to pass in the old_pte to do the comparison. Check the original pte to see whether it's a pte marker, if it is, we should recover uffd-wp bit on the new pte to be installed, so that the next write will be trapped by uffd. Signed-off-by: Peter Xu --- mm/hugetlb.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 7fc213c0ebf8..e8d01277af0f 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -5361,7 +5361,8 @@ static inline vm_fault_t hugetlb_handle_userfault(struct vm_area_struct *vma, static vm_fault_t hugetlb_no_page(struct mm_struct *mm, struct vm_area_struct *vma, struct address_space *mapping, pgoff_t idx, - unsigned long address, pte_t *ptep, unsigned int flags) + unsigned long address, pte_t *ptep, + pte_t old_pte, unsigned int flags) { struct hstate *h = hstate_vma(vma); vm_fault_t ret = VM_FAULT_SIGBUS; @@ -5487,7 +5488,8 @@ static vm_fault_t hugetlb_no_page(struct mm_struct *mm, ptl = huge_pte_lock(h, mm, ptep); ret = 0; - if (!huge_pte_none(huge_ptep_get(ptep))) + /* If pte changed from under us, retry */ + if (!pte_same(huge_ptep_get(ptep), old_pte)) goto backout; if (anon_rmap) { @@ -5497,6 +5499,12 @@ static vm_fault_t hugetlb_no_page(struct mm_struct *mm, page_dup_rmap(page, true); new_pte = make_huge_pte(vma, page, ((vma->vm_flags & VM_WRITE) && (vma->vm_flags & VM_SHARED))); + /* + * If this pte was previously wr-protected, keep it wr-protected even + * if populated. + */ + if (unlikely(is_pte_marker_uffd_wp(old_pte))) + new_pte = huge_pte_wrprotect(huge_pte_mkuffd_wp(new_pte)); set_huge_pte_at(mm, haddr, ptep, new_pte); hugetlb_count_add(pages_per_huge_page(h), mm); @@ -5614,8 +5622,10 @@ vm_fault_t hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma, mutex_lock(&hugetlb_fault_mutex_table[hash]); entry = huge_ptep_get(ptep); - if (huge_pte_none(entry)) { - ret = hugetlb_no_page(mm, vma, mapping, idx, address, ptep, flags); + /* PTE markers should be handled the same way as none pte */ + if (huge_pte_none_mostly(entry)) { + ret = hugetlb_no_page(mm, vma, mapping, idx, address, ptep, + entry, flags); goto out_mutex; }