From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 28520E77188 for ; Mon, 30 Dec 2024 09:10:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9EEA86B00A1; Mon, 30 Dec 2024 04:10:48 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 977666B00A2; Mon, 30 Dec 2024 04:10:48 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7C9B56B00A3; Mon, 30 Dec 2024 04:10:48 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 5A5B56B00A1 for ; Mon, 30 Dec 2024 04:10:48 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 184861C84C9 for ; Mon, 30 Dec 2024 09:10:48 +0000 (UTC) X-FDA: 82951054914.14.1270C0D Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182]) by imf03.hostedemail.com (Postfix) with ESMTP id 1D02920005 for ; Mon, 30 Dec 2024 09:10:24 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=gU551rU5; spf=pass (imf03.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.214.182 as permitted sender) smtp.mailfrom=zhengqi.arch@bytedance.com; dmarc=pass (policy=quarantine) header.from=bytedance.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1735549815; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=76YtbImY0jPwdXnQQVs9oYKl35yZZ81uc4A2YpilpOk=; b=B4Oysvygwzql5Qg7Yjxbq+vHj4pSORmi+pPDhindFQFf14vHLyqrFuDBW8m02PZ505FSml qafPFkNF0qybEvnSa7ClXW8K+aVIH2rir7CSIzGlaMF+e9ytVy5T1J7i53b3rZZYK8MHvi FLgLq51+nq1sz06UuZicagdE7M/fncQ= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1735549815; a=rsa-sha256; cv=none; b=VMPiG9NPgR5jD9YuYzpmQOhUpNOUsLLrpKn2+9o6MTGVMjIs0Rqla3t04JvQYTwjMuPSrr XV1coV/+dNdXkZhBnoUS0/WSrrMfV1t4oSYFAPnHeln1UKHrl0pZSNLMgJtjr+nsAB0zae 9ANyvaD+3Dzf4fyXMT8feEyorZCbbrI= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=gU551rU5; spf=pass (imf03.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.214.182 as permitted sender) smtp.mailfrom=zhengqi.arch@bytedance.com; dmarc=pass (policy=quarantine) header.from=bytedance.com Received: by mail-pl1-f182.google.com with SMTP id d9443c01a7336-21644aca3a0so17748735ad.3 for ; Mon, 30 Dec 2024 01:10:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1735549845; x=1736154645; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=76YtbImY0jPwdXnQQVs9oYKl35yZZ81uc4A2YpilpOk=; b=gU551rU5Ets2/p3S8ICDE2nt3vveC8+/u5rePYTcNGnQWjJRDZmUI7WKP0NtJNV6Tp txfnORGw9Gmy1IoG6vM9f/2v4V2NFFnLy0vbAaRKTs1Nt/ldH2wQvKOdD88zUCRj5Ywc ypclbr7HL2Aw2RLzP6YWI+ATxDC7vBZJxBAF96hGvmYydc+Qj5di0E1mCzj/Hj9Z96Wv h5VNruJJXb72Dx9WrNlyo1HAyheGomSXIEd4VgFhjDhBrr6IQVCTwQjohIcCeut/4e2I KRZY/CExaCXyeiCqp7u2XUrrp305i8H7yOwPfCI6L+eRQeXK3RPRj0ZGtdBaoXgLkjlq iaTw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735549845; x=1736154645; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=76YtbImY0jPwdXnQQVs9oYKl35yZZ81uc4A2YpilpOk=; b=vFD957g0UIG669vMuuum7QAokB3W3B4go4NR56siAB75wMIU/BDbbsz1IyzHwOXW4X Jhrb0l3TyyfRcftpLhy6hiyfusZIWI7wu+a8rARke0WZKAxzJWz0Gg/O1ahM0K22uDJ3 P9xksXAT7WVoennoUygUlKVSI9bSyQ4d7zWQG+hMpIylMlMWTfn/cIoBWQTYuShxOPl0 iTZZe5PQ21Xy3IGF8KarqY9aCqPNKBUSmUMb+xfIuPmLFTd/IMMMzpXIfdyOm/iJaL2I P96DAPRtpFnNPj0YZT0lcK6JKVCwI8sJWe1FBjObZlCU/jofbAk3dGlfM5ELeIkOcQ// QOkA== X-Gm-Message-State: AOJu0YxXbNiF/TJB+9tqqGlynwf7aARkf7QmtD1lduTepFHsPdsL+dDd g7Hv6oqUe3EtqmXZKHksU0GNsK/aT+82fbosBDBzKt29JX2ltJoXyDWSvNAKxJ8= X-Gm-Gg: ASbGncvLS/Tx1P7kGR+w3lAr3HWV8YS3OofkRMSjJvHMrhzCzEyJz3pgBR+NP14Ztmp QTGro1e/TVyKAFLs2LwbrnvRxLtEsnaCPTKGX22QALppgHFYGEka8dqqFBUWvMnYlN1KHsh1e1z pvJcuk6QZuFoIOETvo0O3ScCnWySvxfROBUBEgjAvXpAiTCwCQwxqFad9zWynFxWEu9NQ6DU5dz HVeTSQHCd0AeIpZKKFuyjq9zqqnx4IxYyVLzl9XsIu56B6DRJdIAbGpXz9SFaotCkSsYuuErTem iXFWZETF+hQ+IOw7bXE+uQ== X-Google-Smtp-Source: AGHT+IE9evGbD+yEfyO2G41S4amTNhC2vfWaq9UYV9WQJrC+a4l06PyKmdWx4KtX6MVeEx+cVngAAA== X-Received: by 2002:a05:6a20:c91b:b0:1e1:d22d:cf38 with SMTP id adf61e73a8af0-1e5e05b06bbmr51533885637.21.1735549844993; Mon, 30 Dec 2024 01:10:44 -0800 (PST) Received: from C02DW0BEMD6R.bytedance.net ([203.208.167.150]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-842aba72f7csm17057841a12.4.2024.12.30.01.10.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Dec 2024 01:10:44 -0800 (PST) From: Qi Zheng To: peterz@infradead.org, agordeev@linux.ibm.com, kevin.brodsky@arm.com, palmer@dabbelt.com, tglx@linutronix.de, david@redhat.com, jannh@google.com, hughd@google.com, yuzhao@google.com, willy@infradead.org, muchun.song@linux.dev, vbabka@kernel.org, lorenzo.stoakes@oracle.com, akpm@linux-foundation.org, rientjes@google.com, vishal.moola@gmail.com, arnd@arndb.de, will@kernel.org, aneesh.kumar@kernel.org, npiggin@gmail.com, dave.hansen@linux.intel.com, rppt@kernel.org, ryan.roberts@arm.com Cc: linux-mm@kvack.org, linux-arm-kernel@lists.infradead.org, linuxppc-dev@lists.ozlabs.org, linux-riscv@lists.infradead.org, linux-s390@vger.kernel.org, sparclinux@vger.kernel.org, linux-kernel@vger.kernel.org, x86@kernel.org, linux-arch@vger.kernel.org, linux-csky@vger.kernel.org, linux-hexagon@vger.kernel.org, loongarch@lists.linux.dev, linux-m68k@lists.linux-m68k.org, linux-mips@vger.kernel.org, linux-openrisc@vger.kernel.org, linux-sh@vger.kernel.org, linux-um@lists.infradead.org, Qi Zheng Subject: [PATCH v4 10/15] riscv: pgtable: move pagetable_dtor() to __tlb_remove_table() Date: Mon, 30 Dec 2024 17:07:45 +0800 Message-Id: <0e8f0b3835c15e99145e0006ac1020ae45a2b166.1735549103.git.zhengqi.arch@bytedance.com> X-Mailer: git-send-email 2.24.3 (Apple Git-128) In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 1D02920005 X-Stat-Signature: s9huadoa8koamm6e8cmfpmu45bij9h1t X-Rspam-User: X-HE-Tag: 1735549824-112885 X-HE-Meta: U2FsdGVkX19LOik/Uz05ze1kMCqKFxnpzoJAT1nlGvnBwzY64rb4n4UyE6//K+4JCwvK8bsH3OXYQVoJyll+nWl1Aa3T2Dg5O7xTNWOQKSfbNs4tdZ5fXBERfl84hHUJOBBHUROeah8rNI6K4VYgNqbMTYLO1P/aySA3StImaXmtC9OZKLJJmczL3sVe62Y4zuOlKute3U3GNY9KPVX04QxbSWygfLYlmT1bM40L7U5MQJxkPBu5/fhJ3wArN9/5C06qcgSIA2488R3nrn91ASVrLja1vTOcKOWk1JGksn5mO0qQQVShCQ1Q56pm9Rfdtza4VGCfAJ/uhsLcSFunSH9hWhwe9TB7nqGBtYo4llqqVWbBlKfAyWnucidPfg8LP25x1+076osc2ftEn58qyO8ksQurO6SctE4BHsRWYr3BDkOtMlm2yJ7rftQMFgC0eLmc8QEWo8+iFLhW6OfOExA1jONwccxHVK9+0BKm3qv7V6te3+MzUkKWJD3gH1DvmTidB8V8YfRGmBBImmFqDS9nZtemXkYjN2o0CMzsOTvbV4MS+2MQIr+Jn6DxTGxgfxqw5xHT4pSQoXTpNjj3XUKJ7b8VRyIANswE2kvWr6gZswKjN8BymJ/TrRDaFMDd6EApj0AyfN2oWtIle13LsuZK+Y7FzNKG63oWzRwLf3ngAKSOYPlZC0hG6ig9m8okBCo7V8RexcE2IhocRbNDLeHp8r1J5MOfLZDmZ8/gy9b9YZwwWtD8XM1Ppy0DAW8ENb/eUOHI0Bh1FlsROUpI7idp7XNrSumfFVGrLfdSi7GD/ORJJ8FrsOlfhqLosgGKxwFVlBKbn2BBKk1u5bZjEubqZlQEmRT8emKYnWji2nAcQHfoWMZ0w7HpdI2e3gT/1TEd0NwzywlWSTfXFfPhNOCioyvpGwVYd90SP56QeLQjzlQe4XqyzElPnQNt95eNRtpuoV59BPbihkFxnmL it4Q8OKd yW2w3wZ7N06xdTKGEbpP99ADOBykh+3CnW9gyWG1T9Pt/NDw5ZY3rwkpXy6GH7OuZzCH8krZtbY01r7XCa+XtW2/Ewg+74Rt8llJH6p2HxZOf6/NhmJyLBMh/6mzNiC4RhEpZkqCMEjpaXwSXItzJPKJ69TDZAIoxwB6jogiQX0Dq1PT2FJkMqt2X2Gs7kqAYMfYg4T4mfhydhZPkTDhSD352ndYOBOLljUSjYVUzSQ3Fv4l/utRisXtsy8J/Ob1AFLbIqGIOZArOhC+stz2iMAbhV6hGYnjqK+EiFxe5O1EQcN82tcMmLTd+C0n9Hz0bEwzkaMsCRIEi+rw1Q/WeQs/ODoPG/IkEMUVIv3+4kPCAisf0rntbWx1cJG6Qv4yQvDhmd7Lzbr5lGx3AMe5xPF5RNPu8nI1NWyJeS1o5ccwdqNQChIBN/1e87ZNwLfS646oE4u+RnaFxMS1YuHFB4myQq/QHvKlWhEfpjCy9FgKyg5o= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Move pagetable_dtor() to __tlb_remove_table(), so that ptlock and page table pages can be freed together (regardless of whether RCU is used). This prevents the use-after-free problem where the ptlock is freed immediately but the page table pages is freed later via RCU. Page tables shouldn't have swap cache, so use pagetable_free() instead of free_page_and_swap_cache() to free page table pages. By the way, move the comment above __tlb_remove_table() to riscv_tlb_remove_ptdesc(), it will be more appropriate. Signed-off-by: Qi Zheng Suggested-by: Peter Zijlstra (Intel) Cc: linux-riscv@lists.infradead.org --- arch/riscv/include/asm/pgalloc.h | 38 ++++++++++++++------------------ arch/riscv/include/asm/tlb.h | 14 ++++-------- 2 files changed, 21 insertions(+), 31 deletions(-) diff --git a/arch/riscv/include/asm/pgalloc.h b/arch/riscv/include/asm/pgalloc.h index b6793c5c99296..c8907b8317115 100644 --- a/arch/riscv/include/asm/pgalloc.h +++ b/arch/riscv/include/asm/pgalloc.h @@ -15,12 +15,22 @@ #define __HAVE_ARCH_PUD_FREE #include +/* + * While riscv platforms with riscv_ipi_for_rfence as true require an IPI to + * perform TLB shootdown, some platforms with riscv_ipi_for_rfence as false use + * SBI to perform TLB shootdown. To keep software pagetable walkers safe in this + * case we switch to RCU based table free (MMU_GATHER_RCU_TABLE_FREE). See the + * comment below 'ifdef CONFIG_MMU_GATHER_RCU_TABLE_FREE' in include/asm-generic/tlb.h + * for more details. + */ static inline void riscv_tlb_remove_ptdesc(struct mmu_gather *tlb, void *pt) { - if (riscv_use_sbi_for_rfence()) + if (riscv_use_sbi_for_rfence()) { tlb_remove_ptdesc(tlb, pt); - else + } else { + pagetable_dtor(pt); tlb_remove_page_ptdesc(tlb, pt); + } } static inline void pmd_populate_kernel(struct mm_struct *mm, @@ -97,23 +107,15 @@ static inline void pud_free(struct mm_struct *mm, pud_t *pud) static inline void __pud_free_tlb(struct mmu_gather *tlb, pud_t *pud, unsigned long addr) { - if (pgtable_l4_enabled) { - struct ptdesc *ptdesc = virt_to_ptdesc(pud); - - pagetable_dtor(ptdesc); - riscv_tlb_remove_ptdesc(tlb, ptdesc); - } + if (pgtable_l4_enabled) + riscv_tlb_remove_ptdesc(tlb, virt_to_ptdesc(pud)); } static inline void __p4d_free_tlb(struct mmu_gather *tlb, p4d_t *p4d, unsigned long addr) { - if (pgtable_l5_enabled) { - struct ptdesc *ptdesc = virt_to_ptdesc(p4d); - - pagetable_dtor(ptdesc); + if (pgtable_l5_enabled) riscv_tlb_remove_ptdesc(tlb, virt_to_ptdesc(p4d)); - } } #endif /* __PAGETABLE_PMD_FOLDED */ @@ -142,10 +144,7 @@ static inline pgd_t *pgd_alloc(struct mm_struct *mm) static inline void __pmd_free_tlb(struct mmu_gather *tlb, pmd_t *pmd, unsigned long addr) { - struct ptdesc *ptdesc = virt_to_ptdesc(pmd); - - pagetable_dtor(ptdesc); - riscv_tlb_remove_ptdesc(tlb, ptdesc); + riscv_tlb_remove_ptdesc(tlb, virt_to_ptdesc(pmd)); } #endif /* __PAGETABLE_PMD_FOLDED */ @@ -153,10 +152,7 @@ static inline void __pmd_free_tlb(struct mmu_gather *tlb, pmd_t *pmd, static inline void __pte_free_tlb(struct mmu_gather *tlb, pgtable_t pte, unsigned long addr) { - struct ptdesc *ptdesc = page_ptdesc(pte); - - pagetable_dtor(ptdesc); - riscv_tlb_remove_ptdesc(tlb, ptdesc); + riscv_tlb_remove_ptdesc(tlb, page_ptdesc(pte)); } #endif /* CONFIG_MMU */ diff --git a/arch/riscv/include/asm/tlb.h b/arch/riscv/include/asm/tlb.h index 1f6c38420d8e0..ded8724b3c4f7 100644 --- a/arch/riscv/include/asm/tlb.h +++ b/arch/riscv/include/asm/tlb.h @@ -11,19 +11,13 @@ struct mmu_gather; static void tlb_flush(struct mmu_gather *tlb); #ifdef CONFIG_MMU -#include -/* - * While riscv platforms with riscv_ipi_for_rfence as true require an IPI to - * perform TLB shootdown, some platforms with riscv_ipi_for_rfence as false use - * SBI to perform TLB shootdown. To keep software pagetable walkers safe in this - * case we switch to RCU based table free (MMU_GATHER_RCU_TABLE_FREE). See the - * comment below 'ifdef CONFIG_MMU_GATHER_RCU_TABLE_FREE' in include/asm-generic/tlb.h - * for more details. - */ static inline void __tlb_remove_table(void *table) { - free_page_and_swap_cache(table); + struct ptdesc *ptdesc = (struct ptdesc *)table; + + pagetable_dtor(ptdesc); + pagetable_free(ptdesc); } #endif /* CONFIG_MMU */ -- 2.20.1