Received: by 2002:a05:6500:1b45:b0:1f5:f2ab:c469 with SMTP id cz5csp1002022lqb; Wed, 17 Apr 2024 18:28:47 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWK7Z9AQ56n7CynCLBkf41oVjJOMyZQymv3CzRaoH3O4AhkY10k+tpy9Eex9yqXMWfw3lGieorI0q6yDpEWbFrOp9Tx/6rv9mBKLKQaSQ== X-Google-Smtp-Source: AGHT+IGwtxvM48vDSgjjIwkJ3yetwnVvLlFEJq23SdHUkkUe12KadyPZBwH3mZrXXiSnyYq+uOnH X-Received: by 2002:a17:903:32cf:b0:1e4:471f:2fa1 with SMTP id i15-20020a17090332cf00b001e4471f2fa1mr1486682plr.24.1713403727329; Wed, 17 Apr 2024 18:28:47 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1713403727; cv=pass; d=google.com; s=arc-20160816; b=QWKcgQboiAuGc/dPy6tza6phPN1ywPiTFVvMMptziNQW4hyDjTg5rd+UNYOcOuHY2Y 5EorxQBcLeZIQwcy82k5oQH0zceNJ7b3QIHcbmgFeGVNlsFGzey0zImC++Jy3d5H7A++ DzCI12MmLA3cbAHgcY2K5gHAIVQHLkh0pYi7Blw/XYFWEmBqhP7qDpM9H640jdt9m91Q GCujhchMM8+rJ/wfxuclh3HUfGKU9asumwXawAfxQKIwa2W7t7M/cRTmL2oR7bhn2Rn+ kWNn3UYYU4x2V5xGpZPA7bxQhFl4AJXkU7s/vseqIe6JZa+hcLuaLAWqSWMbBsLVGZJg 4tTg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:dkim-signature; bh=FYDQyLdFdWMdeiUN2dEgWolx8IdIlWszL4UA+NDjWJ0=; fh=xVZQPUbz99rnnZH3ndWO+yC/h+qkMErh9SsZkWT1znk=; b=t64x7YM1fzR8kS8/kFib9yHQ5/MyCcCpVT0PGAlxBMn6Uf1Br3d/dTUiCOyA3/X5y5 8HeY27mmKLp1rkUPBf7MhenOZrnt37LA5EOwIMjGzOGVnS4hzzdNpfF0vzgoGso7fruN 44dRiJBuNfdG/0DVLjfYbv3yraeMZDv0vfpq49CYahWAaV1+GPLb+5+PHJsrXEe3Nv7o RwMjTVK3811YfOu8h7uujlbQCdmu+yNrHDVWtcNAEvHCmb6khZqyQFoRMeTm6+M1vUnp nSkHhD7UFys7jP/CtrhgI/nDJozGpK+b+2Zb69SY3ha/MUXp2ZGOABAtvv8dam6vl0Rw dJrw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=eT3eZnKO; arc=pass (i=1 spf=pass spfdomain=flex--yosryahmed.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-149399-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-149399-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id u20-20020a170903305400b001e81c336882si410532pla.357.2024.04.17.18.28.47 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Apr 2024 18:28:47 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-149399-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=eT3eZnKO; arc=pass (i=1 spf=pass spfdomain=flex--yosryahmed.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-149399-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-149399-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id CC4122825AA for ; Thu, 18 Apr 2024 01:28:46 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6F638848E; Thu, 18 Apr 2024 01:28:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="eT3eZnKO" Received: from mail-pg1-f201.google.com (mail-pg1-f201.google.com [209.85.215.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2714019A for ; Thu, 18 Apr 2024 01:28:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713403720; cv=none; b=n+L8NioxFFkiRs3wmJnL/Lu7LhZcVEUlDT9kJvw9nLQZ5HKx/ANiFIKkm09v+Vnq9Ugkdy6bajREJ2nktpLR0PQqv1wZpWgu97LBULRbTC9RG1+5D8s/MMd1pK6Cy07t1vHiITYrifExBEK0UuR/5vPpp8ES0rOS0zZF6kDWcjw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713403720; c=relaxed/simple; bh=D9uAxfhXCIibdUQvBdEAguSzWPl/w/50yeP3Yso4sDY=; h=Date:Mime-Version:Message-ID:Subject:From:To:Cc:Content-Type; b=eP4RzwdeSwuTFHtjiaPpNyTLWBAvQmXQ1VKS3C56pqBd5L+sfK4f+eyq6TMcq0N80iwJJRTxZ+nNiDqRUsZ01ebCW495+JDHwlf68pGAZZ+Rqvel/my1S6lDz6RLfia5k7p9zuNPuBaaapQ1QpSk3RVEdqOFr3pKzCkFvrTJtCc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--yosryahmed.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=eT3eZnKO; arc=none smtp.client-ip=209.85.215.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--yosryahmed.bounces.google.com Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-5d8bff2b792so333665a12.1 for ; Wed, 17 Apr 2024 18:28:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1713403718; x=1714008518; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=FYDQyLdFdWMdeiUN2dEgWolx8IdIlWszL4UA+NDjWJ0=; b=eT3eZnKOldVPe7K82MsF4QaeqJSwao0PfngldjfOYNu0iJRNF6toUGZZ8ppTPiD2Jb z+f9/SISFwDgCpkyZ/TKHdvUon5lgtcYdtZOH1CeZmeN78vPSDKsHuHj7Q7PxXVVTIrb XIqf98c4DahuWYVB2RGBvobCUj9B+YVt1AVTxyKGWw7SCwFElH1HioInME0KhqAV7FS1 +Ww1nlEPLe0JjnJu9kXAzJQVVIOg2mq4ROfApEYB41MBygZh9jCc05y2W0UNbULqfblN 4wPfDzg0ojdpH/B0iLhpQ7q25H3OPtA/Ofn2K7Vno81Iv8OslCCB7O1GourmCCSavw0g Ke3w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713403718; x=1714008518; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=FYDQyLdFdWMdeiUN2dEgWolx8IdIlWszL4UA+NDjWJ0=; b=dE2Qvl+Y7UH/kuFRGj/5xSdrpPQx6Ncp+PKBunyLECKotPSUPnYXzytHYz8E5JdleG mfOp9ZucJxG6Bk1FIuQKh1U65WamquprWhQI61bJoom3EVQMiHTBLIyPSBUo+8w+F20N JhDMV5hmad2Aq0/wNkviHufWENfC5gxNHVqqmT7AGzVUrZJ2+CzXf0VmxcvpYF1+avur WUThFHRtt+iy7oRm/9Dzx74pac2xw3WpveZeVHpQfc5yoZ/95as6W73nCCX6+sa2kQgX 2rFriqQ97+pFumHL+wk8ky3A1O+mmiFtw1DkpAv7PPXiXgZjiNeXtGfz32I9JRpI3BzX nA/g== X-Forwarded-Encrypted: i=1; AJvYcCUGr7q0UAgyOoF4YbjhdD7C2bTjkNMwLxYQ1RDZqSApI9KUkZcDVUeUaDHOsPNP+JU4B4abLPX2CV5CDH7fTJmdcFtIA7+RYe5dyw5Q X-Gm-Message-State: AOJu0YxXClBi30RLPxLEHvPlyjRxnj551mI6qipNqLoVuS7P0IeME+BY yIQBNmGM+rAKdFjpIFLwNIxXcDWsVMQ1t8rhepXbg5rtaw9d2C4E6UkRis/pYg4xKFLReY+VknS 6hcbaf0FQsKpR4xe7Og== X-Received: from yosry.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:29b4]) (user=yosryahmed job=sendgmr) by 2002:a63:e57:0:b0:5dc:20e1:c894 with SMTP id 23-20020a630e57000000b005dc20e1c894mr3697pgo.4.1713403718174; Wed, 17 Apr 2024 18:28:38 -0700 (PDT) Date: Thu, 18 Apr 2024 01:28:33 +0000 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 X-Mailer: git-send-email 2.44.0.683.g7961c838ac-goog Message-ID: <20240418012835.3360429-1-yosryahmed@google.com> Subject: [PATCH tip:x86/mm v3 1/3] x86/mm: Use IPIs to synchronize LAM enablement From: Yosry Ahmed To: x86@kernel.org, Thomas Gleixner , Ingo Molnar Cc: 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 Content-Type: text/plain; charset="UTF-8" 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 --- v2 -> v3: - Rebased on top of the latest tip:x86/mm after v6.9-rc3. - Collected R-b on patch 2 (thanks!). --- 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 7062b84dd467d..c27798f23ef82 100644 --- a/arch/x86/kernel/process_64.c +++ b/arch/x86/kernel/process_64.c @@ -798,6 +798,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)) @@ -830,8 +840,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 44ac64f3a047c..a041d2ecd8380 100644 --- a/arch/x86/mm/tlb.c +++ b/arch/x86/mm/tlb.c @@ -503,9 +503,9 @@ void switch_mm_irqs_off(struct mm_struct *unused, struct mm_struct *next, { struct mm_struct *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; @@ -619,9 +619,7 @@ void switch_mm_irqs_off(struct mm_struct *unused, struct mm_struct *next, cpumask_clear_cpu(cpu, mm_cpumask(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); @@ -633,6 +631,7 @@ void switch_mm_irqs_off(struct mm_struct *unused, 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); -- 2.44.0.683.g7961c838ac-goog