Skip to contentSkip to conversation

cgroup: improve cgroup_file_notify() scalability

PATCH Superseded Shakeel Butt 2 participants 11 messages Feb 28 – Mar 5

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover cgroup: improve cgroup_file_notify() scalability

Jakub Kicinski reported that after the commit d929525c2e30 ("memcg:
net: track network throttling due to memcg memory pressure"), in Meta fleet, the network intensive workloads started seeing extensive spinlock contention in the rx path in the function cgroup_file_notify().

cgroup_file_notify() uses a single global spinlock to protect the cfile->kn pointer across all cgroups. On systems with many cgroups under memory pressure, this becomes a bottleneck as multiple CPUs in reclaim call __memcg_memory_event(), which walks the cgroup hierarchy calling cgroup_file_notify() at each level -- all serialized on the same lock.

This series reduces the lock hold time by moving kernfs_notify() outside the critical section, adds lockless fast-path checks to avoid the lock entirely in common cases (torn-down files and rate-limited bursts), and replaces the global lock with a per-cgroup_file lock to eliminate cross-cgroup contention.

Shakeel Butt (3):
cgroup: reduce cgroup_file_kn_lock hold time in cgroup_file_notify()
cgroup: add lockless fast-path checks to cgroup_file_notify()
cgroup: replace global cgroup_file_kn_lock with per-cgroup_file lock

 include/linux/cgroup-defs.h |  1 +
 kernel/cgroup/cgroup.c      | 50 +++++++++++++++++++++----------------
 2 files changed, 29 insertions(+), 22 deletions(-)
Signature · 1 line
2.47.3
#1 lore raw

PATCH 1/3 cgroup: reduce cgroup_file_kn_lock hold time in cgroup_file_notify()

cgroup_file_notify() calls kernfs_notify() while holding the global cgroup_file_kn_lock. kernfs_notify() does non-trivial work including wake_up_interruptible() and acquisition of a second global spinlock (kernfs_notify_lock), inflating the hold time.

Take a kernfs_get() reference under the lock and call kernfs_notify() after dropping it, following the pattern from cgroup_file_show().

---
 kernel/cgroup/cgroup.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
kernel/cgroup/cgroup.c +8 -1
index be1d71dda317..33282c7d71e4 100644
@@ -4687,6 +4687,7 @@ int cgroup_add_legacy_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
 void cgroup_file_notify(struct cgroup_file *cfile)
 {
 	unsigned long flags;
+	struct kernfs_node *kn = NULL;
 
 	spin_lock_irqsave(&cgroup_file_kn_lock, flags);
 	if (cfile->kn) {
@@ -4696,11 +4697,17 @@ void cgroup_file_notify(struct cgroup_file *cfile)
 		if (time_in_range(jiffies, last, next)) {
 			timer_reduce(&cfile->notify_timer, next);
 		} else {
-			kernfs_notify(cfile->kn);
+			kn = cfile->kn;
+			kernfs_get(kn);
 			cfile->notified_at = jiffies;
 		}
 	}
 	spin_unlock_irqrestore(&cgroup_file_kn_lock, flags);
+
+	if (kn) {
+		kernfs_notify(kn);
+		kernfs_put(kn);
+	}
 }
 EXPORT_SYMBOL_GPL(cgroup_file_notify);
 
Signature · 1 line
2.47.3
#2 lore raw

PATCH 2/3 cgroup: add lockless fast-path checks to cgroup_file_notify()

In reply to Shakeel Butt

Add two lockless checks before acquiring the lock:

1. READ_ONCE(cfile->kn) NULL check to skip torn-down files.
2. READ_ONCE(cfile->notified_at) check to skip when within the rate-limit window (~10ms).

Both checks have safe error directions -- a stale read can only cause unnecessary lock acquisition, never a missed notification. Annotate all write sites with WRITE_ONCE() to pair with the lockless readers.

The trade-off is that trailing timer_reduce() calls during bursts are skipped, so the deferred notification that delivers the final state may be lost. This is acceptable for the primary callers like __memcg_memory_event() where events keep arriving.

---
 kernel/cgroup/cgroup.c | 21 ++++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)
