From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=216.40.44.15 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796325726; b=I3eSDlzNgn697q3TGF1uAueAJ9IYCbpuVRlw+uZFh8+HBhjyF74/my7gJMNjkOExskv4 PS9TxeyOYso8bKUO3cm80ERRDBGbAukPRRb2ZWBoBiB8quvk+ylf7nzBYzs29Iw1SoaRD 75srZj2TiDF8IIfk3+McOS05zZI9rl2XKOup5vuouhkYpuAcfoZ6Utl4jJ3aHq5tXsWSD jDX1x/MdJt92vCHwKrtiKlxTvgDiAcLqYpLMFjj6YitEl9aJaJz0HJsXIScWes8vHsSvH TonZA++Wg+ZimF7bMHBZ4DPnQoEe74enNssiYd6l9Yg6xMUEcuFKpGMulopRGcmLidg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796325726; h=DMARC-Filter:Received:Received:Date:From:To:Cc:Subject:Message-ID: X-Mailer:MIME-Version:Content-Type:Content-Transfer-Encoding: X-Stat-Signature:X-Rspamd-Server:X-Rspamd-Queue-Id:X-Spam-Status: X-Session-Marker:X-Session-ID:DKIM-Signature:X-HE-Tag:X-HE-Meta; bh=coSwBaV8PMHaI+jzOopaidKpPX0ms5L03dwSslEtrSs=; b=HFKTKYtnIEDK5jQtua42YELTaSiaJJpVX8KkUYRN46NIM4SwmOHnQQ9nP4LsQE3FhHZp z1Flbkltc23fE1I9kZzpwrJIfi4TVmXrpDP5okCmb8eBStA3Hx0JRUkHMxfqgv8EkCqdK EBTuRlCh0J4sJP+TzTnhAo2QDyfPHglLHSfSoHXboZIJpiHoWCnRcneVO5zcDsKaUAY0x hC+MnzVPSSIV51LDIJaOCxPrShryeC/lnPc5aH2F5LVUs2kC5ss2jsYb19ourhl3qJfDH AMEw0xXYOoLoPWsULJhBGu9FifGpWejGhaEg2bkB4uuAj44IByNTc8hUEO6YKUr/uLQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=goodmis.org; spf=pass smtp.mailfrom=goodmis.org; dkim=pass header.d=goodmis.org header.i=@goodmis.org header.a=rsa-sha256 header.s=dkim1 header.b="iYkc/Ozm"; arc=none smtp.remote-ip=216.40.44.15 Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DAA37606A696 for ; Thu, 3 Dec 2026 19:22:04 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org DAA37606A696 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=goodmis.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=goodmis.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=goodmis.org header.i=@goodmis.org header.a=rsa-sha256 header.s=dkim1 header.b=iYkc/Ozm Received: from omf14.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 8D40D80116; Thu, 3 Dec 2026 19:22:03 +0000 (UTC) Received: from [HIDDEN] (Authenticated sender: rostedt@goodmis.org) by omf14.hostedemail.com (Postfix) with ESMTPA id EFB6330; Thu, 3 Dec 2026 19:22:01 +0000 (UTC) Date: Thu, 3 Dec 2026 14:21:55 -0500 From: Steven Rostedt To: LKML Cc: Masami Hiramatsu , Mathieu Desnoyers , Wenwu Hou Subject: [for-next][PATCH] tracing/hwlat: Bind the sampler kthread instead of setting its affinity Message-ID: <20261203142155.44054da8@gandalf.local.home> X-Mailer: Claws Mail 3.20.0git84 (GTK+ 2.24.33; x86_64-pc-linux-gnu) X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Stat-Signature: ydtmfnczz9awtz4rgbem8tgdr5xgywha X-Rspamd-Server: rspamout04 X-Rspamd-Queue-Id: EFB6330 X-Session-Marker: 726F737465647440676F6F646D69732E6F7267 X-Session-ID: U2FsdGVkX1+xZ1xAcUH9SCjlt/iyW5CDzp1n1eOopss= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=goodmis.org; h=date:from:to:cc:subject:message-id:mime-version:content-type:content-transfer-encoding; s=dkim1; bh=coSwBaV8PMHaI+jzOopaidKpPX0ms5L03dwSslEtrSs=; b=iYkc/OzmWQlxP4Uz31S8Z0wtaG15ZqikWsUTP1l/B/2MYXek2j98lrsav8C2e7lBU7f6/DReFfNMyY+PTqPC4JVnmIxVgBn/p+beGbTnEDkt6msJbLvO0hMwthGp92OftNcC0Fv93sIgazES3sAaEFUaZp9b5nJwexydGlhRog0= X-HE-Tag: 1796325721-694844 X-HE-Meta: U2FsdGVkX1+9+5IH7A0IMrnztVP53NKAyNiqLtPpycF1INkd5QqqBbR/Cj89bzsqDrfppXmVJsZ0RbMKlbeaiaI/ahju+6swawsza0Miy76S0h+dJGBZiXmfc4xnCA9QIcwz0fWQyH8MmIS6A4g3b513fG0QZWd/VBgWcPZFF+AuBskT7Ip8NUYxYyaHr+v0xgy6QR6NRp58PkTJGoQ88gN/5nLToSQwneiz47zQyfw+MEI5nxewX4if9grMhEAx2et3KDDYUcu5qrCU1pYq6EswkvAO9SGmgEsJs/o1VfWfdsfZ9caD1q99XubGolFMEdOeEs0HAzR/GgvM1/uzA1fvs6r7bsl6LMNajSw6GwNPCpfJFTZlJYKuyW/TcFzSomxRWaBk9TOMxuiw1l5nJx9DOipq2tVe992+53XPGCYl+awzg85myBgI5VwlbcKMkyFply8UWX9PLOrVMBfn0yEJK0YJtq0KoGQFvSW+2NIu2TIll/a5/kJT3+CxjAI/ git://git.kernel.org/pub/scm/linux/kernel/git/trace/linux-trace.git latency/for-next Head SHA1: e6a66bfe739c91900a6bed249a88e02441bcc914 Wenwu Hou (1): tracing/hwlat: Bind the sampler kthread instead of setting its affinity ---- kernel/trace/trace_hwlat.c | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) --------------------------- commit e6a66bfe739c91900a6bed249a88e02441bcc914 Author: Wenwu Hou Date: Tue Dec 1 16:12:17 2026 +0800 tracing/hwlat: Bind the sampler kthread instead of setting its affinity start_single_kthread() sets the affinity of the kthread it just created before waking it up, but kthread_affine_node() overrides that mask on the first wake-up. See commit 60ba9c38b931 ("kthread: Comment on the purpose and placement of kthread_affine_node() call"). In round-robin mode, move_to_next_cpu() compares save_cpumask against current->cpus_ptr, sees the replaced mask, and switches the tracer to MODE_NONE on its very first iteration: hwlat_detector: cpumask changed while in round-robin mode, switching to mode none So the tracer no longer migrates across tracing_cpumask. More importantly, tracing_cpumask is not honoured at all, and with isolcpus=/nohz_full= the thread cannot run on the isolated CPUs it is meant to sample. Use kthread_bind_mask() to preserve the requested affinity across the first wake-up. This also sets PF_NO_SETAFFINITY, preventing userspace from changing the sampler's CPU affinity. The mask is now computed and validated before kthread_create(), which also avoids creating a thread when tracing_cpumask has no online CPU in common. Tested on a 4-vCPU KVM guest, bit 0 of the mask is CPU 0: mode mask before after none 0b0101 Cpus_allowed_list: 0-3 0,2 round-robin 0b1111 mode -> none, no migrate migrates 1 2 3 0 round-robin 0b0101 mask 0-3, no migrate migrates 0 2 per-cpu 0b0110 2 threads, cpus 1,2 2 threads, cpus 1,2 Fixes: d1a89197589c4 ("kthread: Default affine kthread to its preferred NUMA node") Link: https://patch.msgid.link/20261201081217.3881097-1-hwenwur@gmail.com Signed-off-by: Wenwu Hou Signed-off-by: Steven Rostedt diff --git a/kernel/trace/trace_hwlat.c b/kernel/trace/trace_hwlat.c index 5be9031f1ffe..fbd35f7bfec1 100644 --- a/kernel/trace/trace_hwlat.c +++ b/kernel/trace/trace_hwlat.c @@ -314,20 +314,12 @@ static void move_to_next_cpu(void) struct trace_array *tr = hwlat_trace; int next_cpu; - /* - * If for some reason the user modifies the CPU affinity - * of this thread, then stop migrating for the duration - * of the current test. - */ - if (!cpumask_equal(current_mask, current->cpus_ptr)) - goto change_mode; - cpus_read_lock(); cpumask_and(current_mask, cpu_online_mask, tr->tracing_cpumask); next_cpu = cpumask_next_wrap(raw_smp_processor_id(), current_mask); cpus_read_unlock(); - if (next_cpu >= nr_cpu_ids) /* Shouldn't happen! */ + if (cpumask_empty(current_mask)) /* No online CPU in tracing_cpumask */ goto change_mode; cpumask_clear(current_mask); @@ -338,7 +330,7 @@ static void move_to_next_cpu(void) change_mode: hwlat_data.thread_mode = MODE_NONE; - pr_info(BANNER "cpumask changed while in round-robin mode, switching to mode none\n"); + pr_info(BANNER "No online CPU in tracing_cpumask, switching to mode none\n"); } /* @@ -423,15 +415,12 @@ static int start_single_kthread(struct trace_array *tr) if (kdata->kthread) goto out_put_cpus; - kthread = kthread_create(kthread_fn, NULL, "hwlatd"); - if (IS_ERR(kthread)) { - pr_err(BANNER "could not start sampling thread\n"); - cpus_read_unlock(); - return -ENOMEM; - } - /* Just pick the first CPU on first iteration */ cpumask_and(current_mask, cpu_online_mask, tr->tracing_cpumask); + if (cpumask_empty(current_mask)) { + cpus_read_unlock(); + return -EINVAL; + } if (hwlat_data.thread_mode == MODE_ROUND_ROBIN) { next_cpu = cpumask_first(current_mask); @@ -440,7 +429,14 @@ static int start_single_kthread(struct trace_array *tr) } - set_cpus_allowed_ptr(kthread, current_mask); + kthread = kthread_create(kthread_fn, NULL, "hwlatd"); + if (IS_ERR(kthread)) { + pr_err(BANNER "could not start sampling thread\n"); + cpus_read_unlock(); + return -ENOMEM; + } + + kthread_bind_mask(kthread, current_mask); kdata->kthread = kthread; wake_up_process(kthread);