All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] lib/vsprintf: add %pT[C012] format specifier
@ 2013-12-25 12:37 Tetsuo Handa
  2013-12-26  8:43 ` Joe Perches
  2013-12-27 23:02 ` Andrew Morton
  0 siblings, 2 replies; 45+ messages in thread
From: Tetsuo Handa @ 2013-12-25 12:37 UTC (permalink / raw)
  To: akpm, jkosina, joe, viro, davem; +Cc: linux-kernel

>From 545dae06c6690a0c937e082ed984f828a2ea7aa2 Mon Sep 17 00:00:00 2001
From: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
Date: Wed, 25 Dec 2013 18:16:17 +0900
Subject: [PATCH] lib/vsprintf: add %pT[C012] format specifier

Since task_struct->comm can be modified by other threads while the current
thread is reading it, it is recommended to use get_task_comm() for reading it.

However, since get_task_comm() holds task_struct->alloc_lock spinlock,
some users cannot use get_task_comm(). Also, a lot of users are directly
reading from task_struct->comm even if they can use get_task_comm().
Such users might obtain inconsistent comm name.

This patch introduces %pTC format specifier for reading task_struct->comm
and %pT0 %pT1 %pT2 format specifiers for reading task_struct->comm and
task_struct->pid.

Currently %pT does not hold task_struct->alloc_lock spinlock. This is because
I'm expecting that we will change to update task_struct->comm using RCU.

By using RCU, the comm name read from task_struct->comm will be guaranteed to
be consistent. But before modifying set_task_comm() to use RCU, we need to kill
direct ->comm users who do not use get_task_comm().

Although there might be arguments that whether to modify set_task_comm() to
use RCU, this patch will anyway serve as a cleanup.

Some examples for converting direct ->comm users are shown below.

  pr_info("comm=%s\n", p->comm);                => pr_info("comm=%pTC\n", p);
  pr_info("%s[%u]\n", p->comm, p->pid);         => pr_info("%pT0\n", p);
  pr_info("%s[%u]\n", p->comm, task_pid_nr(p)); => pr_info("%pT0\n", p);
  pr_info("%s/%u\n", p->comm, p->pid);          => pr_info("%pT1\n", p);
  pr_info("%s,%u\n", p->comm, p->pid);          => pr_info("%pT2\n", p);

Since many debug printings use p == current, you can pass NULL instead of p
if p == current. That is, you can simplify like examples shown below.

  pr_info("comm=%s\n", current->comm); => pr_info("comm=%pTC\n", NULL);
  pr_info("(%s/%u)", current->comm, current->pid); => pr_info("(%pT1)", NULL);

Signed-off-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
---
 Documentation/printk-formats.txt |   12 ++++++++++
 lib/vsprintf.c                   |   44 +++++++++++++++++++++++++++++++++++++-
 2 files changed, 55 insertions(+), 1 deletions(-)

diff --git a/Documentation/printk-formats.txt b/Documentation/printk-formats.txt
index 445ad74..c950c04 100644
--- a/Documentation/printk-formats.txt
+++ b/Documentation/printk-formats.txt
@@ -177,6 +177,18 @@ dentry names:
 	equivalent of %s dentry->d_name.name we used to use, %pd<n> prints
 	n last components.  %pD does the same thing for struct file.
 
+task_struct comm name:
+
+        format:  meaning:       example:
+        %pTC     commname       init
+        %pT0     commname[pid]  init[1]
+        %pT1     commname/pid   init/1
+        %pT2     commname,pid   init,1
+
+        For printing task_struct->comm and optionally task_struct->pid.
+        Currently task_struct->alloc_lock is not held; might be replaced
+        by RCU in the future.
+
 struct va_format:
 
 	%pV
diff --git a/lib/vsprintf.c b/lib/vsprintf.c
index 10909c5..4968f57 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -1155,6 +1155,38 @@ char *netdev_feature_string(char *buf, char *end, const u8 *addr,
 	return number(buf, end, *(const netdev_features_t *)addr, spec);
 }
 
