Skip to content

Commit b12e358

Browse files
committed
cgroup: Limit event generation frequency
".events" files generate file modified event to notify userland of possible new events. Some of the events can be quite bursty (e.g. memory high event) and generating notification each time is costly and pointless. This patch implements a event rate limit mechanism. If a new notification is requested before 10ms has passed since the previous notification, the new notification is delayed till then. As this only delays from the second notification on in a given close cluster of notifications, userland reactions to notifications shouldn't be delayed at all in most cases while avoiding notification storms. Signed-off-by: Tejun Heo <tj@kernel.org>
1 parent 5faaf05 commit b12e358

2 files changed

Lines changed: 25 additions & 2 deletions

File tree

‎include/linux/cgroup-defs.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ enum {
105105
struct cgroup_file {
106106
/* do not access any fields from outside cgroup core */
107107
struct kernfs_node *kn;
108+
unsigned long notified_at;
109+
struct timer_list notify_timer;
108110
};
109111

110112
/*

‎kernel/cgroup/cgroup.c‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@
6161

6262
#define CGROUP_FILE_NAME_MAX (MAX_CGROUP_TYPE_NAMELEN + \
6363
MAX_CFTYPE_NAME + 2)
64+
/* let's not notify more than 100 times per second */
65+
#define CGROUP_FILE_NOTIFY_MIN_INTV DIV_ROUND_UP(HZ, 100)
6466

6567
/*
6668
* cgroup_mutex is the master lock. Any modification to cgroup or its
@@ -1554,6 +1556,8 @@ static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
15541556
spin_lock_irq(&cgroup_file_kn_lock);
15551557
cfile->kn = NULL;
15561558
spin_unlock_irq(&cgroup_file_kn_lock);
1559+
1560+
del_timer_sync(&cfile->notify_timer);
15571561
}
15581562

15591563
kernfs_remove_by_name(cgrp->kn, cgroup_file_name(cgrp, cft, name));
@@ -3532,6 +3536,12 @@ static int cgroup_kn_set_ugid(struct kernfs_node *kn)
35323536
return kernfs_setattr(kn, &iattr);
35333537
}
35343538

3539+
static void cgroup_file_notify_timer(struct timer_list *timer)
3540+
{
3541+
cgroup_file_notify(container_of(timer, struct cgroup_file,
3542+
notify_timer));
3543+
}
3544+
35353545
static int cgroup_add_file(struct cgroup_subsys_state *css, struct cgroup *cgrp,
35363546
struct cftype *cft)
35373547
{
@@ -3558,6 +3568,8 @@ static int cgroup_add_file(struct cgroup_subsys_state *css, struct cgroup *cgrp,
35583568
if (cft->file_offset) {
35593569
struct cgroup_file *cfile = (void *)css + cft->file_offset;
35603570

3571+
timer_setup(&cfile->notify_timer, cgroup_file_notify_timer, 0);
3572+
35613573
spin_lock_irq(&cgroup_file_kn_lock);
35623574
cfile->kn = kn;
35633575
spin_unlock_irq(&cgroup_file_kn_lock);
@@ -3807,8 +3819,17 @@ void cgroup_file_notify(struct cgroup_file *cfile)
38073819
unsigned long flags;
38083820

38093821
spin_lock_irqsave(&cgroup_file_kn_lock, flags);
3810-
if (cfile->kn)
3811-
kernfs_notify(cfile->kn);
3822+
if (cfile->kn) {
3823+
unsigned long last = cfile->notified_at;
3824+
unsigned long next = last + CGROUP_FILE_NOTIFY_MIN_INTV;
3825+
3826+
if (time_in_range(jiffies, last, next)) {
3827+
timer_reduce(&cfile->notify_timer, next);
3828+
} else {
3829+
kernfs_notify(cfile->kn);
3830+
cfile->notified_at = jiffies;
3831+
}
3832+
}
38123833
spin_unlock_irqrestore(&cgroup_file_kn_lock, flags);
38133834
}
38143835

0 commit comments

Comments
 (0)