SMP call function cleanup
Subsystems (7)
PATCH 1/1 SMP call function cleanup
Hello,
I've been looking into the SMP call function stuff on different archs and found many different functions...
In the common code part there are 2 functions:smp_call_function() // call a function on all CPUs but my ownon_each_cpu() // call a function on all CPUs
Many archs need an additional function to call a function on a specific CPU:
arch-s390:smp_call_function_on()
arch-alpha:smp_call_function_on_cpu()
arch-ia64:smp_call_function_single()
On i386 there is no smp_call_function_single() so they have a workaround with smp_call_function() and testing for smp_processor_id(). Finally the slab allocator has its own on_each_cpu() function:
mm:smp_call_function_all_cpus()
This is somehow inconsistent.
Proposed cleanup:
there are 3 different kinds of SMP calls:
1. all CPUs
2. all CPUs but my own
3. one CPU
only _one_ basic function is needed to implement all variants:
This can be named on_cpu() and has as parameter a cpumask, the function is called on all CPUs specified there:
void on_cpu(void (*func) (void *info), void *info, int retry,
int wait, cpumask_t map);
Each architecture should implement this call.
The 3 variants can then be implemented in common code as small inlines:
on_each_cpu -> on_cpu( ... , cpu_online_map) on_other_cpus() -> on_cpu( ... , cpu_online_map & ~smp_processor_id()) on_single_cpu() -> on_cpu( ... , cpu_set(cpu, map))
Then each architecture needs only one function that implements smp calls. Beside the consistent naming this names are also shorter then the original.
I've built a patch for s390 & i386.
Jan
---
Jan Glauber
Linux on zSeries Development
IBM Deutschland Entwicklung GmbH
Phone: +49 7031 161911 Mail: [email protected]
arch/i386/kernel/cpu/mtrr/main.c +1 -3
@@ -223,9 +223,7 @@atomic_set (&data.gate,0 );/* Start the ball rolling on other CPUs */ -if (smp_call_function (ipi_handler, &data,1 ,0 ) !=0 ) -panic ("mtrr: timed out waiting for other CPUs\n" ); - +on_other_cpus (ipi_handler, &data,1 ,0 );local_irq_save (flags);while (atomic_read (&data.count)) {
arch/i386/kernel/cpuid.c +2 -3
@@ -55,8 +55,7 @@ {struct cpuid_command *cmd = (struct cpuid_command *) cmd_block; -if ( cmd->cpu ==smp_processor_id () ) -cpuid (cmd->reg, &cmd->data[0 ], &cmd->data[1 ], &cmd->data[2 ], &cmd->data[3 ]); +cpuid (cmd->reg, &cmd->data[0 ], &cmd->data[1 ], &cmd->data[2 ], &cmd->data[3 ]); }static inline void do_cpuid (int cpu,u32 reg,u32 *data)
@@ -71,7 +70,7 @@ cmd.reg = reg; cmd.data = data; -smp_call_function (cpuid_smp_cpuid, &cmd,1 ,1 ); +on_single_cpu (cpuid_smp_cpuid, &cmd,1 ,1 , cpu); }preempt_enable (); }
arch/i386/kernel/i386_ksyms.c +0 -1
@@ -148,7 +148,6 @@/* Global SMP stuff */ EXPORT_SYMBOL (synchronize_irq); -EXPORT_SYMBOL (smp_call_function);/* TLB flushing */ EXPORT_SYMBOL (flush_tlb_page);
arch/i386/kernel/ldt.c +1 -1
@@ -61,7 +61,7 @@load_LDT (pc); mask =cpumask_of_cpu (smp_processor_id ());if (!cpus_equal (current->mm->cpu_vm_mask, mask)) -smp_call_function (flush_ldt,0 ,1 ,1 ); +on_other_cpus (flush_ldt,0 ,1 ,1 );preempt_enable ();#else load_LDT (pc);
arch/i386/kernel/msr.c +4 -6
@@ -99,16 +99,14 @@ {struct msr_command *cmd = (struct msr_command *) cmd_block; -if ( cmd->cpu ==smp_processor_id () ) - cmd->err =wrmsr_eio (cmd->reg, cmd->data[0 ], cmd->data[1 ]); + cmd->err =wrmsr_eio (cmd->reg, cmd->data[0 ], cmd->data[1 ]); }static void msr_smp_rdmsr (void *cmd_block) {struct msr_command *cmd = (struct msr_command *) cmd_block; -if ( cmd->cpu ==smp_processor_id () ) - cmd->err =rdmsr_eio (cmd->reg, &cmd->data[0 ], &cmd->data[1 ]); + cmd->err =rdmsr_eio (cmd->reg, &cmd->data[0 ], &cmd->data[1 ]); }static inline int do_wrmsr (int cpu,u32 reg,u32 eax,u32 edx)
@@ -125,7 +123,7 @@ cmd.data[0 ] = eax; cmd.data[1 ] = edx; -smp_call_function (msr_smp_wrmsr, &cmd,1 ,1 ); +on_single_cpu (msr_smp_wrmsr, &cmd,1 ,1 , cpu); ret = cmd.err; }preempt_enable ();
@@ -144,7 +142,7 @@ cmd.cpu = cpu; cmd.reg = reg; -smp_call_function (msr_smp_rdmsr, &cmd,1 ,1 ); +on_single_cpu (msr_smp_rdmsr, &cmd,1 ,1 , cpu); *eax = cmd.data[0 ]; *edx = cmd.data[1 ];
arch/i386/kernel/reboot.c +1 -1
@@ -239,7 +239,7 @@cleared reboot_smp, and do the reboot if it is the correct CPU, otherwise it halts. */ if (reboot_cpu != cpuid) -smp_call_function ((void *)machine_restart ,NULL ,1 ,0 ); +on_other_cpus ((void *)machine_restart ,NULL ,1 ,0 ); }/* if reboot_cpu is still -1, then we want a tradional reboot,
arch/i386/kernel/smp.c +49 -36
141 lines not on this page · raw message
arch/s390/appldata/appldata_base.c +5 -8
@@ -189,7 +189,7 @@/* * appldata_mod_vtimer_wrap() * -* wrapper function for mod_virt_timer(), because smp_call_function_on() +* wrapper function for mod_virt_timer(), because on_single_cpu() * accepts only one parameter. */ static void appldata_mod_vtimer_wrap (struct appldata_mod_vtimer_args *args) {
@@ -281,9 +281,8 @@if ((buf[0 ] =='1' ) && (!appldata_timer_active)) {for (i =0 ; i <num_online_cpus (); i++) {per_cpu (appldata_timer, i).expires = per_cpu_interval; -smp_call_function_on (add_virt_timer_periodic, - &per_cpu (appldata_timer, i), -0 ,1 , i); +on_single_cpu (add_virt_timer_periodic, + &per_cpu (appldata_timer, i),0 ,1 , i); } appldata_timer_active =1 ;P_INFO ("Monitoring timer started.\n" );
@@ -346,10 +345,8 @@ &per_cpu (appldata_timer, i); appldata_mod_vtimer_args.expires = per_cpu_interval; -smp_call_function_on ( - (void *) appldata_mod_vtimer_wrap, - &appldata_mod_vtimer_args, -0 ,1 , i); +on_single_cpu ((void *) appldata_mod_vtimer_wrap, + &appldata_mod_vtimer_args,0 ,1 , i); } }spin_unlock (&appldata_timer_lock);
arch/s390/kernel/smp.c +51 -111
240 lines not on this page · raw message
drivers/s390/net/iucv.c +3 -19
@@ -670,12 +670,7 @@ulong b2f0_result =0x0deadbeef ;iucv_debug (1 ,"entering" ); -preempt_disable (); -if (smp_processor_id () ==0 ) -iucv_declare_buffer_cpu0 (&b2f0_result); -else -smp_call_function (iucv_declare_buffer_cpu0, &b2f0_result,0 ,1 ); -preempt_enable (); +on_single_cpu (iucv_declare_buffer_cpu0, &b2f0_result,0 ,1 ,0 );iucv_debug (1 ,"Address of EIB = %p" , iucv_external_int_buffer);if (b2f0_result ==0x0deadbeef ) b2f0_result =0xaa ;
@@ -694,13 +689,8 @@ {iucv_debug (1 ,"entering" );if (declare_flag) { -preempt_disable (); -if (smp_processor_id () ==0 ) -iucv_retrieve_buffer_cpu0 (0 ); -else -smp_call_function (iucv_retrieve_buffer_cpu0,0 ,0 ,1 ); +on_single_cpu (iucv_retrieve_buffer_cpu0,0 ,0 ,1 ,0 ); declare_flag =0 ; -preempt_enable (); }iucv_debug (1 ,"exiting" );return 0 ;
@@ -2220,13 +2210,7 @@ } u; u.param = SetMaskFlag; -preempt_disable (); -if (smp_processor_id () ==0 ) -iucv_setmask_cpu0 (&u); -else -smp_call_function (iucv_setmask_cpu0, &u,0 ,1 ); -preempt_enable (); - +on_single_cpu (iucv_setmask_cpu0, &u,0 ,1 ,0 );return u.result; }
include/linux/smp.h +39 -10
70 lines not on this page · raw message
mm/slab.c +3 -4
@@ -51,7 +51,7 @@* On SMP, it additionally reduces the spinlock operations. * * The c_cpuarray may not be read with enabled local interrupts - -* it's changed with a smp_call_function(). +* it's changed with a on_cpu(). * * SMP synchronization: * constructors and destructors are called without any locking.
@@ -1375,6 +1375,7 @@*/ static void smp_call_function_all_cpus (void (*func) (void *arg),void *arg) { +// XXX if order doesn't matter use on_each_cpu() check_irq_on ();preempt_disable ();
@@ -1382,9 +1383,7 @@func (arg);local_irq_enable (); -if (smp_call_function (func, arg,1 ,1 )) -BUG (); - +on_other_cpus (func, arg,1 ,1 );preempt_enable (); }
net/core/flow.c +1 -1
@@ -292,7 +292,7 @@init_completion (&info.completion);local_bh_disable (); -smp_call_function (flow_cache_flush_per_cpu, &info,1 ,0 ); +on_other_cpus (flow_cache_flush_per_cpu, &info,1 ,0 );flow_cache_flush_tasklet ((unsigned long )&info);local_bh_enable ();
Quoted 8 lines · Jan Glauber
I've been looking into the SMP call function stuff on different archs and found many different functions...
In the common code part there are 2 functions:smp_call_function()// call a function on all CPUs but my ownon_each_cpu()// call a function on all CPUs
Many archs need an additional function to call a function on a specific CPU:
arch-s390:
Quoted 8 lines
smp_call_function_on()
arch-alpha:smp_call_function_on_cpu()
arch-ia64:smp_call_function_single()
On i386 there is nosmp_call_function_single()so they have a workaround withsmp_call_function()and testing forsmp_processor_id(). Finally the slab allocator has its ownon_each_cpu()function:
Any chance we could get an smp_call_function_cpumask()?
-- wli
Hi,
Quoted 1 line
Any chance we could get an
smp_call_function_cpumask()?
Unless there is a need Id prefer not to. Some arches do not support IPI to a cpumask, whereas they should do send to all and send to single.
Anton
Quoted 1 line · At some point in the past, I wrote:
Quoted 1 line
Any chance we could get an
smp_call_function_cpumask()?
Quoted 2 lines · Anton Blanchard
Unless there is a need Id prefer not to. Some arches do not support IPI to a cpumask, whereas they should do send to all and send to single.
I remember someone making noise about a need and don't remember the context. I'll just let them take it up when the patches go out. I actually presumed it wouldn't be natively supported, but rather be a helper function for more general usage cases, esp. ones where > 1 cpu is needed, but not all, and hitting every cpu on the system is greatly more expensive than hitting only the desired cpus, which is about all I remember of the noise that was made.
-- wli
Quoted 2 lines · Anton Blanchard
Unless there is a need Id prefer not to. Some arches do not support IPI to a cpumask, whereas they should do send to all and send to single.
This is my preference too. Voyager is one such arch.
If it's added, there certainly has to be some way of ensuring that the cpumask variant isn't used preferentially to execute on a single cpu .
James
Quoted 2 lines · Anton Blanchard
Quoted 2 lines
Unless there is a need Id prefer not to. Some arches do not support IPI to a cpumask, whereas they should do send to all and send to single.
Quoted 3 lines · James Bottomley
This is my preference too. Voyager is one such arch.
If it's added, there certainly has to be some way of ensuring that the cpumask variant isn't used preferentially to execute on a single cpu .
Well, at the moment, anyone in need of IPI'ing > 1 cpu is IPI'ing all, which is not swift, so bear that in mind. Though given on_one_cpu(), I suppose they can do:
for_each_cpu_mask(cpu, foo->mask) on_one_cpu(cpu, bar, ...);
which more or less avoids IPI'ing 1024 cpus to run a function on 2 or whatever they were going on about, so they can likely code it that way.
-- wli
Quoted 9 lines · William Lee Irwin III
Well, at the moment, anyone in need of IPI'ing > 1 cpu is IPI'ing all, which is not swift, so bear that in mind. Though given
on_one_cpu(), I suppose they can do:for_each_cpu_mask(cpu, foo->mask) on_one_cpu(cpu, bar, ...);which more or less avoids IPI'ing 1024 cpus to run a function on 2 or whatever they were going on about, so they can likely code it that way.
But the key is 'anyone in need of'. What I'd like is for you to demonstrate a need of execute on cpumask before it gets added to the API. Murphy's law says that when given a choice people invariably make the wrong one, so lets not introduce choice into the api unless it's absolutely necessary.
James
Quoted 2 lines · William Lee Irwin III
Quoted 2 lines
for_each_cpu_mask(cpu, foo->mask) on_one_cpu(cpu, bar, ...);
Quoted 5 lines · James Bottomley
But the key is 'anyone in need of'. What I'd like is for you to demonstrate a need of execute on cpumask before it gets added to the API. Murphy's law says that when given a choice people invariably make the wrong one, so lets not introduce choice into the api unless it's absolutely necessary.
I remembered what cw told me. It was for ia64 repairing the suboptimal situation where flush_tlb_mm() does the wrong thing and flushes on all cpus regardless of mm->cpu_vm_mask. So we can drop this rather easily, as even when it's resolved, that will be pure arch code that will have the native IPI API's visible to it.
zwane wanted it for some 100% lockless timer code, but I think that's dead or not getting merged in the near future, so the same applies.
-- wli
A message that is not in the archive (sent off-list or not yet received): 1082641822.1329.45.camel@halo
A message that is not in the archive (sent off-list or not yet received): 1082642332.1778.39.camel@mulgrave
Quoted 4 lines · James Bottomley
Because to execute on a single cpu it's less efficient than the single cpu variant (even with the sparse mask traversal) and the
smp_call_functionsare usually in the critical path, so saving cycles is good...
With my patch you would use on_single_cpu:
static inline void on_single_cpu(void (*func) (void *info), void *info,
int nonatomic, int wait, int cpu)
{
cpumask_t map;
preempt_disable();
cpus_clear(map);
cpu_set(cpu, map);
on_cpu(func, info, nonatomic, wait, map);
preempt_enable();
}so the overhead to the single cpu variant of smp_call_function is
cpus_clear(map); cpu_set(cpu, map);
on_cpu():
cpus_and(map, map, cpu_online_map); if (cpus_empty(map)) goto out;
and the for_each loop in on_cpu
I don't know if that overhead is so much that you want to have two smp call functions.
Jan
---
Jan Glauber
Linux on zSeries Development
IBM Deutschland Entwicklung GmbH
Phone: +49 7031 161911 Mail: [email protected]
Quoted 6 lines · Anton Blanchard
Hi,
Quoted 1 line
Any chance we could get an
smp_call_function_cpumask()?Unless there is a need Id prefer not to. Some arches do not support IPI to a cpumask, whereas they should do send to all and send to single.
It's easy for those archs to implement it with a send-all and a software filtering. For the cases where it would be an optimisation to send to a cpumask, it makes sense to provide that function.
Ben.
Quoted 4 lines
It's easy for those archs to implement it with a send-all and a software filtering. For the cases where it would be an optimisation to send to a cpumask, it makes sense to provide that function.
Then someone uses it in a performance critical place and my big SMP ppc64 performance sucks.
We still havent been given a place in generic code where this optimisation makes sense.
Anton
Quoted 6 lines · Anton Blanchard
Quoted 3 lines
It's easy for those archs to implement it with a send-all and a software filtering. For the cases where it would be an optimisation to send to a cpumask, it makes sense to provide that function.
Then someone uses it in a performance critical place and my big SMP ppc64 performance sucks.
Ah ? OpenPIC can nicely IPI to CPU masks iirc, can't xics ? :)-
Quoted 2 lines
We still havent been given a place in generic code where this optimisation makes sense.
I had a few cases where I wanted it until I found different ways to do things (using RCU mostly), like the PTE freeing race. So far, I avoided it. But I regulary come up with some idea which would involve such a thing.
Ben.
Quoted 1 line · William Lee Irwin III
Any chance we could get an
smp_call_function_cpumask()?
That's what I propesed with on_cpu(). I don't care how we name this function, but I found the smp_call_function_on_whatever names a bit too long.
Jan
---
Jan Glauber
Linux on zSeries Development
IBM Deutschland Entwicklung GmbH
Phone: +49 7031 161911 Mail: [email protected]
Hi,
Quoted 9 lines
I've been looking into the SMP call function stuff on different archs and found many different functions...
In the common code part there are 2 functions:
smp_call_function()// call a function on all CPUs but my ownon_each_cpu()// call a function on all CPUsMany archs need an additional function to call a function on a specific CPU:
We noticed this too. Rusty created on_one_cpu below and had a generic (but costly) implementation so architectures could switch across when they wanted.
Anton
--
Name: on_one_cpu function
Author: Rusty Russell
Status: Booted on 2.6.5
Similar to on_each_cpu, this implements on_one_cpu. For archs which don't do it natively, it's implemented in terms of smp_call_function().
include/linux/smp.h +22 -4
@@ -8,6 +8,10 @@#include <linux/config.h> +#define get_cpu () ({ preempt_disable(); smp_processor_id(); }) +#define put_cpu () preempt_enable() +#define put_cpu_no_resched () preempt_enable_no_resched() +#ifdef CONFIG_SMP #include <linux/preempt.h>
@@ -69,6 +73,24 @@ static inline int on_each_cpu(void (*funreturn ret; } +#ifndef __HAVE_ARCH_ON_ONE_CPU +extern int __on_one_cpu (unsigned cpu,void (*func)(void *info),void *info); +static inline int on_one_cpu (unsigned cpu, +void (*func)(void *info),void *info) +{ +int ret; + +if (cpu ==get_cpu ()) { +func (info); + ret =0 ; + }else + ret =__on_one_cpu (cpu, func, info); +put_cpu (); +return ret; +} +#endif + +/* * True once the per process idle is forked */
@@ -107,8 +129,4 @@ static inline void smp_send_reschedule(i#endif /* !SMP */ -#define get_cpu () ({ preempt_disable(); smp_processor_id(); }) -#define put_cpu () preempt_enable() -#define put_cpu_no_resched () preempt_enable_no_resched() -#endif /* __LINUX_SMP_H */
kernel/sched.c +23 -1
@@ -631,7 +631,29 @@ void kick_process(task_t *p)EXPORT_SYMBOL_GPL (kick_process); -#endif +#ifndef __HAVE_ARCH_ON_ONE_CPU +struct which_cpu +{ +unsigned int cpu; +void (*func)(void *info); +void *info; +}; + +static void maybe_on_cpu (void *_which) +{ +struct which_cpu *which = _which; + +if (smp_processor_id () == which->cpu) + which->func (which->info); +} + +int __on_one_cpu (unsigned cpu,void (*func)(void *info),void *info) +{ +struct which_cpu which = { cpu, info }; +return smp_call_function (maybe_on_cpu, &which,1 ,1 ); +} +#endif /* __HAVE_ARCH_ON_ONE_CPU */ +#endif /* CONFIG_SMP */ /*** * try_to_wake_up - wake up a thread
Quoted 9 lines · Anton Blanchard
Hi,
Quoted 7 lines
I've been looking into the SMP call function stuff on different archs and found many different functions...
In the common code part there are 2 functions:
smp_call_function()// call a function on all CPUs but my ownon_each_cpu()// call a function on all CPUs
Quoted 6 lines
Quoted 2 lines
Many archs need an additional function to call a function on a specific CPU:
We noticed this too. Rusty created
on_one_cpubelow and had a generic (but costly) implementation so architectures could switch across when they wanted.
Hm, why can't you just do a:
for_each_cpu_mask(cpu, mask)
send_IPI_single(cpu)
Jan
---
Jan Glauber
Linux on zSeries Development
IBM Deutschland Entwicklung GmbH
Phone: +49 7031 161911 Mail: [email protected]
Quoted 3 lines · Anton Blanchard
Quoted 3 lines
We noticed this too. Rusty created
on_one_cpubelow and had a generic (but costly) implementation so architectures could switch across when they wanted.
Quoted 3 lines · Jan Glauber
Hm, why can't you just do a:
for_each_cpu_mask(cpu, mask)
send_IPI_single(cpu)
I think they're concerned about API minimality. I just debunked or whatever both of the feature requests I had for it on my end after doublechecking on IRC, so there isn't a known immediate need for it.
-- wli
Quoted 7 lines
there are 3 different kinds of SMP calls:
1. all CPUs
2. all CPUs but my own
3. one CPUonly _one_ basic function is needed to implement all variants:
Well, I'd go further and allow an arbitraty cpu_mask. There are cases where that could be an interesting optimisation to be able to send an IPI to a "set" of CPUs. If the HW can't do it easily, just send on all and software mask who gets the actual function call.
Ben.
FWIW, sparc64 has a smp_cross_call_masked() internal routine that sends the IPI to a cpumask_t specified set of cpus.
So whatever is decided, it can easily be implemented on that platform.