Re: [RFC patch] spindep: add cross cache lines checking

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]


On Mon, 2012-03-05 at 13:43 +0800, Alex Shi wrote:
> On Mon, 2012-03-05 at 11:24 +0800, Alex Shi wrote:
> > Oops.
> > Sorry, the patch is not tested well! will update it later. 

resent for correct Thomas's e-mail address. Sorry. 
> 
> corrected version:
> ==========
> >From 28745c1970a61a1420d388660cd9dcc619cd38ba Mon Sep 17 00:00:00 2001
> From: Alex Shi <alex.shi@xxxxxxxxx>
> Date: Mon, 5 Mar 2012 13:03:35 +0800
> Subject: [PATCH] lockdep: add cross cache lines checking
> 
> Modern x86 CPU won't hold whole memory bus when executing 'lock'
> prefixed instructions unless the instruction destination is crossing 2
> cache lines. If so, it is disaster of system performance.
> 
> Actually if the lock is not in the 'packed' structure, gcc places it
> safely under x86 arch. But seems add this checking in
> CONFIG_DEBUG_LOCK_ALLOC is harmless.
> 
> Inspired-by: Andi Kleen <andi.kleen@xxxxxxxxx>
> Signed-off-by: Alex Shi <alex.shi@xxxxxxxxx>
> ---
>  arch/x86/include/asm/cache.h |    2 +
>  include/asm-generic/cache.h  |    2 +
>  lib/spinlock_debug.c         |   76 ++++++++++++++++++++++-------------------
>  3 files changed, 45 insertions(+), 35 deletions(-)
> 
> diff --git a/arch/x86/include/asm/cache.h b/arch/x86/include/asm/cache.h
> index 48f99f1..63c2316 100644
> --- a/arch/x86/include/asm/cache.h
> +++ b/arch/x86/include/asm/cache.h
> @@ -7,6 +7,8 @@
>  #define L1_CACHE_SHIFT	(CONFIG_X86_L1_CACHE_SHIFT)
>  #define L1_CACHE_BYTES	(1 << L1_CACHE_SHIFT)
>  
> +#define L1_CACHE_SIZE_MASK	(~(L1_CACHE_BYTES - 1))
> +
>  #define __read_mostly __attribute__((__section__(".data..read_mostly")))
>  
>  #define INTERNODE_CACHE_SHIFT CONFIG_X86_INTERNODE_CACHE_SHIFT
> diff --git a/include/asm-generic/cache.h b/include/asm-generic/cache.h
> index 1bfcfe5..6f8eb29 100644
> --- a/include/asm-generic/cache.h
> +++ b/include/asm-generic/cache.h
> @@ -9,4 +9,6 @@
>  #define L1_CACHE_SHIFT		5
>  #define L1_CACHE_BYTES		(1 << L1_CACHE_SHIFT)
>  
> +#define L1_CACHE_SIZE_MASK     (~(L1_CACHE_BYTES - 1))
> +
>  #endif /* __ASM_GENERIC_CACHE_H */
> diff --git a/lib/spinlock_debug.c b/lib/spinlock_debug.c
> index 5f3eacd..938a145 100644
> --- a/lib/spinlock_debug.c
> +++ b/lib/spinlock_debug.c
> @@ -13,41 +13,9 @@
>  #include <linux/delay.h>
>  #include <linux/module.h>
>  
> -void __raw_spin_lock_init(raw_spinlock_t *lock, const char *name,
> -			  struct lock_class_key *key)
> -{
> -#ifdef CONFIG_DEBUG_LOCK_ALLOC
> -	/*
> -	 * Make sure we are not reinitializing a held lock:
> -	 */
> -	debug_check_no_locks_freed((void *)lock, sizeof(*lock));
> -	lockdep_init_map(&lock->dep_map, name, key, 0);
> -#endif
> -	lock->raw_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
> -	lock->magic = SPINLOCK_MAGIC;
> -	lock->owner = SPINLOCK_OWNER_INIT;
> -	lock->owner_cpu = -1;
> -}
> -
> -EXPORT_SYMBOL(__raw_spin_lock_init);
> -
> -void __rwlock_init(rwlock_t *lock, const char *name,
> -		   struct lock_class_key *key)
> -{
> -#ifdef CONFIG_DEBUG_LOCK_ALLOC
> -	/*
> -	 * Make sure we are not reinitializing a held lock:
> -	 */
> -	debug_check_no_locks_freed((void *)lock, sizeof(*lock));
> -	lockdep_init_map(&lock->dep_map, name, key, 0);
> -#endif
> -	lock->raw_lock = (arch_rwlock_t) __ARCH_RW_LOCK_UNLOCKED;
> -	lock->magic = RWLOCK_MAGIC;
> -	lock->owner = SPINLOCK_OWNER_INIT;
> -	lock->owner_cpu = -1;
> -}
> -
> -EXPORT_SYMBOL(__rwlock_init);
> +#define is_cross_lines(p)						\
> +	(((unsigned long)(p) & L1_CACHE_SIZE_MASK) !=			\
> +	(((unsigned long)(p) + sizeof(*p) - 1) & L1_CACHE_SIZE_MASK))	\
>  
>  static void spin_dump(raw_spinlock_t *lock, const char *msg)
>  {
> @@ -296,3 +264,41 @@ void do_raw_write_unlock(rwlock_t *lock)
>  	debug_write_unlock(lock);
>  	arch_write_unlock(&lock->raw_lock);
>  }
> +
> +void __raw_spin_lock_init(raw_spinlock_t *lock, const char *name,
> +			  struct lock_class_key *key)
> +{
> +#ifdef CONFIG_DEBUG_LOCK_ALLOC
> +	/*
> +	 * Make sure we are not reinitializing a held lock:
> +	 */
> +	debug_check_no_locks_freed((void *)lock, sizeof(*lock));
> +	lockdep_init_map(&lock->dep_map, name, key, 0);
> +	SPIN_BUG_ON(is_cross_lines(&lock->raw_lock), lock,
> +			"!!! the lock cross cache lines !!!");
> +#endif
> +	lock->raw_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
> +	lock->magic = SPINLOCK_MAGIC;
> +	lock->owner = SPINLOCK_OWNER_INIT;
> +	lock->owner_cpu = -1;
> +}
> +EXPORT_SYMBOL(__raw_spin_lock_init);
> +
> +void __rwlock_init(rwlock_t *lock, const char *name,
> +		   struct lock_class_key *key)
> +{
> +#ifdef CONFIG_DEBUG_LOCK_ALLOC
> +	/*
> +	 * Make sure we are not reinitializing a held lock:
> +	 */
> +	debug_check_no_locks_freed((void *)lock, sizeof(*lock));
> +	lockdep_init_map(&lock->dep_map, name, key, 0);
> +	RWLOCK_BUG_ON(is_cross_lines(&lock->raw_lock), lock,
> +			"!!! the lock cross cache lines !!!");
> +#endif
> +	lock->raw_lock = (arch_rwlock_t) __ARCH_RW_LOCK_UNLOCKED;
> +	lock->magic = RWLOCK_MAGIC;
> +	lock->owner = SPINLOCK_OWNER_INIT;
> +	lock->owner_cpu = -1;
> +}
> +EXPORT_SYMBOL(__rwlock_init);


