Skip to contentSkip to conversation

Use llist in irq_work and xlist

PATCH In review Huang Ying 3 participants 20 messages Aug 30, 2011 – Sep 3, 2011

Subsystems (2)
lore
Share & tools
lore

Huang Ying

author
#0 lore raw

cover Use llist in irq_work and xlist

To remove the code duplication.

[PATCH -mm 1/2] irq_work, Use llist in irq_work
[PATCH -mm 2/2] net, rds, Replace xlist in net/rds/xlist.h with llist

Best Regards,
Huang Ying

author
#1 lore raw

PATCH 1/2 irq_work, Use llist in irq_work

Use llist in irq_work instead of the lock-less linked list implementation in irq_work to avoid the code duplication.

Cc Peter Zijlstra <[email protected]>
---
 include/linux/irq_work.h |   15 ++++---
 init/Kconfig             |    1 
 kernel/irq_work.c        |   92 ++++++++++++++++++-----------------------------
 3 files changed, 47 insertions(+), 61 deletions(-)
include/linux/irq_work.h +9 -6
@@ -1,20 +1,23 @@
 #ifndef _LINUX_IRQ_WORK_H
 #define _LINUX_IRQ_WORK_H
 
+#include <linux/llist.h>
+
 struct irq_work {
-	struct irq_work *next;
+	unsigned long flags;
+	struct llist_node llnode;
 	void (*func)(struct irq_work *);
 };
 
 static inline
-void init_irq_work(struct irq_work *entry, void (*func)(struct irq_work *))
+void init_irq_work(struct irq_work *work, void (*func)(struct irq_work *))
 {
-	entry->next = NULL;
-	entry->func = func;
+	work->flags = 0;
+	work->func = func;
 }
 
-bool irq_work_queue(struct irq_work *entry);
+bool irq_work_queue(struct irq_work *work);
 void irq_work_run(void);
-void irq_work_sync(struct irq_work *entry);
+void irq_work_sync(struct irq_work *work);
 
 #endif /* _LINUX_IRQ_WORK_H */
init/Kconfig +1 -0
@@ -26,6 +26,7 @@ config HAVE_IRQ_WORK
 config IRQ_WORK
 	bool
 	depends on HAVE_IRQ_WORK
+	select LLIST
 
 menu "General setup"
 
kernel/irq_work.c +37 -55

170 lines not on this page · raw message

#2 lore raw

Re: [PATCH -mm 1/2] irq_work, Use llist in irq_work

Quoted 2 lines · Huang Ying

Use llist in irq_work instead of the lock-less linked list implementation in irq_work to avoid the code duplication.

Except you make code horrid as well.. both this and xlist don't have additional function calls, whereas you do.

Also, WTFH do you have unconditinoal cpu_relax() calls inside the cmpxchg() loops, that's just bloody insane.

Move all of lib/llist.c inline, create a new macro for the

#ifndef CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG                                                    
        BUG_ON(in_nmi());                                                                    

#endif

blurb and loose the LLIST Kconfig.

Quoted 12 lines
Cc Peter Zijlstra <[email protected]>
---
 include/linux/irq_work.h |   15 ++++---
 init/Kconfig             |    1 
 kernel/irq_work.c        |   92 ++++++++++++++++++-----------------------------
 3 files changed, 47 insertions(+), 61 deletions(-)
include/linux/irq_work.h +0 -0
@@ -1,20 +1,23 @@
 #ifndef _LINUX_IRQ_WORK_H

include/linux/irq_work.h line 10

Quoted 10 lines
include/linux/irq_work.h +4 -1
 #define _LINUX_IRQ_WORK_H
 
+#include <linux/llist.h>
+
 struct irq_work {
-	struct irq_work *next;
+	unsigned long flags;
+	struct llist_node llnode;
 	void (*func)(struct irq_work *);
 };

Separating out the flags is unfortunate, but ok.

include/linux/irq_work.h line 11

Quoted 1 line
include/linux/irq_work.h +1 -0
+#define LIST_NONEMPTY_BIT	0

This is just sad, see below.

include/linux/irq_work.h line 15

Quoted 10 lines
include/linux/irq_work.h +4 -6
-static inline struct irq_work *next_flags(struct irq_work *entry, int flags)
-{
-	unsigned long next = (unsigned long)entry;
-	next |= flags;
-	return (struct irq_work *)next;
-}
+struct irq_work_list {
+	unsigned long flags;
+	struct llist_head llist;
+};

which is superfluous

Quoted 14 lines
include/linux/irq_work.h +3 -4
@@ -77,23 +62,19 @@ void __weak arch_irq_work_raise(void)
 /*
  * Queue the entry and raise the IPI if needed.
  */
-static void __irq_work_queue(struct irq_work *entry)
+static void __irq_work_queue(struct irq_work *work)
 {
-	struct irq_work *next;
+	struct irq_work_list *irq_work_list;
 
-	preempt_disable();
+	irq_work_list = &get_cpu_var(irq_work_lists);
 
-	do {

include/linux/irq_work.h void __weak arch_irq_work_raise(void) · line 75

Quoted 10 lines
include/linux/irq_work.h +2 -5
-		next = __this_cpu_read(irq_work_list);
-		/* Can assign non-atomic because we keep the flags set. */
-		entry->next = next_flags(next, IRQ_WORK_FLAGS);
-	} while (this_cpu_cmpxchg(irq_work_list, next, entry) != next);
+	llist_add(&work->llnode, &irq_work_list->llist);
 
 	/* The list was empty, raise self-interrupt to start processing. */
-	if (!irq_work_next(entry))
+	if (!test_and_set_bit(LIST_NONEMPTY_BIT, &irq_work_list->flags))
 		arch_irq_work_raise();

So why can't you simply test work->llnode->next? and loose the get/put cpu muck? The existing preempt_disable/enable() are already superfluous and could be removed, you just made all this way more horrid than need be.

Quoted 4 lines
include/linux/irq_work.h +1 -1
-	preempt_enable();
+	put_cpu_var(irq_work_list);
 }
author
#3 lore raw

Re: [PATCH -mm 1/2] irq_work, Use llist in irq_work

Hi, Peter,

Thanks for your comments.

Quoted 9 lines · Peter Zijlstra
Quoted 2 lines · Huang Ying

Use llist in irq_work instead of the lock-less linked list implementation in irq_work to avoid the code duplication.

Except you make code horrid as well.. both this and xlist don't have additional function calls, whereas you do.

Also, WTFH do you have unconditinoal cpu_relax() calls inside the cmpxchg() loops, that's just bloody insane.

You mean we should not use cpu_relax before the first cmpxchg? You suggest something as follow?

