From mboxrd@z Thu Jan 1 00:00:00 1970 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 5DB2DC6FD1C for ; Fri, 24 Mar 2023 22:37:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 02A916B0078; Fri, 24 Mar 2023 18:37:08 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id F1C516B007B; Fri, 24 Mar 2023 18:37:07 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DE47F900002; Fri, 24 Mar 2023 18:37:07 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id CD6266B0078 for ; Fri, 24 Mar 2023 18:37:07 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id A8DB31A02E6 for ; Fri, 24 Mar 2023 22:37:07 +0000 (UTC) X-FDA: 80605253694.20.623A6F2 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf15.hostedemail.com (Postfix) with ESMTP id 78E0FA000E for ; Fri, 24 Mar 2023 22:37:05 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=M4rXs651; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf15.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1679697425; 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=245sQ3Egn4hz+1e4Tmo/BWRmLOSVsP8k+1a6ph+rjmI=; b=H9mJTxLncHE4Ck3HF6SwV9a2pm3sAy44nquYM7FzF7tQg++WOP59EEqtUbVmO6uz5xZLPg pVAggVIBzuKUBcBip5eTIuzXb6polAZZKpjLoTXHicdTl0d2v1n/hpkokz54rdN0WAOxGD TOIWCOT0EI/wZUCzWEamiAZKTl092BI= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=M4rXs651; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf15.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1679697425; a=rsa-sha256; cv=none; b=RTebp5I0tMqbIiCbulhJSYL3FcK5SqTHvkl8mi5lDc5b0Y9Lcg64iT4uG+7bfIAu3EKjy/ t4xwnQ2f5K8ydbwZ8Zz4ofyoD1WxC7hBiqQh+FKL3lyIMmONvfbaLFtWlHswAbK+BE8uo4 ZKzBLHSYT9on4i+BOxitbaiQXEi4O9A= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1679697424; 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=245sQ3Egn4hz+1e4Tmo/BWRmLOSVsP8k+1a6ph+rjmI=; b=M4rXs651253xgAF0l/uMF7kE+SAmNdFT59HHooFRDws1jVyiiXJdZpjApQ4rBmlhvdWX9a sQ21xvxSFRH1rGq9ty7/xjJqQwuav+E4gsZK4+C5W91F+2RH1nhRSye8sEv4gutlEsx1S8 Z4dx0Hn6vzo8wFBjhgW3Vl+ZT1d29/M= Received: from mail-yb1-f197.google.com (mail-yb1-f197.google.com [209.85.219.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-659-DVFmJsqLMminHvh8PGXMhQ-1; Fri, 24 Mar 2023 18:36:56 -0400 X-MC-Unique: DVFmJsqLMminHvh8PGXMhQ-1 Received: by mail-yb1-f197.google.com with SMTP id g187-20020a2520c4000000b00b74680a7904so3083202ybg.15 for ; Fri, 24 Mar 2023 15:36:55 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679697415; h=content-transfer-encoding:in-reply-to:organization:from:references :cc:to:content-language:subject:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=245sQ3Egn4hz+1e4Tmo/BWRmLOSVsP8k+1a6ph+rjmI=; b=M4ym+1i1gSdXA/mAR883L6p8HUnwnASrk3CQ6U5MkNkxDLjUmnrBa8O23hCF8QvWLH waLzb1BpEVVJu1CSalUV4awgf1t7UVy6RlPbfkKNzPxjSxICEoU7sXi6FIQCCTNcsbdP cihmDH2A40mswpPsTgOGLOBL2qmlZDtEochqWmkSgwJ+wPywcCjwgiMl+giWnrft981M bhwZpGk7BYIcBuKtRq7d/kgPTdL29SWWIa0+5qwu7uWihJlc/CNY0rgsgheO92r39HEZ F/npBZ81d21q4R4Sz2q6OgXH83LfNjeKac1X5KvXdey7yAjuYVsa2v5ljqjliF9lslj9 8vbw== X-Gm-Message-State: AAQBX9fT7Aq5rHZRG7iizg14wYekFc7BefpZlHT0Qgim3GRAmYJ5VpyF OkNUhJy+/Rk0lAs79DC2XNsdKyYEBFU81k/0Er0vN3Etd/uQ2qC5vLBmutAIwMjtTAPW8snhtTy BW8+t97aPOzw= X-Received: by 2002:a0d:dfd1:0:b0:541:64e4:2094 with SMTP id i200-20020a0ddfd1000000b0054164e42094mr4013392ywe.24.1679697415399; Fri, 24 Mar 2023 15:36:55 -0700 (PDT) X-Google-Smtp-Source: AKy350bbwDMlwcxIQGcKee8Lw++uBBz1BazzuQGpq2PjaQ8HDMN/bfcPjM7kEylxlrs5ohlMI/tbdg== X-Received: by 2002:a0d:dfd1:0:b0:541:64e4:2094 with SMTP id i200-20020a0ddfd1000000b0054164e42094mr4013371ywe.24.1679697415091; Fri, 24 Mar 2023 15:36:55 -0700 (PDT) Received: from [100.69.142.128] ([206.173.106.22]) by smtp.gmail.com with ESMTPSA id q9-20020a814309000000b00545a08184f4sm664550ywa.132.2023.03.24.15.36.54 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 24 Mar 2023 15:36:54 -0700 (PDT) Message-ID: <8a06be33-1b44-b992-f80a-8764810ebf3f@redhat.com> Date: Fri, 24 Mar 2023 23:36:53 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.9.0 Subject: Re: [PATCH v3] mm/hugetlb: Fix uffd wr-protection for CoW optimization path To: Mike Kravetz , Peter Xu Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrew Morton , Mike Rapoport , Nadav Amit , Axel Rasmussen , Andrea Arcangeli , Muhammad Usama Anjum , linux-stable References: <20230324142620.2344140-1-peterx@redhat.com> <20230324222707.GA3046@monkey> From: David Hildenbrand Organization: Red Hat In-Reply-To: <20230324222707.GA3046@monkey> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 78E0FA000E X-Stat-Signature: oqggs4scarjfrkusxy89tm8gprj83n6k X-HE-Tag: 1679697425-168865 X-HE-Meta: U2FsdGVkX1/s5Ade7GnSOVnTNnFiK/1WNVki/StAY0RZknFu+8xf52WbiwKWSrjCL0u93CVHZl1kzrDKA/IDyBGg0iFYvH8LHl6TmZm5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u3osqdLNy44zDu+Jv73hA+e+tslah1VAfnew4JmU/CInpwgq1qFiJWjnI+XlNxhLpdG+ZUa74Hk6lihvdNq6JaZMmiXgqe64e1VVRVByF4+Hk6ThiXPW4ezv/qxm1Z0MyZswjKlaN2tkxP+Ue5wsW/dvuUvAqfUGlNjQBzPIqF0EEh9ZBFhhjXdh9CF3wBPGbhioS3AmirL7tJLBacaRJ66h58P3RkQsCoFhk5fy4V9J4cx6RWUKpkdoiGBBx13cOdQ1mhyj8pnckSLbHMLlrx8eXQhLP7mhrJ2X7uUE7JmbzvwwhhEmqF5KJmMromjCQkKqYguKcaORRpGYLTlgFFH4eLA4S3O5VV6amfYm9LRnwntPBZUxglxqVdXOIqz4naZ39xtKJ/q8XJGph/WFQesv808cFtVoIaPSOaB/FaLX2gKU3Qtjbq667awFt4o5RWr05RmnVZVuuA== 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: On 24.03.23 23:27, Mike Kravetz wrote: > On 03/24/23 10:26, Peter Xu wrote: >> This patch fixes an issue that a hugetlb uffd-wr-protected mapping can be >> writable even with uffd-wp bit set. It only happens with hugetlb private >> mappings, when someone firstly wr-protects a missing pte (which will >> install a pte marker), then a write to the same page without any prior >> access to the page. >> >> Userfaultfd-wp trap for hugetlb was implemented in hugetlb_fault() before >> reaching hugetlb_wp() to avoid taking more locks that userfault won't need. >> However there's one CoW optimization path that can trigger hugetlb_wp() >> inside hugetlb_no_page(), which will bypass the trap. >> >> This patch skips hugetlb_wp() for CoW and retries the fault if uffd-wp bit >> is detected. The new path will only trigger in the CoW optimization path >> because generic hugetlb_fault() (e.g. when a present pte was wr-protected) >> will resolve the uffd-wp bit already. Also make sure anonymous UNSHARE >> won't be affected and can still be resolved, IOW only skip CoW not CoR. >> >> This patch will be needed for v5.19+ hence copy stable. >> >> Reported-by: Muhammad Usama Anjum >> Cc: linux-stable >> Fixes: 166f3ecc0daf ("mm/hugetlb: hook page faults for uffd write protection") >> Signed-off-by: Peter Xu >> --- >> >> Notes: >> >> v2 is not on the list but in an attachment in the reply; this v3 is mostly >> to make sure it's not the same as the patch used to be attached. Sorry >> Andrew, we need to drop the queued one as I rewrote the commit message. > > My appologies! I saw the code path missed in v2 and assumed you did not > think it applied. So, I said nothing. My bad! > >> Muhammad, I didn't attach your T-b because of the slight functional change. >> Please feel free to re-attach if it still works for you (which I believe >> should). >> >> thanks, >> --- >> mm/hugetlb.c | 14 ++++++++++++-- >> 1 file changed, 12 insertions(+), 2 deletions(-) >> >> diff --git a/mm/hugetlb.c b/mm/hugetlb.c >> index 8bfd07f4c143..a58b3739ed4b 100644 >> --- a/mm/hugetlb.c >> +++ b/mm/hugetlb.c >> @@ -5478,7 +5478,7 @@ static vm_fault_t hugetlb_wp(struct mm_struct *mm, struct vm_area_struct *vma, >> struct folio *pagecache_folio, spinlock_t *ptl) >> { >> const bool unshare = flags & FAULT_FLAG_UNSHARE; >> - pte_t pte; >> + pte_t pte = huge_ptep_get(ptep); >> struct hstate *h = hstate_vma(vma); >> struct page *old_page; >> struct folio *new_folio; >> @@ -5487,6 +5487,17 @@ static vm_fault_t hugetlb_wp(struct mm_struct *mm, struct vm_area_struct *vma, >> unsigned long haddr = address & huge_page_mask(h); >> struct mmu_notifier_range range; >> >> + /* >> + * Never handle CoW for uffd-wp protected pages. It should be only >> + * handled when the uffd-wp protection is removed. >> + * >> + * Note that only the CoW optimization path (in hugetlb_no_page()) >> + * can trigger this, because hugetlb_fault() will always resolve >> + * uffd-wp bit first. >> + */ >> + if (!unshare && huge_pte_uffd_wp(pte)) >> + return 0; > > This looks correct. However, since the previous version looked correct I must > ask. Can we have unshare set and huge_pte_uffd_wp true? If so, then it seems > we would need to possibly propogate that uffd_wp to the new pte as in v2 We can. A reproducer would share an anon hugetlb page because parent and child. In the parent, we would uffd-wp that page. We could trigger unsharing by R/O-pinning that page. -- Thanks, David / dhildenb