Skip to contentSkip to conversation

kvfree_rcu() miscellaneous fixes

PATCH In review Uladzislau Rezki (Sony) 2 participants 8 messages Apr 15, 2021 – Apr 16, 2021

Subsystems (2)
lore
Share & tools
lore

Uladzislau Rezki (Sony)

author maintainer
#0 lore raw

v2 cover kvfree_rcu() miscellaneous fixes

This is a v2 of a small series. See the changelog below:

V1 -> V2:
- document the rcu_delay_page_cache_fill_msec parameter;
- drop the "kvfree_rcu: introduce "flags" variable" patch;
- reword commit messages;
- in the patch [1], do not use READ_ONCE() instances in get_cached_bnode()/put_cached_bnode() it is protected by the lock.
- Capitalize the word following by ":" in commit messages.

Uladzislau Rezki (Sony) (4):

[1]  kvfree_rcu: Use [READ/WRITE]_ONCE() macros to access to nr_bkv_objs
[2]  kvfree_rcu: Add a bulk-list check when a scheduler is run
[3]  kvfree_rcu: Update "monitor_todo" once a batch is started
[4]  kvfree_rcu: Use kfree_rcu_monitor() instead of open-coded variant

Zhang Qiang (1):
[5] kvfree_rcu: Release a page cache under memory pressure

 .../admin-guide/kernel-parameters.txt         |  5 +
 kernel/rcu/tree.c                             | 92 +++++++++++++++----
 2 files changed, 77 insertions(+), 20 deletions(-)
Signature · 1 line
2.20.1
author maintainer
#2 lore raw

PATCH v2 2/5 kvfree_rcu: Use [READ/WRITE]_ONCE() macros to access to nr_bkv_objs

nr_bkv_objs represents the counter of objects in the page-cache. Accessing to it requires taking the lock. Switch to READ_ONCE() WRITE_ONCE() macros to provide an atomic access to that counter. A shrinker is one of the user of it.

---
 kernel/rcu/tree.c | 14 ++++----------
 1 file changed, 4 insertions(+), 10 deletions(-)
kernel/rcu/tree.c +4 -10
index 742152d6b952..07e718fdea12 100644
@@ -3223,7 +3223,7 @@ get_cached_bnode(struct kfree_rcu_cpu *krcp)
 	if (!krcp->nr_bkv_objs)
 		return NULL;
 
-	krcp->nr_bkv_objs--;
+	WRITE_ONCE(krcp->nr_bkv_objs, krcp->nr_bkv_objs - 1);
 	return (struct kvfree_rcu_bulk_data *)
 		llist_del_first(&krcp->bkvcache);
 }
@@ -3237,9 +3237,8 @@ put_cached_bnode(struct kfree_rcu_cpu *krcp,
 		return false;
 
 	llist_add((struct llist_node *) bnode, &krcp->bkvcache);
-	krcp->nr_bkv_objs++;
+	WRITE_ONCE(krcp->nr_bkv_objs, krcp->nr_bkv_objs + 1);
 	return true;
-
 }
 
 static int
@@ -3251,7 +3250,7 @@ drain_page_cache(struct kfree_rcu_cpu *krcp)
 
 	raw_spin_lock_irqsave(&krcp->lock, flags);
 	page_list = llist_del_all(&krcp->bkvcache);
