Received: by 2002:a05:7412:3b8b:b0:fc:a2b0:25d7 with SMTP id nd11csp1225942rdb; Fri, 9 Feb 2024 14:37:49 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWvZZmGmMsizvQTSC+YgTRMOyqM8uHf4lmWsJ397qqbkcvo2iBz3V7sd0OWUgIuKg7dpK7CUXsO03FxkAP3Xms3nSzjmudE2Y6gaeRv2Q== X-Google-Smtp-Source: AGHT+IG51fxtIACuqmBrS6Z5HBq2COjXWI1RHzVWGgJ6Kd0MXQZc9ZpnVVVfhiQLOgk/Txl3Bv8W X-Received: by 2002:a17:902:684f:b0:1d8:deb5:3390 with SMTP id f15-20020a170902684f00b001d8deb53390mr703536pln.42.1707518268780; Fri, 09 Feb 2024 14:37:48 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707518268; cv=pass; d=google.com; s=arc-20160816; b=lm118OlQChD6DhChb7vkTQXkx9XRLZpQZkaRsD7qQIENYTUNPL18turN876TPweJPw M8KGyXTQM0Ew7UKg3E86lJwlqBazUMDctvrGp47/Z4dJUI7OLs2YtmeQl3u1uh8znrLC GSbeNwAKjYZehDG1342nvoGIwElObVAv0I5A3H4va2DJoyj9j56p/BOhkKM8qkw57TAR 1PUa87kvQyT9RNfv0rGbYFyx9kILkkUWfwWMfRe5y5w3HbHYFR+Bm8f3iYdAkO14usos hwixWvsbB9DTnavyun3CgO8h8SB5dR/zelkVhZSB8UvwrTxt8kZXluXpd9eOqzar+pxC gK4Q== 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=2RHsJSQTVDjD1nrjyhKRpvJbAVx2JnALpkrsqlfYjuM=; b=NfnaIhoWoGtbr3+/LAVhOgtwMIPdCQ+LtRTVLPAvKl/C7oLx9g9nYsw6tbhiwVeR2E eCHI9Yl/S2TY1LzTF4VsLqLDJmOvuXuGIjEMV0ybxXnF7T9TKjA+rzSF6KnPz25b+Vfo 5uIuV1lE66+bN7wyYbq2AD69Rdos0adbGZ3/g8V/eazfFPKyQtxFW39X4e65yUpVXPZr FbuvDfi4TDR3LHUW4kcWTHI6+dodFc2G7egki66UTJJezmPCUPEF4lVUYDS9PyopWfLj VQvUPdNzn1Xsn5cuPCj+bKKKAr4RMWTNQDlIq9tnfGaaPP3pcViTE9xyvrD15MqT+QEf oYiQ==; 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-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-60073-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; AJvYcCXdElAKW1xdWYAmPokfRIebrHtcYHmiUkxubuASJLh3GLQZvWQzhvnmFTf/8a2AeMjbn0O9LIwsuCf2kNZInwUF4SFoYi6yjGcEzkYmkg== Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id l14-20020a170903244e00b001d8e1f09afcsi2429203pls.128.2024.02.09.14.37.48 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Feb 2024 14:37:48 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-60073-linux.lists.archive=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-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-60073-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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 5AE13B24DD5 for ; Fri, 9 Feb 2024 22:15:49 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id EE7CB3613A; Fri, 9 Feb 2024 22:15:29 +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 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.7 Let's prepare for further changes by factoring out processing of present PTEs. Signed-off-by: David Hildenbrand --- 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; } -- 2.43.0