+static noinline_for_stack
+char *comm_name(char *buf, char *end, struct task_struct *tsk,
+		struct printf_spec spec, const char *fmt)
+{
+	char name[TASK_COMM_LEN + 20] = { };
+	const char c = *fmt;
+
+	/* Caller can pass NULL instead of current. */
+	if (!tsk)
+		tsk = current;
+	/* Not using get_task_comm() in case I'm in IRQ context. */
+	strncpy(name, tsk->comm, TASK_COMM_LEN - 1);
+	/* Optionally print pid value. */
+	if (c != 'C') {
+		struct printf_spec ps = {
+			.type = FORMAT_TYPE_UINT,
+			.base = 10,
+		};
+		char *p = name + strlen(name);
+		if (c == '0')
+			*p++ = '['; /* comm[pid] */
+		else if (c == '1')
+			*p++ = '/'; /* comm/pid  */
+		else
+			*p++ = ','; /* comm,pid */
+		p = number(p, name + sizeof(name) - 2, task_pid_nr(tsk), ps);
+		if (c == '0' && p < name + sizeof(name) - 2)
+			*p++ = ']';
+	}
+	return string(buf, end, name, spec);
+}
+
 int kptr_restrict __read_mostly;
 
 /*
@@ -1221,6 +1253,7 @@ int kptr_restrict __read_mostly;
  * - 'a' For a phys_addr_t type and its derivative types (passed by reference)
  * - 'd[234]' For a dentry name (optionally 2-4 last components)
  * - 'D[234]' Same as 'd' but for a struct file
+ * - 'T[C012]' task_struct->comm and optionally task_struct->pid
  *
  * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
  * function pointers are really function descriptors, which contain a
@@ -1232,7 +1265,7 @@ char *pointer(const char *fmt, char *buf, char *end, void *ptr,
 {
 	int default_width = 2 * sizeof(void *) + (spec.flags & SPECIAL ? 2 : 0);
 
-	if (!ptr && *fmt != 'K') {
+	if (!ptr && *fmt != 'K' && *fmt != 'T') {
 		/*
 		 * Print (null) with the same width as a pointer so it makes
 		 * tabular output look nice.
@@ -1364,6 +1397,15 @@ char *pointer(const char *fmt, char *buf, char *end, void *ptr,
 		return dentry_name(buf, end,
 				   ((const struct file *)ptr)->f_path.dentry,
 				   spec, fmt);
+	case 'T':
+		switch (fmt[1]) {
+		case 'C':
+		case '0':
+		case '1':
+		case '2':
+			return comm_name(buf, end, ptr, spec, fmt + 1);
+		}
+		break;
 	}
 	spec.flags |= SMALL;
 	if (spec.field_width == -1) {
-- 
1.7.1

^ permalink raw reply related	[flat|nested] 45+ messages in thread

end of thread, other threads:[~2014-01-10 13:16 UTC | newest]

Thread overview: 45+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-12-25 12:37 [PATCH] lib/vsprintf: add %pT[C012] format specifier Tetsuo Handa
2013-12-26  8:43 ` Joe Perches
2013-12-27 23:02 ` Andrew Morton
2013-12-28  3:43   ` Tetsuo Handa
2013-12-28 18:57     ` Geert Uytterhoeven
2013-12-28 19:25       ` Andrew Morton
2013-12-28 19:25         ` Geert Uytterhoeven
2013-12-28 19:53           ` Joe Perches
2013-12-28 20:08             ` Andrew Morton
2013-12-28 20:24               ` Joe Perches
2013-12-29  0:32                 ` Tetsuo Handa
2013-12-29  2:07                   ` Joe Perches
2013-12-29 12:13                     ` Tetsuo Handa
2013-12-30 16:55                       ` Joe Perches
2013-12-31  6:53                         ` Tetsuo Handa
2013-12-31 16:24                           ` Joe Perches
2014-01-01  5:34                             ` Tetsuo Handa
2014-01-01  5:49                               ` Joe Perches
2014-01-01 10:02                                 ` Tetsuo Handa
2014-01-02  1:33                                   ` Joe Perches
2014-01-02  4:49                                     ` Tetsuo Handa
2014-01-03 17:08                                       ` Kees Cook
2014-01-03 17:39                                         ` Joe Perches
2014-01-03 17:49                                           ` Kees Cook
2014-01-04  2:26                                             ` Tetsuo Handa
2014-01-05  3:15                                               ` Tetsuo Handa
2014-01-05 18:09                                                 ` Joe Perches
2014-01-06 14:00                                                   ` Tetsuo Handa
2014-01-06 17:34                                                     ` Joe Perches
2014-01-06 21:41                                                       ` Tetsuo Handa
2014-01-06 22:25                                                         ` Joe Perches
2014-01-07  0:16                                                           ` Pavel Machek
2014-01-07  1:03                                                             ` Joe Perches
2014-01-07  8:37                                                               ` Pavel Machek
2014-01-07 17:34                                                                 ` Joe Perches
2014-01-07 17:56                                                                   ` Pavel Machek
2014-01-07 18:28                                                                     ` Geert Uytterhoeven
2014-01-08 14:19                                                                     ` Tetsuo Handa
2014-01-08 14:46                                                                       ` Pavel Machek
2014-01-06 21:34                                                     ` Pavel Machek
2014-01-05 22:27                                                 ` Pavel Machek
2014-01-06 14:02                                                   ` Tetsuo Handa
2014-01-10 13:15                                                     ` Tetsuo Handa
2014-01-02  1:38                                   ` Joe Perches
2014-01-02 11:51               ` Pavel Machek

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.