irq_work, Use llist in irq_work
Subsystems (4)
cover irq_work, Use llist in irq_work
Changes:
v2:
- Return whether list is empty before adding in llist_add and use that in __irq_work_queue
[PATCH -mm -v2 1/5] llist, Make all llist functions inline
[PATCH -mm -v2 2/5] llist, Define macro to check NMI safe cmpxchg [PATCH -mm -v2 3/5] llist, Move cpu_relax after cmpxchg
[PATCH -mm -v2 4/5] llist, Return whether list is empty before adding in llist_add [PATCH -mm -v2 5/5] irq_work, Use llist in irq_work
PATCH 1/5 llist, Make all llist functions inline
Because llist may be used in performance critical code path, make all llist functions inline to avoid function calling overhead.
--- drivers/acpi/apei/Kconfig | 1 include/linux/llist.h | 122 +++++++++++++++++++++++++++++++++++++++++-- lib/Kconfig | 3 - lib/Makefile | 2 lib/llist.c | 129 ---------------------------------------------- 5 files changed, 116 insertions(+), 141 deletions(-) delete mode 100644 lib/llist.c
drivers/acpi/apei/Kconfig +0 -1
@@ -14,7 +14,6 @@ config ACPI_APEI_GHES depends on ACPI_APEI && X86 select ACPI_HED select IRQ_WORK - select LLIST select GENERIC_ALLOCATOR help Generic Hardware Error Source provides a way to report
include/linux/llist.h +116 -6
138 lines not on this page · raw message
lib/Kconfig +0 -3
@@ -276,7 +276,4 @@ config CORDIC so its calculations are in fixed point. Modules can select this when they require this function. Module will be called cordic. -config LLIST - bool - endmenu
lib/Makefile +0 -2
@@ -116,8 +116,6 @@ obj-$(CONFIG_CPU_RMAP) += cpu_rmap.o obj-$(CONFIG_CORDIC) += cordic.o -obj-$(CONFIG_LLIST) += llist.o - hostprogs-y := gen_crc32table clean-files := crc32table.h
lib/llist.c deleted +0 -129
129 lines not on this page · raw message
PATCH 2/5 llist, Define macro to check NMI safe cmpxchg
In reply to Huang YingTo make code cleaner and reduce code duplication. Thanks Peter Zijlstra for reminding.
--- include/linux/llist.h | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-)
include/linux/llist.h +10 -12
@@ -70,6 +70,12 @@ struct llist_node {#define LLIST_HEAD_INIT (name) { NULL }#define LLIST_HEAD (name) struct llist_head name = LLIST_HEAD_INIT(name) +#ifdef CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG +#define CHECK_NMI_SAFE_CMPXCHG () +#else +#define CHECK_NMI_SAFE_CMPXCHG () BUG_ON(in_nmi()) +#endif +/** * init_llist_head - initialize lock-less list head * @head: the head for your lock-less list
@@ -147,9 +153,7 @@ static inline void llist_add(struct llis {struct llist_node *entry, *old_entry; -#ifndef CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG -BUG_ON (in_nmi ()); -#endif +CHECK_NMI_SAFE_CMPXCHG (); entry = head->first; do {
@@ -171,9 +175,7 @@ static inline void llist_add_batch(struc {struct llist_node *entry, *old_entry; -#ifndef CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG -BUG_ON (in_nmi ()); -#endif +CHECK_NMI_SAFE_CMPXCHG (); entry = head->first; do {
@@ -201,9 +203,7 @@ static inline struct llist_node *llist_d {struct llist_node *entry, *old_entry, *next; -#ifndef CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG -BUG_ON (in_nmi ()); -#endif +CHECK_NMI_SAFE_CMPXCHG (); entry = head->first; do {
@@ -227,9 +227,7 @@ static inline struct llist_node *llist_d*/ static inline struct llist_node *llist_del_all (struct llist_head *head) { -#ifndef CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG -BUG_ON (in_nmi ()); -#endif +CHECK_NMI_SAFE_CMPXCHG ();return xchg (&head->first,NULL ); }
PATCH 3/5 llist, Move cpu_relax after cmpxchg
In reply to Huang YingIf the first cmpxchg calling succeeds, it is not necessary to use cpu_relax before cmpxchg. So cpu_relax in a busy loop involving cmpxchg should go after cmpxchg instead of before that. This patch fixes this in llist.
--- include/linux/llist.h | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-)
include/linux/llist.h +15 -6
@@ -156,11 +156,14 @@ static inline void llist_add(struct llisCHECK_NMI_SAFE_CMPXCHG (); entry = head->first; -do { +for (;;) { old_entry = entry; new->next = entry; + entry =cmpxchg (&head->first, old_entry, new); +if (entry == old_entry) +break ;cpu_relax (); - }while ((entry =cmpxchg (&head->first, old_entry, new)) != old_entry); + } }/**
@@ -178,11 +181,14 @@ static inline void llist_add_batch(strucCHECK_NMI_SAFE_CMPXCHG (); entry = head->first; -do { +for (;;) { old_entry = entry; new_last->next = entry; + entry =cmpxchg (&head->first, old_entry, new_first); +if (entry == old_entry) +break ;cpu_relax (); - }while ((entry =cmpxchg (&head->first, old_entry, new_first)) != old_entry); + } }/**
@@ -206,13 +212,16 @@ static inline struct llist_node *llist_dCHECK_NMI_SAFE_CMPXCHG (); entry = head->first; -do { +for (;;) {if (entry ==NULL )return NULL ; old_entry = entry; next = entry->next; + entry =cmpxchg (&head->first, old_entry, next); +if (entry == old_entry) +break ;cpu_relax (); - }while ((entry =cmpxchg (&head->first, old_entry, next)) != old_entry); + }return entry; }
PATCH 4/5 llist, Return whether list is empty before adding in llist_add
In reply to Huang YingThis is needed by irq_work. And because list_add_xxx functions are inline, this can be optimized out if not needed by callers.
--- include/linux/llist.h | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-)
include/linux/llist.h +10 -2
@@ -148,8 +148,10 @@ static inline bool llist_empty(const str* llist_add - add a new entry * @new: new entry to be added * @head: the head for your lock-less list +* +* Return whether list is empty before adding. */ -static inline void llist_add (struct llist_node *new,struct llist_head *head) +static inline bool llist_add (struct llist_node *new,struct llist_head *head) {struct llist_node *entry, *old_entry;
@@ -164,6 +166,8 @@ static inline void llist_add(struct llisbreak ;cpu_relax (); } + +return old_entry ==NULL ; }/**
@@ -171,8 +175,10 @@ static inline void llist_add(struct llis* @new_first: first entry in batch to be added * @new_last: last entry in batch to be added * @head: the head for your lock-less list +* +* Return whether list is empty before adding. */ -static inline void llist_add_batch (struct llist_node *new_first, +static inline bool llist_add_batch (struct llist_node *new_first,struct llist_node *new_last,struct llist_head *head) {
@@ -189,6 +195,8 @@ static inline void llist_add_batch(strucbreak ;cpu_relax (); } + +return old_entry ==NULL ; }/**
PATCH 5/5 irq_work, Use llist in irq_work
In reply to Huang YingUse llist in irq_work instead of the lock-less linked list implementation in irq_work to avoid the code duplication.
--- include/linux/irq_work.h | 15 ++++--- kernel/irq_work.c | 92 ++++++++++++++++------------------------------- 2 files changed, 42 insertions(+), 65 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 */
kernel/irq_work.c +33 -59
175 lines not on this page · raw message
Peter Zijlstra
maintainer[PATCH 6/5] llist: Add llist_next()
In reply to Huang YingSubject: llist: Add llist_next()
From: Peter Zijlstra <[email protected]>
Date: Mon Sep 12 13:12:28 CEST 2011
So we don't have to expose the struct list_node members
--- include/linux/llist.h | 5 +++++ kernel/irq_work.c | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-)
include/linux/llist.h +5 -0
@@ -144,6 +144,11 @@ static inline bool llist_empty(const strreturn ACCESS_ONCE (head->first) ==NULL ; } +static inline struct llist_node *llist_next (struct llist_node *node) +{ +return node->next; +} +/** * llist_add - add a new entry * @new: new entry to be added
kernel/irq_work.c +1 -1
@@ -110,7 +110,7 @@ void irq_work_run(void)while (llnode !=NULL ) { work = llist_entry(llnode, struct irq_work, llnode); - llnode = llnode->next; + llnode =llist_next (llnode);/* * Clear the PENDING bit, after this point the @work
Peter Zijlstra
maintainer[PATCH 7/5] sched: Convert to use llist
In reply to Huang YingSubject: sched: Convert to use llist
From: Peter Zijlstra <[email protected]>
Date: Mon Sep 12 13:06:17 CEST 2011
Use the generic llist primitives..
--- include/linux/sched.h | 3 ++- kernel/sched.c | 48 ++++++++++-------------------------------------- 2 files changed, 12 insertions(+), 39 deletions(-)
include/linux/sched.h +2 -1
@@ -90,6 +90,7 @@ struct sched_param {#include <linux/task_io_accounting.h> #include <linux/latencytop.h> #include <linux/cred.h> +#include <linux/llist.h> #include <asm/processor.h>
@@ -1225,7 +1226,7 @@ struct task_struct {unsigned int ptrace;#ifdef CONFIG_SMP -struct task_struct *wake_entry; +struct llist_node wake_entry;int on_cpu;#endif int on_rq;
kernel/sched.c +10 -38
84 lines not on this page · raw message
Peter Zijlstra
maintainer[PATCH 8/5] llist: Remove cpu_relax() usage in cmpxchg loops
In reply to Huang YingSubject: llist: Remove cpu_relax() usage in cmpxchg loops
From: Peter Zijlstra <[email protected]>
Date: Mon Sep 12 15:50:49 CEST 2011
Initial benchmarks show they're a net loss (2 socket wsm):
$ for i in /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor ; do echo performance > $i; done $ echo 4096 32000 64 128 > /proc/sys/kernel/sem $ ./sembench -t 2048 -w 1900 -o 0
Pre:
run time 30 seconds 778936 worker burns per second run time 30 seconds 912190 worker burns per second run time 30 seconds 817506 worker burns per second run time 30 seconds 830870 worker burns per second run time 30 seconds 845056 worker burns per second
Post:
run time 30 seconds 905920 worker burns per second run time 30 seconds 849046 worker burns per second run time 30 seconds 886286 worker burns per second run time 30 seconds 822320 worker burns per second run time 30 seconds 900283 worker burns per second
--- include/linux/llist.h | 3 --- 1 file changed, 3 deletions(-)
include/linux/llist.h +0 -3
@@ -169,7 +169,6 @@ static inline bool llist_add(struct llis entry =cmpxchg (&head->first, old_entry, new);if (entry == old_entry)break ; -cpu_relax (); }return old_entry ==NULL ;
@@ -198,7 +197,6 @@ static inline bool llist_add_batch(struc entry =cmpxchg (&head->first, old_entry, new_first);if (entry == old_entry)break ; -cpu_relax (); }return old_entry ==NULL ;
@@ -233,7 +231,6 @@ static inline struct llist_node *llist_d entry =cmpxchg (&head->first, old_entry, next);if (entry == old_entry)break ; -cpu_relax (); }return entry;
Re: [PATCH 8/5] llist: Remove cpu_relax() usage in cmpxchg loops
Quoted 6 lines · Peter Zijlstra
Subject: llist: Remove
cpu_relax()usage in cmpxchg loops
From: Peter Zijlstra <[email protected]>
Date: Mon Sep 12 15:50:49 CEST 2011Initial benchmarks show they're a net loss (2 socket wsm):
May still save power. however:
include/linux/llist.h static inline bool llist_add(struct llis · line 172
Quoted 4 lines
include/linux/llist.h +0
-1entry =cmpxchg (&head->first, old_entry, new);if (entry == old_entry)break ; -cpu_relax ();
You need at least a barrier() then, otherwise the compiler could change the memory order in the loop.
-Andi
Peter Zijlstra
maintainerRe: [PATCH 8/5] llist: Remove cpu_relax() usage in cmpxchg loops
Quoted 10 lines · Andi Kleen
Quoted 6 lines · Peter Zijlstra
Subject: llist: Remove
cpu_relax()usage in cmpxchg loops
From: Peter Zijlstra <[email protected]>
Date: Mon Sep 12 15:50:49 CEST 2011Initial benchmarks show they're a net loss (2 socket wsm):
May still save power. however:
Quoted 7 lines
include/linux/llist.h static inline bool llist_add(struct llis · line 172
Quoted 4 lines
include/linux/llist.h +0
-1entry =cmpxchg (&head->first, old_entry, new);if (entry == old_entry)break ; -cpu_relax ();You need at least a
barrier()then, otherwise the compiler could change the memory order in the loop.
the cmpxchg is a full memory barrier and compiler barrier.
Re: [PATCH 8/5] llist: Remove cpu_relax() usage in cmpxchg loops
In reply to Andi KleenQuoted 9 lines · * Andi Kleen ([email protected]) wrote:
Quoted 6 lines · Peter Zijlstra
Subject: llist: Remove
cpu_relax()usage in cmpxchg loops
From: Peter Zijlstra <[email protected]>
Date: Mon Sep 12 15:50:49 CEST 2011Initial benchmarks show they're a net loss (2 socket wsm):
May still save power.
Looking at kernel/spinlock.c:
void __lockfunc __raw_##op##_lock(locktype##_t *lock) \
{
[...]
while (!raw_##op##_can_lock(lock) && (lock)->break_lock)\
arch_##op##_relax(&lock->raw_lock); \so basically, in typical locking primitives (spinlock), it looks like lower power consumption is preferred over getting the raw maximal performance in fully contented scenarios.
So what is the rationale for making those lock-less lists retry scheme different from spinlocks here ?
Thanks,
Mathieu
Signature · 4 lines
Mathieu Desnoyers Operating System Efficiency R&D Consultant EfficiOS Inc. http://www.efficios.com
Peter Zijlstra
maintainerRe: [PATCH 8/5] llist: Remove cpu_relax() usage in cmpxchg loops
Quoted 16 lines · Mathieu Desnoyers
Quoted 9 lines · * Andi Kleen ([email protected]) wrote:
Quoted 6 lines · Peter Zijlstra
Subject: llist: Remove
cpu_relax()usage in cmpxchg loops
From: Peter Zijlstra <[email protected]>
Date: Mon Sep 12 15:50:49 CEST 2011Initial benchmarks show they're a net loss (2 socket wsm):
May still save power.
Looking at
kernel/spinlock.c:void
__lockfunc__raw_##op##_lock(locktype##_t *lock) \
{
[...]
Quoted 6 lines
while (!raw_##op##_can_lock(lock) && (lock)->break_lock)\ arch_##op##_relax(&lock->raw_lock); \so basically, in typical locking primitives (spinlock), it looks like lower power consumption is preferred over getting the raw maximal performance in fully contented scenarios.
Who says its about power consumption? That was a baseless claim made by Andi which you propagate as a truth. Typically PAUSE is too short to really save any power and the only gain of having it in loops is to provide a window where another core on the cache domain can have a go.
If power consumption would be the prime concern Intel should fix their damn mwait implementation so we can use that for locks. Local spinners + mwait would give a very power efficient 'spin'-lock.
Quoted 2 lines
So what is the rationale for making those lock-less lists retry scheme different from spinlocks here ?
PAUSE should help on the medium contended case (it would be pointless on heavy contention), but hurt the lightly contended case. We have all kinds of contention spinlocks in the kernel, but we don't as of yet have very contended llist users in the kernel.
Furthermore, I would argue we should avoid growing them, significantly contended atomic ops are bad, use a different scheme.
Peter Zijlstra
maintainerRe: [PATCH 8/5] llist: Remove cpu_relax() usage in cmpxchg loops
In reply to Mathieu DesnoyersQuoted 2 lines · Peter Zijlstra
Furthermore, I would argue we should avoid growing them, significantly contended atomic ops are bad, use a different scheme.
To clarify, plain cmpxchg loops are unfair and unbounded in completion time, and we should therefore avoid any sort of medium/high contention. If you get into that situation you're doing it wrong.
Re: [PATCH 8/5] llist: Remove cpu_relax() usage in cmpxchg loops
In reply to Mathieu DesnoyersQuoted 2 lines
so basically, in typical locking primitives (spinlock), it looks like lower power consumption is preferred over getting the raw maximal
It's not only power, its:
- Allow the other siblings make more progress on SMT
- Do some backoff to stress the interconnect less (this is important on >2S):
A tight loop which constantly writes is a extremly stressfull pattern.
- Save some power by allowing the CPU to do more clock gating
-Andi
Peter Zijlstra
maintainerRe: [PATCH 8/5] llist: Remove cpu_relax() usage in cmpxchg loops
Quoted 8 lines · Andi Kleen
Quoted 2 lines
so basically, in typical locking primitives (spinlock), it looks like lower power consumption is preferred over getting the raw maximal
It's not only power, its:
- Allow the other siblings make more progress on SMT
- Do some backoff to stress the interconnect less (this is important on >2S):
A tight loop which constantly writes is a extremly stressfull pattern.
- Save some power by allowing the CPU to do more clock gating
If you're hitting a cmpxchg hard enough for any of those to make a difference you're doing it wrong.
Re: [PATCH 8/5] llist: Remove cpu_relax() usage in cmpxchg loops
In reply to Peter ZijlstraQuoted 10 lines · Peter Zijlstra
Subject: llist: Remove
cpu_relax()usage in cmpxchg loops
From: Peter Zijlstra<[email protected]>
Date: Mon Sep 12 15:50:49 CEST 2011Initial benchmarks show they're a net loss (2 socket wsm):
$ for i in /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor ; do echo performance> $i; done $ echo 4096 32000 64 128> /proc/sys/kernel/sem $ ./sembench -t 2048 -w 1900 -o 0
We hyperthreading enabled, and were all threads loaded? cpu_relax allows the other thread to make more progress while the spinner relaxes.
Signature · 1 line
error compiling committee.c: too many arguments to function
Peter Zijlstra
maintainerRe: [PATCH 8/5] llist: Remove cpu_relax() usage in cmpxchg loops
Quoted 7 lines · Avi Kivity
Quoted 6 lines · Peter Zijlstra
Subject: llist: Remove
cpu_relax()usage in cmpxchg loops
From: Peter Zijlstra<[email protected]>
Date: Mon Sep 12 15:50:49 CEST 2011Initial benchmarks show they're a net loss (2 socket wsm):
Quoted 7 lines
Quoted 4 lines
$ for i in /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor ; do echo performance> $i; done $ echo 4096 32000 64 128> /proc/sys/kernel/sem $ ./sembench -t 2048 -w 1900 -o 0We hyperthreading enabled, and were all threads loaded?
cpu_relaxallows the other thread to make more progress while the spinner relaxes.
Yeah, with HT enabled, the benchmark runs 2048 tasks and does 1900 task bulk wakeups or so. Forgot the details, but it basically stresses the sleep+wakeup paths like nobodies business.
Re: [PATCH 8/5] llist: Remove cpu_relax() usage in cmpxchg loops
Quoted 13 lines · Peter Zijlstra
Quoted 12 lines · Avi Kivity
Quoted 10 lines · Peter Zijlstra
Subject: llist: Remove
cpu_relax()usage in cmpxchg loops
From: Peter Zijlstra<[email protected]>
Date: Mon Sep 12 15:50:49 CEST 2011Initial benchmarks show they're a net loss (2 socket wsm):
$ for i in /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor ; do echo performance> $i; done $ echo 4096 32000 64 128> /proc/sys/kernel/sem $ ./sembench -t 2048 -w 1900 -o 0
Quoted 6 lines
Quoted 2 lines
We hyperthreading enabled, and were all threads loaded?
cpu_relaxallows the other thread to make more progress while the spinner relaxes.Yeah, with HT enabled, the benchmark runs 2048 tasks and does 1900 task bulk wakeups or so. Forgot the details, but it basically stresses the sleep+wakeup paths like nobodies business.
Ok.
Another issue is that hypervisors use PAUSE to detect a spinning guest and issue a directed yield to another vcpu. But for cmpxchg loops, the "spinner" would just commit on the next loop, no? So I think there's no objection from that front.
Signature · 1 line
error compiling committee.c: too many arguments to function
Peter Zijlstra
maintainerRe: [PATCH 8/5] llist: Remove cpu_relax() usage in cmpxchg loops
Quoted 4 lines · Avi Kivity
Another issue is that hypervisors use PAUSE to detect a spinning guest and issue a directed yield to another vcpu. But for cmpxchg loops, the "spinner" would just commit on the next loop, no? So I think there's no objection from that front.
Right, we shouldn't ever spend a significant amount spinning on a cmpxchg. If we do we need to fix that instead.
Re: [PATCH 8/5] llist: Remove cpu_relax() usage in cmpxchg loops
Quoted 9 lines · Peter Zijlstra
Quoted 4 lines · Avi Kivity
Another issue is that hypervisors use PAUSE to detect a spinning guest and issue a directed yield to another vcpu. But for cmpxchg loops, the "spinner" would just commit on the next loop, no? So I think there's no objection from that front.
Right, we shouldn't ever spend a significant amount spinning on a cmpxchg. If we do we need to fix that instead.
I hate arguing while agreeing, but the issue isn't that we don't spend a significant time spinning, but that there is no owner. Should the other cpu go away, we just pick up a new copy of oldval and complete the transaction.
With spinlocks, even if you hold it for just a single guest cycle, the situation is different. If the vcpu that holds the spinlock is preempted, the spinner is forced to spin until the owner is rescheduled.
Signature · 1 line
error compiling committee.c: too many arguments to function
Peter Zijlstra
maintainerRe: [PATCH 8/5] llist: Remove cpu_relax() usage in cmpxchg loops
Quoted 5 lines · Avi Kivity
I hate arguing while agreeing, but the issue isn't that we don't spend a significant time spinning, but that there is no owner. Should the other cpu go away, we just pick up a new copy of oldval and complete the transaction.
Ah, ok. Yes that too ;-)
Peter Zijlstra
maintainerAndrew, shall I take all 8 patches through the scheduler tree?