kernel/cgroup/cgroup.c +14 -7
index 33282c7d71e4..5473ebd0f6c1 100644
@@ -1749,7 +1749,7 @@ static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
 		struct cgroup_file *cfile = (void *)css + cft->file_offset;
 
 		spin_lock_irq(&cgroup_file_kn_lock);
-		cfile->kn = NULL;
+		WRITE_ONCE(cfile->kn, NULL);
 		spin_unlock_irq(&cgroup_file_kn_lock);
 
 		timer_delete_sync(&cfile->notify_timer);
@@ -4430,7 +4430,7 @@ static int cgroup_add_file(struct cgroup_subsys_state *css, struct cgroup *cgrp,
 		timer_setup(&cfile->notify_timer, cgroup_file_notify_timer, 0);
 
 		spin_lock_irq(&cgroup_file_kn_lock);
-		cfile->kn = kn;
+		WRITE_ONCE(cfile->kn, kn);
 		spin_unlock_irq(&cgroup_file_kn_lock);
 	}
 
@@ -4686,20 +4686,27 @@ int cgroup_add_legacy_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
  */
 void cgroup_file_notify(struct cgroup_file *cfile)
 {
-	unsigned long flags;
+	unsigned long flags, last, next;
 	struct kernfs_node *kn = NULL;
 
+	if (!READ_ONCE(cfile->kn))
+		return;
+
+	last = READ_ONCE(cfile->notified_at);
+	if (time_before_eq(jiffies, last + CGROUP_FILE_NOTIFY_MIN_INTV))
+		return;
+
 	spin_lock_irqsave(&cgroup_file_kn_lock, flags);
 	if (cfile->kn) {
-		unsigned long last = cfile->notified_at;
-		unsigned long next = last + CGROUP_FILE_NOTIFY_MIN_INTV;
+		last = cfile->notified_at;
+		next = last + CGROUP_FILE_NOTIFY_MIN_INTV;
 
-		if (time_in_range(jiffies, last, next)) {
+		if (time_before_eq(jiffies, next)) {
 			timer_reduce(&cfile->notify_timer, next);
 		} else {
 			kn = cfile->kn;
 			kernfs_get(kn);
-			cfile->notified_at = jiffies;
+			WRITE_ONCE(cfile->notified_at, jiffies);
 		}
 	}
 	spin_unlock_irqrestore(&cgroup_file_kn_lock, flags);
Signature · 1 line
2.47.3
#3 lore raw

Re: [PATCH 2/3] cgroup: add lockless fast-path checks to cgroup_file_notify()

Hi Shakeel,

Good series to move away from the global lock.

Quoted 48 lines · Shakeel Butt

Add two lockless checks before acquiring the lock:

1. READ_ONCE(cfile->kn) NULL check to skip torn-down files.
2. READ_ONCE(cfile->notified_at) check to skip when within the rate-limit window (~10ms).

Both checks have safe error directions -- a stale read can only cause unnecessary lock acquisition, never a missed notification. Annotate all write sites with WRITE_ONCE() to pair with the lockless readers.

The trade-off is that trailing timer_reduce() calls during bursts are skipped, so the deferred notification that delivers the final state may be lost. This is acceptable for the primary callers like __memcg_memory_event() where events keep arriving.

---
 kernel/cgroup/cgroup.c | 21 ++++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)
kernel/cgroup/cgroup.c +2 -3
index 33282c7d71e4..5473ebd0f6c1 100644
@@ -1749,7 +1749,7 @@ static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
 		struct cgroup_file *cfile = (void *)css + cft->file_offset;
 
 		spin_lock_irq(&cgroup_file_kn_lock);
-		cfile->kn = NULL;
+		WRITE_ONCE(cfile->kn, NULL);
 		spin_unlock_irq(&cgroup_file_kn_lock);
 
 		timer_delete_sync(&cfile->notify_timer);
@@ -4430,7 +4430,7 @@ static int cgroup_add_file(struct cgroup_subsys_state *css, struct cgroup *cgrp,
 		timer_setup(&cfile->notify_timer, cgroup_file_notify_timer, 0);
 
 		spin_lock_irq(&cgroup_file_kn_lock);
-		cfile->kn = kn;
+		WRITE_ONCE(cfile->kn, kn);
 		spin_unlock_irq(&cgroup_file_kn_lock);
 	}
 
@@ -4686,20 +4686,27 @@ int cgroup_add_legacy_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
  */
 void cgroup_file_notify(struct cgroup_file *cfile)
 {
-	unsigned long flags;

kernel/cgroup/cgroup.c int cgroup_add_legacy_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) · line 4698

Quoted 10 lines
kernel/cgroup/cgroup.c +8 -0
+	unsigned long flags, last, next;
 	struct kernfs_node *kn = NULL;
 
+	if (!READ_ONCE(cfile->kn))
+		return;
+
+	last = READ_ONCE(cfile->notified_at);
+	if (time_before_eq(jiffies, last + CGROUP_FILE_NOTIFY_MIN_INTV))
+		return;
+

Previously, if a notification arrived within the rate-limit window, we would still call timer_reduce(&cfile->notify_timer, next) to schedule a deferred notification.

With this change, returning early here bypasses that timer scheduling entirely. Does this risk missing notifications that would have been delivered by the timer?

Quoted 18 lines
kernel/cgroup/cgroup.c +4 -4
 	spin_lock_irqsave(&cgroup_file_kn_lock, flags);
 	if (cfile->kn) {
-		unsigned long last = cfile->notified_at;
-		unsigned long next = last + CGROUP_FILE_NOTIFY_MIN_INTV;
+		last = cfile->notified_at;
+		next = last + CGROUP_FILE_NOTIFY_MIN_INTV;
 
-		if (time_in_range(jiffies, last, next)) {
+		if (time_before_eq(jiffies, next)) {
 			timer_reduce(&cfile->notify_timer, next);
 		} else {
 			kn = cfile->kn;
 			kernfs_get(kn);
-			cfile->notified_at = jiffies;
+			WRITE_ONCE(cfile->notified_at, jiffies);
 		}
 	}
 	spin_unlock_irqrestore(&cgroup_file_kn_lock, flags);
Signature · 2 lines
Best regards,
Ridong
#4 lore raw

Re: [PATCH 2/3] cgroup: add lockless fast-path checks to cgroup_file_notify()

Hi Chen, thanks for taking a look.

Quoted 69 lines · Chen Ridong

Hi Shakeel,

Good series to move away from the global lock.

Quoted 48 lines · Shakeel Butt

Add two lockless checks before acquiring the lock:

1. READ_ONCE(cfile->kn) NULL check to skip torn-down files.
2. READ_ONCE(cfile->notified_at) check to skip when within the rate-limit window (~10ms).

Both checks have safe error directions -- a stale read can only cause unnecessary lock acquisition, never a missed notification. Annotate all write sites with WRITE_ONCE() to pair with the lockless readers.

The trade-off is that trailing timer_reduce() calls during bursts are skipped, so the deferred notification that delivers the final state may be lost. This is acceptable for the primary callers like __memcg_memory_event() where events keep arriving.

---
 kernel/cgroup/cgroup.c | 21 ++++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)
kernel/cgroup/cgroup.c +2 -3
index 33282c7d71e4..5473ebd0f6c1 100644
@@ -1749,7 +1749,7 @@ static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
 		struct cgroup_file *cfile = (void *)css + cft->file_offset;
 
 		spin_lock_irq(&cgroup_file_kn_lock);
-		cfile->kn = NULL;
+		WRITE_ONCE(cfile->kn, NULL);
 		spin_unlock_irq(&cgroup_file_kn_lock);
 
 		timer_delete_sync(&cfile->notify_timer);
@@ -4430,7 +4430,7 @@ static int cgroup_add_file(struct cgroup_subsys_state *css, struct cgroup *cgrp,
 		timer_setup(&cfile->notify_timer, cgroup_file_notify_timer, 0);
 
 		spin_lock_irq(&cgroup_file_kn_lock);
-		cfile->kn = kn;
+		WRITE_ONCE(cfile->kn, kn);
 		spin_unlock_irq(&cgroup_file_kn_lock);
 	}
 
@@ -4686,20 +4686,27 @@ int cgroup_add_legacy_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
  */
 void cgroup_file_notify(struct cgroup_file *cfile)
 {
-	unsigned long flags;

kernel/cgroup/cgroup.c int cgroup_add_legacy_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) · line 4698

Quoted 10 lines
kernel/cgroup/cgroup.c +8 -0
+	unsigned long flags, last, next;
 	struct kernfs_node *kn = NULL;
 
+	if (!READ_ONCE(cfile->kn))
+		return;
+
+	last = READ_ONCE(cfile->notified_at);
+	if (time_before_eq(jiffies, last + CGROUP_FILE_NOTIFY_MIN_INTV))
+		return;
+

Previously, if a notification arrived within the rate-limit window, we would still call timer_reduce(&cfile->notify_timer, next) to schedule a deferred notification.

Quoted 3 lines

With this change, returning early here bypasses that timer scheduling entirely. Does this risk missing notifications that would have been delivered by the timer?

You are indeed right that this can cause missed notifications. After giving some thought I think the lockless check-and-return can be pretty much simplified to timer_pending() check. If timer is active, just do nothing and the notification will be delivered eventually.

I will send the updated version soon. Any comments on the other two patches?

#5 lore raw

Re: [PATCH 2/3] cgroup: add lockless fast-path checks to cgroup_file_notify()

Quoted 4 lines · Shakeel Butt

Hi Chen, thanks for taking a look.

Quoted 1 line · Chen Ridong

[...]

Quoted 13 lines
Quoted 9 lines

Review

Quoted 4 lines
(quoted diff) +4 -0
+	last = READ_ONCE(cfile->notified_at);
+	if (time_before_eq(jiffies, last + CGROUP_FILE_NOTIFY_MIN_INTV))
+		return;
+

Previously, if a notification arrived within the rate-limit window, we would still call timer_reduce(&cfile->notify_timer, next) to schedule a deferred notification.

Quoted 3 lines

With this change, returning early here bypasses that timer scheduling entirely. Does this risk missing notifications that would have been delivered by the timer?

Quoted 7 lines

You are indeed right that this can cause missed notifications. After giving some thought I think the lockless check-and-return can be pretty much simplified to timer_pending() check. If timer is active, just do nothing and the notification will be delivered eventually.

I will send the updated version soon. Any comments on the other two patches?

Something like the following:

From 598199723b50813b015393122796f6775eee02d7 Mon Sep 17 00:00:00 2001
From: Shakeel Butt <[email protected]>
Date: Sat, 28 Feb 2026 04:01:28 -0800
Subject: [PATCH] cgroup: add lockless fast-path checks to cgroup_file_notify()

Add two lockless checks before acquiring the lock:

1. READ_ONCE(cfile->kn) NULL check to skip torn-down files.
2. timer_pending() check to skip when a deferred notification timer is already armed.

Both checks have safe error directions -- a stale read can only cause unnecessary lock acquisition, never a missed notification.

Annotate cfile->kn write sites with WRITE_ONCE() to pair with the lockless reader.

---
 kernel/cgroup/cgroup.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
kernel/cgroup/cgroup.c +8 -2
index 2b298a2cf206..6e816d27ee25 100644
@@ -1749,7 +1749,7 @@ static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
 		struct cgroup_file *cfile = (void *)css + cft->file_offset;
 
 		spin_lock_irq(&cgroup_file_kn_lock);
-		cfile->kn = NULL;
+		WRITE_ONCE(cfile->kn, NULL);
 		spin_unlock_irq(&cgroup_file_kn_lock);
 
 		timer_delete_sync(&cfile->notify_timer);
@@ -4430,7 +4430,7 @@ static int cgroup_add_file(struct cgroup_subsys_state *css, struct cgroup *cgrp,
 		timer_setup(&cfile->notify_timer, cgroup_file_notify_timer, 0);
 
 		spin_lock_irq(&cgroup_file_kn_lock);
-		cfile->kn = kn;
+		WRITE_ONCE(cfile->kn, kn);
 		spin_unlock_irq(&cgroup_file_kn_lock);
 	}
 
@@ -4689,6 +4689,12 @@ void cgroup_file_notify(struct cgroup_file *cfile)
 	unsigned long flags;
 	struct kernfs_node *kn = NULL;
 
+	if (!READ_ONCE(cfile->kn))
+		return;
+
+	if (timer_pending(&cfile->notify_timer))
+		return;
+
 	spin_lock_irqsave(&cgroup_file_kn_lock, flags);
 	if (cfile->kn) {
 		unsigned long last = cfile->notified_at;
Signature · 1 line
2.47.3
#6 lore raw

Re: [PATCH 2/3] cgroup: add lockless fast-path checks to cgroup_file_notify()

Quoted 75 lines · Shakeel Butt
Quoted 4 lines · Shakeel Butt

Hi Chen, thanks for taking a look.

Quoted 1 line · Chen Ridong

[...]

Quoted 13 lines
Quoted 9 lines

kernel/cgroup/cgroup.c void cgroup_file_notify(struct cgroup_file *cfile) · line 4694

Quoted 4 lines
kernel/cgroup/cgroup.c +4 -0
+	last = READ_ONCE(cfile->notified_at);
+	if (time_before_eq(jiffies, last + CGROUP_FILE_NOTIFY_MIN_INTV))
+		return;
+

Previously, if a notification arrived within the rate-limit window, we would still call timer_reduce(&cfile->notify_timer, next) to schedule a deferred notification.

Quoted 3 lines

With this change, returning early here bypasses that timer scheduling entirely. Does this risk missing notifications that would have been delivered by the timer?

Quoted 7 lines

You are indeed right that this can cause missed notifications. After giving some thought I think the lockless check-and-return can be pretty much simplified to timer_pending() check. If timer is active, just do nothing and the notification will be delivered eventually.

I will send the updated version soon. Any comments on the other two patches?

Something like the following:

Quoted 1 line

From 598199723b50813b015393122796f6775eee02d7 Mon Sep 17 00:00:00 2001

From: Shakeel Butt <[email protected]>
Date: Sat, 28 Feb 2026 04:01:28 -0800
Subject: [PATCH] cgroup: add lockless fast-path checks to cgroup_file_notify()

Add two lockless checks before acquiring the lock:

1. READ_ONCE(cfile->kn) NULL check to skip torn-down files.
2. timer_pending() check to skip when a deferred notification timer is already armed.

Both checks have safe error directions -- a stale read can only cause unnecessary lock acquisition, never a missed notification.

Annotate cfile->kn write sites with WRITE_ONCE() to pair with the lockless reader.

---
 kernel/cgroup/cgroup.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
kernel/cgroup/cgroup.c +2 -2
index 2b298a2cf206..6e816d27ee25 100644
@@ -1749,7 +1749,7 @@ static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
 		struct cgroup_file *cfile = (void *)css + cft->file_offset;
 
 		spin_lock_irq(&cgroup_file_kn_lock);
-		cfile->kn = NULL;
+		WRITE_ONCE(cfile->kn, NULL);
 		spin_unlock_irq(&cgroup_file_kn_lock);
 
 		timer_delete_sync(&cfile->notify_timer);
@@ -4430,7 +4430,7 @@ static int cgroup_add_file(struct cgroup_subsys_state *css, struct cgroup *cgrp,
 		timer_setup(&cfile->notify_timer, cgroup_file_notify_timer, 0);
 
 		spin_lock_irq(&cgroup_file_kn_lock);
-		cfile->kn = kn;
+		WRITE_ONCE(cfile->kn, kn);
 		spin_unlock_irq(&cgroup_file_kn_lock);
 	}
 

kernel/cgroup/cgroup.c void cgroup_file_notify(struct cgroup_file *cfile) · line 4697

Quoted 9 lines
kernel/cgroup/cgroup.c +6 -0
@@ -4689,6 +4689,12 @@ void cgroup_file_notify(struct cgroup_file *cfile)
 	unsigned long flags;
 	struct kernfs_node *kn = NULL;
 
+	if (!READ_ONCE(cfile->kn))
+		return;
+
+	if (timer_pending(&cfile->notify_timer))
+		return;
+

The added timer_pending() check seems problematic. According to the function's comment, callers must ensure serialization with other timer operations. Here we're checking timer_pending() locklessly, which means we might:

1. See an inconsistent state if another CPU is concurrently modifying the timer

2. Race with del_timer() or mod_timer() from other contexts

Quoted 3 lines
kernel/cgroup/cgroup.c +0 -0
 	spin_lock_irqsave(&cgroup_file_kn_lock, flags);
 	if (cfile->kn) {
 		unsigned long last = cfile->notified_at;
Signature · 2 lines
Best regards,
Ridong
#7 lore raw

Re: [PATCH 2/3] cgroup: add lockless fast-path checks to cgroup_file_notify()

Quoted 2 lines · Chen Ridong

[...]

Quoted 4 lines
Quoted 4 lines
kernel/cgroup/cgroup.c +0 -0
@@ -4689,6 +4689,12 @@ void cgroup_file_notify(struct cgroup_file *cfile)
 	unsigned long flags;
 	struct kernfs_node *kn = NULL;
 
Quoted 11 lines

kernel/cgroup/cgroup.c void cgroup_file_notify(struct cgroup_file *cfile) · line 4697

Quoted 6 lines
kernel/cgroup/cgroup.c +6 -0
+	if (!READ_ONCE(cfile->kn))
+		return;
+
+	if (timer_pending(&cfile->notify_timer))
+		return;
+

The added timer_pending() check seems problematic. According to the function's comment, callers must ensure serialization with other timer operations. Here we're checking timer_pending() locklessly, which means we might:

That comment seems outdated. Check the commit 90c018942c2ba ("timer: Use hlist_unhashed_lockless() in timer_pending()").

Quoted 2 lines

1. See an inconsistent state if another CPU is concurrently modifying the timer

It will not see inconsistent state but it can see stale state which is totally fine. At worst we will take the lock and recheck but we will never miss the notifications.

Quoted 10 lines

2. Race with del_timer() or mod_timer() from other contexts

Quoted 3 lines
kernel/cgroup/cgroup.c +0 -0
 	spin_lock_irqsave(&cgroup_file_kn_lock, flags);
 	if (cfile->kn) {
 		unsigned long last = cfile->notified_at;
Signature · 2 lines
Best regards,
Ridong
#8 lore raw

Re: [PATCH 2/3] cgroup: add lockless fast-path checks to cgroup_file_notify()

Quoted 20 lines · Shakeel Butt
Quoted 2 lines · Chen Ridong

[...]

Quoted 4 lines
Quoted 4 lines
kernel/cgroup/cgroup.c +0 -0
@@ -4689,6 +4689,12 @@ void cgroup_file_notify(struct cgroup_file *cfile)
 	unsigned long flags;
 	struct kernfs_node *kn = NULL;
 
Quoted 11 lines

kernel/cgroup/cgroup.c void cgroup_file_notify(struct cgroup_file *cfile) · line 4697

Quoted 6 lines
kernel/cgroup/cgroup.c +6 -0
+	if (!READ_ONCE(cfile->kn))
+		return;
+
+	if (timer_pending(&cfile->notify_timer))
+		return;
+

The added timer_pending() check seems problematic. According to the function's comment, callers must ensure serialization with other timer operations. Here we're checking timer_pending() locklessly, which means we might:

Quoted 3 lines

That comment seems outdated. Check the commit 90c018942c2ba ("timer: Use hlist_unhashed_lockless() in timer_pending()").

Maybe we should update the comment.

Quoted 7 lines
Quoted 2 lines

1. See an inconsistent state if another CPU is concurrently modifying the timer

It will not see inconsistent state but it can see stale state which is totally fine. At worst we will take the lock and recheck but we will never miss the notifications.

You are right. Thank you for explanation.

Quoted 10 lines
Quoted 10 lines

2. Race with del_timer() or mod_timer() from other contexts

Quoted 3 lines
kernel/cgroup/cgroup.c +0 -0
 	spin_lock_irqsave(&cgroup_file_kn_lock, flags);
 	if (cfile->kn) {
 		unsigned long last = cfile->notified_at;
Signature · 2 lines
Best regards,
Ridong
Signature · 2 lines
Best regards,
Ridong
#9 lore raw

Re: [PATCH 2/3] cgroup: add lockless fast-path checks to cgroup_file_notify()

In reply to Shakeel Butt
Quoted 76 lines · Shakeel Butt

Hi Chen, thanks for taking a look.

Quoted 69 lines · Chen Ridong

Hi Shakeel,

Good series to move away from the global lock.

Quoted 48 lines · Shakeel Butt

Add two lockless checks before acquiring the lock:

1. READ_ONCE(cfile->kn) NULL check to skip torn-down files.
2. READ_ONCE(cfile->notified_at) check to skip when within the rate-limit window (~10ms).

Both checks have safe error directions -- a stale read can only cause unnecessary lock acquisition, never a missed notification. Annotate all write sites with WRITE_ONCE() to pair with the lockless readers.

The trade-off is that trailing timer_reduce() calls during bursts are skipped, so the deferred notification that delivers the final state may be lost. This is acceptable for the primary callers like __memcg_memory_event() where events keep arriving.

---
 kernel/cgroup/cgroup.c | 21 ++++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)
kernel/cgroup/cgroup.c +2 -3
index 33282c7d71e4..5473ebd0f6c1 100644
@@ -1749,7 +1749,7 @@ static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
 		struct cgroup_file *cfile = (void *)css + cft->file_offset;
 
 		spin_lock_irq(&cgroup_file_kn_lock);
-		cfile->kn = NULL;
+		WRITE_ONCE(cfile->kn, NULL);
 		spin_unlock_irq(&cgroup_file_kn_lock);
 
 		timer_delete_sync(&cfile->notify_timer);
@@ -4430,7 +4430,7 @@ static int cgroup_add_file(struct cgroup_subsys_state *css, struct cgroup *cgrp,
 		timer_setup(&cfile->notify_timer, cgroup_file_notify_timer, 0);
 
 		spin_lock_irq(&cgroup_file_kn_lock);
-		cfile->kn = kn;
+		WRITE_ONCE(cfile->kn, kn);
 		spin_unlock_irq(&cgroup_file_kn_lock);
 	}
 
@@ -4686,20 +4686,27 @@ int cgroup_add_legacy_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
  */
 void cgroup_file_notify(struct cgroup_file *cfile)
 {
-	unsigned long flags;

kernel/cgroup/cgroup.c int cgroup_add_legacy_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) · line 4698

Quoted 10 lines
kernel/cgroup/cgroup.c +8 -0
+	unsigned long flags, last, next;
 	struct kernfs_node *kn = NULL;
 
+	if (!READ_ONCE(cfile->kn))
+		return;
+
+	last = READ_ONCE(cfile->notified_at);
+	if (time_before_eq(jiffies, last + CGROUP_FILE_NOTIFY_MIN_INTV))
+		return;
+

Previously, if a notification arrived within the rate-limit window, we would still call timer_reduce(&cfile->notify_timer, next) to schedule a deferred notification.

Quoted 3 lines

With this change, returning early here bypasses that timer scheduling entirely. Does this risk missing notifications that would have been delivered by the timer?

Quoted 7 lines

You are indeed right that this can cause missed notifications. After giving some thought I think the lockless check-and-return can be pretty much simplified to timer_pending() check. If timer is active, just do nothing and the notification will be delivered eventually.

I will send the updated version soon. Any comments on the other two patches?

The other two patches are fine to me.

Signature · 2 lines
Best regards,
Ridong
#10 lore raw

PATCH 3/3 cgroup: replace global cgroup_file_kn_lock with per-cgroup_file lock

In reply to Shakeel Butt

Replace the global cgroup_file_kn_lock with a per-cgroup_file spinlock to eliminate cross-cgroup contention as it is not really protecting data shared between different cgroups.

The lock is initialized in cgroup_add_file() alongside timer_setup(). No lock acquisition is needed during initialization since the cgroup directory is being populated under cgroup_mutex and no concurrent accessors exist at that point.

---
 include/linux/cgroup-defs.h |  1 +
 kernel/cgroup/cgroup.c      | 24 ++++++++----------------
 2 files changed, 9 insertions(+), 16 deletions(-)
include/linux/cgroup-defs.h +1 -0
index bb92f5c169ca..ba26b5d05ce3 100644
@@ -167,6 +167,7 @@ struct cgroup_file {
 	struct kernfs_node *kn;
 	unsigned long notified_at;
 	struct timer_list notify_timer;
+	spinlock_t lock;
 };
 
 /*
kernel/cgroup/cgroup.c +8 -16
index 5473ebd0f6c1..b502acad3c5c 100644
@@ -107,12 +107,6 @@ static bool cgroup_debug __read_mostly;
  */
 static DEFINE_SPINLOCK(cgroup_idr_lock);
 
-/*
- * Protects cgroup_file->kn for !self csses.  It synchronizes notifications
- * against file removal/re-creation across css hiding.
- */
-static DEFINE_SPINLOCK(cgroup_file_kn_lock);
-
 DEFINE_PERCPU_RWSEM(cgroup_threadgroup_rwsem);
 
 #define cgroup_assert_mutex_or_rcu_locked()				\
@@ -1748,9 +1742,9 @@ static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
 		struct cgroup_subsys_state *css = cgroup_css(cgrp, cft->ss);
 		struct cgroup_file *cfile = (void *)css + cft->file_offset;
 
-		spin_lock_irq(&cgroup_file_kn_lock);
+		spin_lock_irq(&cfile->lock);
 		WRITE_ONCE(cfile->kn, NULL);
-		spin_unlock_irq(&cgroup_file_kn_lock);
+		spin_unlock_irq(&cfile->lock);
 
 		timer_delete_sync(&cfile->notify_timer);
 	}
@@ -4428,10 +4422,8 @@ static int cgroup_add_file(struct cgroup_subsys_state *css, struct cgroup *cgrp,
 		struct cgroup_file *cfile = (void *)css + cft->file_offset;
 
 		timer_setup(&cfile->notify_timer, cgroup_file_notify_timer, 0);
-
-		spin_lock_irq(&cgroup_file_kn_lock);
-		WRITE_ONCE(cfile->kn, kn);
-		spin_unlock_irq(&cgroup_file_kn_lock);
+		spin_lock_init(&cfile->lock);
+		cfile->kn = kn;
 	}
 
 	return 0;
@@ -4696,7 +4688,7 @@ void cgroup_file_notify(struct cgroup_file *cfile)
 	if (time_before_eq(jiffies, last + CGROUP_FILE_NOTIFY_MIN_INTV))
 		return;
 
-	spin_lock_irqsave(&cgroup_file_kn_lock, flags);
+	spin_lock_irqsave(&cfile->lock, flags);
 	if (cfile->kn) {
 		last = cfile->notified_at;
 		next = last + CGROUP_FILE_NOTIFY_MIN_INTV;
@@ -4709,7 +4701,7 @@ void cgroup_file_notify(struct cgroup_file *cfile)
 			WRITE_ONCE(cfile->notified_at, jiffies);
 		}
 	}
-	spin_unlock_irqrestore(&cgroup_file_kn_lock, flags);
+	spin_unlock_irqrestore(&cfile->lock, flags);
 
 	if (kn) {
 		kernfs_notify(kn);
@@ -4727,10 +4719,10 @@ void cgroup_file_show(struct cgroup_file *cfile, bool show)
 {
 	struct kernfs_node *kn;
 
-	spin_lock_irq(&cgroup_file_kn_lock);
+	spin_lock_irq(&cfile->lock);
 	kn = cfile->kn;
 	kernfs_get(kn);
-	spin_unlock_irq(&cgroup_file_kn_lock);
+	spin_unlock_irq(&cfile->lock);
 
 	if (kn)
 		kernfs_show(kn, show);
Signature · 1 line
2.47.3