Skip to content

Commit afb2bc1

Browse files
committed
cgroup: don't guarantee cgroup.procs is sorted if sane_behavior
For some reason, tasks and cgroup.procs guarantee that the result is sorted. This is the only reason this whole pidlist logic is necessary instead of just iterating through sorted member tasks. We can't do anything about the existing interface but at least ensure that such expectation doesn't exist for the new interface so that pidlist logic may be removed in the distant future. This patch scrambles the sort order if sane_behavior so that the output is usually not sorted in the new interface. Signed-off-by: Tejun Heo <tj@kernel.org> Acked-by: Li Zefan <lizefan@huawei.com>
1 parent 0450236 commit afb2bc1

2 files changed

Lines changed: 49 additions & 5 deletions

File tree

‎include/linux/cgroup.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,9 @@ enum {
275275
* - "tasks" is removed. Everything should be at process
276276
* granularity. Use "cgroup.procs" instead.
277277
*
278+
* - "cgroup.procs" is not sorted. pids will be unique unless they
279+
* got recycled inbetween reads.
280+
*
278281
* - "release_agent" and "notify_on_release" are removed.
279282
* Replacement notification mechanism will be implemented.
280283
*

‎kernel/cgroup.c‎

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3564,11 +3564,49 @@ static int pidlist_uniq(pid_t *list, int length)
35643564
return dest;
35653565
}
35663566

3567+
/*
3568+
* The two pid files - task and cgroup.procs - guaranteed that the result
3569+
* is sorted, which forced this whole pidlist fiasco. As pid order is
3570+
* different per namespace, each namespace needs differently sorted list,
3571+
* making it impossible to use, for example, single rbtree of member tasks
3572+
* sorted by task pointer. As pidlists can be fairly large, allocating one
3573+
* per open file is dangerous, so cgroup had to implement shared pool of
3574+
* pidlists keyed by cgroup and namespace.
3575+
*
3576+
* All this extra complexity was caused by the original implementation
3577+
* committing to an entirely unnecessary property. In the long term, we
3578+
* want to do away with it. Explicitly scramble sort order if
3579+
* sane_behavior so that no such expectation exists in the new interface.
3580+
*
3581+
* Scrambling is done by swapping every two consecutive bits, which is
3582+
* non-identity one-to-one mapping which disturbs sort order sufficiently.
3583+
*/
3584+
static pid_t pid_fry(pid_t pid)
3585+
{
3586+
unsigned a = pid & 0x55555555;
3587+
unsigned b = pid & 0xAAAAAAAA;
3588+
3589+
return (a << 1) | (b >> 1);
3590+
}
3591+
3592+
static pid_t cgroup_pid_fry(struct cgroup *cgrp, pid_t pid)
3593+
{
3594+
if (cgroup_sane_behavior(cgrp))
3595+
return pid_fry(pid);
3596+
else
3597+
return pid;
3598+
}
3599+
35673600
static int cmppid(const void *a, const void *b)
35683601
{
35693602
return *(pid_t *)a - *(pid_t *)b;
35703603
}
35713604

3605+
static int fried_cmppid(const void *a, const void *b)
3606+
{
3607+
return pid_fry(*(pid_t *)a) - pid_fry(*(pid_t *)b);
3608+
}
3609+
35723610
static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
35733611
enum cgroup_filetype type)
35743612
{
@@ -3656,7 +3694,10 @@ static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
36563694
css_task_iter_end(&it);
36573695
length = n;
36583696
/* now sort & (if procs) strip out duplicates */
3659-
sort(array, length, sizeof(pid_t), cmppid, NULL);
3697+
if (cgroup_sane_behavior(cgrp))
3698+
sort(array, length, sizeof(pid_t), fried_cmppid, NULL);
3699+
else
3700+
sort(array, length, sizeof(pid_t), cmppid, NULL);
36603701
if (type == CGROUP_FILE_PROCS)
36613702
length = pidlist_uniq(array, length);
36623703

@@ -3777,10 +3818,10 @@ static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
37773818

37783819
while (index < end) {
37793820
int mid = (index + end) / 2;
3780-
if (l->list[mid] == pid) {
3821+
if (cgroup_pid_fry(cgrp, l->list[mid]) == pid) {
37813822
index = mid;
37823823
break;
3783-
} else if (l->list[mid] <= pid)
3824+
} else if (cgroup_pid_fry(cgrp, l->list[mid]) <= pid)
37843825
index = mid + 1;
37853826
else
37863827
end = mid;
@@ -3791,7 +3832,7 @@ static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
37913832
return NULL;
37923833
/* Update the abstract position to be the actual pid that we found */
37933834
iter = l->list + index;
3794-
*pos = *iter;
3835+
*pos = cgroup_pid_fry(cgrp, *iter);
37953836
return iter;
37963837
}
37973838

@@ -3820,7 +3861,7 @@ static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
38203861
if (p >= end) {
38213862
return NULL;
38223863
} else {
3823-
*pos = *p;
3864+
*pos = cgroup_pid_fry(of->cgrp, *p);
38243865
return p;
38253866
}
38263867
}

0 commit comments

Comments
 (0)