From patchwork Mon Apr 17 19:53:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 84427 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp2377111vqo; Mon, 17 Apr 2023 13:13:13 -0700 (PDT) X-Google-Smtp-Source: AKy350ZBsLh3vOe/2DKXlhhcTFxPXN7KG3HpnYrSBm1RdyGDJfUn8CnP07fSTNSZNUQlGxBuNh8J X-Received: by 2002:a17:902:8e8c:b0:1a6:81e6:4bfb with SMTP id bg12-20020a1709028e8c00b001a681e64bfbmr154118plb.9.1681762393063; Mon, 17 Apr 2023 13:13:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681762393; cv=none; d=google.com; s=arc-20160816; b=w+ye0bzSQ9ek9p6Vc9+zywxC0VYUrnQ76l9Rgjg9iJxRBx9PC5J8y+LZvJLNfg9QjJ hkbRvXw4q6OTmEoRsF/4kmzKk+Vp25yT7RzmVRQA57vDXgQquQ0LG6b5e9tLpQQCIAzM qsB7Roe7fP88vEipmWbImvNRozDVyTtiZODPMxDDOmpeYQ3xsChFt6k3HFMBZ4Enn1RT O+8MwAODjQDmSOGXuPLeyBUgduaH1IgG8KUy7FO2nCOW/w8VT+eu6SezIDqNfpTiC4Yv TfJ3K3vZBDHYU7NtZctKjLuaKu+7RltRfTiVeLq6GoQBw83y/x+kPbz9C889Y2VxV7NU k7OA== 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=4kH7JNUmkrK1TIcnPTAjFCorjkRaA4Lvx3pRbnAdGF0=; b=COLb64IcsdEfCdp6VsBOYS2cGngsz3Ew6336st6Er2Dr0QGlzIZ/uC5TB+c792le8h 7pTRyAGt/GHdegWZFKHYrmoKjIYo6QeY6deIBI8ejImiqD+lkRP2AAA6Hm/z0r3MlrSV 9QCvXpck5LrDtyylckZ6kvdKgSNsexJNONky0UdmqdRUabFwE2Vi0EG6bWVT1vuOAFWH oIZ3xwf65X2x+69V3cYC/mSfcp4KcqybEicLXqJxsmcqm9RZjJAKjkp3dd4nryhDXyaM ySdeEZcY15KcTqDIdALG9MADmsAUvh1GhKWSoLJuxiHIxbA7CefFFzMJKM5fXXfq7fc5 ppmg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=DhpRqkZR; 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 c10-20020a170902848a00b001a1ddcfeba2si12237123plo.492.2023.04.17.13.13.00; Mon, 17 Apr 2023 13:13:13 -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=DhpRqkZR; 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 S229804AbjDQTyb (ORCPT + 99 others); Mon, 17 Apr 2023 15:54:31 -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-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_Penttil=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 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,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1763455682753176032?= X-GMAIL-MSGID: =?utf-8?q?1763455682753176032?= 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) 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,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 = 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); + bool uffd_wp = pte_swp_uffd_wp(entry); 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 = swp_entry_to_pte(swp_entry); if (userfaultfd_wp(src_vma) && uffd_wp) - entry = huge_pte_mkuffd_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 Mon Apr 17 19:53:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 84430 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp2377613vqo; Mon, 17 Apr 2023 13:14:16 -0700 (PDT) X-Google-Smtp-Source: AKy350bpp3O9OXg8pSu/sGk1tcHHxCJjzY0Q4Hg9lOVb8KiBrEZiyJ1AVQjAlroAYG75xjR9ZGK3 X-Received: by 2002:a17:903:2596:b0:1a6:5274:c1b0 with SMTP id jb22-20020a170903259600b001a65274c1b0mr66534plb.60.1681762456265; Mon, 17 Apr 2023 13:14:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681762456; cv=none; d=google.com; s=arc-20160816; b=J3CT8cJFfqRURQzZjdgaj9r2fDm/xpQLIz2Q1kMBqFX6CEoHIY8VGX0QszKhyFkCYF tq2bVJOEKr9ewFN4euTIfljSO7bYGXn0XatVdDumIOoVsQaZ5eMiBx/fBFmvjaElmQlW vWpYYD3rYknXIs6dpwfwNbqST+Atzl852UYpHCRXhqknWAjAYJJl348nB2ByATVnYsw4 XrtkHzE16n/C/ztQPc10dccrvNCtOQI7i0veTV3EQyK1pEz3XIfLbcSPoeQ2VsGRnUsP TH0r7I3DWxaMjZTdiXthUk8Bjx2JG+n1o1SGUKAZVJnfRjnFov1rfpJyamef+9HD6mV7 mzuw== 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=ZZajLRXbE1gHSG0/rWcJydl4VAkwntQbZfH27HHj2cI=; b=bnA6Ia9j7m/xYNR3VHCEHzwl0KIYolG9gx24+aPa8c9PbRCILU6JOQyifoVNSAQHMq p8treythwXnujsW84oof41QjwIxJYM9Q2KzbMi/+/dNlN8imxEirbKAAeAnjcBZHA3cI OjoXwC8E65BRrn7i7TNVjRTpYuDBWPqhOiulQboEzJi2X/UGgucdMBSW0lLOYFujB6jB g3o0II7wYwCaoij9XBzWkvwHutt3PQ07LpaJ5VQ1Qs3RA8WPd0INZXBLOA1F/QBMPGCg /Exwyb5HmnI4AyVeDOqUtYqaFaX4O8/4gBU5Q40SnB/Gnrp8DJ5i/Re2RRU14xWcbHT+ HbXQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=UERaqskK; 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 e3-20020a17090301c300b001a6a18a5a27si9916276plh.79.2023.04.17.13.14.03; Mon, 17 Apr 2023 13:14:16 -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=UERaqskK; 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 S230121AbjDQTyd (ORCPT + 99 others); Mon, 17 Apr 2023 15:54:33 -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-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_Penttil=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 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,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1763455749239349983?= X-GMAIL-MSGID: =?utf-8?q?1763455749239349983?= 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 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 = 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 Mon Apr 17 19:53:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 84429 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp2377602vqo; Mon, 17 Apr 2023 13:14:15 -0700 (PDT) X-Google-Smtp-Source: AKy350YG9OX2D0I62wS8hz4FxvYG3TCBLTK3YhFlbS94WdE6TK//j3gbDzfUt34VGzq3yjVJGPDc X-Received: by 2002:a17:902:b716:b0:1a2:85f0:e73f with SMTP id d22-20020a170902b71600b001a285f0e73fmr121273pls.35.1681762454722; Mon, 17 Apr 2023 13:14:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681762454; cv=none; d=google.com; s=arc-20160816; b=kEN2zwFJM7n1XOcEAhK0CXAB2dvr91H+XmJlQKUb1rW8dDacX75J02Hy3ahkEu4nhh k/LUJrczjCNiOR3UMrLT3KV3Mz79cVUN4FlCYAg5la5KbyaEUDqW40ibFFB2dDUPBsmb PNqPEmuuMPgkS4/UEpLL++LgiwNLpa2jvFJ+hH8xKwPVOjnOUeYwrAgiymhAHzqv9CwP GTSZcdni5xTYM+M4fePLvKgqVkWTAqdxD2RiH6NNy3nvTFg/Q8aLjtIds3uovm7qjPZf PRxCdpl/vhh5YaqIe8In2QZi9WKGpPOxKOyy+8pOsjS6EGGQcGCCkEuduMNbJVPAjho4 Q0TA== 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=AhW1wQF9ie6n8DSqgU53V5nLd90ZudqLBFzO+PTy+FPUDTYu0GSwWkMT6cfujo0zLi IP9gOOKZ3EXP8tuDWusAm9tdS/RgCkPch+EbD84cYOi8GhFAItJOhgUpQMr+XqhyseHu MUsV+Z0gR1iSuqzumewlh9RlR882cS+f9aM4M7qO2BmCpqNSCqlYnuoO2Np4+AX9NHOy BY2FZVGjvsdzcNEHVEUZ5V1ST0toVHQ7BFccVPb/yghqr1BaEfSvb4ASNopJ58HpfZY5 kHWLurF87AGjb9MWfq8EEBU09fEzPtI8hmP9RM8ucasEf/NZENefcgpWg2KAo89QMz+H QekA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="ZP/rrgSo"; 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 k1-20020a170902c40100b001a1e237060fsi13580613plk.344.2023.04.17.13.14.02; Mon, 17 Apr 2023 13:14:14 -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="ZP/rrgSo"; 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 S230229AbjDQTyz (ORCPT + 99 others); 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-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_Penttil=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 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,T_SCC_BODY_TEXT_LINE 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?1763455747648315809?= X-GMAIL-MSGID: =?utf-8?q?1763455747648315809?= 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/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 Mon Apr 17 19:53: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: 84424 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp2376869vqo; Mon, 17 Apr 2023 13:12:44 -0700 (PDT) X-Google-Smtp-Source: AKy350ZWL9HuM7HkSzyFXHkLyn5p19/np/tl/DVuk9fclOizf416PmFJUkAgF7PkygfNM9dfFGJD X-Received: by 2002:a17:90a:6305:b0:246:91d0:9e6f with SMTP id e5-20020a17090a630500b0024691d09e6fmr16569060pjj.3.1681762364163; Mon, 17 Apr 2023 13:12:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681762364; cv=none; d=google.com; s=arc-20160816; b=oV+JLifEmvZLGWv4of3kiTaADqddF2QRRg/2fwjp8jjHhozrkPOt8uSIQTn8p1OXMs QlI8kCTlQ0AAEE4gzVSbR5PBaF6d0nzujjBkeM12eB37jPVGUU76nm9XVFTJBb9JkGXk zUsr853wGTxigx+q4X7qO9d84/zYyIKCeQ+1hHCx4DBDbhvGmIFEYlivjJmip914918T 7Myp7UD4eaC1NEmhHJvKNhi3HH4uk4OZ3RdO5bS+47pNDenKI4raKaItBKNjLIZZfq2f K+VKxB4qZSXns8odo8JZJlnAFsG41M13Ja9uRTrmN7qKaTaEOhsg1wceZqJfaC3YJ2js yAEw== 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=b/O1MgiDGTxtU6od8Kj2stSb2r+CglAPXoQzcx6byUSWvvu+aqM/2gYqq4l3O6/877 Qxibc1BvG+d3TImNN3klz58vngE7wShtasvVR/92aVbNreoEcGOXwDwgRz1Loc5V9O8T PuxlqmyLXlDU6MazKKIm0Q2mPjpi0UmuhIjwysgrJ4w/8m28fV1QmMr2fiTdVqY/YhkQ 9/Zn96FY7a7fE4N4emTn87LQ3AltoIv1DR5Gp1TNfvWtoXlWjpoaJYPk3kvlF8sX9yeT CjppAkwVA/WpridUqsFX9qSPjkZeNxHEwNCp6zgXT1C8ACrK7kie0R11+aeYx2+y5SY+ GF6A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=DlGuFqq5; 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 f16-20020a63f110000000b0051ba1651735si4784863pgi.186.2023.04.17.13.12.32; Mon, 17 Apr 2023 13:12:44 -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=DlGuFqq5; 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 S230168AbjDQTyw (ORCPT + 99 others); Mon, 17 Apr 2023 15:54:52 -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-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_Penttil=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 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,T_SCC_BODY_TEXT_LINE 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?1763455652944270852?= X-GMAIL-MSGID: =?utf-8?q?1763455652944270852?= 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 Mon Apr 17 19:53: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: 84425 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp2376918vqo; Mon, 17 Apr 2023 13:12:50 -0700 (PDT) X-Google-Smtp-Source: AKy350ZFpowyF2uyMrbZK1d1nuItjdpdFqhp/oqTLJbNDnBRDs6ILKW+HjkC3EBE1KNyeSe2EKPq X-Received: by 2002:a17:902:f203:b0:1a6:81f8:63c5 with SMTP id m3-20020a170902f20300b001a681f863c5mr148869plc.12.1681762369820; Mon, 17 Apr 2023 13:12:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681762369; cv=none; d=google.com; s=arc-20160816; b=SdGP6xDQ/oGhLMGZhY3k7ug3qSiHDiD7S8tmq/6UHf2XOSOqrHMH8lu6gZj+X6fvwL I5WtCh7fVX0Bgud9IbA4B9h+IqvZIE3fHWGqpncOuwV5/O+2PWWeS0slD6ZuVCQLZKcU US0tuRFvzQ0+1w7vgg+3OMjnrx0ZLOYDDc+OCcoW2emdzi4VDlARRN/s80J/4n9b9yQL 8vTd4jrOQlfiAjM0qW0/it0/sQ2kibZL591ZV7mGDhGtytuvqJUt5eZWf4jOeIBBXc3G br65JuAM6P5cZgS0jS4MsxmjSviUAThkl4yxU67RNYuq1C7zNAPR6XQStn2t38/++K7D PdMA== 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=nL6ukD3uQsP0j8aiX/3IiND2lFTW26LLUmGTHQFycOk=; b=O07mfPmt/VVnWGYblNgkZ2QXHT20u6srxsNGkRsjcmjo86irk/VadMDogzHubYuJCn ciymUTgLK/XpwoMuTzOaN54YIWpopuZP/dpwJ/j6ZGgJyVvbkyc30iW2iWEx5ooKC3tF h8MUQmCPAT3rblNLpsnO9aSU3fuFQUMTU0Vtq7WjE9C9dCwLq86soMQv9vVmdeiMPpnz xhtRAsRGNN5IV2B6CkOds20pS6c8op3OH8+/WwilIJN7bSiD2hEjYcyXzsNXbpEium0G /ZTNzrtp1D1mgBxzFRwnwk6w+Of9xMStsgi7MRKxLIj6GuXCRHqefoWkVrDoD9eJy5OG wPow== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="F9YY/XJr"; 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 k1-20020a170902c40100b001a1e237060fsi13580613plk.344.2023.04.17.13.12.37; Mon, 17 Apr 2023 13:12:49 -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="F9YY/XJr"; 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 S230286AbjDQTzC (ORCPT + 99 others); Mon, 17 Apr 2023 15:55:02 -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-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_Penttil=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 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,T_SCC_BODY_TEXT_LINE 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?1763455658522274278?= X-GMAIL-MSGID: =?utf-8?q?1763455658522274278?= 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 -# 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 Mon Apr 17 19:53: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: 84421 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp2374998vqo; Mon, 17 Apr 2023 13:09:01 -0700 (PDT) X-Google-Smtp-Source: AKy350YhCdmT9dYAIPj3sZvJ1XnSQfCL1jBlePaPnIAFltpxfSWsvBSOEqOqWGHQhWRNLvxPGJXH X-Received: by 2002:a17:90a:eac3:b0:246:cade:8725 with SMTP id ev3-20020a17090aeac300b00246cade8725mr23462939pjb.22.1681762140865; Mon, 17 Apr 2023 13:09:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681762140; cv=none; d=google.com; s=arc-20160816; b=azRE6JoDA7wS5+Gz4PySQm5wG+hfVsso7WdCWbwckFlc0/b0kW9l6D93EZ09m2dfSV PL0NFrP1ns+rhX/wyygFOFaCnyMTcafB7cKSNV6gLAoiKBGMfqyqM2RLm8CRnv6rL2GV dCAOZGx8Tkf9nIii/CJkPDfxmvmmv8wuFk9zvcREz/EPBw9PV1lHwzyhgxzdglKeqq2l TmdnEmKIsOmbfdDrcctHrwo+KhwsMpAaCfrp4h8wqH3K7ZYvD4+TGawI4AGKxqMF9oxA bj7DoznV9ZShn6Sdw8yiym2zy3y5c1b3clXHxVcE4VE3azilPNwmaA7VCOD1YSWwJPul BrYg== 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=y7gltokQzFShDFjL1ksnnTsH6QcBiNJdLCM4WiStCBA=; b=ve8Ml9gMLhP1TwDU3CLMzcJmY4zWX2HpVwfHiXaH5hQe9byrTSVAA9hAPS1HjY/x4t N7ikBc2YTrTK+AoO9HcTo54k6xcxbaHdjUnMxIAz3ZVFMdFUlVkMsxrFk6iEl8Q3csIH 5c5mnRDIOpiiq5tNzRmGZlO3xtdlSuTNSOg/lTDf7OMgxtXJ7/ikWWivu7/PDPqXsZv9 LZHP1PCWagTW4cvy2NFkzM8Bj2GR7ToK4n0pShT+DDGrxVLSLPCVWAM+7O/p8KpB+K05 D8oFc91RgV8R05Iy6A+GxqVzVG5QgY5I8ZjRlbhc/amU6eEBff8ZS+TV0n+e5CZKBxCF GVNg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=XpbbJfdY; 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 s63-20020a632c42000000b0050be799a2fesi12486916pgs.672.2023.04.17.13.08.46; Mon, 17 Apr 2023 13:09:00 -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=XpbbJfdY; 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 S230347AbjDQTy6 (ORCPT + 99 others); Mon, 17 Apr 2023 15:54:58 -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-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_Penttil=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 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,T_SCC_BODY_TEXT_LINE 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?1763455418578506383?= X-GMAIL-MSGID: =?utf-8?q?1763455418578506383?= 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/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,