From patchwork Wed Feb 14 20:44:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 201116 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:bc8a:b0:106:860b:bbdd with SMTP id dn10csp1488657dyb; Wed, 14 Feb 2024 12:45:55 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWkMA0oUdv0QR3vzBMUhsFG5lWwiLMZsxEUQWgRsxLoX3DJ2/b6a4YJDiW/RJAOV8ANgHCRq9/ychoKYmGg7rvRWPd7rA== X-Google-Smtp-Source: AGHT+IG0wW3Ntc7TSfHSY4JOrLOyXamqLk6JvvCwYRaj28lnmk/3mrWQ1+exk/qCHP/r/asaOBaT X-Received: by 2002:a17:902:9a07:b0:1db:4cd5:8dab with SMTP id v7-20020a1709029a0700b001db4cd58dabmr3796031plp.40.1707943555594; Wed, 14 Feb 2024 12:45:55 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707943555; cv=pass; d=google.com; s=arc-20160816; b=X81UoA4fS09s5SS3elS6L7Bjwrjw4PwxFGR047jNcbO/1y396jex9CagmGo5+NIAyH r/YbpQWk4cWZUW80XmmDOt5tAOUyoPba1dUNZD6rFXIJeqX5KJF/t5VVWBHEet1DeEGu LkP9tfmcYBNPGP/adH7RUvxX6nAgzPw/wnKBScPQusqQ4SnboP1nC6VEsY3UHruAg5XH 8gyspuG9gZBtt4hGIL2W2DfgfeB3vndPciIy53v2BAd8Zhdp+RBlHbD5cPgA8Lwu1Vn8 JQEIyZB5Po4shp3LYIijFZ+U7jqRc3jbbYje/IJ4bwxALwOTgFA7fa7nN1Xc4cWZvoxp cJIQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=fqQsnr9EFuS72Lg9egurC5IFPM5MYMM8dNEJpcWm/gk=; fh=EZtm3QLOso1qv2LKHiFv1RFVxi0GSDFNvJl+3hP10CM=; b=rMNldyMZa6X5tiVvWwcBfC3lCPAbhdKnQLo7Uhd6pk00M2ZKdoMYe6e5K5TLZruXQL 2nT+1kTz2zP3XyQ2w8KLadIDnO/MSM6uUtrL9lXqtm688hNlZtHq+MFtQZ/GKl23o7Px 9+3SmWei0VvCv2uTR/hmZ1jZJH/4KVvqGUruWwe1kC6rJUZqiSKvBDNoj9PXDgliFnAJ XRxFl6S3z6IJsj3D804S7FxHShQTbqJus9rdEAIY2ptqoJzx6G5w5jTebrzUpnuGhOCw IRcJds53pa6PYXSVKaRhM8AB387qmuru3tRpVql1To4A3RqS5NLz2J6GIMNPCJH1xhk/ 5SfQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=W1RBDJtB; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-65948-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-65948-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com X-Forwarded-Encrypted: i=2; AJvYcCWk8ySENeEFt+pYuogGJIgJ9VUsarV6FL1fL4npe2hK6jMaGA2yDsldvvCk32z2fRjOWg7qyCqnfmvX0e6IztvonZB6Gw== Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id n8-20020a170902f60800b001db4cafb9f2si2472031plg.517.2024.02.14.12.45.55 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Feb 2024 12:45:55 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-65948-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=W1RBDJtB; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-65948-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-65948-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 68FC82860A5 for ; Wed, 14 Feb 2024 20:45:33 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id AA0C413F01D; Wed, 14 Feb 2024 20:44:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="W1RBDJtB" Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 18A6513DBBC for ; Wed, 14 Feb 2024 20:44:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707943496; cv=none; b=Y3V+yIn06K/QstJVfZqzPnt/iOFoqtnJluMeJolj39Bp39NOq81Lzan6Qo0hSfTEqZIBQ+hy/PzcsfxTWOxuLJRJKtRJ8+ZVp2VRqFC5df2W3XCvgkbWwmaGXyh/Zo64rU6eVpwtsABytcD96FdEc9D4QbmgGgVY8Gl9K9TLDB4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707943496; c=relaxed/simple; bh=iKdLPd0l+BlZy4NkWR4PBv4pdjA5DJYrKJe9pxJlmq0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=dCoi4CZ7czrQ0+oxLVAMmUE3EXlMZkyWYMgt8/YiZd42WXBTZdFZx4z+DnSiQMVrsz+GW8BBNN6dca1DwUm4IZvzH+TKqqcU6y8YunovGeJ5TF8gjfeIumeLLdNopxlhuGRTf3LfohDXJ6wMy50fLe7zF0iToeNYPR2/p0jnKm8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=W1RBDJtB; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1707943493; 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=fqQsnr9EFuS72Lg9egurC5IFPM5MYMM8dNEJpcWm/gk=; b=W1RBDJtBwg9W+BfbT6HUgiiUvjjcIhM0oPUPegvDgLyLaH1uQ/3AzflJdVNzp2tC4YUDsy uRhtK9g5gZ9McblshTAH4b4FlMMINq8sVeqO9aDns+7hQrQuI7QQL/fzhA8jaluOp7a7zs x97Xozf/J+DBpyapPn2gKITmhtTbtJY= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-226-pgh-LenmOMa4VgIEuXmFnA-1; Wed, 14 Feb 2024 15:44:50 -0500 X-MC-Unique: pgh-LenmOMa4VgIEuXmFnA-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 0A351185A782; Wed, 14 Feb 2024 20:44:49 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.194.174]) by smtp.corp.redhat.com (Postfix) with ESMTP id 438201C066AA; Wed, 14 Feb 2024 20:44:45 +0000 (UTC) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, David Hildenbrand , Andrew Morton , Matthew Wilcox , Ryan Roberts , Catalin Marinas , Yin Fengwei , Michal Hocko , Will Deacon , "Aneesh Kumar K.V" , Nick Piggin , Peter Zijlstra , Michael Ellerman , Christophe Leroy , "Naveen N. Rao" , Heiko Carstens , Vasily Gorbik , Alexander Gordeev , Christian Borntraeger , Sven Schnelle , Arnd Bergmann , linux-arch@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-s390@vger.kernel.org Subject: [PATCH v3 02/10] mm/memory: handle !page case in zap_present_pte() separately Date: Wed, 14 Feb 2024 21:44:27 +0100 Message-ID: <20240214204435.167852-3-david@redhat.com> In-Reply-To: <20240214204435.167852-1-david@redhat.com> References: <20240214204435.167852-1-david@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.7 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790908621358636642 X-GMAIL-MSGID: 1790908621358636642 We don't need uptodate accessed/dirty bits, so in theory we could replace ptep_get_and_clear_full() by an optimized ptep_clear_full() function. Let's rely on the provided pte. Further, there is no scenario where we would have to insert uffd-wp markers when zapping something that is not a normal page (i.e., zeropage). Add a sanity check to make sure this remains true. should_zap_folio() no longer has to handle NULL pointers. This change replaces 2/3 "!page/!folio" checks by a single "!page" one. Note that arch_check_zapped_pte() on x86-64 checks the HW-dirty bit to detect shadow stack entries. But for shadow stack entries, the HW dirty bit (in combination with non-writable PTEs) is set by software. So for the arch_check_zapped_pte() check, we don't have to sync against HW setting the HW dirty bit concurrently, it is always set. Reviewed-by: Ryan Roberts Signed-off-by: David Hildenbrand --- mm/memory.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/mm/memory.c b/mm/memory.c index 5b0dc33133a6..4da6923709b2 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -1497,10 +1497,6 @@ static inline bool should_zap_folio(struct zap_details *details, if (should_zap_cows(details)) return true; - /* E.g. the caller passes NULL for the case of a zero folio */ - if (!folio) - return true; - /* Otherwise we should only zap non-anon folios */ return !folio_test_anon(folio); } @@ -1538,24 +1534,28 @@ static inline void zap_present_pte(struct mmu_gather *tlb, int *rss, bool *force_flush, bool *force_break) { struct mm_struct *mm = tlb->mm; - struct folio *folio = NULL; bool delay_rmap = false; + struct folio *folio; struct page *page; page = vm_normal_page(vma, addr, ptent); - if (page) - folio = page_folio(page); + if (!page) { + /* We don't need up-to-date accessed/dirty bits. */ + ptep_get_and_clear_full(mm, addr, pte, tlb->fullmm); + arch_check_zapped_pte(vma, ptent); + tlb_remove_tlb_entry(tlb, pte, addr); + VM_WARN_ON_ONCE(userfaultfd_wp(vma)); + ksm_might_unmap_zero_page(mm, ptent); + return; + } + folio = page_folio(page); if (unlikely(!should_zap_folio(details, folio))) return; ptent = ptep_get_and_clear_full(mm, addr, pte, tlb->fullmm); arch_check_zapped_pte(vma, ptent); tlb_remove_tlb_entry(tlb, pte, addr); zap_install_uffd_wp_if_needed(vma, addr, pte, details, ptent); - if (unlikely(!page)) { - ksm_might_unmap_zero_page(mm, ptent); - return; - } if (!folio_test_anon(folio)) { if (pte_dirty(ptent)) {