Skip to content

Commit de37e50

Browse files
committed
Merge tag 'cgroup-for-7.1-rc4-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/tj/cgroup
Pull cgroup fixes from Tejun Heo: "Two rstat fixes: - Out-of-bounds access in the css_rstat_updated() BPF kfunc when called with an unchecked user-supplied cpu - Over-strict NMI guard after the recent switch to try_cmpxchg left sparc and ppc64 unable to queue rstat updates from NMI" * tag 'cgroup-for-7.1-rc4-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/tj/cgroup: cgroup: rstat: relax NMI guard after switch to try_cmpxchg cgroup/rstat: validate cpu before css_rstat_cpu() access
2 parents 4a5860e + 22572db commit de37e50

4 files changed

Lines changed: 28 additions & 18 deletions

File tree

block/blk-cgroup.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2241,7 +2241,7 @@ void blk_cgroup_bio_start(struct bio *bio)
22412241
}
22422242

22432243
u64_stats_update_end_irqrestore(&bis->sync, flags);
2244-
css_rstat_updated(&blkcg->css, cpu);
2244+
__css_rstat_updated(&blkcg->css, cpu);
22452245
put_cpu();
22462246
}
22472247

include/linux/cgroup.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,7 @@ static inline void cgroup_path_from_kernfs_id(u64 id, char *buf, size_t buflen)
777777
/*
778778
* cgroup scalable recursive statistics.
779779
*/
780+
void __css_rstat_updated(struct cgroup_subsys_state *css, int cpu);
780781
void css_rstat_updated(struct cgroup_subsys_state *css, int cpu);
781782
void css_rstat_flush(struct cgroup_subsys_state *css);
782783

kernel/cgroup/rstat.c

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// SPDX-License-Identifier: GPL-2.0-only
22
#include "cgroup-internal.h"
33

4+
#include <linux/cpumask.h>
45
#include <linux/sched/cputime.h>
56

67
#include <linux/bpf.h>
@@ -53,7 +54,7 @@ static inline struct llist_head *ss_lhead_cpu(struct cgroup_subsys *ss, int cpu)
5354
}
5455

5556
/**
56-
* css_rstat_updated - keep track of updated rstat_cpu
57+
* __css_rstat_updated - keep track of updated rstat_cpu
5758
* @css: target cgroup subsystem state
5859
* @cpu: cpu on which rstat_cpu was updated
5960
*
@@ -63,31 +64,27 @@ static inline struct llist_head *ss_lhead_cpu(struct cgroup_subsys *ss, int cpu)
6364
*
6465
* NOTE: if the user needs the guarantee that the updater either add itself in
6566
* the lockless list or the concurrent flusher flushes its updated stats, a
66-
* memory barrier is needed before the call to css_rstat_updated() i.e. a
67+
* memory barrier is needed before the call to __css_rstat_updated() i.e. a
6768
* barrier after updating the per-cpu stats and before calling
68-
* css_rstat_updated().
69+
* __css_rstat_updated().
6970
*/
70-
__bpf_kfunc void css_rstat_updated(struct cgroup_subsys_state *css, int cpu)
71+
void __css_rstat_updated(struct cgroup_subsys_state *css, int cpu)
7172
{
7273
struct llist_head *lhead;
7374
struct css_rstat_cpu *rstatc;
7475
struct llist_node *self;
7576

76-
/*
77-
* Since bpf programs can call this function, prevent access to
78-
* uninitialized rstat pointers.
79-
*/
77+
/* Prevent access to uninitialized rstat pointers. */
8078
if (!css_uses_rstat(css))
8179
return;
8280

8381
lockdep_assert_preemption_disabled();
8482

8583
/*
86-
* For archs withnot nmi safe cmpxchg or percpu ops support, ignore
87-
* the requests from nmi context.
84+
* The lockless insertion below relies on NMI-safe cmpxchg;
85+
* bail out in NMI on archs that don't provide it.
8886
*/
89-
if ((!IS_ENABLED(CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG) ||
90-
!IS_ENABLED(CONFIG_ARCH_HAS_NMI_SAFE_THIS_CPU_OPS)) && in_nmi())
87+
if (!IS_ENABLED(CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG) && in_nmi())
9188
return;
9289

9390
rstatc = css_rstat_cpu(css, cpu);
@@ -125,6 +122,18 @@ __bpf_kfunc void css_rstat_updated(struct cgroup_subsys_state *css, int cpu)
125122
llist_add(&rstatc->lnode, lhead);
126123
}
127124

