From patchwork Sat Dec 14 09:02:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qi Zheng X-Patchwork-Id: 13908433 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 C1919E77180 for ; Sat, 14 Dec 2024 09:04:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 56B9F6B009D; Sat, 14 Dec 2024 04:04:54 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 542CE6B009E; Sat, 14 Dec 2024 04:04:54 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 40A566B009F; Sat, 14 Dec 2024 04:04:54 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 1D4DE6B009D for ; Sat, 14 Dec 2024 04:04:54 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 9CF84A1CBC for ; Sat, 14 Dec 2024 09:04:53 +0000 (UTC) X-FDA: 82892979414.14.0A66CB6 Received: from mail-pj1-f51.google.com (mail-pj1-f51.google.com [209.85.216.51]) by imf16.hostedemail.com (Postfix) with ESMTP id 988AC180007 for ; Sat, 14 Dec 2024 09:04:24 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=Dl3HHgra; dmarc=pass (policy=quarantine) header.from=bytedance.com; spf=pass (imf16.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.216.51 as permitted sender) smtp.mailfrom=zhengqi.arch@bytedance.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1734167066; a=rsa-sha256; cv=none; b=A5S/olfzUw3znC1hpfBP7VQ5NPYuRPBYcdvgSZ7mYk0KSot3Nvtsx8sDRPvkO8Vfl7zXgT jL3s/jsW5KonCCb7NGabsmmUMfsa1FOJGuUIxkdgTUNLZfy+ceTrRTDsLon8DLarUmDRHw zDqyqMnLPuHnxH1fD/hICMDuq62O0ps= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=Dl3HHgra; dmarc=pass (policy=quarantine) header.from=bytedance.com; spf=pass (imf16.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.216.51 as permitted sender) smtp.mailfrom=zhengqi.arch@bytedance.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1734167066; 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=kxcjg+YJiLOjdKwLGOoVz8JWl+VadyRpr3tadtBHjWA=; b=bJgDFVXGmHPlcSLt/xqvyt+mWGW/Ah0Q9eY2TiIdGczQXC7fEQLcbek80RBre47KEUpKJg Mv0HGci7DIJjNwX2iF8gsit8tMBTqWmvhELXCN6edre+cHgjaUrHI8Xm/SnITEamluIu7l iknQDQvgxWS29xWrEkEkhpfc3jrGTJA= Received: by mail-pj1-f51.google.com with SMTP id 98e67ed59e1d1-2ee786b3277so1658834a91.1 for ; Sat, 14 Dec 2024 01:04:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1734167090; x=1734771890; 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=kxcjg+YJiLOjdKwLGOoVz8JWl+VadyRpr3tadtBHjWA=; b=Dl3HHgra7cfcfIEnY3ojCZapxW8DdRaKYDxCSuEpH5NAfXLRZHwIHQM7kJvWthpf0M If7ASUlgV9ydAGwgqD8X3ZFuwae05NVkEgHLPJNw9DVS5hejMrXwKa/8eyewa0qjbz1I k0R/eAfdvGNsd/IaEEI55MNKTLoXYDUz0rl4ygCmtVg2Yd86E/QsetPz2rLUEAVY5hzJ QqHBdkasHjIbbvP0GtJF4E3aE2JCrcDqjRsSKvTMf1qzUR/IwoVpKjxGoU/dtwT5R9LW al3Zx3yuEY5yrrJsoxBYaYiubWam3YAYUa49z6SdMDq4lXxlkFioluQlwTUhNLuEk+PF nr6Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734167090; x=1734771890; 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=kxcjg+YJiLOjdKwLGOoVz8JWl+VadyRpr3tadtBHjWA=; b=YnbrbM6hIladWIO1RX5kZH6CfX03efIbLGAv3izCFXkdPnw/JFnakSoBhVFBRI6fb1 4IJ8oCQoVEksS6HfcYSDl8/CzmHu/X3Uk0HdSMFy/x/e+zsK4R6J2x8v9GWXzYOJv+Fx pNk8zpedNDHWIInN2G27lg/43vJR6TF7NPyvdDzY53NN0S8+mIIs9efw1WiD2P4wNWgx BO3Mwmt+hmg/P/ikfCahbBX1xPbq2B9DySB0PQ2eIbBxQpf4zJqrPeqJka4LbvWwgeGA Ef0TLIjTypk8t9eqIM4U1sauBEuITHwa5N710Sb77Zge/8LXN3loCPMlQOW6LhUJSi+i esPw== X-Gm-Message-State: AOJu0Yy5QuoaJ0gPdJBkW0Wj43K0UgALk4s3lxnu7TuZcCgEzv1tOUtt iTdNRuwTGZhILKPiNZ4zpkkAtsGgCa9bkDB2NeYPXvKuQH0NU+qahC6saWZ18/A= X-Gm-Gg: ASbGncv9pyuF7Pr4/sp/54OPlwvfZiJ02/4jaYQw1P5k1u9OXEPHDalBJe67RBDPHjn WnjjFEF3tv3oktdEljLsK5e4Q6R8EDz2gKz07KmZMjXMijYADhXXkTLNOarB/wVk4zr676aHsdg pihaGAjUdh8ynoQljeyIwmfReNE45dE48DihvrFGMmdxMm4sIZFVH6WYgcZ3JbfgRI9gu9Dx1vE KvIKOdvZcIiyt1EoTWhOumSkcE2EslmslXTPoWtOHynUAbbopwJhXSbHaanpVwFr6TUuBAiUgJ3 y11jSzOUEdxxiR/Q1ky2q4s4vO/n1A== X-Google-Smtp-Source: AGHT+IGmWNhert5ebnmEBEANehnSTDBWCLVNkF+841k1TH0tjB/D3zS21KwZ8s5YJg+vyRMQfoaR6w== X-Received: by 2002:a17:90b:3b52:b0:2ef:2d9f:8e55 with SMTP id 98e67ed59e1d1-2f28fd66b3dmr9591223a91.17.1734167090585; Sat, 14 Dec 2024 01:04:50 -0800 (PST) Received: from C02DW0BEMD6R.bytedance.net ([2409:8a28:f4f:a9a4:5d46:4ef0:daa9:1db8]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-801d5a90ba0sm905731a12.16.2024.12.14.01.04.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 14 Dec 2024 01:04:50 -0800 (PST) From: Qi Zheng To: peterz@infradead.org, 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 Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Qi Zheng , linux-riscv@lists.infradead.org Subject: [PATCH 08/12] riscv: pgtable: move pagetable_dtor() to __tlb_remove_table() Date: Sat, 14 Dec 2024 17:02:54 +0800 Message-Id: <1e73cf87193d838c64e402896b4252875c212986.1734164094.git.zhengqi.arch@bytedance.com> X-Mailer: git-send-email 2.24.3 (Apple Git-128) In-Reply-To: References: MIME-Version: 1.0 X-Stat-Signature: sh53s5ed7jqqs4zee846p1joboga1k68 X-Rspam-User: X-Rspamd-Queue-Id: 988AC180007 X-Rspamd-Server: rspam08 X-HE-Tag: 1734167064-216656 X-HE-Meta: U2FsdGVkX19yA6ZVo+/29rJ0UMkyEZQhK07dFkAEG4BsCszKBqcgjWt18dn8DBMQiZs6OV+uRdF9PhzxrbORbmIiDgxcvi8O8o4KVuT3LahbqUAR2cfXwKE0aFQBCqfErFGuJIH9Ualoc5dd0JpmndxuXPsd9M11aWK/qldeRPyGU2eank4eqUYkuZ2+XxC1x6neF7F8eQUXgpseCxz7iukWebGZOl1fVTC04PP45yK7UX4YerwkeNwB3BvoHErc59Oo0nHdH2rodHG1eG+8KqadgeqUsBASmYLuQ4cI1bO/bWw/IPPWLXYe0u+7TMGxVyljJiueNabP9XoijH/4cZDGwjBxdUUQH85Scd8K29jL1X+x+COJb5MxOk2WYkI+QEZpysiLjwQsVAVxC3VjVwf/6Hp3eguzovkHaLe6fRTzPwWCVyVEbtgnGeSE/tWmei9ZdxNHeNWhjONhVFUmA4/oRU7OvDKGKx9FW3apruQS7j6FN94xZLt4z8F3EFTaEeuMAAoBF3+HwBYfVzKjdm8yAgNihj9RbZx6d5gNCEub34eK5BS5PSnfzoa7ZtE1rVtvqNmCKqXwKc96C52ost9qQUvp9eLCby9sXnUuVa0LnY8oiqx+2OOFNAIKzRvGxFu82Hdmny+n68pVRlLgijibxqlxdytirnEvqWUImRRPbc3SZ8cdsdKWtvENy1cipFnTe+7tD0UEo2rTgfmpE24FMXRFu192vrHecLVK/ct4eijxyauUYQH3HCaMIbG/lybeQztCyfXkydk6+azLa55akY4zJO14L3BFHNXFZScMYnWByTrEoHEEQbL8TOcrWTCL0ItTTx9TsjTeSv1baQIMGIbaNIdLa97yaUxjc6ZoiXfkBtDtZc42OFpeoOPd8Osom2hlRIcZy3olf4oj5+G7YBo9rOwMLK3PDIPr0Wn4jBbE9dnTvynl/fsVQm7J72h8vWacd/wHuCORz8M v0T1imad pemFIj2Tb7RtO+8KRBhFpOvgdVep2rqK+oXkIkj6rzyeLjaney8SCWCgK5ebAY5V0od4B4r87wuvTe2R570S3N667Wnv57KpkYjMlKG0JhxE/ws1acaU9T9I2sm0eQcyCl7hJ83BEt28ken0aSVelkyl5RubvT4pwHx98svgheYyGqkqsE93bZ5UMSG2z+MZd40X73YhNkIJvFKzfZD9t4hclb+6FJEptls2vGRDj2H7nC3/OjjK4Nga16tsBLo5AHGn8+zKI9KEu+VQbVssh+TegLqGpMnrFaDiCQk/1/74O16/cctp8LbhtSiKJ5YqVMx+wkv6SAU/jWklYpiZuLpjmAZmzbZHwbm1clADtsm/JSvPZWXwSy9aUGMtc/ck7CflbLk369s+azhcuGleEYh+cmX++TGSVWRqUP99v5sOCkDU= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000834, 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 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 09c2eff571a49..fc50d14010246 100644 --- a/arch/riscv/include/asm/pgalloc.h +++ b/arch/riscv/include/asm/pgalloc.h @@ -18,12 +18,22 @@ #define __HAVE_ARCH_P4D_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, @@ -109,12 +119,8 @@ 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)); } #define p4d_alloc_one p4d_alloc_one @@ -136,12 +142,8 @@ static inline void p4d_free(struct mm_struct *mm, p4d_t *p4d) 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 */ @@ -170,10 +172,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 */ @@ -181,10 +180,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 */