From patchwork Fri Jan 26 08:06:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yosry Ahmed X-Patchwork-Id: 13532172 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 D1E49C48260 for ; Fri, 26 Jan 2024 08:06:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 60B6B8D0008; Fri, 26 Jan 2024 03:06:52 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 5A9C38D0002; Fri, 26 Jan 2024 03:06:52 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 425C28D0008; Fri, 26 Jan 2024 03:06:52 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 28C798D0002 for ; Fri, 26 Jan 2024 03:06:52 -0500 (EST) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id C06398046A for ; Fri, 26 Jan 2024 08:06:51 +0000 (UTC) X-FDA: 81720731022.13.8273BD3 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) by imf03.hostedemail.com (Postfix) with ESMTP id 08EB920016 for ; Fri, 26 Jan 2024 08:06:49 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=gLccipLw; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf03.hostedemail.com: domain of 3GWizZQoKCOokaedkMTYQPSaaSXQ.OaYXUZgj-YYWhMOW.adS@flex--yosryahmed.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3GWizZQoKCOokaedkMTYQPSaaSXQ.OaYXUZgj-YYWhMOW.adS@flex--yosryahmed.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1706256410; 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=qO38tEb5zM4VymKs6ibKE+57i3iPVkOa3/Idb5enOqs=; b=Q7NKzSU8qBEy4+0OerqY9NLaad0iVibeHpX9tlwnqZHz3JbUFkj7E30qvqRVhlMpqcysxA 643ZxHTVmbgTN9RAF/AsBSfz16WkRww3QPaS4d/1bN2YXrh4/22fKt+SCWAzbGIkXL464z 8nn0xkV0xxSsA86lMqVLxgGMtPgKrYg= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=gLccipLw; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf03.hostedemail.com: domain of 3GWizZQoKCOokaedkMTYQPSaaSXQ.OaYXUZgj-YYWhMOW.adS@flex--yosryahmed.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3GWizZQoKCOokaedkMTYQPSaaSXQ.OaYXUZgj-YYWhMOW.adS@flex--yosryahmed.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706256410; a=rsa-sha256; cv=none; b=PBQaglGnOTFmoHr7yXvBcvm7E1gu6xpZGmu54/28JEGglR4y2zmpDxIgV+rHjy1tYlEJ19 EWayNNtTs/qPdBO4pSt10T+dWBlxDFTIjd1PwrHpcaHdqNfnfiOR6UPEJe9ZAvrTVDbK5N fVhbhDAeX6vmyxUfN9HvYhVTnhV7x1Y= Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-dc24de01bd9so231626276.1 for ; Fri, 26 Jan 2024 00:06:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1706256409; x=1706861209; 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=qO38tEb5zM4VymKs6ibKE+57i3iPVkOa3/Idb5enOqs=; b=gLccipLwXdYj34JXAlFIrZ9VLDYGawhu+LVFQlqM8J2VFjDtq+dxGxJJCdYTEOZdL7 koWHkGsOAuex09IAbTsZUuEQ8LqcCUYCEuLwIBED1/jPjAv0CTwn6i6a0CZeFy7u+paa tvylTzCEAgvmGtV6ti/TX4aakaUb6Y6cOZ08KNym2IBzhdnc9E+Oi1A4dj/t0KfpZVyf RLA44tYSnOVbtP6WQQiZ/IIa1AiDdr8N6bC+CSyDZ0tus+vPbDUu3FG8KWAXVRxd9lU3 zaIOQI4xeY/CSt0zjvQC502JSPzT2pXxI4KC18fK9OLZMeGHtIIL/mD1FlZQlCiu80V/ +W/w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706256409; x=1706861209; 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=qO38tEb5zM4VymKs6ibKE+57i3iPVkOa3/Idb5enOqs=; b=U38dCmJJz7ONsa1bAo92r16dCL5mjwNIvLXoQ4XM3Re0cHpGtVzxTqWkBovyy9IM1M i4p986lk7L7404YgMZLNlG1sbxlnXaNyeRS6tOiiHr23HSqowSOELixEmBCmPo8eKX1x +m0hp4rRPx+yKtEXMmPYUBxbPNqkHZFz5hAMFMjbki26HA0iEX+cGgbhsjtMRCTlS1jC /k3bFRZGtymQzBoaUGg9wacQdGUh+hEOzN8+ldLVBYvhZ/1oIMaA40pj/TBNid+JRkVy mvJkzR2RvhJU05JQbphanyg1O2Ybl5uzdGSk7Rjb1zCv1SjEWZ2lX7VRgMDBiqiRuwPu 6GJw== X-Gm-Message-State: AOJu0YzvdF9Y4hINiAORiFjGXuX3oFeLPOUflKEyaIQdDXI6VS2/eGmk i7+5WRa5y8CfGlf5wPr5OKG4lhE37rlZIj4kdB5MfRwjKRoZLdsGRNtiSzSF74xWSyFPG/XNN0O im/Wo8SFsrxCUxmcacQ== X-Google-Smtp-Source: AGHT+IGtSc7FTilAvbWm1bnuTEL3QKF1axH5jK6w0o1wwqlROcZ4vY9lg44aTpbRR3M0/L4W8/DAZIlzpDwiNTHT X-Received: from yosry.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:29b4]) (user=yosryahmed job=sendgmr) by 2002:a05:6902:2085:b0:dc2:5525:f6b with SMTP id di5-20020a056902208500b00dc255250f6bmr539405ybb.7.1706256409103; Fri, 26 Jan 2024 00:06:49 -0800 (PST) Date: Fri, 26 Jan 2024 08:06:44 +0000 In-Reply-To: <20240126080644.1714297-1-yosryahmed@google.com> Mime-Version: 1.0 References: <20240126080644.1714297-1-yosryahmed@google.com> X-Mailer: git-send-email 2.43.0.429.g432eaa2c6b-goog Message-ID: <20240126080644.1714297-2-yosryahmed@google.com> Subject: [PATCH 2/2] x86/mm: clarify "prev" usage in switch_mm_irqs_off() From: Yosry Ahmed To: Ingo Molnar Cc: Thomas Gleixner , Borislav Petkov , Dave Hansen , Andy Lutomirski , Peter Zijlstra , Andrew Morton , x86@kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Yosry Ahmed X-Rspam-User: X-Stat-Signature: kactfbiqscji7jwffbooacxksw3qrxn3 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 08EB920016 X-HE-Tag: 1706256409-137720 X-HE-Meta: U2FsdGVkX18QodMyVBJtpcCeepNAgDXUyl2qzNQNrGLcEi6uOZ5ZG8ZvIfSpovhKTKuHcROhVpi9HfzcGxafwvp7mxxyGFZ9GPvoPNOleyhdLYJ90LOs0JnUZlqT7P/TiFI2K6ZHlJZ1BsGKYej1V1Kk4qMBOUDtYDgD8NP26B0Ky38zED9cXzg7/ptAXUfIbz8v0L6uWF4162qQ/xZNT95fwohqG9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Tw/u6jHF0hss1DUNGao5OIaZgr/HWdm5i4kLylTR5azbR35KeXdsJpUo3J+nLzFMOnlqY/tiIRTGIqGKR8AOUY5UcE4zcrY5vy/5uD6r43hSkUGt45kmyNm6AevJrza4d3ZDopNOhnXXC/DTmPTRk4i5tuO420oSryXieuvqytJbnQETxwGjNQjDhS6bYvN/cx5NooSdbA0GLVmoaCW1JRn9m9kJNeEC3e3BtGl1UKnMyfXKFpJpa48gVpsRJRqX0MFekoqNdkKDPLD5z6/DQHtLo/lP9kFjjGxZup0hAzK+D1f7rozcet7x8XqQ/E92gcXgkXI2zbuqsUrGdUuomLvBY9PLeNEd9XSWWNltzeKekzrE+OBz93yG0AtyzLivNNrA6z+gEqSRHpOEPyIg== 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: In the x86 implementation of switch_mm_irqs_off(), we do not use the "prev" argument passed in by the caller, we use exclusively use "real_prev", which is cpu_tlbstate.loaded_mm. This is not obvious at the first sight. Furthermore, a comment describes a condition that happens when called with prev == next, but this should not affect the function in any way since prev is unused. Apparently, the comment is intended to clarify why we don't rely on prev == next to decide whether we need to update CR3, but again, it is not obvious. The comment also references the fact that leave_mm() calls with prev == NULL and tsk == NULL, but this also shouldn't matter because prev is unused and tsk is only used in one function which has a NULL check. Clarify things by renaming (prev -> unused) and (real_prev -> prev), also move and rewrite the comment as an explanation for why we don't rely on "prev" supplied by the caller in x86 code and use our own. Hopefully this makes reading the code easier. Signed-off-by: Yosry Ahmed --- arch/x86/mm/tlb.c | 35 ++++++++++++++++------------------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/arch/x86/mm/tlb.c b/arch/x86/mm/tlb.c index 80b0caa82a91b..bf9605caf24f7 100644 --- a/arch/x86/mm/tlb.c +++ b/arch/x86/mm/tlb.c @@ -492,10 +492,16 @@ void cr4_update_pce(void *ignored) static inline void cr4_update_pce_mm(struct mm_struct *mm) { } #endif -void switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *next, +/* + * The "prev" argument passed by the caller does not always match CR3. For + * example, the scheduler passes in active_mm when switching from lazy TLB mode + * to normal mode, but switch_mm_irqs_off() can be called from x86 code without + * updating active_mm. Use cpu_tlbstate.loaded_mm instead. + */ +void switch_mm_irqs_off(struct mm_struct *unused, struct mm_struct *next, struct task_struct *tsk) { - struct mm_struct *real_prev = this_cpu_read(cpu_tlbstate.loaded_mm); + 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); @@ -504,15 +510,6 @@ void switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *next, bool need_flush; u16 new_asid; - /* - * NB: The scheduler will call us with prev == next when switching - * from lazy TLB mode to normal mode if active_mm isn't changing. - * When this happens, we don't assume that CR3 (and hence - * cpu_tlbstate.loaded_mm) matches next. - * - * NB: leave_mm() calls us with prev == NULL and tsk == NULL. - */ - /* We don't want flush_tlb_func() to run concurrently with us. */ if (IS_ENABLED(CONFIG_PROVE_LOCKING)) WARN_ON_ONCE(!irqs_disabled()); @@ -527,7 +524,7 @@ void switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *next, * isn't free. */ #ifdef CONFIG_DEBUG_VM - if (WARN_ON_ONCE(__read_cr3() != build_cr3(real_prev->pgd, prev_asid, + if (WARN_ON_ONCE(__read_cr3() != build_cr3(prev->pgd, prev_asid, tlbstate_lam_cr3_mask()))) { /* * If we were to BUG here, we'd be very likely to kill @@ -559,7 +556,7 @@ void switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *next, * provides that full memory barrier and core serializing * instruction. */ - if (real_prev == next) { + if (prev == next) { /* Not actually switching mm's */ VM_WARN_ON(this_cpu_read(cpu_tlbstate.ctxs[prev_asid].ctx_id) != next->context.ctx_id); @@ -574,7 +571,7 @@ void switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *next, * mm_cpumask. The TLB shootdown code can figure out from * cpu_tlbstate_shared.is_lazy whether or not to send an IPI. */ - if (WARN_ON_ONCE(real_prev != &init_mm && + if (WARN_ON_ONCE(prev != &init_mm && !cpumask_test_cpu(cpu, mm_cpumask(next)))) cpumask_set_cpu(cpu, mm_cpumask(next)); @@ -616,10 +613,10 @@ void switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *next, * Skip kernel threads; we never send init_mm TLB flushing IPIs, * but the bitmap manipulation can cause cache line contention. */ - if (real_prev != &init_mm) { + if (prev != &init_mm) { VM_WARN_ON_ONCE(!cpumask_test_cpu(cpu, - mm_cpumask(real_prev))); - cpumask_clear_cpu(cpu, mm_cpumask(real_prev)); + mm_cpumask(prev))); + cpumask_clear_cpu(cpu, mm_cpumask(prev)); } /* @@ -656,9 +653,9 @@ 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); - if (next != real_prev) { + if (next != prev) { cr4_update_pce_mm(next); - switch_ldt(real_prev, next); + switch_ldt(prev, next); } }