Skip to contentSkip to conversation

v5 Improve task->comm locking situation

PATCH In review John Stultz 8 participants 17 messages May 17, 2011 – May 18, 2011

Subsystems (8)
lore
Share & tools
lore
#0 lore raw

cover v5 Improve task->comm locking situation

Ok. Here's v5. Hopefully this is getting close to something that can be queued for 2.6.40.

Since my commit 4614a696bd1c3a9af3a08f0e5874830a85b889d4, the current->comm value could be changed by other threads.

This changed the comm locking rules, which previously allowed for unlocked current->comm access, since only the thread itself could change its comm.

While this was brought up at the time, it was not considered problematic, as the comm writing was done in such a way that only null or incomplete comms could be read. However, recently folks have made it clear they want to see this issue resolved.

So fair enough, as I opened this can of worms, I should work to resolve it and this patchset is my initial attempt.

The proposed solution here is to introduce a new spinlock that exclusively protects the comm value. We use it to serialize access via get_task_comm() and set_task_comm(). Since some comm access is open-coded using the task lock, we preserve the task locking in set_task_comm for now. Once all comm access is converted to using get_task_comm, we can clean that up as well.

I've also introduced a printk %ptc accessor, which makes the conversion to locked access simpler (as most uses are for printks) as well as a checkpatch rule to try to catch any new current->comm users from being introduced.

New in this version: Added a spin_lock_init in copy_process as caught by Jiri. Also tweaked the regex as suggested by Joe.

Hopefully this will allow for a smooth transition, where we can slowly fix up the unlocked current->comm access bit by bit, reducing the race window with each patch, while not making the situation any worse then it was yesterday.

Thanks for the comments and feedback so far.
Any additional comments/feedback would still be appreciated.

thanks
-john

CC Joe Perches <[email protected]>
CC Michal Nazarewicz <[email protected]>
CC Andy Whitcroft <[email protected]>
CC Jiri Slaby <[email protected]>
CC KOSAKI Motohiro <[email protected]>
CC David Rientjes <[email protected]>
CC Dave Hansen <[email protected]>
CC Andrew Morton <[email protected]>

John Stultz (3):
comm: Introduce comm_lock spinlock to protect task->comm access
printk: Add %ptc to safely print a task's comm checkpatch.pl: Add check for task comm references

 fs/exec.c                 |   19 ++++++++++++++++---
 include/linux/init_task.h |    1 +
 include/linux/sched.h     |    5 ++---
 kernel/fork.c             |    1 +
 lib/vsprintf.c            |   24 ++++++++++++++++++++++++
 scripts/checkpatch.pl     |    7 +++++++
 6 files changed, 51 insertions(+), 6 deletions(-)
Signature · 8 lines
1.7.3.2.146.gca209

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#1 lore raw

PATCH 1/3 comm: Introduce comm_lock spinlock to protect task->comm access

