Skip to content

Commit 210272a

Browse files
Matthew Wilcoxgregkh
Matthew Wilcox
authored andcommitted
driver core: Remove completion from struct klist_node
Removing the completion from klist_node reduces its size from 64 bytes to 28 on x86-64. To maintain the semantics of klist_remove(), we add a single list of klist nodes which are pending deletion and scan them. Signed-off-by: Matthew Wilcox <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 929d2fa commit 210272a

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

include/linux/klist.h

-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#define _LINUX_KLIST_H
1414

1515
#include <linux/spinlock.h>
16-
#include <linux/completion.h>
1716
#include <linux/kref.h>
1817
#include <linux/list.h>
1918

@@ -41,7 +40,6 @@ struct klist_node {
4140
void *n_klist; /* never access directly */
4241
struct list_head n_node;
4342
struct kref n_ref;
44-
struct completion n_removed;
4543
};
4644

4745
extern void klist_add_tail(struct klist_node *n, struct klist *k);

lib/klist.c

+40-3
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636

3737
#include <linux/klist.h>
3838
#include <linux/module.h>
39+
#include <linux/sched.h>
3940

4041
/*
4142
* Use the lowest bit of n_klist to mark deleted nodes and exclude
@@ -108,7 +109,6 @@ static void add_tail(struct klist *k, struct klist_node *n)
108109
static void klist_node_init(struct klist *k, struct klist_node *n)
109110
{
110111
INIT_LIST_HEAD(&n->n_node);
111-
init_completion(&n->n_removed);
112112
kref_init(&n->n_ref);
113113
knode_set_klist(n, k);
114114
if (k->get)
@@ -171,13 +171,34 @@ void klist_add_before(struct klist_node *n, struct klist_node *pos)
171171
}
172172
EXPORT_SYMBOL_GPL(klist_add_before);
173173

174+
struct klist_waiter {
175+
struct list_head list;
176+
struct klist_node *node;
177+
struct task_struct *process;
178+
int woken;
179+
};
180+
181+
static DEFINE_SPINLOCK(klist_remove_lock);
182+
static LIST_HEAD(klist_remove_waiters);
183+
174184
static void klist_release(struct kref *kref)
175185
{
186+
struct klist_waiter *waiter, *tmp;
176187
struct klist_node *n = container_of(kref, struct klist_node, n_ref);
177188

178189
WARN_ON(!knode_dead(n));
179190
list_del(&n->n_node);
180-
complete(&n->n_removed);
191+
spin_lock(&klist_remove_lock);
192+
list_for_each_entry_safe(waiter, tmp, &klist_remove_waiters, list) {
193+
if (waiter->node != n)
194+
continue;
195+
196+
waiter->woken = 1;
197+
mb();
198+
wake_up_process(waiter->process);
199+
list_del(&waiter->list);
200+
}
201+
spin_unlock(&klist_remove_lock);
181202
knode_set_klist(n, NULL);
182203
}
183204

@@ -217,8 +238,24 @@ EXPORT_SYMBOL_GPL(klist_del);
217238
*/
218239
void klist_remove(struct klist_node *n)
219240
{
241+
struct klist_waiter waiter;
242+
243+
waiter.node = n;
244+
waiter.process = current;
245+
waiter.woken = 0;
246+
spin_lock(&klist_remove_lock);
247+
list_add(&waiter.list, &klist_remove_waiters);
248+
spin_unlock(&klist_remove_lock);
249+
220250
klist_del(n);
221-
wait_for_completion(&n->n_removed);
251+
252+
for (;;) {
253+
set_current_state(TASK_UNINTERRUPTIBLE);
254+
if (waiter.woken)
255+
break;
256+
schedule();
257+
}
258+
__set_current_state(TASK_RUNNING);
222259
}
223260
EXPORT_SYMBOL_GPL(klist_remove);
224261

0 commit comments

Comments
 (0)