From nobody Mon Feb 9 17:34:48 2026 Received: from mail-qk1-f182.google.com (mail-qk1-f182.google.com [209.85.222.182]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 16F4844B686 for ; Tue, 20 Jan 2026 18:24:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768933487; cv=none; b=T+Azj/FmakWQUWRnQAw7iFzI8B2tdHpj1FLnwpO0miodXjxc32carGkIt519sojJqwEHLAATLA+vDpocd13FTDuhuGjFM46pBw1H+gCADZVzfVoj7ts37Epdut46mDdjKgXTOkCQewr0o7wqIF9SUziH+prrEuxc3qRlDgHrVMY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768933487; c=relaxed/simple; bh=uhMN5SDvwZKz4HUOAWprEN0emQee+wLZlPBbDgAYD+4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=lzBUodBddqll/f7di59DDhhAanlpr5OblZNVx3U5qSIZVpDP4Es+vbkGVLZEqzbEPRgONe/JN7+su1mm0VpgKoT/wpxkC9CoW9qQo6wqCY9boY2fOHUpR6i2wU13hVBFPnX3lnCIwLetKui0KUBhYomRIG3odnyAZswrEkudQ9A= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=debian.org; spf=pass smtp.mailfrom=gmail.com; arc=none smtp.client-ip=209.85.222.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=debian.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-qk1-f182.google.com with SMTP id af79cd13be357-8c532d8be8cso533572985a.2 for ; Tue, 20 Jan 2026 10:24:45 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1768933485; x=1769538285; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=QtgxYc8DWWj4JDK65TWyeuo8xjeUjjmBnOPjo+S2drw=; b=Ua/gWuAE0p94/PbaCRhVtcTQRy4aB6ATSiHQBIc0A3taC1Y7KdeuDVr4NZfYhCjz4h T7wJr+vLPs2qEgGElcloYU6ATrnt9YRcBIHk78TsRqATwHOxdQ4fMyDQf9eQCBorE4ol nkHtNmASndkCNHLHhje4GDYrKXNNs0rmS/W06EWFC7cehBWnAVwfrwQ2sYQRjQSaclwG EhSuR9h+CtHQuAd2t2udjJ5yh2TVRKMZSD6n6sPKefQwjPGHdaF0OaoBuHLACA5E0JO0 ejgYL1tAJpHDCvU1DQzkFagDDF2PceLj61P3ely1sHGUBRTOXx+gag17kFNMwjx80rjq bIqQ== X-Forwarded-Encrypted: i=1; AJvYcCUhmp8aguRzC9yviDflMBMuOKCoicW9cuumF88kk795F/smSaPVtZk+1nNxTZ0SdeiWXIJyep5CSsWErQA=@vger.kernel.org X-Gm-Message-State: AOJu0YwISJaM4LmzagkzAhtsHH8H2nnhTYtwlrtaopa8wTQDqrBwEKCp 97+yLMzXI9VwTCSGMBtwfgcFfOAhTquHUtXdKkJ+bRZiuKNynhbhmckmqFDpjA== X-Gm-Gg: AY/fxX7GzzVRYQwdRDVSH8PD2Gy7SQk3HPokCivd1IBCdNSewd3dnR82+qlhdogrVL5 sVdZjSWaOqn97AuFS20Dx+i7uB3qMhfsHNYGKPxevfhVjE4xfs6rMRAfRETBWK8Zyx8cFXWTvOu 4zxp24tbAnnVjyTgb2YXWukAIZHi2ZifyYUgsu/CW+huvtM2ERxrq/mZYYj3w/suGs8aUSmmgxh DpXWX6hxVmTKau5ELhW538as8+AFgiveSnr4XiCMBQMPJYtSAcjTGHiIqkwyXoOTejK2KiWjJnA e4KB+Su2UgAxqE5jq0VKYjm/hwVRyed6/0yKeClbdLl9JFxv21Hotsb6boHzx/vzmV+nwZnkYO7 L8sMMyk38rKp7xOAmvZ0vj9krhdw8OQ+lnGB4FyD2QqhUb6kK/XyvfRwMN4+W29BdQeasjGM6GM 1f X-Received: by 2002:a05:6830:3744:b0:7c7:690c:8a80 with SMTP id 46e09a7af769-7cfded44fa9mr6579427a34.9.1768926245620; Tue, 20 Jan 2026 08:24:05 -0800 (PST) Received: from localhost ([2a03:2880:10ff:5::]) by smtp.gmail.com with ESMTPSA id 46e09a7af769-7cfdf28ef44sm8865220a34.18.2026.01.20.08.24.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Jan 2026 08:24:04 -0800 (PST) From: Breno Leitao Date: Tue, 20 Jan 2026 08:23:47 -0800 Subject: [PATCH net-next v2 1/5] printk: Add execution context (task name/CPU) to printk_info Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260120-nbcon-v2-1-b61f960587a8@debian.org> References: <20260120-nbcon-v2-0-b61f960587a8@debian.org> In-Reply-To: <20260120-nbcon-v2-0-b61f960587a8@debian.org> To: Breno Leitao , Andrew Lunn , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , pmladek@suse.com, john.ogness@linutronix.de Cc: Greg Kroah-Hartman , Steven Rostedt , Sergey Senozhatsky , Andrew Morton , netdev@vger.kernel.org, linux-kernel@vger.kernel.org, asantostc@gmail.com, efault@gmx.de, gustavold@gmail.com, calvin@wbinvd.org, jv@jvosburgh.net, mpdesouza@suse.com, kernel-team@meta.com X-Mailer: b4 0.15-dev-47773 X-Developer-Signature: v=1; a=openpgp-sha256; l=9280; i=leitao@debian.org; h=from:subject:message-id; bh=uhMN5SDvwZKz4HUOAWprEN0emQee+wLZlPBbDgAYD+4=; b=owEBbQKS/ZANAwAIATWjk5/8eHdtAcsmYgBpb6wiwrhHlnYabC1yTvrM1KVwgr+iv/GVWDHYM sd7IDRnDGCJAjMEAAEIAB0WIQSshTmm6PRnAspKQ5s1o5Of/Hh3bQUCaW+sIgAKCRA1o5Of/Hh3 bbyBD/0VCaiJBo5QXkxWDoJGYYoqA910sbr7HZqiI0UfSg8QKFYFFmWJOAucqFFdh+xJzeVkKbK 1sCFQDhOPTntTHNYOcY2Dk9yJEJRWjGs6gVXddm+m9D0rUdlcTz5CKatZrq2BPx0U9nlnqBoVVN lEnJO+XRdgcBxPl8hRTyU8ww0FVQqqIkSusd1dkr9YCfYmPpnELfYezeUJj0n9dFmCB47Z8m1Y7 nRLgbcsfZEJ3kx1xYU3XT7bUBB+P80rxeCMU8qtDWpYlKCg2SthkWryjO2jaYUSnlpon5dABfAf z9strlni9WgTzhB0pQm2+WIcWproMAGljxyMeVJxyTy7qs9SQ8aW07bIb5r4WPt+Pzlvtv/JaDl OgK9Z/oELQ2KFc6lddXfQCX/incQcWnP8HYGcPEid+7F8oRfNH8AxAwd4dkak4BgoGSUHwVj9Cc CVoTjtuHLryfGIGfcz4OTrnCpER6uz7L3o832BPTnM0M3JoWK/lkY8aw8JZ4PqI37kBhFTiE9wy c4xyTHhRAFLtZ8cmb1TPcXXjKsDlalT+gb0SkBA4iSjG6vyqoiwlKM3w8VO1bbEzLiZ5JlWa3MD LKaNBZWj3HGNq0sRvvwMJN//9ypH1iUv2Bo0tmPq7AVOM5OHA80xco/JZkfpBjfQ4+u53OIl9OF zC6VouwQ5P9f5cQ== X-Developer-Key: i=leitao@debian.org; a=openpgp; fpr=AC8539A6E8F46702CA4A439B35A3939FFC78776D Extend struct printk_info to include the task name, pid, and CPU number where printk messages originate. This information is captured at vprintk_store() time and propagated through printk_message to nbcon_write_context, making it available to nbcon console drivers. This is useful for consoles like netconsole that want to include execution context in their output, allowing correlation of messages with specific tasks and CPUs regardless of where the console driver actually runs. The feature is controlled by CONFIG_PRINTK_EXECUTION_CTX, which is automatically selected by CONFIG_NETCONSOLE_DYNAMIC. When disabled, the helper functions compile to no-ops with no overhead. Suggested-by: John Ogness Signed-off-by: Breno Leitao Signed-off-by: Petr Mladek --- drivers/net/Kconfig | 1 + include/linux/console.h | 8 ++++++ kernel/printk/internal.h | 8 ++++++ kernel/printk/nbcon.c | 15 +++++++++++ kernel/printk/printk.c | 52 +++++++++++++++++++++++++++++++++++= +++- kernel/printk/printk_ringbuffer.h | 4 +++ lib/Kconfig.debug | 20 +++++++++++++++ 7 files changed, 107 insertions(+), 1 deletion(-) diff --git a/drivers/net/Kconfig b/drivers/net/Kconfig index ac12eaf11755d..12e47cb27ffa5 100644 --- a/drivers/net/Kconfig +++ b/drivers/net/Kconfig @@ -341,6 +341,7 @@ config NETCONSOLE_DYNAMIC bool "Dynamic reconfiguration of logging targets" depends on NETCONSOLE && SYSFS && CONFIGFS_FS && \ !(NETCONSOLE=3Dy && CONFIGFS_FS=3Dm) + select CONSOLE_HAS_EXECUTION_CTX help This option enables the ability to dynamically reconfigure target parameters (interface, IP addresses, port numbers, MAC addresses) diff --git a/include/linux/console.h b/include/linux/console.h index fc9f5c5c1b04c..cc5dc3bf58b60 100644 --- a/include/linux/console.h +++ b/include/linux/console.h @@ -298,12 +298,20 @@ struct nbcon_context { * @outbuf: Pointer to the text buffer for output * @len: Length to write * @unsafe_takeover: If a hostile takeover in an unsafe state has occurred + * @cpu: CPU on which the message was generated + * @pid: PID of the task that generated the message + * @comm: Name of the task that generated the message */ struct nbcon_write_context { struct nbcon_context __private ctxt; char *outbuf; unsigned int len; bool unsafe_takeover; +#ifdef CONFIG_PRINTK_EXECUTION_CTX + int cpu; + pid_t pid; + char comm[TASK_COMM_LEN]; +#endif }; =20 /** diff --git a/kernel/printk/internal.h b/kernel/printk/internal.h index 5f5f626f42794..5fdea56827564 100644 --- a/kernel/printk/internal.h +++ b/kernel/printk/internal.h @@ -281,12 +281,20 @@ struct printk_buffers { * nothing to output and this record should be skipped. * @seq: The sequence number of the record used for @pbufs->outbuf. * @dropped: The number of dropped records from reading @seq. + * @cpu: CPU on which the message was generated. + * @pid: PID of the task that generated the message + * @comm: Name of the task that generated the message. */ struct printk_message { struct printk_buffers *pbufs; unsigned int outbuf_len; u64 seq; unsigned long dropped; +#ifdef CONFIG_PRINTK_EXECUTION_CTX + int cpu; + pid_t pid; + char comm[TASK_COMM_LEN]; +#endif }; =20 bool printk_get_next_message(struct printk_message *pmsg, u64 seq, diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c index 3fa403f9831f0..c2b3c4d2146e3 100644 --- a/kernel/printk/nbcon.c +++ b/kernel/printk/nbcon.c @@ -946,6 +946,19 @@ void nbcon_reacquire_nobuf(struct nbcon_write_context = *wctxt) } EXPORT_SYMBOL_GPL(nbcon_reacquire_nobuf); =20 +#ifdef CONFIG_PRINTK_EXECUTION_CTX +static inline void wctxt_load_execution_ctx(struct nbcon_write_context *wc= txt, + struct printk_message *pmsg) +{ + wctxt->cpu =3D pmsg->cpu; + wctxt->pid =3D pmsg->pid; + memcpy(wctxt->comm, pmsg->comm, TASK_COMM_LEN); +} +#else +static inline void wctxt_load_execution_ctx(struct nbcon_write_context *wc= txt, + struct printk_message *pmsg) {} +#endif + /** * nbcon_emit_next_record - Emit a record in the acquired context * @wctxt: The write context that will be handed to the write function @@ -1048,6 +1061,8 @@ static bool nbcon_emit_next_record(struct nbcon_write= _context *wctxt, bool use_a /* Initialize the write context for driver callbacks. */ nbcon_write_context_set_buf(wctxt, &pmsg.pbufs->outbuf[0], pmsg.outbuf_le= n); =20 + wctxt_load_execution_ctx(wctxt, &pmsg); + if (use_atomic) con->write_atomic(con, wctxt); else diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 1d765ad242b82..8885e8e7753fe 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -2131,11 +2131,38 @@ static inline void printk_delay(int level) } } =20 +#define CALLER_ID_MASK 0x80000000 + static inline u32 printk_caller_id(void) { return in_task() ? task_pid_nr(current) : - 0x80000000 + smp_processor_id(); + CALLER_ID_MASK + smp_processor_id(); +} + +#ifdef CONFIG_PRINTK_EXECUTION_CTX +/* Store the opposite info than caller_id. */ +static inline u32 printk_caller_id2(void) +{ + return !in_task() ? task_pid_nr(current) : + CALLER_ID_MASK + smp_processor_id(); +} + +static inline pid_t printk_info_get_pid(const struct printk_info *info) +{ + u32 caller_id =3D info->caller_id; + u32 caller_id2 =3D info->caller_id2; + + return caller_id & CALLER_ID_MASK ? caller_id2 : caller_id; +} + +static inline int printk_info_get_cpu(const struct printk_info *info) +{ + u32 caller_id =3D info->caller_id; + u32 caller_id2 =3D info->caller_id2; + + return (caller_id & CALLER_ID_MASK ? caller_id : caller_id2) & ~CALLER_ID= _MASK; } +#endif =20 /** * printk_parse_prefix - Parse level and control flags. @@ -2213,6 +2240,27 @@ static u16 printk_sprint(char *text, u16 size, int f= acility, return text_len; } =20 +#ifdef CONFIG_PRINTK_EXECUTION_CTX +static inline void printk_store_execution_ctx(struct printk_info *info) +{ + info->caller_id2 =3D printk_caller_id2(); + get_task_comm(info->comm, current); +} + +static inline void pmsg_load_execution_ctx(struct printk_message *pmsg, + const struct printk_info *info) +{ + pmsg->cpu =3D printk_info_get_cpu(info); + pmsg->pid =3D printk_info_get_pid(info); + memcpy(pmsg->comm, info->comm, TASK_COMM_LEN); +} +#else +static inline void printk_store_execution_ctx(struct printk_info *info) {} + +static inline void pmsg_load_execution_ctx(struct printk_message *pmsg, + const struct printk_info *info) {} +#endif + __printf(4, 0) int vprintk_store(int facility, int level, const struct dev_printk_info *dev_info, @@ -2320,6 +2368,7 @@ int vprintk_store(int facility, int level, r.info->caller_id =3D caller_id; if (dev_info) memcpy(&r.info->dev_info, dev_info, sizeof(r.info->dev_info)); + printk_store_execution_ctx(r.info); =20 /* A message without a trailing newline can be continued. */ if (!(flags & LOG_NEWLINE)) @@ -3002,6 +3051,7 @@ bool printk_get_next_message(struct printk_message *p= msg, u64 seq, pmsg->seq =3D r.info->seq; pmsg->dropped =3D r.info->seq - seq; force_con =3D r.info->flags & LOG_FORCE_CON; + pmsg_load_execution_ctx(pmsg, r.info); =20 /* * Skip records that are not forced to be printed on consoles and that diff --git a/kernel/printk/printk_ringbuffer.h b/kernel/printk/printk_ringb= uffer.h index 4ef81349d9fbe..bf5b23a0bf54e 100644 --- a/kernel/printk/printk_ringbuffer.h +++ b/kernel/printk/printk_ringbuffer.h @@ -23,6 +23,10 @@ struct printk_info { u8 flags:5; /* internal record flags */ u8 level:3; /* syslog level */ u32 caller_id; /* thread id or processor id */ +#ifdef CONFIG_PRINTK_EXECUTION_CTX + u32 caller_id2; /* caller_id complement */ + char comm[TASK_COMM_LEN]; /* name of the task that generated the message = */ +#endif =20 struct dev_printk_info dev_info; }; diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug index ba36939fda79b..57f91ee10b8e6 100644 --- a/lib/Kconfig.debug +++ b/lib/Kconfig.debug @@ -35,6 +35,26 @@ config PRINTK_CALLER no option to enable/disable at the kernel command line parameter or sysfs interface. =20 +config CONSOLE_HAS_EXECUTION_CTX + bool + help + Selected by console drivers that support execution context + (task name/CPU) in their output. This enables PRINTK_EXECUTION_CTX + to provide the necessary infrastructure. + +config PRINTK_EXECUTION_CTX + bool "Include execution context (task/CPU) in printk messages" + depends on PRINTK && CONSOLE_HAS_EXECUTION_CTX + default CONSOLE_HAS_EXECUTION_CTX + help + This option extends struct printk_info to include extra execution + context in printk, such as task name and CPU number from where the + message originated. This is useful for correlating printk messages + with specific execution contexts. + + This is automatically enabled when a console driver that supports + execution context is selected. + config STACKTRACE_BUILD_ID bool "Show build ID information in stacktraces" depends on PRINTK --=20 2.47.3