2020-07-02 07:53:24

by Weiping Zhang

[permalink] [raw]
Subject: [RFC PATCH] proc: add support detach proccess's autogroup

Since setid will create a autogroup for that process, there is no
way to detach a process from that autogroup, this patch add a new interface
to detach a process from its autogroup. You can write anything to
/proc/<pid>/autogroup_detach to do that.

Signed-off-by: Weiping Zhang <[email protected]>
---
fs/proc/base.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 50 insertions(+)

diff --git a/fs/proc/base.c b/fs/proc/base.c
index d86c0afc8a85..a210cab8b52d 100644
--- a/fs/proc/base.c
+++ b/fs/proc/base.c
@@ -1541,6 +1541,55 @@ static const struct file_operations proc_pid_sched_autogroup_operations = {
.release = single_release,
};

+static int sched_autogroup_detach_show(struct seq_file *m, void *v)
+{
+ struct inode *inode = m->private;
+
+ seq_printf(m, "write any data to detach this task from a autogroup\n");
+
+ return 0;
+}
+
+static int sched_autogroup_detach_open(struct inode *inode, struct file *filp)
+{
+ int ret;
+
+ ret = single_open(filp, sched_autogroup_detach_show, NULL);
+ if (!ret) {
+ struct seq_file *m = filp->private_data;
+
+ m->private = inode;
+ }
+ return ret;
+}
+
+static ssize_t
+sched_autogroup_detach_write(struct file *file, const char __user *buf,
+ size_t count, loff_t *offset)
+{
+ struct inode *inode = file_inode(file);
+ struct task_struct *p;
+ int ret;
+
+ p = get_proc_task(inode);
+ if (!p)
+ return -ESRCH;
+
+ sched_autogroup_detach(p);
+
+ put_task_struct(p);
+
+ return count;
+}
+
+static const struct file_operations proc_pid_sched_autogroup_detach_operations = {
+ .open = sched_autogroup_detach_open,
+ .read = seq_read,
+ .write = sched_autogroup_detach_write,
+ .llseek = seq_lseek,
+ .release = single_release,
+};
+
#endif /* CONFIG_SCHED_AUTOGROUP */

#ifdef CONFIG_TIME_NS
@@ -3161,6 +3210,7 @@ static const struct pid_entry tgid_base_stuff[] = {
#endif
#ifdef CONFIG_SCHED_AUTOGROUP
REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
+ REG("autogroup_detach", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_detach_operations),
#endif
#ifdef CONFIG_TIME_NS
REG("timens_offsets", S_IRUGO|S_IWUSR, proc_timens_offsets_operations),
--
2.14.1


2020-07-02 08:15:35

by Peter Zijlstra

[permalink] [raw]
Subject: Re: [RFC PATCH] proc: add support detach proccess's autogroup

On Thu, Jul 02, 2020 at 01:20:43PM +0800, Weiping Zhang wrote:
> Since setid will create a autogroup for that process, there is no
> way to detach a process from that autogroup, this patch add a new interface
> to detach a process from its autogroup. You can write anything to
> /proc/<pid>/autogroup_detach to do that.

This is indeed what the patch does; but it fails to tell me why. Why do
we want this? Why should I care about your patch :-)

Please, always explain why you want things done, that can also help us
consider if the proposed solution is the right one.

2020-07-02 10:22:40

by Weiping Zhang

[permalink] [raw]
Subject: Re: [RFC PATCH] proc: add support detach proccess's autogroup

On Thu, Jul 2, 2020 at 4:18 PM Peter Zijlstra <[email protected]> wrote:
>
> On Thu, Jul 02, 2020 at 01:20:43PM +0800, Weiping Zhang wrote:
> > Since setid will create a autogroup for that process, there is no
> > way to detach a process from that autogroup, this patch add a new interface
> > to detach a process from its autogroup. You can write anything to
> > /proc/<pid>/autogroup_detach to do that.
>
> This is indeed what the patch does; but it fails to tell me why. Why do
> we want this? Why should I care about your patch :-)
>
> Please, always explain why you want things done, that can also help us
> consider if the proposed solution is the right one.

The reason is that, there are lots of autogroup created in our system, because
I forgot to disable CONFIG_SCHED_AUTOGROUP, and it leads to a hotspot
in tg_load_down (kenrel-3.10.0-514.16.1.el7.x86_64).
When user login system by ssh and launch a background job, it will create a
new autogroup even user logout ssh.
I cannot find a way do clear these unused autogroup by current kernel,
so I write a separate module to clear those unused autogroup for my system.
I think we should provide a interface to detach a process's autogroup.

Thanks

2020-07-07 09:35:24

by Weiping Zhang

[permalink] [raw]
Subject: Re: [RFC PATCH] proc: add support detach proccess's autogroup

On Thu, Jul 2, 2020 at 6:21 PM Weiping Zhang <[email protected]> wrote:
>
> On Thu, Jul 2, 2020 at 4:18 PM Peter Zijlstra <[email protected]> wrote:
> >
> > On Thu, Jul 02, 2020 at 01:20:43PM +0800, Weiping Zhang wrote:
> > > Since setid will create a autogroup for that process, there is no
> > > way to detach a process from that autogroup, this patch add a new interface
> > > to detach a process from its autogroup. You can write anything to
> > > /proc/<pid>/autogroup_detach to do that.
> >
> > This is indeed what the patch does; but it fails to tell me why. Why do
> > we want this? Why should I care about your patch :-)
> >
> > Please, always explain why you want things done, that can also help us
> > consider if the proposed solution is the right one.
>
> The reason is that, there are lots of autogroup created in our system, because
> I forgot to disable CONFIG_SCHED_AUTOGROUP, and it leads to a hotspot
> in tg_load_down (kenrel-3.10.0-514.16.1.el7.x86_64).
> When user login system by ssh and launch a background job, it will create a
> new autogroup even user logout ssh.
> I cannot find a way do clear these unused autogroup by current kernel,
> so I write a separate module to clear those unused autogroup for my system.
> I think we should provide a interface to detach a process's autogroup.
>
ping