From patchwork Fri Feb 9 22:15:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 199148 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:50ea:b0:106:860b:bbdd with SMTP id r10csp1170092dyd; Fri, 9 Feb 2024 14:38:05 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVwO3GAVlh0gTyPRz2ae7NGUClNMQJWtBtgGoBbiJxYKacppVSWmFLoSDHQVevlON+31gn4XRLuA0ZN4rXYQjC1l+nRGg== X-Google-Smtp-Source: AGHT+IGUYZXP5Kfab6u+vg9U/HWJZThSOc18SwV44c3TGSxEa55CZ4AxMFCmyoiIPt6RhO94xHh1 X-Received: by 2002:a05:6830:1d75:b0:6e0:d6e9:debe with SMTP id l21-20020a0568301d7500b006e0d6e9debemr549233oti.32.1707518285018; Fri, 09 Feb 2024 14:38:05 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707518284; cv=pass; d=google.com; s=arc-20160816; b=s2igbGHCgguN+N6vc+QM8Prny5dhGwZ0kJs6VgO2hXmAjxtP8dIv3Pv7SYAbMPIVH2 1K8+hqN5cVPToLMJWwPCal0igkNrMIhGS55ErzH4TmL1qHp1zNeMUREuxsXrSK4dzi7u /SkShdzy3DoUnEO5NHJlsA9TtQ0yCDh5eiCxWP2ar6Y+qMBBk6mz2cjfPJSm1pqIIDRO N3OSx2RWGYOvvpVzbYsH+B0MRGZCsFGUF9ndbyAWD1dOGLHhMY8kRvrE7XdMKUwWe1Wz Ux3dWYpm1QaOUX3zvm+xA+xK5H2nvgomxJEFEgmfA2O0zyowKPuNbJtjiySOFonXvjOV sWbQ== 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=GUXSwV0Ba8onVJaRwxmEJ/5t7CtvdY0dXq3jtt3HQ88=; fh=qPzgHq9Q4OEAfoYSA2J4nJ2SE8tGGHj+rknwfMc4rRQ=; b=ITF7CPuKzPwynH/tF6BXeUQyNTLbrUMSuwLcF4XrCHq//PXPT1/Dm4rlNVDLE8zBrb dQd1l91hxQ6NCvYiMF5LSsj1PV8LaMmDija/nqAnCdxIs8Kj4YpcNHuMhG9QEjcUttXI tH0BpKeEGHLvRV8m8pzVhHyAgVqIwoQdgS6yniPujhUJaVP1jcsrm22XwLR3ZpdcfbR1 RYoZ4ykAJZTm1KspjGoWLaUVyGydlkS2LtBpu5slF4p0mdB6UrCUADkXGhs44xmHABaR bg8uJicV9WFfUD0LXQfNPVylG++5e7QUVzSfzi5tgHmjBHTROEFunUJ32HFBQlOeMzv3 1w2Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=ERcXIKFB; 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-60073-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-60073-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com X-Forwarded-Encrypted: i=2; AJvYcCXx61O1hziteq+gXvRpmZMps+WS4bufkRhQmjFdqylfYxDx1x3qVSsDLGr3FS2UZyWrj0L7M3Zs7N6qPXW/Q/cZ7ISjLg== Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id k125-20020a632483000000b005dc4f115f2csi2431002pgk.496.2024.02.09.14.38.04 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Feb 2024 14:38:04 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-60073-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=ERcXIKFB; 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-60073-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-60073-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 E5C6CB2561A for ; Fri, 9 Feb 2024 22:15:59 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 0EB65374F1; Fri, 9 Feb 2024 22:15:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="ERcXIKFB" 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 701273613E for ; Fri, 9 Feb 2024 22:15:27 +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=1707516929; cv=none; b=XCwIHGBJiE4M/2+fuC0JGXdiqwdCcGuDbRFCbmQ0tkfYESUCihcIHhDxsLaIAwJs4vBIWIjMlM+B/ucZJLk3/rUbU/x8yg+kKbh0IKr0vYpf6Zyw6oE6c6JNzA+eby9tazpjH8jk9nP0nCWilRWHrFOOoO2SJKn/jLpt9cVz8HY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707516929; c=relaxed/simple; bh=TnUCOm2JT+0JTJyQU5SkjDNAWwULzDd20SY3FqhSdfo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=mUQpbGCtKbfcbM5GHG+YYgW2R6wnIpNyMe6gBBRqtZAKTse+YWptqonKqYJX4z98bD4pKqVEGCvd4iKTk6k0mkSNNuhdMXwpZuqEwuVRsuhNser54BVZ86MumGzJmDa9WTxrypGdzu+gQsIxs/bRhhImNqBCArWpBOErG9Pvx2o= 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=ERcXIKFB; 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=1707516926; 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=GUXSwV0Ba8onVJaRwxmEJ/5t7CtvdY0dXq3jtt3HQ88=; b=ERcXIKFB2dbeOOCA8ZvA+Kv2LwAlx6Qh74YqwX+mAKOnUARoHIPz9TSrRinP0odavoOAIt +TB5wKYyf81UIBpo/i/AO+HSAycTqfuS+FI9uHuRhDnsZkzHOASCEBnqhbVG4eYFo45IP7 At/Wz4iMGXVomtAxX14cB4hId/MbxW0= 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-42-CTYzAu7yOQSNMpmHOZXTyA-1; Fri, 09 Feb 2024 17:15:21 -0500 X-MC-Unique: CTYzAu7yOQSNMpmHOZXTyA-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 85BC185A589; Fri, 9 Feb 2024 22:15:20 +0000 (UTC) Received: from t14s.redhat.com (unknown [10.39.194.59]) by smtp.corp.redhat.com (Postfix) with ESMTP id 74DCF1C14B04; Fri, 9 Feb 2024 22:15:16 +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 01/10] mm/memory: factor out zapping of present pte into zap_present_pte() Date: Fri, 9 Feb 2024 23:15:00 +0100 Message-ID: <20240209221509.585251-2-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 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.7 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790462693072376814 X-GMAIL-MSGID: 1790462693072376814 Let's prepare for further changes by factoring out processing of present PTEs. Signed-off-by: David Hildenbrand Reviewed-by: Ryan Roberts --- mm/memory.c | 94 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 53 insertions(+), 41 deletions(-) diff --git a/mm/memory.c b/mm/memory.c index 7c3ca41a7610..5b0dc33133a6 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -1532,13 +1532,61 @@ zap_install_uffd_wp_if_needed(struct vm_area_struct *vma, pte_install_uffd_wp_if_needed(vma, addr, pte, pteval); } +static inline void zap_present_pte(struct mmu_gather *tlb, + struct vm_area_struct *vma, pte_t *pte, pte_t ptent, + unsigned long addr, struct zap_details *details, + int *rss, bool *force_flush, bool *force_break) +{ + struct mm_struct *mm = tlb->mm; + struct folio *folio = NULL; + bool delay_rmap = false; + struct page *page; + + page = vm_normal_page(vma, addr, ptent); + if (page) + 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)) { + folio_mark_dirty(folio); + if (tlb_delay_rmap(tlb)) { + delay_rmap = true; + *force_flush = true; + } + } + if (pte_young(ptent) && likely(vma_has_recency(vma))) + folio_mark_accessed(folio); + } + rss[mm_counter(folio)]--; + if (!delay_rmap) { + folio_remove_rmap_pte(folio, page, vma); + if (unlikely(page_mapcount(page) < 0)) + print_bad_pte(vma, addr, ptent, page); + } + if (unlikely(__tlb_remove_page(tlb, page, delay_rmap))) { + *force_flush = true; + *force_break = true; + } +} + static unsigned long zap_pte_range(struct mmu_gather *tlb, struct vm_area_struct *vma, pmd_t *pmd, unsigned long addr, unsigned long end, struct zap_details *details) { + bool force_flush = false, force_break = false; struct mm_struct *mm = tlb->mm; - int force_flush = 0; int rss[NR_MM_COUNTERS]; spinlock_t *ptl; pte_t *start_pte; @@ -1555,7 +1603,7 @@ static unsigned long zap_pte_range(struct mmu_gather *tlb, arch_enter_lazy_mmu_mode(); do { pte_t ptent = ptep_get(pte); - struct folio *folio = NULL; + struct folio *folio; struct page *page; if (pte_none(ptent)) @@ -1565,45 +1613,9 @@ static unsigned long zap_pte_range(struct mmu_gather *tlb, break; if (pte_present(ptent)) { - unsigned int delay_rmap; - - page = vm_normal_page(vma, addr, ptent); - if (page) - folio = page_folio(page); - - if (unlikely(!should_zap_folio(details, folio))) - continue; - 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); - continue; - } - - delay_rmap = 0; - if (!folio_test_anon(folio)) { - if (pte_dirty(ptent)) { - folio_mark_dirty(folio); - if (tlb_delay_rmap(tlb)) { - delay_rmap = 1; - force_flush = 1; - } - } - if (pte_young(ptent) && likely(vma_has_recency(vma))) - folio_mark_accessed(folio); - } - rss[mm_counter(folio)]--; - if (!delay_rmap) { - folio_remove_rmap_pte(folio, page, vma); - if (unlikely(page_mapcount(page) < 0)) - print_bad_pte(vma, addr, ptent, page); - } - if (unlikely(__tlb_remove_page(tlb, page, delay_rmap))) { - force_flush = 1; + zap_present_pte(tlb, vma, pte, ptent, addr, details, + rss, &force_flush, &force_break); + if (unlikely(force_break)) { addr += PAGE_SIZE; break; }