void llist_add(struct llist_node *new, struct llist_head *head)

{
        struct llist_node *entry, *old_entry;
#ifndef CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG
        BUG_ON(in_nmi());

#endif

        entry = head->first;
        for (;;) {
                old_entry = entry;
                new->next = entry;
                entry = cmpxchg(&head->first, old_entry, new);
                if (entry == old_entry)
                        break;
                cpu_relax();
        }
}
Quoted 7 lines

Move all of lib/llist.c inline, create a new macro for the

#ifndef CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG                                                    
        BUG_ON(in_nmi());                                                                    

#endif

blurb and loose the LLIST Kconfig.

OK. I will do that.

Quoted 64 lines
Quoted 12 lines
Cc Peter Zijlstra <[email protected]>
---
 include/linux/irq_work.h |   15 ++++---
 init/Kconfig             |    1 
 kernel/irq_work.c        |   92 ++++++++++++++++++-----------------------------
 3 files changed, 47 insertions(+), 61 deletions(-)
include/linux/irq_work.h +0 -0
@@ -1,20 +1,23 @@
 #ifndef _LINUX_IRQ_WORK_H

include/linux/irq_work.h line 10

Quoted 10 lines
include/linux/irq_work.h +4 -1
 #define _LINUX_IRQ_WORK_H
 
+#include <linux/llist.h>
+
 struct irq_work {
-	struct irq_work *next;
+	unsigned long flags;
+	struct llist_node llnode;
 	void (*func)(struct irq_work *);
 };

Separating out the flags is unfortunate, but ok.

include/linux/irq_work.h line 11

Quoted 1 line
include/linux/irq_work.h +1 -0
+#define LIST_NONEMPTY_BIT	0

This is just sad, see below.

include/linux/irq_work.h line 15

Quoted 10 lines
include/linux/irq_work.h +4 -6
-static inline struct irq_work *next_flags(struct irq_work *entry, int flags)
-{
-	unsigned long next = (unsigned long)entry;
-	next |= flags;
-	return (struct irq_work *)next;
-}
+struct irq_work_list {
+	unsigned long flags;
+	struct llist_head llist;
+};

which is superfluous

Quoted 20 lines
include/linux/irq_work.h +4 -8
@@ -77,23 +62,19 @@ void __weak arch_irq_work_raise(void)
 /*
  * Queue the entry and raise the IPI if needed.
  */
