From patchwork Tue Mar 12 15:56:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yosry Ahmed X-Patchwork-Id: 13590248 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 6606EC54E5D for ; Tue, 12 Mar 2024 15:56:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DE1348D005A; Tue, 12 Mar 2024 11:56:53 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D91478D0057; Tue, 12 Mar 2024 11:56:53 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C59318D005A; Tue, 12 Mar 2024 11:56:53 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id B45618D0057 for ; Tue, 12 Mar 2024 11:56:53 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 8AC6F80720 for ; Tue, 12 Mar 2024 15:56:53 +0000 (UTC) X-FDA: 81888840306.13.BE6074D Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) by imf28.hostedemail.com (Postfix) with ESMTP id ED6FEC0008 for ; Tue, 12 Mar 2024 15:56:51 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=HVbd3oyo; spf=pass (imf28.hostedemail.com: domain of 3Q3vwZQoKCDImcgfmOVaSRUccUZS.QcaZWbil-aaYjOQY.cfU@flex--yosryahmed.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3Q3vwZQoKCDImcgfmOVaSRUccUZS.QcaZWbil-aaYjOQY.cfU@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=1710259012; 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: references:dkim-signature; bh=Utpo4un8WURVOryq3iaezlwySVNXykpwBlPdcQcpqSc=; b=kFsiGX56c21nw4vkuS2sADqECTYQw2bNWyQGI2v+M2joVYC8Ku8DNbKy4yetiWjZs+7oV5 Guxl5RxG4rJ9aDvX+yWDBHlL7gF2xtQvgu0lxqcKfA4ZU7VLBaY9QJPZ8XVwMA3rKjF5KF 3UvQ4HI/Mg5y4j1TevtXCAnTCyMhj7s= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1710259012; a=rsa-sha256; cv=none; b=4Bw8OZHZfB7EOMrLG2iijQl/iRhg6sySdhPFKWZQlxwHmbHthaBxgs0SmRxwdquWogJVNZ QEt0lgdRGEG5jnOds0sJ/BhNffEx5GnTrqyCAw3upowRabkMKJcg+gazGbGOObps542b0o QE4MyTnANF/QY3I6p4Cd7APSPjljyAk= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=HVbd3oyo; spf=pass (imf28.hostedemail.com: domain of 3Q3vwZQoKCDImcgfmOVaSRUccUZS.QcaZWbil-aaYjOQY.cfU@flex--yosryahmed.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3Q3vwZQoKCDImcgfmOVaSRUccUZS.QcaZWbil-aaYjOQY.cfU@flex--yosryahmed.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-dce775fa8adso9791485276.1 for ; Tue, 12 Mar 2024 08:56:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1710259011; x=1710863811; darn=kvack.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=Utpo4un8WURVOryq3iaezlwySVNXykpwBlPdcQcpqSc=; b=HVbd3oyoyiRViQGT9ZUIy1uCPwQqTcrhoOluLKKPZ0AN2BYPM/cbH0mBSF/AoIPnbD ZhhKZDuoeQ22MgQuQ5GUowgcgq20vaTsCAGScgockpsdDI6PrczYYJwlzpExg78zagE6 al8TNWNHZx5JOTHeIFnXfJQ9egVFl1lNsGhdf22idAN6UzKfKOIX7khfhqDWLUIgPiU6 XK5wnj5M4eKlCMl/xA/xNCM3Ts6cb1CDD8DNUdNARzNKzmkUziDIBuwQifzbskWDNjqQ 8DABP6NZmEhb7zDIk8XP6hbMX3l/JBto7V+asAX/42YvwrJA4wF9ercBkLWADOh4pQR7 sXjQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710259011; x=1710863811; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=Utpo4un8WURVOryq3iaezlwySVNXykpwBlPdcQcpqSc=; b=CEokwOQkKVpNVgY7j2i9s+c/YSKhm2osHtepuGKe4wO0ueFtWBQxgVTPweVvqjV0Ky vQap2BGGIrUAaGFPfqzJgpH+hYf2ENBFgWjswlIPAbvXjVPUGafcziZOxWFyE15YBA9v 1HvkQoffXqzv9LvFnZJbpHMfkfUAlttRstj9ieiJJT/p/yijgIguQdFI0YLcYLD9gNsE it0H9riiSGalNXU6VgVIYmhcqn/Iy+kU7wsnIQSflnU5zo6l+a573jfk/cP+K4HA1/Nk vtOJDEs6HBjcIAf7K/xO3Ymc1gZAMRpB9oufo+wzB1i2qkxvA8KdwureC0VHpQuXghO5 n0RQ== X-Forwarded-Encrypted: i=1; AJvYcCWmxflNHKwf9WYvcK2AXuQSlxpktJGWFirjf/N5INucZ7vy05/HivcvUk1D9ngVVAR2UJHAZSGcfHjgHITlzHe1Whg= X-Gm-Message-State: AOJu0YyRwhATlZFnVXYQ6KAH2E7N/03peXIFD+7Odzz/bu2cOAPKyb6E IodoWv3oTaxZ7HWd7Lyu4qdr69Og2bX3I0gFgw4btVbGGjQZH7ZDd6EZnlA4+DGVHEGbfN3q4kr oSxOwTZc8X0J3x94+5Q== X-Google-Smtp-Source: AGHT+IFTM3Svt5aG2KDAZZ5idfNa42+hKp13eDZYuCEzCHp68sCT3zJVywoPrbfqfLH7MdR0l2OCOvYs3V662FOa X-Received: from yosry.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:29b4]) (user=yosryahmed job=sendgmr) by 2002:a05:6902:10c2:b0:dc6:e1ed:bd1a with SMTP id w2-20020a05690210c200b00dc6e1edbd1amr2596454ybu.2.1710259011049; Tue, 12 Mar 2024 08:56:51 -0700 (PDT) Date: Tue, 12 Mar 2024 15:56:39 +0000 Mime-Version: 1.0 X-Mailer: git-send-email 2.44.0.278.ge034bb2e1d-goog Message-ID: <20240312155641.4003683-1-yosryahmed@google.com> Subject: [PATCH v2 1/3] x86/mm: Use IPIs to synchronize LAM enablement 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: ED6FEC0008 X-Rspam-User: X-Stat-Signature: 9unoguuchmzxi7ju67sagg1dsbwz6x44 X-Rspamd-Server: rspam03 X-HE-Tag: 1710259011-57069 X-HE-Meta: U2FsdGVkX1+04gh6n34tGF9qsNJrKD7IeNR/K9vKAyoA7fVyxBHomjyZddjRfN/+nO3YKMfCIfUO9a8HQduZ+J0xebOQDSS2WnkPNDyt5vjjvTQdbSAnjKwep+FYTpbab6Vh8PNJ3OZ35JK37YCkt+IGm3B47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Z+1c7a9z17vJGeh2K9Ru2HeDJVNniL9gXBlThMVNz9usSxVb872oG1F5UitTHfPo3gWTtwDezRROS2mmzyr6JFR5DhZiQ+FzzQgorKZZJSvs13aWucDoAmueFk0fPgTHYle0S+5Hczt6n 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. If LAM is enabled by a userspace process while a kthread is using its mm, the kthread will not observe LAM enablement (i.e. LAM will be disabled in CR3). This could be fine for the kthread itself, as LAM only affects userspace addresses. However, if the kthread context switches to a thread in the same userspace process, CR3 may or may not be updated because the mm_struct doesn't change (based on pending TLB flushes). If CR3 is not updated, the userspace thread will run incorrectly with LAM disabled, which may cause page faults when using tagged addresses. Example scenario: CPU 1 CPU 2 /* kthread */ kthread_use_mm() /* user thread */ prctl_enable_tagged_addr() /* LAM enabled on CPU 2 */ /* LAM disabled on CPU 1 */ context_switch() /* to CPU 1 */ /* Switching to user thread */ switch_mm_irqs_off() /* CR3 not updated */ /* LAM is still disabled on CPU 1 */ Synchronize LAM enablement by sending an IPI from prctl_enable_tagged_addr() to all CPUs running with the mm_struct to enable LAM. This makes sure LAM is enabled on CPU 1 in the above scenario before prctl_enable_tagged_addr() returns and userspace starts using tagged addresses, and before it's possible to run the userspace process on CPU 1. In switch_mm_irqs_off(), move reading the LAM mask until after mm_cpumask() is updated. This ensures that if an outdated LAM mask is written to CR3, an IPI is received to update it right after IRQs are re-enabled. Fixes: 82721d8b25d7 ("x86/mm: Handle LAM on context switch") Suggested-by: Andy Lutomirski Signed-off-by: Yosry Ahmed Reviewed-by: Kirill A. Shutemov --- v1 -> v2: - Defer dereferences in enable_lam_func() until after we check cpu_tlbstate.loaded_mm. - Collect Rbs. --- arch/x86/kernel/process_64.c | 13 +++++++++++-- arch/x86/mm/tlb.c | 7 +++---- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/arch/x86/kernel/process_64.c b/arch/x86/kernel/process_64.c index 33b268747bb7b..76e91fc68c5f3 100644 --- a/arch/x86/kernel/process_64.c +++ b/arch/x86/kernel/process_64.c @@ -750,6 +750,16 @@ static long prctl_map_vdso(const struct vdso_image *image, unsigned long addr) #define LAM_U57_BITS 6 +static void enable_lam_func(void *__mm) +{ + struct mm_struct *mm = __mm; + + if (this_cpu_read(cpu_tlbstate.loaded_mm) == mm) { + write_cr3(__read_cr3() | mm->context.lam_cr3_mask); + set_tlbstate_lam_mode(mm); + } +} + static int prctl_enable_tagged_addr(struct mm_struct *mm, unsigned long nr_bits) { if (!cpu_feature_enabled(X86_FEATURE_LAM)) @@ -782,8 +792,7 @@ static int prctl_enable_tagged_addr(struct mm_struct *mm, unsigned long nr_bits) return -EINVAL; } - write_cr3(__read_cr3() | mm->context.lam_cr3_mask); - set_tlbstate_lam_mode(mm); + on_each_cpu_mask(mm_cpumask(mm), enable_lam_func, mm, true); set_bit(MM_CONTEXT_LOCK_LAM, &mm->context.flags); mmap_write_unlock(mm); diff --git a/arch/x86/mm/tlb.c b/arch/x86/mm/tlb.c index 5768d386efab6..e8feb2e154db2 100644 --- a/arch/x86/mm/tlb.c +++ b/arch/x86/mm/tlb.c @@ -497,9 +497,9 @@ void switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *next, { struct mm_struct *real_prev = this_cpu_read(cpu_tlbstate.loaded_mm); u16 prev_asid = this_cpu_read(cpu_tlbstate.loaded_mm_asid); - unsigned long new_lam = mm_lam_cr3_mask(next); bool was_lazy = this_cpu_read(cpu_tlbstate_shared.is_lazy); unsigned cpu = smp_processor_id(); + unsigned long new_lam; u64 next_tlb_gen; bool need_flush; u16 new_asid; @@ -622,9 +622,7 @@ void switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *next, cpumask_clear_cpu(cpu, mm_cpumask(real_prev)); } - /* - * Start remote flushes and then read tlb_gen. - */ + /* Start receiving IPIs and then read tlb_gen (and LAM below) */ if (next != &init_mm) cpumask_set_cpu(cpu, mm_cpumask(next)); next_tlb_gen = atomic64_read(&next->context.tlb_gen); @@ -636,6 +634,7 @@ void switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *next, barrier(); } + 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);