From patchwork Mon Apr 17 19:53:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13214489 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 99E47C77B72 for ; Mon, 17 Apr 2023 19:53:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 240B06B007E; Mon, 17 Apr 2023 15:53:31 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1F1548E0002; Mon, 17 Apr 2023 15:53:31 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 01C978E0001; Mon, 17 Apr 2023 15:53:30 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id E52456B007E for ; Mon, 17 Apr 2023 15:53:30 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id B9D2B806B7 for ; Mon, 17 Apr 2023 19:53:30 +0000 (UTC) X-FDA: 80691932580.21.235E04C Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf18.hostedemail.com (Postfix) with ESMTP id 8495C1C0007 for ; Mon, 17 Apr 2023 19:53:28 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=YgAOFWRb; spf=pass (imf18.hostedemail.com: domain of peterx@redhat.com designates 170.10.133.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=1681761208; 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=ZZajLRXbE1gHSG0/rWcJydl4VAkwntQbZfH27HHj2cI=; b=5suKwCf3cdUeDXwsvprJeyxzW0a7GY8E5GDetdY95tnWg64itOMiPka1XllOy8x+rLxcRK yW29Sx/enWWuJ+TRdUafh0qa1c93kKGh7KPgsq3+0GYk6ddkj8B2lErSCAeyQ761csxA78 xFUFtRjfarDVr18yZ5PklmaV4DFNj4k= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=YgAOFWRb; spf=pass (imf18.hostedemail.com: domain of peterx@redhat.com designates 170.10.133.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=1681761208; a=rsa-sha256; cv=none; b=Dfe9x8v5OLKLQVh3qxc49er8ANKcJYoyM8WGtRAwuTfr0fLD/Sduzmx2MBW9QQ9X9rlslu WKNNuHpuLfF1uBsYn0n8ylzl67MnqIqIn4hQSvoqPlfJ/dKqfW5yxo3TNMuctd9Lz8IsCl BLwgp1HMA4j2az+27hP2mP6l7/fDIyg= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1681761207; 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=ZZajLRXbE1gHSG0/rWcJydl4VAkwntQbZfH27HHj2cI=; b=YgAOFWRbRZYCjZ4335n4h/zs2N2oH0HWBOaxXrq7EfLtmHWo08MHDBD5J/2Jm3MoqBdDJU VvzgK6Ro4NBdZ1CnyhrZJnWCaHYecAUEOg/O/E1W3/w3bi8rD9Z/OdhNTW+ZQE6rTkwPG0 8264GiNQUIYro77xnxrIQZL6SU6IfEw= Received: from mail-qt1-f198.google.com (mail-qt1-f198.google.com [209.85.160.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-190-m4X3MQhXNXykyIGB8D0j6w-1; Mon, 17 Apr 2023 15:53:26 -0400 X-MC-Unique: m4X3MQhXNXykyIGB8D0j6w-1 Received: by mail-qt1-f198.google.com with SMTP id d75a77b69052e-3ed767b30easo1904891cf.1 for ; Mon, 17 Apr 2023 12:53:26 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681761204; x=1684353204; 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=ZZajLRXbE1gHSG0/rWcJydl4VAkwntQbZfH27HHj2cI=; b=bt7xBY6iFwT5FtU8ruE2R72nXkh7HDLQlZvVpjzOzkQtMJYWDJws1R7jEkeaBUlfac GZbLYpMcNFD8Qtz9D7e5nu8DPkDVDbLoDsP3ehysj2+waTtIiJ4BQkztjntZ+J08+lPo tTgcMVUfL23AI0w7HFluvvh2d0nvAlQ1jWjRhbROcGBehQQ/GByX+EvRq2I4OEJap1ci IrgvPDG78FGtx4/udwOgqFPfZGkgXD3PgYVLW9tdIDbu9L9f8PhHhPbUkMrm2fF8Lnas vQaFR3ZG0bMr092DCXhI+Aox6nLxRxBqahTYTiDoO1mXgdGeCiRMapBRm0IZKQjFupq/ VKog== X-Gm-Message-State: AAQBX9cdWRtCWxZNJwDMJRqZc+bZhtwgH4VqXbuVyKEt/5vj+t4SzRS2 CQbc/+AKVdZ3PTVnzaQUfmUu+gdWdKfw+qOSgXQzj3DwTV6QpPK3N6ZXHiX5m6cEhZ2GXZUnXGB +/nw/WMGc89oiSVRPVH4= X-Received: by 2002:a05:622a:1a9b:b0:3ea:ef5:5b8c with SMTP id s27-20020a05622a1a9b00b003ea0ef55b8cmr19155678qtc.3.1681761204286; Mon, 17 Apr 2023 12:53:24 -0700 (PDT) X-Google-Smtp-Source: AKy350aPbpBAnSBo8hIMWFwYiZl6fBgvrueoUnmaQeBSdZmgAZBXgdsikL50SEQY0Q9p7+B82l1+8Q== X-Received: by 2002:a05:622a:1a9b:b0:3ea:ef5:5b8c with SMTP id s27-20020a05622a1a9b00b003ea0ef55b8cmr19155663qtc.3.1681761204064; Mon, 17 Apr 2023 12:53:24 -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 r17-20020ac87ef1000000b003edfb5d7637sm1731278qtc.73.2023.04.17.12.53.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Apr 2023 12:53:23 -0700 (PDT) From: Peter Xu To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Mike Kravetz , Andrea Arcangeli , =?utf-8?q?Mika_Penttil=C3=A4?= , Andrew Morton , peterx@redhat.com, Axel Rasmussen , Nadav Amit , David Hildenbrand , linux-stable Subject: [PATCH v2 2/6] mm/hugetlb: Fix uffd-wp bit lost when unsharing happens Date: Mon, 17 Apr 2023 15:53:13 -0400 Message-Id: <20230417195317.898696-3-peterx@redhat.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230417195317.898696-1-peterx@redhat.com> References: <20230417195317.898696-1-peterx@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 8495C1C0007 X-Stat-Signature: aojuckoe5h6zbtto9467xkomax5h9c93 X-HE-Tag: 1681761208-306167 X-HE-Meta: U2FsdGVkX18naXPLt8q2zvzYx3X6o4lB9Nnin5iFUOtKGoz1PSFo7D42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7r4CurryMa2JUd3hR9yp2OapfQKBnx5CFLkGSba1bzLFMPzEd/oa2SFvUP5P+Y/3/F5bKmXb0vUVIA53CFfWMuwBhGhfmnnP9Z1n17WWsXlrA8Z+eAZbVe708aGwlxLpKwvZ4Ebqn+BwYMTBQipiyHie0BDtCApR/UzeTObkXwjMEpjTfxjJ9oOWXjAnWT0Yz5vh9XFyLpOxfj3FcchHyN5RTjyopwWqo169zM2LAICaYjRuKFlr+Q4PA5Sd1qvJgyQ7+7XkiRps4Kb8bCX++V9GusyeEGX1ssrfaKJmaw8cl/FObElhuQ7RugHM/7EeVa6KdwC4o211jR4lhYpawAJIpJVqcfc71gcQIscn2V0g/sfLQnVlJEuUexYW5ugABP72LchYgClx8lclViOzRGgegM4qPMNzqdUXSmECPPPcSf+SNgBsYjRmjiYAkuFC0= 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 Reviewed-by: David Hildenbrand Reviewed-by: Mike Kravetz Signed-off-by: Peter Xu --- mm/hugetlb.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 0213efaf31be..cd3a9d8f4b70 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);