From patchwork Wed Feb 14 20:44:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 201117 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:bc8a:b0:106:860b:bbdd with SMTP id dn10csp1488797dyb; Wed, 14 Feb 2024 12:46:15 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXddE63J7DUncbJEXg5vSpgPDOzZ/AGqRRFsToLkkxdH2W57onfQ5ku9qq+luSFXNSawASN1g9p79jxlZf7cxsku9iwDQ== X-Google-Smtp-Source: AGHT+IFashhCtlh0Ftii3lnKhYa68tUzr9rZeyndIMHyalIDQorH/i7rNfFCpKj6YmGUGTmxI2Z3 X-Received: by 2002:a1f:dac3:0:b0:4c0:5e95:b9f3 with SMTP id r186-20020a1fdac3000000b004c05e95b9f3mr4111133vkg.5.1707943575753; Wed, 14 Feb 2024 12:46:15 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707943575; cv=pass; d=google.com; s=arc-20160816; b=RG9WssbmEh7RtiOejbv2PDmL49SKTC7kekjyIQcg2XnQpbsyTKSF9TdkX+tCdnd/Td JIFHqI+Vikmg2Nbjd3hy77JgSSREWUHOgb+BHcd1kLrxrYl92+wJrF9TOZGbqiL7wK+i 65dWrcurAW2JOeUzE6xmJFoGs3KtxBoh344vAQZJREKsGcEtknRupmR34BuQiUZzNw3q dMaMjASxQ41mQVbS3b8pZNYtSL1rAcEwmjmjRaf4/X7qxy1yDHtX4iSF8hf0a3F9r5Ky hK361+INY/JEZ1gvdiWwDFJnOgPHPMIx2rQvDbEnfjjU5m5FRT0xGx9wEyo4LlqjLZhS vTSA== 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=dEQuQbE8PusGeIN6wyahI5tWOdzdv7mIipcs9sgG+zs=; fh=bTCZQ3FviAQEdD7gIIhRXJ3HZysoPdxBLzmxWEfwHy8=; b=xoQk/OrN6JDrpb7lxFmS2cTqEvZz5xzVqdmh2qdZVKezvmhay+O6nP8SWAOpPUJLyR qO0fqajurWgAWvNVQKTfQ5KWYTGOonGDGa5o7/IYNaoOqy0Pe56n/WXroa/j1SaX9ac8 zMbGR5WDaE2PqFiJo2WHoP1kSiynk91gIgfy91kEHYSmz2nfXUlBn5N0zH9nWIAMlGjn xz4d1dktbJuQGMsbxzw2mngMiVPLwvrwRIhykozz2VOz5U98AxlWRligLdQVvhWsvp4r Q4gABnR0GSLVbz8oSx5R2n5spFCb/axK5pWPXkLQOVrdSXk9mJuDAoU0WhtAqPRFtfU7 0Bbg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=a2AwhUFz; 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-65947-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-65947-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com X-Forwarded-Encrypted: i=2; AJvYcCWWne41Fb43O2YO5FLK4EM1sRC1GaGaX/O5m+abZkO6U5kFSk9oQtNq7o5kDsZhmUHhMg8xnSFYpKJKuehJ3k+Ml8K4Ig== Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id jx14-20020a0562142b0e00b0068ed5efaf47si5392182qvb.244.2024.02.14.12.46.15 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Feb 2024 12:46:15 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-65947-ouuuleilei=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=a2AwhUFz; 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-65947-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-65947-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 8BC1E1C2247D for ; Wed, 14 Feb 2024 20:45:20 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3D70113F004; Wed, 14 Feb 2024 20:44:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="a2AwhUFz" 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 BDB4013EFF4 for ; Wed, 14 Feb 2024 20:44:52 +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=1707943494; cv=none; b=R+gzFkWeYJ1W9XnpGm06DwhQ12Y/YrvA/RYw06iSn0yJ0VlAgU8Hk6Ue+iJarSlGKV1X9sqEUxvrG2m14P9ujppqYzQkDGPlGfU25YpVdCBoiG7Gy0jEeJvwn5U8jjzvH/D6EBSMRSYMjE17SYE5tzS6xdfTltXbOBb8/GrSpd8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707943494; c=relaxed/simple; bh=7HbJDHbNEg6CVfsuVyc/qkeafaxTSnyk1xYc8prFL0c=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=irXLmORR36EviLBzR9xW267HXSviNf7rWfaxmwDR9mGkRs4MgmC6tm67SfNzrYhSZvKjpVYwaQFLBSERy4CjNl/SJwHPowsIN8SpJEDBkuC9R+xsaIcmiQ5AjIb1cW71o8HLP1jN/jYA+4jMYo9YSiJECn9k07QV6QM1z1Pc/ZE= 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=a2AwhUFz; 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=1707943491; 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=dEQuQbE8PusGeIN6wyahI5tWOdzdv7mIipcs9sgG+zs=; b=a2AwhUFzhRV/eG1TZMNiQUiTgShreLYj0RtSOikqu4mdgcwEhPC8eS2/FiZB2ybaPFungN D00cCP4T+U7MdPHd/HIJzslfQg6SRHiynHlCKfjmPYGyKTUMYBAhFJlZU2RX3f2GzTYrv7 eqvXCR9r0IbapTgj0Rrb58j8nb3c/eM= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-373--0kZEuV3Pfig40WvEuXQ9Q-1; Wed, 14 Feb 2024 15:44:46 -0500 X-MC-Unique: -0kZEuV3Pfig40WvEuXQ9Q-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 05DB528B6A1E; Wed, 14 Feb 2024 20:44:45 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.194.174]) by smtp.corp.redhat.com (Postfix) with ESMTP id 5C51A1C066A9; Wed, 14 Feb 2024 20:44:41 +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 01/10] mm/memory: factor out zapping of present pte into zap_present_pte() Date: Wed, 14 Feb 2024 21:44:26 +0100 Message-ID: <20240214204435.167852-2-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: 1790908642990977899 X-GMAIL-MSGID: 1790908642990977899 Let's prepare for further changes by factoring out processing of present PTEs. Reviewed-by: Ryan Roberts 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; }