From nobody Wed Feb 11 08:11:52 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4B61AC76188 for ; Wed, 5 Apr 2023 15:52:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239107AbjDEPw2 (ORCPT ); Wed, 5 Apr 2023 11:52:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38964 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239133AbjDEPwP (ORCPT ); Wed, 5 Apr 2023 11:52:15 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4999965AB for ; Wed, 5 Apr 2023 08:51:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1680709884; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=dX5bBmfEsfxuoyfhYLC4nRt+vkdDVDFdF9t4fgBK6nc=; b=PRPjKAVx90wyO8giVwWrjThIbjjPz/PTx1A+ATSVS9SetOEfoXgBHJEoTb/XuoYZnTCuEj +WpFuKwTo178qoyGGidG3Nlc9K4MwhaMhzD3KCX2IRk8nF97olydd9OEVhPBReVw/cXWEd oMxLWwy2vlPVt5OkuPlWfvxFsLacEh8= Received: from mail-qt1-f197.google.com (mail-qt1-f197.google.com [209.85.160.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-564-yIScEAaOPuieDHHVXPlBqg-1; Wed, 05 Apr 2023 11:51:23 -0400 X-MC-Unique: yIScEAaOPuieDHHVXPlBqg-1 Received: by mail-qt1-f197.google.com with SMTP id d75a77b69052e-3e2daffa0d4so12288731cf.0 for ; Wed, 05 Apr 2023 08:51:22 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680709882; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=dX5bBmfEsfxuoyfhYLC4nRt+vkdDVDFdF9t4fgBK6nc=; b=7L6dmqYXgknIQ3k4GhKECULUj6SRLvcb7rwvMO+Gwba3F9zAQRvTipNnEyHxYGUZ+H 7RO/G9G4Y5yP+niXF/8bNZlYdk0oNrbrGgtIgScOMQmlDePiEBUT59a52Psr5KUYWTwt cqOZ1jtEaaLJ3UG1stHMpM8sobaHDw3Gc6V8J0n80xOb7GDh+dE101quIcrFu4rNpRBq DPMGHpRi5TvthTxIoNBFuL2DnSJFfBrgkBifXkKzin1lgemDBMvsi3G7sS5OAfslHhSA v3iGHbMYHn0F0FXSgqJOhh0VGGW9Ii2ZWgzerTm8Q1dIOPBgZN4Wz2dz8ur/gqM/uDUS H70g== X-Gm-Message-State: AAQBX9c4LDI+xg80Z0xjwglp4r8LCCFvk/NUL5m/14H5XU08swOKlU/E sFtqsqUsCZ/W1ZZs73vFb3CFdKsqMAd/lNorhUK6yB/27DGUaT2GHBBRSsdg4O9PK/T2B6zfDNw 38zdoOmO1k2l6IPMIT78/Stvv X-Received: by 2002:a05:622a:1a24:b0:3e6:707e:d3c2 with SMTP id f36-20020a05622a1a2400b003e6707ed3c2mr6579972qtb.0.1680709882538; Wed, 05 Apr 2023 08:51:22 -0700 (PDT) X-Google-Smtp-Source: AKy350aQwlmz/B2rFUoV+ynxi7aLMiwTeZZ+VAdvhVh5mZQhiM9KEBDhUFK1/5/JAnwKKQvIr4jnNQ== X-Received: by 2002:a05:622a:1a24:b0:3e6:707e:d3c2 with SMTP id f36-20020a05622a1a2400b003e6707ed3c2mr6579938qtb.0.1680709882192; Wed, 05 Apr 2023 08:51:22 -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 21-20020a370415000000b0074683c45f6csm4538557qke.1.2023.04.05.08.51.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Apr 2023 08:51:21 -0700 (PDT) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Axel Rasmussen , Nadav Amit , David Hildenbrand , Andrew Morton , peterx@redhat.com, Andrea Arcangeli , Mike Rapoport , Yang Shi , linux-stable Subject: [PATCH] mm/khugepaged: Check again on anon uffd-wp during isolation Date: Wed, 5 Apr 2023 11:51:20 -0400 Message-Id: <20230405155120.3608140-1-peterx@redhat.com> X-Mailer: git-send-email 2.39.1 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" 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 Reviewed-by: David Hildenbrand --- 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 =3D SCAN_PTE_NON_PRESENT; goto out; } + if (pte_uffd_wp(pteval)) { + result =3D SCAN_PTE_UFFD_WP; + goto out; + } page =3D vm_normal_page(vma, address, pteval); if (unlikely(!page) || unlikely(is_zone_device_page(page))) { result =3D SCAN_PAGE_NULL; --=20 2.39.1