From patchwork Thu Apr 13 23:11:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 83119 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp1945vqo; Thu, 13 Apr 2023 16:19:05 -0700 (PDT) X-Google-Smtp-Source: AKy350ad+xkqeFleXOCnHEMfXILIUgxS8mGffFkj6oz7xOupDEHcEH8rV6GfKR+n43qYrQztjVUV X-Received: by 2002:aca:1809:0:b0:363:a5fd:9cd5 with SMTP id h9-20020aca1809000000b00363a5fd9cd5mr1979331oih.3.1681427945694; Thu, 13 Apr 2023 16:19:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681427945; cv=none; d=google.com; s=arc-20160816; b=qsnHPNLPSSI541ZGqD2Zo6wtkhn2GdZF+dZ6xzzhhasSVg1810z+v2gWdN9ciebTmp 4Lu/ki/X2g+JcEdjc15O347QYunbg4SqN6DwMBUCPdAixwSLo3GQQHD8nrfzOMaFDj/m jzaCGGmOz06CdY5vGUWjiAYfDhDL+1olcJ/KSrOOrN9H9ygEZIwDSZcdhea40YA4vv+Z 7zouPtsfyAK95xy8Ga3ILX53WuKVgPYwF6ku/s6kX2eas0eHAhjMIjni2FzuQIjpjQPn zhLrUBEoemcmtLUx/byjN0/PIW0b0uVTUwgWlxHId0kFg6P6bxJv/CF2KC7/trFKRA8I KT9A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=yQk3ShNsJBmwBAeq1XAgk+wt78LOM0H1LyrpvOkMJhI=; b=sK0iu6beIQhNdnvlfM8nmdeDLtgJVoGO/jaBUyX5vHCDoRw/UGXfY69Xz06Lt8o0H8 hDg8vsQ7rmZha/ULgOvz8Sxw7Dje/5JP876nACjGw3OUXtMztUc0SlK4BxZ6KhqaG6On 1GaCGTHPsukER4qram3q3Sj1T5ktxwqHU01t4rAp6AQS5t74exncyQ9Yg4KSn4aAkbuq 5s5qEl+1fmjieg1XVsIQ/bHf2OXjqfvTozYLygDuTJ+uAW+E6DXsYaQ100J5GkA1G572 pTkd70NIzGWWrfTKL32Wb8kwD1xxHSuJOkKrYsFaoHpxRXFickJKDn/KlY+K8gERLZK2 q13A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Pj0oA1Va; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x16-20020a544010000000b0037f84c78ed7si2584183oie.71.2023.04.13.16.18.29; Thu, 13 Apr 2023 16:19:05 -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=Pj0oA1Va; 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 S230305AbjDMXMW (ORCPT + 99 others); Thu, 13 Apr 2023 19:12:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43002 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230253AbjDMXMS (ORCPT ); Thu, 13 Apr 2023 19:12:18 -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 EE1254488 for ; Thu, 13 Apr 2023 16:11:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1681427487; 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=yQk3ShNsJBmwBAeq1XAgk+wt78LOM0H1LyrpvOkMJhI=; b=Pj0oA1VaVaTBz7Co/mHv7+3QEOlqTk/prCxWkwerwaFt1yDy7Lzmj6IrjslmO3Ryyht08R gXKCHMXs9YK4YXdNU3KJWOLEWNgbLW7MKsnBHcu7bjdShCWi8ngSFoVT7nW1fkKCyjuD/N 9SvoktEMDqwxidZFnkQ/vOQor1M73Os= Received: from mail-qt1-f199.google.com (mail-qt1-f199.google.com [209.85.160.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-177-MrpqThBnNPWN7iSdYw2WJA-1; Thu, 13 Apr 2023 19:11:26 -0400 X-MC-Unique: MrpqThBnNPWN7iSdYw2WJA-1 Received: by mail-qt1-f199.google.com with SMTP id d75a77b69052e-3e699015faeso6672201cf.0 for ; Thu, 13 Apr 2023 16:11:25 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681427484; x=1684019484; 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=yQk3ShNsJBmwBAeq1XAgk+wt78LOM0H1LyrpvOkMJhI=; b=g8b6WmadfC2NmQDbfmDyPceJY6eYY8V/U0OXh5egq63F2Vi85m8TrTmvIX1nhzMMmx lmH5Eczde+rCPvVtYl/uHuYJtTDMEWT6b+ZbG02iT6pfGa60aDvBfVjyRbK308LxPAMx Au35nep0E8WRqfAre1eqEP/rfOjJwM2uNc8UgAxLIEb+fi8wJp/tcrIQvpsJt2iGtPv8 wwTAh0RLnL2YwyXD6ZDC5kp41UhbsoUvv425gCgu6/WjjaDom3Qs7JNyxZr2r5+AevLD 62dZGy6IXgnVB7+PHLd4X727R3b5oZjd3JWfLB5KeP0XEuvSwM8fZXYY5qYpBImOFsul c59Q== X-Gm-Message-State: AAQBX9fi2rMT9jOGElttwPmEz6hSo3i98VCN+NY5jLzyMMER5SMUD/N9 72onLwON3IAnE8Vjpe4tkxNmVhKYnhQvTrkYnmU7ONqAn1qIoiN7XqKQdTU9Xzec1m70cQdILbN yupi8hCvZExDzY1l7auVbfShV2AY2bqJxKko8hhEasbOwjyuPDnzfaf+eFrs/Tx1qazMQUrL+c7 +XWDtl4w== X-Received: by 2002:a05:622a:19a1:b0:3e3:98cf:68ae with SMTP id u33-20020a05622a19a100b003e398cf68aemr1101164qtc.2.1681427484669; Thu, 13 Apr 2023 16:11:24 -0700 (PDT) X-Received: by 2002:a05:622a:19a1:b0:3e3:98cf:68ae with SMTP id u33-20020a05622a19a100b003e398cf68aemr1101122qtc.2.1681427484321; Thu, 13 Apr 2023 16:11: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 g3-20020ac81243000000b003ea1b97acfasm612446qtj.49.2023.04.13.16.11.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Apr 2023 16:11:23 -0700 (PDT) From: Peter Xu To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Axel Rasmussen , Andrew Morton , David Hildenbrand , peterx@redhat.com, Mike Kravetz , Nadav Amit , Andrea Arcangeli , linux-stable Subject: [PATCH 1/6] mm/hugetlb: Fix uffd-wp during fork() Date: Thu, 13 Apr 2023 19:11:15 -0400 Message-Id: <20230413231120.544685-2-peterx@redhat.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230413231120.544685-1-peterx@redhat.com> References: <20230413231120.544685-1-peterx@redhat.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE autolearn=ham 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1763104989732649449?= X-GMAIL-MSGID: =?utf-8?q?1763104989732649449?= 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 Reviewed-by: David Hildenbrand --- 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) { + 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); } From patchwork Thu Apr 13 23:11:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 83120 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp2064vqo; Thu, 13 Apr 2023 16:19:20 -0700 (PDT) X-Google-Smtp-Source: AKy350b1IrrGm7tMeAVRL8/xKGTwzHLInZAWgud2nTkAyFtjrDEn6ojQfSBeyiBJ+S0kRSKEKZ70 X-Received: by 2002:a05:6a00:1707:b0:633:5c46:5a69 with SMTP id h7-20020a056a00170700b006335c465a69mr6446949pfc.31.1681427960384; Thu, 13 Apr 2023 16:19:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681427960; cv=none; d=google.com; s=arc-20160816; b=eJzJ99DF9oGOdoC3FMBQ0lJoQWHr7IRbndhwWDRV55mvvVl64oEKx6S5sEoy/03YLA MJCVChFOAfOpB+iJLg37ZcXsdW39J/aBHELykwkTGMgdxstTe6Bad1rD6hrBy1wD9ixZ SXEdJys19uQ0MXrAVehmZ9EVmrpbCzlu+TTWZZSnL3qnfccGWoujck8C90eoI+Q8XAp3 IcHlXOX6dFue8qBPWnr6RMN/JAv/m3IJTRgf5T3HIA9imtsRYXgporfQgqG3TfRi662l b8yjVcATu1tQuTgiC1RHdM3FREClMsCEvR7trgEwUmX1z+IHX0RVIQmYlbVSpXoL6zwt vOuw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=sPrSeNO5Sj4btD4u1XV4AOohfTbtE2i2MNiZ002Yg6Y=; b=BVuTn7oIoFZGaXcgrBN2//aH/gjfrD9TRL9dHt3k3/2y8T/jzwuRNYSN1w6QeSpf/R cTfLa/K3qsIW+lGW4iYENAXJnsej3nTiYvaHkOj8ifQrjXe8VBjgKmKj5DyobntA9bkV hJX19eO37tO7llTJWaVDJlI6LixvdRCkaaoMzmM5y5RIaI+mvecy1NXjyBFewfy2iq15 O3WLdOKiLQa7mGM5ajNHZDOofeZTg4gNKZh7jRCquscc3JeUszo0XNDPUjrNdCMcQlVM AOKOzH4E10zGfiK9xjgfj9UoJz3dcS6AmCa0f6JBGwmDpkoMIVERglIQNw7NQ8vOYqAk CBNA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="TxD/o0u7"; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id f8-20020aa79688000000b0062ae6345c6dsi2617424pfk.392.2023.04.13.16.19.05; Thu, 13 Apr 2023 16:19:20 -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="TxD/o0u7"; 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 S230223AbjDMXMS (ORCPT + 99 others); Thu, 13 Apr 2023 19:12:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42994 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229578AbjDMXMQ (ORCPT ); Thu, 13 Apr 2023 19:12:16 -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 DD91B4EDC for ; Thu, 13 Apr 2023 16:11:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1681427489; 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=sPrSeNO5Sj4btD4u1XV4AOohfTbtE2i2MNiZ002Yg6Y=; b=TxD/o0u7U68RMOA2/AfI3LifWOajGaY68YTF2MzlsvYbyB6MvURc0mMGIgaNhBTVWw7yJK oaPIRPUBZ2hBwduJT8Uy4s+aJ9MOqpiCH010ALqP2eTj7ub5Pwiqgf1Ag2L8wZfS4n9Ahh fOVP02A3J0jJUiKvvdEzM9/6jvMFAg8= 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-623-EideDlK8PiyjdhCIELtpxA-1; Thu, 13 Apr 2023 19:11:28 -0400 X-MC-Unique: EideDlK8PiyjdhCIELtpxA-1 Received: by mail-qk1-f197.google.com with SMTP id af79cd13be357-74a25fcda05so96963885a.1 for ; Thu, 13 Apr 2023 16:11:27 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681427487; x=1684019487; 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=sPrSeNO5Sj4btD4u1XV4AOohfTbtE2i2MNiZ002Yg6Y=; b=ajSe9Jeopvr4p5OhnF1eBu3Gn8Pq+wZJFB0LdqZq3tdXaI1wkDKWkw2T2sfYRJbwER MT5h06LcmwR0l9tkGr4/9KYncLr2Da9ynEFpciRMwdKBhkoMDr1dCZKloMI7uP6OGGN8 161bMOQloJde/2M5IVmgXAFWjgcGYAw+0ClboIyDU7/2Fe9uO0th0wtpiKlD3Zywo8Mu m6k0JznqhfphOqmMsl0yLnKAp3p8FO7LRrrMvcFFF9mdwL5EX7Mixrvi/SMk8eZ9oMbB bvOS7e48LyYAIU3SM8we9HY2zkgdXZYNV4isMwzWzwFVFhbft0sm9+nA71sCafUWb/Pe vPDw== X-Gm-Message-State: AAQBX9eeYnzBR4RpGvQ3dAeeu+DFqZCTMhMJ/lSJlbV4UnJJp6f6pZ++ XPUy7MT/Ut0Yg16TMxudZM5d4AGagjttxx9hXZtvA1lHGD9NpfKbMBnSn07AD9UgSQ7QUo3ZdX4 qlnm/3mlXPN+YmAuNwha0dJRi40e8UfkALLq6oSEtQ+bUh0lPzioDFPDya0QvEXBSfWjgfjQw3m NUPHFjCA== X-Received: by 2002:a05:622a:1aa6:b0:3e2:be32:cb74 with SMTP id s38-20020a05622a1aa600b003e2be32cb74mr1006409qtc.3.1681427487047; Thu, 13 Apr 2023 16:11:27 -0700 (PDT) X-Received: by 2002:a05:622a:1aa6:b0:3e2:be32:cb74 with SMTP id s38-20020a05622a1aa600b003e2be32cb74mr1006377qtc.3.1681427486764; Thu, 13 Apr 2023 16:11: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 g3-20020ac81243000000b003ea1b97acfasm612446qtj.49.2023.04.13.16.11.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Apr 2023 16:11:25 -0700 (PDT) From: Peter Xu To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Axel Rasmussen , Andrew Morton , David Hildenbrand , peterx@redhat.com, Mike Kravetz , Nadav Amit , Andrea Arcangeli , linux-stable Subject: [PATCH 2/6] mm/hugetlb: Fix uffd-wp bit lost when unsharing happens Date: Thu, 13 Apr 2023 19:11:16 -0400 Message-Id: <20230413231120.544685-3-peterx@redhat.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230413231120.544685-1-peterx@redhat.com> References: <20230413231120.544685-1-peterx@redhat.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE autolearn=ham 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1763105005297087628?= X-GMAIL-MSGID: =?utf-8?q?1763105005297087628?= 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 protection") Reported-by: Mike Kravetz Signed-off-by: Peter Xu Reviewed-by: David Hildenbrand Reviewed-by: Mike Kravetz --- mm/hugetlb.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 7320e64aacc6..083aae35bff8 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 = hugetlb_walk(vma, haddr, huge_page_size(h)); if (likely(ptep && pte_same(huge_ptep_get(ptep), pte))) { + pte_t newpte = 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 = 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 = page_folio(old_page); From patchwork Thu Apr 13 23:11:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 83116 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp1897vqo; Thu, 13 Apr 2023 16:19:01 -0700 (PDT) X-Google-Smtp-Source: AKy350ZfF3YqihtUCAqhlvGsowtUu6ZQbi4LqxVViO2PqdZor17vapWG0EHIev7ToOrlA9A7vo1l X-Received: by 2002:a17:902:f543:b0:1a2:37fc:b591 with SMTP id h3-20020a170902f54300b001a237fcb591mr632619plf.69.1681427941383; Thu, 13 Apr 2023 16:19:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681427941; cv=none; d=google.com; s=arc-20160816; b=bx7fchIlbT5afuxGM7QEJodEZ8HaNVEZ7Y75Wm/YQ6PKaZGt9FAovxZgU5tkGuGq9D stKpFNZmOK/RwfHECPP8AFgUbQKB+6diS76maBXkUQSfXSy+YD1OP65BAGtDVkZvAXfK 3tqsDVgTduCryYgYyE+U+6l5PkZeC7c+6oFoO4pWPGXaF2IC3ZWxPXWg9XuHa7oaTQzf qOhDa47hX/AeOiGBcj/bivJAW+aKYAOw/pVhw/xtNc1wO4Kmd1+OQBIn/Y+nwMQeRG04 a1JWAMp16f3A7rjd1FaNy6zaK9mBavPbgYU7/wICHdmdJ2RYdgCLN3HG9fzIe/lJCxy/ R73g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=VlJAJroC0qfUjlayNMIBwxZydfuRD55bTGjabDZPAD4=; b=g6jgzoNeSccZiQWuMnQZJto4Caed4JKiZYOnm7H7+oqKDOKbW0rU8T48Y6FTS76w7v r1NH15s1n8Z979Q5QD1ReEZfhrjCG4xnu+me/Za4yNJUclHh3fSDxdnJfE7BfDM3h4MM 3ryILyZElF40RjI2drHZ6rLs31c34ANCLrCTUMSFqkO1syy8jHa5IHnlSgXhQQuVlTBO B8KFSRVJFIfoeq34l48pbZlp6ZI0SkVq5Cusmw1W0nkoW2+c6JcpQ6+Dc3KQ7hmvDHZf cnB99Y20Ku7jMr3Elh8oXFZqeKbS1xTvMw27s8e1+9wFz0N/0OoYA/B1VZ+NScRnCiBj VdAQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=TnBSw08O; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id iw18-20020a170903045200b0019f269f06fbsi2919303plb.3.2023.04.13.16.18.24; Thu, 13 Apr 2023 16:19:01 -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=TnBSw08O; 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 S230368AbjDMXMe (ORCPT + 99 others); Thu, 13 Apr 2023 19:12:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42840 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230253AbjDMXM0 (ORCPT ); Thu, 13 Apr 2023 19:12:26 -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 1188646A9 for ; Thu, 13 Apr 2023 16:11:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1681427492; 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=TnBSw08Ofe1FonYiJSbfR0l2EnYAnj0MC03K3hIMQ7u/rsHxBEbHEO7fpGjD4/HqeukUNk /nbKPWVovu7TqS5fgBwKVnGpBpIos5UokhNOXLKWKU1+Dbry8BBRIsLTehBq7CU1EbPDpT dVTP+33iie9nYpb0wx2yB4Q0sdEeG5w= 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-126-EEkusKa8NRmbUY7Hswl6Vw-1; Thu, 13 Apr 2023 19:11:30 -0400 X-MC-Unique: EEkusKa8NRmbUY7Hswl6Vw-1 Received: by mail-qt1-f197.google.com with SMTP id d75a77b69052e-3eab4df3ef0so822621cf.0 for ; Thu, 13 Apr 2023 16:11:30 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681427489; x=1684019489; 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=hP5pKlwPXgKbzIf6pKd1YPKN0Hr7s1y41L4kGCihHGx9DPQD0CZnA9OqhqEihJkrXA kFCPxAKyBw8oZaRykJOcfoy2GLlxzh9Oahvppt4i6wKS7Pxo807PIATTeC0bDaFHAsAB PiQoC+w1Zri2ORwo5e5KzF83D6CI6+mVkaQsGoKTervgh+8NqTHe8VKIIoO8BvtDyONr 3trs3+XLnofzqYCnCJa4JdUxhpEjl6PJYTOjHK4aIuEq3JK938DyV+MmkmG6/Pey93IC 7xPDC2AjDirba/HSsMBH7pVPNSdsi52Mjxjx3ESOovH0MrRbI7TxNY///ZIHkUbl9r43 E3Wg== X-Gm-Message-State: AAQBX9fSRb5kJ5jtbMo0L95umcnBa2oUQ9uuHYHOsqgDOy2BcBf+l0lj dq3ggk04mwVXonclbz0LZpT3IR+Fs+gJaOEppZTE/V+uT7LVLStvBtn1W8ymXOr68I1jc6nRTc/ BGOaccffjq8Y4mfWBCGmGqGIEluG6au6dXwJDfYi3HHTyDsmeteswtttwhlns3gyLHASR7R5Bgg 21cp3D6A== X-Received: by 2002:a05:622a:1a0b:b0:3e6:3af1:de7a with SMTP id f11-20020a05622a1a0b00b003e63af1de7amr979948qtb.4.1681427489625; Thu, 13 Apr 2023 16:11:29 -0700 (PDT) X-Received: by 2002:a05:622a:1a0b:b0:3e6:3af1:de7a with SMTP id f11-20020a05622a1a0b00b003e63af1de7amr979903qtb.4.1681427489278; Thu, 13 Apr 2023 16:11: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 g3-20020ac81243000000b003ea1b97acfasm612446qtj.49.2023.04.13.16.11.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Apr 2023 16:11:28 -0700 (PDT) From: Peter Xu To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Axel Rasmussen , Andrew Morton , David Hildenbrand , peterx@redhat.com, Mike Kravetz , Nadav Amit , Andrea Arcangeli Subject: [PATCH 3/6] selftests/mm: Add a few options for uffd-unit-test Date: Thu, 13 Apr 2023 19:11:17 -0400 Message-Id: <20230413231120.544685-4-peterx@redhat.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230413231120.544685-1-peterx@redhat.com> References: <20230413231120.544685-1-peterx@redhat.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE autolearn=ham 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1763104984901058162?= X-GMAIL-MSGID: =?utf-8?q?1763104984901058162?= 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 --- 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/selftests/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[] = { }, }; +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 = sizeof(uffd_tests) / sizeof(uffd_test_case_t); int n_mems = sizeof(mem_types) / sizeof(mem_type_t); + const char *test_filter = NULL; + bool list_only = 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; - has_uffd = test_uffd_api(false); - has_uffd |= test_uffd_api(true); + while ((opt = getopt(argc, argv, "f:hl")) != -1) { + switch (opt) { + case 'f': + test_filter = optarg; + break; + case 'l': + list_only = true; + break; + case 'h': + default: + /* Unknown */ + usage(argv[0]); + break; + } + } + + if (!test_filter && !list_only) { + has_uffd = test_uffd_api(false); + has_uffd |= test_uffd_api(true); - 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); + } } for (i = 0; i < n_tests; i++) { test = &uffd_tests[i]; + if (test_filter && !strstr(test->name, test_filter)) + continue; + if (list_only) { + printf("%s\n", test->name); + continue; + } for (j = 0; j < n_mems; j++) { mem_type = &mem_types[j]; if (!(test->mem_targets & mem_type->mem_flag)) @@ -952,7 +989,8 @@ int main(int argc, char *argv[]) } } - uffd_test_report(); + if (!list_only) + uffd_test_report(); return ksft_get_fail_cnt() ? KSFT_FAIL : KSFT_PASS; } From patchwork Thu Apr 13 23:11:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 83124 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp2305vqo; Thu, 13 Apr 2023 16:19:55 -0700 (PDT) X-Google-Smtp-Source: AKy350a/4HrbKYW4jhYfcHywIo73m8zrDdIgAicNX8G7rRYJASm4i1Z98U5RTqCE4PoIcQKRxOrk X-Received: by 2002:aa7:8887:0:b0:627:ea7a:ff46 with SMTP id z7-20020aa78887000000b00627ea7aff46mr5608338pfe.16.1681427995443; Thu, 13 Apr 2023 16:19:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681427995; cv=none; d=google.com; s=arc-20160816; b=M/s6aSBxN3L4vGwOhKOsAe4RucfIv5D0CJBb/ZRwzMx/H4Dzr2xXIsl87We3VFpiSm m+f+t9UQRViM+xoRMA9Okxwfqxzn8zE/fdaHdtMjzQb0zXz7np/hitgpTXxZ6zap9hFH gxgYiUq5tEmjsmPY8usMx0CbNLlTqfTCli373BLome+J9yJ9MFtrjpZ9dRdhprq3m3qv jYv5TLiC/6+why0pciz1mu0iQUez3yu33qbV4bwWY0g0qNtD9mlChY340F1wr5RKGID8 jD913PYVTtG1IpaofbYp6BJ8rOjsTS749LXm0Y2CwLAFtjMgen5exHNd2tNkx/vN8SZ8 72Ag== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=PiB4O8c7FzfgLpxlGuxco4WipLi6p7uHP+fG5udokS8=; b=hsZ0+0mh+NselLR7pbwhOGfnGh3Mcq6VVYcZagPIXukv9CAPu3IebnJi7zf6n+Z28X yehF6siSOxMb5X1h7/JUX5RZ0Az1LTbtiWEHGadATlM2v7d0TzHK7nFL4L8Nq/qN8avL TUVyq4Wkcewax5mMZa9kfJngDOFs3HOTNV19lkwQLpDs9lsPPxouPe4ZH4+5V8Q+Sava urs/h+Jyy3ySteTAJNzpfW/srM5gO8p/pyrpcQHCnqm7ctc+ebgsIjUxFeQG2BpChMYQ QgUPk1K/ok/XTqDqCkkdt+dnf3sk3XNxkDcvB9Hw/Y8KETtpKzqeQpM9J1C2mcX54rTW rudw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=IVATLpeG; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h129-20020a625387000000b0063b21db5973si2639709pfb.358.2023.04.13.16.19.41; Thu, 13 Apr 2023 16:19:55 -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=IVATLpeG; 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 S230359AbjDMXMh (ORCPT + 99 others); Thu, 13 Apr 2023 19:12:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43162 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230321AbjDMXM1 (ORCPT ); Thu, 13 Apr 2023 19:12:27 -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 54A7544A7 for ; Thu, 13 Apr 2023 16:11:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1681427494; 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=IVATLpeGNZVLtexgURJ9v5KEjo4XqoKNFAifajKjA2AoQGYYfzliEUVaBTkqusocvKMFYC nSGwuoPuYO8Lt9hz8vgHUcCQwvFlXGNQwtxVtBLNA1TqEsdlhGy9ppR9Pw3Eerq8TQv09l N1rnhm2lgw1oSCd8hdHktt9+c87oVN4= Received: from mail-qt1-f199.google.com (mail-qt1-f199.google.com [209.85.160.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-668-fR1VycY6MsiVDhtXiWB0pQ-1; Thu, 13 Apr 2023 19:11:33 -0400 X-MC-Unique: fR1VycY6MsiVDhtXiWB0pQ-1 Received: by mail-qt1-f199.google.com with SMTP id d75a77b69052e-3e63ec50808so6303951cf.1 for ; Thu, 13 Apr 2023 16:11:33 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681427491; x=1684019491; 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=CQTcBiHBW/mFc3PVrD/kq9EYSVfkJmc9lbj6+++/DG686g/8xRCzcZWpaPlM0rVPF+ npiUEVRlLInAoMMA9s918ZM4mY/fGYeGTq5e7N8WXdHN3ZYI2aWH4icGWK69isU1o8Wo 5Emk2ZB67biT6lxYIhRCTuHhsjgABW2eInc+N34GvhfMUYzJltKhcmQK1CFVh2i9YIhn 4o40GiPCYB0oMS/QXU9jQRdpKHiJ6iRHKjtYJdYgIKhlOjhglCv2I73n2MLBDbRm/Jfh nDDuuXO4QX98ceL6Y5c3JhBXAxqMLxzVqcvIGp5oToiYDM7OPfvmDnFq77Fla5HhS9fj D/NQ== X-Gm-Message-State: AAQBX9cd4FtBA6VWPmJGl4XlPZ1m+iqCh8bPDS3ZCVqf83fvqIC+M7Kz aZv1f9sQ2aBV+PgbeJelEvhNzLu4I+luGNavzJxPFKg+NiNiK/l56LdyF59UXyZ2lin57E4KSt5 2CvtQBQeIJWTum5/UBWRRnEDmuJ7YjvQyrsXum3M6nKVTxt1LeikWsO0oYGl+puCPqagLFHfP0D v7sQGWbA== X-Received: by 2002:ac8:5813:0:b0:3e6:707e:d3b2 with SMTP id g19-20020ac85813000000b003e6707ed3b2mr1243727qtg.0.1681427491579; Thu, 13 Apr 2023 16:11:31 -0700 (PDT) X-Received: by 2002:ac8:5813:0:b0:3e6:707e:d3b2 with SMTP id g19-20020ac85813000000b003e6707ed3b2mr1243678qtg.0.1681427491125; Thu, 13 Apr 2023 16:11:31 -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 g3-20020ac81243000000b003ea1b97acfasm612446qtj.49.2023.04.13.16.11.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Apr 2023 16:11:29 -0700 (PDT) From: Peter Xu To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Axel Rasmussen , Andrew Morton , David Hildenbrand , peterx@redhat.com, Mike Kravetz , Nadav Amit , Andrea Arcangeli Subject: [PATCH 4/6] selftests/mm: Extend and rename uffd pagemap test Date: Thu, 13 Apr 2023 19:11:18 -0400 Message-Id: <20230413231120.544685-5-peterx@redhat.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230413231120.544685-1-peterx@redhat.com> References: <20230413231120.544685-1-peterx@redhat.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE autolearn=ham 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1763105041510168090?= X-GMAIL-MSGID: =?utf-8?q?1763105041510168090?= 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/selftests/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) -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 = fork(); + fork_event_args *args = data; + struct uffd_msg msg = { 0 }; + + /* Read until a full msg received */ + while (uffd_read_msg(args->parent_uffd, &msg)); + + if (msg.event != UFFD_EVENT_FORK) + err("wrong message: %u\n", msg.event); + + /* Just to be properly freed later */ + args->child_uffd = msg.arg.fork.ufd; + return NULL; +} + +static int pagemap_test_fork(int uffd, bool with_event) +{ + fork_event_args args = { .parent_uffd = uffd, .child_uffd = -1 }; + pthread_t thread; + pid_t child; uint64_t value; int fd, result; + /* Prepare a thread to resolve EVENT_FORK */ + if (with_event) { + if (pthread_create(&thread, NULL, fork_event_consumer, &args)) + err("pthread_create()"); + } + + child = fork(); if (!child) { /* Open the pagemap fd of the child itself */ fd = pagemap_open(); value = 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; } @@ -295,7 +335,8 @@ static void uffd_wp_unpopulated_test(uffd_test_args_t *args) uffd_test_pass(); } -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 = 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; + } - 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); + } /* Uffd-wp should persist even swapped out */ value = 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; + } /* 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 = 2; value = 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); } static void check_memory_contents(char *p) @@ -836,10 +908,20 @@ uffd_test_case_t uffd_tests[] = { .uffd_feature_required = 0, }, { - .name = "pagemap", - .uffd_fn = uffd_pagemap_test, - .mem_targets = MEM_ANON, - .uffd_feature_required = UFFD_FEATURE_PAGEFAULT_FLAG_WP, + .name = "wp-fork", + .uffd_fn = uffd_wp_fork_test, + .mem_targets = MEM_ALL, + .uffd_feature_required = UFFD_FEATURE_PAGEFAULT_FLAG_WP | + UFFD_FEATURE_WP_HUGETLBFS_SHMEM, + }, + { + .name = "wp-fork-with-event", + .uffd_fn = uffd_wp_fork_with_event_test, + .mem_targets = MEM_ALL, + .uffd_feature_required = UFFD_FEATURE_PAGEFAULT_FLAG_WP | + UFFD_FEATURE_WP_HUGETLBFS_SHMEM | + /* when set, child process should inherit uffd-wp bits */ + UFFD_FEATURE_EVENT_FORK, }, { .name = "wp-unpopulated", From patchwork Thu Apr 13 23:11:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 83122 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp2120vqo; Thu, 13 Apr 2023 16:19:29 -0700 (PDT) X-Google-Smtp-Source: AKy350YSjSb1oR9vVa+sK7dwghQ5bjYiInhUPRjXPSAX6lJOdLXX6lsR2nxQDbtemlPyabWot7N+ X-Received: by 2002:a05:6870:60a5:b0:177:c4bc:c1c8 with SMTP id t37-20020a05687060a500b00177c4bcc1c8mr2778980oae.0.1681427969558; Thu, 13 Apr 2023 16:19:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681427969; cv=none; d=google.com; s=arc-20160816; b=ufspYkvqkzjJMudrUKI4LWmt3G3AbvByKQ4bvRRnkoBFOlYBMULjX60/lMXlUqwjoA V3bU04bfMN8w4yt7xvqDEhmAtQKtEtNZ+nJBWhEcSB2dyQR00uvdXkZ0qSuShSL0ACU5 Jl4EuAyBjtvHLiBAe2xm22FMG9FTK1TxkV5arbYs5z1XzmN83LNJ4WkuiTKnLQmbyb6R gyHZNMHfvuchCYRZimTRoJLG0bjbB778tv9F8MNw+uFm50d1zWpIS9NJp3mtf6nj7YYt C0qTVU+CJGOIhZA2HyOS3TdFFyMRefaUQosjsVHOFHCzhtbaGNv6Zu4gjGzD71+Itk2d d1xQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=UrLvr60huy61e3psoajwIv3A4LyU2VRqVkvYCOVTv4c=; b=KX00WV3STK0pciB4jxcu051TDyPEc/onE3ZFnECfc0nhN2ttG4HJvw+BWxIQspB5JC G9yxgV+9nGee39YInghzDK5K4vfB0XvNsD2crzGeAOUOGaxwUO1/FyDMkQ+A0dnAeY2j WHvbgr8ywCxMpM5KnVC1VqXOVJHM6+37oX/kcH1akhliFb+mazcsldR5F7BIAbiuctmn c47YXPRVuhVzmdT7DOwMVP98mzMVUpuGWxdA9PT6eeGegMIEIJWfjtqi96uRkagia2pq oNJJvxzLyYaeccdKhOuKc1GLT+JfU6bJHqraJCi6IZMckwPeQ6pyqpk4pFCTYwnK45aL InmQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Gtb7DL7z; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id v1-20020a05683011c100b006a14aa6116asi2643003otq.37.2023.04.13.16.19.15; Thu, 13 Apr 2023 16:19:29 -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=Gtb7DL7z; 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 S230382AbjDMXMl (ORCPT + 99 others); Thu, 13 Apr 2023 19:12:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43236 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230327AbjDMXM1 (ORCPT ); Thu, 13 Apr 2023 19:12:27 -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 3694B49D2 for ; Thu, 13 Apr 2023 16:11:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1681427497; 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=UrLvr60huy61e3psoajwIv3A4LyU2VRqVkvYCOVTv4c=; b=Gtb7DL7zoE+Tm/3L4YYshWjdQzDeBiytm+FOOSc0mRXOovkxQ6Uw8uocjMxnIhe28X6TIq yjQd/Dfypr/Th7laQ/I1mWxi8iTgpRURAQWdSUhlYYnjOUN/VOaoFq6/NSn2EvGvrJaK6A ymHZObmk/KPO4rn+mY4YjeafDQfh8yg= Received: from mail-qv1-f69.google.com (mail-qv1-f69.google.com [209.85.219.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-223-acdjznG5MEyeqAu0eKBGJg-1; Thu, 13 Apr 2023 19:11:36 -0400 X-MC-Unique: acdjznG5MEyeqAu0eKBGJg-1 Received: by mail-qv1-f69.google.com with SMTP id 6a1803df08f44-5ef52fc3c8aso691566d6.1 for ; Thu, 13 Apr 2023 16:11:36 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681427494; x=1684019494; 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=UrLvr60huy61e3psoajwIv3A4LyU2VRqVkvYCOVTv4c=; b=KecDmWdr6zdPJpomCodqK0wewsgqB8fmrQsIKa0gciwjKhBVWPDP+5suqLtQeH24on efGS42meeczB7gK/pA7c0Q+mecBVuN0BUGvHyXon+rOef8eld89Mhyj4E43oZV2vyosI EKVpdqmBr4d9vytGhJqlDc0wv7uPX8Dh0ND5lM4i3+wlz5l/l4kZMHbDY+Wbd9tasEDJ m4Wyu1X9yMil3huS4BWbJZ5dlHA28a3geD2Oz5AXB2HWkV0oSeKBG4Ry6mPWHMUTC1K0 NhKTDgSfLUDipyCesAr2ez/Ll0pGjIxAZsR1fG+TDcf7RX2tGY35XnpMdr7eNLP8ZD9M IPhw== X-Gm-Message-State: AAQBX9fie35aeOrwWIA9rQQoUKMx44b59VacilApoI/9nzJN/+toGAgf wBB8HE2JJBPB2CVzDRIUHuPfBE98NPEwjvwcALZ3NFXfW4udiF0C3yEmpsJC6x+LjXI0knz/z5A C5933Zjsc9pI0ufapqPXV+ncUXUS7JeLntvhhHH/PqN6nj62eZxN7VeDqfnGH7ihZ5gMvhR1siS evYdERug== X-Received: by 2002:a05:622a:19a4:b0:3e8:e986:b20b with SMTP id u36-20020a05622a19a400b003e8e986b20bmr822645qtc.6.1681427493800; Thu, 13 Apr 2023 16:11:33 -0700 (PDT) X-Received: by 2002:a05:622a:19a4:b0:3e8:e986:b20b with SMTP id u36-20020a05622a19a400b003e8e986b20bmr822605qtc.6.1681427493424; Thu, 13 Apr 2023 16:11: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 g3-20020ac81243000000b003ea1b97acfasm612446qtj.49.2023.04.13.16.11.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Apr 2023 16:11:31 -0700 (PDT) From: Peter Xu To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Axel Rasmussen , Andrew Morton , David Hildenbrand , peterx@redhat.com, Mike Kravetz , Nadav Amit , Andrea Arcangeli Subject: [PATCH 5/6] selftests/mm: Rename COW_EXTRA_LIBS to IOURING_EXTRA_LIBS Date: Thu, 13 Apr 2023 19:11:19 -0400 Message-Id: <20230413231120.544685-6-peterx@redhat.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230413231120.544685-1-peterx@redhat.com> References: <20230413231120.544685-1-peterx@redhat.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE autolearn=ham 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1763105014879754657?= X-GMAIL-MSGID: =?utf-8?q?1763105014879754657?= The macro and facility can be reused in other tests too. Make it general. Signed-off-by: Peter Xu Reviewed-by: David Hildenbrand --- 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 5a3434419403..9ffce175d5e6 100644 --- a/tools/testing/selftests/mm/Makefile +++ b/tools/testing/selftests/mm/Makefile @@ -161,8 +161,8 @@ warn_32bit_failure: endif endif -# cow_EXTRA_LIBS may get set in local_config.mk, or it may be left empty. -$(OUTPUT)/cow: LDLIBS += $(COW_EXTRA_LIBS) +# IOURING_EXTRA_LIBS may get set in local_config.mk, or it may be left empty. +$(OUTPUT)/cow: LDLIBS += $(IOURING_EXTRA_LIBS) $(OUTPUT)/mlock-random-test $(OUTPUT)/memfd_secret: LDLIBS += -lcap @@ -175,11 +175,11 @@ local_config.mk local_config.h: check_config.sh EXTRA_CLEAN += local_config.mk local_config.h -ifeq ($(COW_EXTRA_LIBS),) +ifeq ($(IOURING_EXTRA_LIBS),) all: warn_missing_liburing 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/selftests/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 if [ -f $tmpfile_o ]; then echo "#define LOCAL_CONFIG_HAVE_LIBURING 1" > $OUTPUT_H_FILE - echo "COW_EXTRA_LIBS = -luring" > $OUTPUT_MKFILE + echo "IOURING_EXTRA_LIBS = -luring" > $OUTPUT_MKFILE else echo "// No liburing support found" > $OUTPUT_H_FILE echo "# No liburing support found, so:" > $OUTPUT_MKFILE - echo "COW_EXTRA_LIBS = " >> $OUTPUT_MKFILE + echo "IOURING_EXTRA_LIBS = " >> $OUTPUT_MKFILE fi rm ${tmpname}.* From patchwork Thu Apr 13 23:12:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 83121 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp2112vqo; Thu, 13 Apr 2023 16:19:27 -0700 (PDT) X-Google-Smtp-Source: AKy350byLXD7YZL1NzN/VnZhaqeRN1UWCwzB4/Tj3hs2fZ4lfXyU1AVM7+sAnrvt4wztJp9FSvKX X-Received: by 2002:a05:6870:4722:b0:187:98c4:2766 with SMTP id b34-20020a056870472200b0018798c42766mr2061639oaq.14.1681427967334; Thu, 13 Apr 2023 16:19:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681427967; cv=none; d=google.com; s=arc-20160816; b=GlnxOL2vW5z5lILs4X+9qE7x2ffsBrlIJe0oFDZhbirw9C4TXWfpM0IvsQr/ZOOQU6 qBrf8+JKqGlBxNbcizom24n7hYTwkNu+DgjLKklzd4HsJeHx2nY7F302zK8qWHlecPsf QJdEUdiV2WnjWNcEiF5C7w02XqKjCS+BwzZVnT4ZYZsLdMIrUn0L/nipECJrdExACHtM 14BfnkOMhszw1Q448RynnLWAHYPOFgRtmauBHbhxK0ZmnQxLDlavO4gTj4YF1sJ6BVAE oRL7qrpkfkWKj92UmOytg/yFhfxM9hABA3jeSi7mhyzaX/B1kPbz0FWb5M2H1Sy0q8k3 h0jw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=U50mzc3x7opcua/Zq5xIU5DytHBPjAF3IuThARnv+MM=; b=SEiGhY4mPnIQmKPsawpCgY9uxTV6yIqY/uLsTYb33FfIfQ7NA4p2j37lNOrkPt5ilC D5bkj3qbdCh16Kt+DS2OZwp96mbZd52ytksDhKfPoiDi23sPahkQnd9Mu8w4dQhj+cva N4GJb9Qj8LpMw0miGy5YPMVN7zvq/pqNQZF30idVQlSyTi1axgdJVIMLrDbNH0AnIrbq yC9DPJkfBngK55Hr3pqavrz7CN9E+NgpX6BS4Yhzi/Vq9s1OuupPiVbU3/DdSkvX2ZJP bU8EFcvlOArJx4YgEWwI3JG4QD1nY29UoPG3RBFw0ECZ+vdAuGqwAoBOYRXg9/ft6IRI yCvw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=ZCitj2Ox; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id go9-20020a0568701f8900b001879c9bc140si1518768oac.244.2023.04.13.16.19.13; Thu, 13 Apr 2023 16:19:27 -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=ZCitj2Ox; 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 S230298AbjDMXNF (ORCPT + 99 others); Thu, 13 Apr 2023 19:13:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43338 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230253AbjDMXM7 (ORCPT ); Thu, 13 Apr 2023 19:12:59 -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 8DEEA19AD for ; Thu, 13 Apr 2023 16:12:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1681427533; 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=U50mzc3x7opcua/Zq5xIU5DytHBPjAF3IuThARnv+MM=; b=ZCitj2OxvSxTiK53ie/rCtYueeBNF+T4eJaCpu1Qd1x3w94OlGkZp1z4JwpytoesD2z7Sn 7OgBzXrSXQo0ebbkBAjXpaF48lSvEUmcDoV/J4YByVkpuQpgIPANQOY7JIhCq0WO9udDpW qwzWj03kX6OtVY2oRP01gtFLmFeXsak= 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-313-B-Fx5p2GNFGh9UPmRZIy7Q-1; Thu, 13 Apr 2023 19:12:12 -0400 X-MC-Unique: B-Fx5p2GNFGh9UPmRZIy7Q-1 Received: by mail-qt1-f198.google.com with SMTP id d75a77b69052e-3eab4df3ef0so823811cf.0 for ; Thu, 13 Apr 2023 16:12:12 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681427532; x=1684019532; 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=U50mzc3x7opcua/Zq5xIU5DytHBPjAF3IuThARnv+MM=; b=NUqRgQJplkdKYiLi+dP1Rgn7rcRswOi9ovV2NfHFgLssL/1ZM5NOQgZcaaIPkYK1ZA kpxXFWdpetCbJDl4mpjsyZM+J8l2huLHhP5xWpXOdKc+FLX6OhGLZh8DQ0+vqM8pYl2g g+zJFhliUgBSUKDQyC7zbZa39SY/WVSHeT43NxTnS4SqdETTNZDxG0WeaW5hb8UQOd0d OayFEOoR7NI+H35El0V/bMc+oSRMSdRYXmPQ06OQVivYWnn6Jo/c0n3Wn9KB1Jm+Mbjy 90pywEZ/t3I074pG7xW+t0tJAdY/UNLlfaBPqa3AyINzlQ3ALln9/aOYVlB7zdtek1k2 7/Ew== X-Gm-Message-State: AAQBX9cnRGT2qw2IpE3rFrkkIIfgPT9ak/R1mncpRz28ePJtgM1De9lh h8yJARqU7P7q4IlX1tPoVptnG77I7exVObBGW7FqASk89HVrAZ8dDnxzIQUn/xaWwdjzSz6Mplo PekUemZ31Mw0K4aduclwZXZag X-Received: by 2002:a05:622a:1a0b:b0:3e6:3af1:de7a with SMTP id f11-20020a05622a1a0b00b003e63af1de7amr983573qtb.4.1681427531955; Thu, 13 Apr 2023 16:12:11 -0700 (PDT) X-Received: by 2002:a05:622a:1a0b:b0:3e6:3af1:de7a with SMTP id f11-20020a05622a1a0b00b003e63af1de7amr983544qtb.4.1681427531653; Thu, 13 Apr 2023 16:12:11 -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 p26-20020ac8741a000000b003dd8ad765dcsm814463qtq.76.2023.04.13.16.12.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Apr 2023 16:12:11 -0700 (PDT) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Nadav Amit , peterx@redhat.com, Mike Kravetz , Andrew Morton , Andrea Arcangeli , Axel Rasmussen , David Hildenbrand Subject: [PATCH 6/6] selftests/mm: Add tests for RO pinning vs fork() Date: Thu, 13 Apr 2023 19:12:09 -0400 Message-Id: <20230413231209.544825-1-peterx@redhat.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230413231120.544685-1-peterx@redhat.com> References: <20230413231120.544685-1-peterx@redhat.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE autolearn=ham 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1763105012415265409?= X-GMAIL-MSGID: =?utf-8?q?1763105012415265409?= Add 10 one more test 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 only private pages matter here, but no hurt to also run all of them over shared. 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/selftests/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 @@ #include "uffd-common.h" +#include "../../../../mm/gup_test.h" + #ifdef __NR_userfaultfd /* 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; } -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 = { + .addr = (uintptr_t)buffer, + .size = size, + /* Read-only pins */ + .flags = 0, + }; + + if (args->pinned) + err("already pinned"); + + args->gup_fd = 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 = 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 = false; +} + +static int pagemap_test_fork(int uffd, bool with_event, bool test_pin) { fork_event_args args = { .parent_uffd = uffd, .child_uffd = -1 }; pthread_t thread; @@ -264,7 +312,17 @@ static int pagemap_test_fork(int uffd, bool with_event) child = fork(); if (!child) { /* Open the pagemap fd of the child itself */ + pin_args args = {}; + fd = 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 = 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 = 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 = 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_args_t *args) uffd_wp_fork_test_common(args, true); } +static void uffd_wp_fork_pin_test_common(uffd_test_args_t *args, + bool with_event) +{ + int pagemap_fd; + pin_args pin_args = {}; + + if (uffd_register(uffd, area_dst, page_size, false, true, false)) + err("register failed"); + + pagemap_fd = pagemap_open(); + + /* Touch the page */ + *area_dst = 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==true). + * 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[] = { /* when set, child process should inherit uffd-wp bits */ UFFD_FEATURE_EVENT_FORK, }, + { + .name = "wp-fork-pin", + .uffd_fn = uffd_wp_fork_pin_test, + .mem_targets = MEM_ALL, + .uffd_feature_required = UFFD_FEATURE_PAGEFAULT_FLAG_WP | + UFFD_FEATURE_WP_HUGETLBFS_SHMEM, + }, + { + .name = "wp-fork-pin-with-event", + .uffd_fn = uffd_wp_fork_pin_with_event_test, + .mem_targets = MEM_ALL, + .uffd_feature_required = UFFD_FEATURE_PAGEFAULT_FLAG_WP | + UFFD_FEATURE_WP_HUGETLBFS_SHMEM | + /* when set, child process should inherit uffd-wp bits */ + UFFD_FEATURE_EVENT_FORK, + }, { .name = "wp-unpopulated", .uffd_fn = uffd_wp_unpopulated_test,