Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp1781352rwl; Fri, 24 Mar 2023 15:43:59 -0700 (PDT) X-Google-Smtp-Source: AK7set9cSD2d4YhMk3/YcfzDrlIiDyYVF27HXpgtc6PVJNG6gk7Y+2/O/PY6rLkd9rgUxKjCQrG4 X-Received: by 2002:a05:6a20:b2a2:b0:d9:27f7:8c4a with SMTP id ei34-20020a056a20b2a200b000d927f78c4amr4123719pzb.0.1679697838800; Fri, 24 Mar 2023 15:43:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679697838; cv=none; d=google.com; s=arc-20160816; b=HCVK5kWT4F7zic1sRy+UYvOYaZGfwjh7Urs6aXLiiSUUX8BCeXYyjfx1HUqif71uNa cnfhpbfnmgvqRkr9Fc+RKdZO+NwavmjafEnSAAMgZxVswmZ6Bfqz50wOXoztudhJvXtP aJxj+2w9gcwxDbetn8d9EBXhjMj6C7xWqNZgScnf5BA0Y8cUs4ObNfphItlE3DjtwzJc A8jhBvIkdep2CY70+3on5wD06BSjs9sDP4keSMwjDwz8XOgMtbHVhleGh8Au0DTgbhfz xWVp5vCI7l0jMG0s/fZDzy15OIXVq1x4ZW6XVsUdGx5tNmDUyHjCbSKgEKjDjrUeO99I q9Mw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to :organization:from:references:cc:to:content-language:subject :user-agent:mime-version:date:message-id:dkim-signature; bh=245sQ3Egn4hz+1e4Tmo/BWRmLOSVsP8k+1a6ph+rjmI=; b=F+Cx/EGOPr3tp332V6CQIudQCCmx/GBqITG4hV1gNlkcHLNU79hR+Ym79JCCe2sQy2 HiFi14ogO9X3a4ltwXBk1MqZahUCbrZicMi1tEUZluNe31//Chzh6exOxVy1DyLtyPTk EegaJ7Ip8XSGVvifIpgWETJwg1VvYB3oV+6v9kBgghojcBcqvtFCvphH0tfdDVP1T03B /K8ktVZbx9dk52RL72mlyUOOwmh3V3OZAsIKQ4AfEyLSG6XNWsVAUvI/AuzTv0+XdMEX 5mcJ5ghOrb9tCf1kiJp7099BdAgDYr9i0GSvepTtUfMNQruS60W4ZFAA7pRFht53X/jO 8abw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Fbpy43WP; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id v10-20020a63f20a000000b0050fab21430asi10237857pgh.537.2023.03.24.15.43.44; Fri, 24 Mar 2023 15:43:58 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Fbpy43WP; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232120AbjCXWhs (ORCPT + 99 others); Fri, 24 Mar 2023 18:37:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56998 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231966AbjCXWhq (ORCPT ); Fri, 24 Mar 2023 18:37:46 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 26170D52F for ; Fri, 24 Mar 2023 15:36:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1679697417; 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=Fbpy43WPM9ZOL9WKdaP0uGyqUfXecWiNgm8FlKzO9OhBRG6BnvBYygfaLhYp36rQrNyVUY xVjYUqnKwdJDKU1k8Lvxy167NDRQm52TbV5hX8FPKBoRYADzAbpHwUjVRZ8Yt4cmb8wMAt lNz5RLPcv4cyCAe1x62DY4Z5NQanWjc= Received: from mail-yb1-f198.google.com (mail-yb1-f198.google.com [209.85.219.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-597-rHAHyOuRMIeIX_07LJD6tw-1; Fri, 24 Mar 2023 18:36:56 -0400 X-MC-Unique: rHAHyOuRMIeIX_07LJD6tw-1 Received: by mail-yb1-f198.google.com with SMTP id f66-20020a255145000000b00b714602d43fso3150344ybb.10 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=HYMqR5u3sGkrY12SQsJlKT1wr096DuvaNwNeqtvYqpbKR0tdAYqFCp1r4jyI3fKUUS 4tkJk1sWC8+z1FK9BMIIyGDhXG7ZEL6MEzXehO2Es6vmvjUGTR3uyG9y0RKPTJVg0qrF n051QK7Q4CuzTdxfvWC9PijvQiqdIb3JNqLMAjRAKLcVt3v/Sc4Tv0A93ToJE5qNk1xv E88tpXDayeoSbq4R96U3gcWT2W4U6WG8a9mFn/xOjiYc0YZkSAQi/OWBgOXPDbhSTXsH ++I0GiazSTH9c4nvBF05abHSfKFKnPaQNj09Kgmo6cKQnHER4vvSDHO6gdGII2CwCDWh 2ZoA== X-Gm-Message-State: AAQBX9edwzGBJmgFah5DVOtBwuOt6/T+hTwRlAX/mDbC/XxWWnpa15CE Bo7JWzYHYMBumM46fiIev2eFpWJoRKyH7e1CByOEzs0t9wbWh68BDA5sNmlh31mlSTOIdNwHaCM Em5EgnJoybo+FKixe0RarVeLY X-Received: by 2002:a0d:dfd1:0:b0:541:64e4:2094 with SMTP id i200-20020a0ddfd1000000b0054164e42094mr4013390ywe.24.1679697415399; Fri, 24 Mar 2023 15:36:55 -0700 (PDT) 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 Content-Language: en-US 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> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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