-static void __irq_work_queue(struct irq_work *entry)
+static void __irq_work_queue(struct irq_work *work)
 {
-	struct irq_work *next;
+	struct irq_work_list *irq_work_list;
 
-	preempt_disable();
+	irq_work_list = &get_cpu_var(irq_work_lists);
 
-	do {
-		next = __this_cpu_read(irq_work_list);
-		/* Can assign non-atomic because we keep the flags set. */
-		entry->next = next_flags(next, IRQ_WORK_FLAGS);
-	} while (this_cpu_cmpxchg(irq_work_list, next, entry) != next);
+	llist_add(&work->llnode, &irq_work_list->llist);
 
Quoted 6 lines

include/linux/irq_work.h void __weak arch_irq_work_raise(void) · line 75

Quoted 4 lines
include/linux/irq_work.h +1 -1
 	/* The list was empty, raise self-interrupt to start processing. */
-	if (!irq_work_next(entry))
+	if (!test_and_set_bit(LIST_NONEMPTY_BIT, &irq_work_list->flags))
 		arch_irq_work_raise();

So why can't you simply test work->llnode->next?

Yes. That is better. Even if there may be a small race window, it is not a big issue to raise one extra self interrupt seldom.

Quoted 4 lines

and loose the get/put
cpu muck? The existing preempt_disable/enable() are already superfluous and could be removed, you just made all this way more horrid than need be.

Will it cause race condition to remove preempt_disable/enable? Considering something as follow:

- get irq_work_list of CPU A
- queue irq_work into irq_work_list of CPU A
- preempted and resumed execution on CPU B
- arch_irq_work_raise on CPU B

irq_work_run on CPU B will do nothing. While irq_work need to wait for next timer interrupt. Isn't it an issue?

Best Regards,
Huang Ying

author
#4 lore raw

Re: [PATCH -mm 1/2] irq_work, Use llist in irq_work

Quoted 23 lines · Huang Ying
Quoted 16 lines
Quoted 16 lines
kernel/irq_work.c +4 -8
-static void __irq_work_queue(struct irq_work *entry)
+static void __irq_work_queue(struct irq_work *work)
 {
-	struct irq_work *next;
+	struct irq_work_list *irq_work_list;
 
-	preempt_disable();
+	irq_work_list = &get_cpu_var(irq_work_lists);
 
-	do {
-		next = __this_cpu_read(irq_work_list);
-		/* Can assign non-atomic because we keep the flags set. */
-		entry->next = next_flags(next, IRQ_WORK_FLAGS);
-	} while (this_cpu_cmpxchg(irq_work_list, next, entry) != next);
+	llist_add(&work->llnode, &irq_work_list->llist);
Quoted 6 lines

kernel/irq_work.c void __weak arch_irq_work_raise(void) · line 75

Quoted 4 lines
kernel/irq_work.c +1 -1
 	/* The list was empty, raise self-interrupt to start processing. */
-	if (!irq_work_next(entry))
+	if (!test_and_set_bit(LIST_NONEMPTY_BIT, &irq_work_list->flags))
 		arch_irq_work_raise();

So why can't you simply test work->llnode->next?

Quoted 2 lines

Yes. That is better. Even if there may be a small race window, it is not a big issue to raise one extra self interrupt seldom.

Remember something about this. I didn't test work->llnode->next here because I didn't want expose the implementation details like that here.
How about make llist_add() return whether list is empty before adding? Because it will be an inline function, that should be optimized out if
the caller do not need the information.

Best Regards,
Huang Ying

#5 lore raw

Re: [PATCH -mm 1/2] irq_work, Use llist in irq_work

Quoted 24 lines · Huang Ying
Quoted 23 lines · Huang Ying
Quoted 22 lines
Quoted 10 lines
kernel/irq_work.c +3 -4
-static void __irq_work_queue(struct irq_work *entry)
+static void __irq_work_queue(struct irq_work *work)
 {
-	struct irq_work *next;
+	struct irq_work_list *irq_work_list;
 
-	preempt_disable();
+	irq_work_list = &get_cpu_var(irq_work_lists);
 
-	do {

kernel/irq_work.c void __weak arch_irq_work_raise(void) · line 75

Quoted 10 lines
kernel/irq_work.c +2 -5
-		next = __this_cpu_read(irq_work_list);
-		/* Can assign non-atomic because we keep the flags set. */
-		entry->next = next_flags(next, IRQ_WORK_FLAGS);
-	} while (this_cpu_cmpxchg(irq_work_list, next, entry) != next);
+	llist_add(&work->llnode, &irq_work_list->llist);
 
 	/* The list was empty, raise self-interrupt to start processing. */
-	if (!irq_work_next(entry))
+	if (!test_and_set_bit(LIST_NONEMPTY_BIT, &irq_work_list->flags))
 		arch_irq_work_raise();

So why can't you simply test work->llnode->next?

Quoted 8 lines
Quoted 2 lines

Yes. That is better. Even if there may be a small race window, it is not a big issue to raise one extra self interrupt seldom.

Remember something about this. I didn't test work->llnode->next here because I didn't want expose the implementation details like that here.
How about make llist_add() return whether list is empty before adding? Because it will be an inline function, that should be optimized out if
the caller do not need the information.

You could also use llist_empty() although that brings me to that ACCESS_ONCE thing in there, what's the point?

author
#6 lore raw

Re: [PATCH -mm 1/2] irq_work, Use llist in irq_work

Quoted 28 lines · Peter Zijlstra
Quoted 27 lines · Huang Ying
Quoted 25 lines · Huang Ying
Quoted 16 lines
Quoted 16 lines
kernel/irq_work.c +4 -8
-static void __irq_work_queue(struct irq_work *entry)
+static void __irq_work_queue(struct irq_work *work)
 {
-	struct irq_work *next;
+	struct irq_work_list *irq_work_list;
 
-	preempt_disable();
+	irq_work_list = &get_cpu_var(irq_work_lists);
 
-	do {
-		next = __this_cpu_read(irq_work_list);
-		/* Can assign non-atomic because we keep the flags set. */
-		entry->next = next_flags(next, IRQ_WORK_FLAGS);
-	} while (this_cpu_cmpxchg(irq_work_list, next, entry) != next);
+	llist_add(&work->llnode, &irq_work_list->llist);
Quoted 6 lines

kernel/irq_work.c void __weak arch_irq_work_raise(void) · line 75

Quoted 4 lines
kernel/irq_work.c +1 -1
 	/* The list was empty, raise self-interrupt to start processing. */
-	if (!irq_work_next(entry))
+	if (!test_and_set_bit(LIST_NONEMPTY_BIT, &irq_work_list->flags))
 		arch_irq_work_raise();

So why can't you simply test work->llnode->next?

Yes. That is better. Even if there may be a small race window, it is not a big issue to raise one extra self interrupt seldom.

Quoted 8 lines
Quoted 5 lines

Remember something about this. I didn't test work->llnode->next here because I didn't want expose the implementation details like that here.
How about make llist_add() return whether list is empty before adding? Because it will be an inline function, that should be optimized out if
the caller do not need the information.

You could also use llist_empty() although that brings me to that ACCESS_ONCE thing in there, what's the point?

Something as follow with llist_empty() seems not work.

  empty = llist_empty(irq_work_list);
  llist_add(&work->llnode, irq_work_list);
  if (empty)
          arch_irq_work_raise();

Because irq_work IRQ handler or timer IRQ handler may be executed just before "llist_add(&work->llnode, irq_work_list)", so that, although "empty == false", arch_irq_work_raise() still should be executed.

Can you tell me how to that with llist_empty()?

For ACCESS_ONCE, Mathiew suggest me to add it,

Hi, Mathiew,

Can you explain why ACCESS_ONCE should be used here?

Best Regards,
Huang Ying

#7 lore raw

Re: [PATCH -mm 1/2] irq_work, Use llist in irq_work

Quoted 10 lines · Huang Ying

Something as follow with llist_empty() seems not work.

  empty = llist_empty(irq_work_list);
  llist_add(&work->llnode, irq_work_list);
  if (empty)
          arch_irq_work_raise();

Because irq_work IRQ handler or timer IRQ handler may be executed just before "llist_add(&work->llnode, irq_work_list)", so that, although "empty == false", arch_irq_work_raise() still should be executed.

Right, I was thinking:

	llist_add(&work->llist, irq_work_list);
	if (llist_empty(&work->llist))
		arch_irq_work_raise();

And then ran into the difference between llist_node and llist_head. Now we could sort that by introducing llist_next() and write it like:

	if (!llist_next(&work->list))
		arch_irq_work_raise();
author
#8 lore raw

Re: [PATCH -mm 1/2] irq_work, Use llist in irq_work

Quoted 18 lines · Peter Zijlstra
Quoted 10 lines · Huang Ying

Something as follow with llist_empty() seems not work.

  empty = llist_empty(irq_work_list);
  llist_add(&work->llnode, irq_work_list);
  if (empty)
          arch_irq_work_raise();

Because irq_work IRQ handler or timer IRQ handler may be executed just before "llist_add(&work->llnode, irq_work_list)", so that, although "empty == false", arch_irq_work_raise() still should be executed.

Right, I was thinking:

	llist_add(&work->llist, irq_work_list);
	if (llist_empty(&work->llist))
		arch_irq_work_raise();
Quoted 6 lines

And then ran into the difference between llist_node and llist_head. Now we could sort that by introducing llist_next() and write it like:

	if (!llist_next(&work->list))
		arch_irq_work_raise();

This reveals some implementation details of llist. But it will reveal some implementation details to make llist_add() return whether list is empty before adding as Mathieu pointed out. So I think something like this or just to check work->list->next should be acceptable.

Best Regards,
Huang Ying

#9 lore raw

Re: [PATCH -mm 1/2] irq_work, Use llist in irq_work

Quoted 22 lines · * Huang Ying ([email protected]) wrote:
Quoted 21 lines · Peter Zijlstra
Quoted 10 lines · Huang Ying

Something as follow with llist_empty() seems not work.

  empty = llist_empty(irq_work_list);
  llist_add(&work->llnode, irq_work_list);
  if (empty)
          arch_irq_work_raise();

Because irq_work IRQ handler or timer IRQ handler may be executed just before "llist_add(&work->llnode, irq_work_list)", so that, although "empty == false", arch_irq_work_raise() still should be executed.

Right, I was thinking:

	llist_add(&work->llist, irq_work_list);
	if (llist_empty(&work->llist))
		arch_irq_work_raise();

And then ran into the difference between llist_node and llist_head. Now we could sort that by introducing llist_next() and write it like:

Quoted 8 lines
Quoted 3 lines
	if (!llist_next(&work->list))
		arch_irq_work_raise();

This reveals some implementation details of llist. But it will reveal some implementation details to make llist_add() return whether list is empty before adding as Mathieu pointed out. So I think something like this or just to check work->list->next should be acceptable.

No. These solutions all appear to have some relatively high level of ugliness and expose too much of the structure internals.

I'll submit a patch to change the API from llist to llstack shortly for comments.

Thanks,

Mathieu

Signature · 4 lines
Mathieu Desnoyers
Operating System Efficiency R&D Consultant
EfficiOS Inc.
http://www.efficios.com
#10 lore raw

Re: [PATCH -mm 1/2] irq_work, Use llist in irq_work

In reply to Huang Ying
Quoted 26 lines · * Huang Ying ([email protected]) wrote:
Quoted 25 lines · Peter Zijlstra
Quoted 24 lines · Huang Ying
Quoted 23 lines · Huang Ying
Quoted 22 lines
Quoted 10 lines
kernel/irq_work.c +3 -4
-static void __irq_work_queue(struct irq_work *entry)
+static void __irq_work_queue(struct irq_work *work)
 {
-	struct irq_work *next;
+	struct irq_work_list *irq_work_list;
 
-	preempt_disable();
+	irq_work_list = &get_cpu_var(irq_work_lists);
 
-	do {

kernel/irq_work.c void __weak arch_irq_work_raise(void) · line 75

Quoted 10 lines
kernel/irq_work.c +2 -5
-		next = __this_cpu_read(irq_work_list);
-		/* Can assign non-atomic because we keep the flags set. */
-		entry->next = next_flags(next, IRQ_WORK_FLAGS);
-	} while (this_cpu_cmpxchg(irq_work_list, next, entry) != next);
+	llist_add(&work->llnode, &irq_work_list->llist);
 
 	/* The list was empty, raise self-interrupt to start processing. */
-	if (!irq_work_next(entry))
+	if (!test_and_set_bit(LIST_NONEMPTY_BIT, &irq_work_list->flags))
 		arch_irq_work_raise();

So why can't you simply test work->llnode->next?

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 2 lines

Yes. That is better. Even if there may be a small race window, it is not a big issue to raise one extra self interrupt seldom.

Remember something about this. I didn't test work->llnode->next here because I didn't want expose the implementation details like that here.
How about make llist_add() return whether list is empty before adding? Because it will be an inline function, that should be optimized out if
the caller do not need the information.

Yes, that would make sense.

something like

static inline
struct llist_node *llist_add(struct llist_node *new, struct llist_head *head)

{
	struct llist_node *entry, *old_entry;
#ifndef CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG
	BUG_ON(in_nmi());

#endif

	entry = head->first;
	do {
		old_entry = entry;
		new->next = entry;
		cpu_relax();
	} while ((entry = cmpxchg(&head->first, old_entry, new)) != old_entry);
        return old_entry;
}

Where llist_add returns NULL if the list was initially empty, else returns non-null. This return value usage should be documented in the function header, of course.

BUT, big warning here: this whole thing _should_ be renamed as a "lockless stack" rather than a "lockless list". Really. I said it in the past, and here I see another example showing why we should. The only reason why we can get away this easily with knowing atomically if the structure was empty prior to the insertion is because this "list" hehaves like a stack (LIFO). I foresee we'll need to add "lockless queues" at some point (FIFO), which has the ability to enqueue/dequeue concurrently without sharing the same cache-lines when the queue is non-empty. Within that kind of queue structure, knowing if the queue was empty prior to insertion would become a bottleneck, so I would not advise to make that part of _that_ API, which would require to add a new "llqueue" API. And at that point, the "llist" vs "llqueue" semantic would become really confusing. Maybe "llstack" would be more appropriate here instead of llist ? Or llfifo ? The API we can provide with a lock-less structure does not only depend on how the elements are organized, but also on the operations allowed on the structure. So the API should reflect that. So I'm saying this warning again: if we don't fix that now, I think we're heading into a lock-free structure namespacing trainwreck that will limit our ability to add other lock-free operations due vague naming that does not take the operations allowed on the structure into consideration, combined with API constraints permitted by a specific given behavior (e.g. FIFO semantic) that tend to define these lock-free APIs.

I've been working on lock-free structures myself in Userspace RCU: I have lock-free stack and queue, wait-free stack, wait-free queue, and (work in progress), RCU red-black tree (not lock-free), and lock-free RCU expandable hash table. If we plan namespacing of lock-free data structure correctly, I think there will be room for very interesting performance and scalability improvements in the future.

Quoted 15 lines
Quoted 3 lines

You could also use llist_empty() although that brings me to that ACCESS_ONCE thing in there, what's the point?

Something as follow with llist_empty() seems not work.

  empty = llist_empty(irq_work_list);
  llist_add(&work->llnode, irq_work_list);
  if (empty)
          arch_irq_work_raise();

Because irq_work IRQ handler or timer IRQ handler may be executed just before "llist_add(&work->llnode, irq_work_list)", so that, although "empty == false", arch_irq_work_raise() still should be executed.

Quoted 8 lines

Can you tell me how to that with llist_empty()?

For ACCESS_ONCE, Mathiew suggest me to add it,

Hi, Mathiew,

Can you explain why ACCESS_ONCE should be used here?

It was mainly to force the compiler to re-fetch the head->first value from memory in busy-waiting loops. So even though the right practice is to have a cpu_relax() in the body of the loop (which would force the refetch due to the compiler barrier), having the ACCESS_ONCE in llist_empty() should not hurt much. It also seems to be what atomic*.h does (volatile read), so I guess the expected behavior wrt atomic accesses are to behave as volatile, hence my recommendation to make this llist_empty a volatile access. Quoting my previous email on that topic:

"Would it make sense to do:

return ACCESS_ONCE(head->first) == NULL;

instead ? Otherwise the compiler can choose to keep the result around in registers without re-reading (e.g. busy waiting loop)."

So I was not implying it was strictly required, merely wondering whether it should be added.

Best regards,

Mathieu

Quoted 3 lines

Best Regards,
Huang Ying

Signature · 4 lines
Mathieu Desnoyers
Operating System Efficiency R&D Consultant
EfficiOS Inc.
http://www.efficios.com
#11 lore raw

Re: [PATCH -mm 1/2] irq_work, Use llist in irq_work

Quoted 62 lines · * Mathieu Desnoyers ([email protected]) wrote:
Quoted 26 lines · * Huang Ying ([email protected]) wrote:
Quoted 25 lines · Peter Zijlstra
Quoted 24 lines · Huang Ying
Quoted 23 lines · Huang Ying
Quoted 22 lines
Quoted 10 lines
(quoted diff) +3 -4
-static void __irq_work_queue(struct irq_work *entry)
+static void __irq_work_queue(struct irq_work *work)
 {
-	struct irq_work *next;
+	struct irq_work_list *irq_work_list;
 
-	preempt_disable();
+	irq_work_list = &get_cpu_var(irq_work_lists);
 
-	do {

Review

Quoted 10 lines
(quoted diff) +2 -5
-		next = __this_cpu_read(irq_work_list);
-		/* Can assign non-atomic because we keep the flags set. */
-		entry->next = next_flags(next, IRQ_WORK_FLAGS);
-	} while (this_cpu_cmpxchg(irq_work_list, next, entry) != next);
+	llist_add(&work->llnode, &irq_work_list->llist);
 
 	/* The list was empty, raise self-interrupt to start processing. */
-	if (!irq_work_next(entry))
+	if (!test_and_set_bit(LIST_NONEMPTY_BIT, &irq_work_list->flags))
 		arch_irq_work_raise();

So why can't you simply test work->llnode->next?

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 2 lines

Yes. That is better. Even if there may be a small race window, it is not a big issue to raise one extra self interrupt seldom.

Remember something about this. I didn't test work->llnode->next here because I didn't want expose the implementation details like that here.
How about make llist_add() return whether list is empty before adding? Because it will be an inline function, that should be optimized out if
the caller do not need the information.

Yes, that would make sense.

something like

static inline
struct llist_node *llist_add(struct llist_node *new, struct llist_head *head)

{
	struct llist_node *entry, *old_entry;
#ifndef CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG
	BUG_ON(in_nmi());

#endif

	entry = head->first;
	do {
		old_entry = entry;
		new->next = entry;
		cpu_relax();
	} while ((entry = cmpxchg(&head->first, old_entry, new)) != old_entry);
        return old_entry;
}

Where llist_add returns NULL if the list was initially empty, else returns non-null. This return value usage should be documented in the function header, of course.

Quoted 14 lines

BUT, big warning here: this whole thing _should_ be renamed as a "lockless stack" rather than a "lockless list". Really. I said it in the past, and here I see another example showing why we should. The only reason why we can get away this easily with knowing atomically if the structure was empty prior to the insertion is because this "list" hehaves like a stack (LIFO). I foresee we'll need to add "lockless queues" at some point (FIFO), which has the ability to enqueue/dequeue concurrently without sharing the same cache-lines when the queue is non-empty. Within that kind of queue structure, knowing if the queue was empty prior to insertion would become a bottleneck, so I would not advise to make that part of _that_ API, which would require to add a new "llqueue" API. And at that point, the "llist" vs "llqueue" semantic would become really confusing. Maybe "llstack" would be more appropriate here instead of llist ? Or llfifo ? The API we can provide with a

I meant lllifo here, sorry. (3 'l's in a row is kind of messy though).

Thanks,

Mathieu

Quoted 74 lines

lock-less structure does not only depend on how the elements are organized, but also on the operations allowed on the structure. So the API should reflect that. So I'm saying this warning again: if we don't fix that now, I think we're heading into a lock-free structure namespacing trainwreck that will limit our ability to add other lock-free operations due vague naming that does not take the operations allowed on the structure into consideration, combined with API constraints permitted by a specific given behavior (e.g. FIFO semantic) that tend to define these lock-free APIs.

I've been working on lock-free structures myself in Userspace RCU: I have lock-free stack and queue, wait-free stack, wait-free queue, and (work in progress), RCU red-black tree (not lock-free), and lock-free RCU expandable hash table. If we plan namespacing of lock-free data structure correctly, I think there will be room for very interesting performance and scalability improvements in the future.

Quoted 15 lines
Quoted 3 lines

You could also use llist_empty() although that brings me to that ACCESS_ONCE thing in there, what's the point?

Something as follow with llist_empty() seems not work.

  empty = llist_empty(irq_work_list);
  llist_add(&work->llnode, irq_work_list);
  if (empty)
          arch_irq_work_raise();

Because irq_work IRQ handler or timer IRQ handler may be executed just before "llist_add(&work->llnode, irq_work_list)", so that, although "empty == false", arch_irq_work_raise() still should be executed.

Quoted 8 lines

Can you tell me how to that with llist_empty()?

For ACCESS_ONCE, Mathiew suggest me to add it,

Hi, Mathiew,

Can you explain why ACCESS_ONCE should be used here?

It was mainly to force the compiler to re-fetch the head->first value from memory in busy-waiting loops. So even though the right practice is to have a cpu_relax() in the body of the loop (which would force the refetch due to the compiler barrier), having the ACCESS_ONCE in llist_empty() should not hurt much. It also seems to be what atomic*.h does (volatile read), so I guess the expected behavior wrt atomic accesses are to behave as volatile, hence my recommendation to make this llist_empty a volatile access. Quoting my previous email on that topic:

"Would it make sense to do:

return ACCESS_ONCE(head->first) == NULL;

instead ? Otherwise the compiler can choose to keep the result around in registers without re-reading (e.g. busy waiting loop)."

So I was not implying it was strictly required, merely wondering whether it should be added.

Best regards,

Mathieu

Quoted 3 lines

Best Regards,
Huang Ying

Signature · 4 lines
Mathieu Desnoyers
Operating System Efficiency R&D Consultant
EfficiOS Inc.
http://www.efficios.com
Signature · 4 lines
Mathieu Desnoyers
Operating System Efficiency R&D Consultant
EfficiOS Inc.
http://www.efficios.com
author
#12 lore raw

Re: [PATCH -mm 1/2] irq_work, Use llist in irq_work

In reply to Mathieu Desnoyers
Quoted 77 lines · Mathieu Desnoyers
Quoted 26 lines · * Huang Ying ([email protected]) wrote:
Quoted 25 lines · Peter Zijlstra
Quoted 24 lines · Huang Ying
Quoted 23 lines · Huang Ying
Quoted 22 lines
Quoted 10 lines
(quoted diff) +3 -4
-static void __irq_work_queue(struct irq_work *entry)
+static void __irq_work_queue(struct irq_work *work)
 {
-	struct irq_work *next;
+	struct irq_work_list *irq_work_list;
 
-	preempt_disable();
+	irq_work_list = &get_cpu_var(irq_work_lists);
 
-	do {

Review

Quoted 10 lines
(quoted diff) +2 -5
-		next = __this_cpu_read(irq_work_list);
-		/* Can assign non-atomic because we keep the flags set. */
-		entry->next = next_flags(next, IRQ_WORK_FLAGS);
-	} while (this_cpu_cmpxchg(irq_work_list, next, entry) != next);
+	llist_add(&work->llnode, &irq_work_list->llist);
 
 	/* The list was empty, raise self-interrupt to start processing. */
-	if (!irq_work_next(entry))
+	if (!test_and_set_bit(LIST_NONEMPTY_BIT, &irq_work_list->flags))
 		arch_irq_work_raise();

So why can't you simply test work->llnode->next?

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 2 lines

Yes. That is better. Even if there may be a small race window, it is not a big issue to raise one extra self interrupt seldom.

Remember something about this. I didn't test work->llnode->next here because I didn't want expose the implementation details like that here.
How about make llist_add() return whether list is empty before adding? Because it will be an inline function, that should be optimized out if
the caller do not need the information.

Yes, that would make sense.

something like

static inline
struct llist_node *llist_add(struct llist_node *new, struct llist_head *head)

{
	struct llist_node *entry, *old_entry;
#ifndef CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG
	BUG_ON(in_nmi());

#endif

	entry = head->first;
	do {
		old_entry = entry;
		new->next = entry;
		cpu_relax();
	} while ((entry = cmpxchg(&head->first, old_entry, new)) != old_entry);
        return old_entry;
}

Where llist_add returns NULL if the list was initially empty, else returns non-null. This return value usage should be documented in the function header, of course.

BUT, big warning here: this whole thing _should_ be renamed as a "lockless stack" rather than a "lockless list". Really. I said it in the past, and here I see another example showing why we should. The only reason why we can get away this easily with knowing atomically if the structure was empty prior to the insertion is because this "list" hehaves like a stack (LIFO). I foresee we'll need to add "lockless queues" at some point (FIFO), which has the ability to enqueue/dequeue concurrently without sharing the same cache-lines when the queue is non-empty. Within that kind of queue structure, knowing if the queue was empty prior to insertion would become a bottleneck, so I would not advise to make that part of _that_ API, which would require to add a new "llqueue" API. And at that point, the "llist" vs "llqueue" semantic would become really confusing. Maybe "llstack" would be more appropriate here instead of llist ? Or llfifo ? The API we can provide with a lock-less structure does not only depend on how the elements are

Quoted 8 lines

organized, but also on the operations allowed on the structure. So the API should reflect that. So I'm saying this warning again: if we don't fix that now, I think we're heading into a lock-free structure namespacing trainwreck that will limit our ability to add other lock-free operations due vague naming that does not take the operations allowed on the structure into consideration, combined with API constraints permitted by a specific given behavior (e.g. FIFO semantic) that tend to define these lock-free APIs.

I remember the previous consensus between us is to keep the API of llist and may change its implementation in the future. But it seems that define a really general llist API is too hard. But fortunately, because llist is just inside kernel (not like a user space library, which is used by various applications), we can change its name in the future if it is needed.

Quoted 6 lines

I've been working on lock-free structures myself in Userspace RCU: I have lock-free stack and queue, wait-free stack, wait-free queue, and (work in progress), RCU red-black tree (not lock-free), and lock-free RCU expandable hash table. If we plan namespacing of lock-free data structure correctly, I think there will be room for very interesting performance and scalability improvements in the future.

That is interesting. But we need find the user in Linux kernel first.

Quoted 35 lines
Quoted 15 lines
Quoted 3 lines

You could also use llist_empty() although that brings me to that ACCESS_ONCE thing in there, what's the point?

Something as follow with llist_empty() seems not work.

  empty = llist_empty(irq_work_list);
  llist_add(&work->llnode, irq_work_list);
  if (empty)
          arch_irq_work_raise();

Because irq_work IRQ handler or timer IRQ handler may be executed just before "llist_add(&work->llnode, irq_work_list)", so that, although "empty == false", arch_irq_work_raise() still should be executed.

Quoted 8 lines

Can you tell me how to that with llist_empty()?

For ACCESS_ONCE, Mathiew suggest me to add it,

Hi, Mathiew,

Can you explain why ACCESS_ONCE should be used here?

It was mainly to force the compiler to re-fetch the head->first value from memory in busy-waiting loops. So even though the right practice is to have a cpu_relax() in the body of the loop (which would force the refetch due to the compiler barrier), having the ACCESS_ONCE in llist_empty() should not hurt much. It also seems to be what atomic*.h does (volatile read), so I guess the expected behavior wrt atomic accesses are to behave as volatile, hence my recommendation to make this llist_empty a volatile access. Quoting my previous email on that topic:

"Would it make sense to do:

Quoted 7 lines

return ACCESS_ONCE(head->first) == NULL;

instead ? Otherwise the compiler can choose to keep the result around in registers without re-reading (e.g. busy waiting loop)."

So I was not implying it was strictly required, merely wondering whether it should be added.

Thanks for your explanation. I should refer to our previous email firstly. I think your point is reasonable.

Best Regards,
Huang Ying

#13 lore raw

Re: [PATCH -mm 1/2] irq_work, Use llist in irq_work

Quoted 87 lines · * Huang Ying ([email protected]) wrote:
Quoted 77 lines · Mathieu Desnoyers
Quoted 26 lines · * Huang Ying ([email protected]) wrote:
Quoted 25 lines · Peter Zijlstra
Quoted 24 lines · Huang Ying
Quoted 23 lines · Huang Ying
Quoted 22 lines
Quoted 10 lines
(quoted diff) +3 -4
-static void __irq_work_queue(struct irq_work *entry)
+static void __irq_work_queue(struct irq_work *work)
 {
-	struct irq_work *next;
+	struct irq_work_list *irq_work_list;
 
-	preempt_disable();
+	irq_work_list = &get_cpu_var(irq_work_lists);
 
-	do {

Review

Quoted 10 lines
(quoted diff) +2 -5
-		next = __this_cpu_read(irq_work_list);
-		/* Can assign non-atomic because we keep the flags set. */
-		entry->next = next_flags(next, IRQ_WORK_FLAGS);
-	} while (this_cpu_cmpxchg(irq_work_list, next, entry) != next);
+	llist_add(&work->llnode, &irq_work_list->llist);
 
 	/* The list was empty, raise self-interrupt to start processing. */
-	if (!irq_work_next(entry))
+	if (!test_and_set_bit(LIST_NONEMPTY_BIT, &irq_work_list->flags))
 		arch_irq_work_raise();

So why can't you simply test work->llnode->next?

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 2 lines

Yes. That is better. Even if there may be a small race window, it is not a big issue to raise one extra self interrupt seldom.

Remember something about this. I didn't test work->llnode->next here because I didn't want expose the implementation details like that here.
How about make llist_add() return whether list is empty before adding? Because it will be an inline function, that should be optimized out if
the caller do not need the information.

Yes, that would make sense.

something like

static inline
struct llist_node *llist_add(struct llist_node *new, struct llist_head *head)

{
	struct llist_node *entry, *old_entry;
#ifndef CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG
	BUG_ON(in_nmi());

#endif

	entry = head->first;
	do {
		old_entry = entry;
		new->next = entry;
		cpu_relax();
	} while ((entry = cmpxchg(&head->first, old_entry, new)) != old_entry);
        return old_entry;
}

Where llist_add returns NULL if the list was initially empty, else returns non-null. This return value usage should be documented in the function header, of course.

BUT, big warning here: this whole thing _should_ be renamed as a "lockless stack" rather than a "lockless list". Really. I said it in the past, and here I see another example showing why we should. The only reason why we can get away this easily with knowing atomically if the structure was empty prior to the insertion is because this "list" hehaves like a stack (LIFO). I foresee we'll need to add "lockless queues" at some point (FIFO), which has the ability to enqueue/dequeue concurrently without sharing the same cache-lines when the queue is non-empty. Within that kind of queue structure, knowing if the queue was empty prior to insertion would become a bottleneck, so I would not advise to make that part of _that_ API, which would require to add a new "llqueue" API. And at that point, the "llist" vs "llqueue" semantic would become really confusing. Maybe "llstack" would be more appropriate here instead of llist ? Or llfifo ? The API we can provide with a lock-less structure does not only depend on how the elements are

Quoted 8 lines

organized, but also on the operations allowed on the structure. So the API should reflect that. So I'm saying this warning again: if we don't fix that now, I think we're heading into a lock-free structure namespacing trainwreck that will limit our ability to add other lock-free operations due vague naming that does not take the operations allowed on the structure into consideration, combined with API constraints permitted by a specific given behavior (e.g. FIFO semantic) that tend to define these lock-free APIs.

Quoted 2 lines

I remember the previous consensus between us is to keep the API of llist and may change its implementation in the future.

Previously, the concensus was to keep the "llist" name until we could gather evidence that this name is inappropriate. The feature request from Peter Zijlstra outlines very well the need for an llstack API that allows the push operation to return whether the stack was empty or not before the operation.

Quoted 1 line

But it seems that define a really general llist API is too hard.

As I've been saying over and over: lock-free API naming needs to take into account the operations allowed on the structure, so "list" really isn't precise enough.

Quoted 4 lines

But fortunately, because
llist is just inside kernel (not like a user space library, which is used by various applications), we can change its name in the future if it is needed.

With the feature request we have here, it appears the time to change it has already come.

Quoted 9 lines
Quoted 7 lines

I've been working on lock-free structures myself in Userspace RCU: I have lock-free stack and queue, wait-free stack, wait-free queue, and (work in progress), RCU red-black tree (not lock-free), and lock-free RCU expandable hash table. If we plan namespacing of lock-free data structure correctly, I think there will be room for very interesting performance and scalability improvements in the future.

That is interesting. But we need find the user in Linux kernel first.

Sure. That will happen in due time. :)

Quoted 38 lines
Quoted 38 lines
Quoted 16 lines
Quoted 4 lines

You could also use llist_empty() although that brings me to that ACCESS_ONCE thing in there, what's the point?

Something as follow with llist_empty() seems not work.

  empty = llist_empty(irq_work_list);
  llist_add(&work->llnode, irq_work_list);
  if (empty)
          arch_irq_work_raise();

Because irq_work IRQ handler or timer IRQ handler may be executed just before "llist_add(&work->llnode, irq_work_list)", so that, although "empty == false", arch_irq_work_raise() still should be executed.

Quoted 8 lines

Can you tell me how to that with llist_empty()?

For ACCESS_ONCE, Mathiew suggest me to add it,

Hi, Mathiew,

Can you explain why ACCESS_ONCE should be used here?

It was mainly to force the compiler to re-fetch the head->first value from memory in busy-waiting loops. So even though the right practice is to have a cpu_relax() in the body of the loop (which would force the refetch due to the compiler barrier), having the ACCESS_ONCE in llist_empty() should not hurt much. It also seems to be what atomic*.h does (volatile read), so I guess the expected behavior wrt atomic accesses are to behave as volatile, hence my recommendation to make this llist_empty a volatile access. Quoting my previous email on that topic:

"Would it make sense to do:

return ACCESS_ONCE(head->first) == NULL;

Quoted 8 lines
Quoted 5 lines

instead ? Otherwise the compiler can choose to keep the result around in registers without re-reading (e.g. busy waiting loop)."

So I was not implying it was strictly required, merely wondering whether it should be added.

Thanks for your explanation. I should refer to our previous email firstly. I think your point is reasonable.

Thanks,

Mathieu

Quoted 3 lines

Best Regards,
Huang Ying

Signature · 4 lines
Mathieu Desnoyers
Operating System Efficiency R&D Consultant
EfficiOS Inc.
http://www.efficios.com
#14 lore raw

Re: [PATCH -mm 1/2] irq_work, Use llist in irq_work

In reply to Huang Ying
Quoted 1 line · Huang Ying

You mean we should not use cpu_relax before the first cmpxchg?

Yeah, that's just wasting time for no reason..

Quoted 10 lines

You suggest something as follow?

void llist_add(struct llist_node *new, struct llist_head *head)

{
        struct llist_node *entry, *old_entry;
#ifndef CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG
        BUG_ON(in_nmi());

#endif

Quoted 10 lines
        entry = head->first;
        for (;;) {
                old_entry = entry;
                new->next = entry;
                entry = cmpxchg(&head->first, old_entry, new);
                if (entry == old_entry)
                        break;
                cpu_relax();
        }
}

If you insist on having cpu_relax(), then yes that's lots better. Also avoids the assignment in your conditional. Thing with cpu_relax() is that its only beneficial in the highly contended case and degrade light/un-contended loads.

Also, just noticed, why do you have different list_head/list_node structures? They're the same, a single pointer.

Quoted 8 lines
Quoted 4 lines

and loose the get/put
cpu muck? The existing preempt_disable/enable() are already superfluous and could be removed, you just made all this way more horrid than need be.

Will it cause race condition to remove preempt_disable/enable? Considering something as follow:

Quoted 7 lines

- get irq_work_list of CPU A
- queue irq_work into irq_work_list of CPU A
- preempted and resumed execution on CPU B
- arch_irq_work_raise on CPU B

irq_work_run on CPU B will do nothing. While irq_work need to wait for next timer interrupt. Isn't it an issue?

Yes that's unfortunate, the current version would work just fine without preempt but that's because of the this_cpu_* ops foo.

Not sure it would make sense to add a special this_cpu_llist_add() or so.. esp seeing that this_cpu_* is basically x86-only.

author
#15 lore raw

Re: [PATCH -mm 1/2] irq_work, Use llist in irq_work

Quoted 27 lines · Peter Zijlstra
Quoted 1 line · Huang Ying

You mean we should not use cpu_relax before the first cmpxchg?

Yeah, that's just wasting time for no reason..

Quoted 10 lines

You suggest something as follow?

void llist_add(struct llist_node *new, struct llist_head *head)

{
        struct llist_node *entry, *old_entry;
#ifndef CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG
        BUG_ON(in_nmi());

#endif

Quoted 10 lines
        entry = head->first;
        for (;;) {
                old_entry = entry;
                new->next = entry;
                entry = cmpxchg(&head->first, old_entry, new);
                if (entry == old_entry)
                        break;
                cpu_relax();
        }
}
Quoted 4 lines

If you insist on having cpu_relax(), then yes that's lots better. Also avoids the assignment in your conditional. Thing with cpu_relax() is that its only beneficial in the highly contended case and degrade light/un-contended loads.

Because llist is in library, it may be used in highly contended case and light/un-contended loads. So maybe code as above is best choice for llist.

Quoted 2 lines

Also, just noticed, why do you have different list_head/list_node structures? They're the same, a single pointer.

There is only one structure before (llist_head). Linus give me some comments about that, suggests to use 2 structures. I think his comments are reasonable. Copied here:

"
Btw, looking at the lockless list, I really think it's a mistake to use "struct llist_head" for both the head and for the list linkage.

They are _very_ different. In the <linux/list.h> kind of model, the list_head really *is* the same regardless of where in the list you are, and you can have headless lists (ie a list that doesn't really have an anchor, the list is just comprised of the elements themselves).

But that's not true of the lockless list. There, the head is very much magical, and as I mentioned, you can NOT do a "traverse starting at head" thing. You can only do add/del/clear on the head, and then to traverse you have to have a list *without* the head entry, because it has been removed.

So from a pure type safety angle, I think the lockless list should have clearly separate types for the head and for the actual list entries, because they behave totally differently. When the normal "list_add()" things can be used to add things in the middle - so each list entry really can act as a head - that isn't true at all for the lockless version.
"

Quoted 13 lines
Quoted 13 lines
Quoted 4 lines

and loose the get/put
cpu muck? The existing preempt_disable/enable() are already superfluous and could be removed, you just made all this way more horrid than need be.

Will it cause race condition to remove preempt_disable/enable? Considering something as follow:

- get irq_work_list of CPU A
- queue irq_work into irq_work_list of CPU A
- preempted and resumed execution on CPU B
- arch_irq_work_raise on CPU B

Quoted 8 lines
Quoted 2 lines

irq_work_run on CPU B will do nothing. While irq_work need to wait for next timer interrupt. Isn't it an issue?

Yes that's unfortunate, the current version would work just fine without preempt but that's because of the this_cpu_* ops foo.

Not sure it would make sense to add a special this_cpu_llist_add() or so.. esp seeing that this_cpu_* is basically x86-only.

Even with this_cpu_* ops, it seems that the race condition I described is still possible.

- use this_cpu_cmpxchg to queue irq_work into irq_work_list of CPU A
- preempted and resumed execution on CPU B
- arch_irq_work_raise on CPU B

Do you think so?

Best Regards,
Huang Ying

#16 lore raw

Re: [PATCH -mm 1/2] irq_work, Use llist in irq_work

Quoted 2 lines · Huang Ying

Because llist is in library, it may be used in highly contended case and light/un-contended loads. So maybe code as above is best choice for llist.

Well the thing is, if you're heavily contended you should probably be doing something else..

Quoted 6 lines
Quoted 2 lines

Also, just noticed, why do you have different list_head/list_node structures? They're the same, a single pointer.

There is only one structure before (llist_head). Linus give me some comments about that, suggests to use 2 structures. I think his comments are reasonable. Copied here:

OK

Quoted 8 lines

Even with this_cpu_* ops, it seems that the race condition I described is still possible.

- use this_cpu_cmpxchg to queue irq_work into irq_work_list of CPU A
- preempted and resumed execution on CPU B
- arch_irq_work_raise on CPU B

Do you think so?

No you're right, got my brain inside-out today or so..

author
#17 lore raw

Re: [PATCH -mm 1/2] irq_work, Use llist in irq_work

Quoted 7 lines · Peter Zijlstra
Quoted 2 lines · Huang Ying

Because llist is in library, it may be used in highly contended case and light/un-contended loads. So maybe code as above is best choice for llist.

Well the thing is, if you're heavily contended you should probably be doing something else..

So which solution is preferable?

1) no cpu_relax
2) cpu_relax after first cmpxchg

Personally, I prefer 2). It should have acceptable overhead in ligh/un-contended loads. Do you agree.

Best Regards,
Huang Ying

#18 lore raw

Re: [PATCH -mm 1/2] irq_work, Use llist in irq_work

Quoted 9 lines · Huang Ying
Quoted 7 lines · Peter Zijlstra
Quoted 2 lines · Huang Ying

Because llist is in library, it may be used in highly contended case and light/un-contended loads. So maybe code as above is best choice for llist.

Well the thing is, if you're heavily contended you should probably be doing something else..

Quoted 7 lines

So which solution is preferable?

1) no cpu_relax
2) cpu_relax after first cmpxchg

Personally, I prefer 2). It should have acceptable overhead in ligh/un-contended loads. Do you agree.

I guess so, we can always poke at it again when someone finds something to measure.

author
#19 lore raw

PATCH 2/2 net, rds, Replace xlist in net/rds/xlist.h with llist

In reply to Huang Ying

The functionality of xlist and llist is almost same. This patch replace xlist with llist to avoid code duplication.

Known issues: don't know how to test this, need special hardware?

---
 net/rds/Kconfig   |    1 
 net/rds/ib_rdma.c |  112 ++++++++++++++++++++++++------------------------------
 net/rds/xlist.h   |   80 --------------------------------------
 3 files changed, 52 insertions(+), 141 deletions(-)
 delete mode 100644 net/rds/xlist.h
net/rds/Kconfig +1 -0
@@ -9,6 +9,7 @@ config RDS
 
 config RDS_RDMA
 	tristate "RDS over Infiniband and iWARP"
+	select LLIST
 	depends on RDS && INFINIBAND && INFINIBAND_ADDR_TRANS
 	---help---
 	  Allow RDS to use Infiniband and iWARP as a transport.
net/rds/ib_rdma.c +51 -61

214 lines not on this page · raw message

net/rds/xlist.h deleted +0 -80

80 lines not on this page · raw message