Skip to contentSkip to conversation

kvfree_rcu: Release page cache under memory pressure

PATCH Superseded [email protected] 2 participants 2 messages Jan 28, 2021 RCU

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kvfree_rcu: Release page cache under memory pressure

From: Zqiang <[email protected]>

Add free per-cpu existing krcp's page cache operation, when the system is under memory pressure.

---
 kernel/rcu/tree.c | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)
kernel/rcu/tree.c +26 -0
index c1ae1e52f638..4e1c14b12bdd 100644
@@ -3571,17 +3571,41 @@ void kvfree_call_rcu(struct rcu_head *head, rcu_callback_t func)
 }
 EXPORT_SYMBOL_GPL(kvfree_call_rcu);
 
+static inline int free_krc_page_cache(struct kfree_rcu_cpu *krcp)
+{
+	unsigned long flags;
+	struct kvfree_rcu_bulk_data *bnode;
+	int i, num = 0;
+
+	for (i = 0; i < rcu_min_cached_objs; i++) {
+		raw_spin_lock_irqsave(&krcp->lock, flags);
+		bnode = get_cached_bnode(krcp);
+		raw_spin_unlock_irqrestore(&krcp->lock, flags);
+		if (!bnode)
+			break;
+		free_page((unsigned long)bnode);
+		num++;
+	}
+
+	return num;
+}
+
 static unsigned long
 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);
 	}
 
 	return count;
@@ -3604,6 +3628,8 @@ kfree_rcu_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
 		else
 			raw_spin_unlock_irqrestore(&krcp->lock, flags);
 
+		count += free_krc_page_cache(krcp);
+
 		sc->nr_to_scan -= count;
 		freed += count;
 
Signature · 1 line
2.17.1
maintainer
#1 lore raw

Hello, Zqiang.

See below some nits:

Quoted 14 lines

Add free per-cpu existing krcp's page cache operation, when the system is under memory pressure.

---
 kernel/rcu/tree.c | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)
kernel/rcu/tree.c +0 -0
index c1ae1e52f638..4e1c14b12bdd 100644

kernel/rcu/tree.c void kvfree_call_rcu(struct rcu_head *head, rcu_callback_t func) · line 3574

Quoted 4 lines
kernel/rcu/tree.c +1 -0
@@ -3571,17 +3571,41 @@ void kvfree_call_rcu(struct rcu_head *head, rcu_callback_t func)
 }
 EXPORT_SYMBOL_GPL(kvfree_call_rcu);
 
+static inline int free_krc_page_cache(struct kfree_rcu_cpu *krcp)

Do we need it "inlined"?

Quoted 6 lines
kernel/rcu/tree.c +6 -0
+{
+	unsigned long flags;
+	struct kvfree_rcu_bulk_data *bnode;
+	int i, num = 0;
+
+	for (i = 0; i < rcu_min_cached_objs; i++) {

kernel/rcu/tree.c void kvfree_call_rcu(struct rcu_head *head, rcu_callback_t func) · line 3590

Quoted 10 lines
kernel/rcu/tree.c +10 -0
+		raw_spin_lock_irqsave(&krcp->lock, flags);
+		bnode = get_cached_bnode(krcp);
+		raw_spin_unlock_irqrestore(&krcp->lock, flags);
+		if (!bnode)
+			break;
+		free_page((unsigned long)bnode);
+		num++;
+	}
+
+	return num;

Get rid of "num" and return i instead?

Quoted 22 lines
kernel/rcu/tree.c +7 -0
+}
+
 static unsigned long
 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);
 	}
 
 	return count;

kernel/rcu/tree.c kfree_rcu_shrink_scan(struct shrinker *shrink, struct shrink_control *sc) · line 3631

Quoted 4 lines
kernel/rcu/tree.c +1 -0
@@ -3604,6 +3628,8 @@ kfree_rcu_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
 		else
 			raw_spin_unlock_irqrestore(&krcp->lock, flags);
 
+		count += free_krc_page_cache(krcp);

Move it upper right after count = krcp->count;, so a "count" is set in one place what i more readable and clear?

Thank you!

--
Vlad Rezki