From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.216.41 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796112772; b=a+gLjJGEPMggxq9cNGqyL1m3Sq99Lnp1K4T4LuFpb5a4jNJ+oM5xKqzM+sYBVYDtnvFn 660hXlvN1RxXVkm2sSvQj+BGBLrga6su9npfGeLURC4+bF9MVVxyZ4+Rg/NSscemPhVjA 37VLl6ZrBl+s17unEsP0+S4Rjz56iONMTFsGCyeSVvWVI474y78Tcm3VO1IOzZdZnS/mT PLMEyVlbiI/GPFJYbErAhyOJxuDp2lND2Gr65rlgB8ce1h27CVpvAYUhB/1NfLqZT+ZVz 8HK2HuHogEmtCons+SDz2aCSvm92c5ACcqiG7Kmo9Mthbr9thtWLyyBkahRvFAfWr6g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796112772; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:MIME-Version: Content-Transfer-Encoding; bh=hgQ7qD2wn6TQv8uyMdWIQKBMfnc7ZojPa3T+7GBth3w=; b=sArrF5R55tHxTAy15kh1gms1oGc0sE9MSUZfRBMkaWLjfa7tYIaSWYVAoKElZCi/T04x IbA2fhm30j4EWWnIS3PeKJsRtggyq/I6IPMsxcPVD04Tzp+qB09QqlVvzxW2Y3J7mf5Ow J7BgtMEeUmZZ0bNeI3Xagk+TfAj/I/TSErznSZfdXh4KcsGOqKx5nB/PNBK+PAKieFNzf Djik4xamCS0cvtdvEpILyr6TZeVzhz+rWipbXCLqnLY/kEjnZtUReqYu+Z8vwwt5getFY FMpk16e9tpszgvhokadDfx5humHpOpPBvcgHhG/4IsVg02EFmNnwdF2UYW7c/lipcwQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=SQb51aUb; arc=none smtp.remote-ip=209.85.216.41 Received: from mail-pj1-f41.google.com (mail-pj1-f41.google.com [209.85.216.41]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8154E606A32A for ; Tue, 1 Dec 2026 08:12:52 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 8154E606A32A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=SQb51aUb Received: by mail-pj1-f41.google.com with SMTP id 98e67ed59e1d1-3b403511a3eso1814029a91.1 for ; Tue, 01 Dec 2026 00:12:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796112771; x=1796717571; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=hgQ7qD2wn6TQv8uyMdWIQKBMfnc7ZojPa3T+7GBth3w=; b=SQb51aUbkRv8NlnnGQUuud5m9hOA4gzd+amQbD+YkUnSZq+BG3f9shc21h80f7sRWi 4ZaHY7Bur+PdgfIjs9S+qIFj4naf2rSRFdkqrG1n+u1WRyRQJ1SwlwvY39V6R1N8jaF1 4A56EFCIKvOJkvSACpow5kc5mZSDxNPXfpcJDrUi6ZDHlGFkWLymnEd5h8ooz87XV+Zv oRT62y+5i3U3zFy6vNbGERcJDQzn9K7shxQEP+v2BxI7kdEg9A1TcyFTtBbEX7NoWJlt LHa6lG08VD+rOlPc+uXaATeniZAPGCy1cVbtUx6evl97XoUlUw84UrsLCvHtYdy4IGGK ugRA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796112771; x=1796717571; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=hgQ7qD2wn6TQv8uyMdWIQKBMfnc7ZojPa3T+7GBth3w=; b=XcKEO1/CdNMelqqceIG50eQ/JEmUD/wFnCmVVJcp+wtPIZnXjLsOVgmz5httF0anMU V+sW2hWsAVTuvp4FTQ880bl0iTTjMDr5h0djDkoRQw6lRucfcGYg9ULyA+SsFD9CKclD ruX2mZYnwSh3zha5+SdFtKi2xCXLqZNEOlgmzv8/bOFO5MQ/Hx+Aky9EHfgb75lG8Wiy TSUdzjw9Nv+qCRpE0iWoeZc7q8tL0HzndGeXocr9fuVYEVIUxC2rdk3U5Ig/J6RHHTof jMskgucL4Nn6nvEymnR9b6wdixufvVTpRuysvJDZb4vod6ipAL+/xgdQuljEuyIVnBsh E9AQ== X-Forwarded-Encrypted: i=1; AL+hErGSyICnFLjU5Bet5s0yiRXFIjwDEXedRPqR/dD1WK5r0+fqyiwOUH/paRuCO49FNfHVWp/WjJ1yXKs0a0/QBf8gBLk=@vger.kernel.org X-Gm-Message-State: APuU4Z+UZxIB6vaZ8O/68oKiSO9EDzINsGYjmg/rHWRGLH0m7n/lqQYr EsRUAfgh2vmIE5Ij8bi79Mlmglvj/x4wlThKgWuliezv4a0qPtA+LoEQHjyEzoidpwI= X-Gm-Gg: AZFdTZcCK326P2ysQ+uCM5Hq4c3XFT+cDXoWyzhOtmMMpPY/pQG3bSrpj9Z0G1zItfX 6Tw4P0B90QxTQRUQxMvvGaQSCXZ1NWhEcwYCMIyvi4p+D9Rel3bLG6kIHQByX1b38RnpaQnj7TD K05tu9AlnaE2FzjOJobmLef5l+OMseRgqhIB0XTNeJ+FpTz6DDqITCoZCKSMPCh/dIjusnGr9f0 O7JXO3pFzHSrZ6UNi2Yjfqfp/zlgqfkmJClFzw0hQEGjj9jJnXH8mT9+gmS7LSjcYPGnA8mmsKG ks5J0C6Aw6TuTOIx4l6lVLa8lZkk01lY38oYje7W2kYsjrMc/RPOMAEv391M4WNkovEefkKGt5I SAKDnvcCql2QHiI0UnuOZTzcx0J0u3cyB1dsaeOf+G4hLSggoo5O/B3JVv5/kFECK8WwiHYu9SZ fLt30pDv72zQCXdiZHw1HgV7s06D5EM1JhAouWa5YzCqt48HqyKMh0sPombfebCyRCjtmsqNVUq ppMxPMU131llp39WY8dnW+ciy/3AMIQi1BpKlklg4r9NpuwRaK+XXpg2JzeSVb0uDiEx2D1Ow7e W7JEBnkcQbccStaFMTaG5Axo1K05Gs52bQod/H30e30vQIt88lQo4jU= X-Received: by 2002:a17:90a:e705:b0:3ac:9c37:9422 with SMTP id 98e67ed59e1d1-3b41c7e4cf5mr54019757a91.2.1796112771493; Tue, 01 Dec 2026 00:12:51 -0800 (PST) Received: from mi-ThinkCentre-M760t.mioffice.cn ([43.224.245.234]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3b41adcafe0sm59647290a91.16.2026.12.01.00.12.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Dec 2026 00:12:50 -0800 (PST) From: Wenwu Hou To: rostedt@goodmis.org Cc: mhiramat@kernel.org, mathieu.desnoyers@efficios.com, frederic@kernel.org, linux-trace-kernel@vger.kernel.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org, Wenwu Hou Subject: [PATCH v3] tracing/hwlat: Bind the sampler kthread instead of setting its affinity Date: Tue, 1 Dec 2026 16:12:17 +0800 Message-ID: <20261201081217.3881097-1-hwenwur@gmail.com> X-Mailer: git-send-email 2.43.0 X-Mailing-List: linux-trace-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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") Cc: stable@vger.kernel.org Signed-off-by: Wenwu Hou --- Changes in v3 (relative to v1): - Clarify that binding prevents userspace affinity changes, as discussed in v2. - Remove the obsolete affinity check and check for an empty mask. v1: https://lore.kernel.org/linux-trace-kernel/20261111020755.1422284-1-hwenwur@gmail.com/ v2: https://lore.kernel.org/linux-trace-kernel/20261125084600.2306377-1-hwenwur@gmail.com/ kernel/trace/trace_hwlat.c | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/kernel/trace/trace_hwlat.c b/kernel/trace/trace_hwlat.c index 5be9031f1ffe3..fbd35f7bfec1a 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); -- 2.43.0