linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] connector: report comm change event when modifying /proc/pid/task/tid/comm
@ 2019-09-16 14:13 KeMeng Shi
  2019-09-16 21:10 ` Will Deacon
  0 siblings, 1 reply; 7+ messages in thread
From: KeMeng Shi @ 2019-09-16 14:13 UTC (permalink / raw)
  To: akpm, james.morris, gregkh, mortonm, will.deacon,
	kristina.martsenko, yuehaibing, malat, j.neuschaefer
  Cc: linux-kernel

Commit f786ecba41588 ("connector: add comm change event report to proc
 connector") added proc_comm_connector to report comm change event, and
prctl will report comm change event when dealing with PR_SET_NAME case.

prctl can only set the name of the calling thread. In order to set the name
of other threads in a process, modifying /proc/self/task/tid/comm is a
general way.It's exactly how pthread_setname_np do to set name of a thread.

It's unable to get comm change event of thread if the name of thread is set
by other thread via pthread_setname_np. This update provides a chance for
application to monitor and control threads whose name is set by other
threads.

Signed-off-by: KeMeng Shi <shikemeng@huawei.com>
---
 fs/proc/base.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/fs/proc/base.c b/fs/proc/base.c
index ebea9501afb8..34ffe572ac69 100644
--- a/fs/proc/base.c
+++ b/fs/proc/base.c
@@ -94,6 +94,7 @@
 #include <linux/sched/debug.h>
 #include <linux/sched/stat.h>
 #include <linux/posix-timers.h>
+#include <linux/cn_proc.h>
 #include <trace/events/oom.h>
 #include "internal.h"
 #include "fd.h"
@@ -1549,10 +1550,12 @@ static ssize_t comm_write(struct file *file, const char __user *buf,
 	if (!p)
 		return -ESRCH;
 
-	if (same_thread_group(current, p))
+	if (same_thread_group(current, p)) {
 		set_task_comm(p, buffer);
-	else
+		proc_comm_connector(p);
+	} else {
 		count = -EINVAL;
+	}
 
 	put_task_struct(p);
 
-- 
2.19.1


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

* Re: [PATCH] connector: report comm change event when modifying /proc/pid/task/tid/comm
  2019-09-16 14:13 [PATCH] connector: report comm change event when modifying /proc/pid/task/tid/comm KeMeng Shi
@ 2019-09-16 21:10 ` Will Deacon
  2019-09-17  2:36   ` KeMeng Shi
  2019-09-17 13:56   ` KeMeng Shi
  0 siblings, 2 replies; 7+ messages in thread
From: Will Deacon @ 2019-09-16 21:10 UTC (permalink / raw)
  To: KeMeng Shi
  Cc: akpm, james.morris, gregkh, mortonm, will.deacon,
	kristina.martsenko, yuehaibing, malat, j.neuschaefer,
	linux-kernel