--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


[Other Archives]     [Linux Kernel Newbies]     [Linux Driver Development]     [Fedora Kernel]     [Linux Kernel Testers]     [Linux SH]     [Linux Omap]     [Linux Kbuild]     [Linux Tape]     [Linux Input]     [Linux Kernel Janitors]     [Linux Kernel Packagers]     [Linux Doc]     [Linux Man Pages]     [Linux API]     [Linux Memory Management]     [Linux Modules]     [Linux Standards]     [Kernel Announce]     [Netdev]     [Git]     [Linux PCI]     Linux CAN Development     [Linux I2C]     [Linux RDMA]     [Linux NUMA]     [Netfilter]     [Netfilter Devel]     [SELinux]     [Bugtraq]     [FIO]     [Linux Perf Users]     [Linux Serial]     [Linux PPP]     [Linux ISDN]     [Linux Next]     [Kernel Stable Commits]     [Linux Tip Commits]     [Kernel MM Commits]     [Linux Security Module]     [Filesystem Development]     [Ext3 Filesystem]     [Linux bcache]     [Ext4 Filesystem]     [Linux BTRFS]     [Linux CEPH Filesystem]     [Linux XFS]     [XFS]     [Linux NFS]     [Linux CIFS]     [Ecryptfs]     [Linux NILFS]     [Linux Cachefs]     [Reiser FS]     [Initramfs]     [Linux FB Devel]     [Linux OpenGL]     [DRI Devel]     [Fastboot]     [Linux RT Users]     [Linux RT Stable]     [eCos]     [Corosync]     [Linux Clusters]     [LVS Devel]     [Hot Plug]     [Linux Virtualization]     [KVM]     [KVM PPC]     [KVM ia64]     [Linux Containers]     [Linux Hexagon]     [Linux Cgroups]     [Util Linux]     [Wireless]     [Linux Bluetooth]     [Bluez Devel]     [Ethernet Bridging]     [Embedded Linux]     [Barebox]     [Linux MMC]     [Linux IIO]     [Sparse]     [Smatch]     [Linux Arch]     [x86 Platform Driver]     [Linux ACPI]     [Linux IBM ACPI]     [LM Sensors]     [CPU Freq]     [Linux Power Management]     [Linmodems]     [Linux DCCP]     [Linux SCTP]     [ALSA Devel]     [Linux USB]     [Linux PA RISC]     [Linux Samsung SOC]     [MIPS Linux]     [IBM S/390 Linux]     [ARM Linux]     [ARM Kernel]     [ARM MSM]     [Tegra Devel]     [Sparc Linux]     [Linux Security]     [Linux Sound]     [Linux Media]     [Video 4 Linux]     [Linux IRDA Users]     [Linux for the blind]     [Linux RAID]     [Linux ATA RAID]     [Device Mapper]     [Linux SCSI]     [SCSI Target Devel]     [Linux SCSI Target Infrastructure]     [Linux IDE]     [Linux SMP]     [Linux AXP]     [Linux Alpha]     [Linux M68K]     [Linux ia64]     [Linux 8086]     [Linux x86_64]     [Linux Config]     [Linux Apps]     [Linux MSDOS]     [Linux X.25]     [Linux Crypto]     [DM Crypt]     [Linux Trace Users]     [Linux Btrace]     [Linux Watchdog]     [Utrace Devel]     [Linux C Programming]     [Linux Assembly]     [Dash]     [DWARVES]     [Hail Devel]     [Linux Kernel Debugger]     [Linux gcc]     [Gcc Help]     [X.Org]     [Wine]

Add to Google Powered by Linux

[Older Kernel Discussion]     [Yosemite National Park Forum]     [Large Format Photos]     [Gimp]     [Yosemite Photos]     [Stuff]