Skip to content

Commit 4616120

Browse files
shakeelbhtejun
authored andcommitted
cgroup: add lockless fast-path checks to cgroup_file_notify()
Add lockless checks before acquiring cgroup_file_kn_lock: 1. READ_ONCE(cfile->kn) NULL check to skip torn-down files. 2. READ_ONCE(cfile->notified_at) rate-limit check to skip when within the notification interval. If within the interval, arm the deferred timer via timer_reduce() and confirm it is pending before returning -- if the timer fired in between, fall through to the lock path so the notification is not lost. Both checks have safe error directions -- a stale read can only cause unnecessary lock acquisition, never a missed notification. The critical section is simplified to just taking a kernfs_get() reference and updating notified_at. Annotate cfile->kn and cfile->notified_at write sites with WRITE_ONCE() to pair with the lockless readers. Reported-by: Jakub Kicinski <[email protected]> Signed-off-by: Shakeel Butt <[email protected]> Signed-off-by: Tejun Heo <[email protected]>
1 parent 05070cd commit 4616120

1 file changed

Lines changed: 17 additions & 13 deletions

File tree

kernel/cgroup/cgroup.c

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1694,7 +1694,7 @@ static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
16941694
struct cgroup_file *cfile = (void *)css + cft->file_offset;
16951695

16961696
spin_lock_irq(&cgroup_file_kn_lock);
1697-
cfile->kn = NULL;
1697+
WRITE_ONCE(cfile->kn, NULL);
16981698
spin_unlock_irq(&cgroup_file_kn_lock);
16991699

17001700
timer_delete_sync(&cfile->notify_timer);
@@ -4375,7 +4375,7 @@ static int cgroup_add_file(struct cgroup_subsys_state *css, struct cgroup *cgrp,
43754375
timer_setup(&cfile->notify_timer, cgroup_file_notify_timer, 0);
43764376

43774377
spin_lock_irq(&cgroup_file_kn_lock);
4378-
cfile->kn = kn;
4378+
WRITE_ONCE(cfile->kn, kn);
43794379
spin_unlock_irq(&cgroup_file_kn_lock);
43804380
}
43814381

@@ -4631,21 +4631,25 @@ int cgroup_add_legacy_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
46314631
*/
46324632
void cgroup_file_notify(struct cgroup_file *cfile)
46334633
{
4634-
unsigned long flags;
4634+
unsigned long flags, last, next;
46354635
struct kernfs_node *kn = NULL;
46364636

4637+
if (!READ_ONCE(cfile->kn))
4638+
return;
4639+
4640+
last = READ_ONCE(cfile->notified_at);
4641+
next = last + CGROUP_FILE_NOTIFY_MIN_INTV;
4642+
if (time_in_range(jiffies, last, next)) {
4643+
timer_reduce(&cfile->notify_timer, next);
4644+
if (timer_pending(&cfile->notify_timer))
4645+
return;
4646+
}
4647+
46374648
spin_lock_irqsave(&cgroup_file_kn_lock, flags);
46384649
if (cfile->kn) {
4639-
unsigned long last = cfile->notified_at;
4640-
unsigned long next = last + CGROUP_FILE_NOTIFY_MIN_INTV;
4641-
4642-
if (time_in_range(jiffies, last, next)) {
4643-
timer_reduce(&cfile->notify_timer, next);
4644-
} else {
4645-
kn = cfile->kn;
4646-
kernfs_get(kn);
4647-
cfile->notified_at = jiffies;
4648-
}
4650+
kn = cfile->kn;
4651+
kernfs_get(kn);
4652+
WRITE_ONCE(cfile->notified_at, jiffies);
46494653
}
46504654
spin_unlock_irqrestore(&cgroup_file_kn_lock, flags);
46514655

0 commit comments

Comments
 (0)