From patchwork Tue Mar 12 03:59:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yosry Ahmed X-Patchwork-Id: 13589505 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 A9876C54E49 for ; Tue, 12 Mar 2024 03:59:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 229E76B0141; Mon, 11 Mar 2024 23:59:57 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1D8C16B0145; Mon, 11 Mar 2024 23:59:57 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0A2486B0143; Mon, 11 Mar 2024 23:59:57 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id EE6956B017E for ; Mon, 11 Mar 2024 23:59:56 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 9E79580D96 for ; Tue, 12 Mar 2024 03:59:56 +0000 (UTC) X-FDA: 81887033592.21.DEEDBC6 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) by imf16.hostedemail.com (Postfix) with ESMTP id ED5AD180006 for ; Tue, 12 Mar 2024 03:59:54 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=E0SdI+SX; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf16.hostedemail.com: domain of 3OtPvZQoKCNUPFJIP18D547FF7C5.3FDC9ELO-DDBM13B.FI7@flex--yosryahmed.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3OtPvZQoKCNUPFJIP18D547FF7C5.3FDC9ELO-DDBM13B.FI7@flex--yosryahmed.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1710215995; 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=H0vvm77KC92mPCMDKE7JIHK7Q88ob4yruXgItrPO0iI=; b=2ALLYI/BA9StcsNhO8JxHQFHJ4m+kuC0Al8w9y8GPUc/xOLw25UgKRJO3DQnFGIispD/IY wH5XzZkyowKW9/DhOIVzrCFI6y9MaFZLY0BkMukaBKTG1Y33Hlx9oenkVnOsr/a8f0zxYA ZxUFV7i9ajl8Q7UPv2HgBVZNMp8uj7c= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=E0SdI+SX; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf16.hostedemail.com: domain of 3OtPvZQoKCNUPFJIP18D547FF7C5.3FDC9ELO-DDBM13B.FI7@flex--yosryahmed.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3OtPvZQoKCNUPFJIP18D547FF7C5.3FDC9ELO-DDBM13B.FI7@flex--yosryahmed.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1710215995; a=rsa-sha256; cv=none; b=Pffl/lN02jF2wih2ERKqb4FZrWigyGGCiiF3Ni1e6sqabxG5Y9dBPJO3YM1EJ8aUhw6et/ B9N4ZaIsz8edeBqbL8dQTo+9EpeMbvdwskVLGozG+zCvToQmDeuruJrs53zL9e3MoA6ufn R+QanDlLcgjy8vFKYsMB0punS/n0mPc= Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-dd169dd4183so305602276.3 for ; Mon, 11 Mar 2024 20:59:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1710215994; x=1710820794; darn=kvack.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=H0vvm77KC92mPCMDKE7JIHK7Q88ob4yruXgItrPO0iI=; b=E0SdI+SX+84Wh7sl0TgXUJ/x4XHAyzIvijfqksNzUUhEH+Yvizuq00CTBnbrdYMCQ9 7VAfkjOVKVQLeXjesX2dVs6+9FBi1BhB7weUVcWJpjlPD7BQubR+/qy1OSf0ZxUpvcv4 iS4n4SlpiNLXaZVdO2cwje0QzhJmRXVDg2XOFK/ID7FvZT3hsLs+06H5qR1H2zdOL2uc Q5OxjsXnRbxIQz0W+QOEsUi78EMK0jLdqZVgq+lkXQO34jKRXwn2hh/ash/ys3JhXcML IrhmvyvMWNg9aLJNMgc79UlRov1juuBKeOTZV731EME0OaV2hQ9BFX3U+dblcItJ2F64 Djhg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710215994; x=1710820794; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=H0vvm77KC92mPCMDKE7JIHK7Q88ob4yruXgItrPO0iI=; b=cA1vvc6+W4hxOxMZb6pTh7UrkbEYoPKOiD4GHT73gbjEeIA155iq8AbFiV3IeMaba9 KtxF+Ov809BVRBod7MkwYlKKwpiBtzqrP387QPcGYT4P57XlQ5SSSOgMG2gk82ZS1xBX EzJaKQ/gCSP/aGRyRK6jQDxucp/Q/+NnX8ByDWTJ9o9r7/QIaSRy8Y9XwdvBKMcym7wn CIueOv07nsb1ejaK9L9rfS22IDXBcyjw3piq99pMOJAIhiYugbWRBr7i9HSm1eUxsSWz IkoQaYyK8rilMczao0AHS0+YhhBk29tAzbYqEQDFvPQs186Kbo0nvCvXsY8symd2WjEv wM9A== X-Forwarded-Encrypted: i=1; AJvYcCVcDOSfDpao72Bc7G/l85ou8B/8c1L72LwP5GRe/JYZGY4FkU08JDZzp74S/WDlYs01Siix+kDgd/xrYvm5nxj0Gww= X-Gm-Message-State: AOJu0YyDjnDsH3i+cHtZrlt+ltyzBSzjGYiUjO7Kv+q4feU9BTmONrSt 8jONfZ9APahZC6iiC6KKglWp3sAIvMliKx5qXn4RyGrZKIERYRz5Xdon/1B7Axu8fq883FXGDxj HBsROrsl6SBKPL6iYwg== X-Google-Smtp-Source: AGHT+IE3j2RbmWLGWhXwtCN+tJydKqNDYdq63uVBUe/GpO70ij5hxpAGdtBQ8Cwz7kcE3p6RwDKKNDFJe0VFKiIx X-Received: from yosry.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:29b4]) (user=yosryahmed job=sendgmr) by 2002:a05:6902:f02:b0:dcd:2f2d:7a0f with SMTP id et2-20020a0569020f0200b00dcd2f2d7a0fmr427882ybb.9.1710215994014; Mon, 11 Mar 2024 20:59:54 -0700 (PDT) Date: Tue, 12 Mar 2024 03:59:49 +0000 Mime-Version: 1.0 X-Mailer: git-send-email 2.44.0.278.ge034bb2e1d-goog Message-ID: <20240312035951.3535980-1-yosryahmed@google.com> Subject: [PATCH 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-Server: rspam09 X-Rspamd-Queue-Id: ED5AD180006 X-Stat-Signature: pc5otxuywab5teziwxp1nnm1y8e3sf6b X-Rspam-User: X-HE-Tag: 1710215994-607089 X-HE-Meta: U2FsdGVkX19i/Mmac58M9cO6J977hR/X8vhWZQpmO59qWrQTun02je2j2FOXsDgK0fgqyVbQycXAtl5A2WwWGtDE/1gRutZ48jdPXXtFtOIqF9BtTQnzVF0QL1fdx35RX3yuQCyg6+J/6sUxYSwvPZkm+4neZLKfRW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Agd/FOdSGDjVIMzVVmAc3Y+Hrk+lE5lDR4s1Ci1N+Vzr+3Kh63KXcEbrPV0R23xz/JSfuF41SG9/rluqfIrsUA9MjyF1ccG9+3R6nYo4PfRbS4A1gGeldnfql01z2aDFR4R0F6HT4kSkutGlq0OGrO0PrS0nyKZeztF1X/jggoGaNsgQKC/DmJFAM+gsaC0J9qn8qqr7UNoTq4gFfZ9FYsJoB39TQTtOvz1nyFqC2s5ZppQdDmonsT41/z/3bjVvHgD3hTk8O9krrwz1dig7yitRDLlM2ZTLGqxrVGNbMEvdaa4rZrTDESM7EBN8RDjgOYQREqNo1RUPmv9Sm7ZoWxML/VoD/4L8o/IyR8Pd4cxqyebZekEyn08Y04RBS/6n+jOHsJ5zJaVoaQuWxzjvLxVSoGBQv7HzFMGynXB93CUyvmpZZl7zew5r1RUDe+Ouzg= 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 --- 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);