From patchwork Fri Nov 1 21:49:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Herton R. Krzesinski" X-Patchwork-Id: 13859790 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 AA75EE6F08E for ; Fri, 1 Nov 2024 21:49:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 121D86B00A1; Fri, 1 Nov 2024 17:49:18 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0D14E6B00A4; Fri, 1 Nov 2024 17:49:18 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EDB056B00A5; Fri, 1 Nov 2024 17:49:17 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id CDFA86B00A1 for ; Fri, 1 Nov 2024 17:49:17 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 8238E160468 for ; Fri, 1 Nov 2024 21:49:17 +0000 (UTC) X-FDA: 82738866126.13.D24286D Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf10.hostedemail.com (Postfix) with ESMTP id D7B2CC001F for ; Fri, 1 Nov 2024 21:49:02 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=YnIcsuVM; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf10.hostedemail.com: domain of herton@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=herton@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730497592; a=rsa-sha256; cv=none; b=OQRvv9c0juDOw/kowcr6nNLU/F0KUeSfcAuqcphD0vcnPrrNoXTchh1EgUgAX2Dk5mvfZ5 vWWzEjxMj7GiYGthileMFByzXsaZR1ZZI+BWB7YwCn76NeINpMmFMGarHt926aHXT/0S+r 1Qbt1GmKfEo+pUoMeArQdZNxmexKFNQ= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=YnIcsuVM; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf10.hostedemail.com: domain of herton@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=herton@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1730497592; 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:content-transfer-encoding:in-reply-to: references:dkim-signature; bh=PUoCs1r+x1Oq6GxS6Ct3AhaTO7U6watKZI+tJWSNU4M=; b=uxYj6IBudWUwZDNpccpFaUwyibCDLg1OBs/toCPvSbe3fPYk6eYB7lYJlYaZ67c0IARwGD MVFtc7vor5LMyNhO8pgO0X1cwjH6l0Y/3xzDyyVgouOoPu/awsQf1IV5lCrhGUxNS81Onw Mlf1JRnZNUmEyNwMc3IN0Hog9e5vqKE= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1730497755; h=from:from: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:content-transfer-encoding; bh=PUoCs1r+x1Oq6GxS6Ct3AhaTO7U6watKZI+tJWSNU4M=; b=YnIcsuVMtJ7R34Eb3E6Z5D9ZJZG7aO5EHl5v49yzwPdS445n/URRuRNiiUUQwk6Ih21HJE WEH77OByleTEnKteGWl0tx04AY0LCe3f0mWF6YQ5GaarjveKRa5RMMQdyGeySO9SoUZBwY 15FI4nP6Cn/AporVk3dGajxECJ0QGIY= Received: from mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-692-jUjfIl1IN1CS8mG-xfmPLQ-1; Fri, 01 Nov 2024 17:49:10 -0400 X-MC-Unique: jUjfIl1IN1CS8mG-xfmPLQ-1 Received: from mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.15]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 572D919560AB; Fri, 1 Nov 2024 21:49:07 +0000 (UTC) Received: from redhat.com (unknown [10.96.134.12]) by mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 3DBD41956086; Fri, 1 Nov 2024 21:49:05 +0000 (UTC) From: "Herton R. Krzesinski" To: Andrew Morton Cc: Nicholas Piggin , Linus Torvalds , Peter Zijlstra , Michael Ellerman , Thomas Gleixner , linuxppc-dev@lists.ozlabs.org, linux-mm@kvack.org Subject: [PATCH] lazy tlb: fix hotplug exit race with MMU_LAZY_TLB_SHOOTDOWN Date: Fri, 1 Nov 2024 18:49:04 -0300 Message-ID: <20241101214904.3178140-1-herton@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.15 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Stat-Signature: iacss6q7a738b4qemacoh173n3fjjkir X-Rspamd-Queue-Id: D7B2CC001F X-Rspamd-Server: rspam08 X-Rspam-User: X-HE-Tag: 1730497742-792844 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TMsGxPOzGcNsd51jfigO4pQpuirO5PfesuwUNjrnPLf0eqkrc3c+jHIYaWcPsoILpRRrZ6Jy+Od4d5k98i5QIOWv8kWCSj5tKWS4l5HiL3w0Bl8vwq2gA8sk/9K1+0tSm1t4heubxBwpv7iha2TUufzNBDqxmcs0bwsq3gFxn8xhIFEFE+DllUS9szGM9WkB7sQkYR6h89Q2dBp7kRd8Q/Fw4BhNKx8uLHgiCYV84mcAWO8IBgN03xEc9IfBNit0PlAedW6B74uJvG5QqCxSl4nj3ew0J1j7QvFyi8jvuTtDl7owXfgWe0OYnOEwnBkyhTlW5Z2y7gDRuTCqlJFETg0fjLp5IPm8LNCz9aCZR+LMVVPndjw3BUWBA1WHSsNF2R9MMmkgY8fdAe3ZD1AxwPtbovITIccBtq8t5e57SnxLQ/L7TqAQIG/QMayDTVAoZBzjCT/XVpi6qBbJFG/WV4wAf+XeMRWQ5prwl+k4zuRlW5tDF/3H+Nrso0/h0D0Td6jPNFL3LiXAplYMyM= 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: From: Nicholas Piggin CPU unplug first calls __cpu_disable(), and that's where powerpc calls cleanup_cpu_mmu_context(), which clears this CPU from mm_cpumask() of all mms in the system. However this CPU may still be using a lazy tlb mm, and its mm_cpumask bit will be cleared from it. The CPU does not switch away from the lazy tlb mm until arch_cpu_idle_dead() calls idle_task_exit(). If that user mm exits in this window, it will not be subject to the lazy tlb mm shootdown and may be freed while in use as a lazy mm by the CPU that is being unplugged. cleanup_cpu_mmu_context() could be moved later, but it looks better to move the lazy tlb mm switching earlier. The problem with doing the lazy mm switching in idle_task_exit() is explained in commit bf2c59fce4074 ("sched/core: Fix illegal RCU from offline CPUs"), which added a wart to switch away from the mm but leave it set in active_mm to be cleaned up later. So instead, switch away from the lazy tlb mm at sched_cpu_wait_empty(), which is the last hotplug state before teardown (CPUHP_AP_SCHED_WAIT_EMPTY). This CPU will never switch to a user thread from this point, so it has no chance to pick up a new lazy tlb mm. This removes the lazy tlb mm handling wart in CPU unplug. With this, idle_task_exit() is not needed anymore and can be cleaned up. This leaves the prototype alone, to be cleaned after this change. herton: took the suggestions from https://lore.kernel.org/all/87jzvyprsw.ffs@tglx/ and made adjustments on the initial patch proposed by Nicholas. Link: https://lkml.kernel.org/r/20230524060455.147699-1-npiggin@gmail.com Link: https://lore.kernel.org/all/20230525205253.E2FAEC433EF@smtp.kernel.org/ Fixes: 2655421ae69fa ("lazy tlb: shoot lazies, non-refcounting lazy tlb mm reference handling scheme") Signed-off-by: Nicholas Piggin Cc: Linus Torvalds Cc: Peter Zijlstra Suggested-by: Thomas Gleixner Signed-off-by: Herton R. Krzesinski --- include/linux/sched/hotplug.h | 4 ---- kernel/cpu.c | 11 ++++++----- kernel/sched/core.c | 26 +++++++++++++++++++------- 3 files changed, 25 insertions(+), 16 deletions(-) Herton: I contacted Nicholas by email, he was ok with me going ahead and posting this, I saw the original patch was stalled/didn't went forward. Thus I'm posting this but keeping his From/authorship, since he is original author of the patch, so we can have this moving forward. I have a report and also reproduced the warning similar to the one reported at https://github.com/linuxppc/issues/issues/469 - which can be triggered doing cpu offline/online loop with CONFIG_DEBUG_VM enabled. This patch fixes the problem. I updated the changelog/patch based on the suggestions given and to the best of my knowledge/investigation on this issue, thorough review is appreciated. If this is ok then I can submit a followup for this to cleanup idle_task_exit(). diff --git a/include/linux/sched/hotplug.h b/include/linux/sched/hotplug.h index 412cdaba33eb..17e04859b9a4 100644 --- a/include/linux/sched/hotplug.h +++ b/include/linux/sched/hotplug.h @@ -18,10 +18,6 @@ extern int sched_cpu_dying(unsigned int cpu); # define sched_cpu_dying NULL #endif -#ifdef CONFIG_HOTPLUG_CPU -extern void idle_task_exit(void); -#else static inline void idle_task_exit(void) {} -#endif #endif /* _LINUX_SCHED_HOTPLUG_H */ diff --git a/kernel/cpu.c b/kernel/cpu.c index d293d52a3e00..fb4f46885cb2 100644 --- a/kernel/cpu.c +++ b/kernel/cpu.c @@ -904,13 +904,14 @@ static int finish_cpu(unsigned int cpu) struct task_struct *idle = idle_thread_get(cpu); struct mm_struct *mm = idle->active_mm; - /* - * idle_task_exit() will have switched to &init_mm, now - * clean up any remaining active_mm state. + /* + * sched_force_init_mm() ensured the use of &init_mm, + * drop that refcount now that the CPU has stopped. */ - if (mm != &init_mm) - idle->active_mm = &init_mm; + WARN_ON(mm != &init_mm); + idle->active_mm = NULL; mmdrop_lazy_tlb(mm); + return 0; } diff --git a/kernel/sched/core.c b/kernel/sched/core.c index dbfb5717d6af..8a51acd6d650 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -7826,19 +7826,26 @@ void sched_setnuma(struct task_struct *p, int nid) #ifdef CONFIG_HOTPLUG_CPU /* - * Ensure that the idle task is using init_mm right before its CPU goes - * offline. + * Invoked on the outgoing CPU in context of the CPU hotplug thread + * after ensuring that there are no user space tasks left on the CPU. + * + * If there is a lazy mm in use on the hotplug thread, drop it and + * switch to init_mm. + * + * The reference count on init_mm is dropped in finish_cpu(). */ -void idle_task_exit(void) +static void sched_force_init_mm(void) { struct mm_struct *mm = current->active_mm; - BUG_ON(cpu_online(smp_processor_id())); - BUG_ON(current != this_rq()->idle); - if (mm != &init_mm) { - switch_mm(mm, &init_mm, current); + mmgrab_lazy_tlb(&init_mm); + local_irq_disable(); + current->active_mm = &init_mm; + switch_mm_irqs_off(mm, &init_mm, current); + local_irq_enable(); finish_arch_post_lock_switch(); + mmdrop_lazy_tlb(mm); } /* finish_cpu(), as ran on the BP, will clean up the active_mm state */ @@ -7972,6 +7979,10 @@ static void balance_hotplug_wait(void) #else +static void sched_force_init_mm(void) +{ +} + static inline void balance_push(struct rq *rq) { } @@ -8240,6 +8251,7 @@ int sched_cpu_starting(unsigned int cpu) int sched_cpu_wait_empty(unsigned int cpu) { balance_hotplug_wait(); + sched_force_init_mm(); return 0; }