IPI: Avoid to use 2 cache lines for one call_single_data
Subsystems (10)
cover IPI: Avoid to use 2 cache lines for one call_single_data
From: Huang Ying <[email protected]>
struct call_single_data is used in IPI to transfer information between CPUs. Its size is bigger than sizeof(unsigned long) and less than cache line size. Now, it is allocated with no any alignment requirement. This makes it possible for allocated call_single_data to cross 2 cache lines. So that double the number of the cache lines that need to be transferred among CPUs. This is resolved by aligning the allocated call_single_data with cache line size.
To allocate cache line size aligned percpu memory dynamically, alloc_percpu_aligned() is introduced and used in iova drivers too.
To test the effect of the patch, we use the vm-scalability multiple thread swap test case (swap-w-seq-mt). The test will create multiple threads and each thread will eat memory until all RAM and part of swap is used, so that huge number of IPI will be triggered when unmapping memory. In the test, the throughput of memory writing improves ~5% compared with misaligned call_single_data because of faster IPI.
Best Regards,
Huang, Ying
PATCH 1/3 percpu: Add alloc_percpu_aligned()
From: Huang Ying <[email protected]>
To allocate percpu memory that is aligned with cache line size dynamically. We can statically allocate percpu memory that is aligned with cache line size with DEFINE_PER_CPU_ALIGNED(), but we have no correspondent API for dynamic allocation.
--- include/linux/percpu.h | 3 +++ 1 file changed, 3 insertions(+)
include/linux/percpu.h +3 -0
@@ -129,5 +129,8 @@ extern phys_addr_t per_cpu_ptr_to_phys(void *addr);#define alloc_percpu (type) \ (typeof(type) __percpu *)__alloc_percpu(sizeof(type), \ __alignof__(type)) +#define alloc_percpu_aligned (type) \ + ((typeof(type) __percpu *)__alloc_percpu(sizeof(type), \ + max_t(unsigned int, cache_line_size(), __alignof__(type))))#endif /* __LINUX_PERCPU_H */
Signature · 1 line
2.13.2
Re: [PATCH 1/3] percpu: Add alloc_percpu_aligned()
include/linux/percpu.h extern phys_addr_t per_cpu_ptr_to_phys(void *addr); · line 136
Quoted 11 lines · Ying
include/linux/percpu.h +3
-0@@ -129,5 +129,8 @@ extern phys_addr_t per_cpu_ptr_to_phys(void *addr);#define alloc_percpu (type) \ (typeof(type) __percpu *)__alloc_percpu(sizeof(type), \ __alignof__(type)) +#define alloc_percpu_aligned (type) \ + ((typeof(type) __percpu *)__alloc_percpu(sizeof(type), \ + max_t(unsigned int, cache_line_size(), __alignof__(type))))#endif /* __LINUX_PERCPU_H */
This is not needeed since alloc_percpu() already uses __alignof__(type).
If you add an attribute to the definition of "type" that requires cacheline alignmet (f.e. __cacheline_aligned) then alloc_percpu() will align the allocation as you desire.
Re: [PATCH 1/3] percpu: Add alloc_percpu_aligned()
Quoted 12 lines · Christopher Lameter
Quoted 10 lines · Ying
include/linux/percpu.h +3
-0@@ -129,5 +129,8 @@ extern phys_addr_t per_cpu_ptr_to_phys(void *addr);#define alloc_percpu (type) \ (typeof(type) __percpu *)__alloc_percpu(sizeof(type), \ __alignof__(type)) +#define alloc_percpu_aligned (type) \ + ((typeof(type) __percpu *)__alloc_percpu(sizeof(type), \ + max_t(unsigned int, cache_line_size(), __alignof__(type))))
Quoted 7 lines
include/linux/percpu.h extern phys_addr_t per_cpu_ptr_to_phys(void *addr); · line 136
Quoted 1 line
include/linux/percpu.h +0
-0#endif /* __LINUX_PERCPU_H */ This is not needeed since
alloc_percpu()already uses__alignof__(type).If you add an attribute to the definition of "type" that requires cacheline alignmet (f.e.
__cacheline_aligned) thenalloc_percpu()will align the allocation as you desire.
OK.
Best Regards,
Huang, Ying
PATCH 2/3 iova: Use alloc_percpu_aligned()
In reply to Huang, YingFrom: Huang Ying <[email protected]>
To use the newly introduced alloc_percpu_aligned(), which can allocate cache line size aligned percpu memory dynamically.
--- drivers/iommu/iova.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iommu/iova.c +1 -1
@@ -698,7 +698,7 @@ static void init_iova_rcaches(struct iova_domain *iovad) rcache = &iovad->rcaches[i];spin_lock_init (&rcache->lock); rcache->depot_size =0 ; - rcache->cpu_rcaches =__alloc_percpu (sizeof (*cpu_rcache),cache_line_size ()); + rcache->cpu_rcaches =alloc_percpu_aligned (*cpu_rcache);if (WARN_ON (!rcache->cpu_rcaches))continue ;for_each_possible_cpu (cpu) {
Signature · 1 line
2.13.2
PATCH 3/3 IPI: Avoid to use 2 cache lines for one call_single_data
In reply to Huang, YingFrom: Huang Ying <[email protected]>
struct call_single_data is used in IPI to transfer information between CPUs. Its size is bigger than sizeof(unsigned long) and less than cache line size. Now, it is allocated with no any alignment requirement. This makes it possible for allocated call_single_data to cross 2 cache lines. So that double the number of the cache lines that need to be transferred among CPUs. This is resolved by aligning the allocated call_single_data with cache line size.
To test the effect of the patch, we use the vm-scalability multiple thread swap test case (swap-w-seq-mt). The test will create multiple threads and each thread will eat memory until all RAM and part of swap is used, so that huge number of IPI will be triggered when unmapping memory. In the test, the throughput of memory writing improves ~5% compared with misaligned call_single_data because of faster IPI.
--- kernel/smp.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
kernel/smp.c +4 -2
@@ -51,7 +51,7 @@ int smpcfd_prepare_cpu(unsigned int cpu)free_cpumask_var (cfd->cpumask);return -ENOMEM ; } - cfd->csd = alloc_percpu(struct call_single_data); + cfd->csd = alloc_percpu_aligned(struct call_single_data);if (!cfd->csd) {free_cpumask_var (cfd->cpumask);free_cpumask_var (cfd->cpumask_ipi);
@@ -269,7 +269,9 @@ int smp_call_function_single(int cpu, smp_call_func_t func, void *info, int wait) {struct call_single_data *csd; -struct call_single_data csd_stack = { .flags =CSD_FLAG_LOCK |CSD_FLAG_SYNCHRONOUS }; +struct call_single_data csd_stack____cacheline_aligned = { + .flags =CSD_FLAG_LOCK |CSD_FLAG_SYNCHRONOUS + };int this_cpu;int err;
Signature · 1 line
2.13.2
Quoted 34 lines · Ying
From: Huang Ying <[email protected]>
struct
call_single_datais used in IPI to transfer information between CPUs. Its size is bigger than sizeof(unsigned long) and less than cache line size. Now, it is allocated with no any alignment requirement. This makes it possible for allocatedcall_single_datato cross 2 cache lines. So that double the number of the cache lines that need to be transferred among CPUs. This is resolved by aligning the allocatedcall_single_datawith cache line size.To test the effect of the patch, we use the vm-scalability multiple thread swap test case (swap-w-seq-mt). The test will create multiple threads and each thread will eat memory until all RAM and part of swap is used, so that huge number of IPI will be triggered when unmapping memory. In the test, the throughput of memory writing improves ~5% compared with misaligned
call_single_databecause of faster IPI.Signed-off-by "Huang, Ying" <[email protected]>Cc Peter Zijlstra <[email protected]>Cc Ingo Molnar <[email protected]>Cc Michael Ellerman <[email protected]>Cc Borislav Petkov <[email protected]>Cc Thomas Gleixner <[email protected]>Cc Juergen Gross <[email protected]>Cc Aaron Lu <[email protected]>--- kernel/smp.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)kernel/smp.c +0
-0
kernel/smp.c int smpcfd_prepare_cpu(unsigned int cpu) · line 54
Quoted 5 lines
kernel/smp.c +1
-1@@ -51,7 +51,7 @@ int smpcfd_prepare_cpu(unsigned int cpu)free_cpumask_var (cfd->cpumask);return -ENOMEM ; } - cfd->csd = alloc_percpu(struct call_single_data); + cfd->csd = alloc_percpu_aligned(struct call_single_data);
I do not believe allocating 64 bytes (per cpu) for this structure is needed. That would be an increase of cache lines.
What we can do instead is to force an alignment on 4*sizeof(void *). (32 bytes on 64bit, 16 bytes on 32bit arches)
Maybe something like this :
include/linux/smp.h +1 -1
@@ -19,7 +19,7 @@ struct call_single_data {smp_call_func_t func;void *info;unsigned int flags; -}; +} __attribute__((aligned(4 * sizeof(void *))));/* total number of cpus in this system (may exceed NR_CPUS) */ extern unsigned int total_cpus;
Peter Zijlstra
maintainerQuoted 10 lines · Eric Dumazet
What we can do instead is to force an alignment on 4*sizeof(void *). (32 bytes on 64bit, 16 bytes on 32bit arches)
Maybe something like this :
include/linux/smp.h +0
-0
include/linux/smp.h struct call_single_data { · line 22
Quoted 5 lines
include/linux/smp.h +1
-1@@ -19,7 +19,7 @@ struct call_single_data {smp_call_func_t func;void *info;unsigned int flags; -}; +} __attribute__((aligned(4 * sizeof(void *))));
Agreed.
Quoted 54 lines · Eric Dumazet
Quoted 34 lines · Ying
From: Huang Ying <[email protected]>
struct
call_single_datais used in IPI to transfer information between CPUs. Its size is bigger than sizeof(unsigned long) and less than cache line size. Now, it is allocated with no any alignment requirement. This makes it possible for allocatedcall_single_datato cross 2 cache lines. So that double the number of the cache lines that need to be transferred among CPUs. This is resolved by aligning the allocatedcall_single_datawith cache line size.To test the effect of the patch, we use the vm-scalability multiple thread swap test case (swap-w-seq-mt). The test will create multiple threads and each thread will eat memory until all RAM and part of swap is used, so that huge number of IPI will be triggered when unmapping memory. In the test, the throughput of memory writing improves ~5% compared with misaligned
call_single_databecause of faster IPI.Signed-off-by "Huang, Ying" <[email protected]>Cc Peter Zijlstra <[email protected]>Cc Ingo Molnar <[email protected]>Cc Michael Ellerman <[email protected]>Cc Borislav Petkov <[email protected]>Cc Thomas Gleixner <[email protected]>Cc Juergen Gross <[email protected]>Cc Aaron Lu <[email protected]>--- kernel/smp.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)kernel/smp.c +0
-0kernel/smp.c int smpcfd_prepare_cpu(unsigned int cpu) · line 54
Quoted 5 lines
kernel/smp.c +1
-1@@ -51,7 +51,7 @@ int smpcfd_prepare_cpu(unsigned int cpu)free_cpumask_var (cfd->cpumask);return -ENOMEM ; } - cfd->csd = alloc_percpu(struct call_single_data); + cfd->csd = alloc_percpu_aligned(struct call_single_data);I do not believe allocating 64 bytes (per cpu) for this structure is needed. That would be an increase of cache lines.
What we can do instead is to force an alignment on 4*sizeof(void *). (32 bytes on 64bit, 16 bytes on 32bit arches)
Maybe something like this :
include/linux/smp.h +0
-0
include/linux/smp.h struct call_single_data { · line 25
Quoted 8 lines
include/linux/smp.h +1
-1@@ -19,7 +19,7 @@ struct call_single_data {smp_call_func_t func;void *info;unsigned int flags; -}; +} __attribute__((aligned(4 * sizeof(void *))));/* total number of cpus in this system (may exceed NR_CPUS) */ extern unsigned int total_cpus;
OK. And if the sizeof(struct call_single_data) changes, we need to change the alignment accordingly too. So I add some BUILD_BUG_ON() for that.
Best Regards,
Huang, Ying
------------------>8------------------
Quoted 1 line
From
2c400e9b1793f1c1d33bc278f5bc066e32ca4feeMon Sep 17 00:00:00 2001
From: Huang Ying <[email protected]>
Date: Thu, 27 Jul 2017 16:43:20 +0800
Subject: [PATCH -v2] IPI: Avoid to use 2 cache lines for one call_single_data
struct call_single_data is used in IPI to transfer information between CPUs. Its size is bigger than sizeof(unsigned long) and less than cache line size. Now, it is allocated with no any alignment requirement. This makes it possible for allocated call_single_data to cross 2 cache lines. So that double the number of the cache lines that need to be transferred among CPUs.
This is resolved by aligning the allocated call_single_data with 4 * sizeof(void *). If the size of struct call_single_data is changed in the future, the alignment should be changed accordingly. It should be more than sizeof(struct call_single_data) and the power of 2.
To test the effect of the patch, we use the vm-scalability multiple thread swap test case (swap-w-seq-mt). The test will create multiple threads and each thread will eat memory until all RAM and part of swap is used, so that huge number of IPI will be triggered when unmapping memory. In the test, the throughput of memory writing improves ~5% compared with misaligned call_single_data because of faster IPI.
--- include/linux/smp.h | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)
include/linux/smp.h +10 -1
@@ -13,13 +13,22 @@#include <linux/init.h> #include <linux/llist.h> +#define CSD_ALIGNMENT (4 * sizeof(void *)) +typedef void (*smp_call_func_t )(void *info);struct call_single_data {struct llist_node llist;smp_call_func_t func;void *info;unsigned int flags; -}; +}__aligned(CSD_ALIGNMENT) ; + +/* To avoid allocate csd across 2 cache lines */ +static inline void check_alignment_of_csd (void ) +{ +BUILD_BUG_ON ((CSD_ALIGNMENT & (CSD_ALIGNMENT -1 )) !=0 ); +BUILD_BUG_ON (sizeof (struct call_single_data ) >CSD_ALIGNMENT ); +}/* total number of cpus in this system (may exceed NR_CPUS) */ extern unsigned int total_cpus;
Signature · 1 line
2.13.2
Peter Zijlstra
maintainerQuoted 18 lines · Ying
include/linux/smp.h +3
-1@@ -13,13 +13,22 @@#include <linux/init.h> #include <linux/llist.h> +#define CSD_ALIGNMENT (4 * sizeof(void *)) +typedef void (*smp_call_func_t )(void *info);struct call_single_data {struct llist_node llist;smp_call_func_t func;void *info;unsigned int flags; -}; +}__aligned(CSD_ALIGNMENT) ;
include/linux/smp.h line 34
Quoted 10 lines
include/linux/smp.h +7
-0+ +/* To avoid allocate csd across 2 cache lines */ +static inline void check_alignment_of_csd (void ) +{ +BUILD_BUG_ON ((CSD_ALIGNMENT & (CSD_ALIGNMENT -1 )) !=0 ); +BUILD_BUG_ON (sizeof (struct call_single_data ) >CSD_ALIGNMENT ); +}/* total number of cpus in this system (may exceed NR_CPUS) */ extern unsigned int total_cpus;
Bah, C sucks.. a much larger but possibly nicer patch
---
arch/mips/kernel/smp.c +3 -3
@@ -648,12 +648,12 @@ EXPORT_SYMBOL(flush_tlb_one);#ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST static DEFINE_PER_CPU(atomic_t, tick_broadcast_count); -static DEFINE_PER_CPU(struct call_single_data, tick_broadcast_csd); +static DEFINE_PER_CPU(call_single_data_t, tick_broadcast_csd);void tick_broadcast (const struct cpumask *mask) {atomic_t *count; -struct call_single_data *csd; +call_single_data_t *csd;int cpu;for_each_cpu (cpu, mask) {
@@ -674,7 +674,7 @@ static void tick_broadcast_callee(void *info)static int __init tick_broadcast_init (void ) { -struct call_single_data *csd; +call_single_data_t *csd;int cpu;for (cpu =0 ; cpu <NR_CPUS ; cpu++) {
block/blk-softirq.c +1 -1
@@ -60,7 +60,7 @@ static void trigger_softirq(void *data)static int raise_blk_irq (int cpu,struct request *rq) {if (cpu_online (cpu)) { -struct call_single_data *data = &rq->csd; +call_single_data_t *data = &rq->csd; data->func = trigger_softirq; data->info = rq;
drivers/block/null_blk.c +1 -1
@@ -13,7 +13,7 @@struct nullb_cmd {struct list_head list;struct llist_node ll_list; -struct call_single_data csd; +call_single_data_t csd;struct request *rq;struct bio *bio;unsigned int tag;
drivers/cpuidle/coupled.c +3 -3
@@ -119,7 +119,7 @@ struct cpuidle_coupled {#define CPUIDLE_COUPLED_NOT_IDLE (-1) -static DEFINE_PER_CPU(struct call_single_data, cpuidle_coupled_poke_cb); +static DEFINE_PER_CPU(call_single_data_t, cpuidle_coupled_poke_cb);/* * The cpuidle_coupled_poke_pending mask is used to avoid calling
@@ -339,7 +339,7 @@ static void cpuidle_coupled_handle_poke(void *info)*/ static void cpuidle_coupled_poke (int cpu) { -struct call_single_data *csd = &per_cpu (cpuidle_coupled_poke_cb, cpu); +call_single_data_t *csd = &per_cpu (cpuidle_coupled_poke_cb, cpu);if (!cpumask_test_and_set_cpu (cpu, &cpuidle_coupled_poke_pending))smp_call_function_single_async (cpu, csd);
@@ -651,7 +651,7 @@ int cpuidle_coupled_register_device(struct cpuidle_device *dev) {int cpu;struct cpuidle_device *other_dev; -struct call_single_data *csd; +call_single_data_t *csd;struct cpuidle_coupled *coupled;if (cpumask_empty (&dev->coupled_cpus))
drivers/net/ethernet/cavium/liquidio/lio_main.c +1 -1
@@ -2468,7 +2468,7 @@ static void liquidio_napi_drv_callback(void *arg)if (OCTEON_CN23XX_PF (oct) || droq->cpu_id == this_cpu) {napi_schedule_irqoff (&droq->napi); }else { -struct call_single_data *csd = &droq->csd; +call_single_data_t *csd = &droq->csd; csd->func = napi_schedule_wrapper; csd->info = &droq->napi;
drivers/net/ethernet/cavium/liquidio/octeon_droq.h +1 -1
@@ -328,7 +328,7 @@ struct octeon_droq {u32 cpu_id; -struct call_single_data csd; +call_single_data_t csd; };#define OCT_DROQ_SIZE (sizeof(struct octeon_droq))
include/linux/blkdev.h +1 -1
@@ -134,7 +134,7 @@ typedef __u32 __bitwise req_flags_t;struct request {struct list_head queuelist;union { -struct call_single_data csd; +call_single_data_t csd;u64 fifo_time; };
include/linux/netdevice.h +1 -1
@@ -2774,7 +2774,7 @@ struct softnet_data {unsigned int input_queue_head____cacheline_aligned_in_smp ;/* Elements below can be accessed between CPUs for RPS/RFS */ -struct call_single_data csd____cacheline_aligned_in_smp ; +call_single_data_t csd____cacheline_aligned_in_smp ;struct softnet_data *rps_ipi_next;unsigned int cpu;unsigned int input_queue_tail;
include/linux/smp.h +5 -2
@@ -14,13 +14,16 @@#include <linux/llist.h> typedef void (*smp_call_func_t )(void *info); -struct call_single_data { +struct __call_single_data {struct llist_node llist;smp_call_func_t func;void *info;unsigned int flags; }; +typedef struct __call_single_data call_single_data_t +__aligned(sizeof(struct __call_single_data)) ; +/* total number of cpus in this system (may exceed NR_CPUS) */ extern unsigned int total_cpus;
@@ -48,7 +51,7 @@ void on_each_cpu_cond(bool (*cond_func)(int cpu, void *info), smp_call_func_t func, void *info, bool wait, gfp_t gfp_flags); -int smp_call_function_single_async (int cpu,struct call_single_data *csd); +int smp_call_function_single_async (int cpu,call_single_data_t *csd);#ifdef CONFIG_SMP
kernel/sched/sched.h +1 -1
@@ -769,7 +769,7 @@ struct rq {#ifdef CONFIG_SCHED_HRTICK #ifdef CONFIG_SMP int hrtick_csd_pending; -struct call_single_data hrtick_csd; +call_single_data_t hrtick_csd;#endif struct hrtimer hrtick_timer;#endif
kernel/smp.c +13 -13
@@ -28,7 +28,7 @@ enum { };struct call_function_data { -struct call_single_data __percpu *csd; +call_single_data_t __percpu *csd;cpumask_var_t cpumask;cpumask_var_t cpumask_ipi; };
@@ -51,7 +51,7 @@ int smpcfd_prepare_cpu(unsigned int cpu)free_cpumask_var (cfd->cpumask);return -ENOMEM ; } - cfd->csd = alloc_percpu(struct call_single_data); + cfd->csd =alloc_percpu (call_single_data_t);if (!cfd->csd) {free_cpumask_var (cfd->cpumask);free_cpumask_var (cfd->cpumask_ipi);
@@ -103,12 +103,12 @@ void __init call_function_init(void)* previous function call. For multi-cpu calls its even more interesting * as we'll have to ensure no other cpu is observing our csd. */ -static __always_inline void csd_lock_wait(struct call_single_data *csd) +static __always_inline void csd_lock_wait(call_single_data_t *csd) {smp_cond_load_acquire (&csd->flags, !(VAL &CSD_FLAG_LOCK )); } -static __always_inline void csd_lock(struct call_single_data *csd) +static __always_inline void csd_lock(call_single_data_t *csd) {csd_lock_wait (csd); csd->flags |=CSD_FLAG_LOCK ;
@@ -121,7 +121,7 @@ static __always_inline void csd_lock(struct call_single_data *csd)smp_wmb (); } -static __always_inline void csd_unlock(struct call_single_data *csd) +static __always_inline void csd_unlock(call_single_data_t *csd) {WARN_ON (!(csd->flags &CSD_FLAG_LOCK ));
@@ -131,14 +131,14 @@ static __always_inline void csd_unlock(struct call_single_data *csd)smp_store_release (&csd->flags,0 ); } -static DEFINE_PER_CPU_SHARED_ALIGNED(struct call_single_data, csd_data); +static DEFINE_PER_CPU_SHARED_ALIGNED(call_single_data_t, csd_data);/* * Insert a previously allocated call_single_data element * for execution on the given CPU. data must already have * ->func, ->info, and ->flags set. */ -static int generic_exec_single (int cpu,struct call_single_data *csd, +static int generic_exec_single (int cpu,call_single_data_t *csd,smp_call_func_t func,void *info) {if (cpu ==smp_processor_id ()) {
@@ -210,7 +210,7 @@ static void flush_smp_call_function_queue(bool warn_cpu_offline) {struct llist_head *head;struct llist_node *entry; -struct call_single_data *csd, *csd_next; +call_single_data_t *csd, *csd_next;static bool warned;WARN_ON (!irqs_disabled ());
@@ -268,8 +268,8 @@ static void flush_smp_call_function_queue(bool warn_cpu_offline)int smp_call_function_single (int cpu,smp_call_func_t func,void *info,int wait) { -struct call_single_data *csd; -struct call_single_data csd_stack = { .flags =CSD_FLAG_LOCK |CSD_FLAG_SYNCHRONOUS }; +call_single_data_t *csd; +call_single_data_t csd_stack = { .flags =CSD_FLAG_LOCK |CSD_FLAG_SYNCHRONOUS };int this_cpu;int err;
@@ -321,7 +321,7 @@ EXPORT_SYMBOL(smp_call_function_single);* NOTE: Be careful, there is unfortunately no current debugging facility to * validate the correctness of this serialization. */ -int smp_call_function_single_async (int cpu,struct call_single_data *csd) +int smp_call_function_single_async (int cpu,call_single_data_t *csd) {int err =0 ;
@@ -444,7 +444,7 @@ void smp_call_function_many(const struct cpumask *mask,cpumask_clear (cfd->cpumask_ipi);for_each_cpu (cpu, cfd->cpumask) { -struct call_single_data *csd =per_cpu_ptr (cfd->csd, cpu); +call_single_data_t *csd =per_cpu_ptr (cfd->csd, cpu);csd_lock (csd);if (wait)
@@ -460,7 +460,7 @@ void smp_call_function_many(const struct cpumask *mask,if (wait) {for_each_cpu (cpu, cfd->cpumask) { -struct call_single_data *csd; +call_single_data_t *csd; csd =per_cpu_ptr (cfd->csd, cpu);csd_lock_wait (csd);
kernel/up.c +1 -1
@@ -23,7 +23,7 @@ int smp_call_function_single(int cpu, void (*func) (void *info), void *info, }EXPORT_SYMBOL (smp_call_function_single); -int smp_call_function_single_async (int cpu,struct call_single_data *csd) +int smp_call_function_single_async (int cpu,call_single_data_t *csd) {unsigned long flags;
Peter Zijlstra <[email protected]> writes:
[snip]
Quoted 9 lines
include/linux/smp.h +0
-1@@ -14,13 +14,16 @@#include <linux/llist.h> typedef void (*smp_call_func_t )(void *info); -struct call_single_data {
include/linux/smp.h line 26
Quoted 10 lines
include/linux/smp.h +4
-0+struct __call_single_data {struct llist_node llist;smp_call_func_t func;void *info;unsigned int flags; }; +typedef struct __call_single_data call_single_data_t +__aligned(sizeof(struct __call_single_data)) ; +
Another requirement of the alignment is that it should be the power of
2. Otherwise, for example, if someone adds a field to struct, so that the size becomes 40 on x86_64. The alignment should be 64 instead of 40.
Best Regards,
Huang, Ying
Quoted 3 lines
include/linux/smp.h +0
-0/* total number of cpus in this system (may exceed NR_CPUS) */ extern unsigned int total_cpus;
[snip]
Quoted 18 lines · Ying
Peter Zijlstra <[email protected]> writes:
[snip]Quoted 16 lines
include/linux/smp.h +1
-1@@ -14,13 +14,16 @@#include <linux/llist.h> typedef void (*smp_call_func_t )(void *info); -struct call_single_data { +struct __call_single_data {struct llist_node llist;smp_call_func_t func;void *info;unsigned int flags; };
Quoted 8 lines
include/linux/smp.h line 26
Quoted 3 lines
include/linux/smp.h +3
-0+typedef struct __call_single_data call_single_data_t +__aligned(sizeof(struct __call_single_data)) ; +Another requirement of the alignment is that it should be the power of
2. Otherwise, for example, if someone adds a field to struct, so that the size becomes 40 onx86_64. The alignment should be 64 instead of 40.
Thanks Aaron, he reminded me that there is a roundup_pow_of_two(). So the typedef could be,
typedef struct __call_single_data call_single_data_t
__aligned(roundup_pow_of_two(sizeof(struct __call_single_data));
Best Regards,
Huang, Ying
Quoted 7 lines
Best Regards,
Huang, YingQuoted 3 lines
include/linux/smp.h +0
-0/* total number of cpus in this system (may exceed NR_CPUS) */ extern unsigned int total_cpus;[snip]
Peter Zijlstra
maintainerQuoted 19 lines · Ying
Quoted 9 lines · Ying
Quoted 7 lines · Peter Zijlstra
include/linux/smp.h +1
-0+struct __call_single_data {struct llist_node llist;smp_call_func_t func;void *info;unsigned int flags; };Quoted 8 lines
include/linux/smp.h line 26
Quoted 3 lines
include/linux/smp.h +3
-0+typedef struct __call_single_data call_single_data_t +__aligned(sizeof(struct __call_single_data)) ; +Another requirement of the alignment is that it should be the power of
2. Otherwise, for example, if someone adds a field to struct, so that the size becomes 40 onx86_64. The alignment should be 64 instead of 40.
Quoted 6 lines
Thanks Aaron, he reminded me that there is a
roundup_pow_of_two(). So the typedef could be,typedef struct
__call_single_datacall_single_data_t__aligned(roundup_pow_of_two(sizeof(struct __call_single_data));
Yes, that would take away the requirement to play padding games with the struct. Then again, maybe its a good thing to have to be explicit about it.
If you see:
struct __call_single_data {
struct llist_node llist;
smp_call_func_t func;
void *info
int flags;
void *extra_field;
unsigned long __padding[3]; /* make align work */
};that makes it very clear what is going on. In any case, we can delay this part because the current structure is a power-of-2 for both ILP32 and LP64. So only the person growing this will have to deal with it ;-)
Quoted 43 lines · Peter Zijlstra
Quoted 19 lines · Ying
Quoted 9 lines · Ying
Quoted 7 lines · Peter Zijlstra
include/linux/smp.h +1
-0+struct __call_single_data {struct llist_node llist;smp_call_func_t func;void *info;unsigned int flags; };Quoted 8 lines
include/linux/smp.h line 26
Quoted 3 lines
include/linux/smp.h +3
-0+typedef struct __call_single_data call_single_data_t +__aligned(sizeof(struct __call_single_data)) ; +Another requirement of the alignment is that it should be the power of
2. Otherwise, for example, if someone adds a field to struct, so that the size becomes 40 onx86_64. The alignment should be 64 instead of 40.Quoted 6 lines
Thanks Aaron, he reminded me that there is a
roundup_pow_of_two(). So the typedef could be,typedef struct
__call_single_datacall_single_data_t__aligned(roundup_pow_of_two(sizeof(struct __call_single_data));Yes, that would take away the requirement to play padding games with the struct. Then again, maybe its a good thing to have to be explicit about it.
If you see:
struct __call_single_data { struct llist_node llist; smp_call_func_t func; void *info int flags; void *extra_field; unsigned long __padding[3]; /* make align work */ };
Quoted 3 lines
that makes it very clear what is going on. In any case, we can delay this part because the current structure is a power-of-2 for both ILP32 and LP64. So only the person growing this will have to deal with it ;-)
Yes. That looks good. So you will prepare the final patch? Or you hope me to do that?
Best Regards,
Huang, Ying
Peter Zijlstra
maintainerQuoted 2 lines · Ying
Yes. That looks good. So you will prepare the final patch? Or you hope me to do that?
I was hoping you'd do it ;-)
Quoted 5 lines · Peter Zijlstra
Quoted 2 lines · Ying
Yes. That looks good. So you will prepare the final patch? Or you hope me to do that?
I was hoping you'd do it ;-)
Thanks! Here is the updated patch
Best Regards,
Huang, Ying
---------->8----------
Quoted 1 line
From
957735e9ff3922368286540dab852986fc7b23b5Mon Sep 17 00:00:00 2001
From: Huang Ying <[email protected]>
Date: Mon, 7 Aug 2017 16:55:33 +0800
Subject: [PATCH -v3] IPI: Avoid to use 2 cache lines for one
call_single_data
struct call_single_data is used in IPI to transfer information between CPUs. Its size is bigger than sizeof(unsigned long) and less than cache line size. Now, it is allocated with no explicit alignment requirement. This makes it possible for allocated call_single_data to cross 2 cache lines. So that double the number of the cache lines that need to be transferred among CPUs.
This is resolved by requiring call_single_data to be aligned with the size of call_single_data. Now the size of call_single_data is the power of 2. If we add new fields to call_single_data, we may need to add pads to make sure the size of new definition is the power of 2. Fortunately, this is enforced by gcc, which will report error for not power of 2 alignment requirement.
To set alignment requirement of call_single_data to the size of call_single_data, a struct definition and a typedef is used.
To test the effect of the patch, we use the vm-scalability multiple thread swap test case (swap-w-seq-mt). The test will create multiple threads and each thread will eat memory until all RAM and part of swap is used, so that huge number of IPI will be triggered when unmapping memory. In the test, the throughput of memory writing improves ~5% compared with misaligned call_single_data because of faster IPI.
--- arch/mips/kernel/smp.c | 6 ++-- block/blk-softirq.c | 2 +- drivers/block/null_blk.c | 2 +- drivers/cpuidle/coupled.c | 10 +++---- drivers/net/ethernet/cavium/liquidio/lio_main.c | 2 +- drivers/net/ethernet/cavium/liquidio/octeon_droq.h | 2 +- include/linux/blkdev.h | 2 +- include/linux/netdevice.h | 2 +- include/linux/smp.h | 8 ++++-- kernel/sched/sched.h | 2 +- kernel/smp.c | 32 ++++++++++++---------- kernel/up.c | 2 +- 12 files changed, 39 insertions(+), 33 deletions(-)
arch/mips/kernel/smp.c +3 -3
@@ -648,12 +648,12 @@ EXPORT_SYMBOL(flush_tlb_one);#ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST static DEFINE_PER_CPU(atomic_t, tick_broadcast_count); -static DEFINE_PER_CPU(struct call_single_data, tick_broadcast_csd); +static DEFINE_PER_CPU(call_single_data_t, tick_broadcast_csd);void tick_broadcast (const struct cpumask *mask) {atomic_t *count; -struct call_single_data *csd; +call_single_data_t *csd;int cpu;for_each_cpu (cpu, mask) {
@@ -674,7 +674,7 @@ static void tick_broadcast_callee(void *info)static int __init tick_broadcast_init (void ) { -struct call_single_data *csd; +call_single_data_t *csd;int cpu;for (cpu =0 ; cpu <NR_CPUS ; cpu++) {
block/blk-softirq.c +1 -1
@@ -60,7 +60,7 @@ static void trigger_softirq(void *data)static int raise_blk_irq (int cpu,struct request *rq) {if (cpu_online (cpu)) { -struct call_single_data *data = &rq->csd; +call_single_data_t *data = &rq->csd; data->func = trigger_softirq; data->info = rq;
drivers/block/null_blk.c +1 -1
@@ -13,7 +13,7 @@struct nullb_cmd {struct list_head list;struct llist_node ll_list; -struct call_single_data csd; +call_single_data_t csd;struct request *rq;struct bio *bio;unsigned int tag;
drivers/cpuidle/coupled.c +5 -5
@@ -119,13 +119,13 @@ struct cpuidle_coupled {#define CPUIDLE_COUPLED_NOT_IDLE (-1) -static DEFINE_PER_CPU(struct call_single_data, cpuidle_coupled_poke_cb); +static DEFINE_PER_CPU(call_single_data_t, cpuidle_coupled_poke_cb);/* * The cpuidle_coupled_poke_pending mask is used to avoid calling -* __smp_call_function_single with the per cpu call_single_data struct already +* __smp_call_function_single with the per cpu call_single_data_t struct already * in use. This prevents a deadlock where two cpus are waiting for each others -* call_single_data struct to be available +* call_single_data_t struct to be available */ static cpumask_t cpuidle_coupled_poke_pending;
@@ -339,7 +339,7 @@ static void cpuidle_coupled_handle_poke(void *info)*/ static void cpuidle_coupled_poke (int cpu) { -struct call_single_data *csd = &per_cpu (cpuidle_coupled_poke_cb, cpu); +call_single_data_t *csd = &per_cpu (cpuidle_coupled_poke_cb, cpu);if (!cpumask_test_and_set_cpu (cpu, &cpuidle_coupled_poke_pending))smp_call_function_single_async (cpu, csd);
@@ -651,7 +651,7 @@ int cpuidle_coupled_register_device(struct cpuidle_device *dev) {int cpu;struct cpuidle_device *other_dev; -struct call_single_data *csd; +call_single_data_t *csd;struct cpuidle_coupled *coupled;if (cpumask_empty (&dev->coupled_cpus))
drivers/net/ethernet/cavium/liquidio/lio_main.c +1 -1
@@ -2468,7 +2468,7 @@ static void liquidio_napi_drv_callback(void *arg)if (OCTEON_CN23XX_PF (oct) || droq->cpu_id == this_cpu) {napi_schedule_irqoff (&droq->napi); }else { -struct call_single_data *csd = &droq->csd; +call_single_data_t *csd = &droq->csd; csd->func = napi_schedule_wrapper; csd->info = &droq->napi;
drivers/net/ethernet/cavium/liquidio/octeon_droq.h +1 -1
@@ -328,7 +328,7 @@ struct octeon_droq {u32 cpu_id; -struct call_single_data csd; +call_single_data_t csd; };#define OCT_DROQ_SIZE (sizeof(struct octeon_droq))
include/linux/blkdev.h +1 -1
@@ -134,7 +134,7 @@ typedef __u32 __bitwise req_flags_t;struct request {struct list_head queuelist;union { -struct call_single_data csd; +call_single_data_t csd;u64 fifo_time; };
include/linux/netdevice.h +1 -1
@@ -2774,7 +2774,7 @@ struct softnet_data {unsigned int input_queue_head____cacheline_aligned_in_smp ;/* Elements below can be accessed between CPUs for RPS/RFS */ -struct call_single_data csd____cacheline_aligned_in_smp ; +call_single_data_t csd____cacheline_aligned_in_smp ;struct softnet_data *rps_ipi_next;unsigned int cpu;unsigned int input_queue_tail;
include/linux/smp.h +6 -2
@@ -14,13 +14,17 @@#include <linux/llist.h> typedef void (*smp_call_func_t )(void *info); -struct call_single_data { +struct __call_single_data {struct llist_node llist;smp_call_func_t func;void *info;unsigned int flags; }; +/* Use __aligned() to avoid to use 2 cache lines for 1 csd */ +typedef struct __call_single_data call_single_data_t +__aligned(sizeof(struct __call_single_data)) ; +/* total number of cpus in this system (may exceed NR_CPUS) */ extern unsigned int total_cpus;
@@ -48,7 +52,7 @@ void on_each_cpu_cond(bool (*cond_func)(int cpu, void *info), smp_call_func_t func, void *info, bool wait, gfp_t gfp_flags); -int smp_call_function_single_async (int cpu,struct call_single_data *csd); +int smp_call_function_single_async (int cpu,call_single_data_t *csd);#ifdef CONFIG_SMP
kernel/sched/sched.h +1 -1
@@ -769,7 +769,7 @@ struct rq {#ifdef CONFIG_SCHED_HRTICK #ifdef CONFIG_SMP int hrtick_csd_pending; -struct call_single_data hrtick_csd; +call_single_data_t hrtick_csd;#endif struct hrtimer hrtick_timer;#endif
kernel/smp.c +17 -15
@@ -28,7 +28,7 @@ enum { };struct call_function_data { -struct call_single_data __percpu *csd; +call_single_data_t __percpu *csd;cpumask_var_t cpumask;cpumask_var_t cpumask_ipi; };
@@ -51,7 +51,7 @@ int smpcfd_prepare_cpu(unsigned int cpu)free_cpumask_var (cfd->cpumask);return -ENOMEM ; } - cfd->csd = alloc_percpu(struct call_single_data); + cfd->csd =alloc_percpu (call_single_data_t);if (!cfd->csd) {free_cpumask_var (cfd->cpumask);free_cpumask_var (cfd->cpumask_ipi);
@@ -103,12 +103,12 @@ void __init call_function_init(void)* previous function call. For multi-cpu calls its even more interesting * as we'll have to ensure no other cpu is observing our csd. */ -static __always_inline void csd_lock_wait(struct call_single_data *csd) +static __always_inline void csd_lock_wait(call_single_data_t *csd) {smp_cond_load_acquire (&csd->flags, !(VAL &CSD_FLAG_LOCK )); } -static __always_inline void csd_lock(struct call_single_data *csd) +static __always_inline void csd_lock(call_single_data_t *csd) {csd_lock_wait (csd); csd->flags |=CSD_FLAG_LOCK ;
@@ -116,12 +116,12 @@ static __always_inline void csd_lock(struct call_single_data *csd)/* * prevent CPU from reordering the above assignment * to ->flags with any subsequent assignments to other -* fields of the specified call_single_data structure: +* fields of the specified call_single_data_t structure: */ smp_wmb (); } -static __always_inline void csd_unlock(struct call_single_data *csd) +static __always_inline void csd_unlock(call_single_data_t *csd) {WARN_ON (!(csd->flags &CSD_FLAG_LOCK ));
@@ -131,14 +131,14 @@ static __always_inline void csd_unlock(struct call_single_data *csd)smp_store_release (&csd->flags,0 ); } -static DEFINE_PER_CPU_SHARED_ALIGNED(struct call_single_data, csd_data); +static DEFINE_PER_CPU_SHARED_ALIGNED(call_single_data_t, csd_data);/* -* Insert a previously allocated call_single_data element +* Insert a previously allocated call_single_data_t element * for execution on the given CPU. data must already have * ->func, ->info, and ->flags set. */ -static int generic_exec_single (int cpu,struct call_single_data *csd, +static int generic_exec_single (int cpu,call_single_data_t *csd,smp_call_func_t func,void *info) {if (cpu ==smp_processor_id ()) {
@@ -210,7 +210,7 @@ static void flush_smp_call_function_queue(bool warn_cpu_offline) {struct llist_head *head;struct llist_node *entry; -struct call_single_data *csd, *csd_next; +call_single_data_t *csd, *csd_next;static bool warned;WARN_ON (!irqs_disabled ());
@@ -268,8 +268,10 @@ static void flush_smp_call_function_queue(bool warn_cpu_offline)int smp_call_function_single (int cpu,smp_call_func_t func,void *info,int wait) { -struct call_single_data *csd; -struct call_single_data csd_stack = { .flags =CSD_FLAG_LOCK |CSD_FLAG_SYNCHRONOUS }; +call_single_data_t *csd; +call_single_data_t csd_stack = { + .flags =CSD_FLAG_LOCK |CSD_FLAG_SYNCHRONOUS , + };int this_cpu;int err;
@@ -321,7 +323,7 @@ EXPORT_SYMBOL(smp_call_function_single);* NOTE: Be careful, there is unfortunately no current debugging facility to * validate the correctness of this serialization. */ -int smp_call_function_single_async (int cpu,struct call_single_data *csd) +int smp_call_function_single_async (int cpu,call_single_data_t *csd) {int err =0 ;
@@ -444,7 +446,7 @@ void smp_call_function_many(const struct cpumask *mask,cpumask_clear (cfd->cpumask_ipi);for_each_cpu (cpu, cfd->cpumask) { -struct call_single_data *csd =per_cpu_ptr (cfd->csd, cpu); +call_single_data_t *csd =per_cpu_ptr (cfd->csd, cpu);csd_lock (csd);if (wait)
@@ -460,7 +462,7 @@ void smp_call_function_many(const struct cpumask *mask,if (wait) {for_each_cpu (cpu, cfd->cpumask) { -struct call_single_data *csd; +call_single_data_t *csd; csd =per_cpu_ptr (cfd->csd, cpu);csd_lock_wait (csd);
kernel/up.c +1 -1
@@ -23,7 +23,7 @@ int smp_call_function_single(int cpu, void (*func) (void *info), void *info, }EXPORT_SYMBOL (smp_call_function_single); -int smp_call_function_single_async (int cpu,struct call_single_data *csd) +int smp_call_function_single_async (int cpu,call_single_data_t *csd) {unsigned long flags;
Signature · 1 line
2.11.0
Hi, Peter,
Quoted 37 lines · Ying
Quoted 5 lines · Peter Zijlstra
Quoted 2 lines · Ying
Yes. That looks good. So you will prepare the final patch? Or you hope me to do that?
I was hoping you'd do it ;-)
Thanks! Here is the updated patch
Best Regards,
Huang, Ying---------->8----------
From957735e9ff3922368286540dab852986fc7b23b5Mon Sep 17 00:00:00 2001
From: Huang Ying <[email protected]>
Date: Mon, 7 Aug 2017 16:55:33 +0800
Subject: [PATCH -v3] IPI: Avoid to use 2 cache lines for one
call_single_datastruct
call_single_datais used in IPI to transfer information between CPUs. Its size is bigger than sizeof(unsigned long) and less than cache line size. Now, it is allocated with no explicit alignment requirement. This makes it possible for allocatedcall_single_datato cross 2 cache lines. So that double the number of the cache lines that need to be transferred among CPUs.This is resolved by requiring
call_single_datato be aligned with the size ofcall_single_data. Now the size ofcall_single_datais the power of 2. If we add new fields tocall_single_data, we may need to add pads to make sure the size of new definition is the power of 2. Fortunately, this is enforced by gcc, which will report error for not power of 2 alignment requirement.To set alignment requirement of
call_single_datato the size ofcall_single_data, a struct definition and a typedef is used.
Quoted 6 lines
To test the effect of the patch, we use the vm-scalability multiple thread swap test case (swap-w-seq-mt). The test will create multiple threads and each thread will eat memory until all RAM and part of swap is used, so that huge number of IPI will be triggered when unmapping memory. In the test, the throughput of memory writing improves ~5% compared with misaligned
call_single_databecause of faster IPI.
What do you think about this version?
Best Regards,
Huang, Ying
Quoted 346 lines
[Add call_single_data_t and align with size of call_single_data]Suggested-by Peter Zijlstra <[email protected]>Signed-off-by "Huang, Ying" <[email protected]>Cc Peter Zijlstra <[email protected]>Cc Ingo Molnar <[email protected]>Cc Michael Ellerman <[email protected]>Cc Borislav Petkov <[email protected]>Cc Thomas Gleixner <[email protected]>Cc Juergen Gross <[email protected]>Cc Aaron Lu <[email protected]>--- arch/mips/kernel/smp.c | 6 ++-- block/blk-softirq.c | 2 +- drivers/block/null_blk.c | 2 +- drivers/cpuidle/coupled.c | 10 +++---- drivers/net/ethernet/cavium/liquidio/lio_main.c | 2 +- drivers/net/ethernet/cavium/liquidio/octeon_droq.h | 2 +- include/linux/blkdev.h | 2 +- include/linux/netdevice.h | 2 +- include/linux/smp.h | 8 ++++-- kernel/sched/sched.h | 2 +- kernel/smp.c | 32 ++++++++++++---------- kernel/up.c | 2 +- 12 files changed, 39 insertions(+), 33 deletions(-)arch/mips/kernel/smp.c +3
-3@@ -648,12 +648,12 @@ EXPORT_SYMBOL(flush_tlb_one);#ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST static DEFINE_PER_CPU(atomic_t, tick_broadcast_count); -static DEFINE_PER_CPU(struct call_single_data, tick_broadcast_csd); +static DEFINE_PER_CPU(call_single_data_t, tick_broadcast_csd);void tick_broadcast (const struct cpumask *mask) {atomic_t *count; -struct call_single_data *csd; +call_single_data_t *csd;int cpu;for_each_cpu (cpu, mask) {@@ -674,7 +674,7 @@ static void tick_broadcast_callee(void *info)static int __init tick_broadcast_init (void ) { -struct call_single_data *csd; +call_single_data_t *csd;int cpu;for (cpu =0 ; cpu <NR_CPUS ; cpu++) {block/blk-softirq.c +1
-1@@ -60,7 +60,7 @@ static void trigger_softirq(void *data)static int raise_blk_irq (int cpu,struct request *rq) {if (cpu_online (cpu)) { -struct call_single_data *data = &rq->csd; +call_single_data_t *data = &rq->csd; data->func = trigger_softirq; data->info = rq;drivers/block/null_blk.c +1
-1@@ -13,7 +13,7 @@struct nullb_cmd {struct list_head list;struct llist_node ll_list; -struct call_single_data csd; +call_single_data_t csd;struct request *rq;struct bio *bio;unsigned int tag;drivers/cpuidle/coupled.c +5
-5@@ -119,13 +119,13 @@ struct cpuidle_coupled {#define CPUIDLE_COUPLED_NOT_IDLE (-1) -static DEFINE_PER_CPU(struct call_single_data, cpuidle_coupled_poke_cb); +static DEFINE_PER_CPU(call_single_data_t, cpuidle_coupled_poke_cb);/* * The cpuidle_coupled_poke_pending mask is used to avoid calling -* __smp_call_function_single with the per cpu call_single_data struct already +* __smp_call_function_single with the per cpu call_single_data_t struct already * in use. This prevents a deadlock where two cpus are waiting for each others -* call_single_data struct to be available +* call_single_data_t struct to be available */ static cpumask_t cpuidle_coupled_poke_pending;@@ -339,7 +339,7 @@ static void cpuidle_coupled_handle_poke(void *info)*/ static void cpuidle_coupled_poke (int cpu) { -struct call_single_data *csd = &per_cpu (cpuidle_coupled_poke_cb, cpu); +call_single_data_t *csd = &per_cpu (cpuidle_coupled_poke_cb, cpu);if (!cpumask_test_and_set_cpu (cpu, &cpuidle_coupled_poke_pending))smp_call_function_single_async (cpu, csd);@@ -651,7 +651,7 @@ int cpuidle_coupled_register_device(struct cpuidle_device *dev) {int cpu;struct cpuidle_device *other_dev; -struct call_single_data *csd; +call_single_data_t *csd;struct cpuidle_coupled *coupled;if (cpumask_empty (&dev->coupled_cpus))drivers/net/ethernet/cavium/liquidio/lio_main.c +1
-1@@ -2468,7 +2468,7 @@ static void liquidio_napi_drv_callback(void *arg)if (OCTEON_CN23XX_PF (oct) || droq->cpu_id == this_cpu) {napi_schedule_irqoff (&droq->napi); }else { -struct call_single_data *csd = &droq->csd; +call_single_data_t *csd = &droq->csd; csd->func = napi_schedule_wrapper; csd->info = &droq->napi;drivers/net/ethernet/cavium/liquidio/octeon_droq.h +1
-1@@ -328,7 +328,7 @@ struct octeon_droq {u32 cpu_id; -struct call_single_data csd; +call_single_data_t csd; };#define OCT_DROQ_SIZE (sizeof(struct octeon_droq))include/linux/blkdev.h +1
-1@@ -134,7 +134,7 @@ typedef __u32 __bitwise req_flags_t;struct request {struct list_head queuelist;union { -struct call_single_data csd; +call_single_data_t csd;u64 fifo_time; };include/linux/netdevice.h +1
-1@@ -2774,7 +2774,7 @@ struct softnet_data {unsigned int input_queue_head____cacheline_aligned_in_smp ;/* Elements below can be accessed between CPUs for RPS/RFS */ -struct call_single_data csd____cacheline_aligned_in_smp ; +call_single_data_t csd____cacheline_aligned_in_smp ;struct softnet_data *rps_ipi_next;unsigned int cpu;unsigned int input_queue_tail;include/linux/smp.h +6
-2@@ -14,13 +14,17 @@#include <linux/llist.h> typedef void (*smp_call_func_t )(void *info); -struct call_single_data { +struct __call_single_data {struct llist_node llist;smp_call_func_t func;void *info;unsigned int flags; }; +/* Use __aligned() to avoid to use 2 cache lines for 1 csd */ +typedef struct __call_single_data call_single_data_t +__aligned(sizeof(struct __call_single_data)) ; +/* total number of cpus in this system (may exceed NR_CPUS) */ extern unsigned int total_cpus;@@ -48,7 +52,7 @@ void on_each_cpu_cond(bool (*cond_func)(int cpu, void *info), smp_call_func_t func, void *info, bool wait, gfp_t gfp_flags); -int smp_call_function_single_async (int cpu,struct call_single_data *csd); +int smp_call_function_single_async (int cpu,call_single_data_t *csd);#ifdef CONFIG_SMP kernel/sched/sched.h +1
-1@@ -769,7 +769,7 @@ struct rq {#ifdef CONFIG_SCHED_HRTICK #ifdef CONFIG_SMP int hrtick_csd_pending; -struct call_single_data hrtick_csd; +call_single_data_t hrtick_csd;#endif struct hrtimer hrtick_timer;#endif kernel/smp.c +17
-15@@ -28,7 +28,7 @@ enum { };struct call_function_data { -struct call_single_data __percpu *csd; +call_single_data_t __percpu *csd;cpumask_var_t cpumask;cpumask_var_t cpumask_ipi; };@@ -51,7 +51,7 @@ int smpcfd_prepare_cpu(unsigned int cpu)free_cpumask_var (cfd->cpumask);return -ENOMEM ; } - cfd->csd = alloc_percpu(struct call_single_data); + cfd->csd =alloc_percpu (call_single_data_t);if (!cfd->csd) {free_cpumask_var (cfd->cpumask);free_cpumask_var (cfd->cpumask_ipi);@@ -103,12 +103,12 @@ void __init call_function_init(void)* previous function call. For multi-cpu calls its even more interesting * as we'll have to ensure no other cpu is observing our csd. */ -static __always_inline void csd_lock_wait(struct call_single_data *csd) +static __always_inline void csd_lock_wait(call_single_data_t *csd) {smp_cond_load_acquire (&csd->flags, !(VAL &CSD_FLAG_LOCK )); } -static __always_inline void csd_lock(struct call_single_data *csd) +static __always_inline void csd_lock(call_single_data_t *csd) {csd_lock_wait (csd); csd->flags |=CSD_FLAG_LOCK ;@@ -116,12 +116,12 @@ static __always_inline void csd_lock(struct call_single_data *csd)/* * prevent CPU from reordering the above assignment * to ->flags with any subsequent assignments to other -* fields of the specified call_single_data structure: +* fields of the specified call_single_data_t structure: */ smp_wmb (); } -static __always_inline void csd_unlock(struct call_single_data *csd) +static __always_inline void csd_unlock(call_single_data_t *csd) {WARN_ON (!(csd->flags &CSD_FLAG_LOCK ));@@ -131,14 +131,14 @@ static __always_inline void csd_unlock(struct call_single_data *csd)smp_store_release (&csd->flags,0 ); } -static DEFINE_PER_CPU_SHARED_ALIGNED(struct call_single_data, csd_data); +static DEFINE_PER_CPU_SHARED_ALIGNED(call_single_data_t, csd_data);/* -* Insert a previously allocated call_single_data element +* Insert a previously allocated call_single_data_t element * for execution on the given CPU. data must already have * ->func, ->info, and ->flags set. */ -static int generic_exec_single (int cpu,struct call_single_data *csd, +static int generic_exec_single (int cpu,call_single_data_t *csd,smp_call_func_t func,void *info) {if (cpu ==smp_processor_id ()) {@@ -210,7 +210,7 @@ static void flush_smp_call_function_queue(bool warn_cpu_offline) {struct llist_head *head;struct llist_node *entry; -struct call_single_data *csd, *csd_next; +call_single_data_t *csd, *csd_next;static bool warned;WARN_ON (!irqs_disabled ());@@ -268,8 +268,10 @@ static void flush_smp_call_function_queue(bool warn_cpu_offline)int smp_call_function_single (int cpu,smp_call_func_t func,void *info,int wait) { -struct call_single_data *csd; -struct call_single_data csd_stack = { .flags =CSD_FLAG_LOCK |CSD_FLAG_SYNCHRONOUS }; +call_single_data_t *csd; +call_single_data_t csd_stack = { + .flags =CSD_FLAG_LOCK |CSD_FLAG_SYNCHRONOUS , + };int this_cpu;int err;@@ -321,7 +323,7 @@ EXPORT_SYMBOL(smp_call_function_single);* NOTE: Be careful, there is unfortunately no current debugging facility to * validate the correctness of this serialization. */ -int smp_call_function_single_async (int cpu,struct call_single_data *csd) +int smp_call_function_single_async (int cpu,call_single_data_t *csd) {int err =0 ;@@ -444,7 +446,7 @@ void smp_call_function_many(const struct cpumask *mask,cpumask_clear (cfd->cpumask_ipi);for_each_cpu (cpu, cfd->cpumask) { -struct call_single_data *csd =per_cpu_ptr (cfd->csd, cpu); +call_single_data_t *csd =per_cpu_ptr (cfd->csd, cpu);csd_lock (csd);if (wait)@@ -460,7 +462,7 @@ void smp_call_function_many(const struct cpumask *mask,if (wait) {for_each_cpu (cpu, cfd->cpumask) { -struct call_single_data *csd; +call_single_data_t *csd; csd =per_cpu_ptr (cfd->csd, cpu);csd_lock_wait (csd);kernel/up.c +1
-1@@ -23,7 +23,7 @@ int smp_call_function_single(int cpu, void (*func) (void *info), void *info, }EXPORT_SYMBOL (smp_call_function_single); -int smp_call_function_single_async (int cpu,struct call_single_data *csd) +int smp_call_function_single_async (int cpu,call_single_data_t *csd) {unsigned long flags;
Quoted 41 lines · Ying
Hi, Peter,
Quoted 37 lines · Ying
Quoted 5 lines · Peter Zijlstra
Quoted 2 lines · Ying
Yes. That looks good. So you will prepare the final patch? Or you hope me to do that?
I was hoping you'd do it ;-)
Thanks! Here is the updated patch
Best Regards,
Huang, Ying---------->8----------
From957735e9ff3922368286540dab852986fc7b23b5Mon Sep 17 00:00:00 2001
From: Huang Ying <[email protected]>
Date: Mon, 7 Aug 2017 16:55:33 +0800
Subject: [PATCH -v3] IPI: Avoid to use 2 cache lines for one
call_single_datastruct
call_single_datais used in IPI to transfer information between CPUs. Its size is bigger than sizeof(unsigned long) and less than cache line size. Now, it is allocated with no explicit alignment requirement. This makes it possible for allocatedcall_single_datato cross 2 cache lines. So that double the number of the cache lines that need to be transferred among CPUs.This is resolved by requiring
call_single_datato be aligned with the size ofcall_single_data. Now the size ofcall_single_datais the power of 2. If we add new fields tocall_single_data, we may need to add pads to make sure the size of new definition is the power of 2. Fortunately, this is enforced by gcc, which will report error for not power of 2 alignment requirement.To set alignment requirement of
call_single_datato the size ofcall_single_data, a struct definition and a typedef is used.
Quoted 9 lines
Quoted 6 lines
To test the effect of the patch, we use the vm-scalability multiple thread swap test case (swap-w-seq-mt). The test will create multiple threads and each thread will eat memory until all RAM and part of swap is used, so that huge number of IPI will be triggered when unmapping memory. In the test, the throughput of memory writing improves ~5% compared with misaligned
call_single_databecause of faster IPI.What do you think about this version?
Ping.
Best Regards,
Huang, Ying
Peter Zijlstra
maintainerQuoted 4 lines · Ying
Quoted 2 lines
What do you think about this version?
Ping.
Thanks, yes that got lost in the inbox :-(
I'll queue it, thanks !
tip-bot for Ying Huang
merge bot[tip:locking/core] smp: Avoid using two cache lines for struct call_single_data
In reply to Huang, Yingsmp: Avoid using two cache lines for struct call_single_data
Commit-ID: 966a967116e699762dbf4af7f9e0d1955c25aa37 Gitweb: http://git.kernel.org/tip/966a967116e699762dbf4af7f9e0d1955c25aa37 Author: Ying Huang <[email protected]> AuthorDate: Tue, 8 Aug 2017 12:30:00 +0800 Committer: Ingo Molnar <[email protected]> CommitDate: Tue, 29 Aug 2017 15:14:38 +0200
smp: Avoid using two cache lines for struct call_single_data
struct call_single_data is used in IPIs to transfer information between CPUs. Its size is bigger than sizeof(unsigned long) and less than cache line size. Currently it is not allocated with any explicit alignment requirements. This makes it possible for allocated call_single_data to cross two cache lines, which results in double the number of the cache lines that need to be transferred among CPUs.
This can be fixed by requiring call_single_data to be aligned with the size of call_single_data. Currently the size of call_single_data is the power of 2. If we add new fields to call_single_data, we may need to add padding to make sure the size of new definition is the power of 2 as well.
Fortunately, this is enforced by GCC, which will report bad sizes.
To set alignment requirements of call_single_data to the size of call_single_data, a struct definition and a typedef is used.
To test the effect of the patch, I used the vm-scalability multiple thread swap test case (swap-w-seq-mt). The test will create multiple threads and each thread will eat memory until all RAM and part of swap is used, so that huge number of IPIs are triggered when unmapping memory. In the test, the throughput of memory writing improves ~5% compared with misaligned call_single_data, because of faster IPIs.
--- arch/mips/kernel/smp.c | 6 ++-- block/blk-softirq.c | 2 +- drivers/block/null_blk.c | 2 +- drivers/cpuidle/coupled.c | 10 +++---- drivers/net/ethernet/cavium/liquidio/lio_main.c | 2 +- drivers/net/ethernet/cavium/liquidio/octeon_droq.h | 2 +- include/linux/blkdev.h | 2 +- include/linux/netdevice.h | 2 +- include/linux/smp.h | 8 ++++-- kernel/sched/sched.h | 2 +- kernel/smp.c | 32 ++++++++++++---------- kernel/up.c | 2 +- 12 files changed, 39 insertions(+), 33 deletions(-)
arch/mips/kernel/smp.c +3 -3
@@ -648,12 +648,12 @@ EXPORT_SYMBOL(flush_tlb_one);#ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST static DEFINE_PER_CPU(atomic_t, tick_broadcast_count); -static DEFINE_PER_CPU(struct call_single_data, tick_broadcast_csd); +static DEFINE_PER_CPU(call_single_data_t, tick_broadcast_csd);void tick_broadcast (const struct cpumask *mask) {atomic_t *count; -struct call_single_data *csd; +call_single_data_t *csd;int cpu;for_each_cpu (cpu, mask) {
@@ -674,7 +674,7 @@ static void tick_broadcast_callee(void *info)static int __init tick_broadcast_init (void ) { -struct call_single_data *csd; +call_single_data_t *csd;int cpu;for (cpu =0 ; cpu <NR_CPUS ; cpu++) {
block/blk-softirq.c +1 -1
@@ -60,7 +60,7 @@ static void trigger_softirq(void *data)static int raise_blk_irq (int cpu,struct request *rq) {if (cpu_online (cpu)) { -struct call_single_data *data = &rq->csd; +call_single_data_t *data = &rq->csd; data->func = trigger_softirq; data->info = rq;
drivers/block/null_blk.c +1 -1
@@ -13,7 +13,7 @@struct nullb_cmd {struct list_head list;struct llist_node ll_list; -struct call_single_data csd; +call_single_data_t csd;struct request *rq;struct bio *bio;unsigned int tag;
drivers/cpuidle/coupled.c +5 -5
@@ -119,13 +119,13 @@ struct cpuidle_coupled {#define CPUIDLE_COUPLED_NOT_IDLE (-1) -static DEFINE_PER_CPU(struct call_single_data, cpuidle_coupled_poke_cb); +static DEFINE_PER_CPU(call_single_data_t, cpuidle_coupled_poke_cb);/* * The cpuidle_coupled_poke_pending mask is used to avoid calling -* __smp_call_function_single with the per cpu call_single_data struct already +* __smp_call_function_single with the per cpu call_single_data_t struct already * in use. This prevents a deadlock where two cpus are waiting for each others -* call_single_data struct to be available +* call_single_data_t struct to be available */ static cpumask_t cpuidle_coupled_poke_pending;
@@ -339,7 +339,7 @@ static void cpuidle_coupled_handle_poke(void *info)*/ static void cpuidle_coupled_poke (int cpu) { -struct call_single_data *csd = &per_cpu (cpuidle_coupled_poke_cb, cpu); +call_single_data_t *csd = &per_cpu (cpuidle_coupled_poke_cb, cpu);if (!cpumask_test_and_set_cpu (cpu, &cpuidle_coupled_poke_pending))smp_call_function_single_async (cpu, csd);
@@ -651,7 +651,7 @@ int cpuidle_coupled_register_device(struct cpuidle_device *dev) {int cpu;struct cpuidle_device *other_dev; -struct call_single_data *csd; +call_single_data_t *csd;struct cpuidle_coupled *coupled;if (cpumask_empty (&dev->coupled_cpus))
drivers/net/ethernet/cavium/liquidio/lio_main.c +1 -1
@@ -2468,7 +2468,7 @@ static void liquidio_napi_drv_callback(void *arg)if (OCTEON_CN23XX_PF (oct) || droq->cpu_id == this_cpu) {napi_schedule_irqoff (&droq->napi); }else { -struct call_single_data *csd = &droq->csd; +call_single_data_t *csd = &droq->csd; csd->func = napi_schedule_wrapper; csd->info = &droq->napi;
drivers/net/ethernet/cavium/liquidio/octeon_droq.h +1 -1
@@ -328,7 +328,7 @@ struct octeon_droq {u32 cpu_id; -struct call_single_data csd; +call_single_data_t csd; };#define OCT_DROQ_SIZE (sizeof(struct octeon_droq))
include/linux/blkdev.h +1 -1
@@ -134,7 +134,7 @@ typedef __u32 __bitwise req_flags_t;struct request {struct list_head queuelist;union { -struct call_single_data csd; +call_single_data_t csd;u64 fifo_time; };
include/linux/netdevice.h +1 -1
@@ -2774,7 +2774,7 @@ struct softnet_data {unsigned int input_queue_head____cacheline_aligned_in_smp ;/* Elements below can be accessed between CPUs for RPS/RFS */ -struct call_single_data csd____cacheline_aligned_in_smp ; +call_single_data_t csd____cacheline_aligned_in_smp ;struct softnet_data *rps_ipi_next;unsigned int cpu;unsigned int input_queue_tail;
include/linux/smp.h +6 -2
@@ -14,13 +14,17 @@#include <linux/llist.h> typedef void (*smp_call_func_t )(void *info); -struct call_single_data { +struct __call_single_data {struct llist_node llist;smp_call_func_t func;void *info;unsigned int flags; }; +/* Use __aligned() to avoid to use 2 cache lines for 1 csd */ +typedef struct __call_single_data call_single_data_t +__aligned(sizeof(struct __call_single_data)) ; +/* total number of cpus in this system (may exceed NR_CPUS) */ extern unsigned int total_cpus;
@@ -48,7 +52,7 @@ void on_each_cpu_cond(bool (*cond_func)(int cpu, void *info), smp_call_func_t func, void *info, bool wait, gfp_t gfp_flags); -int smp_call_function_single_async (int cpu,struct call_single_data *csd); +int smp_call_function_single_async (int cpu,call_single_data_t *csd);#ifdef CONFIG_SMP
kernel/sched/sched.h +1 -1
@@ -769,7 +769,7 @@ struct rq {#ifdef CONFIG_SCHED_HRTICK #ifdef CONFIG_SMP int hrtick_csd_pending; -struct call_single_data hrtick_csd; +call_single_data_t hrtick_csd;#endif struct hrtimer hrtick_timer;#endif
kernel/smp.c +17 -15
@@ -28,7 +28,7 @@ enum { };struct call_function_data { -struct call_single_data __percpu *csd; +call_single_data_t __percpu *csd;cpumask_var_t cpumask;cpumask_var_t cpumask_ipi; };
@@ -51,7 +51,7 @@ int smpcfd_prepare_cpu(unsigned int cpu)free_cpumask_var (cfd->cpumask);return -ENOMEM ; } - cfd->csd = alloc_percpu(struct call_single_data); + cfd->csd =alloc_percpu (call_single_data_t);if (!cfd->csd) {free_cpumask_var (cfd->cpumask);free_cpumask_var (cfd->cpumask_ipi);
@@ -103,12 +103,12 @@ void __init call_function_init(void)* previous function call. For multi-cpu calls its even more interesting * as we'll have to ensure no other cpu is observing our csd. */ -static __always_inline void csd_lock_wait(struct call_single_data *csd) +static __always_inline void csd_lock_wait(call_single_data_t *csd) {smp_cond_load_acquire (&csd->flags, !(VAL &CSD_FLAG_LOCK )); } -static __always_inline void csd_lock(struct call_single_data *csd) +static __always_inline void csd_lock(call_single_data_t *csd) {csd_lock_wait (csd); csd->flags |=CSD_FLAG_LOCK ;
@@ -116,12 +116,12 @@ static __always_inline void csd_lock(struct call_single_data *csd)/* * prevent CPU from reordering the above assignment * to ->flags with any subsequent assignments to other -* fields of the specified call_single_data structure: +* fields of the specified call_single_data_t structure: */ smp_wmb (); } -static __always_inline void csd_unlock(struct call_single_data *csd) +static __always_inline void csd_unlock(call_single_data_t *csd) {WARN_ON (!(csd->flags &CSD_FLAG_LOCK ));
@@ -131,14 +131,14 @@ static __always_inline void csd_unlock(struct call_single_data *csd)smp_store_release (&csd->flags,0 ); } -static DEFINE_PER_CPU_SHARED_ALIGNED(struct call_single_data, csd_data); +static DEFINE_PER_CPU_SHARED_ALIGNED(call_single_data_t, csd_data);/* -* Insert a previously allocated call_single_data element +* Insert a previously allocated call_single_data_t element * for execution on the given CPU. data must already have * ->func, ->info, and ->flags set. */ -static int generic_exec_single (int cpu,struct call_single_data *csd, +static int generic_exec_single (int cpu,call_single_data_t *csd,smp_call_func_t func,void *info) {if (cpu ==smp_processor_id ()) {
@@ -210,7 +210,7 @@ static void flush_smp_call_function_queue(bool warn_cpu_offline) {struct llist_head *head;struct llist_node *entry; -struct call_single_data *csd, *csd_next; +call_single_data_t *csd, *csd_next;static bool warned;WARN_ON (!irqs_disabled ());
@@ -268,8 +268,10 @@ static void flush_smp_call_function_queue(bool warn_cpu_offline)int smp_call_function_single (int cpu,smp_call_func_t func,void *info,int wait) { -struct call_single_data *csd; -struct call_single_data csd_stack = { .flags =CSD_FLAG_LOCK |CSD_FLAG_SYNCHRONOUS }; +call_single_data_t *csd; +call_single_data_t csd_stack = { + .flags =CSD_FLAG_LOCK |CSD_FLAG_SYNCHRONOUS , + };int this_cpu;int err;
@@ -321,7 +323,7 @@ EXPORT_SYMBOL(smp_call_function_single);* NOTE: Be careful, there is unfortunately no current debugging facility to * validate the correctness of this serialization. */ -int smp_call_function_single_async (int cpu,struct call_single_data *csd) +int smp_call_function_single_async (int cpu,call_single_data_t *csd) {int err =0 ;
@@ -444,7 +446,7 @@ void smp_call_function_many(const struct cpumask *mask,cpumask_clear (cfd->cpumask_ipi);for_each_cpu (cpu, cfd->cpumask) { -struct call_single_data *csd =per_cpu_ptr (cfd->csd, cpu); +call_single_data_t *csd =per_cpu_ptr (cfd->csd, cpu);csd_lock (csd);if (wait)
@@ -460,7 +462,7 @@ void smp_call_function_many(const struct cpumask *mask,if (wait) {for_each_cpu (cpu, cfd->cpumask) { -struct call_single_data *csd; +call_single_data_t *csd; csd =per_cpu_ptr (cfd->csd, cpu);csd_lock_wait (csd);
kernel/up.c +1 -1
@@ -23,7 +23,7 @@ int smp_call_function_single(int cpu, void (*func) (void *info), void *info, }EXPORT_SYMBOL (smp_call_function_single); -int smp_call_function_single_async (int cpu,struct call_single_data *csd) +int smp_call_function_single_async (int cpu,call_single_data_t *csd) {unsigned long flags;
Peter Zijlstra
maintainerQuoted 18 lines · Ying
Peter Zijlstra <[email protected]> writes:
[snip]Quoted 16 lines
include/linux/smp.h +1
-1@@ -14,13 +14,16 @@#include <linux/llist.h> typedef void (*smp_call_func_t )(void *info); -struct call_single_data { +struct __call_single_data {struct llist_node llist;smp_call_func_t func;void *info;unsigned int flags; };
Quoted 8 lines
include/linux/smp.h line 26
Quoted 3 lines
include/linux/smp.h +3
-0+typedef struct __call_single_data call_single_data_t +__aligned(sizeof(struct __call_single_data)) ; +Another requirement of the alignment is that it should be the power of
2. Otherwise, for example, if someone adds a field to struct, so that the size becomes 40 onx86_64. The alignment should be 64 instead of 40.
Yes I know. This generates a compiler error if sizeof() isn't a power of 2. That's similar to the BUILD_BUG_ON() you added.
Quoted 2 lines · Ying
To allocate cache line size aligned percpu memory dynamically,
alloc_percpu_aligned()is introduced and used in iova drivers too.
alloc_percpu() already aligns objects as specified when they are declared.
Moreover the function is improperly named since it aligns to a cacheline(). If you want this then you would use
alloc_percpu_cacheline_aligned()
But then the alignment can already be requested by adding __cacheline_aligned to the per cpu definition.