On Mon, Sep 16, 2019 at 10:13:41AM -0400, KeMeng Shi wrote:
> Commit f786ecba41588 ("connector: add comm change event report to proc
>  connector") added proc_comm_connector to report comm change event, and
> prctl will report comm change event when dealing with PR_SET_NAME case.
> 
> prctl can only set the name of the calling thread. In order to set the name
> of other threads in a process, modifying /proc/self/task/tid/comm is a
> general way.It's exactly how pthread_setname_np do to set name of a thread.
> 
> It's unable to get comm change event of thread if the name of thread is set
> by other thread via pthread_setname_np. This update provides a chance for
> application to monitor and control threads whose name is set by other
> threads.
> 
> Signed-off-by: KeMeng Shi <shikemeng@huawei.com>
> ---
>  fs/proc/base.c | 7 +++++--
>  1 file changed, 5 insertions(+), 2 deletions(-)
> 
> diff --git a/fs/proc/base.c b/fs/proc/base.c
> index ebea9501afb8..34ffe572ac69 100644
> --- a/fs/proc/base.c
> +++ b/fs/proc/base.c
> @@ -94,6 +94,7 @@
>  #include <linux/sched/debug.h>
>  #include <linux/sched/stat.h>
>  #include <linux/posix-timers.h>
> +#include <linux/cn_proc.h>
>  #include <trace/events/oom.h>
>  #include "internal.h"
>  #include "fd.h"
> @@ -1549,10 +1550,12 @@ static ssize_t comm_write(struct file *file, const char __user *buf,
>  	if (!p)
>  		return -ESRCH;
>  
> -	if (same_thread_group(current, p))
> +	if (same_thread_group(current, p)) {
>  		set_task_comm(p, buffer);
> -	else
> +		proc_comm_connector(p);
> +	} else {
>  		count = -EINVAL;
> +	}
>  
>  	put_task_struct(p);

The rough idea looks ok to me but I have two concerns:

  (1) This looks like it will be visible to userspace, and this changes
      the behaviour after ~8 years of not reporting this event.

  (2) What prevents proc_comm_connector(p) running concurrently with itself
      via the prctl()? The locking seems to be confined to set_task_comm().

Will

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

* Re: Re: [PATCH] connector: report comm change event when modifying /proc/pid/task/tid/comm
  2019-09-16 21:10 ` Will Deacon
@ 2019-09-17  2:36   ` KeMeng Shi
  2019-09-17 13:56   ` KeMeng Shi
  1 sibling, 0 replies; 7+ messages in thread
From: KeMeng Shi @ 2019-09-17  2:36 UTC (permalink / raw)
  To: will, akpm, james.morris, gregkh, mortonm, will.deacon,
	kristina.martsenko, yuehaibing, malat, j.neuschaefer
  Cc: linux-kernel

>(2) What prevents proc_comm_connector(p) running concurrently with itself
> via the prctl()? The locking seems to be confined to set_task_comm().
To be honest, I did not consider the concurrence problem at beginning. And
some comm change events may lost or are reported repeatly as follows:
set name via procfs	set name via prctl
set_task_comm
			set_task_comm
proc_comm_connector
			proc_comm_connector
Comm change event belong to procfs losts and the fresh comm change belong
to prctl is reported twice. Actually, there is also concurrence problem
without this update as follows:
set name via procfs	set name via prctl
			set_task_comm
set_task_comm
			proc_comm_connector
Comm change event from procfs is reported instead of prctl, this may
bothers user who only care the comm change via prctl.

There is no matter if user only care the latest comm change otherwise, put
setting name and reporting event in crtical region may be the only answer.

Thanks for review.
KeMeng Shi

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

* Re: Re: [PATCH] connector: report comm change event when modifying /proc/pid/task/tid/comm
  2019-09-16 21:10 ` Will Deacon
  2019-09-17  2:36   ` KeMeng Shi
@ 2019-09-17 13:56   ` KeMeng Shi
  2019-09-17 17:07     ` Will Deacon
  1 sibling, 1 reply; 7+ messages in thread
From: KeMeng Shi @ 2019-09-17 13:56 UTC (permalink / raw)
  To: will, akpm, james.morris, gregkh, mortonm, will.deacon,
	kristina.martsenko, yuehaibing, malat, j.neuschaefer
  Cc: linux-kernel

on 2019/9/17 at 5:10, Will Deacon wrote:
>The rough idea looks ok to me but I have two concerns:
>
>  (1) This looks like it will be visible to userspace, and this changes
>      the behaviour after ~8 years of not reporting this event.
This do bother for users who only care the comm change via prctl, but it
also benefits users who want all comm changes. Maybe the best way is add
something like config or switch to meet the both conditions above. In my
opinion, users cares comm change event rather than how it change.

>(2) What prevents proc_comm_connector(p) running concurrently with itself
> via the prctl()? The locking seems to be confined to set_task_comm().
To be honest, I did not consider the concurrence problem at beginning. And
some comm change events may lost or are reported repeatly as follows:
set name via procfs	set name via prctl
set_task_comm
			set_task_comm
proc_comm_connector
			proc_comm_connector
Comm change event belong to procfs losts and the fresh comm change belong
to prctl is reported twice. Actually, there is also concurrence problem
without this update as follows:
set name via procfs	set name via prctl
			set_task_comm
set_task_comm
			proc_comm_connector
Comm change event from procfs is reported instead of prctl, this may
bothers user who only care the comm change via prctl.

There is no matter if user only care the latest comm change otherwise, put
setting name and reporting event in crtical region may be the only answer.

I'm sorry the response to (1) concern is missing in last mail. This is a
full version. Apologize again for my mistake.

Thanks for review.
KeMeng Shi

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

* Re: Re: [PATCH] connector: report comm change event when modifying /proc/pid/task/tid/comm
  2019-09-17 13:56   ` KeMeng Shi
@ 2019-09-17 17:07     ` Will Deacon
  2019-09-19  2:43       ` KeMeng Shi
  0 siblings, 1 reply; 7+ messages in thread
From: Will Deacon @ 2019-09-17 17:07 UTC (permalink / raw)
  To: KeMeng Shi
  Cc: akpm, james.morris, gregkh, mortonm, will.deacon,
	kristina.martsenko, yuehaibing, malat, j.neuschaefer,
	linux-kernel

On Tue, Sep 17, 2019 at 09:56:28AM -0400, KeMeng Shi wrote:
> on 2019/9/17 at 5:10, Will Deacon wrote:
> >The rough idea looks ok to me but I have two concerns:
> >
> >  (1) This looks like it will be visible to userspace, and this changes
> >      the behaviour after ~8 years of not reporting this event.
> This do bother for users who only care the comm change via prctl, but it
> also benefits users who want all comm changes. Maybe the best way is add
> something like config or switch to meet the both conditions above. In my
> opinion, users cares comm change event rather than how it change.

I was really just looking for some intuition as to how this event is
currently used and why extending it like this is unlikely to break those
existing users.

> >(2) What prevents proc_comm_connector(p) running concurrently with itself
> > via the prctl()? The locking seems to be confined to set_task_comm().
> To be honest, I did not consider the concurrence problem at beginning. And
> some comm change events may lost or are reported repeatly as follows:
> set name via procfs	set name via prctl
> set_task_comm
> 			set_task_comm
> proc_comm_connector
> 			proc_comm_connector
> Comm change event belong to procfs losts and the fresh comm change belong
> to prctl is reported twice. Actually, there is also concurrence problem
> without this update as follows:
> set name via procfs	set name via prctl
> 			set_task_comm
> set_task_comm
> 			proc_comm_connector
> Comm change event from procfs is reported instead of prctl, this may
> bothers user who only care the comm change via prctl.

Perhaps, although given that proc_comm_connector() is currently only called
on the prctl() path, then it does at least provide the comm from the most
recent prctl() invocation. With your path, the calls can go out of order,
so I think that probably needs fixing.

Will

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

* Re: Re: Re: [PATCH] connector: report comm change event when modifying /proc/pid/task/tid/comm
  2019-09-17 17:07     ` Will Deacon
@ 2019-09-19  2:43       ` KeMeng Shi
  2019-09-19 20:07         ` Andrew Morton
  0 siblings, 1 reply; 7+ messages in thread
From: KeMeng Shi @ 2019-09-19  2:43 UTC (permalink / raw)
  To: will
  Cc: akpm, james.morris, gregkh, mortonm, will.deacon,
	kristina.martsenko, yuehaibing, malat, j.neuschaefer,
	linux-kernel

On 2019/9/18 at 1:08, Will Deacon wrote:
>On Tue, Sep 17, 2019 at 09:56:28AM -0400, KeMeng Shi wrote:
>>on 2019/9/17 at 5:10, Will Deacon wrote:
>>>The rough idea looks ok to me but I have two concerns:
>>>
>>>  (1) This looks like it will be visible to userspace, and this changes
>>>      the behaviour after ~8 years of not reporting this event.
>>This do bother for users who only care the comm change via prctl, but 
>>it also benefits users who want all comm changes. Maybe the best way 
>>is add something like config or switch to meet the both conditions 
>>above. In my opinion, users cares comm change event rather than how it
>>change.
>
>I was really just looking for some intuition as to how this event is currently
>used and why extending it like this is unlikely to break those existing users.

By listening these comm change events, user is able to monitor and control
specific threads that they are interested. For instance, a process control
daemon listening to proc connector and following comm value policies can
place specific threads to assigned cgroup partitions (quota from commit
 f786ecba415888 ("connector: add comm change event report to proc
 connector")).  It's harmless as user ignore the threads with names that
they are not interested.

>>>(2) What prevents proc_comm_connector(p) running concurrently with 
>>>itself  via the prctl()? The locking seems to be confined to set_task_comm().
>>To be honest, I did not consider the concurrence problem at beginning. 
>>And some comm change events may lost or are reported repeatly as follows:
>>set name via procfs	set name via prctl
>>set_task_comm
>>			set_task_comm
>>proc_comm_connector
>>			proc_comm_connector
>>Comm change event belong to procfs losts and the fresh comm change 
>>belong to prctl is reported twice. Actually, there is also concurrence 
>>problem without this update as follows:
>>set name via procfs	set name via prctl
>>			set_task_comm
>>set_task_comm
>>			proc_comm_connector
>>Comm change event from procfs is reported instead of prctl, this may 
>>bothers user who only care the comm change via prctl.
>
>Perhaps, although given that proc_comm_connector() is currently only
>called on the prctl() path, then it does at least provide the comm
>from the most recent prctl() invocation. With your path, the calls
>can go out of order, so I think that probably needs fixing.

It's indeed necessary to fix the concurrence problem. I will submit
a v2 patch when I fix this.

Thanks for your review and advise.
KeMeng Shi

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

* Re: [PATCH] connector: report comm change event when modifying /proc/pid/task/tid/comm
  2019-09-19  2:43       ` KeMeng Shi
@ 2019-09-19 20:07         ` Andrew Morton
  0 siblings, 0 replies; 7+ messages in thread
From: Andrew Morton @ 2019-09-19 20:07 UTC (permalink / raw)
  To: KeMeng Shi
  Cc: will, james.morris, gregkh, mortonm, will.deacon,
	kristina.martsenko, yuehaibing, malat, j.neuschaefer,
	linux-kernel, David Miller

On Wed, 18 Sep 2019 22:43:21 -0400 KeMeng Shi <shikemeng@huawei.com> wrote:

> It's indeed necessary to fix the concurrence problem. I will submit
> a v2 patch when I fix this.

Even though this is a procfs change, connector patches are usually
handled by davem.  So please cc himself, Evgeniy Polyakov and
netdev@vger.kernel.org on the next version, thanks.


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

end of thread, other threads:[~2019-09-19 20:07 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-09-16 14:13 [PATCH] connector: report comm change event when modifying /proc/pid/task/tid/comm KeMeng Shi
2019-09-16 21:10 ` Will Deacon
2019-09-17  2:36   ` KeMeng Shi
2019-09-17 13:56   ` KeMeng Shi
2019-09-17 17:07     ` Will Deacon
2019-09-19  2:43       ` KeMeng Shi
2019-09-19 20:07         ` Andrew Morton

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).