mirror of
https://github.com/AuxXxilium/linux_dsm_epyc7002.git
synced 2024-12-25 19:35:07 +07:00
0ade34c370
We've measured that we spend ~0.6% of sys cpu time in cpumask_next_and(). It's essentially a joined iteration in search for a non-zero bit, which is currently implemented as a lookup join (find a nonzero bit on the lhs, lookup the rhs to see if it's set there). Implement a direct join (find a nonzero bit on the incrementally built join). Also add generic bitmap benchmarks in the new `test_find_bit` module for new function (see `find_next_and_bit` in [2] and [3] below). For cpumask_next_and, direct benchmarking shows that it's 1.17x to 14x faster with a geometric mean of 2.1 on 32 CPUs [1]. No impact on memory usage. Note that on Arm, the new pure-C implementation still outperforms the old one that uses a mix of C and asm (`find_next_bit`) [3]. [1] Approximate benchmark code: ``` unsigned long src1p[nr_cpumask_longs] = {pattern1}; unsigned long src2p[nr_cpumask_longs] = {pattern2}; for (/*a bunch of repetitions*/) { for (int n = -1; n <= nr_cpu_ids; ++n) { asm volatile("" : "+rm"(src1p)); // prevent any optimization asm volatile("" : "+rm"(src2p)); unsigned long result = cpumask_next_and(n, src1p, src2p); asm volatile("" : "+rm"(result)); } } ``` Results: pattern1 pattern2 time_before/time_after 0x0000ffff 0x0000ffff 1.65 0x0000ffff 0x00005555 2.24 0x0000ffff 0x00001111 2.94 0x0000ffff 0x00000000 14.0 0x00005555 0x0000ffff 1.67 0x00005555 0x00005555 1.71 0x00005555 0x00001111 1.90 0x00005555 0x00000000 6.58 0x00001111 0x0000ffff 1.46 0x00001111 0x00005555 1.49 0x00001111 0x00001111 1.45 0x00001111 0x00000000 3.10 0x00000000 0x0000ffff 1.18 0x00000000 0x00005555 1.18 0x00000000 0x00001111 1.17 0x00000000 0x00000000 1.25 ----------------------------- geo.mean 2.06 [2] test_find_next_bit, X86 (skylake) [ 3913.477422] Start testing find_bit() with random-filled bitmap [ 3913.477847] find_next_bit: 160868 cycles, 16484 iterations [ 3913.477933] find_next_zero_bit: 169542 cycles, 16285 iterations [ 3913.478036] find_last_bit: 201638 cycles, 16483 iterations [ 3913.480214] find_first_bit: 4353244 cycles, 16484 iterations [ 3913.480216] Start testing find_next_and_bit() with random-filled bitmap [ 3913.481074] find_next_and_bit: 89604 cycles, 8216 iterations [ 3913.481075] Start testing find_bit() with sparse bitmap [ 3913.481078] find_next_bit: 2536 cycles, 66 iterations [ 3913.481252] find_next_zero_bit: 344404 cycles, 32703 iterations [ 3913.481255] find_last_bit: 2006 cycles, 66 iterations [ 3913.481265] find_first_bit: 17488 cycles, 66 iterations [ 3913.481266] Start testing find_next_and_bit() with sparse bitmap [ 3913.481272] find_next_and_bit: 764 cycles, 1 iterations [3] test_find_next_bit, arm (v7 odroid XU3). [ 267.206928] Start testing find_bit() with random-filled bitmap [ 267.214752] find_next_bit: 4474 cycles, 16419 iterations [ 267.221850] find_next_zero_bit: 5976 cycles, 16350 iterations [ 267.229294] find_last_bit: 4209 cycles, 16419 iterations [ 267.279131] find_first_bit: 1032991 cycles, 16420 iterations [ 267.286265] Start testing find_next_and_bit() with random-filled bitmap [ 267.302386] find_next_and_bit: 2290 cycles, 8140 iterations [ 267.309422] Start testing find_bit() with sparse bitmap [ 267.316054] find_next_bit: 191 cycles, 66 iterations [ 267.322726] find_next_zero_bit: 8758 cycles, 32703 iterations [ 267.329803] find_last_bit: 84 cycles, 66 iterations [ 267.336169] find_first_bit: 4118 cycles, 66 iterations [ 267.342627] Start testing find_next_and_bit() with sparse bitmap [ 267.356919] find_next_and_bit: 91 cycles, 1 iterations [courbet@google.com: v6] Link: http://lkml.kernel.org/r/20171129095715.23430-1-courbet@google.com [geert@linux-m68k.org: m68k/bitops: always include <asm-generic/bitops/find.h>] Link: http://lkml.kernel.org/r/1512556816-28627-1-git-send-email-geert@linux-m68k.org Link: http://lkml.kernel.org/r/20171128131334.23491-1-courbet@google.com Signed-off-by: Clement Courbet <courbet@google.com> Signed-off-by: Geert Uytterhoeven <geert@linux-m68k.org> Cc: Yury Norov <ynorov@caviumnetworks.com> Cc: Geert Uytterhoeven <geert@linux-m68k.org> Cc: Alexey Dobriyan <adobriyan@gmail.com> Cc: Rasmus Villemoes <linux@rasmusvillemoes.dk> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
352 lines
8.7 KiB
C
352 lines
8.7 KiB
C
/* SPDX-License-Identifier: GPL-2.0 */
|
|
/*
|
|
* Copyright 1995, Russell King.
|
|
* Various bits and pieces copyrights include:
|
|
* Linus Torvalds (test_bit).
|
|
* Big endian support: Copyright 2001, Nicolas Pitre
|
|
* reworked by rmk.
|
|
*
|
|
* bit 0 is the LSB of an "unsigned long" quantity.
|
|
*
|
|
* Please note that the code in this file should never be included
|
|
* from user space. Many of these are not implemented in assembler
|
|
* since they would be too costly. Also, they require privileged
|
|
* instructions (which are not available from user mode) to ensure
|
|
* that they are atomic.
|
|
*/
|
|
|
|
#ifndef __ASM_ARM_BITOPS_H
|
|
#define __ASM_ARM_BITOPS_H
|
|
|
|
#ifdef __KERNEL__
|
|
|
|
#ifndef _LINUX_BITOPS_H
|
|
#error only <linux/bitops.h> can be included directly
|
|
#endif
|
|
|
|
#include <linux/compiler.h>
|
|
#include <linux/irqflags.h>
|
|
#include <asm/barrier.h>
|
|
|
|
/*
|
|
* These functions are the basis of our bit ops.
|
|
*
|
|
* First, the atomic bitops. These use native endian.
|
|
*/
|
|
static inline void ____atomic_set_bit(unsigned int bit, volatile unsigned long *p)
|
|
{
|
|
unsigned long flags;
|
|
unsigned long mask = BIT_MASK(bit);
|
|
|
|
p += BIT_WORD(bit);
|
|
|
|
raw_local_irq_save(flags);
|
|
*p |= mask;
|
|
raw_local_irq_restore(flags);
|
|
}
|
|
|
|
static inline void ____atomic_clear_bit(unsigned int bit, volatile unsigned long *p)
|
|
{
|
|
unsigned long flags;
|
|
unsigned long mask = BIT_MASK(bit);
|
|
|
|
p += BIT_WORD(bit);
|
|
|
|
raw_local_irq_save(flags);
|
|
*p &= ~mask;
|
|
raw_local_irq_restore(flags);
|
|
}
|
|
|
|
static inline void ____atomic_change_bit(unsigned int bit, volatile unsigned long *p)
|
|
{
|
|
unsigned long flags;
|
|
unsigned long mask = BIT_MASK(bit);
|
|
|
|
p += BIT_WORD(bit);
|
|
|
|
raw_local_irq_save(flags);
|
|
*p ^= mask;
|
|
raw_local_irq_restore(flags);
|
|
}
|
|
|
|
static inline int
|
|
____atomic_test_and_set_bit(unsigned int bit, volatile unsigned long *p)
|
|
{
|
|
unsigned long flags;
|
|
unsigned int res;
|
|
unsigned long mask = BIT_MASK(bit);
|
|
|
|
p += BIT_WORD(bit);
|
|
|
|
raw_local_irq_save(flags);
|
|
res = *p;
|
|
*p = res | mask;
|
|
raw_local_irq_restore(flags);
|
|
|
|
return (res & mask) != 0;
|
|
}
|
|
|
|
static inline int
|
|
____atomic_test_and_clear_bit(unsigned int bit, volatile unsigned long *p)
|
|
{
|
|
unsigned long flags;
|
|
unsigned int res;
|
|
unsigned long mask = BIT_MASK(bit);
|
|
|
|
p += BIT_WORD(bit);
|
|
|
|
raw_local_irq_save(flags);
|
|
res = *p;
|
|
*p = res & ~mask;
|
|
raw_local_irq_restore(flags);
|
|
|
|
return (res & mask) != 0;
|
|
}
|
|
|
|
static inline int
|
|
____atomic_test_and_change_bit(unsigned int bit, volatile unsigned long *p)
|
|
{
|
|
unsigned long flags;
|
|
unsigned int res;
|
|
unsigned long mask = BIT_MASK(bit);
|
|
|
|
p += BIT_WORD(bit);
|
|
|
|
raw_local_irq_save(flags);
|
|
res = *p;
|
|
*p = res ^ mask;
|
|
raw_local_irq_restore(flags);
|
|
|
|
return (res & mask) != 0;
|
|
}
|
|
|
|
#include <asm-generic/bitops/non-atomic.h>
|
|
|
|
/*
|
|
* A note about Endian-ness.
|
|
* -------------------------
|
|
*
|
|
* When the ARM is put into big endian mode via CR15, the processor
|
|
* merely swaps the order of bytes within words, thus:
|
|
*
|
|
* ------------ physical data bus bits -----------
|
|
* D31 ... D24 D23 ... D16 D15 ... D8 D7 ... D0
|
|
* little byte 3 byte 2 byte 1 byte 0
|
|
* big byte 0 byte 1 byte 2 byte 3
|
|
*
|
|
* This means that reading a 32-bit word at address 0 returns the same
|
|
* value irrespective of the endian mode bit.
|
|
*
|
|
* Peripheral devices should be connected with the data bus reversed in
|
|
* "Big Endian" mode. ARM Application Note 61 is applicable, and is
|
|
* available from http://www.arm.com/.
|
|
*
|
|
* The following assumes that the data bus connectivity for big endian
|
|
* mode has been followed.
|
|
*
|
|
* Note that bit 0 is defined to be 32-bit word bit 0, not byte 0 bit 0.
|
|
*/
|
|
|
|
/*
|
|
* Native endian assembly bitops. nr = 0 -> word 0 bit 0.
|
|
*/
|
|
extern void _set_bit(int nr, volatile unsigned long * p);
|
|
extern void _clear_bit(int nr, volatile unsigned long * p);
|
|
extern void _change_bit(int nr, volatile unsigned long * p);
|
|
extern int _test_and_set_bit(int nr, volatile unsigned long * p);
|
|
extern int _test_and_clear_bit(int nr, volatile unsigned long * p);
|
|
extern int _test_and_change_bit(int nr, volatile unsigned long * p);
|
|
|
|
/*
|
|
* Little endian assembly bitops. nr = 0 -> byte 0 bit 0.
|
|
*/
|
|
extern int _find_first_zero_bit_le(const unsigned long *p, unsigned size);
|
|
extern int _find_next_zero_bit_le(const unsigned long *p, int size, int offset);
|
|
extern int _find_first_bit_le(const unsigned long *p, unsigned size);
|
|
extern int _find_next_bit_le(const unsigned long *p, int size, int offset);
|
|
|
|
/*
|
|
* Big endian assembly bitops. nr = 0 -> byte 3 bit 0.
|
|
*/
|
|
extern int _find_first_zero_bit_be(const unsigned long *p, unsigned size);
|
|
extern int _find_next_zero_bit_be(const unsigned long *p, int size, int offset);
|
|
extern int _find_first_bit_be(const unsigned long *p, unsigned size);
|
|
extern int _find_next_bit_be(const unsigned long *p, int size, int offset);
|
|
|
|
#ifndef CONFIG_SMP
|
|
/*
|
|
* The __* form of bitops are non-atomic and may be reordered.
|
|
*/
|
|
#define ATOMIC_BITOP(name,nr,p) \
|
|
(__builtin_constant_p(nr) ? ____atomic_##name(nr, p) : _##name(nr,p))
|
|
#else
|
|
#define ATOMIC_BITOP(name,nr,p) _##name(nr,p)
|
|
#endif
|
|
|
|
/*
|
|
* Native endian atomic definitions.
|
|
*/
|
|
#define set_bit(nr,p) ATOMIC_BITOP(set_bit,nr,p)
|
|
#define clear_bit(nr,p) ATOMIC_BITOP(clear_bit,nr,p)
|
|
#define change_bit(nr,p) ATOMIC_BITOP(change_bit,nr,p)
|
|
#define test_and_set_bit(nr,p) ATOMIC_BITOP(test_and_set_bit,nr,p)
|
|
#define test_and_clear_bit(nr,p) ATOMIC_BITOP(test_and_clear_bit,nr,p)
|
|
#define test_and_change_bit(nr,p) ATOMIC_BITOP(test_and_change_bit,nr,p)
|
|
|
|
#ifndef __ARMEB__
|
|
/*
|
|
* These are the little endian, atomic definitions.
|
|
*/
|
|
#define find_first_zero_bit(p,sz) _find_first_zero_bit_le(p,sz)
|
|
#define find_next_zero_bit(p,sz,off) _find_next_zero_bit_le(p,sz,off)
|
|
#define find_first_bit(p,sz) _find_first_bit_le(p,sz)
|
|
#define find_next_bit(p,sz,off) _find_next_bit_le(p,sz,off)
|
|
|
|
#else
|
|
/*
|
|
* These are the big endian, atomic definitions.
|
|
*/
|
|
#define find_first_zero_bit(p,sz) _find_first_zero_bit_be(p,sz)
|
|
#define find_next_zero_bit(p,sz,off) _find_next_zero_bit_be(p,sz,off)
|
|
#define find_first_bit(p,sz) _find_first_bit_be(p,sz)
|
|
#define find_next_bit(p,sz,off) _find_next_bit_be(p,sz,off)
|
|
|
|
#endif
|
|
|
|
#if __LINUX_ARM_ARCH__ < 5
|
|
|
|
#include <asm-generic/bitops/ffz.h>
|
|
#include <asm-generic/bitops/__fls.h>
|
|
#include <asm-generic/bitops/__ffs.h>
|
|
#include <asm-generic/bitops/fls.h>
|
|
#include <asm-generic/bitops/ffs.h>
|
|
|
|
#else
|
|
|
|
static inline int constant_fls(int x)
|
|
{
|
|
int r = 32;
|
|
|
|
if (!x)
|
|
return 0;
|
|
if (!(x & 0xffff0000u)) {
|
|
x <<= 16;
|
|
r -= 16;
|
|
}
|
|
if (!(x & 0xff000000u)) {
|
|
x <<= 8;
|
|
r -= 8;
|
|
}
|
|
if (!(x & 0xf0000000u)) {
|
|
x <<= 4;
|
|
r -= 4;
|
|
}
|
|
if (!(x & 0xc0000000u)) {
|
|
x <<= 2;
|
|
r -= 2;
|
|
}
|
|
if (!(x & 0x80000000u)) {
|
|
x <<= 1;
|
|
r -= 1;
|
|
}
|
|
return r;
|
|
}
|
|
|
|
/*
|
|
* On ARMv5 and above those functions can be implemented around the
|
|
* clz instruction for much better code efficiency. __clz returns
|
|
* the number of leading zeros, zero input will return 32, and
|
|
* 0x80000000 will return 0.
|
|
*/
|
|
static inline unsigned int __clz(unsigned int x)
|
|
{
|
|
unsigned int ret;
|
|
|
|
asm("clz\t%0, %1" : "=r" (ret) : "r" (x));
|
|
|
|
return ret;
|
|
}
|
|
|
|
/*
|
|
* fls() returns zero if the input is zero, otherwise returns the bit
|
|
* position of the last set bit, where the LSB is 1 and MSB is 32.
|
|
*/
|
|
static inline int fls(int x)
|
|
{
|
|
if (__builtin_constant_p(x))
|
|
return constant_fls(x);
|
|
|
|
return 32 - __clz(x);
|
|
}
|
|
|
|
/*
|
|
* __fls() returns the bit position of the last bit set, where the
|
|
* LSB is 0 and MSB is 31. Zero input is undefined.
|
|
*/
|
|
static inline unsigned long __fls(unsigned long x)
|
|
{
|
|
return fls(x) - 1;
|
|
}
|
|
|
|
/*
|
|
* ffs() returns zero if the input was zero, otherwise returns the bit
|
|
* position of the first set bit, where the LSB is 1 and MSB is 32.
|
|
*/
|
|
static inline int ffs(int x)
|
|
{
|
|
return fls(x & -x);
|
|
}
|
|
|
|
/*
|
|
* __ffs() returns the bit position of the first bit set, where the
|
|
* LSB is 0 and MSB is 31. Zero input is undefined.
|
|
*/
|
|
static inline unsigned long __ffs(unsigned long x)
|
|
{
|
|
return ffs(x) - 1;
|
|
}
|
|
|
|
#define ffz(x) __ffs( ~(x) )
|
|
|
|
#endif
|
|
|
|
#include <asm-generic/bitops/fls64.h>
|
|
|
|
#include <asm-generic/bitops/sched.h>
|
|
#include <asm-generic/bitops/hweight.h>
|
|
#include <asm-generic/bitops/lock.h>
|
|
|
|
#ifdef __ARMEB__
|
|
|
|
static inline int find_first_zero_bit_le(const void *p, unsigned size)
|
|
{
|
|
return _find_first_zero_bit_le(p, size);
|
|
}
|
|
#define find_first_zero_bit_le find_first_zero_bit_le
|
|
|
|
static inline int find_next_zero_bit_le(const void *p, int size, int offset)
|
|
{
|
|
return _find_next_zero_bit_le(p, size, offset);
|
|
}
|
|
#define find_next_zero_bit_le find_next_zero_bit_le
|
|
|
|
static inline int find_next_bit_le(const void *p, int size, int offset)
|
|
{
|
|
return _find_next_bit_le(p, size, offset);
|
|
}
|
|
#define find_next_bit_le find_next_bit_le
|
|
|
|
#endif
|
|
|
|
#include <asm-generic/bitops/find.h>
|
|
#include <asm-generic/bitops/le.h>
|
|
|
|
/*
|
|
* Ext2 is defined to use little-endian byte ordering.
|
|
*/
|
|
#include <asm-generic/bitops/ext2-atomic-setbit.h>
|
|
|
|
#endif /* __KERNEL__ */
|
|
|
|
#endif /* _ARM_BITOPS_H */
|