mirror of
https://github.com/torvalds/linux.git
synced 2024-11-22 20:22:09 +00:00
4e23eeebb2
This branch consists of: Qu Wenruo: lib: bitmap: fix the duplicated comments on bitmap_to_arr64() https://lore.kernel.org/lkml/0d85e1dbad52ad7fb5787c4432bdb36cbd24f632.1656063005.git.wqu@suse.com/ Alexander Lobakin: bitops: let optimize out non-atomic bitops on compile-time constants https://lore.kernel.org/lkml/20220624121313.2382500-1-alexandr.lobakin@intel.com/T/ Yury Norov: lib: cleanup bitmap-related headers https://lore.kernel.org/linux-arm-kernel/YtCVeOGLiQ4gNPSf@yury-laptop/T/#m305522194c4d38edfdaffa71fcaaf2e2ca00a961 Alexander Lobakin: x86/olpc: fix 'logical not is only applied to the left hand side' https://www.spinics.net/lists/kernel/msg4440064.html Yury Norov: lib/nodemask: inline wrappers around bitmap https://lore.kernel.org/all/20220723214537.2054208-1-yury.norov@gmail.com/ -----BEGIN PGP SIGNATURE----- iQGzBAABCgAdFiEEi8GdvG6xMhdgpu/4sUSA/TofvsgFAmLpVvwACgkQsUSA/Tof vsiAHgwAwS9pl8GJ+fKYnue2CYo9349d2oT6BBUs/Rv8uqYEa4QkpYsR7NS733TG pos0hhoRvSOzrUP4qppXUjfJ+NkzLgpnKFOeWfFoNAKlHuaaMRvF3Y0Q/P8g0/Kg HPWcCQLHyCH9Wjs3e2TTgRjxTrHuruD2VJ401/PX/lw0DicUhmev5mUFa10uwFkP ZJRprjoFn9HJ0Hk16pFZDi36d3YumhACOcWRiJdoBDrEPV3S6lm9EeOy/yHBNp5k 9bKj+RboeT2t70KaZcKv+M5j1nu0cAhl7kRkjcxcmGyimI0l82Vgq9yFxhGqvWg8 RnCrJ5EaO08FGCAKG9GEwzdiNa24Gdq5XZSpQA7JZHmhmchpnnlNenJicyv0gOQi abChZeWSEsyA+78l2+kk9nezfVKUOnKDEZQxBVTOyWsmZYxHZV94oam340VjQDaY 4/fETdOy/qqPIxnpxAeFGWxZjcVaYiYPLj7KLPMsB0aAAF7pZrem465vSfgbrE81 +gCdqrWd =4dTW -----END PGP SIGNATURE----- Merge tag 'bitmap-6.0-rc1' of https://github.com/norov/linux Pull bitmap updates from Yury Norov: - fix the duplicated comments on bitmap_to_arr64() (Qu Wenruo) - optimize out non-atomic bitops on compile-time constants (Alexander Lobakin) - cleanup bitmap-related headers (Yury Norov) - x86/olpc: fix 'logical not is only applied to the left hand side' (Alexander Lobakin) - lib/nodemask: inline wrappers around bitmap (Yury Norov) * tag 'bitmap-6.0-rc1' of https://github.com/norov/linux: (26 commits) lib/nodemask: inline next_node_in() and node_random() powerpc: drop dependency on <asm/machdep.h> in archrandom.h x86/olpc: fix 'logical not is only applied to the left hand side' lib/cpumask: move some one-line wrappers to header file headers/deps: mm: align MANITAINERS and Docs with new gfp.h structure headers/deps: mm: Split <linux/gfp_types.h> out of <linux/gfp.h> headers/deps: mm: Optimize <linux/gfp.h> header dependencies lib/cpumask: move trivial wrappers around find_bit to the header lib/cpumask: change return types to unsigned where appropriate cpumask: change return types to bool where appropriate lib/bitmap: change type of bitmap_weight to unsigned long lib/bitmap: change return types to bool where appropriate arm: align find_bit declarations with generic kernel iommu/vt-d: avoid invalid memory access via node_online(NUMA_NO_NODE) lib/test_bitmap: test the tail after bitmap_to_arr64() lib/bitmap: fix off-by-one in bitmap_to_arr64() lib: test_bitmap: add compile-time optimization/evaluations assertions bitmap: don't assume compiler evaluates small mem*() builtins calls net/ice: fix initializing the bitmap in the switch code bitops: let optimize out non-atomic bitops on compile-time constants ...
201 lines
5.0 KiB
C
201 lines
5.0 KiB
C
// SPDX-License-Identifier: GPL-2.0
|
|
#include <linux/slab.h>
|
|
#include <linux/kernel.h>
|
|
#include <linux/bitops.h>
|
|
#include <linux/cpumask.h>
|
|
#include <linux/export.h>
|
|
#include <linux/memblock.h>
|
|
#include <linux/numa.h>
|
|
|
|
/**
|
|
* cpumask_next_wrap - helper to implement for_each_cpu_wrap
|
|
* @n: the cpu prior to the place to search
|
|
* @mask: the cpumask pointer
|
|
* @start: the start point of the iteration
|
|
* @wrap: assume @n crossing @start terminates the iteration
|
|
*
|
|
* Returns >= nr_cpu_ids on completion
|
|
*
|
|
* Note: the @wrap argument is required for the start condition when
|
|
* we cannot assume @start is set in @mask.
|
|
*/
|
|
unsigned int cpumask_next_wrap(int n, const struct cpumask *mask, int start, bool wrap)
|
|
{
|
|
unsigned int next;
|
|
|
|
again:
|
|
next = cpumask_next(n, mask);
|
|
|
|
if (wrap && n < start && next >= start) {
|
|
return nr_cpumask_bits;
|
|
|
|
} else if (next >= nr_cpumask_bits) {
|
|
wrap = true;
|
|
n = -1;
|
|
goto again;
|
|
}
|
|
|
|
return next;
|
|
}
|
|
EXPORT_SYMBOL(cpumask_next_wrap);
|
|
|
|
/* These are not inline because of header tangles. */
|
|
#ifdef CONFIG_CPUMASK_OFFSTACK
|
|
/**
|
|
* alloc_cpumask_var_node - allocate a struct cpumask on a given node
|
|
* @mask: pointer to cpumask_var_t where the cpumask is returned
|
|
* @flags: GFP_ flags
|
|
*
|
|
* Only defined when CONFIG_CPUMASK_OFFSTACK=y, otherwise is
|
|
* a nop returning a constant 1 (in <linux/cpumask.h>)
|
|
* Returns TRUE if memory allocation succeeded, FALSE otherwise.
|
|
*
|
|
* In addition, mask will be NULL if this fails. Note that gcc is
|
|
* usually smart enough to know that mask can never be NULL if
|
|
* CONFIG_CPUMASK_OFFSTACK=n, so does code elimination in that case
|
|
* too.
|
|
*/
|
|
bool alloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags, int node)
|
|
{
|
|
*mask = kmalloc_node(cpumask_size(), flags, node);
|
|
|
|
#ifdef CONFIG_DEBUG_PER_CPU_MAPS
|
|
if (!*mask) {
|
|
printk(KERN_ERR "=> alloc_cpumask_var: failed!\n");
|
|
dump_stack();
|
|
}
|
|
#endif
|
|
|
|
return *mask != NULL;
|
|
}
|
|
EXPORT_SYMBOL(alloc_cpumask_var_node);
|
|
|
|
/**
|
|
* alloc_bootmem_cpumask_var - allocate a struct cpumask from the bootmem arena.
|
|
* @mask: pointer to cpumask_var_t where the cpumask is returned
|
|
*
|
|
* Only defined when CONFIG_CPUMASK_OFFSTACK=y, otherwise is
|
|
* a nop (in <linux/cpumask.h>).
|
|
* Either returns an allocated (zero-filled) cpumask, or causes the
|
|
* system to panic.
|
|
*/
|
|
void __init alloc_bootmem_cpumask_var(cpumask_var_t *mask)
|
|
{
|
|
*mask = memblock_alloc(cpumask_size(), SMP_CACHE_BYTES);
|
|
if (!*mask)
|
|
panic("%s: Failed to allocate %u bytes\n", __func__,
|
|
cpumask_size());
|
|
}
|
|
|
|
/**
|
|
* free_cpumask_var - frees memory allocated for a struct cpumask.
|
|
* @mask: cpumask to free
|
|
*
|
|
* This is safe on a NULL mask.
|
|
*/
|
|
void free_cpumask_var(cpumask_var_t mask)
|
|
{
|
|
kfree(mask);
|
|
}
|
|
EXPORT_SYMBOL(free_cpumask_var);
|
|
|
|
/**
|
|
* free_bootmem_cpumask_var - frees result of alloc_bootmem_cpumask_var
|
|
* @mask: cpumask to free
|
|
*/
|
|
void __init free_bootmem_cpumask_var(cpumask_var_t mask)
|
|
{
|
|
memblock_free(mask, cpumask_size());
|
|
}
|
|
#endif
|
|
|
|
#if NR_CPUS > 1
|
|
/**
|
|
* cpumask_local_spread - select the i'th cpu with local numa cpu's first
|
|
* @i: index number
|
|
* @node: local numa_node
|
|
*
|
|
* This function selects an online CPU according to a numa aware policy;
|
|
* local cpus are returned first, followed by non-local ones, then it
|
|
* wraps around.
|
|
*
|
|
* It's not very efficient, but useful for setup.
|
|
*/
|
|
unsigned int cpumask_local_spread(unsigned int i, int node)
|
|
{
|
|
unsigned int cpu;
|
|
|
|
/* Wrap: we always want a cpu. */
|
|
i %= num_online_cpus();
|
|
|
|
if (node == NUMA_NO_NODE) {
|
|
for_each_cpu(cpu, cpu_online_mask)
|
|
if (i-- == 0)
|
|
return cpu;
|
|
} else {
|
|
/* NUMA first. */
|
|
for_each_cpu_and(cpu, cpumask_of_node(node), cpu_online_mask)
|
|
if (i-- == 0)
|
|
return cpu;
|
|
|
|
for_each_cpu(cpu, cpu_online_mask) {
|
|
/* Skip NUMA nodes, done above. */
|
|
if (cpumask_test_cpu(cpu, cpumask_of_node(node)))
|
|
continue;
|
|
|
|
if (i-- == 0)
|
|
return cpu;
|
|
}
|
|
}
|
|
BUG();
|
|
}
|
|
EXPORT_SYMBOL(cpumask_local_spread);
|
|
|
|
static DEFINE_PER_CPU(int, distribute_cpu_mask_prev);
|
|
|
|
/**
|
|
* Returns an arbitrary cpu within srcp1 & srcp2.
|
|
*
|
|
* Iterated calls using the same srcp1 and srcp2 will be distributed within
|
|
* their intersection.
|
|
*
|
|
* Returns >= nr_cpu_ids if the intersection is empty.
|
|
*/
|
|
unsigned int cpumask_any_and_distribute(const struct cpumask *src1p,
|
|
const struct cpumask *src2p)
|
|
{
|
|
unsigned int next, prev;
|
|
|
|
/* NOTE: our first selection will skip 0. */
|
|
prev = __this_cpu_read(distribute_cpu_mask_prev);
|
|
|
|
next = cpumask_next_and(prev, src1p, src2p);
|
|
if (next >= nr_cpu_ids)
|
|
next = cpumask_first_and(src1p, src2p);
|
|
|
|
if (next < nr_cpu_ids)
|
|
__this_cpu_write(distribute_cpu_mask_prev, next);
|
|
|
|
return next;
|
|
}
|
|
EXPORT_SYMBOL(cpumask_any_and_distribute);
|
|
|
|
unsigned int cpumask_any_distribute(const struct cpumask *srcp)
|
|
{
|
|
unsigned int next, prev;
|
|
|
|
/* NOTE: our first selection will skip 0. */
|
|
prev = __this_cpu_read(distribute_cpu_mask_prev);
|
|
|
|
next = cpumask_next(prev, srcp);
|
|
if (next >= nr_cpu_ids)
|
|
next = cpumask_first(srcp);
|
|
|
|
if (next < nr_cpu_ids)
|
|
__this_cpu_write(distribute_cpu_mask_prev, next);
|
|
|
|
return next;
|
|
}
|
|
EXPORT_SYMBOL(cpumask_any_distribute);
|
|
#endif /* NR_CPUS */
|