-	krcp->nr_bkv_objs = 0;
+	WRITE_ONCE(krcp->nr_bkv_objs, 0);
 	raw_spin_unlock_irqrestore(&krcp->lock, flags);
 
 	llist_for_each_safe(pos, n, page_list) {
@@ -3655,18 +3654,13 @@ kfree_rcu_shrink_count(struct shrinker *shrink, struct shrink_control *sc)
 {
 	int cpu;
 	unsigned long count = 0;
-	unsigned long flags;
 
 	/* Snapshot count of all CPUs */
 	for_each_possible_cpu(cpu) {
 		struct kfree_rcu_cpu *krcp = per_cpu_ptr(&krc, cpu);
 
 		count += READ_ONCE(krcp->count);
-
-		raw_spin_lock_irqsave(&krcp->lock, flags);
-		count += krcp->nr_bkv_objs;
-		raw_spin_unlock_irqrestore(&krcp->lock, flags);
-
+		count += READ_ONCE(krcp->nr_bkv_objs);
 		atomic_set(&krcp->backoff_page_cache_fill, 1);
 	}
 
Signature · 1 line
2.20.1
author maintainer
#1 lore raw

PATCH v2 1/5 kvfree_rcu: Release a page cache under memory pressure

In reply to Uladzislau Rezki (Sony)

From: Zhang Qiang <[email protected]>

Add a drain_page_cache() function to drain a per-cpu page cache. The reason behind of it is a system can run into a low memory condition, in that case a page shrinker can ask for its users to free their caches in order to get extra memory available for other needs in a system.

When a system hits such condition, a page cache is drained for all CPUs in a system. By default a page cache work is delayed with 5 seconds interval until a memory pressure disappears, if needed it can be changed. See a rcu_delay_page_cache_fill_msec module parameter.

---
 .../admin-guide/kernel-parameters.txt         |  5 ++
 kernel/rcu/tree.c                             | 82 +++++++++++++++++--
 2 files changed, 78 insertions(+), 9 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +5 -0
index 78dc87435ca7..6b769f5cf14c 100644
@@ -4154,6 +4154,11 @@
 			whole algorithm to behave better in low memory
 			condition.
 
+	rcutree.rcu_delay_page_cache_fill_msec= [KNL]
+			Set delay for a page-cache refill when a low memory
+			condition occurs. That is in milliseconds. Allowed
+			value is within a 0:100000 range.
+
 	rcutree.jiffies_till_first_fqs= [KNL]
 			Set delay from grace-period initialization to
 			first attempt to force quiescent states.
kernel/rcu/tree.c +73 -9

164 lines not on this page · raw message

Signature · 1 line
2.20.1
author maintainer
#3 lore raw

PATCH v2 3/5 kvfree_rcu: Add a bulk-list check when a scheduler is run

In reply to Uladzislau Rezki (Sony)

RCU_SCHEDULER_RUNNING is set when a scheduling is available. That signal is used in order to check and queue a "monitor work" to reclaim freed objects(if they are) during a boot-up phase.

We have it because, the main path of the kvfree_rcu() call can not queue the work untill the scheduler is up and running.

Currently in such helper only "krcp->head" is checked to figure out if there are outstanding objects to be released. And this is only one channel. After adding a bulk interface there are two extra which have to be checked also: "krcp->bkvhead[0]" as well as "krcp->bkvhead[1]". So, we have to queue the "monitor work" if _any_ corresponding channel is not empty.

---
 kernel/rcu/tree.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/rcu/tree.c +2 -1
index 07e718fdea12..3ddc9dc97487 100644
@@ -3712,7 +3712,8 @@ void __init kfree_rcu_scheduler_running(void)
 		struct kfree_rcu_cpu *krcp = per_cpu_ptr(&krc, cpu);
 
 		raw_spin_lock_irqsave(&krcp->lock, flags);
-		if (!krcp->head || krcp->monitor_todo) {
+		if ((!krcp->bkvhead[0] && !krcp->bkvhead[1] && !krcp->head) ||
+				krcp->monitor_todo) {
 			raw_spin_unlock_irqrestore(&krcp->lock, flags);
 			continue;
 		}
Signature · 1 line
2.20.1
author maintainer
#5 lore raw

PATCH v2 5/5 kvfree_rcu: Use kfree_rcu_monitor() instead of open-coded variant

In reply to Uladzislau Rezki (Sony)

To queue a new batch we have a kfree_rcu_monitor() function that checks "monitor_todo" var. and invokes kfree_rcu_drain_unlock() to start a new batch after a GP. Get rid of open-coded case by switching it to the separate function.

---
 kernel/rcu/tree.c | 8 +-------
 1 file changed, 1 insertion(+), 7 deletions(-)
kernel/rcu/tree.c +1 -7
index 17c128d93825..b3e04c4fefcf 100644
@@ -3670,7 +3670,6 @@ static unsigned long
 kfree_rcu_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
 {
 	int cpu, freed = 0;
-	unsigned long flags;
 
 	for_each_possible_cpu(cpu) {
 		int count;
@@ -3678,12 +3677,7 @@ kfree_rcu_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
 
 		count = krcp->count;
 		count += drain_page_cache(krcp);
-
-		raw_spin_lock_irqsave(&krcp->lock, flags);
-		if (krcp->monitor_todo)
-			kfree_rcu_drain_unlock(krcp, flags);
-		else
-			raw_spin_unlock_irqrestore(&krcp->lock, flags);
+		kfree_rcu_monitor(&krcp->monitor_work.work);
 
 		sc->nr_to_scan -= count;
 		freed += count;
Signature · 1 line
2.20.1
author maintainer
#4 lore raw

PATCH v2 4/5 kvfree_rcu: Update "monitor_todo" once a batch is started

In reply to Uladzislau Rezki (Sony)

Before attempting of starting a new batch a "monitor_todo" var. is set to "false" and set back to "true" when a previous RCU batch is still in progress.

Drop it to "false" only when a new batch has been successfully queued, if not, it stays active anyway. There is no reason in setting it force and back.

---
 kernel/rcu/tree.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
kernel/rcu/tree.c +1 -2
index 3ddc9dc97487..17c128d93825 100644
@@ -3415,15 +3415,14 @@ static inline void kfree_rcu_drain_unlock(struct kfree_rcu_cpu *krcp,
 					  unsigned long flags)
 {
 	// Attempt to start a new batch.
-	krcp->monitor_todo = false;
 	if (queue_kfree_rcu_work(krcp)) {
 		// Success! Our job is done here.
+		krcp->monitor_todo = false;
 		raw_spin_unlock_irqrestore(&krcp->lock, flags);
 		return;
 	}
 
 	// Previous RCU batch still in progress, try again later.
-	krcp->monitor_todo = true;
 	schedule_delayed_work(&krcp->monitor_work, KFREE_DRAIN_JIFFIES);
 	raw_spin_unlock_irqrestore(&krcp->lock, flags);
 }
Signature · 1 line
2.20.1
maintainer
#6 lore raw
In reply to Uladzislau Rezki (Sony)
Quoted 11 lines · Uladzislau Rezki (Sony)

This is a v2 of a small series. See the changelog below:

V1 -> V2:
- document the rcu_delay_page_cache_fill_msec parameter;
- drop the "kvfree_rcu: introduce "flags" variable" patch;
- reword commit messages;
- in the patch [1], do not use READ_ONCE() instances in get_cached_bnode()/put_cached_bnode() it is protected by the lock.
- Capitalize the word following by ":" in commit messages.

Quoted 8 lines

Uladzislau Rezki (Sony) (4):

[1]  kvfree_rcu: Use [READ/WRITE]_ONCE() macros to access to nr_bkv_objs
[2]  kvfree_rcu: Add a bulk-list check when a scheduler is run
[3]  kvfree_rcu: Update "monitor_todo" once a batch is started
[4]  kvfree_rcu: Use kfree_rcu_monitor() instead of open-coded variant

Zhang Qiang (1):
[5] kvfree_rcu: Release a page cache under memory pressure

I have queued these, thank you both! And they pass touch tests, but could you please check that "git am -3" correctly resolved a couple of conflicts, one in Documentation/admin-guide/kernel-parameters.txt and the other in kernel/rcu/tree.c?

Thanx, Paul

Quoted 7 lines
 .../admin-guide/kernel-parameters.txt         |  5 +
 kernel/rcu/tree.c                             | 92 +++++++++++++++----
 2 files changed, 77 insertions(+), 20 deletions(-)
Signature · 1 line
2.20.1
author maintainer
#7 lore raw
Quoted 18 lines · Paul E. McKenney
Quoted 17 lines · Uladzislau Rezki (Sony)

This is a v2 of a small series. See the changelog below:

V1 -> V2:
- document the rcu_delay_page_cache_fill_msec parameter;
- drop the "kvfree_rcu: introduce "flags" variable" patch;
- reword commit messages;
- in the patch [1], do not use READ_ONCE() instances in get_cached_bnode()/put_cached_bnode() it is protected by the lock.
- Capitalize the word following by ":" in commit messages.

Uladzislau Rezki (Sony) (4):

[1]  kvfree_rcu: Use [READ/WRITE]_ONCE() macros to access to nr_bkv_objs
[2]  kvfree_rcu: Add a bulk-list check when a scheduler is run
[3]  kvfree_rcu: Update "monitor_todo" once a batch is started
[4]  kvfree_rcu: Use kfree_rcu_monitor() instead of open-coded variant
Quoted 8 lines
Quoted 2 lines

Zhang Qiang (1):
[5] kvfree_rcu: Release a page cache under memory pressure

I have queued these, thank you both! And they pass touch tests, but could you please check that "git am -3" correctly resolved a couple of conflicts, one in Documentation/admin-guide/kernel-parameters.txt and the other in kernel/rcu/tree.c?

Thanks!

I have double checked it. I see that everything is in place and has been correctly applied on your latest "dev".

--
Vlad Rezki