Skip to content

Commit

Permalink
cgroup: Prepare for using css_task_iter_*() in BPF
Browse files Browse the repository at this point in the history
This patch makes some preparations for using css_task_iter_*() in BPF
Program.

1. Flags CSS_TASK_ITER_* are #define-s and it's not easy for bpf prog to
use them. Convert them to enum so bpf prog can take them from vmlinux.h.

2. In the next patch we will add css_task_iter_*() in common kfuncs which
is not safe. Since css_task_iter_*() does spin_unlock_irq() which might
screw up irq flags depending on the context where bpf prog is running.
So we should use irqsave/irqrestore here and the switching is harmless.

Suggested-by: Alexei Starovoitov <[email protected]>
Signed-off-by: Chuyi Zhou <[email protected]>
Acked-by: Tejun Heo <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Alexei Starovoitov <[email protected]>
  • Loading branch information
kchuyizhou authored and Alexei Starovoitov committed Oct 20, 2023
1 parent 6bd5e16 commit 6da8830
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
12 changes: 5 additions & 7 deletions include/linux/cgroup.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,11 @@ struct kernel_clone_args;
#define CGROUP_WEIGHT_DFL 100
#define CGROUP_WEIGHT_MAX 10000

/* walk only threadgroup leaders */
#define CSS_TASK_ITER_PROCS (1U << 0)
/* walk all threaded css_sets in the domain */
#define CSS_TASK_ITER_THREADED (1U << 1)

/* internal flags */
#define CSS_TASK_ITER_SKIPPED (1U << 16)
enum {
CSS_TASK_ITER_PROCS = (1U << 0), /* walk only threadgroup leaders */
CSS_TASK_ITER_THREADED = (1U << 1), /* walk all threaded css_sets in the domain */
CSS_TASK_ITER_SKIPPED = (1U << 16), /* internal flags */
};

/* a css_task_iter should be treated as an opaque object */
struct css_task_iter {
Expand Down
18 changes: 12 additions & 6 deletions kernel/cgroup/cgroup.c
Original file line number Diff line number Diff line change
Expand Up @@ -4917,9 +4917,11 @@ static void css_task_iter_advance(struct css_task_iter *it)
void css_task_iter_start(struct cgroup_subsys_state *css, unsigned int flags,
struct css_task_iter *it)
{
unsigned long irqflags;

memset(it, 0, sizeof(*it));

spin_lock_irq(&css_set_lock);
spin_lock_irqsave(&css_set_lock, irqflags);

it->ss = css->ss;
it->flags = flags;
Expand All @@ -4933,7 +4935,7 @@ void css_task_iter_start(struct cgroup_subsys_state *css, unsigned int flags,

css_task_iter_advance(it);

spin_unlock_irq(&css_set_lock);
spin_unlock_irqrestore(&css_set_lock, irqflags);
}

/**
Expand All @@ -4946,12 +4948,14 @@ void css_task_iter_start(struct cgroup_subsys_state *css, unsigned int flags,
*/
struct task_struct *css_task_iter_next(struct css_task_iter *it)
{
unsigned long irqflags;

if (it->cur_task) {
put_task_struct(it->cur_task);
it->cur_task = NULL;
}

spin_lock_irq(&css_set_lock);
spin_lock_irqsave(&css_set_lock, irqflags);

/* @it may be half-advanced by skips, finish advancing */
if (it->flags & CSS_TASK_ITER_SKIPPED)
Expand All @@ -4964,7 +4968,7 @@ struct task_struct *css_task_iter_next(struct css_task_iter *it)
css_task_iter_advance(it);
}

spin_unlock_irq(&css_set_lock);
spin_unlock_irqrestore(&css_set_lock, irqflags);

return it->cur_task;
}
Expand All @@ -4977,11 +4981,13 @@ struct task_struct *css_task_iter_next(struct css_task_iter *it)
*/
void css_task_iter_end(struct css_task_iter *it)
{
unsigned long irqflags;

if (it->cur_cset) {
spin_lock_irq(&css_set_lock);
spin_lock_irqsave(&css_set_lock, irqflags);
list_del(&it->iters_node);
put_css_set_locked(it->cur_cset);
spin_unlock_irq(&css_set_lock);
spin_unlock_irqrestore(&css_set_lock, irqflags);
}

if (it->cur_dcset)
Expand Down

0 comments on commit 6da8830

Please sign in to comment.