From patchwork Tue Mar 12 15:56:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yosry Ahmed X-Patchwork-Id: 13590249 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 292D9C54E58 for ; Tue, 12 Mar 2024 15:56:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B493B8D005B; Tue, 12 Mar 2024 11:56:55 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AF92C8D0057; Tue, 12 Mar 2024 11:56:55 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9C1688D005B; Tue, 12 Mar 2024 11:56:55 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 896868D0057 for ; Tue, 12 Mar 2024 11:56:55 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 340DD1A0551 for ; Tue, 12 Mar 2024 15:56:55 +0000 (UTC) X-FDA: 81888840390.29.0EC23A0 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) by imf20.hostedemail.com (Postfix) with ESMTP id 818491C0011 for ; Tue, 12 Mar 2024 15:56:53 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=4PuZlJ3Q; spf=pass (imf20.hostedemail.com: domain of 3RHvwZQoKCDMndhgnPWbTSVddVaT.RdbaXcjm-bbZkPRZ.dgV@flex--yosryahmed.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3RHvwZQoKCDMndhgnPWbTSVddVaT.RdbaXcjm-bbZkPRZ.dgV@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=1710259013; 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=dGVz1SMb2zVErgYS/f8v/LIvaYP5LVM/6U9nkrrlBVE=; b=L1Wo9IN2bWfvs3zp4gpV0P8bkNZ/GjBnJz1fJCqqEsTY3AtP0vaCXnjTvNSIROYgH3aT99 q7llrBy5JCR9G97QMqEoENRlI3XR88oq8UP/lKFiIiU8c5HPuTMTk7Si4QQ/7Fwz3lOJ87 SAoXOfrGG4jfZAa9qs1LauVSEUfOAFQ= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1710259013; a=rsa-sha256; cv=none; b=FYLEzpAwfIGu8hJeqfautkRKMUootjE+2TjfTuNjO2ezypUPYkwdavRxqE5bkllrq6l3gq +4RmXMVTgkH3dW8MO3eWCXarhX488cf+wLY0ss8zZFRF31zPBYFeH7GOtPnvrgRCseNqDX LXfoEl4x9WJYOLTOOIQPosJfj9QoNME= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=4PuZlJ3Q; spf=pass (imf20.hostedemail.com: domain of 3RHvwZQoKCDMndhgnPWbTSVddVaT.RdbaXcjm-bbZkPRZ.dgV@flex--yosryahmed.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3RHvwZQoKCDMndhgnPWbTSVddVaT.RdbaXcjm-bbZkPRZ.dgV@flex--yosryahmed.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-dc693399655so10700320276.1 for ; Tue, 12 Mar 2024 08:56:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1710259012; x=1710863812; 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=dGVz1SMb2zVErgYS/f8v/LIvaYP5LVM/6U9nkrrlBVE=; b=4PuZlJ3Qw3DiaH0hHg/n+lRd2Lpx9BFWZsQnlsZz3o3B9etsj1t6pF9bonJ72H2pST 76qcf/KMI6T/63W5qnXHLy2Q6/FtQ3suh5Xn0Pg4+f0n5GTam+AyYyTF6jcvJyg/b4n9 CgyRdhJKmAsFgcn/rkfp0sSXSVqw0eb3bxX1ppOnRErc0lB2at6BajHDbhijypnk3i25 o87Fd5TOaJy36LP8mn/1qx6+zEL6mdn2OYnB8MKkLaV9yccoeqn9Eu348VML9u8ccWns sywCA9R+w5unCh5cRNw9EAEoYqlsrgNLLFVRoQPt3GfJniD0doPJu3E3/bIBaPqrvmlS sC3w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710259012; x=1710863812; 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=dGVz1SMb2zVErgYS/f8v/LIvaYP5LVM/6U9nkrrlBVE=; b=ZBT0Bi/lnuXZE4hJspOZ1FjV3xhb//rvqSRyCzvvERUuua7doIf+f7T7a0hE18yoIK rGWPMX4LiKI0oN0M7n5xgtUKWN8DIkHKfl+5sGTwnWG5eBMs/aOmC9YNpE8GfK4BgKv9 kg4IDsA2MIwqI9tSvkB2nWwkO3YqwaHSydD96cEcFi9IridhL9kTypfZ65qThk8Sqc/6 5XAb/23qJSqtEJTbepa1IME1o3XrZH46xFvZ1/PZk2+WxRdR3rtwmf1/C9mLBJecPvCj aP2m5c8VjnTZx7Z8ky7hiVR+pOpa0aAEZLKG3Ept55YOoEwBngXBST84+8Q9c8x9hxv3 7cig== X-Forwarded-Encrypted: i=1; AJvYcCUa+wLBTrzUMgG2+INYKgnZ/n22IV4DgLiFVHpLit58xg01nSg0dvF228tOLM8twFotH9ewNIhjL6FRaIMxPRW0n0c= X-Gm-Message-State: AOJu0YzhNXazRPUDTxL19hPT2qeMLLs2spwKBtP7uO0aK6px35VFVSJQ Rj3HA1KV5oU90/EHlv7JWhjXTeFyNfegpWxHYQtY8UeBT34cbwnIQ0+NzCMv9R35BNO+0KWS7Q7 nBiBi503YALxGCe5ZwQ== X-Google-Smtp-Source: AGHT+IEWr4BUvfejWnq0y5OUGkVPSF87K17RjNhILNkA1XXGVsqD9DHHQ9r1YalvT3IW9/ZqycxovNLaT6NPWZZp X-Received: from yosry.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:29b4]) (user=yosryahmed job=sendgmr) by 2002:a05:6902:70a:b0:dc7:68b5:4f3d with SMTP id k10-20020a056902070a00b00dc768b54f3dmr2745152ybt.11.1710259012661; Tue, 12 Mar 2024 08:56:52 -0700 (PDT) Date: Tue, 12 Mar 2024 15:56:40 +0000 In-Reply-To: <20240312155641.4003683-1-yosryahmed@google.com> Mime-Version: 1.0 References: <20240312155641.4003683-1-yosryahmed@google.com> X-Mailer: git-send-email 2.44.0.278.ge034bb2e1d-goog Message-ID: <20240312155641.4003683-2-yosryahmed@google.com> Subject: [PATCH v2 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: 818491C0011 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: sq7jytnw655a4qq7tafxmrpfjdzwybhw X-HE-Tag: 1710259013-675853 X-HE-Meta: U2FsdGVkX1+p/9V55AXHUTcXln+nYOrs6X5OAMAdBQ2I/k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w5hqHaSgmwMlr9XlC7Ta0swXhUtVcPxtmndIdGfO0DX/1AEnKj5wuqQwVOxdRwdNiySNcIeB7NEDPlwef9m9YeowvnmdWou5uydc4G4A5VEgjxKOXSToGxobqAVE1Gtq/keXXGwVSllefidQmu+5wfcX7XAxwbB4U+u4Tl4l6crUmot9T7y6FVPZkrFiz8qC1E3WN+HYwr9NeqlzpKV3XjM7HgaZ/SN95OQTTxBdXfpILVsWSM54 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 Reviewed-by: Kirill A. Shutemov --- 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..3dd8c1f51e45c 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; if (this_cpu_read(cpu_tlbstate.loaded_mm) == mm) { - write_cr3(__read_cr3() | mm->context.lam_cr3_mask); - set_tlbstate_lam_mode(mm); + lam = mm_lam_cr3_mask(mm); + write_cr3(__read_cr3() | lam); + cpu_tlbstate_update_lam(lam, mm_untag_mask(mm)); } } 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);