From patchwork Mon Nov 4 14:23:18 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: 2006269 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=J1bWFqfn; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=BcNPvv8f; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.ozlabs.org (client-ip=112.213.38.117; helo=lists.ozlabs.org; envelope-from=linuxppc-dev+bounces-2831-incoming=patchwork.ozlabs.org@lists.ozlabs.org; receiver=patchwork.ozlabs.org) Received: from lists.ozlabs.org (lists.ozlabs.org [112.213.38.117]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4XhtxS3qRPz1xxN for ; Tue, 5 Nov 2024 01:23:40 +1100 (AEDT) Received: from boromir.ozlabs.org (localhost [127.0.0.1]) by lists.ozlabs.org (Postfix) with ESMTP id 4XhtxR4Q19z2xwD; Tue, 5 Nov 2024 01:23:39 +1100 (AEDT) X-Original-To: linuxppc-dev@lists.ozlabs.org Authentication-Results: lists.ozlabs.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=lists.ozlabs.org; s=201707; t=1730730219; cv=none; b=j2JMKfmNwbZepvdHnVKc7T4Tw7KNqsq0kapzW11BIORDZJl3pY9T2zefwF5QH4ruT5fBVQ7X7zPFTmUeuhugRcxp6NW1DsHkXMfi1SQVqYA8MFlhy0khuEn6bFHGPqMXyg5wVrIN8+qNaine100EPgRtjeJ+Ix2P9x+kX3z0usx1nYkEIbV+125Byy9tNCBmUZcYTGi1FCnKIZik4GqJ7z+ElBXdivuvd4t+bTPUHnm4B18hcuX9sAZc7x9lf53erYMYqSFcfU6aMseGr/xqPafRHhu+ecRFcJbEVzqFZ58srBYjhRLr/usy4Zh3ZOVT9eX8fXD4EtZUmeV+SAnzAg== ARC-Message-Signature: i=1; a=rsa-sha256; d=lists.ozlabs.org; s=201707; t=1730730219; c=relaxed/relaxed; bh=j4zN/ElscuerA//rJUqf/4KurcEwyLi6zSlvuwt93ss=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version:Content-Type; b=Wzkg97H1aD7aR9HgS1CTLYloFFmqS8n+AwmhndE0lnb6frVtFkPuTXupf9To7sh7F9k4nMFT6r/i7sUts3akMqe1Rb5vFtr1LtVVo4BaEJGr8c+BDw4itxymtUrcpf9ZIX2hEZ3rodyFemhP+1v7kpHLGTk+5PShUxHk5MtyxHZXd0XIiKCSv3D5ulosR31VfKJYMwwdUDFoOsxuCppok0Dvhxt2rl56YiWwQO8+tmwlXPRKbIW1N/3aGtvLPW45/i9RdWcebHiTlAQlulwhsCYSS+P2vUDbRrcnRXbyusvpV4ntWnQOXk2KCsKJjhNTYRBnefh0daRqCbk1wprj8w== ARC-Authentication-Results: i=1; lists.ozlabs.org; dmarc=pass (p=none dis=none) header.from=redhat.com; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=J1bWFqfn; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=BcNPvv8f; dkim-atps=neutral; spf=pass (client-ip=170.10.133.124; helo=us-smtp-delivery-124.mimecast.com; envelope-from=herton@redhat.com; receiver=lists.ozlabs.org) smtp.mailfrom=redhat.com Authentication-Results: lists.ozlabs.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: lists.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=J1bWFqfn; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=BcNPvv8f; dkim-atps=neutral Authentication-Results: lists.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=redhat.com (client-ip=170.10.133.124; helo=us-smtp-delivery-124.mimecast.com; envelope-from=herton@redhat.com; receiver=lists.ozlabs.org) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (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 lists.ozlabs.org (Postfix) with ESMTPS id 4XhtxP6j5yz2xt7 for ; Tue, 5 Nov 2024 01:23:34 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1730730209; 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=j4zN/ElscuerA//rJUqf/4KurcEwyLi6zSlvuwt93ss=; b=J1bWFqfnRMDdjuOOiYW8SD3ytXQEn3JfU2z0VM9NMaW9sGLgyh5aFgMnih0c05xBC7S6c3 GRUC6X0v0HJzcSHk3gFZiZl780lpVruMNDUxGZhZCkp1XYTqdqigSOcXdw7sIDBaZ2Vomm lFYsScw0cHEuae9Q0/85bVjdXL/sDmA= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1730730210; 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=j4zN/ElscuerA//rJUqf/4KurcEwyLi6zSlvuwt93ss=; b=BcNPvv8fWPLcVUF6+qGNLiOisGXwUmlnXgQhxRvIOTzl1PgE6x7yVQrsdfaZAHI7iSJIg1 OfCYsShXWOWz8MWtZhSjM0J6a2wH32jmWqFzjLmYsX6fG2u7cbKgbBghgQTrQlEKRUCDnh L8l3F+MZa47mznP5i0BC/wCUWzMgzKI= Received: from mx-prod-mc-02.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-80-t7I83Uq_PpO99z-09uYBSA-1; Mon, 04 Nov 2024 09:23:24 -0500 X-MC-Unique: t7I83Uq_PpO99z-09uYBSA-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-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id A30011955DCD; Mon, 4 Nov 2024 14:23:21 +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 04B571956089; Mon, 4 Nov 2024 14:23:19 +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, herton@redhat.com Subject: [PATCH v2] lazy tlb: fix hotplug exit race with MMU_LAZY_TLB_SHOOTDOWN Date: Mon, 4 Nov 2024 11:23:18 -0300 Message-ID: <20241104142318.3295663-1-herton@redhat.com> X-Mailing-List: linuxppc-dev@lists.ozlabs.org List-Id: List-Help: List-Owner: List-Post: List-Archive: , List-Subscribe: , , List-Unsubscribe: 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-Spam-Status: No, score=-0.3 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_PASS autolearn=disabled version=4.0.0 X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on lists.ozlabs.org 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 | 22 +++++++++++++++------- 3 files changed, 21 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(). v2: fix warning reported by kernel test robot https://lore.kernel.org/oe-kbuild-all/202411022220.0u2CXCAM-lkp@intel.com/ - sched_force_init_mm is only used under CONFIG_HOTPLUG_CPU at sched_cpu_wait_empty, so we don't need to define it for !CONFIG_HOTPLUG_CPU 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..7d8f47a8f000 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 */ @@ -8240,6 +8247,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; }