The implicit rules for current->comm access being safe without locking are no longer true. Accessing current->comm without holding the task lock may result in null or incomplete strings (however, access won't run off the end of the string).

In order to properly fix this, I've introduced a comm_lock spinlock which will protect comm access and modified get_task_comm() and set_task_comm() to use it.

Since there are a number of cases where comm access is open-coded safely grabbing the task_lock(), we preserve the task locking in set_task_comm, so those users are also safe.

With this patch, users that access current->comm without a lock are still prone to null/incomplete comm strings, but it should be no worse then it is now.

The next step is to go through and convert all comm accesses to use get_task_comm(). This is substantial, but can be done bit by bit, reducing the race windows with each patch.

CC Joe Perches <[email protected]>
CC Michal Nazarewicz <[email protected]>
CC Andy Whitcroft <[email protected]>
CC Jiri Slaby <[email protected]>
CC KOSAKI Motohiro <[email protected]>
CC David Rientjes <[email protected]>
CC Dave Hansen <[email protected]>
CC Andrew Morton <[email protected]>
---
 fs/exec.c                 |   19 ++++++++++++++++---
 include/linux/init_task.h |    1 +
 include/linux/sched.h     |    5 ++---
 kernel/fork.c             |    1 +
 4 files changed, 20 insertions(+), 6 deletions(-)
fs/exec.c +16 -3
index 5e62d26..34fa611 100644
@@ -998,17 +998,28 @@ static void flush_old_files(struct files_struct * files)
 
 char *get_task_comm(char *buf, struct task_struct *tsk)
 {
-	/* buf must be at least sizeof(tsk->comm) in size */
-	task_lock(tsk);
+	unsigned long flags;
+
+	spin_lock_irqsave(&tsk->comm_lock, flags);
 	strncpy(buf, tsk->comm, sizeof(tsk->comm));
-	task_unlock(tsk);
+	spin_unlock_irqrestore(&tsk->comm_lock, flags);
 	return buf;
 }
 
 void set_task_comm(struct task_struct *tsk, char *buf)
 {
+	unsigned long flags;
+
+	/*
+	 * XXX - Even though comm is protected by comm_lock,
+	 * we take the task_lock here to serialize against
+	 * current users that directly access comm.
+	 * Once those users are removed, we can drop the
+	 * task locking & memsetting.
+	 */
 	task_lock(tsk);
 
+	spin_lock_irqsave(&tsk->comm_lock, flags);
 	/*
 	 * Threads may access current->comm without holding
 	 * the task lock, so write the string carefully.
@@ -1018,6 +1029,8 @@ void set_task_comm(struct task_struct *tsk, char *buf)
 	memset(tsk->comm, 0, TASK_COMM_LEN);
 	wmb();
 	strlcpy(tsk->comm, buf, sizeof(tsk->comm));
+	spin_unlock_irqrestore(&tsk->comm_lock, flags);
+
 	task_unlock(tsk);
 	perf_event_comm(tsk);
 }
include/linux/init_task.h +1 -0
index caa151f..b69d94b 100644
@@ -161,6 +161,7 @@ extern struct cred init_cred;
 	.group_leader	= &tsk,						\
 	RCU_INIT_POINTER(.real_cred, &init_cred),			\
 	RCU_INIT_POINTER(.cred, &init_cred),				\
+	.comm_lock	= __SPIN_LOCK_UNLOCKED(tsk.comm_lock),		\
 	.comm		= "swapper",					\
 	.thread		= INIT_THREAD,					\
 	.fs		= &init_fs,					\
include/linux/sched.h +2 -3
index 18d63ce..f8a7cdf 100644
@@ -1333,10 +1333,9 @@ struct task_struct {
 	const struct cred __rcu *cred;	/* effective (overridable) subjective task
 					 * credentials (COW) */
 	struct cred *replacement_session_keyring; /* for KEYCTL_SESSION_TO_PARENT */
-
+	spinlock_t comm_lock;		/* protect's comm */
 	char comm[TASK_COMM_LEN]; /* executable name excluding path
-				     - access with [gs]et_task_comm (which lock
-				       it with task_lock())
+				     - access with [gs]et_task_comm
 				     - initialized normally by setup_new_exec */
 /* file system info */
 	int link_count, total_link_count;
kernel/fork.c +1 -0
index e7548de..f53bf29 100644
@@ -1080,6 +1080,7 @@ static struct task_struct *copy_process(unsigned long clone_flags,
 	rcu_copy_process(p);
 	p->vfork_done = NULL;
 	spin_lock_init(&p->alloc_lock);
+	spin_lock_init(&p->comm_lock);
 
 	init_sigpending(&p->pending);
 
Signature · 8 lines
1.7.3.2.146.gca209

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#2 lore raw

Re: [PATCH 1/3] comm: Introduce comm_lock spinlock to protect task->comm access

Quoted 4 lines · John Stultz

The implicit rules for current->comm access being safe without locking are no longer true. Accessing current->comm without holding the task lock may result in null or incomplete strings (however, access won't run off the end of the string).

This is rather unfortunate - task->comm is used in a number of performance critical codepaths such as tracing.

Why does this matter so much? A NULL string is not a big deal.

Note, since task->comm is 16 bytes there's the CMPXCHG16B instruction on x86 which could be used to update it atomically, should atomicity really be desired.

Thanks,

Ingo

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#3 lore raw

Re: [PATCH 1/3] comm: Introduce comm_lock spinlock to protect task->comm access

Quoted 7 lines · Ingo Molnar
Quoted 4 lines · John Stultz

The implicit rules for current->comm access being safe without locking are no longer true. Accessing current->comm without holding the task lock may result in null or incomplete strings (however, access won't run off the end of the string).

Quoted 8 lines

This is rather unfortunate - task->comm is used in a number of performance critical codepaths such as tracing.

Why does this matter so much? A NULL string is not a big deal.

Note, since task->comm is 16 bytes there's the CMPXCHG16B instruction on x86 which could be used to update it atomically, should atomicity really be desired.

The changelog also fails to mention _WHY_ this is no longer true. Nor does it treat why making it true again isn't an option.

Who is changing another task's comm? That's just silly.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#4 lore raw

Re: [PATCH 1/3] comm: Introduce comm_lock spinlock to protect task->comm access

Quoted 3 lines · Peter Zijlstra

The changelog also fails to mention _WHY_ this is no longer true. Nor does it treat why making it true again isn't an option.

It's been true since:

	4614a696bd1c3a9af3a08f0e5874830a85b889d4
	Author: john stultz <[email protected]>
	Date:   Mon Dec 14 18:00:05 2009 -0800

	    procfs: allow threads to rename siblings via /proc/pid/tasks/tid/comm

Although at the time it appears that nobody was concerned about races so proper syncronization was never implemented. We always had the prctl(PR_SET_NAME) so the majority of comm reads, those to current, required no locking, but this commit changed that. The remainder of comm dereferences always required task_lock() and the helper get_task_comm() to read the string into a (usually stack-allocated) buffer.

Quoted 2 lines

Who is changing another task's comm? That's just silly.

I agree, and I suggested taking write privileges away from /proc/pid/comm, but others find that it is useful to be able to differentiate between threads in the same thread group without using the prctl() for debugging?

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#5 lore raw

Re: [PATCH 1/3] comm: Introduce comm_lock spinlock to protect task->comm access

In reply to Peter Zijlstra
Quoted 13 lines · Peter Zijlstra
Quoted 12 lines · Ingo Molnar
Quoted 4 lines · John Stultz

The implicit rules for current->comm access being safe without locking are no longer true. Accessing current->comm without holding the task lock may result in null or incomplete strings (however, access won't run off the end of the string).

This is rather unfortunate - task->comm is used in a number of performance critical codepaths such as tracing.

Why does this matter so much? A NULL string is not a big deal.

Quoted 8 lines
Quoted 3 lines

Note, since task->comm is 16 bytes there's the CMPXCHG16B instruction on x86 which could be used to update it atomically, should atomicity really be desired.

The changelog also fails to mention _WHY_ this is no longer true. Nor does it treat why making it true again isn't an option.

Who is changing another task's comm? That's just silly.

I'm not sure it's silly or not. But the fact is, comm override was introduced following patch. Personally I'd like to mark it to "depend on EXPERT". but John seems to dislike the idea.

commit 4614a696bd1c3a9af3a08f0e5874830a85b889d4

Author: john stultz <[email protected]>
Date:   Mon Dec 14 18:00:05 2009 -0800

procfs: allow threads to rename siblings via /proc/pid/tasks/tid/comm

#6 lore raw

Re: [PATCH 1/3] comm: Introduce comm_lock spinlock to protect task->comm access

In reply to Ingo Molnar
Quoted 11 lines · Ingo Molnar
Quoted 4 lines · John Stultz

The implicit rules for current->comm access being safe without locking are no longer true. Accessing current->comm without holding the task lock may result in null or incomplete strings (however, access won't run off the end of the string).

This is rather unfortunate - task->comm is used in a number of performance critical codepaths such as tracing.

Why does this matter so much? A NULL string is not a big deal.

I'll defer to KOSAKI Motohiro and David on this bit. :)

Quoted 3 lines

Note, since task->comm is 16 bytes there's the CMPXCHG16B instruction on x86 which could be used to update it atomically, should atomicity really be desired.

Could we use this where cmpxchg16b is available and fall back to locking if not? Or does that put too much of a penalty on arches that don't have cmpxchg16b support?

Alternatively, we can have locked accessors that are safe in the majority of slow-path warning printks, and provide unlocked accessors for cases where the performance is critical and the code can properly handle possibly incomplete comms.

thanks
-john

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#7 lore raw

Re: [PATCH 1/3] comm: Introduce comm_lock spinlock to protect task->comm access

Quoted 10 lines · John Stultz
Quoted 9 lines · Ingo Molnar
Quoted 4 lines · John Stultz

The implicit rules for current->comm access being safe without locking are no longer true. Accessing current->comm without holding the task lock may result in null or incomplete strings (however, access won't run off the end of the string).

This is rather unfortunate - task->comm is used in a number of performance critical codepaths such as tracing.

Right.

Quoted 3 lines
Quoted 1 line

Why does this matter so much? A NULL string is not a big deal.

I'll defer to KOSAKI Motohiro and David on this bit. :)

Heh, I did ask you current locking rule of task->comm after you introduced writable /proc/<pid>/comm.

Quoted 4 lines
Quoted 3 lines

Note, since task->comm is 16 bytes there's the CMPXCHG16B instruction on x86 which could be used to update it atomically, should atomicity really be desired.

Quoted 8 lines

Could we use this where cmpxchg16b is available and fall back to locking if not? Or does that put too much of a penalty on arches that don't have cmpxchg16b support?

Alternatively, we can have locked accessors that are safe in the majority of slow-path warning printks, and provide unlocked accessors for cases where the performance is critical and the code can properly handle possibly incomplete comms.

Probably, this is safer choice.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#8 lore raw

PATCH 2/3 printk: Add %ptc to safely print a task's comm

In reply to John Stultz

Accessing task->comm requires proper locking. However in the past access to current->comm could be done without locking. This is no longer the case, so all comm access needs to be done while holding the comm_lock.

In my attempt to clean up unprotected comm access, I've noticed most comm access is done for printk output. To simplify correct locking in these cases, I've introduced a new %ptc format, which will print the corresponding task's comm.

Example use:
printk("%ptc: unaligned epc - sending SIGBUS.\n", current);

CC Joe Perches <[email protected]>
CC Michal Nazarewicz <[email protected]>
CC Andy Whitcroft <[email protected]>
CC Jiri Slaby <[email protected]>
CC KOSAKI Motohiro <[email protected]>
CC David Rientjes <[email protected]>
CC Dave Hansen <[email protected]>
CC Andrew Morton <[email protected]>
---
 lib/vsprintf.c |   24 ++++++++++++++++++++++++
 1 files changed, 24 insertions(+), 0 deletions(-)
lib/vsprintf.c +24 -0
index bc0ac6b..b7a9953 100644
@@ -797,6 +797,23 @@ char *uuid_string(char *buf, char *end, const u8 *addr,
 	return string(buf, end, uuid, spec);
 }
 
+static noinline_for_stack
+char *task_comm_string(char *buf, char *end, void *addr,
+			 struct printf_spec spec, const char *fmt)
+{
+	struct task_struct *tsk = addr;
+	char *ret;
+	unsigned long flags;
+
+	spin_lock_irqsave(&tsk->comm_lock, flags);
+	ret = string(buf, end, tsk->comm, spec);
+	spin_unlock_irqrestore(&tsk->comm_lock, flags);
+
+	return ret;
+}
+
+
+
 int kptr_restrict = 1;
 
 /*
@@ -864,6 +881,12 @@ char *pointer(const char *fmt, char *buf, char *end, void *ptr,
 	}
 
 	switch (*fmt) {
+	case 't':
+		switch (fmt[1]) {
+		case 'c':
+			return task_comm_string(buf, end, ptr, spec, fmt);
+		}
+		break;
 	case 'F':
 	case 'f':
 		ptr = dereference_function_descriptor(ptr);
@@ -1151,6 +1174,7 @@ qualifier:
  *   http://tools.ietf.org/html/draft-ietf-6man-text-addr-representation-00
  * %pU[bBlL] print a UUID/GUID in big or little endian using lower or upper
  *   case.
+ * %ptc outputs the task's comm name
  * %n is ignored
  *
  * The return value is the number of characters which would
Signature · 8 lines
1.7.3.2.146.gca209

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#9 lore raw

Re: [PATCH 2/3] printk: Add %ptc to safely print a task's comm

Quoted 32 lines · John Stultz

Accessing task->comm requires proper locking. However in the past access to current->comm could be done without locking. This is no longer the case, so all comm access needs to be done while holding the comm_lock.

In my attempt to clean up unprotected comm access, I've noticed most comm access is done for printk output. To simplify correct locking in these cases, I've introduced a new %ptc format, which will print the corresponding task's comm.

Example use:
printk("%ptc: unaligned epc - sending SIGBUS.\n", current);

CC Joe Perches <[email protected]>
CC Michal Nazarewicz <[email protected]>
CC Andy Whitcroft <[email protected]>
CC Jiri Slaby <[email protected]>
CC KOSAKI Motohiro <[email protected]>
CC David Rientjes <[email protected]>
CC Dave Hansen <[email protected]>
CC Andrew Morton <[email protected]>
---
 lib/vsprintf.c |   24 ++++++++++++++++++++++++
 1 files changed, 24 insertions(+), 0 deletions(-)
lib/vsprintf.c +0 -0
index bc0ac6b..b7a9953 100644

lib/vsprintf.c char *uuid_string(char *buf, char *end, const u8 *addr, · line 800

Quoted 4 lines
lib/vsprintf.c +1 -0
@@ -797,6 +797,23 @@ char *uuid_string(char *buf, char *end, const u8 *addr,
 	return string(buf, end, uuid, spec);
 }
 
+static noinline_for_stack

I still fail to see why this should be slowed down by noinlining it. Care to explain?

With my setup, the code below inlined will use 32 bytes of stack. The same as %pK case. Uninlined it obviously eats "only" 8 bytes for IP.

lib/vsprintf.c char *uuid_string(char *buf, char *end, const u8 *addr, · line 813

Quoted 13 lines
lib/vsprintf.c +13 -0
+char *task_comm_string(char *buf, char *end, void *addr,
+			 struct printf_spec spec, const char *fmt)
+{
+	struct task_struct *tsk = addr;
+	char *ret;
+	unsigned long flags;
+
+	spin_lock_irqsave(&tsk->comm_lock, flags);
+	ret = string(buf, end, tsk->comm, spec);
+	spin_unlock_irqrestore(&tsk->comm_lock, flags);
+
+	return ret;
+}

thanks,

Signature · 8 lines
js

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
maintainer
#10 lore raw

Re: [PATCH 2/3] printk: Add %ptc to safely print a task's comm

Quoted 8 lines · Jiri Slaby
Quoted 5 lines · John Stultz

Accessing task->comm requires proper locking. However in the past access to current->comm could be done without locking. This is no longer the case, so all comm access needs to be done while holding the comm_lock.
+static noinline_for_stack

I still fail to see why this should be slowed down by noinlining it. Care to explain?

Any vsprintf is slow.

Quoted 2 lines

With my setup, the code below inlined will use 32 bytes of stack. The same as %pK case. Uninlined it obviously eats "only" 8 bytes for IP.

The idea is to avoid excess stack consumption for things like:

	struct va_format vaf;

	const char *fmt = "some format with %ptc";

	vaf.fmt = fmt;
	vaf.va = &va_list;

	printk("some format with %pV\n", &vaf);
Quoted 5 lines
Quoted 5 lines
lib/vsprintf.c +5 -0
+char *task_comm_string(char *buf, char *end, void *addr,
+			 struct printf_spec spec, const char *fmt)
+{
+	struct task_struct *tsk = addr;
+	char *ret;
Quoted 8 lines

lib/vsprintf.c char *uuid_string(char *buf, char *end, const u8 *addr, · line 813

Quoted 8 lines
lib/vsprintf.c +8 -0
+	unsigned long flags;
+
+	spin_lock_irqsave(&tsk->comm_lock, flags);
+	ret = string(buf, end, tsk->comm, spec);
+	spin_unlock_irqrestore(&tsk->comm_lock, flags);
+
+	return ret;
+}

I think it was more of a problem when "4k stacks" was the default than today, but I think it is still "good form".

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#11 lore raw

Re: [PATCH 2/3] printk: Add %ptc to safely print a task's comm

Quoted 17 lines · Joe Perches
Quoted 8 lines · Jiri Slaby
Quoted 5 lines · John Stultz

Accessing task->comm requires proper locking. However in the past access to current->comm could be done without locking. This is no longer the case, so all comm access needs to be done while holding the comm_lock.
+static noinline_for_stack

I still fail to see why this should be slowed down by noinlining it. Care to explain?

Any vsprintf is slow.

Quoted 2 lines

With my setup, the code below inlined will use 32 bytes of stack. The same as %pK case. Uninlined it obviously eats "only" 8 bytes for IP.

The idea is to avoid excess stack consumption for things like:

Quoted 8 lines
	struct va_format vaf;

	const char *fmt = "some format with %ptc";

	vaf.fmt = fmt;
	vaf.va = &va_list;

	printk("some format with %pV\n", &vaf);

There is no way how can noinline_for_stack for task_comm_string lower the stack usage here, right? Note that it adds no more requirements to the stack than there were before. Simply because there are no buffers on the stack in task_comm_string.

If nothing, it saves 100 bytes of .text.

thanks,

Signature · 8 lines
js

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
maintainer
#12 lore raw

Re: [PATCH 2/3] printk: Add %ptc to safely print a task's comm

Quoted 16 lines · Jiri Slaby
Quoted 7 lines · Joe Perches
Quoted 6 lines · Jiri Slaby
Quoted 5 lines · John Stultz

Accessing task->comm requires proper locking. However in the past access to current->comm could be done without locking. This is no longer the case, so all comm access needs to be done while holding the comm_lock.
+static noinline_for_stack

Quoted 8 lines
Quoted 2 lines

With my setup, the code below inlined will use 32 bytes of stack. The same as %pK case. Uninlined it obviously eats "only" 8 bytes for IP.

The idea is to avoid excess stack consumption for things like:

	struct va_format vaf;
	const char *fmt = "some format with %ptc";
	vaf.fmt = fmt;
	vaf.va = &va_list;
	printk("some format with %pV\n", &vaf);
Quoted 4 lines

There is no way how can noinline_for_stack for task_comm_string lower the stack usage here, right? Note that it adds no more requirements to the stack than there were before. Simply because there are no buffers on the stack in task_comm_string.

Isn't flags always on stack in function pointer
if task_comm_string were inlined?

I believe gcc isn't too good about reusing stack for blocks

void foo(args)

{
	if (bar) {
		long baz;
		...
	} else
		int baz;
		...
}

I believe gcc still creates 2 separate baz vars on foo's stack.

Quoted 1 line

If nothing, it saves 100 bytes of .text.

Submit patches to vsprintf for all the cases you think appropriate.

Quoted 1 line

thanks,

cheers, Joe

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#13 lore raw

Re: [PATCH 2/3] printk: Add %ptc to safely print a task's comm

In reply to Jiri Slaby
Quoted 5 lines · Jiri Slaby
Quoted 1 line · John Stultz

+static noinline_for_stack

I still fail to see why this should be slowed down by noinlining it. Care to explain?

Just that I was hesitant to change it without consensus and it follows the convention of other similarly called functions.

Quoted 2 lines

With my setup, the code below inlined will use 32 bytes of stack. The same as %pK case. Uninlined it obviously eats "only" 8 bytes for IP.

Maybe could we defer that discussion into a following patch, which maybe does a similar analysis on the other noinline_for_stack usage in that case?

(And I may be dropping the whole series here in a bit, so more debate on it might be moot)

thanks
-john

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#14 lore raw

PATCH 3/3 checkpatch.pl: Add check for task comm references

In reply to John Stultz

Now that accessing current->comm needs to be protected, avoid new current->comm or other task->comm usage by adding a warning to checkpatch.pl.

Fair warning: I know zero perl, so this was written in the style of "monkey see, monkey do". It does appear to work in my testing though.

Thanks to Jiri Slaby, Michal Nazarewicz and Joe Perches for help improving the regex!

Close review and feedback would be appreciated.

CC Joe Perches <[email protected]>
CC Michal Nazarewicz <[email protected]>
CC Andy Whitcroft <[email protected]>
CC Jiri Slaby <[email protected]>
CC KOSAKI Motohiro <[email protected]>
CC David Rientjes <[email protected]>
CC Dave Hansen <[email protected]>
CC Andrew Morton <[email protected]>
---
 scripts/checkpatch.pl |    7 +++++++
 1 files changed, 7 insertions(+), 0 deletions(-)
scripts/checkpatch.pl +7 -0
index d867081..a67ea69 100755
@@ -2868,6 +2868,13 @@ sub process {
 			WARN("usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
 		}
 
+# check for current->comm usage
+		our $common_comm_vars = qr{(?x:
+		        current|tsk|p|task|curr|chip|t|object|me
+		)};
+		if ($line =~ /\b($common_comm_vars)\s*->\s*comm\b/) {
+			WARN("comm access needs to be protected. Use get_task_comm, or printk's \%ptc formatting.\n" . $herecurr);
+		}
 # check for %L{u,d,i} in strings
 		my $string;
 		while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
Signature · 8 lines
1.7.3.2.146.gca209

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#15 lore raw

Re: [PATCH 3/3] checkpatch.pl: Add check for task comm references

Quoted 32 lines · John Stultz

Now that accessing current->comm needs to be protected, avoid new current->comm or other task->comm usage by adding a warning to checkpatch.pl.

Fair warning: I know zero perl, so this was written in the style of "monkey see, monkey do". It does appear to work in my testing though.

Thanks to Jiri Slaby, Michal Nazarewicz and Joe Perches for help improving the regex!

Close review and feedback would be appreciated.

CC Joe Perches <[email protected]>
CC Michal Nazarewicz <[email protected]>
CC Andy Whitcroft <[email protected]>
CC Jiri Slaby <[email protected]>
CC KOSAKI Motohiro <[email protected]>
CC David Rientjes <[email protected]>
CC Dave Hansen <[email protected]>
CC Andrew Morton <[email protected]>
---
 scripts/checkpatch.pl |    7 +++++++
 1 files changed, 7 insertions(+), 0 deletions(-)
scripts/checkpatch.pl +0 -0
index d867081..a67ea69 100755

scripts/checkpatch.pl sub process { · line 2873

Quoted 6 lines
scripts/checkpatch.pl +3 -0
@@ -2868,6 +2868,13 @@ sub process {
 			WARN("usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
 		}
 
+# check for current->comm usage
+		our $common_comm_vars = qr{(?x:
+		        current|tsk|p|task|curr|chip|t|object|me

Hrm, chip->comm looks like a total bullshit. object->comm refers to kmemleak object, so this would trigger false alarms too.

scripts/checkpatch.pl sub process { · line 2880

Quoted 7 lines
scripts/checkpatch.pl +4 -0
+		)};
+		if ($line =~ /\b($common_comm_vars)\s*->\s*comm\b/) {
+			WARN("comm access needs to be protected. Use get_task_comm, or printk's \%ptc formatting.\n" . $herecurr);
+		}
 # check for %L{u,d,i} in strings
 		my $string;
 		while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {

thanks,

Signature · 1 line
js
#16 lore raw

Re: [PATCH 3/3] checkpatch.pl: Add check for task comm references

In reply to John Stultz

scripts/checkpatch.pl sub process { · line 2872

Quoted 10 lines · John Stultz
scripts/checkpatch.pl +2 -0
index d867081..a67ea69 100755
@@ -2868,6 +2868,13 @@ sub process {
 			WARN("usage of NR_CPUS is often wrong - consider using  
 cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
 		}
+# check for current->comm usage
+		our $common_comm_vars = qr{(?x:

It should by "my" not "our".

scripts/checkpatch.pl sub process { · line 2874

Quoted 2 lines
scripts/checkpatch.pl +2 -0
+		        current|tsk|p|task|curr|chip|t|object|me
+		)};

Also, I would stick it on a single line, ie.:

		my $comm_vars = qr/current|tsk|p|task|curr|chip|t|object|me/;

scripts/checkpatch.pl sub process { · line 2875

Quoted 1 line
scripts/checkpatch.pl +1 -0
+		if ($line =~ /\b($common_comm_vars)\s*->\s*comm\b/) {

The parens are not needed.

scripts/checkpatch.pl sub process { · line 2878

Quoted 3 lines
scripts/checkpatch.pl +2 -0
+			WARN("comm access needs to be protected. Use get_task_comm, or  
 printk's \%ptc formatting.\n" . $herecurr);
+		}

Empty line should be here.

Quoted 3 lines
scripts/checkpatch.pl +0 -0
 # check for %L{u,d,i} in strings
 		my $string;

while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {

Signature · 4 lines
Best regards,                                         _     _
.o. | Liege of Serenely Enlightened Majesty of      o' \,=./ `o
..o | Computer Science,  Michal "mina86" Nazarewicz    (o o)
ooo +-----<email/xmpp: [email protected]>-----ooO--(_)--Ooo--