Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp905535rwl; Wed, 5 Apr 2023 09:10:43 -0700 (PDT) X-Google-Smtp-Source: AKy350bJiqxdGGlEeM1erY8YIXF1j3hRzqZfIK7Gcju3CSQ0HpIHGPHpb4ri6Fg1xB2T9pkR4kNI X-Received: by 2002:a05:6a20:6aa9:b0:d9:e45d:95da with SMTP id bi41-20020a056a206aa900b000d9e45d95damr5661220pzb.20.1680711043687; Wed, 05 Apr 2023 09:10:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680711043; cv=none; d=google.com; s=arc-20160816; b=C9Wgqef7MzZC+rjIYZ7nVcFSvjpLy1v0wh1jOuLPmklzNrx/J/mL+Zjj0PuoTs03eB wK7wWdDBuWpG+WVUFP3XrTHbxG4PrhLSDxNrSfYsWw7CTxY8OKlJ3c8a4gWuZiNdhrdb kaSPlJBdDh1aH90aJy2mjVmRS+QGsiYromZbS5vtKuBKb7WMITt/T30fi/H/rHWNtQjz BQpX+wgEZPuxhpsvtNBjygComj8ShHg+c2uGGL4J3HxwL2tgKLzAHDXOR1h8GdlwPesk OI0aXVJKYRxByY3pOr3AC326V0XlqtXUlKQqN10Oz9JO6lnYqCrmTvsKHGw+WE5BOSip nwGw== 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=vp3w4ancoxCL4bBUeQO/Nr+Lv6anEYF8uurUtTzkQO0=; b=WAnG6T9hweccxHwXKfYE1BpjKcnLuYjRPXRmbLqpH8KK3/50wUxAgg5we1n75DYWQT slxvipxzcuge72LZcMs5lApgtnSYwhYLfhKuIiAEJMjeS4QiI6LfcbfijSKUx3Jq3Gf1 +p/ucmflduUTkbjKGIcFM50jPOonx8C922ghpfCeW1IS/oBY7t7tMemdTOP/BGNGHu9R FV5Pbfz/5igLBXN5SobYoOwq6JcUJE7syghRflXl3VyZLcGHxeizsscGCMDFEbkR/UjQ RZ0FQP0kZGnltoteiArUYuO4oEAOcRw7gjbUu1XGJcMniaxcP4TcLvMXe5SLMJgJ3usO 5eog== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="D1SD/LFI"; 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 c20-20020a6566d4000000b0050fabb82891si12732751pgw.255.2023.04.05.09.10.31; Wed, 05 Apr 2023 09:10:43 -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="D1SD/LFI"; 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 S238896AbjDEPzl (ORCPT + 99 others); Wed, 5 Apr 2023 11:55:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45834 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238791AbjDEPzj (ORCPT ); Wed, 5 Apr 2023 11:55:39 -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 467ADC0 for ; Wed, 5 Apr 2023 08:54:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1680710091; 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=vp3w4ancoxCL4bBUeQO/Nr+Lv6anEYF8uurUtTzkQO0=; b=D1SD/LFI/xktYCHzgJmeRXgkhrOxW6lB9GWaAhcQpdIMX7TDECoEYsGc71nwYj1bDvY5u7 iqle/KRZRb7Lt0NyRPgQoULjMl2TQCENTnYlZ86D4WjrzcjADVo9CEi16BElR9FIbYLMFD dtZPozeXslyN37kWkBXovycUFfWIp+U= Received: from mail-wr1-f69.google.com (mail-wr1-f69.google.com [209.85.221.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-359-ZM3c5hhPPuaEz2D70lTdGA-1; Wed, 05 Apr 2023 11:54:50 -0400 X-MC-Unique: ZM3c5hhPPuaEz2D70lTdGA-1 Received: by mail-wr1-f69.google.com with SMTP id m4-20020adfa3c4000000b002e75158e632so1857142wrb.17 for ; Wed, 05 Apr 2023 08:54:49 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680710089; 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=vp3w4ancoxCL4bBUeQO/Nr+Lv6anEYF8uurUtTzkQO0=; b=O8RtgG0p5TGCQnA7jv+21aRpPLoCxgtIT99VTq4qeelQSItgZfAmsdJYM5jmyUZ1j2 RlbZZEWOZuQ34jXrZKwK+g0M7bNfOqbmOenD+iDfhfwg4bkeBdkZDkARkoQ9Ou6P/9Gp 6Fi0wSglsJhFstc1S3/Ib5mdSV1B3Mb0ur9N2QU1GBG4FrCNEJ8b+m2Pe01gN4ZXQWLD Zdl9VjEY4Nr0RdRfyFg5g9PVrQA6FAmMR65vzLDRxpal0nSHw9Uc6lop9tuq4BYDiDxj omkdBYs3QJYTBm+dS9BdBqovAtOVV8iYAXJRWcRAhNCCCPm+jBO3g98/smVKtUYVa6SV 5Nww== X-Gm-Message-State: AAQBX9fa19LzWXJkmTw0/8E+okbOy5fkD6eaS/wOJEIqcUHXTPjRaE1B HU441I2wgshvLmLEhPy5h962/65OvLAhEscHSF49+t/9t+pmRiztU6fmL+s/4oj3Shvj1S/wCAl Il+O+NymUJIiAInqSqJwubdLp X-Received: by 2002:adf:e0c3:0:b0:2cf:e747:b0d4 with SMTP id m3-20020adfe0c3000000b002cfe747b0d4mr4423521wri.40.1680710088969; Wed, 05 Apr 2023 08:54:48 -0700 (PDT) X-Received: by 2002:adf:e0c3:0:b0:2cf:e747:b0d4 with SMTP id m3-20020adfe0c3000000b002cfe747b0d4mr4423506wri.40.1680710088611; Wed, 05 Apr 2023 08:54:48 -0700 (PDT) Received: from ?IPV6:2003:cb:c703:d00:ca74:d9ea:11e0:dfb? (p200300cbc7030d00ca74d9ea11e00dfb.dip0.t-ipconnect.de. [2003:cb:c703:d00:ca74:d9ea:11e0:dfb]) by smtp.gmail.com with ESMTPSA id c1-20020adfef41000000b002d322b9a7f5sm15323873wrp.88.2023.04.05.08.54.47 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 05 Apr 2023 08:54:48 -0700 (PDT) Message-ID: <19dad66f-4ec8-358c-df7f-35481f855c81@redhat.com> Date: Wed, 5 Apr 2023 17:54:46 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.9.1 Subject: Re: [PATCH] mm/khugepaged: Check again on anon uffd-wp during isolation Content-Language: en-US To: Peter Xu , linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Axel Rasmussen , Nadav Amit , Andrew Morton , Andrea Arcangeli , Mike Rapoport , Yang Shi , linux-stable References: <20230405155120.3608140-1-peterx@redhat.com> From: David Hildenbrand Organization: Red Hat In-Reply-To: <20230405155120.3608140-1-peterx@redhat.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-1.6 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 05.04.23 17:51, Peter Xu wrote: > Khugepaged collapse an anonymous thp in two rounds of scans. The 2nd round > done in __collapse_huge_page_isolate() after hpage_collapse_scan_pmd(), > during which all the locks will be released temporarily. It means the > pgtable can change during this phase before 2nd round starts. > > It's logically possible some ptes got wr-protected during this phase, and > we can errornously collapse a thp without noticing some ptes are > wr-protected by userfault. e1e267c7928f wanted to avoid it but it only did > that for the 1st phase, not the 2nd phase. > > Since __collapse_huge_page_isolate() happens after a round of small page > swapins, we don't need to worry on any !present ptes - if it existed > khugepaged will already bail out. So we only need to check present ptes > with uffd-wp bit set there. > > This is something I found only but never had a reproducer, I thought it was > one caused a bug in Muhammad's recent pagemap new ioctl work, but it turns > out it's not the cause of that but an userspace bug. However this seems to > still be a real bug even with a very small race window, still worth to have > it fixed and copy stable. > > Cc: linux-stable > Fixes: e1e267c7928f ("khugepaged: skip collapse if uffd-wp detected") > Signed-off-by: Peter Xu > --- > mm/khugepaged.c | 4 ++++ > 1 file changed, 4 insertions(+) > > diff --git a/mm/khugepaged.c b/mm/khugepaged.c > index a19aa140fd52..42ac93b4bd87 100644 > --- a/mm/khugepaged.c > +++ b/mm/khugepaged.c > @@ -575,6 +575,10 @@ static int __collapse_huge_page_isolate(struct vm_area_struct *vma, > result = SCAN_PTE_NON_PRESENT; > goto out; > } > + if (pte_uffd_wp(pteval)) { > + result = SCAN_PTE_UFFD_WP; > + goto out; > + } > page = vm_normal_page(vma, address, pteval); > if (unlikely(!page) || unlikely(is_zone_device_page(page))) { > result = SCAN_PAGE_NULL; Yes, I agree that would be a small race where it could happen. Reviewed-by: David Hildenbrand -- Thanks, David / dhildenb