125+
/*
126+
* BPF-facing wrapper for __css_rstat_updated(). Validate the caller-provided
127+
* CPU before passing it to the internal rstat updater.
128+
*/
129+
__bpf_kfunc void css_rstat_updated(struct cgroup_subsys_state *css, int cpu)
130+
{
131+
if (unlikely(cpu < 0 || cpu >= nr_cpu_ids || !cpu_possible(cpu)))
132+
return;
133+
134+
__css_rstat_updated(css, cpu);
135+
}
136+
128137
static void __css_process_update_tree(struct cgroup_subsys_state *css, int cpu)
129138
{
130139
/* put @css and all ancestors on the corresponding updated lists */
@@ -170,7 +179,7 @@ static void css_process_update_tree(struct cgroup_subsys *ss, int cpu)
170179
* flusher flush the stats updated by the updater who have
171180
* observed that they are already on the list. The
172181
* corresponding barrier pair for this one should be before
173-
* css_rstat_updated() by the user.
182+
* __css_rstat_updated() by the user.
174183
*
175184
* For now, there aren't any such user, so not adding the
176185
* barrier here but if such a use-case arise, please add
@@ -614,7 +623,7 @@ static void cgroup_base_stat_cputime_account_end(struct cgroup *cgrp,
614623
unsigned long flags)
615624
{
616625
u64_stats_update_end_irqrestore(&rstatbc->bsync, flags);
617-
css_rstat_updated(&cgrp->self, smp_processor_id());
626+
__css_rstat_updated(&cgrp->self, smp_processor_id());
618627
put_cpu_ptr(rstatbc);
619628
}
620629

mm/memcontrol.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,7 @@ static inline void memcg_rstat_updated(struct mem_cgroup *memcg, long val,
679679
if (!val)
680680
return;
681681

682-
css_rstat_updated(&memcg->css, cpu);
682+
__css_rstat_updated(&memcg->css, cpu);
683683
statc_pcpu = memcg->vmstats_percpu;
684684
for (; statc_pcpu; statc_pcpu = statc->parent_pcpu) {
685685
statc = this_cpu_ptr(statc_pcpu);
@@ -2796,7 +2796,7 @@ static inline void account_slab_nmi_safe(struct mem_cgroup *memcg,
27962796
struct mem_cgroup_per_node *pn = memcg->nodeinfo[pgdat->node_id];
27972797

27982798
/* preemption is disabled in_nmi(). */
2799-
css_rstat_updated(&memcg->css, smp_processor_id());
2799+
__css_rstat_updated(&memcg->css, smp_processor_id());
28002800
if (idx == NR_SLAB_RECLAIMABLE_B)
28012801
atomic_add(nr, &pn->slab_reclaimable);
28022802
else
@@ -3019,7 +3019,7 @@ static inline void account_kmem_nmi_safe(struct mem_cgroup *memcg, int val)
30193019
mod_memcg_state(memcg, MEMCG_KMEM, val);
30203020
} else {
30213021
/* preemption is disabled in_nmi(). */
3022-
css_rstat_updated(&memcg->css, smp_processor_id());
3022+
__css_rstat_updated(&memcg->css, smp_processor_id());
30233023
atomic_add(val, &memcg->kmem_stat);
30243024
}
30253025
}

0 commit comments

Comments
 (0)