From nobody Wed Dec 17 18:59:47 2025 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 EE8D5C77B72 for ; Mon, 17 Apr 2023 19:54:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230135AbjDQTyc (ORCPT ); Mon, 17 Apr 2023 15:54:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33228 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229967AbjDQTy2 (ORCPT ); Mon, 17 Apr 2023 15:54:28 -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 58BF8768A for ; Mon, 17 Apr 2023 12:53:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1681761204; 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: in-reply-to:in-reply-to:references:references; bh=4kH7JNUmkrK1TIcnPTAjFCorjkRaA4Lvx3pRbnAdGF0=; b=DhpRqkZRL+PyubNkgjQLYvdqNoS8PA/GFNH+eqtPKZ7vnOaryAbdXhAgmuI0MMwiL18wAS 2yrkhugf+gYB+50wyWHaiWwWr3GWzRudfr1jQhQZUsse0zp3tdGtEL4sl0SCm7NUwySJOH Ache4MCxJ3IbCz3iR8qk6FB9rDhveiE= 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-374-bZNAUa01Og6OBn0MA0NVDQ-1; Mon, 17 Apr 2023 15:53:23 -0400 X-MC-Unique: bZNAUa01Og6OBn0MA0NVDQ-1 Received: by mail-qt1-f197.google.com with SMTP id d75a77b69052e-3ef3116d1dcso1937081cf.1 for ; Mon, 17 Apr 2023 12:53:23 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681761202; x=1684353202; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=4kH7JNUmkrK1TIcnPTAjFCorjkRaA4Lvx3pRbnAdGF0=; b=erSSpw18DqBkI7lPsT0vjPRhltyoosVxEY341273gKeBXc+SBYkvL9rI+OMy6Wywsk GpiXhHzXzjrB5Q78hfczXuFu1wzcNQabdvzd6RdOUiQtr0aWqJxuoyD4qrZSv4WXK2ML zYeDwCLeQ+9tLBYm4Pbr+pT60q8RHgHcCqzZz8HGfjnGwSVgus//ULkOKilFoXS64Zcv RWWfy3tiaUzowAkzNjj6yoOjOdYFo3G7okiAvEiDfZ4SUh5aeBtkrLd39uFQnRI8iWQZ ZwJ+kSkUwngaR83tkh/dBH3TeFxS6UF4gcaMfEWOZHo+xUIhGevde/1/OSyewsfaP8+r UwlQ== X-Gm-Message-State: AAQBX9fVKVFvdZD7+PnlufjxxSN841QHpG/o/8QHvqMXY+Gr76d3VZ5L X0n7gH/GnQudYqmKEwj0mM3CQHv0S7MME1DpM6kJvd6CAi4wxfXZlO8V1MFC1+4oO6CrbGmRw2Y ET4leZungotSeVpEnuUeq9JD4onHJqeSwA+Rupkda1KrsZh0MBMB0RjC+LYgs9EMI+hVWT8hZQm UdpuuCIQ== X-Received: by 2002:a05:622a:19a4:b0:3e8:e986:b20b with SMTP id u36-20020a05622a19a400b003e8e986b20bmr17654479qtc.6.1681761202064; Mon, 17 Apr 2023 12:53:22 -0700 (PDT) X-Google-Smtp-Source: AKy350ZImFWbK1bH7N466hKMggFlbQGVKGvl3FHNzHOn9K4F9JpZRZ/yhdls5xIATBP5zpu3FcayoQ== X-Received: by 2002:a05:622a:19a4:b0:3e8:e986:b20b with SMTP id u36-20020a05622a19a400b003e8e986b20bmr17654449qtc.6.1681761201723; Mon, 17 Apr 2023 12:53:21 -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 r17-20020ac87ef1000000b003edfb5d7637sm1731278qtc.73.2023.04.17.12.53.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Apr 2023 12:53:21 -0700 (PDT) From: Peter Xu To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Mike Kravetz , Andrea Arcangeli , =?UTF-8?q?Mika=20Penttil=C3=A4?= , Andrew Morton , peterx@redhat.com, Axel Rasmussen , Nadav Amit , David Hildenbrand , linux-stable Subject: [PATCH v2 1/6] mm/hugetlb: Fix uffd-wp during fork() Date: Mon, 17 Apr 2023 15:53:12 -0400 Message-Id: <20230417195317.898696-2-peterx@redhat.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230417195317.898696-1-peterx@redhat.com> References: <20230417195317.898696-1-peterx@redhat.com> 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" 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()") Reviewed-by: David Hildenbrand Signed-off-by: Peter Xu --- mm/hugetlb.c | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index f16b25b1a6b9..0213efaf31be 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -4953,11 +4953,15 @@ static bool is_hugetlb_entry_hwpoisoned(pte_t pte) =20 static void hugetlb_install_folio(struct vm_area_struct *vma, pte_t *ptep, unsigned lo= ng addr, - struct folio *new_folio) + struct folio *new_folio, pte_t old) { + pte_t newpte =3D 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->pa= ge, 1)); + if (userfaultfd_wp(vma) && huge_pte_uffd_wp(old)) + newpte =3D 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,12 @@ int copy_hugetlb_page_range(struct mm_struct *dst, = struct mm_struct *src, */ ; } else if (unlikely(is_hugetlb_entry_hwpoisoned(entry))) { - bool uffd_wp =3D huge_pte_uffd_wp(entry); - - if (!userfaultfd_wp(dst_vma) && uffd_wp) + if (!userfaultfd_wp(dst_vma)) entry =3D 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 =3D pte_to_swp_entry(entry); - bool uffd_wp =3D huge_pte_uffd_wp(entry); + bool uffd_wp =3D pte_swp_uffd_wp(entry); =20 if (!is_readable_migration_entry(swp_entry) && cow) { /* @@ -5050,10 +5052,10 @@ int copy_hugetlb_page_range(struct mm_struct *dst, = struct mm_struct *src, swp_offset(swp_entry)); entry =3D swp_entry_to_pte(swp_entry); if (userfaultfd_wp(src_vma) && uffd_wp) - entry =3D huge_pte_mkuffd_wp(entry); + entry =3D 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 =3D 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, st= ruct 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, st= ruct mm_struct *src, entry =3D huge_pte_wrprotect(entry); } =20 + if (!userfaultfd_wp(dst_vma)) + entry =3D huge_pte_clear_uffd_wp(entry); + set_huge_pte_at(dst, addr, dst_pte, entry); hugetlb_count_add(npages, dst); } --=20 2.39.1 From nobody Wed Dec 17 18:59:47 2025 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 DE384C77B76 for ; Mon, 17 Apr 2023 19:54:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230143AbjDQTye (ORCPT ); Mon, 17 Apr 2023 15:54:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33252 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229738AbjDQTy3 (ORCPT ); Mon, 17 Apr 2023 15:54:29 -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 73FD4E6 for ; Mon, 17 Apr 2023 12:53:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1681761206; 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: in-reply-to:in-reply-to:references:references; bh=ZZajLRXbE1gHSG0/rWcJydl4VAkwntQbZfH27HHj2cI=; b=UERaqskK3tXv/LdQiirxN4czmv8yGqF3TI6D2EFVZHjOnUfZqY4oLMb9TYlCkxFVu3B16b VvF3rQL4NGcNUE7LvuBbtSlVkQ4+rBVF0tM2RJNxbu12VD7w1pNHqcGEXN4Zs3pwVd3ysi n54kKzWAELzAIITExzh8MVQhe3jKEWg= 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-399-pYY-GGELN5-Ng1fR-ZQUXQ-1; Mon, 17 Apr 2023 15:53:25 -0400 X-MC-Unique: pYY-GGELN5-Ng1fR-ZQUXQ-1 Received: by mail-qt1-f197.google.com with SMTP id d75a77b69052e-3ed767b30easo1904911cf.1 for ; Mon, 17 Apr 2023 12:53:25 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681761204; x=1684353204; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ZZajLRXbE1gHSG0/rWcJydl4VAkwntQbZfH27HHj2cI=; b=NWBMYm5aHIsxlBp8ondzM8isHbwfygMoEuFJoBCXJIQP5vOamVzxImXe0fNcao6JiO eGKycxrGE2h2fWk+M3c9ylu2rpiyHwJZ91b6SkW9gq8raCQmPTRCqtdiHw+QJj5IdomV OxRFxkLKmS+FG+4ZdsQcGXWyorEPgm4LYEQuL5oCEpJclMsywLUN1Jm7focSC2Ce75UC AjrHvKlIlRICUX9wVf0g1LUVNN3+7vhOy1fQexnKhXhvJnqbEzr96JErqFC35N8tqR15 Y/pUKByWkQu4aVnSTnS8kM+DKcKrdkX2mSA7lioM4IbZt7PJ3qnP70eMCXlnLzbvWBBr lJEw== X-Gm-Message-State: AAQBX9c8iTF+bYI72lGcMjUHzRN2lLsG0JXx/mU8hWEus9Fza7BrCyAC ZUPxJVlevVBC8mOZUQrZ+J3Bt4GhD/aMs9tqnm+kH/GaTZT0KJx80Rdb1zJV0CRj6XXEJ3jYflc 14REu41e6BbGNqZU0dQURRh7D4LlrO0hxgwwON+no6W8XXRffY+CaRaQre5U5YX2QOUB/rTlLG8 wTPawbjA== X-Received: by 2002:a05:622a:1a9b:b0:3ea:ef5:5b8c with SMTP id s27-20020a05622a1a9b00b003ea0ef55b8cmr19155691qtc.3.1681761204345; Mon, 17 Apr 2023 12:53:24 -0700 (PDT) X-Google-Smtp-Source: AKy350aPbpBAnSBo8hIMWFwYiZl6fBgvrueoUnmaQeBSdZmgAZBXgdsikL50SEQY0Q9p7+B82l1+8Q== X-Received: by 2002:a05:622a:1a9b:b0:3ea:ef5:5b8c with SMTP id s27-20020a05622a1a9b00b003ea0ef55b8cmr19155663qtc.3.1681761204064; Mon, 17 Apr 2023 12:53:24 -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 r17-20020ac87ef1000000b003edfb5d7637sm1731278qtc.73.2023.04.17.12.53.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Apr 2023 12:53:23 -0700 (PDT) From: Peter Xu To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Mike Kravetz , Andrea Arcangeli , =?UTF-8?q?Mika=20Penttil=C3=A4?= , Andrew Morton , peterx@redhat.com, Axel Rasmussen , Nadav Amit , David Hildenbrand , linux-stable Subject: [PATCH v2 2/6] mm/hugetlb: Fix uffd-wp bit lost when unsharing happens Date: Mon, 17 Apr 2023 15:53:13 -0400 Message-Id: <20230417195317.898696-3-peterx@redhat.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230417195317.898696-1-peterx@redhat.com> References: <20230417195317.898696-1-peterx@redhat.com> 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" When we try to unshare a pinned page for a private hugetlb, uffd-wp bit can get lost during unsharing. Fix it by carrying it over. This should be very rare, only if an unsharing happened on a private hugetlb page with uffd-wp protected (e.g. in a child which shares the same page with parent with UFFD_FEATURE_EVENT_FORK enabled). Cc: linux-stable Fixes: 166f3ecc0daf ("mm/hugetlb: hook page faults for uffd write protectio= n") Reported-by: Mike Kravetz Reviewed-by: David Hildenbrand Reviewed-by: Mike Kravetz Signed-off-by: Peter Xu --- mm/hugetlb.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 0213efaf31be..cd3a9d8f4b70 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -5637,13 +5637,16 @@ static vm_fault_t hugetlb_wp(struct mm_struct *mm, = struct vm_area_struct *vma, spin_lock(ptl); ptep =3D hugetlb_walk(vma, haddr, huge_page_size(h)); if (likely(ptep && pte_same(huge_ptep_get(ptep), pte))) { + pte_t newpte =3D make_huge_pte(vma, &new_folio->page, !unshare); + /* Break COW or unshare */ huge_ptep_clear_flush(vma, haddr, ptep); mmu_notifier_invalidate_range(mm, range.start, range.end); page_remove_rmap(old_page, vma, true); hugepage_add_new_anon_rmap(new_folio, vma, haddr); - set_huge_pte_at(mm, haddr, ptep, - make_huge_pte(vma, &new_folio->page, !unshare)); + if (huge_pte_uffd_wp(pte)) + newpte =3D huge_pte_mkuffd_wp(newpte); + set_huge_pte_at(mm, haddr, ptep, newpte); folio_set_hugetlb_migratable(new_folio); /* Make the old page be freed below */ new_folio =3D page_folio(old_page); --=20 2.39.1 From nobody Wed Dec 17 18:59:47 2025 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 9CD0EC77B72 for ; Mon, 17 Apr 2023 19:54:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230164AbjDQTyz (ORCPT ); Mon, 17 Apr 2023 15:54:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33302 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230453AbjDQTyo (ORCPT ); Mon, 17 Apr 2023 15:54:44 -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 3C40B49DE for ; Mon, 17 Apr 2023 12:53:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1681761209; 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: in-reply-to:in-reply-to:references:references; bh=VlJAJroC0qfUjlayNMIBwxZydfuRD55bTGjabDZPAD4=; b=ZP/rrgSoE8H9BWi3SPbjCRHnruOYAOKmECWWGBS7em4hapH1bU7Oqr/XeGCDTbsTf+iJ8d v3YYFvtsDL2Z5avJZYs/N1aJuNkWGuEMNlPsZmPnkZRywhVuDHjU//+aAZY9RHocslZfQL Hb4nFryoUJ2xGvroq0l5JKHQ1fsSRi0= Received: from mail-qt1-f198.google.com (mail-qt1-f198.google.com [209.85.160.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-282-kbxCxsszMya_WNCi-f4CHQ-1; Mon, 17 Apr 2023 15:53:28 -0400 X-MC-Unique: kbxCxsszMya_WNCi-f4CHQ-1 Received: by mail-qt1-f198.google.com with SMTP id d75a77b69052e-3ed767b30easo1905071cf.1 for ; Mon, 17 Apr 2023 12:53:28 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681761207; x=1684353207; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=VlJAJroC0qfUjlayNMIBwxZydfuRD55bTGjabDZPAD4=; b=PEpzUxSrViaNRRpWeADN++C5Yj7jF5lN2MbPOyE/58Yy74CJWc6I3QqFDsrtK8H40l 16FNG62byDuMgl6bMa7vs30ICmcriVrGaeYe7mc8gWCf56L7TbbDNDBbI2/Soorb0Rgb kZjTKjGxtDrZhozfoK7jvw9BAPNvpAtML9kPESwETQx/8XsZHizLbqjSH7bfIP6sn6K4 QAfzB7/5rJZJQL021aWy/NUb1S1LTDyxfMVTXTC4LQW/hTbh+CSJkb5fEvp6HeC/pqDi pfF7OHd0jmjKLXCsyWmnxOwMY6CnRaASwdz2c6tiYgKbjbkiIlzWv0guCXMV6TVzZyRd W5NQ== X-Gm-Message-State: AAQBX9cecB4SpY42T/z5V2sN5dSRKCs7donLG+cQqvJXHyVwKiadjVdi mYMPE8+azsOgR7XPJdEsQK5PkP36dy64Pe5EYdzXLmqcfgLc+xzZLbLggfUQgYsEZfSuwSsmlEF YyGswNywh00Mb2Xcy8/zflQYQMfVzX65O82yPDkJSOCUAgXlXVLSVHDJvz9reXOTQhCaSWvaQAz ey6aeANA== X-Received: by 2002:ac8:4e43:0:b0:3bf:cf77:a861 with SMTP id e3-20020ac84e43000000b003bfcf77a861mr19130150qtw.4.1681761206873; Mon, 17 Apr 2023 12:53:26 -0700 (PDT) X-Google-Smtp-Source: AKy350atiTLtabqgcPaZB1CQI3pkzcoQyHjGkW0AJzAmgZF9tCzczfoqxlZpXO4VXi+keofELA+BDw== X-Received: by 2002:ac8:4e43:0:b0:3bf:cf77:a861 with SMTP id e3-20020ac84e43000000b003bfcf77a861mr19130115qtw.4.1681761206528; Mon, 17 Apr 2023 12:53:26 -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 r17-20020ac87ef1000000b003edfb5d7637sm1731278qtc.73.2023.04.17.12.53.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Apr 2023 12:53:25 -0700 (PDT) From: Peter Xu To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Mike Kravetz , Andrea Arcangeli , =?UTF-8?q?Mika=20Penttil=C3=A4?= , Andrew Morton , peterx@redhat.com, Axel Rasmussen , Nadav Amit , David Hildenbrand Subject: [PATCH v2 3/6] selftests/mm: Add a few options for uffd-unit-test Date: Mon, 17 Apr 2023 15:53:14 -0400 Message-Id: <20230417195317.898696-4-peterx@redhat.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230417195317.898696-1-peterx@redhat.com> References: <20230417195317.898696-1-peterx@redhat.com> 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" Namely: "-f": add a wildcard filter for tests to run "-l": list tests rather than running any "-h": help msg Signed-off-by: Peter Xu Reviewed-by: David Hildenbrand --- tools/testing/selftests/mm/uffd-unit-tests.c | 52 +++++++++++++++++--- 1 file changed, 45 insertions(+), 7 deletions(-) diff --git a/tools/testing/selftests/mm/uffd-unit-tests.c b/tools/testing/s= elftests/mm/uffd-unit-tests.c index d871bf732e62..452ca05a829d 100644 --- a/tools/testing/selftests/mm/uffd-unit-tests.c +++ b/tools/testing/selftests/mm/uffd-unit-tests.c @@ -909,28 +909,65 @@ uffd_test_case_t uffd_tests[] =3D { }, }; =20 +static void usage(const char *prog) +{ + printf("usage: %s [-f TESTNAME]\n", prog); + puts(""); + puts(" -f: test name to filter (e.g., event)"); + puts(" -h: show the help msg"); + puts(" -l: list tests only"); + puts(""); + exit(KSFT_FAIL); +} + int main(int argc, char *argv[]) { int n_tests =3D sizeof(uffd_tests) / sizeof(uffd_test_case_t); int n_mems =3D sizeof(mem_types) / sizeof(mem_type_t); + const char *test_filter =3D NULL; + bool list_only =3D false; uffd_test_case_t *test; mem_type_t *mem_type; uffd_test_args_t args; char test_name[128]; const char *errmsg; - int has_uffd; + int has_uffd, opt; int i, j; =20 - has_uffd =3D test_uffd_api(false); - has_uffd |=3D test_uffd_api(true); + while ((opt =3D getopt(argc, argv, "f:hl")) !=3D -1) { + switch (opt) { + case 'f': + test_filter =3D optarg; + break; + case 'l': + list_only =3D true; + break; + case 'h': + default: + /* Unknown */ + usage(argv[0]); + break; + } + } + + if (!test_filter && !list_only) { + has_uffd =3D test_uffd_api(false); + has_uffd |=3D test_uffd_api(true); =20 - if (!has_uffd) { - printf("Userfaultfd not supported or unprivileged, skip all tests\n"); - exit(KSFT_SKIP); + if (!has_uffd) { + printf("Userfaultfd not supported or unprivileged, skip all tests\n"); + exit(KSFT_SKIP); + } } =20 for (i =3D 0; i < n_tests; i++) { test =3D &uffd_tests[i]; + if (test_filter && !strstr(test->name, test_filter)) + continue; + if (list_only) { + printf("%s\n", test->name); + continue; + } for (j =3D 0; j < n_mems; j++) { mem_type =3D &mem_types[j]; if (!(test->mem_targets & mem_type->mem_flag)) @@ -952,7 +989,8 @@ int main(int argc, char *argv[]) } } =20 - uffd_test_report(); + if (!list_only) + uffd_test_report(); =20 return ksft_get_fail_cnt() ? KSFT_FAIL : KSFT_PASS; } --=20 2.39.1 From nobody Wed Dec 17 18:59:47 2025 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 4EEB6C77B78 for ; Mon, 17 Apr 2023 19:54:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230211AbjDQTyx (ORCPT ); Mon, 17 Apr 2023 15:54:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33350 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230454AbjDQTyo (ORCPT ); Mon, 17 Apr 2023 15:54:44 -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 BF8256EB1 for ; Mon, 17 Apr 2023 12:53:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1681761211; 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: in-reply-to:in-reply-to:references:references; bh=PiB4O8c7FzfgLpxlGuxco4WipLi6p7uHP+fG5udokS8=; b=DlGuFqq5mO+6tNfbVSNcs4mL4/S420Z3QlL7DGSbj+JCAnmzmEY9NcQZfrvU14oF9BBLP2 cudzb+lH1PpxKa7Un1T0KAJ/KCqI+wUfmDEFxgyuNc3dZRuQYT/snCCqPBobaHDGQTQ2N7 jboM8HcVxVgs71k/fG1AI2GcM58Fs+M= Received: from mail-qk1-f197.google.com (mail-qk1-f197.google.com [209.85.222.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-400-yqvIume8OiyQWcc_AAwebw-1; Mon, 17 Apr 2023 15:53:30 -0400 X-MC-Unique: yqvIume8OiyQWcc_AAwebw-1 Received: by mail-qk1-f197.google.com with SMTP id af79cd13be357-74acb477be6so72178385a.1 for ; Mon, 17 Apr 2023 12:53:30 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681761209; x=1684353209; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=PiB4O8c7FzfgLpxlGuxco4WipLi6p7uHP+fG5udokS8=; b=aDso02lrE/qXfmVC3GBa5CYS+LBzDDJmq7LjiSk6kyv66WVMRDGp/izxZRpeDSUGPO KYFfcoSFOAWgL0631VGA6jcR15LYJULTjVpJF+0efoEbwSO34D6nC8FznYzXEN4ILDvE aaL59sD3lqGrUbZHszwIVeDJK5QDYAL4Pi/irSHl/javwwa4OAqu0GqALzpF04hAZCWs Xk2VkVSdwwFjR6QLsccXI7eouuh90vSlduruGs4vYdXG6GUik8zVXwgQauk5Pi3CeFUE C2m/NLnKkaaD7o6MGPl6hbBuddEYkveHceI4ZhLRnlYBinkT/JNK69gVi2iwVpIzfS9L fQCw== X-Gm-Message-State: AAQBX9cltNvn3urTiEOhlh/kmK+yqWmvkGC/6hBgNa9n9ig/9zGmPWEa Xof6+aqOm2CiuY3X+3hkHrcLXrglWgDqRPIC94sOhMyH70+dfaoPfoiL2QQTsVHfG2wG6vkn6oq Nxyrnaw6tE5jrS5sD4+4lSo2pQ6iTChlIOACaNfTIvkPOOW/LFeeHjSH3oW76V9xa28ibz5d5nA Ae+OfsJw== X-Received: by 2002:a05:622a:1352:b0:3ef:37fa:e1d6 with SMTP id w18-20020a05622a135200b003ef37fae1d6mr1229514qtk.2.1681761209399; Mon, 17 Apr 2023 12:53:29 -0700 (PDT) X-Google-Smtp-Source: AKy350Y/Xr56P7wXtTgHgR9HpSg1x1E8ea69mUaTjl3roEaHjKyTAe98r0gM0V7aTgCT/jxwZH4gBA== X-Received: by 2002:a05:622a:1352:b0:3ef:37fa:e1d6 with SMTP id w18-20020a05622a135200b003ef37fae1d6mr1229477qtk.2.1681761209061; Mon, 17 Apr 2023 12:53:29 -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 r17-20020ac87ef1000000b003edfb5d7637sm1731278qtc.73.2023.04.17.12.53.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Apr 2023 12:53:28 -0700 (PDT) From: Peter Xu To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Mike Kravetz , Andrea Arcangeli , =?UTF-8?q?Mika=20Penttil=C3=A4?= , Andrew Morton , peterx@redhat.com, Axel Rasmussen , Nadav Amit , David Hildenbrand Subject: [PATCH v2 4/6] selftests/mm: Extend and rename uffd pagemap test Date: Mon, 17 Apr 2023 15:53:15 -0400 Message-Id: <20230417195317.898696-5-peterx@redhat.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230417195317.898696-1-peterx@redhat.com> References: <20230417195317.898696-1-peterx@redhat.com> 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" Extend it to all types of mem, meanwhile add one parallel test when EVENT_FORK is enabled, where uffd-wp bits should be persisted rather than dropped. Since at it, rename the test to "wp-fork" to better show what it means. Making the new test called "wp-fork-with-event". Before: Testing pagemap on anon... done After: Testing wp-fork on anon... done Testing wp-fork on shmem... done Testing wp-fork on shmem-private... done Testing wp-fork on hugetlb... done Testing wp-fork on hugetlb-private... done Testing wp-fork-with-event on anon... done Testing wp-fork-with-event on shmem... done Testing wp-fork-with-event on shmem-private... done Testing wp-fork-with-event on hugetlb... done Testing wp-fork-with-event on hugetlb-private... done Signed-off-by: Peter Xu --- tools/testing/selftests/mm/uffd-unit-tests.c | 130 +++++++++++++++---- 1 file changed, 106 insertions(+), 24 deletions(-) diff --git a/tools/testing/selftests/mm/uffd-unit-tests.c b/tools/testing/s= elftests/mm/uffd-unit-tests.c index 452ca05a829d..739fc4d30342 100644 --- a/tools/testing/selftests/mm/uffd-unit-tests.c +++ b/tools/testing/selftests/mm/uffd-unit-tests.c @@ -227,25 +227,65 @@ static int pagemap_open(void) err("pagemap uffd-wp bit error: 0x%"PRIx64, value); \ } while (0) =20 -static int pagemap_test_fork(bool present) +typedef struct { + int parent_uffd, child_uffd; +} fork_event_args; + +static void *fork_event_consumer(void *data) { - pid_t child =3D fork(); + fork_event_args *args =3D data; + struct uffd_msg msg =3D { 0 }; + + /* Read until a full msg received */ + while (uffd_read_msg(args->parent_uffd, &msg)); + + if (msg.event !=3D UFFD_EVENT_FORK) + err("wrong message: %u\n", msg.event); + + /* Just to be properly freed later */ + args->child_uffd =3D msg.arg.fork.ufd; + return NULL; +} + +static int pagemap_test_fork(int uffd, bool with_event) +{ + fork_event_args args =3D { .parent_uffd =3D uffd, .child_uffd =3D -1 }; + pthread_t thread; + pid_t child; uint64_t value; int fd, result; =20 + /* Prepare a thread to resolve EVENT_FORK */ + if (with_event) { + if (pthread_create(&thread, NULL, fork_event_consumer, &args)) + err("pthread_create()"); + } + + child =3D fork(); if (!child) { /* Open the pagemap fd of the child itself */ fd =3D pagemap_open(); value =3D pagemap_get_entry(fd, area_dst); /* - * After fork() uffd-wp bit should be gone as long as we're - * without UFFD_FEATURE_EVENT_FORK + * After fork(), we should handle uffd-wp bit differently: + * + * (1) when with EVENT_FORK, it should persist + * (2) when without EVENT_FORK, it should be dropped */ - pagemap_check_wp(value, false); + pagemap_check_wp(value, with_event); /* Succeed */ exit(0); } waitpid(child, &result, 0); + + if (with_event) { + if (pthread_join(thread, NULL)) + err("pthread_join()"); + if (args.child_uffd < 0) + err("Didn't receive child uffd"); + close(args.child_uffd); + } + return result; } =20 @@ -295,7 +335,8 @@ static void uffd_wp_unpopulated_test(uffd_test_args_t *= args) uffd_test_pass(); } =20 -static void uffd_pagemap_test(uffd_test_args_t *args) +static void uffd_wp_fork_test_common(uffd_test_args_t *args, + bool with_event) { int pagemap_fd; uint64_t value; @@ -311,23 +352,42 @@ static void uffd_pagemap_test(uffd_test_args_t *args) wp_range(uffd, (uint64_t)area_dst, page_size, true); value =3D pagemap_get_entry(pagemap_fd, area_dst); pagemap_check_wp(value, true); - /* Make sure uffd-wp bit dropped when fork */ - if (pagemap_test_fork(true)) - err("Detected stall uffd-wp bit in child"); - - /* Exclusive required or PAGEOUT won't work */ - if (!(value & PM_MMAP_EXCLUSIVE)) - err("multiple mapping detected: 0x%"PRIx64, value); + if (pagemap_test_fork(uffd, with_event)) { + uffd_test_fail("Detected %s uffd-wp bit in child in present pte", + with_event ? "missing" : "stall"); + goto out; + } =20 - if (madvise(area_dst, page_size, MADV_PAGEOUT)) - err("madvise(MADV_PAGEOUT) failed"); + /* + * This is an attempt for zapping the pgtable so as to test the + * markers. + * + * For private mappings, PAGEOUT will only work on exclusive ptes + * (PM_MMAP_EXCLUSIVE) which we should satisfy. + * + * For shared, PAGEOUT may not work. Use DONTNEED instead which + * plays a similar role of zapping (rather than freeing the page) + * to expose pte markers. + */ + if (args->mem_type->shared) { + if (madvise(area_dst, page_size, MADV_DONTNEED)) + err("MADV_DONTNEED"); + } else { + /* + * NOTE: ignore retval because private-hugetlb doesn't yet + * support swapping, so it could fail. + */ + madvise(area_dst, page_size, MADV_PAGEOUT); + } =20 /* Uffd-wp should persist even swapped out */ value =3D pagemap_get_entry(pagemap_fd, area_dst); pagemap_check_wp(value, true); - /* Make sure uffd-wp bit dropped when fork */ - if (pagemap_test_fork(false)) - err("Detected stall uffd-wp bit in child"); + if (pagemap_test_fork(uffd, with_event)) { + uffd_test_fail("Detected %s uffd-wp bit in child in zapped pte", + with_event ? "missing" : "stall"); + goto out; + } =20 /* Unprotect; this tests swap pte modifications */ wp_range(uffd, (uint64_t)area_dst, page_size, false); @@ -338,9 +398,21 @@ static void uffd_pagemap_test(uffd_test_args_t *args) *area_dst =3D 2; value =3D pagemap_get_entry(pagemap_fd, area_dst); pagemap_check_wp(value, false); - - close(pagemap_fd); uffd_test_pass(); +out: + if (uffd_unregister(uffd, area_dst, nr_pages * page_size)) + err("unregister failed"); + close(pagemap_fd); +} + +static void uffd_wp_fork_test(uffd_test_args_t *args) +{ + uffd_wp_fork_test_common(args, false); +} + +static void uffd_wp_fork_with_event_test(uffd_test_args_t *args) +{ + uffd_wp_fork_test_common(args, true); } =20 static void check_memory_contents(char *p) @@ -836,10 +908,20 @@ uffd_test_case_t uffd_tests[] =3D { .uffd_feature_required =3D 0, }, { - .name =3D "pagemap", - .uffd_fn =3D uffd_pagemap_test, - .mem_targets =3D MEM_ANON, - .uffd_feature_required =3D UFFD_FEATURE_PAGEFAULT_FLAG_WP, + .name =3D "wp-fork", + .uffd_fn =3D uffd_wp_fork_test, + .mem_targets =3D MEM_ALL, + .uffd_feature_required =3D UFFD_FEATURE_PAGEFAULT_FLAG_WP | + UFFD_FEATURE_WP_HUGETLBFS_SHMEM, + }, + { + .name =3D "wp-fork-with-event", + .uffd_fn =3D uffd_wp_fork_with_event_test, + .mem_targets =3D MEM_ALL, + .uffd_feature_required =3D UFFD_FEATURE_PAGEFAULT_FLAG_WP | + UFFD_FEATURE_WP_HUGETLBFS_SHMEM | + /* when set, child process should inherit uffd-wp bits */ + UFFD_FEATURE_EVENT_FORK, }, { .name =3D "wp-unpopulated", --=20 2.39.1 From nobody Wed Dec 17 18:59:47 2025 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 AC09EC77B72 for ; Mon, 17 Apr 2023 19:55:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230424AbjDQTzE (ORCPT ); Mon, 17 Apr 2023 15:55:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33436 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230090AbjDQTyw (ORCPT ); Mon, 17 Apr 2023 15:54:52 -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 29BB34232 for ; Mon, 17 Apr 2023 12:53:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1681761216; 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: in-reply-to:in-reply-to:references:references; bh=nL6ukD3uQsP0j8aiX/3IiND2lFTW26LLUmGTHQFycOk=; b=F9YY/XJrSlczL7wPBf0oYKvzQ6/0CTmM47PL0kpjrJA5VT4uH7MliYxXPsY4asfjYHHBXg cwLzc9paztU3g4zqOZKNDUzhk1Y94Sz8mpC2YtgWjmfRN9PjgLIuc/BN2PXSOTYZny8/qu 5p8+vUaDOzs/dad68w51uYtFZqazSJA= Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-126-sfuNc7wnN_WGDij7zcikxg-1; Mon, 17 Apr 2023 15:53:32 -0400 X-MC-Unique: sfuNc7wnN_WGDij7zcikxg-1 Received: by mail-qk1-f199.google.com with SMTP id af79cd13be357-74accc750c1so34639485a.0 for ; Mon, 17 Apr 2023 12:53:32 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681761211; x=1684353211; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=nL6ukD3uQsP0j8aiX/3IiND2lFTW26LLUmGTHQFycOk=; b=Og8U0gwkmNgSyDx8U04B31gnrxXZ+kM+aMcW+2C9uPsy7+wC5XvzP4+YJpT5XAbBsr Eo7p+aJu/GQaZjDP7Ic2Kcpkem1kG3tP7wa121FLE0gEpLpN27FcdYqZgp9ipKmYDINd LetRNCqCrFAXaZuLgy010WEh0TcAe+YxR/7Al7ivYWsQZQY+NsVNDCVWsXtWpz/T3TNm LIEyBDpK2pdvtAWToijNTcnXvtgo/I77w+PHE8ejqeZBzdQEhcPyPPf3StZ6oeY1Ji6O g5wdsZ6oJuTAmil/DVsGIk2KEvrqPelvXVxzF5ang2WSv64Gn+/oE46HS1n3RulsVt9O IrAQ== X-Gm-Message-State: AAQBX9e/8z6FvYkfuH8ZFRU8185wrR5c67PstZ2Z/MCJAiKQ3bGQ+Ejg PLxayl/9GH4MpTaiEY0dBI2v9AG8rX1dqALCW79nMHuraGX0t7txDmKZwXmpUBy8TQzw7Xs/OoV FVqadClW1V/yIHZwiiV2WCTdEGv9Aafpnkwna3QiLSiwWrl4dFkzbinDb5vfTMNmURZrj+/AcgY wfAzQdjQ== X-Received: by 2002:a05:6214:5095:b0:5ef:55d8:7164 with SMTP id kk21-20020a056214509500b005ef55d87164mr16879409qvb.5.1681761211391; Mon, 17 Apr 2023 12:53:31 -0700 (PDT) X-Google-Smtp-Source: AKy350ZYsrWygXMFCyj7XgLRO6y6vn18wQ8faUnma5Zhq/ehDr1HRYT9sovIA6wDuciwvG7UbAnfUQ== X-Received: by 2002:a05:6214:5095:b0:5ef:55d8:7164 with SMTP id kk21-20020a056214509500b005ef55d87164mr16879362qvb.5.1681761210964; Mon, 17 Apr 2023 12:53:30 -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 r17-20020ac87ef1000000b003edfb5d7637sm1731278qtc.73.2023.04.17.12.53.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Apr 2023 12:53:29 -0700 (PDT) From: Peter Xu To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Mike Kravetz , Andrea Arcangeli , =?UTF-8?q?Mika=20Penttil=C3=A4?= , Andrew Morton , peterx@redhat.com, Axel Rasmussen , Nadav Amit , David Hildenbrand Subject: [PATCH v2 5/6] selftests/mm: Rename COW_EXTRA_LIBS to IOURING_EXTRA_LIBS Date: Mon, 17 Apr 2023 15:53:16 -0400 Message-Id: <20230417195317.898696-6-peterx@redhat.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230417195317.898696-1-peterx@redhat.com> References: <20230417195317.898696-1-peterx@redhat.com> 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" The macro and facility can be reused in other tests too. Make it general. Reviewed-by: David Hildenbrand Signed-off-by: Peter Xu --- tools/testing/selftests/mm/Makefile | 8 ++++---- tools/testing/selftests/mm/check_config.sh | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/tools/testing/selftests/mm/Makefile b/tools/testing/selftests/= mm/Makefile index 63c03a6414fc..0ee00769b53f 100644 --- a/tools/testing/selftests/mm/Makefile +++ b/tools/testing/selftests/mm/Makefile @@ -161,8 +161,8 @@ warn_32bit_failure: endif endif =20 -# cow_EXTRA_LIBS may get set in local_config.mk, or it may be left empty. -$(OUTPUT)/cow: LDLIBS +=3D $(COW_EXTRA_LIBS) +# IOURING_EXTRA_LIBS may get set in local_config.mk, or it may be left emp= ty. +$(OUTPUT)/cow: LDLIBS +=3D $(IOURING_EXTRA_LIBS) =20 $(OUTPUT)/mlock-random-test $(OUTPUT)/memfd_secret: LDLIBS +=3D -lcap =20 @@ -175,11 +175,11 @@ local_config.mk local_config.h: check_config.sh =20 EXTRA_CLEAN +=3D local_config.mk local_config.h =20 -ifeq ($(COW_EXTRA_LIBS),) +ifeq ($(IOURING_EXTRA_LIBS),) all: warn_missing_liburing =20 warn_missing_liburing: @echo ; \ - echo "Warning: missing liburing support. Some COW tests will be skipped."= ; \ + echo "Warning: missing liburing support. Some tests will be skipped." ; \ echo endif diff --git a/tools/testing/selftests/mm/check_config.sh b/tools/testing/sel= ftests/mm/check_config.sh index bcba3af0acea..3954f4746161 100644 --- a/tools/testing/selftests/mm/check_config.sh +++ b/tools/testing/selftests/mm/check_config.sh @@ -21,11 +21,11 @@ $CC -c $tmpfile_c -o $tmpfile_o >/dev/null 2>&1 =20 if [ -f $tmpfile_o ]; then echo "#define LOCAL_CONFIG_HAVE_LIBURING 1" > $OUTPUT_H_FILE - echo "COW_EXTRA_LIBS =3D -luring" > $OUTPUT_MKFILE + echo "IOURING_EXTRA_LIBS =3D -luring" > $OUTPUT_MKFILE else echo "// No liburing support found" > $OUTPUT_H_FILE echo "# No liburing support found, so:" > $OUTPUT_MKFILE - echo "COW_EXTRA_LIBS =3D " >> $OUTPUT_MKFILE + echo "IOURING_EXTRA_LIBS =3D " >> $OUTPUT_MKFILE fi =20 rm ${tmpname}.* --=20 2.39.1 From nobody Wed Dec 17 18:59:47 2025 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 E0B82C77B72 for ; Mon, 17 Apr 2023 19:55:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230224AbjDQTy7 (ORCPT ); Mon, 17 Apr 2023 15:54:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33372 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230203AbjDQTyu (ORCPT ); Mon, 17 Apr 2023 15:54:50 -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 29F8C46BC for ; Mon, 17 Apr 2023 12:53:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1681761216; 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: in-reply-to:in-reply-to:references:references; bh=y7gltokQzFShDFjL1ksnnTsH6QcBiNJdLCM4WiStCBA=; b=XpbbJfdYajrrp3hMSH2oTF7Vje90EyVxKw6PJuyf/IBRn/dR2uUVqDiXkw1neybkiYICU0 WIXbYnoQaygHmXsIxGcOutsz1IOO6Nah30kELSSROmG5proKYfYKQ7RVBer7oNdqfFwALN pB9vfAHuLTHCOZ9PGri5Fwt5ai1aFRE= 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-216-5aB9MYjCObCgZ5txVv6Hbw-1; Mon, 17 Apr 2023 15:53:35 -0400 X-MC-Unique: 5aB9MYjCObCgZ5txVv6Hbw-1 Received: by mail-qt1-f197.google.com with SMTP id d75a77b69052e-3ecc0c4b867so7375721cf.1 for ; Mon, 17 Apr 2023 12:53:35 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681761213; x=1684353213; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=y7gltokQzFShDFjL1ksnnTsH6QcBiNJdLCM4WiStCBA=; b=Fc+fj8sGyj0Beks/fOz9huHPESaBDwHKABLvC4rbKkwmxce+3dTSOC3ZVIpgQ0tYoy edUdZJdrWRKU6nXpYdbgpC/yypnwiZ5kraMY/F1jeGDF6QOW70++QATDTWYM+BteAtxp fwylVM7c8EE8Pa0eVpuE7f2/t6Ab+MgDiSxj/h7mMmmNdB77q53WI7g/MT1TF7FouIB3 1mK5BMkeSJgy0rS+CXRzdYFDOojzZLqAGVn0e9IdpYFJNjhH18KFbeXMcboJG3PeREsw uaiBdZevM8RC6dX7cuQBBYKg1KFqWvALu2ZXrv7Wpbngs/d+naifpeDOLmpcREy8qbGc BGbA== X-Gm-Message-State: AAQBX9d74P/grVzkV0Cd13NnTJ7fCQU+yHsv6W6rkdft3grdaBOBxerT oS+2AhWsdc4+4W+ERsIJSFN8R0I6By7KLR1GVKVtKEGvCWo62miH+DjTewx9qY5l+jsZ0m1gMCp 4wpR6A4HppdU/TPSfCSJzIzOwYqvjF/3R+KrMZ1PjRcBnkhjq7vFuZ9MkpUk788vL1elLNV+M7u bVmOGS1A== X-Received: by 2002:a05:622a:1aaa:b0:3ee:5637:29cc with SMTP id s42-20020a05622a1aaa00b003ee563729ccmr9226105qtc.0.1681761213625; Mon, 17 Apr 2023 12:53:33 -0700 (PDT) X-Google-Smtp-Source: AKy350b6uFndWE+tj9O/onYcGkZ0fK6zrVlztfCRN9qHsXGXyZEKmCVQ7jywbyXKWj+B4rqOxPDQOw== X-Received: by 2002:a05:622a:1aaa:b0:3ee:5637:29cc with SMTP id s42-20020a05622a1aaa00b003ee563729ccmr9226079qtc.0.1681761213267; Mon, 17 Apr 2023 12:53:33 -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 r17-20020ac87ef1000000b003edfb5d7637sm1731278qtc.73.2023.04.17.12.53.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Apr 2023 12:53:32 -0700 (PDT) From: Peter Xu To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Mike Kravetz , Andrea Arcangeli , =?UTF-8?q?Mika=20Penttil=C3=A4?= , Andrew Morton , peterx@redhat.com, Axel Rasmussen , Nadav Amit , David Hildenbrand Subject: [PATCH v2 6/6] selftests/mm: Add tests for RO pinning vs fork() Date: Mon, 17 Apr 2023 15:53:17 -0400 Message-Id: <20230417195317.898696-7-peterx@redhat.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230417195317.898696-1-peterx@redhat.com> References: <20230417195317.898696-1-peterx@redhat.com> 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" Add a test suite (with 10 more sub-tests) to cover RO pinning against fork() over uffd-wp. It covers both: (1) Early CoW test in fork() when page pinned, (2) page unshare due to RO longterm pin. They are: Testing wp-fork-pin on anon... done Testing wp-fork-pin on shmem... done Testing wp-fork-pin on shmem-private... done Testing wp-fork-pin on hugetlb... done Testing wp-fork-pin on hugetlb-private... done Testing wp-fork-pin-with-event on anon... done Testing wp-fork-pin-with-event on shmem... done Testing wp-fork-pin-with-event on shmem-private... done Testing wp-fork-pin-with-event on hugetlb... done Testing wp-fork-pin-with-event on hugetlb-private... done CONFIG_GUP_TEST needed or they'll be skipped. Testing wp-fork-pin on anon... skipped [reason: Possibly CONFIG_GUP_TEST = missing or unprivileged] Note that the major test goal is on private memory, but no hurt to also run all of them over shared because shared memory should work the same. Signed-off-by: Peter Xu --- tools/testing/selftests/mm/uffd-unit-tests.c | 144 ++++++++++++++++++- 1 file changed, 141 insertions(+), 3 deletions(-) diff --git a/tools/testing/selftests/mm/uffd-unit-tests.c b/tools/testing/s= elftests/mm/uffd-unit-tests.c index 739fc4d30342..269c86768a02 100644 --- a/tools/testing/selftests/mm/uffd-unit-tests.c +++ b/tools/testing/selftests/mm/uffd-unit-tests.c @@ -7,6 +7,8 @@ =20 #include "uffd-common.h" =20 +#include "../../../../mm/gup_test.h" + #ifdef __NR_userfaultfd =20 /* The unit test doesn't need a large or random size, make it 32MB for now= */ @@ -247,7 +249,53 @@ static void *fork_event_consumer(void *data) return NULL; } =20 -static int pagemap_test_fork(int uffd, bool with_event) +typedef struct { + int gup_fd; + bool pinned; +} pin_args; + +/* + * Returns 0 if succeed, <0 for errors. pin_pages() needs to be paired + * with unpin_pages(). Currently it needs to be RO longterm pin to satisfy + * all needs of the test cases (e.g., trigger unshare, trigger fork() early + * CoW, etc.). + */ +static int pin_pages(pin_args *args, void *buffer, size_t size) +{ + struct pin_longterm_test test =3D { + .addr =3D (uintptr_t)buffer, + .size =3D size, + /* Read-only pins */ + .flags =3D 0, + }; + + if (args->pinned) + err("already pinned"); + + args->gup_fd =3D open("/sys/kernel/debug/gup_test", O_RDWR); + if (args->gup_fd < 0) + return -errno; + + if (ioctl(args->gup_fd, PIN_LONGTERM_TEST_START, &test)) { + /* Even if gup_test existed, can be an old gup_test / kernel */ + close(args->gup_fd); + return -errno; + } + args->pinned =3D true; + return 0; +} + +static void unpin_pages(pin_args *args) +{ + if (!args->pinned) + err("unpin without pin first"); + if (ioctl(args->gup_fd, PIN_LONGTERM_TEST_STOP)) + err("PIN_LONGTERM_TEST_STOP"); + close(args->gup_fd); + args->pinned =3D false; +} + +static int pagemap_test_fork(int uffd, bool with_event, bool test_pin) { fork_event_args args =3D { .parent_uffd =3D uffd, .child_uffd =3D -1 }; pthread_t thread; @@ -264,7 +312,17 @@ static int pagemap_test_fork(int uffd, bool with_event) child =3D fork(); if (!child) { /* Open the pagemap fd of the child itself */ + pin_args args =3D {}; + fd =3D pagemap_open(); + + if (test_pin && pin_pages(&args, area_dst, page_size)) + /* + * Normally when reach here we have pinned in + * previous tests, so shouldn't fail anymore + */ + err("pin page failed in child"); + value =3D pagemap_get_entry(fd, area_dst); /* * After fork(), we should handle uffd-wp bit differently: @@ -273,6 +331,8 @@ static int pagemap_test_fork(int uffd, bool with_event) * (2) when without EVENT_FORK, it should be dropped */ pagemap_check_wp(value, with_event); + if (test_pin) + unpin_pages(&args); /* Succeed */ exit(0); } @@ -352,7 +412,7 @@ static void uffd_wp_fork_test_common(uffd_test_args_t *= args, wp_range(uffd, (uint64_t)area_dst, page_size, true); value =3D pagemap_get_entry(pagemap_fd, area_dst); pagemap_check_wp(value, true); - if (pagemap_test_fork(uffd, with_event)) { + if (pagemap_test_fork(uffd, with_event, false)) { uffd_test_fail("Detected %s uffd-wp bit in child in present pte", with_event ? "missing" : "stall"); goto out; @@ -383,7 +443,7 @@ static void uffd_wp_fork_test_common(uffd_test_args_t *= args, /* Uffd-wp should persist even swapped out */ value =3D pagemap_get_entry(pagemap_fd, area_dst); pagemap_check_wp(value, true); - if (pagemap_test_fork(uffd, with_event)) { + if (pagemap_test_fork(uffd, with_event, false)) { uffd_test_fail("Detected %s uffd-wp bit in child in zapped pte", with_event ? "missing" : "stall"); goto out; @@ -415,6 +475,68 @@ static void uffd_wp_fork_with_event_test(uffd_test_arg= s_t *args) uffd_wp_fork_test_common(args, true); } =20 +static void uffd_wp_fork_pin_test_common(uffd_test_args_t *args, + bool with_event) +{ + int pagemap_fd; + pin_args pin_args =3D {}; + + if (uffd_register(uffd, area_dst, page_size, false, true, false)) + err("register failed"); + + pagemap_fd =3D pagemap_open(); + + /* Touch the page */ + *area_dst =3D 1; + wp_range(uffd, (uint64_t)area_dst, page_size, true); + + /* + * 1. First pin, then fork(). This tests fork() special path when + * doing early CoW if the page is private. + */ + if (pin_pages(&pin_args, area_dst, page_size)) { + uffd_test_skip("Possibly CONFIG_GUP_TEST missing " + "or unprivileged"); + close(pagemap_fd); + uffd_unregister(uffd, area_dst, page_size); + return; + } + + if (pagemap_test_fork(uffd, with_event, false)) { + uffd_test_fail("Detected %s uffd-wp bit in early CoW of fork()", + with_event ? "missing" : "stall"); + unpin_pages(&pin_args); + goto out; + } + + unpin_pages(&pin_args); + + /* + * 2. First fork(), then pin (in the child, where test_pin=3D=3Dtrue). + * This tests COR, aka, page unsharing on private memories. + */ + if (pagemap_test_fork(uffd, with_event, true)) { + uffd_test_fail("Detected %s uffd-wp bit when RO pin", + with_event ? "missing" : "stall"); + goto out; + } + uffd_test_pass(); +out: + if (uffd_unregister(uffd, area_dst, page_size)) + err("register failed"); + close(pagemap_fd); +} + +static void uffd_wp_fork_pin_test(uffd_test_args_t *args) +{ + uffd_wp_fork_pin_test_common(args, false); +} + +static void uffd_wp_fork_pin_with_event_test(uffd_test_args_t *args) +{ + uffd_wp_fork_pin_test_common(args, true); +} + static void check_memory_contents(char *p) { unsigned long i, j; @@ -923,6 +1045,22 @@ uffd_test_case_t uffd_tests[] =3D { /* when set, child process should inherit uffd-wp bits */ UFFD_FEATURE_EVENT_FORK, }, + { + .name =3D "wp-fork-pin", + .uffd_fn =3D uffd_wp_fork_pin_test, + .mem_targets =3D MEM_ALL, + .uffd_feature_required =3D UFFD_FEATURE_PAGEFAULT_FLAG_WP | + UFFD_FEATURE_WP_HUGETLBFS_SHMEM, + }, + { + .name =3D "wp-fork-pin-with-event", + .uffd_fn =3D uffd_wp_fork_pin_with_event_test, + .mem_targets =3D MEM_ALL, + .uffd_feature_required =3D UFFD_FEATURE_PAGEFAULT_FLAG_WP | + UFFD_FEATURE_WP_HUGETLBFS_SHMEM | + /* when set, child process should inherit uffd-wp bits */ + UFFD_FEATURE_EVENT_FORK, + }, { .name =3D "wp-unpopulated", .uffd_fn =3D uffd_wp_unpopulated_test, --=20 2.39.1