From patchwork Thu Apr 13 23:11:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13210719 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id A5D99C77B6F for ; Thu, 13 Apr 2023 23:11:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8E12B900005; Thu, 13 Apr 2023 19:11:31 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 869BF900004; Thu, 13 Apr 2023 19:11:31 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6E2AD900005; Thu, 13 Apr 2023 19:11:31 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 5F988900004 for ; Thu, 13 Apr 2023 19:11:31 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 3F2151A0476 for ; Thu, 13 Apr 2023 23:11:31 +0000 (UTC) X-FDA: 80677916382.08.A7297FF Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf09.hostedemail.com (Postfix) with ESMTP id 31B5D140011 for ; Thu, 13 Apr 2023 23:11:29 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="QHbL/w4d"; spf=pass (imf09.hostedemail.com: domain of peterx@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1681427489; h=from:from:sender: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:dkim-signature; bh=sPrSeNO5Sj4btD4u1XV4AOohfTbtE2i2MNiZ002Yg6Y=; b=KK7+cNEJXaO0+l6kCRw2DO1j6bCYGSlKj3vDSaARNGZbNLpiU5RuhJ4xh/8/GEb8TPXQk3 PWA7IKgyuMIOs1hEW0NSA7G8cVfcz0H9G4qZGXLkL/pzsZun5ZR2SCZ1XKrItzt2M+b6ic 1efVhyaPzhGXAKqZU7J/GZ0v/KN1b/Q= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="QHbL/w4d"; spf=pass (imf09.hostedemail.com: domain of peterx@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1681427489; a=rsa-sha256; cv=none; b=pmkrk4dbP/bDNwFkKHzl4X0ijf/V4/3sZ8YSlURGczSQt0MV3BRZY97uwn84e+oEnmJhvO 6QilWLorygY7giz4ZzfjwWpttMRm7q2M2cGOPXR+Ao7+VkuTRsp0+q9Fsd+glM1cIVtctF 2//x2S0pgYnxMdjUxpKDCPJr0HIp/Mk= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1681427488; 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=sPrSeNO5Sj4btD4u1XV4AOohfTbtE2i2MNiZ002Yg6Y=; b=QHbL/w4dmvGxFeQLBb7JjktmB6rwN+AfJXZpHrDwAnrHLEnM7JPNTka5XJxEivFpaIniBJ 2hCrYpAB+3msNZEeX9o6sWPRmBlsYy1MlLzSGaE5Gm8sV6UTp+gV5/GyjcKFlU/Jdfm8Ts l9jS0wG33ssKUjVTvNZ43JAxojkM1lY= Received: from mail-qk1-f198.google.com (mail-qk1-f198.google.com [209.85.222.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-25-C3imq5NMNXCosO6WE6BdlA-1; Thu, 13 Apr 2023 19:11:27 -0400 X-MC-Unique: C3imq5NMNXCosO6WE6BdlA-1 Received: by mail-qk1-f198.google.com with SMTP id af79cd13be357-74acff8cfaeso17907385a.0 for ; Thu, 13 Apr 2023 16:11:27 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681427487; x=1684019487; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=sPrSeNO5Sj4btD4u1XV4AOohfTbtE2i2MNiZ002Yg6Y=; b=SpzgmfBaBB4fuWslNT9lM03LbOIraeFTez95nA1ONUKpgGx9XdoU1mA7UOnOj+94R/ pjmaJmB80iUUyJVvvlfY+R2eUrzHAgjIsxWE346QHMjlMzNfLp5beePDMAnBN5oZjZsK g9nubWqKsNMIiWXYEyltfizIMYmrOXrFXXKVO1RQCSYWv4nCiVFLL0XhtHx6PH52aOuj subFikkAVOwywW75H7BQ/j6hRqmx4My2UIeEr65Uq5Yf/dcWzPXQ53ntsiboIwoDaqad a4PIzWx60iLkJbmYR5IpH1eBMcfr/xJ0gVJ5gR891Zea189ncb+ezHf+9ofrCPZppbgK g0nA== X-Gm-Message-State: AAQBX9dVPP9btSasv7h7C+dgvrlpLSD5UkdZ7AqhMMNd8kdWQI4iSJ1L I+X5troNZNz41GAh0eSTX0XKAvl772NV5fSBlS1cKSJxi82t3XHfIIb8GhL17b3P45ikACr8RI2 SjV4XbfZbUkw= X-Received: by 2002:a05:622a:1aa6:b0:3e2:be32:cb74 with SMTP id s38-20020a05622a1aa600b003e2be32cb74mr1006397qtc.3.1681427486994; Thu, 13 Apr 2023 16:11:26 -0700 (PDT) X-Google-Smtp-Source: AKy350ay1srzjNEubUa8Bu2M4QrSigXg05em6AqIPROuVf3uz6yCOvlGUBFyb0XrOhaExs1Nao5lxA== X-Received: by 2002:a05:622a:1aa6:b0:3e2:be32:cb74 with SMTP id s38-20020a05622a1aa600b003e2be32cb74mr1006377qtc.3.1681427486764; Thu, 13 Apr 2023 16:11:26 -0700 (PDT) Received: from x1n.redhat.com (bras-base-aurron9127w-grc-40-70-52-229-124.dsl.bell.ca. [70.52.229.124]) by smtp.gmail.com with ESMTPSA id g3-20020ac81243000000b003ea1b97acfasm612446qtj.49.2023.04.13.16.11.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Apr 2023 16:11:25 -0700 (PDT) From: Peter Xu To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Axel Rasmussen , Andrew Morton , David Hildenbrand , peterx@redhat.com, Mike Kravetz , Nadav Amit , Andrea Arcangeli , linux-stable Subject: [PATCH 2/6] mm/hugetlb: Fix uffd-wp bit lost when unsharing happens Date: Thu, 13 Apr 2023 19:11:16 -0400 Message-Id: <20230413231120.544685-3-peterx@redhat.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230413231120.544685-1-peterx@redhat.com> References: <20230413231120.544685-1-peterx@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Rspamd-Queue-Id: 31B5D140011 X-Stat-Signature: y19q5nk7mwpjfyoyzg6hwho4jrcp4oo3 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1681427489-777748 X-HE-Meta: U2FsdGVkX1+0OaGC+6YN+dgwJYALlQQWNkzKXwx9tZxRaBpcjTy2gXEyLxk+/C6al25kavnGMuQqwodGU2EtFTvlSScfZCt/CC2KdW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9j9bWtsFj/60ZwV05pzTf2oZyCPYOer3cW620Pkri7+VtXdcg6I99+Jkf4K7kziIPbpqe73g9G8LSxAuh2hpN7oC9RuMaWOv4LfBsqH7eb6f+p/LUS5etIz1DX1Q58JMnFDsKMd8yRGk++azdHAZfScZnnTrnhqppV+My+f6yi6dk07VN59c0jqbCqGaSkWGnHvC9mKBRHaOt2NV2rF3f4Kj218wTkRw1D3o2c4QDAoTKDWKa9nHZUlPHeSl2K5rIPnTtw5C0FyYfDBy7XWddHiW+Ifv7OzK5q7NwXQ40AtAsyjoJ0WF34YH7+eHk075wALGIleqIqFbhSAPOIb2JzDihRiFsCT7xtfPZkvNaZDc1xEJcXQYVUGGxFqeHs825iijS2zoSft98O91mhu4MScrmUFNI= 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: When we try to unshare a pinned page for a private hugetlb, uffd-wp bit can get lost during unsharing. Fix it by carrying it over. This should be very rare, only if an unsharing happened on a private hugetlb page with uffd-wp protected (e.g. in a child which shares the same page with parent with UFFD_FEATURE_EVENT_FORK enabled). Cc: linux-stable Fixes: 166f3ecc0daf ("mm/hugetlb: hook page faults for uffd write protection") Reported-by: Mike Kravetz Signed-off-by: Peter Xu Reviewed-by: David Hildenbrand Reviewed-by: Mike Kravetz --- mm/hugetlb.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 7320e64aacc6..083aae35bff8 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -5637,13 +5637,16 @@ static vm_fault_t hugetlb_wp(struct mm_struct *mm, struct vm_area_struct *vma, spin_lock(ptl); ptep = hugetlb_walk(vma, haddr, huge_page_size(h)); if (likely(ptep && pte_same(huge_ptep_get(ptep), pte))) { + pte_t newpte = make_huge_pte(vma, &new_folio->page, !unshare); + /* Break COW or unshare */ huge_ptep_clear_flush(vma, haddr, ptep); mmu_notifier_invalidate_range(mm, range.start, range.end); page_remove_rmap(old_page, vma, true); hugepage_add_new_anon_rmap(new_folio, vma, haddr); - set_huge_pte_at(mm, haddr, ptep, - make_huge_pte(vma, &new_folio->page, !unshare)); + if (huge_pte_uffd_wp(pte)) + newpte = huge_pte_mkuffd_wp(newpte); + set_huge_pte_at(mm, haddr, ptep, newpte); folio_set_hugetlb_migratable(new_folio); /* Make the old page be freed below */ new_folio = page_folio(old_page);