Skip to content

Commit b8f35fa

Browse files
imran-kngregkh
authored andcommitted
kernfs: Change kernfs_notify_list to llist.
At present kernfs_notify_list is implemented as a singly linked list of kernfs_node(s), where last element points to itself and value of ->attr.next tells if node is present on the list or not. Both addition and deletion to list happen under kernfs_notify_lock. Change kernfs_notify_list to llist so that addition to list can heppen locklessly. Suggested by: Al Viro <[email protected]> Acked-by: Tejun Heo <[email protected]> Signed-off-by: Imran Khan <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 086c00c commit b8f35fa

File tree

2 files changed

+21
-28
lines changed

2 files changed

+21
-28
lines changed

fs/kernfs/file.c

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,16 @@ struct kernfs_open_node {
3838
struct list_head files; /* goes through kernfs_open_file.list */
3939
};
4040

41-
/*
42-
* kernfs_notify() may be called from any context and bounces notifications
43-
* through a work item. To minimize space overhead in kernfs_node, the
44-
* pending queue is implemented as a singly linked list of kernfs_nodes.
45-
* The list is terminated with the self pointer so that whether a
46-
* kernfs_node is on the list or not can be determined by testing the next
47-
* pointer for NULL.
41+
/**
42+
* attribute_to_node - get kernfs_node object corresponding to a kernfs attribute
43+
* @ptr: &struct kernfs_elem_attr
44+
* @type: struct kernfs_node
45+
* @member: name of member (i.e attr)
4846
*/
49-
#define KERNFS_NOTIFY_EOL ((void *)&kernfs_notify_list)
47+
#define attribute_to_node(ptr, type, member) \
48+
container_of(ptr, type, member)
5049

51-
static DEFINE_SPINLOCK(kernfs_notify_lock);
52-
static struct kernfs_node *kernfs_notify_list = KERNFS_NOTIFY_EOL;
50+
static LLIST_HEAD(kernfs_notify_list);
5351

5452
/**
5553
* kernfs_deref_open_node - Get kernfs_open_node corresponding to @kn.
@@ -902,18 +900,16 @@ static void kernfs_notify_workfn(struct work_struct *work)
902900
struct kernfs_node *kn;
903901
struct kernfs_super_info *info;
904902
struct kernfs_root *root;
903+
struct llist_node *free;
904+
struct kernfs_elem_attr *attr;
905905
repeat:
906906
/* pop one off the notify_list */
907-
spin_lock_irq(&kernfs_notify_lock);
908-
kn = kernfs_notify_list;
909-
if (kn == KERNFS_NOTIFY_EOL) {
910-
spin_unlock_irq(&kernfs_notify_lock);
907+
free = llist_del_first(&kernfs_notify_list);
908+
if (free == NULL)
911909
return;
912-
}
913-
kernfs_notify_list = kn->attr.notify_next;
914-
kn->attr.notify_next = NULL;
915-
spin_unlock_irq(&kernfs_notify_lock);
916910

911+
attr = llist_entry(free, struct kernfs_elem_attr, notify_next);
912+
kn = attribute_to_node(attr, struct kernfs_node, attr);
917913
root = kernfs_root(kn);
918914
/* kick fsnotify */
919915
down_write(&root->kernfs_rwsem);
@@ -969,12 +965,14 @@ static void kernfs_notify_workfn(struct work_struct *work)
969965
void kernfs_notify(struct kernfs_node *kn)
970966
{
971967
static DECLARE_WORK(kernfs_notify_work, kernfs_notify_workfn);
972-
unsigned long flags;
973968
struct kernfs_open_node *on;
974969

975970
if (WARN_ON(kernfs_type(kn) != KERNFS_FILE))
976971
return;
977972

973+
/* Because we are using llist for kernfs_notify_list */
974+
WARN_ON_ONCE(in_nmi());
975+
978976
/* kick poll immediately */
979977
rcu_read_lock();
980978
on = rcu_dereference(kn->attr.open);
@@ -985,14 +983,9 @@ void kernfs_notify(struct kernfs_node *kn)
985983
rcu_read_unlock();
986984

987985
/* schedule work to kick fsnotify */
988-
spin_lock_irqsave(&kernfs_notify_lock, flags);
989-
if (!kn->attr.notify_next) {
990-
kernfs_get(kn);
991-
kn->attr.notify_next = kernfs_notify_list;
992-
kernfs_notify_list = kn;
993-
schedule_work(&kernfs_notify_work);
994-
}
995-
spin_unlock_irqrestore(&kernfs_notify_lock, flags);
986+
kernfs_get(kn);
987+
llist_add(&kn->attr.notify_next, &kernfs_notify_list);
988+
schedule_work(&kernfs_notify_work);
996989
}
997990
EXPORT_SYMBOL_GPL(kernfs_notify);
998991

include/linux/kernfs.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ struct kernfs_elem_attr {
116116
const struct kernfs_ops *ops;
117117
struct kernfs_open_node __rcu *open;
118118
loff_t size;
119-
struct kernfs_node *notify_next; /* for kernfs_notify() */
119+
struct llist_node notify_next; /* for kernfs_notify() */
120120
};
121121

122122
/*

0 commit comments

Comments
 (0)