From patchwork Wed Feb 14 20:44:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 201125 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:bc8a:b0:106:860b:bbdd with SMTP id dn10csp1502449dyb; Wed, 14 Feb 2024 13:12:14 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUB6rwaqEhOrOtgk+XWeQP4aOwX+7PrW30M176MxB5gO5bI9krR0iWUyjxojNIYZ+eoANO1D9ZQc612JGtyAGFY29FMuw== X-Google-Smtp-Source: AGHT+IFSGkV15QzNwRXIO8ZjNNea6mzXsLUyLLyD0ztOpi7Os0gjFvK+vrRQrRW4vZCDXlZrUsET X-Received: by 2002:a92:d08e:0:b0:363:dfc7:acc1 with SMTP id h14-20020a92d08e000000b00363dfc7acc1mr4058048ilh.2.1707945133957; Wed, 14 Feb 2024 13:12:13 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707945133; cv=pass; d=google.com; s=arc-20160816; b=0crYyLjgjuFi14IBGO7WN7F+z2/6qYBkBgCEH6QevKBJgDr9S4A4CrAyD+439Spcbj akx3J6v+HcsaAEQqLdUiTidCx2SDl/O06JC1eiTha4SraVYPA3T4AN/H0sO7oilq2G0M RJ+NECFu/LdeICOeBHxOk+Pr8OPKqttwJvLornjXame4MnQP5qHN3whyhd70qJV6FdR5 fFT1CiZvNZ87F3YjdJ73BsSFBlUOyGKAJFw+3RULfgk9P9yHkSkGV7XAb4orL/5iNetG vyA2qoXrnh/OZrrvjPQfUecu2GxpKJoCgQMYhtOr8f1YBW6W/iXOF6gaQrPcV4Rhrsp8 Qlkw== 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=n9j1KYXQ4zaztvvj4LKspKPgCJOW47QsQhj6IMoW5Lc=; b=V0t7uubv2fkj+EwPoJZSXIHe6LiQZANvb3lBE8p6DRkpHtdqg/fCuXmA15cJmAK2Dh I3DjNYx8oL/ghaoPQoXf93/o0W3w6vtub56MIeCyN9z8wVbQdnn1D5BxN3pBiXs8o1tk vnfg79gB7BcljAlJ52koDKWPJ7LA7eq8Xg+OGHniGj35FZJqus1r4G6bEDtA7r2+aagN 2IQLuTYWaoN6Ig4rVjTcURi81cKaTyzIpyMdD16vXaU+iTtc3q8vd4xzAgaU2E1VXB4Y NxEmJ2ZCh4XQQl7xTnHuV6N/ZscZK2RzXYp+lAYssMmWYngqZRqLJy8k06boWzoh/f2Y ypvg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=jPkqsZIp; 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-65949-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-65949-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com X-Forwarded-Encrypted: i=2; AJvYcCXlPRr+0J9bv97dSPKUTu6OX81TN7tIvUx/Zi2t/rrir4XmcUPZ+PSaomOYR+2OuSIzb4mMRLiPvk0J/drNyH1ZjNSdog== Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id m25-20020a637d59000000b005dc432da142si4301629pgn.479.2024.02.14.13.12.13 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Feb 2024 13:12:13 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-65949-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=jPkqsZIp; 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-65949-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-65949-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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 8EA11B26B4C for ; Wed, 14 Feb 2024 20:45:49 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D392E13F017; Wed, 14 Feb 2024 20:45:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="jPkqsZIp" 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 98A0013F013 for ; Wed, 14 Feb 2024 20:44:58 +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=1707943500; cv=none; b=YheeeLTSi+JE3MKH2L+67+awVi1HsOq8I8Uur2F7r+UYZglHLH6yGElGp7hQ6xBJx8KynTDw2BkOiclDJkuQrrHG9WwrlRSM/AWJH8blA/+IwKscdfrBYzyloHo4spbkbnI31Jev25jDuSESdZ6B9y9fWZdsKSbb+aBYEgMvWU8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707943500; c=relaxed/simple; bh=gJjdcDN9SIZ57EMSsnxvz4s6wGh9ngGev4G0JhDn5Lw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=rHWEGwB/MRJvgYRGPAYnJl5QHMjIk4xfJqbQ2jsWVy7+zwWV5Ac/tbu9En/P5tFn7uf4yIYhp5RZZclz/huTieFSFaHCo5faDGWl+Q2IMF88GPT4xeFKceScL/z099fBpbikN5NxPe1npFyjrhRVPTeg8N6YacW6N9id5gkrhh0= 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=jPkqsZIp; 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=1707943497; 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=jPkqsZIpAhXubwcPIQoGJd4sJN/P/hl1VJhVEMIqk+U2e7zkIQu8pwMZJD+jV+eMSqHyKS 3xjOGiTSolq9lVvkwxGzLYsKIVo882ujB49Wdj6CzlZf0PhIGQxpCP+fKkUXEtBKZz1Gbv oWCEEZyKBNE7EnIExK4hDC2JALzpXvw= 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-460-Ud6bEsrlM92gS6OHI6pxZA-1; Wed, 14 Feb 2024 15:44:54 -0500 X-MC-Unique: Ud6bEsrlM92gS6OHI6pxZA-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 DDD578630C4; Wed, 14 Feb 2024 20:44:52 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.194.174]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4741B1C060B1; Wed, 14 Feb 2024 20:44:49 +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 03/10] mm/memory: further separate anon and pagecache folio handling in zap_present_pte() Date: Wed, 14 Feb 2024 21:44:28 +0100 Message-ID: <20240214204435.167852-4-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: 1790910276769075359 X-GMAIL-MSGID: 1790910276769075359 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))