Skip to contentSkip to conversation

slob: introduce __kfree_rcu

PATCH In review Lai Jiangshan 1 participant 1 message Mar 3, 2009 Memory management

lore
Share & tools
lore
#0 lore raw

PATCH 2/6 slob: introduce __kfree_rcu

Introduce __kfree_rcu() for kfree_rcu()

The object pointer is stored in head->func instead of the function pointer, and these rcu head(which are the same batch) are queued in a list. When a batch's grace period is completed, the objects in this batch are freed, and then we proccess the next batch(if next batch not empty).

---

mm/slob.c +51 -0
index 52bc8a2..e703295 100644
@@ -618,6 +618,57 @@ unsigned int kmem_cache_size(struct kmem_cache *c)
 }
 EXPORT_SYMBOL(kmem_cache_size);
 
+static DEFINE_SPINLOCK(kfree_rcu_lock);
+static struct rcu_head kfree_rcu_head;
+static struct rcu_head *curr_head;
+static struct rcu_head *next_head, **next_tail = &next_head;
+static void kfree_rcu_advance_batch(void);
+
+static void kfree_rcu_batch_callback(struct rcu_head *unused)
+{
+	unsigned long flags;
+	struct rcu_head *list;
+
+	spin_lock_irqsave(&kfree_rcu_lock, flags);
+	list = curr_head;
+	curr_head = NULL;
+	kfree_rcu_advance_batch();
+	spin_unlock_irqrestore(&kfree_rcu_lock, flags);
+
+	while (list) {
+		struct rcu_head *next = list->next;
+		prefetch(next);
+		kfree((void *)(unsigned long)list->func);
+		list = next;
+	}
+}
+
+static void kfree_rcu_advance_batch(void)
+{
+	if (!curr_head && next_head) {
+		curr_head = next_head;
+		next_head = NULL;
+		next_tail = &next_head;
+
+		call_rcu(&kfree_rcu_head, kfree_rcu_batch_callback);
+	}
+}
+
+void __kfree_rcu(const void *obj, struct rcu_head *rcu)
+{
+	unsigned long flags;
+
+	rcu->func = (typeof(rcu->func))(unsigned long)obj;
+	rcu->next = NULL;
+
+	spin_lock_irqsave(&kfree_rcu_lock, flags);
+	*next_tail = rcu;
+	next_tail = &rcu->next;
+	kfree_rcu_advance_batch();
+	spin_unlock_irqrestore(&kfree_rcu_lock, flags);
+}
+EXPORT_SYMBOL(__kfree_rcu);
+
 const char *kmem_cache_name(struct kmem_cache *c)
 {
 	return c->name;