From patchwork Tue Mar 12 03:59:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yosry Ahmed X-Patchwork-Id: 13589506 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 157CAC54E58 for ; Tue, 12 Mar 2024 03:59:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 956DD6B014D; Mon, 11 Mar 2024 23:59:58 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 92C4C6B0180; Mon, 11 Mar 2024 23:59:58 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7A5C66B0181; Mon, 11 Mar 2024 23:59:58 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 691F46B014D for ; Mon, 11 Mar 2024 23:59:58 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 4389C8049C for ; Tue, 12 Mar 2024 03:59:58 +0000 (UTC) X-FDA: 81887033676.11.926D31A Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) by imf21.hostedemail.com (Postfix) with ESMTP id 743221C0003 for ; Tue, 12 Mar 2024 03:59:56 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=1yoUKlvR; spf=pass (imf21.hostedemail.com: domain of 3O9PvZQoKCNYQGKJQ29E658GG8D6.4GEDAFMP-EECN24C.GJ8@flex--yosryahmed.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3O9PvZQoKCNYQGKJQ29E658GG8D6.4GEDAFMP-EECN24C.GJ8@flex--yosryahmed.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1710215996; 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-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=GoKed6yKAS+GwnXsUmVoWRiO+MONN8g5vxEH6EyDAwM=; b=qI+EC9JWlpAV35j/mrcLuGCGqqGW/xmOt2jzB6qWsBAbIIMhS+BQwHgDHpLQiAgSWn8pDR hqzcfEVcNCMNC+bGEvaEgZ0AerIKoG47TYCj4eBAFG7kVsJ11u2EbWQ55Knm516l6wyKsI X0UgwFP9gygypRcof7lOoVd4tXaKYDs= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1710215996; a=rsa-sha256; cv=none; b=CD3uQuGbp3XnpElr7cnsTlVpI9ULQAh4iPiLazgslQSGY9FTA9Aj47IVAjGnRRmds+oXs6 GoCdUPtcShIIoVoJvDiXForqB/Bq7xISSv+GmtOh3UqxvNP5smW9RS66C+WfK8uBSONgi/ 5PcHn/kxnguQU/qslD3qfQyp6Od9ZQ4= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=1yoUKlvR; spf=pass (imf21.hostedemail.com: domain of 3O9PvZQoKCNYQGKJQ29E658GG8D6.4GEDAFMP-EECN24C.GJ8@flex--yosryahmed.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3O9PvZQoKCNYQGKJQ29E658GG8D6.4GEDAFMP-EECN24C.GJ8@flex--yosryahmed.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-dc6b2682870so8396024276.0 for ; Mon, 11 Mar 2024 20:59:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1710215995; x=1710820795; darn=kvack.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=GoKed6yKAS+GwnXsUmVoWRiO+MONN8g5vxEH6EyDAwM=; b=1yoUKlvRxnGBD7RuvXFLIZhbl9whEfzePMBx6ZPwvjJdjr8yNNmmEpZzXvqzqG3gy8 eaS9+m2iZMNXlzagS04L9EobfIYS3uENvvlTxT/gNYpT3AZZqDUM9sOzcJ7dRSxJIX1Z BLvLkrFO5cyZNF6+XOY/LQ+gk1TadQrBk2/5Lj8ppBgy/jXF/6DGCOEXcE3C8y4kjbZ8 ntY3fYl7crqBQdlMqt1hafs6ITf+TrmZ8O+zMdMo94cHV/2BXQ2TsdPuzZRg/pk1STb9 09G0ekdOtwoGA45J8IMiSxOgAu85IpJwS6E0VL1h6K1rlDKRwybURvmujMW/UTs3dGM5 ZtYA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710215995; x=1710820795; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=GoKed6yKAS+GwnXsUmVoWRiO+MONN8g5vxEH6EyDAwM=; b=BUF9FKKu9klegz0x8IvMXtWC7ffwW36oX16vXap4unOQ8eQuqZy9UXolG5B0DU5Mks x46cOC3QpD08VGIZdViX69fd2q1IuC7T9D7IvcT002ZXqD11Ayo2TG3omKqvdaN3uNPj Omc9cSQM/QAGzNmwmCD7NIk2QhC+WWr4qPCBIDuHF/n5RAQ88xexOvEEPA4HymAwydZr y3djWrYD4gsZXtSlMgAGLQtXTDd/aiNlSlYWUIwjqHUx9nipA4CziptWBfRizi5r24qq sRzhd7Ry7ID1aIE9oUZRqimxceH9Pz4VzAOkwilwHgNGymmGUCUbPssuPHIPizhgfTVE X2tw== X-Forwarded-Encrypted: i=1; AJvYcCW9kBIDAlcRBlYFQlzbufFEfE1kP6/nOG0qa5/mZwHfDuD+acPYOIA9VZffjBP37QNfrV36x6mbk4braArOPQtcerE= X-Gm-Message-State: AOJu0Yy1arGNTbkFBDEGtBbjZpFM34fELlFSuWrQTHC+YJvUT4UeCUB5 hF5yuuoMEMWB9SvySEz0QrZvBbtrhMJvvOWJLSRUhp4deehhb4mQp2RpE5g1nLW6dyXfmPxRJhc kYjb7ZDM0Gf/u87Hs6w== X-Google-Smtp-Source: AGHT+IExFRL/azN3q8HgBq6A2xtMpxQXpFaPTK8KJYQ1jXBub6ECtGohzNaeSuMsvvLKLtLSLkGboXwUHiFLXv1f X-Received: from yosry.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:29b4]) (user=yosryahmed job=sendgmr) by 2002:a05:6902:1081:b0:dc7:8e30:e2e3 with SMTP id v1-20020a056902108100b00dc78e30e2e3mr2416970ybu.2.1710215995630; Mon, 11 Mar 2024 20:59:55 -0700 (PDT) Date: Tue, 12 Mar 2024 03:59:50 +0000 In-Reply-To: <20240312035951.3535980-1-yosryahmed@google.com> Mime-Version: 1.0 References: <20240312035951.3535980-1-yosryahmed@google.com> X-Mailer: git-send-email 2.44.0.278.ge034bb2e1d-goog Message-ID: <20240312035951.3535980-2-yosryahmed@google.com> Subject: [PATCH 2/3] x86/mm: Fix LAM inconsistency during context switch From: Yosry Ahmed To: x86@kernel.org Cc: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" , Andy Lutomirski , Peter Zijlstra , "Kirill A. Shutemov" , Rick Edgecombe , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Yosry Ahmed X-Rspamd-Queue-Id: 743221C0003 X-Rspam-User: X-Stat-Signature: w8fka8xjwd8scbnip9qfjia8akfw1yuc X-Rspamd-Server: rspam03 X-HE-Tag: 1710215996-127249 X-HE-Meta: U2FsdGVkX1+jxzj4DZuCqaj6bC7g/FUSnrjm+1nYYkN06GmdNbjDRymQzoRjfBNk7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tllKXcNI7SwXTOOfsszxqJWsUdVTsZ5l7TsDifZtGPjCjfTL6zDseAxNd4cAhJEQBy5lBE2YlHFTSc8Fv5EQZwMGeDywA1ziy18YVarOVl+fcgVI11V/uDYu+EeMy0AnTkt8RIO0yqWPslI20hSfgKZiauLxLzqVeUkWEWTkpG42T8mN1DfZ5HqcdO4VGvGUJsOTJl429vmWze8E+IhwALHg94bP74Ny8fkuB8riAjJdJfpyX/wJzFP5Agcn2p6E3ptjd87w1JxNdZIiiMDwn1y4onQNJEDK4dBffR/HWZNan8RCzdZNaMNJ5KEUQ8avz4BofzIdZlv7RTaIWuaDeQB9Y19aB5uhe8wjbrLOnuS4PHgOI4DFtzohfm9h+qCI2ue4NkgeoCo4/C7d1cruLG5mtPxbJ6OcXeQoRUS1YA1woikFLzWH8kyG1PthYswV99rhMmRRgHhlfp4mAkqdiuiVIUh8gCRR7uEY+8Y0rXG7w8Nk9az37yo= 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: LAM can only be enabled when a process is single-threaded. But _kernel_ threads can temporarily use a single-threaded process's mm. That means that a context-switching kernel thread can race and observe the mm's LAM metadata (mm->context.lam_cr3_mask) change. The context switch code does two logical things with that metadata: populate CR3 and populate 'cpu_tlbstate.lam'. If it hits this race, 'cpu_tlbstate.lam' and CR3 can end up out of sync. This de-synchronization is currently harmless. But it is confusing and might lead to warnings or real bugs. Update set_tlbstate_lam_mode() to take in the LAM mask and untag mask instead of an mm_struct pointer, and while we are at it, rename it to cpu_tlbstate_update_lam(). This should also make it clearer that we are updating cpu_tlbstate. In switch_mm_irqs_off(), read the LAM mask once and use it for both the cpu_tlbstate update and the CR3 update. Signed-off-by: Yosry Ahmed --- arch/x86/include/asm/mmu_context.h | 8 +++++++- arch/x86/include/asm/tlbflush.h | 9 ++++----- arch/x86/kernel/process_64.c | 6 ++++-- arch/x86/mm/tlb.c | 8 +++++--- 4 files changed, 20 insertions(+), 11 deletions(-) diff --git a/arch/x86/include/asm/mmu_context.h b/arch/x86/include/asm/mmu_context.h index 8dac45a2c7fcf..19091ebb86338 100644 --- a/arch/x86/include/asm/mmu_context.h +++ b/arch/x86/include/asm/mmu_context.h @@ -88,7 +88,13 @@ static inline void switch_ldt(struct mm_struct *prev, struct mm_struct *next) #ifdef CONFIG_ADDRESS_MASKING static inline unsigned long mm_lam_cr3_mask(struct mm_struct *mm) { - return mm->context.lam_cr3_mask; + /* + * When switch_mm_irqs_off() is called for a kthread, it may race with + * LAM enablement. switch_mm_irqs_off() uses the LAM mask to do two + * things: populate CR3 and populate 'cpu_tlbstate.lam'. Make sure it + * reads a single value for both. + */ + return READ_ONCE(mm->context.lam_cr3_mask); } static inline void dup_lam(struct mm_struct *oldmm, struct mm_struct *mm) diff --git a/arch/x86/include/asm/tlbflush.h b/arch/x86/include/asm/tlbflush.h index 25726893c6f4d..69e79fff41b80 100644 --- a/arch/x86/include/asm/tlbflush.h +++ b/arch/x86/include/asm/tlbflush.h @@ -399,11 +399,10 @@ static inline u64 tlbstate_lam_cr3_mask(void) return lam << X86_CR3_LAM_U57_BIT; } -static inline void set_tlbstate_lam_mode(struct mm_struct *mm) +static inline void cpu_tlbstate_update_lam(unsigned long lam, u64 untag_mask) { - this_cpu_write(cpu_tlbstate.lam, - mm->context.lam_cr3_mask >> X86_CR3_LAM_U57_BIT); - this_cpu_write(tlbstate_untag_mask, mm->context.untag_mask); + this_cpu_write(cpu_tlbstate.lam, lam >> X86_CR3_LAM_U57_BIT); + this_cpu_write(tlbstate_untag_mask, untag_mask); } #else @@ -413,7 +412,7 @@ static inline u64 tlbstate_lam_cr3_mask(void) return 0; } -static inline void set_tlbstate_lam_mode(struct mm_struct *mm) +static inline void cpu_tlbstate_update_lam(unsigned long lam, u64 untag_mask) { } #endif diff --git a/arch/x86/kernel/process_64.c b/arch/x86/kernel/process_64.c index 76e91fc68c5f3..748d2b3bdb985 100644 --- a/arch/x86/kernel/process_64.c +++ b/arch/x86/kernel/process_64.c @@ -753,10 +753,12 @@ static long prctl_map_vdso(const struct vdso_image *image, unsigned long addr) static void enable_lam_func(void *__mm) { struct mm_struct *mm = __mm; + unsigned long lam = mm_lam_cr3_mask(mm); + u64 untag_mask = mm_untag_mask(mm); if (this_cpu_read(cpu_tlbstate.loaded_mm) == mm) { - write_cr3(__read_cr3() | mm->context.lam_cr3_mask); - set_tlbstate_lam_mode(mm); + write_cr3(__read_cr3() | lam); + cpu_tlbstate_update_lam(lam, untag_mask); } } diff --git a/arch/x86/mm/tlb.c b/arch/x86/mm/tlb.c index e8feb2e154db2..b2f74c451b3d7 100644 --- a/arch/x86/mm/tlb.c +++ b/arch/x86/mm/tlb.c @@ -11,6 +11,7 @@ #include #include #include +#include #include #include @@ -635,7 +636,6 @@ void switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *next, } new_lam = mm_lam_cr3_mask(next); - set_tlbstate_lam_mode(next); if (need_flush) { this_cpu_write(cpu_tlbstate.ctxs[new_asid].ctx_id, next->context.ctx_id); this_cpu_write(cpu_tlbstate.ctxs[new_asid].tlb_gen, next_tlb_gen); @@ -654,6 +654,7 @@ void switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *next, this_cpu_write(cpu_tlbstate.loaded_mm, next); this_cpu_write(cpu_tlbstate.loaded_mm_asid, new_asid); + cpu_tlbstate_update_lam(new_lam, mm_untag_mask(next)); if (next != real_prev) { cr4_update_pce_mm(next); @@ -700,6 +701,7 @@ void initialize_tlbstate_and_flush(void) int i; struct mm_struct *mm = this_cpu_read(cpu_tlbstate.loaded_mm); u64 tlb_gen = atomic64_read(&init_mm.context.tlb_gen); + unsigned long lam = mm_lam_cr3_mask(mm); unsigned long cr3 = __read_cr3(); /* Assert that CR3 already references the right mm. */ @@ -707,7 +709,7 @@ void initialize_tlbstate_and_flush(void) /* LAM expected to be disabled */ WARN_ON(cr3 & (X86_CR3_LAM_U48 | X86_CR3_LAM_U57)); - WARN_ON(mm_lam_cr3_mask(mm)); + WARN_ON(lam); /* * Assert that CR4.PCIDE is set if needed. (CR4.PCIDE initialization @@ -726,7 +728,7 @@ void initialize_tlbstate_and_flush(void) this_cpu_write(cpu_tlbstate.next_asid, 1); this_cpu_write(cpu_tlbstate.ctxs[0].ctx_id, mm->context.ctx_id); this_cpu_write(cpu_tlbstate.ctxs[0].tlb_gen, tlb_gen); - set_tlbstate_lam_mode(mm); + cpu_tlbstate_update_lam(lam, mm_untag_mask(mm)); for (i = 1; i < TLB_NR_DYN_ASIDS; i++) this_cpu_write(cpu_tlbstate.ctxs[i].ctx_id, 0);