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 A1C82C77B6F for ; Fri, 14 Apr 2023 09:37:50 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9A153900003; Fri, 14 Apr 2023 05:37:49 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 95163900002; Fri, 14 Apr 2023 05:37:49 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 819C2900003; Fri, 14 Apr 2023 05:37:49 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 72682900002 for ; Fri, 14 Apr 2023 05:37:49 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 40C8F1A019B for ; Fri, 14 Apr 2023 09:37:49 +0000 (UTC) X-FDA: 80679494658.22.C5A440F Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf15.hostedemail.com (Postfix) with ESMTP id 03E81A0005 for ; Fri, 14 Apr 2023 09:37:46 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=ZYfalgO0; spf=pass (imf15.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@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=1681465067; 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=y7jfa/TTSXXNR+HI17/4WsjHOhACytVYBySIdLruFJk=; b=lo0sP1oymmH7b4RwNT2Fli+bcXWmmyjzuM0JLsd4mapwwut+Ztruaw05+a8Oeum9lLJZ4k 6dLOFKB6tuAiQAmCuz66wdCPeZt4/zEmo/EH3oOsFou5oNJqOuB5IbMu9UoBAvis0hmeZc ga7CQGAhHtBJoZtmFKDGwoEzKjTFzW4= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=ZYfalgO0; spf=pass (imf15.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1681465067; a=rsa-sha256; cv=none; b=xDb0wy0BANvW213H2ts/R3AP9ej48ACDu3ymVIzalYvn/lpgCJo9cAz98KTjDgrZGGFsDU mXo7lUzco81C14rfqFQBn2EfdMr2mc55b+K2TLk2rxSzq8/mzvKcA+wQnSoaZMZg2w93Tg UbwLis2vz6B/gdh1twHPXrjQ3NAfLbg= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1681465066; 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=y7jfa/TTSXXNR+HI17/4WsjHOhACytVYBySIdLruFJk=; b=ZYfalgO06aNXC4RxxcE8NT3ok1VlOPJZ1IwMGD4qQzkvhrPA8g1aQKJzGvjFY6CpFt7sAD OWvWJt0HthgG7+WQj8GYMut40r594EauB/AgiDfJgzbUUpvxRefqDtI9ZE+mxGZ7IGWYTo /8F+eyuiiY7PHoMfrec412uewtpY1NM= Received: from mail-wr1-f72.google.com (mail-wr1-f72.google.com [209.85.221.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-306-gRTfVYETOeGeLnun1FLCyA-1; Fri, 14 Apr 2023 05:37:44 -0400 X-MC-Unique: gRTfVYETOeGeLnun1FLCyA-1 Received: by mail-wr1-f72.google.com with SMTP id v16-20020adfa1d0000000b002f64d93cfd4so979855wrv.14 for ; Fri, 14 Apr 2023 02:37:43 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681465063; x=1684057063; h=content-transfer-encoding:in-reply-to:subject:organization:from :references:cc:to:content-language:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=y7jfa/TTSXXNR+HI17/4WsjHOhACytVYBySIdLruFJk=; b=XLiGpJqzAewpYE6+mvk+OPfd9dzTnySLuTjJUXV+D2jqiq2Inue6JGtb7Iwcp2BgFx KdhO0HKlYHk+3xxeQR+hut4QJKYz8zFhcl81UcuoLD7AW/qW3hHxJ/zCR1odsQmbyWtp D7kcxFvdn7WvgwmzqK8+7vTpCv7RGy1RtSPIAQsG8BaOY1wo/Eq0EKJdZI9kqVSAY+/z YJnkG8MG5uYGb9OXi50sZjwRrSp5qxSwfgfK0X3/zaZJXETfHXslTiwQrxRE/m0UKSwg SX4zBcfhnbQTsMp3dKPkodRdPdXJMj7rabNZOa3qPvVwbyiBZgDBjNu7St81txKJdobU 2oiw== X-Gm-Message-State: AAQBX9c7AGYAOWyY7POGOIFXzrvTxqxtrM/VKZzRXkF9d7jfuEx68yfy CJ3hyy2p0bruR9HxbHRyvKm4rOmlOIy+0JPuQlRQwb/hwU0jq0dkVMtSyW0Kzcuwddxq5wKC9O3 vGDwbsDi2dqA= X-Received: by 2002:a5d:5350:0:b0:2e4:abb1:3e8b with SMTP id t16-20020a5d5350000000b002e4abb13e8bmr3748503wrv.25.1681465062787; Fri, 14 Apr 2023 02:37:42 -0700 (PDT) X-Google-Smtp-Source: AKy350Z1swKpxAiu43bOkrN04CBR2jPIDXEy0bemkCbdTlJVl7ANlzOYK7CrGeUhL+007cOsBJ0tSg== X-Received: by 2002:a5d:5350:0:b0:2e4:abb1:3e8b with SMTP id t16-20020a5d5350000000b002e4abb13e8bmr3748486wrv.25.1681465062444; Fri, 14 Apr 2023 02:37:42 -0700 (PDT) Received: from ?IPV6:2003:cb:c702:5700:cb5b:f73a:c650:1d9? (p200300cbc7025700cb5bf73ac65001d9.dip0.t-ipconnect.de. [2003:cb:c702:5700:cb5b:f73a:c650:1d9]) by smtp.gmail.com with ESMTPSA id v10-20020a05600c470a00b003ef36ef3833sm7684982wmo.8.2023.04.14.02.37.41 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 14 Apr 2023 02:37:42 -0700 (PDT) Message-ID: <17c46475-5889-1f60-39be-6208d70c90a0@redhat.com> Date: Fri, 14 Apr 2023 11:37:41 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.9.1 To: Peter Xu , linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Axel Rasmussen , Andrew Morton , Mike Kravetz , Nadav Amit , Andrea Arcangeli , linux-stable References: <20230413231120.544685-1-peterx@redhat.com> <20230413231120.544685-2-peterx@redhat.com> From: David Hildenbrand Organization: Red Hat Subject: Re: [PATCH 1/6] mm/hugetlb: Fix uffd-wp during fork() In-Reply-To: <20230413231120.544685-2-peterx@redhat.com> 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-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 03E81A0005 X-Stat-Signature: a596qhapfz758yis5wfcbmfi71k4eh6j X-Rspam-User: X-HE-Tag: 1681465066-759721 X-HE-Meta: U2FsdGVkX1/PQmsyqhDF5z0LV9o/S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O8PnIT/5K0R4XVD3gxNzqcEUShKRkmaeAa66FHihTVreYjcZN+ifEN2O7hSspIeZPNxwbOId0AHlpaW51uTl+BxTlj4qUe4uOlMgsEFyhTvjDUp1P44wxqnlWWAalu84eNTMMPFhyLqEF0BQS+uPOdpL58Po069g+IuKMDZkWV13bIkKdlGISSJmFKK7qCvhGvcUyeXuR3nbt7HLt5SfEI9NhBOw/r8AefaIKNAoPC/GaUDApJH93DlIQVTVBYWIBfMSrY8Ozzp/2DLEnwINAUgkrVQ1pKzmmF3cJWGaQqiby1eYwtMqgP3/TTOgjnszao8OuOroBOL3Yz1TtEShWEswVmdp4ZXgrLeSGGnQgACJ+9yNi0Crhj5pFCV33sxTQv2//bMRmlCG3Dmyyy6NZwGZd/TBn1SKqo+jKopWRRLwA8aLfNkqqk38OXmhTFWA08q0c= 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 14.04.23 01:11, Peter Xu wrote: > There're a bunch of things that were wrong: > > - Reading uffd-wp bit from a swap entry should use pte_swp_uffd_wp() > rather than huge_pte_uffd_wp(). > > - When copying over a pte, we should drop uffd-wp bit when > !EVENT_FORK (aka, when !userfaultfd_wp(dst_vma)). > > - When doing early CoW for private hugetlb (e.g. when the parent page was > pinned), uffd-wp bit should be properly carried over if necessary. > > No bug reported probably because most people do not even care about these > corner cases, but they are still bugs and can be exposed by the recent unit > tests introduced, so fix all of them in one shot. > > Cc: linux-stable > Fixes: bc70fbf269fd ("mm/hugetlb: handle uffd-wp during fork()") > Signed-off-by: Peter Xu > --- > mm/hugetlb.c | 26 ++++++++++++++++---------- > 1 file changed, 16 insertions(+), 10 deletions(-) > > diff --git a/mm/hugetlb.c b/mm/hugetlb.c > index f16b25b1a6b9..7320e64aacc6 100644 > --- a/mm/hugetlb.c > +++ b/mm/hugetlb.c > @@ -4953,11 +4953,15 @@ static bool is_hugetlb_entry_hwpoisoned(pte_t pte) > > static void > hugetlb_install_folio(struct vm_area_struct *vma, pte_t *ptep, unsigned long addr, > - struct folio *new_folio) > + struct folio *new_folio, pte_t old) > { Nit: The function now expects old to be !swap_pte. Which works perfectly fine with existing code -- the function name is a bit generic and misleading, unfortunately. IMHO, instead of factoring that functionality out to desperately try keeping copy_hugetlb_page_range() somewhat readable, we should just have factored out the complete copy+replace into a copy_hugetlb_page() function -- similar to the ordinary page handling -- which would have made copy_hugetlb_page_range() more readable eventually. Anyhow, unrelated. > + pte_t newpte = make_huge_pte(vma, &new_folio->page, 1); > + > __folio_mark_uptodate(new_folio); > hugepage_add_new_anon_rmap(new_folio, vma, addr); > - set_huge_pte_at(vma->vm_mm, addr, ptep, make_huge_pte(vma, &new_folio->page, 1)); > + if (userfaultfd_wp(vma) && huge_pte_uffd_wp(old)) > + newpte = huge_pte_mkuffd_wp(newpte); > + set_huge_pte_at(vma->vm_mm, addr, ptep, newpte); > hugetlb_count_add(pages_per_huge_page(hstate_vma(vma)), vma->vm_mm); > folio_set_hugetlb_migratable(new_folio); > } > @@ -5032,14 +5036,11 @@ int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src, > */ > ; > } else if (unlikely(is_hugetlb_entry_hwpoisoned(entry))) { > - bool uffd_wp = huge_pte_uffd_wp(entry); > - > - if (!userfaultfd_wp(dst_vma) && uffd_wp) > + if (!userfaultfd_wp(dst_vma)) > entry = huge_pte_clear_uffd_wp(entry); > set_huge_pte_at(dst, addr, dst_pte, entry); > } else if (unlikely(is_hugetlb_entry_migration(entry))) { > swp_entry_t swp_entry = pte_to_swp_entry(entry); > - bool uffd_wp = huge_pte_uffd_wp(entry); > > if (!is_readable_migration_entry(swp_entry) && cow) { > /* > @@ -5049,11 +5050,12 @@ int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src, > swp_entry = make_readable_migration_entry( > swp_offset(swp_entry)); > entry = swp_entry_to_pte(swp_entry); > - if (userfaultfd_wp(src_vma) && uffd_wp) > - entry = huge_pte_mkuffd_wp(entry); > + if (userfaultfd_wp(src_vma) && > + pte_swp_uffd_wp(entry)) > + entry = pte_swp_mkuffd_wp(entry); > set_huge_pte_at(src, addr, src_pte, entry); > } > - if (!userfaultfd_wp(dst_vma) && uffd_wp) > + if (!userfaultfd_wp(dst_vma)) > entry = huge_pte_clear_uffd_wp(entry); > set_huge_pte_at(dst, addr, dst_pte, entry); > } else if (unlikely(is_pte_marker(entry))) { > @@ -5114,7 +5116,8 @@ int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src, > /* huge_ptep of dst_pte won't change as in child */ > goto again; > } > - hugetlb_install_folio(dst_vma, dst_pte, addr, new_folio); > + hugetlb_install_folio(dst_vma, dst_pte, addr, > + new_folio, src_pte_old); > spin_unlock(src_ptl); > spin_unlock(dst_ptl); > continue; > @@ -5132,6 +5135,9 @@ int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src, > entry = huge_pte_wrprotect(entry); > } > > + if (!userfaultfd_wp(dst_vma)) > + entry = huge_pte_clear_uffd_wp(entry); > + > set_huge_pte_at(dst, addr, dst_pte, entry); > hugetlb_count_add(npages, dst); > } LGTM Reviewed-by: David Hildenbrand -- Thanks, David / dhildenb