Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > linux.kernel > #1444484 > unrolled thread
| Started by | Waiman Long <Waiman.Long@hpe.com> |
|---|---|
| First post | 2016-07-15 19:50 +0200 |
| Last post | 2016-07-19 07:10 +0200 |
| Articles | 7 — 5 participants |
Back to article view | Back to linux.kernel
[PATCH v3 0/4] vfs: Use dlock list for SB's s_inodes list Waiman Long <Waiman.Long@hpe.com> - 2016-07-15 19:50 +0200
[PATCH v3 1/4] lib/dlock-list: Distributed and lock-protected lists Waiman Long <Waiman.Long@hpe.com> - 2016-07-15 19:50 +0200
Re: [PATCH v3 1/4] lib/dlock-list: Distributed and lock-protected lists Tejun Heo <tj@kernel.org> - 2016-07-19 01:40 +0200
Re: [PATCH v3 1/4] lib/dlock-list: Distributed and lock-protected lists Tejun Heo <tj@kernel.org> - 2016-07-19 21:30 +0200
Re: [PATCH v3 1/4] lib/dlock-list: Distributed and lock-protected lists Christoph Lameter <cl@linux.com> - 2016-07-21 02:50 +0200
Re: [PATCH v3 1/4] lib/dlock-list: Distributed and lock-protected lists Dave Chinner <david@fromorbit.com> - 2016-07-21 03:50 +0200
Re: [PATCH v3 1/4] lib/dlock-list: Distributed and lock-protected lists Al Viro <viro@ZenIV.linux.org.uk> - 2016-07-19 07:10 +0200
| From | Waiman Long <Waiman.Long@hpe.com> |
|---|---|
| Date | 2016-07-15 19:50 +0200 |
| Subject | [PATCH v3 0/4] vfs: Use dlock list for SB's s_inodes list |
| Message-ID | <rVfpn-1mA-3@gated-at.bofh.it> |
v2->v3: - Remove the 2 persubnode API patches. - Merge __percpu tag patch 2 into patch 1. - As suggested by Tejun Heo, restructure the dlock_list_head data structure to hide the __percpu tag and rename some of the functions and structures. - Move most of the code from dlock_list.h to dlock_list.c and export the symbols. v1->v2: - Add a set of simple per-subnode APIs that is between percpu and per-node in granularity. - Make dlock list to use the per-subnode APIs so as to reduce the total number of separate linked list that needs to be managed and iterated. - There is no change in patches 1-5. This is a follow up of the following patchset: [PATCH v7 0/4] vfs: Use per-cpu list for SB's s_inodes list https://lkml.org/lkml/2016/4/12/1009 The main change is the renaming of percpu list to dlock list, as suggested by Christoph Lameter. It also adds a new patch from Boqun Feng to add the __percpu modifier for parameters. Patch 1 introduces the dlock list. Patch 2 cleans up the fsnotify_unmount_inodes() function by making the code simpler and more standard. Patch 3 replaces the use of list_for_each_entry_safe() in evict_inodes() and invalidate_inodes() by list_for_each_entry(). Patch 4 modifies the superblock and inode structures to use the dlock list. The corresponding functions that reference those structures are modified. Jan Kara (2): fsnotify: Simplify inode iteration on umount vfs: Remove unnecessary list_for_each_entry_safe() variants Waiman Long (2): lib/dlock-list: Distributed and lock-protected lists vfs: Use dlock list for superblock's inode list fs/block_dev.c | 13 ++- fs/drop_caches.c | 10 +- fs/fs-writeback.c | 13 ++- fs/inode.c | 40 +++---- fs/notify/inode_mark.c | 53 +++------- fs/quota/dquot.c | 16 ++-- fs/super.c | 7 +- include/linux/dlock-list.h | 135 +++++++++++++++++++++++ include/linux/fs.h | 8 +- lib/Makefile | 2 +- lib/dlock-list.c | 254 ++++++++++++++++++++++++++++++++++++++++++++ 11 files changed, 455 insertions(+), 96 deletions(-) create mode 100644 include/linux/dlock-list.h create mode 100644 lib/dlock-list.c
[toc] | [next] | [standalone]
| From | Waiman Long <Waiman.Long@hpe.com> |
|---|---|
| Date | 2016-07-15 19:50 +0200 |
| Subject | [PATCH v3 1/4] lib/dlock-list: Distributed and lock-protected lists |
| Message-ID | <rVfpo-1mA-43@gated-at.bofh.it> |
| In reply to | #1444484 |
Linked list is used everywhere in the Linux kernel. However, if many
threads are trying to add or delete entries into the same linked list,
it can create a performance bottleneck.
This patch introduces a new list APIs that provide a set of distributed
lists (one per CPU), each of which is protected by its own spinlock.
To the callers, however, the set of lists acts like a single
consolidated list. This allows list entries insertion and deletion
operations to happen in parallel instead of being serialized with a
global list and lock.
List entry insertion is strictly per cpu. List deletion, however, can
happen in a cpu other than the one that did the insertion. So we still
need lock to protect the list. Because of that, there may still be
a small amount of contention when deletion is being done.
A new header file include/linux/dlock-list.h will be added with the
associated dlock_list_head and dlock_list_node structures. The following
functions are provided to manage the per-cpu list:
1. int init_dlock_list_head(struct dlock_list_head *dlist)
2. void dlock_list_add(struct dlock_list_node *node,
struct dlock_list_head *dlist)
3. void dlock_list_del(struct dlock_list *node)
Iteration of all the list entries within a group of per-cpu lists is
done by calling either the dlock_list_next() or dlock_list_next_safe()
functions in a while loop. They correspond to the list_for_each_entry()
and list_for_each_entry_safe() macros respectively. The iteration
states are keep in a dlock_list_iter structure that is passed to the
iteration functions.
Suggested-by: Tejun Heo <tj@kernel.org>
Signed-off-by: Waiman Long <Waiman.Long@hpe.com>
Reviewed-by: Jan Kara <jack@suse.cz>
---
include/linux/dlock-list.h | 135 +++++++++++++++++++++++
lib/Makefile | 2 +-
lib/dlock-list.c | 254 ++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 390 insertions(+), 1 deletions(-)
create mode 100644 include/linux/dlock-list.h
create mode 100644 lib/dlock-list.c
diff --git a/include/linux/dlock-list.h b/include/linux/dlock-list.h
new file mode 100644
index 0000000..2647b7d
--- /dev/null
+++ b/include/linux/dlock-list.h
@@ -0,0 +1,135 @@
+/*
+ * Distributed and locked list
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * (C) Copyright 2016 Hewlett-Packard Enterprise Development LP
+ *
+ * Authors: Waiman Long <waiman.long@hpe.com>
+ */
+#ifndef __LINUX_DLOCK_LIST_H
+#define __LINUX_DLOCK_LIST_H
+
+#include <linux/spinlock.h>
+#include <linux/list.h>
+#include <linux/percpu.h>
+
+/*
+ * include/linux/dlock-list.h
+ *
+ * A distributed (per-cpu) set of lists each of which is protected by its
+ * own spinlock, but acts like a single consolidated list to the callers.
+ *
+ * The dlock_list_head_percpu structure contains the spinlock, the other
+ * dlock_list_node structures only contains a pointer to the spinlock in
+ * dlock_list_head_percpu.
+ */
+struct dlock_list_head_percpu {
+ struct list_head list;
+ spinlock_t lock;
+};
+
+struct dlock_list_head {
+ struct dlock_list_head_percpu __percpu *head;
+};
+
+/*
+ * dlock list node data structure
+ */
+struct dlock_list_node {
+ struct list_head list;
+ spinlock_t *lockptr;
+};
+
+#define DLOCK_LIST_HEAD_PERCPU_INIT(name) \
+ { \
+ .list.prev = &name.list, \
+ .list.next = &name.list, \
+ .list.lock = __SPIN_LOCK_UNLOCKED(name), \
+ }
+
+/*
+ * dlock list iteration state
+ */
+struct dlock_list_iter {
+ int cpu;
+ spinlock_t *lock;
+ struct list_head *head; /* List head of current per-cpu list */
+ struct dlock_list_node *curr;
+ struct dlock_list_node *next;
+};
+
+#define DLOCK_LIST_ITER_INIT() \
+ { \
+ .cpu = -1, \
+ }
+
+#define DEFINE_DLOCK_LIST_ITER(s) \
+ struct dlock_list_iter s = DLOCK_LIST_ITER_INIT()
+
+static inline void init_dlock_list_iter(struct dlock_list_iter *iter)
+{
+ *iter = (struct dlock_list_iter)DLOCK_LIST_ITER_INIT();
+}
+
+#define DLOCK_LIST_NODE_INIT(name) \
+ { \
+ .list.prev = &name.list, \
+ .list.next = &name.list, \
+ }
+
+static inline void init_dlock_list_node(struct dlock_list_node *node)
+{
+ INIT_LIST_HEAD(&node->list);
+ node->lockptr = NULL;
+}
+
+/*
+ * Check if all the dlock lists are empty
+ *
+ * This can be a pretty expensive function call. If this function is required
+ * in a performance critical path, we may have to maintain a global count
+ * of the list entries in the global dlock_list_head structure instead.
+ */
+static inline bool dlock_list_empty(struct dlock_list_head *dlist)
+{
+ int cpu;
+
+ for_each_possible_cpu(cpu)
+ if (!list_empty(&per_cpu_ptr(dlist->head, cpu)->list))
+ return false;
+ return true;
+}
+
+/*
+ * Allocation and freeing of dlock list
+ */
+extern int alloc_dlock_list_head(struct dlock_list_head *dlist);
+extern void free_dlock_list_head(struct dlock_list_head *dlist);
+
+/*
+ * The dlock list iteration functions which return true if iteration has
+ * to be continued.
+ */
+extern bool dlock_list_next(struct dlock_list_head *dlist,
+ struct dlock_list_iter *iter);
+extern bool dlock_list_next_safe(struct dlock_list_head *dlist,
+ struct dlock_list_iter *iter);
+
+/*
+ * The dlock list addition and deletion functions here are not irq-safe.
+ * Special irq-safe variants will have to be added if we need them.
+ */
+extern void dlock_list_add(struct dlock_list_node *node,
+ struct dlock_list_head *dlist);
+extern void dlock_list_del(struct dlock_list_node *node);
+
+#endif /* __LINUX_DLOCK_LIST_H */
diff --git a/lib/Makefile b/lib/Makefile
index 499fb35..92e8c38 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -40,7 +40,7 @@ obj-y += bcd.o div64.o sort.o parser.o halfmd4.o debug_locks.o random32.o \
gcd.o lcm.o list_sort.o uuid.o flex_array.o iov_iter.o clz_ctz.o \
bsearch.o find_bit.o llist.o memweight.o kfifo.o \
percpu-refcount.o percpu_ida.o rhashtable.o reciprocal_div.o \
- once.o
+ once.o dlock-list.o
obj-y += string_helpers.o
obj-$(CONFIG_TEST_STRING_HELPERS) += test-string_helpers.o
obj-y += hexdump.o
diff --git a/lib/dlock-list.c b/lib/dlock-list.c
new file mode 100644
index 0000000..af4a9f3
--- /dev/null
+++ b/lib/dlock-list.c
@@ -0,0 +1,254 @@
+/*
+ * Distributed and locked list
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * (C) Copyright 2016 Hewlett-Packard Enterprise Development LP
+ *
+ * Authors: Waiman Long <waiman.long@hpe.com>
+ */
+#include <linux/dlock-list.h>
+#include <linux/lockdep.h>
+#include <linux/export.h>
+
+/*
+ * As all the locks in the dlock list are dynamically allocated, they need
+ * to belong to their own special lock class to avoid warning and stack
+ * trace in kernel log when lockdep is enabled. Statically allocated locks
+ * don't have this problem.
+ */
+static struct lock_class_key dlock_list_key;
+
+/**
+ * alloc_dlock_list_head - Initialize and allocate the per-cpu list head
+ * @dlist: Pointer to the dlock_list_head structure to be initialized
+ * Return: 0 if successful, -ENOMEM if memory allocation error
+ *
+ * This function does not allocate the dlock_list_head structure itself. The
+ * callers will have to do their own memory allocation, if necessary. However,
+ * this allows embedding the dlock_list_head structure directly into other
+ * structures.
+ */
+int alloc_dlock_list_head(struct dlock_list_head *dlist)
+{
+ struct dlock_list_head dlist_tmp;
+ int cpu;
+
+ dlist_tmp.head = alloc_percpu(struct dlock_list_head_percpu);
+ if (!dlist_tmp.head)
+ return -ENOMEM;
+
+ for_each_possible_cpu(cpu) {
+ struct dlock_list_head_percpu *head;
+
+ head = per_cpu_ptr(dlist_tmp.head, cpu);
+ INIT_LIST_HEAD(&head->list);
+ head->lock = __SPIN_LOCK_UNLOCKED(&head->lock);
+ lockdep_set_class(&head->lock, &dlock_list_key);
+ }
+
+ dlist->head = dlist_tmp.head;
+ return 0;
+}
+EXPORT_SYMBOL(alloc_dlock_list_head);
+
+/**
+ * free_dlock_list_head - Free the per-cpu list head of dlock list
+ * @dlist: Pointer of the dlock_list_head structure to be freed
+ *
+ * This function doesn't free the dlock_list_head structure itself. So
+ * the caller will have to do it, if necessary.
+ */
+void free_dlock_list_head(struct dlock_list_head *dlist)
+{
+ free_percpu(dlist->head);
+ dlist->head = NULL;
+}
+EXPORT_SYMBOL(free_dlock_list_head);
+
+/**
+ * dlock_list_add - Adds a node to the given dlock list
+ * @node : The node to be added
+ * @dlist: The dlock list where the node is to be added
+ *
+ * List selection is based on the CPU being used when the dlock_list_add()
+ * function is called. However, deletion may be done by a different CPU.
+ * So we still need to use a lock to protect the content of the list.
+ */
+void dlock_list_add(struct dlock_list_node *node,
+ struct dlock_list_head *dlist)
+{
+ struct dlock_list_head_percpu *head;
+
+ /*
+ * Disable preemption to make sure that CPU won't gets changed.
+ */
+ head = get_cpu_ptr(dlist->head);
+ spin_lock(&head->lock);
+ node->lockptr = &head->lock;
+ list_add(&node->list, &head->list);
+ spin_unlock(&head->lock);
+ put_cpu_ptr(dlist->head);
+}
+EXPORT_SYMBOL(dlock_list_add);
+
+/**
+ * dlock_list_del - Delete a node from a dlock list
+ * @node : The node to be deleted
+ *
+ * We need to check the lock pointer again after taking the lock to guard
+ * against concurrent deletion of the same node. If the lock pointer changes
+ * (becomes NULL or to a different one), we assume that the deletion was done
+ * elsewhere. A warning will be printed if this happens as it is likely to be
+ * a bug.
+ */
+void dlock_list_del(struct dlock_list_node *node)
+{
+ spinlock_t *lock = READ_ONCE(node->lockptr);
+
+ if (unlikely(!lock)) {
+ WARN_ONCE(1,
+ "dlock_list_del: node 0x%lx has no associated lock\n",
+ (unsigned long)node);
+ return;
+ }
+
+ spin_lock(lock);
+ if (likely(lock == node->lockptr)) {
+ list_del_init(&node->list);
+ node->lockptr = NULL;
+ } else {
+ /*
+ * This path should never be executed.
+ */
+ WARN_ON_ONCE(1);
+ }
+ spin_unlock(lock);
+}
+EXPORT_SYMBOL(dlock_list_del);
+
+/*
+ * Helper function to find the first entry of the next per-cpu list
+ * It works somewhat like for_each_possible_cpu(cpu).
+ *
+ * Return: true if the entry is found, false if all the lists exhausted
+ *
+ */
+static inline bool dlock_list_next_cpu(struct dlock_list_head *dlist,
+ struct dlock_list_iter *iter)
+{
+ if (iter->lock)
+ spin_unlock(iter->lock);
+next_cpu:
+ /*
+ * for_each_possible_cpu(cpu)
+ */
+ iter->cpu = cpumask_next(iter->cpu, cpu_possible_mask);
+ if (iter->cpu >= nr_cpu_ids)
+ return false; /* All the per-cpu lists iterated */
+
+ iter->head = &per_cpu_ptr(dlist->head, iter->cpu)->list;
+ if (list_empty(iter->head))
+ goto next_cpu;
+
+ iter->lock = &per_cpu_ptr(dlist->head, iter->cpu)->lock;
+ spin_lock(iter->lock);
+ /*
+ * There is a slight chance that the list may become empty just
+ * before the lock is acquired. So an additional check is
+ * needed to make sure that iter->curr points to a valid entry.
+ */
+ if (list_empty(iter->head)) {
+ spin_unlock(iter->lock);
+ goto next_cpu;
+ }
+ iter->curr = list_entry(iter->head->next,
+ struct dlock_list_node, list);
+ return true;
+}
+
+/**
+ * dlock_list_next - Iterate to the next entry of the dlock list
+ * @dlist: Pointer to the dlock_list_head structure
+ * @iter : Pointer to the dlock list iterator structure
+ * Return: true if the next entry is found, false if all the entries iterated
+ *
+ * The iterator has to be properly initialized before calling this function.
+ * This iteration function isn't safe with respect to list entry removal, but
+ * it can correctly iterate newly added entries right after the current one.
+ * This iteration function is designed to be used in a while loop.
+ *
+ * Usage example:
+ *
+ * DEFINE_DLOCK_LIST_ITER(iter);
+ * while (dlock_list_next(dlist, &iter)) {
+ * ...
+ * }
+ */
+bool dlock_list_next(struct dlock_list_head *dlist,
+ struct dlock_list_iter *iter)
+{
+ /*
+ * Find next entry
+ */
+ if (iter->curr)
+ iter->curr = list_next_entry(iter->curr, list);
+
+ if (!iter->curr || (&iter->curr->list == iter->head)) {
+ /*
+ * The current per-cpu list has been exhausted, try the next
+ * per-cpu list.
+ */
+ if (!dlock_list_next_cpu(dlist, iter))
+ return false;
+ }
+
+ WARN_ON_ONCE(iter->curr->lockptr != iter->lock);
+ return true; /* Continue the iteration */
+}
+EXPORT_SYMBOL(dlock_list_next);
+
+/**
+ * dlock_list_next_safe - Removal-safe iterator of dlock list
+ * @dlist: Pointer to the dlock_list_head structure
+ * @iter : Pointer to the dlock list iterator structure
+ * Return: true if the next entry is found, false if all the entries iterated
+ *
+ * The iterator has to be properly initialized before calling this function.
+ * This iteration function is safe with respect to list entry removal.
+ * However, it cannot correctly iterate newly added entries right after the
+ * current one.
+ */
+bool dlock_list_next_safe(struct dlock_list_head *dlist,
+ struct dlock_list_iter *iter)
+{
+ /*
+ * Find next entry
+ */
+ if (iter->curr) {
+ iter->curr = iter->next;
+ iter->next = list_next_entry(iter->next, list);
+ }
+
+ if (!iter->curr || (&iter->curr->list == iter->head)) {
+ /*
+ * The current per-cpu list has been exhausted, try the next
+ * per-cpu list.
+ */
+ if (!dlock_list_next_cpu(dlist, iter))
+ return false;
+ iter->next = list_next_entry(iter->curr, list);
+ }
+
+ WARN_ON_ONCE(iter->curr->lockptr != iter->lock);
+ return true; /* Continue the iteration */
+}
+EXPORT_SYMBOL(dlock_list_next_safe);
--
1.7.1
[toc] | [prev] | [next] | [standalone]
| From | Tejun Heo <tj@kernel.org> |
|---|---|
| Date | 2016-07-19 01:40 +0200 |
| Subject | Re: [PATCH v3 1/4] lib/dlock-list: Distributed and lock-protected lists |
| Message-ID | <rWqiK-4rm-31@gated-at.bofh.it> |
| In reply to | #1444485 |
Hello, Waiman.
On Fri, Jul 15, 2016 at 01:39:40PM -0400, Waiman Long wrote:
> Suggested-by: Tejun Heo <tj@kernel.org>
Not sure I should be on suggested-by given that this wasn't my idea at
all.
> +/*
> + * include/linux/dlock-list.h
> + *
> + * A distributed (per-cpu) set of lists each of which is protected by its
> + * own spinlock, but acts like a single consolidated list to the callers.
> + *
> + * The dlock_list_head_percpu structure contains the spinlock, the other
> + * dlock_list_node structures only contains a pointer to the spinlock in
> + * dlock_list_head_percpu.
> + */
The more I think about it, the more bothered I'm about the dlock_list
name. For the most part, this isn't different from other percpu data
structures in the kernel. Sure, it might benefit from doing Nth cpu,
but so are other percpu data structures and it's not just "distributed
lock" list either. The list itself is percpu, not just locking. Can
we please go back to percpu_list? Christoph, what do you think?
> +struct dlock_list_node {
> + struct list_head list;
> + spinlock_t *lockptr;
> +};
Wouldn't it be better to point to dlock_list_percpu?
> +#define DLOCK_LIST_HEAD_PERCPU_INIT(name) \
> + { \
> + .list.prev = &name.list, \
> + .list.next = &name.list, \
Use LIST_HEAD_INIT()? Also, why do we even need the initializers if
the data structure can only be dynamically allocated. In fact, do
the definitions even need to be exposed in the header?
> + .list.lock = __SPIN_LOCK_UNLOCKED(name), \
> + }
> +
> +/*
> + * dlock list iteration state
> + */
> +struct dlock_list_iter {
> + int cpu;
^
I'm not sure lining up with space here is common in kernel.
> + spinlock_t *lock;
> + struct list_head *head; /* List head of current per-cpu list */
> + struct dlock_list_node *curr;
> + struct dlock_list_node *next;
> +};
> +
> +#define DLOCK_LIST_ITER_INIT() \
^
Don't we usually omit () in these cases?
> + { \
> + .cpu = -1, \
> + }
> +
> +#define DEFINE_DLOCK_LIST_ITER(s) \
> + struct dlock_list_iter s = DLOCK_LIST_ITER_INIT()
> +
> +static inline void init_dlock_list_iter(struct dlock_list_iter *iter)
> +{
> + *iter = (struct dlock_list_iter)DLOCK_LIST_ITER_INIT();
> +}
> +
> +#define DLOCK_LIST_NODE_INIT(name) \
> + { \
> + .list.prev = &name.list, \
> + .list.next = &name.list, \
^
LIST_HEAD_INIT()?
> + }
> +
> +static inline void init_dlock_list_node(struct dlock_list_node *node)
> +{
> + INIT_LIST_HEAD(&node->list);
> + node->lockptr = NULL;
Why not use DLOCK_LIST_NODE_INIT()?
> +}
> +
> +/*
> + * Check if all the dlock lists are empty
> + *
> + * This can be a pretty expensive function call. If this function is required
> + * in a performance critical path, we may have to maintain a global count
> + * of the list entries in the global dlock_list_head structure instead.
> + */
/** function comment please.
> +static inline bool dlock_list_empty(struct dlock_list_head *dlist)
> +{
> + int cpu;
> +
> + for_each_possible_cpu(cpu)
> + if (!list_empty(&per_cpu_ptr(dlist->head, cpu)->list))
> + return false;
> + return true;
> +}
> +
> +/*
> + * Allocation and freeing of dlock list
> + */
> +extern int alloc_dlock_list_head(struct dlock_list_head *dlist);
^
ditto with alignment
> +extern void free_dlock_list_head(struct dlock_list_head *dlist);
> +
> +/*
> + * The dlock list iteration functions which return true if iteration has
> + * to be continued.
> + */
> +extern bool dlock_list_next(struct dlock_list_head *dlist,
> + struct dlock_list_iter *iter);
> +extern bool dlock_list_next_safe(struct dlock_list_head *dlist,
> + struct dlock_list_iter *iter);
Why not return dlock_list_node * for the current node? That'd more
conventional and allows dlock_list_iter to be opaque.
> diff --git a/lib/dlock-list.c b/lib/dlock-list.c
> new file mode 100644
> index 0000000..af4a9f3
> --- /dev/null
> +++ b/lib/dlock-list.c
...
> +int alloc_dlock_list_head(struct dlock_list_head *dlist)
> +{
> + struct dlock_list_head dlist_tmp;
> + int cpu;
> +
> + dlist_tmp.head = alloc_percpu(struct dlock_list_head_percpu);
> + if (!dlist_tmp.head)
> + return -ENOMEM;
> +
> + for_each_possible_cpu(cpu) {
> + struct dlock_list_head_percpu *head;
> +
> + head = per_cpu_ptr(dlist_tmp.head, cpu);
> + INIT_LIST_HEAD(&head->list);
> + head->lock = __SPIN_LOCK_UNLOCKED(&head->lock);
> + lockdep_set_class(&head->lock, &dlock_list_key);
> + }
> +
> + dlist->head = dlist_tmp.head;
Just use dlist->head directly or use local __perpcu head pointer?
> + return 0;
> +}
> +EXPORT_SYMBOL(alloc_dlock_list_head);
Does this actually need to be exported? If so, it might be a better
idea to start with EXPORT_SYMBOL_GPL().
> +void dlock_list_add(struct dlock_list_node *node,
> + struct dlock_list_head *dlist)
> +{
> + struct dlock_list_head_percpu *head;
^
This probably requires __percpu annotation. Have you run it through
sparse and checked for address space warnings?
> +
> + /*
> + * Disable preemption to make sure that CPU won't gets changed.
> + */
> + head = get_cpu_ptr(dlist->head);
> + spin_lock(&head->lock);
> + node->lockptr = &head->lock;
> + list_add(&node->list, &head->list);
> + spin_unlock(&head->lock);
> + put_cpu_ptr(dlist->head);
> +}
> +EXPORT_SYMBOL(dlock_list_add);
> +
> +/**
> + * dlock_list_del - Delete a node from a dlock list
> + * @node : The node to be deleted
> + *
> + * We need to check the lock pointer again after taking the lock to guard
> + * against concurrent deletion of the same node. If the lock pointer changes
> + * (becomes NULL or to a different one), we assume that the deletion was done
> + * elsewhere. A warning will be printed if this happens as it is likely to be
> + * a bug.
> + */
> +void dlock_list_del(struct dlock_list_node *node)
> +{
> + spinlock_t *lock = READ_ONCE(node->lockptr);
> +
> + if (unlikely(!lock)) {
> + WARN_ONCE(1,
> + "dlock_list_del: node 0x%lx has no associated lock\n",
> + (unsigned long)node);
Maybe "if (WARN_ONCE(!lock...)"? WARN_ONCE implies unlikely.
> + return;
> + }
> +
> + spin_lock(lock);
> + if (likely(lock == node->lockptr)) {
> + list_del_init(&node->list);
> + node->lockptr = NULL;
> + } else {
> + /*
> + * This path should never be executed.
> + */
> + WARN_ON_ONCE(1);
> + }
This still kinda bothers me because this pretty much requires the
users to have strong synchronization around the operations and makes
it unusable in situations where opportunistic behaviors are
acceptable. It negates the usefulness quite a bit.
> + spin_unlock(lock);
> +}
> +EXPORT_SYMBOL(dlock_list_del);
> +
> +/*
> + * Helper function to find the first entry of the next per-cpu list
> + * It works somewhat like for_each_possible_cpu(cpu).
> + *
> + * Return: true if the entry is found, false if all the lists exhausted
> + *
> + */
> +static inline bool dlock_list_next_cpu(struct dlock_list_head *dlist,
^
Just let the compiler figure it out.
> + struct dlock_list_iter *iter)
> +{
> + if (iter->lock)
> + spin_unlock(iter->lock);
> +next_cpu:
> + /*
> + * for_each_possible_cpu(cpu)
> + */
> + iter->cpu = cpumask_next(iter->cpu, cpu_possible_mask);
> + if (iter->cpu >= nr_cpu_ids)
> + return false; /* All the per-cpu lists iterated */
> +
> + iter->head = &per_cpu_ptr(dlist->head, iter->cpu)->list;
> + if (list_empty(iter->head))
> + goto next_cpu;
> +
> + iter->lock = &per_cpu_ptr(dlist->head, iter->cpu)->lock;
> + spin_lock(iter->lock);
> + /*
> + * There is a slight chance that the list may become empty just
> + * before the lock is acquired. So an additional check is
> + * needed to make sure that iter->curr points to a valid entry.
> + */
> + if (list_empty(iter->head)) {
> + spin_unlock(iter->lock);
> + goto next_cpu;
> + }
> + iter->curr = list_entry(iter->head->next,
> + struct dlock_list_node, list);
> + return true;
> +}
...
> +/**
> + * dlock_list_next_safe - Removal-safe iterator of dlock list
> + * @dlist: Pointer to the dlock_list_head structure
> + * @iter : Pointer to the dlock list iterator structure
> + * Return: true if the next entry is found, false if all the entries iterated
> + *
> + * The iterator has to be properly initialized before calling this function.
> + * This iteration function is safe with respect to list entry removal.
> + * However, it cannot correctly iterate newly added entries right after the
> + * current one.
> + */
This still looks wrong to me. If you want to provide the two variants
of iterations, can't you just implement one next function and build
the two types of iterations on top of it?
Thanks.
--
tejun
[toc] | [prev] | [next] | [standalone]
| From | Tejun Heo <tj@kernel.org> |
|---|---|
| Date | 2016-07-19 21:30 +0200 |
| Subject | Re: [PATCH v3 1/4] lib/dlock-list: Distributed and lock-protected lists |
| Message-ID | <rWISl-83d-9@gated-at.bofh.it> |
| In reply to | #1445924 |
Hello,
On Tue, Jul 19, 2016 at 02:42:31PM -0400, Waiman Long wrote:
> On 07/18/2016 07:38 PM, Tejun Heo wrote:
> > > +struct dlock_list_node {
> > > + struct list_head list;
> > > + spinlock_t *lockptr;
> > > +};
> > Wouldn't it be better to point to dlock_list_percpu?
>
> I could. However, the only thing that matter is the spinlock that protects
> the list entry.
Yeah, we can get back to this when it's actually necessary. It just
looked a bit weird to me.
> > > +/*
> > > + * The dlock list iteration functions which return true if iteration has
> > > + * to be continued.
> > > + */
> > > +extern bool dlock_list_next(struct dlock_list_head *dlist,
> > > + struct dlock_list_iter *iter);
> > > +extern bool dlock_list_next_safe(struct dlock_list_head *dlist,
> > > + struct dlock_list_iter *iter);
> > Why not return dlock_list_node * for the current node? That'd more
> > conventional and allows dlock_list_iter to be opaque.
>
> Yes, I can make it return dlock_list_node *.
>
> However, to make dlock_list_iter opaque, I will have to dynamically allocate
> the structure. That will add an extra memory allocation and free calls as
> well as handling the error case of running out of memory. I don't think that
> is worth doing at this point.
Sure, keep it defined in the header file. Just don't require users to
reach into it and add a comment saying that the struct is opaque to
its users.
> > > +int alloc_dlock_list_head(struct dlock_list_head *dlist)
> > > +{
> > > + struct dlock_list_head dlist_tmp;
> > > + int cpu;
> > > +
> > > + dlist_tmp.head = alloc_percpu(struct dlock_list_head_percpu);
> > > + if (!dlist_tmp.head)
> > > + return -ENOMEM;
> > > +
> > > + for_each_possible_cpu(cpu) {
> > > + struct dlock_list_head_percpu *head;
> > > +
> > > + head = per_cpu_ptr(dlist_tmp.head, cpu);
> > > + INIT_LIST_HEAD(&head->list);
> > > + head->lock = __SPIN_LOCK_UNLOCKED(&head->lock);
> > > + lockdep_set_class(&head->lock,&dlock_list_key);
> > > + }
> > > +
> > > + dlist->head = dlist_tmp.head;
> > Just use dlist->head directly or use local __perpcu head pointer?
>
> I just don't want to expose the structure to world until it is fully
> initialized. If you think I am over-cautious, I can use dlist->head as
> suggested.
I don't think it makes any actual difference. No strong opinion
either way. Just use local __percpu head pointer then?
> > > + return 0;
> > > +}
> > > +EXPORT_SYMBOL(alloc_dlock_list_head);
> > Does this actually need to be exported? If so, it might be a better
> > idea to start with EXPORT_SYMBOL_GPL().
>
> For the current use case, we probably don't need to export the symbols.
> Other use cases may require that. I will change it to use the version
> instead.
If it's not immediately necessary, it's best to not export at all.
> > > +void dlock_list_del(struct dlock_list_node *node)
> > > +{
> > > + spinlock_t *lock = READ_ONCE(node->lockptr);
> > > +
> > > + if (unlikely(!lock)) {
> > > + WARN_ONCE(1,
> > > + "dlock_list_del: node 0x%lx has no associated lock\n",
> > > + (unsigned long)node);
> > Maybe "if (WARN_ONCE(!lock...)"? WARN_ONCE implies unlikely.
>
> OK, will do that.
>
> > > + return;
> > > + }
> > > +
> > > + spin_lock(lock);
> > > + if (likely(lock == node->lockptr)) {
> > > + list_del_init(&node->list);
> > > + node->lockptr = NULL;
> > > + } else {
> > > + /*
> > > + * This path should never be executed.
> > > + */
> > > + WARN_ON_ONCE(1);
> > > + }
> > This still kinda bothers me because this pretty much requires the
> > users to have strong synchronization around the operations and makes
> > it unusable in situations where opportunistic behaviors are
> > acceptable. It negates the usefulness quite a bit.
>
> I understand your concern. I will make it retry again with the new lock.
It doesn't necessarily have to retry but shouldn't break down when
used in an opportunistic racy way - e.g. if adds and removes race, the
order of operations isn't clearly defined as such any outcome is fine
as long as the list maintains its integrity.
> > > +/**
> > > + * dlock_list_next_safe - Removal-safe iterator of dlock list
> > > + * @dlist: Pointer to the dlock_list_head structure
> > > + * @iter : Pointer to the dlock list iterator structure
> > > + * Return: true if the next entry is found, false if all the entries iterated
> > > + *
> > > + * The iterator has to be properly initialized before calling this function.
> > > + * This iteration function is safe with respect to list entry removal.
> > > + * However, it cannot correctly iterate newly added entries right after the
> > > + * current one.
> > > + */
> > This still looks wrong to me. If you want to provide the two variants
> > of iterations, can't you just implement one next function and build
> > the two types of iterations on top of it?
>
> I have been thinking about making dlock_list_next_cpu() the real external
> function and have 2 inline functions that implement dlock_list_next() and
> dlock_list_next_safe(). That may strike a better balance between performance
> and code abstraction. I will do so if you have no objection to that.
Yeah, please give it a try. As mentioned in another reply, it'd
probably be best to provide an iteration macro which encapsulates the
whole thing.
Thanks.
--
tejun
[toc] | [prev] | [next] | [standalone]
| From | Christoph Lameter <cl@linux.com> |
|---|---|
| Date | 2016-07-21 02:50 +0200 |
| Subject | Re: [PATCH v3 1/4] lib/dlock-list: Distributed and lock-protected lists |
| Message-ID | <rXalz-c2-9@gated-at.bofh.it> |
| In reply to | #1445924 |
On Wed, 20 Jul 2016, Waiman Long wrote: > Christoph, are you OK with Tejun's request to revert the name back to > percpu_list? Or do you still think the current name is better? The percpu structure contains a spinlock and may be remotely accessed? You are aware that other percpu variables that share the same cacheline will be negatively impacted by accesses from other processors? The role of percpu areas are to have memory areas where the code can expect that cachelines are exclusively there for that processor. How frequent are the remote accesses? If this is rare then ok.
[toc] | [prev] | [next] | [standalone]
| From | Dave Chinner <david@fromorbit.com> |
|---|---|
| Date | 2016-07-21 03:50 +0200 |
| Subject | Re: [PATCH v3 1/4] lib/dlock-list: Distributed and lock-protected lists |
| Message-ID | <rXbhD-Mz-5@gated-at.bofh.it> |
| In reply to | #1447555 |
On Wed, Jul 20, 2016 at 07:48:13PM -0500, Christoph Lameter wrote: > On Wed, 20 Jul 2016, Waiman Long wrote: > > > Christoph, are you OK with Tejun's request to revert the name back to > > percpu_list? Or do you still think the current name is better? > > The percpu structure contains a spinlock and may be remotely accessed? You > are aware that other percpu variables that share the same cacheline will > be negatively impacted by accesses from other processors? > > The role of percpu areas are to have memory areas where the code can > expect that cachelines are exclusively there for that processor. > > How frequent are the remote accesses? If this is rare then ok. Remote access will be the common case on traversal and removal from the superblock inode list. Under memory reclaim, the access should at least be from a CPU on the same node (as inode reclaim is NUMA aware). However, any other inode eviction event (e.g. inode unlink) removing it from the sb list will essentially be from a random CPU. Traversals (such as from the sync code, or cache invalidations) will run on a single CPU, so alomst all access from them will be be remote. So it's really only a per-cpu structure for list addition.... Cheers, Dave. -- Dave Chinner david@fromorbit.com
[toc] | [prev] | [next] | [standalone]
| From | Al Viro <viro@ZenIV.linux.org.uk> |
|---|---|
| Date | 2016-07-19 07:10 +0200 |
| Subject | Re: [PATCH v3 1/4] lib/dlock-list: Distributed and lock-protected lists |
| Message-ID | <rWvs5-8aD-1@gated-at.bofh.it> |
| In reply to | #1444485 |
On Fri, Jul 15, 2016 at 01:39:40PM -0400, Waiman Long wrote:
> +struct dlock_list_head_percpu {
> + struct list_head list;
> + spinlock_t lock;
> +};
> +#define DLOCK_LIST_HEAD_PERCPU_INIT(name) \
> + { \
> + .list.prev = &name.list, \
> + .list.next = &name.list, \
> + .list.lock = __SPIN_LOCK_UNLOCKED(name), \
What's .list.lock and how does that even compile?
> +extern bool dlock_list_next(struct dlock_list_head *dlist,
> + struct dlock_list_iter *iter);
Ugh... Why not dlist_for_each_entry(), seeing that all users end up with
the same boilerplate?
[toc] | [prev] | [standalone]
Back to top | Article view | linux.kernel
csiph-web