Received: by 2002:a05:7412:3b8b:b0:fc:a2b0:25d7 with SMTP id nd11csp1217134rdb; Fri, 9 Feb 2024 14:16:22 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCU2tUbopOquuwRjK6oLAftx1GmaS+0m5W273tXCYt2UYCC7EmxJbba6SS0u1uDXYJAf1IHN1QDVMXajSH/eKtIXdqavHHyeq0D+ovM7tQ== X-Google-Smtp-Source: AGHT+IEi98h2RBGNAJ4PPyZHgH0EO2jf/WncI5v94C4WxLfAj5rtP5DWTLSh+WeO/Ke4Ww+ttV8r X-Received: by 2002:a05:6214:b6c:b0:68c:cb14:781f with SMTP id ey12-20020a0562140b6c00b0068ccb14781fmr629440qvb.4.1707516982062; Fri, 09 Feb 2024 14:16:22 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707516982; cv=pass; d=google.com; s=arc-20160816; b=GOshoWXzvinvJ/91rNWdeOirFPztMH+fILRYsBKjk7XNqzZhF31tPXzAoDcjiswtsf s0uX1T1IYCjp6dm3bHJ2775zqPC3BUyuA1dGUFjB+LLPUjflTC/xuwBe0JOBdvrSq1U5 3gKHwbBqVtBeDrfHDEyLYM0JG8z5bfG7NLFQ7kAlv+gBUa12Ai40Xrq9R3hlUKT9EamA qvHRTlRHXLxIR8GxkFS8S4atrWieVzbWmGEbS7zBqxq4k6/Ei23bCECDmxwlSr2pBoC8 H3OCgr9Cjz1kk3VxD6CHy0vWvMp8nBW5YeR7x8U/FkeNkXupIhVDDCIviPoNv4yXRrWt vd3w== 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=M/Ux9HRgPwmdR3zuVPtEfBlFP5wVpYCc/8Whwmc2o7c=; fh=MUcqeJ3mPX8XSN533A0QoxJIn4fkrLOzkd/GP3vLJcE=; b=O830Ww1HRPqEikU0DErA5nhfJ0JVRirawSDwmAPwCNBgME+BPlgFL8+LIkmqPdn7Pc DDqTcPnzfTZg0GNOOGMxBmp1NKUf7x1plppoY30wlp70VhwXx8gXRNwzFIaU98Crh1Zp 1qCqjrWEvY8sHG70ZGTrOS16ioAzd8S5JmyqeD0Xkhx26J8UNNokCaG1mfamCO6ZpgdE Rl5T93Jc+d9ooKrWoFicSuCOAhRUgRg6R/9gLF03vbeyhzj9PX9DSgchkssMPXWPsHx8 Gopxu+a0D2gcUwaY0GTembI/u86MXrM7QgPdHRT3RttEJrYOLyj/pqvbdUiSbzb1NVGR FONQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=OqWnVdxW; 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-60075-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-60075-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com X-Forwarded-Encrypted: i=2; AJvYcCUYzFSiqp/Q8JwjGiUv9yYoWuqRupeXyE3hTopJjNokZ/xSwbNTJ0o+TmppgVKNYHjfWZtyKX6OsJbCBDjShUfhMaGSVDBrr4Sbt2kUUw== Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id g4-20020a0562140ac400b0068cc1897a9esi3098342qvi.51.2024.02.09.14.16.21 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Feb 2024 14:16:22 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-60075-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=OqWnVdxW; 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-60075-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-60075-linux.lists.archive=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 ny.mirrors.kernel.org (Postfix) with ESMTPS id E5B0A1C20E8A for ; Fri, 9 Feb 2024 22:16:20 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id AD0F7383B2; Fri, 9 Feb 2024 22:15:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="OqWnVdxW" Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.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 548EF381CF for ; Fri, 9 Feb 2024 22:15:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707516939; cv=none; b=avXIo2tQoDZgaDrE8Coss+K+zxoCg+d9FONBId+YAHvTeysvEM/TA/iJLZ9uZBtVI8X3gFrR/W8bW6Y6GPz9BLL9BWSD8zcTPAukfN952ZQ4efx/qhHsswErRgoIhP4FiNzqw57kjlvQGTi3C4owAxbD4pug8PZxKVOU+dQzhxI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707516939; c=relaxed/simple; bh=gJjdcDN9SIZ57EMSsnxvz4s6wGh9ngGev4G0JhDn5Lw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=UiEODUYFR5E2Yh+27ZswlcVMZazAn97r6iWwB2nPhhGwxgHr1dN+jdk1yf0IzKZXexQ73TPSYkeleP7ATQzkpL9GDNEMLGhAb4xg9KRhdMDgVkBsSWJIfHF3Eje3firyUYVmww8rmNR7n2xxgH4C8b0V7uN0SXvC8QJv3BqKYm8= 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=OqWnVdxW; arc=none smtp.client-ip=170.10.129.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=1707516937; 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=M/Ux9HRgPwmdR3zuVPtEfBlFP5wVpYCc/8Whwmc2o7c=; b=OqWnVdxWb+fU4UNVKI13tV0Q3mtH+bxe6CsHFZlepgkzWo5oph2o3FZb3lx17JXwoL0qTZ DNctaMHmzjd4k2ig/ncnSfsyK0/4f/cAIp3xXnCekLSua/v9wSarciEa7ChOE7mVUrY0Y9 +pU7vKPuD4OCFZFnhtwfOe34XPXApRA= 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-639-ojxWH6gHP7CoaMSJUSv5fA-1; Fri, 09 Feb 2024 17:15:32 -0500 X-MC-Unique: ojxWH6gHP7CoaMSJUSv5fA-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 85BE9185A780; Fri, 9 Feb 2024 22:15:31 +0000 (UTC) Received: from t14s.redhat.com (unknown [10.39.194.59]) by smtp.corp.redhat.com (Postfix) with ESMTP id 512C21C14B04; Fri, 9 Feb 2024 22:15:27 +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 v2 03/10] mm/memory: further separate anon and pagecache folio handling in zap_present_pte() Date: Fri, 9 Feb 2024 23:15:02 +0100 Message-ID: <20240209221509.585251-4-david@redhat.com> In-Reply-To: <20240209221509.585251-1-david@redhat.com> References: <20240209221509.585251-1-david@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.7 We don't need up-to-date accessed-dirty information for anon folios and can simply work with the ptent we already have. Also, we know the RSS counter we want to update. We can safely move arch_check_zapped_pte() + tlb_remove_tlb_entry() + zap_install_uffd_wp_if_needed() after updating the folio and RSS. While at it, only call zap_install_uffd_wp_if_needed() if there is even any chance that pte_install_uffd_wp_if_needed() would do *something*. That is, just don't bother if uffd-wp does not apply. Reviewed-by: Ryan Roberts Signed-off-by: David Hildenbrand --- mm/memory.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/mm/memory.c b/mm/memory.c index 4da6923709b2..7a3ebb6e5909 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -1552,12 +1552,9 @@ static inline void zap_present_pte(struct mmu_gather *tlb, 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 (!folio_test_anon(folio)) { + ptent = ptep_get_and_clear_full(mm, addr, pte, tlb->fullmm); if (pte_dirty(ptent)) { folio_mark_dirty(folio); if (tlb_delay_rmap(tlb)) { @@ -1567,8 +1564,17 @@ static inline void zap_present_pte(struct mmu_gather *tlb, } if (pte_young(ptent) && likely(vma_has_recency(vma))) folio_mark_accessed(folio); + rss[mm_counter(folio)]--; + } else { + /* We don't need up-to-date accessed/dirty bits. */ + ptep_get_and_clear_full(mm, addr, pte, tlb->fullmm); + rss[MM_ANONPAGES]--; } - rss[mm_counter(folio)]--; + arch_check_zapped_pte(vma, ptent); + tlb_remove_tlb_entry(tlb, pte, addr); + if (unlikely(userfaultfd_pte_wp(vma, ptent))) + zap_install_uffd_wp_if_needed(vma, addr, pte, details, ptent); + if (!delay_rmap) { folio_remove_rmap_pte(folio, page, vma); if (unlikely(page_mapcount(page) < 0)) -- 2.43.0