Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge tag 'bitmap-for-6.19' of github.com:/norov/linux

Pull bitmap updates from Yury Norov:

- Runtime field_{get,prep}() (Geert)

- Rust ID pool updates (Alice)

- min_t() simplification (David)

- __sw_hweightN kernel-doc fixes (Andy)

- cpumask.h headers cleanup (Andy)

* tag 'bitmap-for-6.19' of github.com:/norov/linux: (32 commits)
rust_binder: use bitmap for allocation of handles
rust: id_pool: do not immediately acquire new ids
rust: id_pool: do not supply starting capacity
rust: id_pool: rename IdPool::new() to with_capacity()
rust: bitmap: add BitmapVec::new_inline()
rust: bitmap: add MAX_LEN and MAX_INLINE_LEN constants
cpumask: Don't use "proxy" headers
soc: renesas: Use bitfield helpers
clk: renesas: Use bitfield helpers
ALSA: usb-audio: Convert to common field_{get,prep}() helpers
soc: renesas: rz-sysc: Convert to common field_get() helper
pinctrl: ma35: Convert to common field_{get,prep}() helpers
iio: mlx90614: Convert to common field_{get,prep}() helpers
iio: dac: Convert to common field_prep() helper
gpio: aspeed: Convert to common field_{get,prep}() helpers
EDAC/ie31200: Convert to common field_get() helper
crypto: qat - convert to common field_get() helper
clk: at91: Convert to common field_{get,prep}() helpers
bitfield: Add non-constant field_{prep,get}() helpers
bitfield: Add less-checking __FIELD_{GET,PREP}()
...

+303 -142
+1
MAINTAINERS
··· 4432 4432 F: include/asm-generic/bitops 4433 4433 F: include/asm-generic/bitops.h 4434 4434 F: include/linux/bitops.h 4435 + F: lib/hweight.c 4435 4436 F: lib/test_bitops.c 4436 4437 F: tools/*/bitops* 4437 4438
+2
arch/x86/include/asm/cpumask.h
··· 2 2 #ifndef _ASM_X86_CPUMASK_H 3 3 #define _ASM_X86_CPUMASK_H 4 4 #ifndef __ASSEMBLER__ 5 + 6 + #include <linux/compiler.h> 5 7 #include <linux/cpumask.h> 6 8 7 9 extern void setup_cpu_local_masks(void);
+47 -17
drivers/android/binder/process.rs
··· 19 19 cred::Credential, 20 20 error::Error, 21 21 fs::file::{self, File}, 22 + id_pool::IdPool, 22 23 list::{List, ListArc, ListArcField, ListLinks}, 23 24 mm, 24 25 prelude::*, ··· 395 394 struct ProcessNodeRefs { 396 395 /// Used to look up nodes using the 32-bit id that this process knows it by. 397 396 by_handle: RBTree<u32, ListArc<NodeRefInfo, { NodeRefInfo::LIST_PROC }>>, 397 + /// Used to quickly find unused ids in `by_handle`. 398 + handle_is_present: IdPool, 398 399 /// Used to look up nodes without knowing their local 32-bit id. The usize is the address of 399 400 /// the underlying `Node` struct as returned by `Node::global_id`. 400 401 by_node: RBTree<usize, u32>, ··· 411 408 fn new() -> Self { 412 409 Self { 413 410 by_handle: RBTree::new(), 411 + handle_is_present: IdPool::new(), 414 412 by_node: RBTree::new(), 415 413 freeze_listeners: RBTree::new(), 416 414 } ··· 806 802 pub(crate) fn insert_or_update_handle( 807 803 self: ArcBorrow<'_, Process>, 808 804 node_ref: NodeRef, 809 - is_mananger: bool, 805 + is_manager: bool, 810 806 ) -> Result<u32> { 811 807 { 812 808 let mut refs = self.node_refs.lock(); ··· 825 821 let reserve2 = RBTreeNodeReservation::new(GFP_KERNEL)?; 826 822 let info = UniqueArc::new_uninit(GFP_KERNEL)?; 827 823 828 - let mut refs = self.node_refs.lock(); 824 + let mut refs_lock = self.node_refs.lock(); 825 + let mut refs = &mut *refs_lock; 826 + 827 + let (unused_id, by_handle_slot) = loop { 828 + // ID 0 may only be used by the manager. 829 + let start = if is_manager { 0 } else { 1 }; 830 + 831 + if let Some(res) = refs.handle_is_present.find_unused_id(start) { 832 + match refs.by_handle.entry(res.as_u32()) { 833 + rbtree::Entry::Vacant(entry) => break (res, entry), 834 + rbtree::Entry::Occupied(_) => { 835 + pr_err!("Detected mismatch between handle_is_present and by_handle"); 836 + res.acquire(); 837 + kernel::warn_on!(true); 838 + return Err(EINVAL); 839 + } 840 + } 841 + } 842 + 843 + let grow_request = refs.handle_is_present.grow_request().ok_or(ENOMEM)?; 844 + drop(refs_lock); 845 + let resizer = grow_request.realloc(GFP_KERNEL)?; 846 + refs_lock = self.node_refs.lock(); 847 + refs = &mut *refs_lock; 848 + refs.handle_is_present.grow(resizer); 849 + }; 850 + let handle = unused_id.as_u32(); 829 851 830 852 // Do a lookup again as node may have been inserted before the lock was reacquired. 831 853 if let Some(handle_ref) = refs.by_node.get(&node_ref.node.global_id()) { ··· 861 831 return Ok(handle); 862 832 } 863 833 864 - // Find id. 865 - let mut target: u32 = if is_mananger { 0 } else { 1 }; 866 - for handle in refs.by_handle.keys() { 867 - if *handle > target { 868 - break; 869 - } 870 - if *handle == target { 871 - target = target.checked_add(1).ok_or(ENOMEM)?; 872 - } 873 - } 874 - 875 834 let gid = node_ref.node.global_id(); 876 835 let (info_proc, info_node) = { 877 - let info_init = NodeRefInfo::new(node_ref, target, self.into()); 836 + let info_init = NodeRefInfo::new(node_ref, handle, self.into()); 878 837 match info.pin_init_with(info_init) { 879 838 Ok(info) => ListArc::pair_from_pin_unique(info), 880 839 // error is infallible ··· 884 865 // `info_node` into the right node's `refs` list. 885 866 unsafe { info_proc.node_ref2().node.insert_node_info(info_node) }; 886 867 887 - refs.by_node.insert(reserve1.into_node(gid, target)); 888 - refs.by_handle.insert(reserve2.into_node(target, info_proc)); 889 - Ok(target) 868 + refs.by_node.insert(reserve1.into_node(gid, handle)); 869 + by_handle_slot.insert(info_proc, reserve2); 870 + unused_id.acquire(); 871 + Ok(handle) 890 872 } 891 873 892 874 pub(crate) fn get_transaction_node(&self, handle: u32) -> BinderResult<NodeRef> { ··· 952 932 let id = info.node_ref().node.global_id(); 953 933 refs.by_handle.remove(&handle); 954 934 refs.by_node.remove(&id); 935 + refs.handle_is_present.release_id(handle as usize); 936 + 937 + if let Some(shrink) = refs.handle_is_present.shrink_request() { 938 + drop(refs); 939 + // This intentionally ignores allocation failures. 940 + if let Ok(new_bitmap) = shrink.realloc(GFP_KERNEL) { 941 + refs = self.node_refs.lock(); 942 + refs.handle_is_present.shrink(new_bitmap); 943 + } 944 + } 955 945 } 956 946 } else { 957 947 // All refs are cleared in process exit, so this warning is expected in that case.
+1
drivers/clk/at91/clk-peripheral.c
··· 3 3 * Copyright (C) 2013 Boris BREZILLON <b.brezillon@overkiz.com> 4 4 */ 5 5 6 + #include <linux/bitfield.h> 6 7 #include <linux/bitops.h> 7 8 #include <linux/clk-provider.h> 8 9 #include <linux/clkdev.h>
-3
drivers/clk/at91/pmc.h
··· 117 117 unsigned int parent; 118 118 }; 119 119 120 - #define field_get(_mask, _reg) (((_reg) & (_mask)) >> (ffs(_mask) - 1)) 121 - #define field_prep(_mask, _val) (((_val) << (ffs(_mask) - 1)) & (_mask)) 122 - 123 120 #define ndck(a, s) (a[s - 1].id + 1) 124 121 #define nck(a) (a[ARRAY_SIZE(a) - 1].id + 1) 125 122
+3 -3
drivers/clk/renesas/clk-div6.c
··· 7 7 * Contact: Laurent Pinchart <laurent.pinchart@ideasonboard.com> 8 8 */ 9 9 10 + #include <linux/bitfield.h> 10 11 #include <linux/clk-provider.h> 11 12 #include <linux/init.h> 12 13 #include <linux/io.h> ··· 172 171 if (clock->src_mask == 0) 173 172 return 0; 174 173 175 - hw_index = (readl(clock->reg) & clock->src_mask) >> 176 - __ffs(clock->src_mask); 174 + hw_index = field_get(clock->src_mask, readl(clock->reg)); 177 175 for (i = 0; i < clk_hw_get_num_parents(hw); i++) { 178 176 if (clock->parents[i] == hw_index) 179 177 return i; ··· 191 191 if (index >= clk_hw_get_num_parents(hw)) 192 192 return -EINVAL; 193 193 194 - src = clock->parents[index] << __ffs(clock->src_mask); 194 + src = field_prep(clock->src_mask, clock->parents[index]); 195 195 writel((readl(clock->reg) & ~clock->src_mask) | src, clock->reg); 196 196 return 0; 197 197 }
+5 -10
drivers/clk/renesas/rcar-gen3-cpg.c
··· 54 54 { 55 55 struct cpg_pll_clk *pll_clk = to_pll_clk(hw); 56 56 unsigned int mult; 57 - u32 val; 58 57 59 - val = readl(pll_clk->pllcr_reg) & CPG_PLLnCR_STC_MASK; 60 - mult = (val >> __ffs(CPG_PLLnCR_STC_MASK)) + 1; 58 + mult = FIELD_GET(CPG_PLLnCR_STC_MASK, readl(pll_clk->pllcr_reg)) + 1; 61 59 62 60 return parent_rate * mult * pll_clk->fixed_mult; 63 61 } ··· 92 94 93 95 val = readl(pll_clk->pllcr_reg); 94 96 val &= ~CPG_PLLnCR_STC_MASK; 95 - val |= (mult - 1) << __ffs(CPG_PLLnCR_STC_MASK); 97 + val |= FIELD_PREP(CPG_PLLnCR_STC_MASK, mult - 1); 96 98 writel(val, pll_clk->pllcr_reg); 97 99 98 100 for (i = 1000; i; i--) { ··· 174 176 unsigned long parent_rate) 175 177 { 176 178 struct cpg_z_clk *zclk = to_z_clk(hw); 177 - unsigned int mult; 178 - u32 val; 179 - 180 - val = readl(zclk->reg) & zclk->mask; 181 - mult = 32 - (val >> __ffs(zclk->mask)); 179 + unsigned int mult = 32 - field_get(zclk->mask, readl(zclk->reg)); 182 180 183 181 return DIV_ROUND_CLOSEST_ULL((u64)parent_rate * mult, 184 182 32 * zclk->fixed_div); ··· 225 231 if (readl(zclk->kick_reg) & CPG_FRQCRB_KICK) 226 232 return -EBUSY; 227 233 228 - cpg_reg_modify(zclk->reg, zclk->mask, (32 - mult) << __ffs(zclk->mask)); 234 + cpg_reg_modify(zclk->reg, zclk->mask, 235 + field_prep(zclk->mask, 32 - mult)); 229 236 230 237 /* 231 238 * Set KICK bit in FRQCRB to update hardware setting and wait for
+3 -6
drivers/clk/renesas/rcar-gen4-cpg.c
··· 279 279 unsigned long parent_rate) 280 280 { 281 281 struct cpg_z_clk *zclk = to_z_clk(hw); 282 - unsigned int mult; 283 - u32 val; 284 - 285 - val = readl(zclk->reg) & zclk->mask; 286 - mult = 32 - (val >> __ffs(zclk->mask)); 282 + unsigned int mult = 32 - field_get(zclk->mask, readl(zclk->reg)); 287 283 288 284 return DIV_ROUND_CLOSEST_ULL((u64)parent_rate * mult, 289 285 32 * zclk->fixed_div); ··· 330 334 if (readl(zclk->kick_reg) & CPG_FRQCRB_KICK) 331 335 return -EBUSY; 332 336 333 - cpg_reg_modify(zclk->reg, zclk->mask, (32 - mult) << __ffs(zclk->mask)); 337 + cpg_reg_modify(zclk->reg, zclk->mask, 338 + field_prep(zclk->mask, 32 - mult)); 334 339 335 340 /* 336 341 * Set KICK bit in FRQCRB to update hardware setting and wait for
+1 -7
drivers/crypto/intel/qat/qat_common/adf_pm_dbgfs_utils.c
··· 1 1 // SPDX-License-Identifier: GPL-2.0-only 2 2 /* Copyright(c) 2025 Intel Corporation */ 3 + #include <linux/bitfield.h> 3 4 #include <linux/bitops.h> 4 5 #include <linux/sprintf.h> 5 6 #include <linux/string_helpers.h> 6 7 7 8 #include "adf_pm_dbgfs_utils.h" 8 - 9 - /* 10 - * This is needed because a variable is used to index the mask at 11 - * pm_scnprint_table(), making it not compile time constant, so the compile 12 - * asserts from FIELD_GET() or u32_get_bits() won't be fulfilled. 13 - */ 14 - #define field_get(_mask, _reg) (((_reg) & (_mask)) >> (ffs(_mask) - 1)) 15 9 16 10 #define PM_INFO_MAX_KEY_LEN 21 17 11
+1 -3
drivers/edac/ie31200_edac.c
··· 44 44 * but lo_hi_readq() ensures that we are safe across all e3-1200 processors. 45 45 */ 46 46 47 + #include <linux/bitfield.h> 47 48 #include <linux/module.h> 48 49 #include <linux/init.h> 49 50 #include <linux/pci.h> ··· 139 138 #define IE31200_CAPID0_PDCD BIT(4) 140 139 #define IE31200_CAPID0_DDPCD BIT(6) 141 140 #define IE31200_CAPID0_ECC BIT(1) 142 - 143 - /* Non-constant mask variant of FIELD_GET() */ 144 - #define field_get(_mask, _reg) (((_reg) & (_mask)) >> (ffs(_mask) - 1)) 145 141 146 142 static int nr_channels; 147 143 static struct pci_dev *mci_pdev;
+1 -4
drivers/gpio/gpio-aspeed.c
··· 5 5 * Joel Stanley <joel@jms.id.au> 6 6 */ 7 7 8 + #include <linux/bitfield.h> 8 9 #include <linux/cleanup.h> 9 10 #include <linux/clk.h> 10 11 #include <linux/gpio/aspeed.h> ··· 30 29 * access to some GPIOs and to arbitrate between coprocessor and ARM. 31 30 */ 32 31 #include <linux/gpio/consumer.h> 33 - 34 - /* Non-constant mask variant of FIELD_GET() and FIELD_PREP() */ 35 - #define field_get(_mask, _reg) (((_reg) & (_mask)) >> (ffs(_mask) - 1)) 36 - #define field_prep(_mask, _val) (((_val) << (ffs(_mask) - 1)) & (_mask)) 37 32 38 33 #define GPIO_G7_IRQ_STS_BASE 0x100 39 34 #define GPIO_G7_IRQ_STS_OFFSET(x) (GPIO_G7_IRQ_STS_BASE + (x) * 0x4)
-3
drivers/iio/dac/ad3530r.c
··· 53 53 #define AD3530R_MAX_CHANNELS 8 54 54 #define AD3531R_MAX_CHANNELS 4 55 55 56 - /* Non-constant mask variant of FIELD_PREP() */ 57 - #define field_prep(_mask, _val) (((_val) << (ffs(_mask) - 1)) & (_mask)) 58 - 59 56 enum ad3530r_mode { 60 57 AD3530R_NORMAL_OP, 61 58 AD3530R_POWERDOWN_1K,
+1 -4
drivers/iio/temperature/mlx90614.c
··· 22 22 * the "wakeup" GPIO is not given, power management will be disabled. 23 23 */ 24 24 25 + #include <linux/bitfield.h> 25 26 #include <linux/delay.h> 26 27 #include <linux/err.h> 27 28 #include <linux/gpio/consumer.h> ··· 68 67 #define MLX90614_CONST_OFFSET_REM 500000 /* remainder of offset (273.15*50) */ 69 68 #define MLX90614_CONST_SCALE 20 /* Scale in milliKelvin (0.02 * 1000) */ 70 69 #define MLX90614_CONST_FIR 0x7 /* Fixed value for FIR part of low pass filter */ 71 - 72 - /* Non-constant mask variant of FIELD_GET() and FIELD_PREP() */ 73 - #define field_get(_mask, _reg) (((_reg) & (_mask)) >> (ffs(_mask) - 1)) 74 - #define field_prep(_mask, _val) (((_val) << (ffs(_mask) - 1)) & (_mask)) 75 70 76 71 struct mlx_chip_info { 77 72 /* EEPROM offsets with 16-bit data, MSB first */
-4
drivers/pinctrl/nuvoton/pinctrl-ma35.c
··· 81 81 #define MVOLT_1800 0 82 82 #define MVOLT_3300 1 83 83 84 - /* Non-constant mask variant of FIELD_GET() and FIELD_PREP() */ 85 - #define field_get(_mask, _reg) (((_reg) & (_mask)) >> (ffs(_mask) - 1)) 86 - #define field_prep(_mask, _val) (((_val) << (ffs(_mask) - 1)) & (_mask)) 87 - 88 84 static const char * const gpio_group_name[] = { 89 85 "gpioa", "gpiob", "gpioc", "gpiod", "gpioe", "gpiof", "gpiog", 90 86 "gpioh", "gpioi", "gpioj", "gpiok", "gpiol", "gpiom", "gpion",
+2 -2
drivers/soc/renesas/renesas-soc.c
··· 5 5 * Copyright (C) 2014-2016 Glider bvba 6 6 */ 7 7 8 + #include <linux/bitfield.h> 8 9 #include <linux/io.h> 9 10 #include <linux/of.h> 10 11 #include <linux/of_address.h> ··· 525 524 eshi, eslo); 526 525 } 527 526 528 - if (soc->id && 529 - ((product & id->mask) >> __ffs(id->mask)) != soc->id) { 527 + if (soc->id && field_get(id->mask, product) != soc->id) { 530 528 pr_warn("SoC mismatch (product = 0x%x)\n", product); 531 529 ret = -ENODEV; 532 530 goto free_soc_dev_attr;
+1 -2
drivers/soc/renesas/rz-sysc.c
··· 5 5 * Copyright (C) 2024 Renesas Electronics Corp. 6 6 */ 7 7 8 + #include <linux/bitfield.h> 8 9 #include <linux/cleanup.h> 9 10 #include <linux/io.h> 10 11 #include <linux/mfd/syscon.h> ··· 16 15 #include <linux/sys_soc.h> 17 16 18 17 #include "rz-sysc.h" 19 - 20 - #define field_get(_mask, _reg) (((_reg) & (_mask)) >> (ffs(_mask) - 1)) 21 18 22 19 /** 23 20 * struct rz_sysc - RZ SYSC private data structure
+87 -8
include/linux/bitfield.h
··· 17 17 * FIELD_{GET,PREP} macros take as first parameter shifted mask 18 18 * from which they extract the base mask and shift amount. 19 19 * Mask must be a compilation time constant. 20 + * field_{get,prep} are variants that take a non-const mask. 20 21 * 21 22 * Example: 22 23 * ··· 61 60 62 61 #define __bf_cast_unsigned(type, x) ((__unsigned_scalar_typeof(type))(x)) 63 62 64 - #define __BF_FIELD_CHECK(_mask, _reg, _val, _pfx) \ 63 + #define __BF_FIELD_CHECK_MASK(_mask, _val, _pfx) \ 65 64 ({ \ 66 65 BUILD_BUG_ON_MSG(!__builtin_constant_p(_mask), \ 67 66 _pfx "mask is not constant"); \ ··· 70 69 ~((_mask) >> __bf_shf(_mask)) & \ 71 70 (0 + (_val)) : 0, \ 72 71 _pfx "value too large for the field"); \ 73 - BUILD_BUG_ON_MSG(__bf_cast_unsigned(_mask, _mask) > \ 74 - __bf_cast_unsigned(_reg, ~0ull), \ 75 - _pfx "type of reg too small for mask"); \ 76 72 __BUILD_BUG_ON_NOT_POWER_OF_2((_mask) + \ 77 73 (1ULL << __bf_shf(_mask))); \ 74 + }) 75 + 76 + #define __BF_FIELD_CHECK_REG(mask, reg, pfx) \ 77 + BUILD_BUG_ON_MSG(__bf_cast_unsigned(mask, mask) > \ 78 + __bf_cast_unsigned(reg, ~0ull), \ 79 + pfx "type of reg too small for mask") 80 + 81 + #define __BF_FIELD_CHECK(mask, reg, val, pfx) \ 82 + ({ \ 83 + __BF_FIELD_CHECK_MASK(mask, val, pfx); \ 84 + __BF_FIELD_CHECK_REG(mask, reg, pfx); \ 85 + }) 86 + 87 + #define __FIELD_PREP(mask, val, pfx) \ 88 + ({ \ 89 + __BF_FIELD_CHECK_MASK(mask, val, pfx); \ 90 + ((typeof(mask))(val) << __bf_shf(mask)) & (mask); \ 91 + }) 92 + 93 + #define __FIELD_GET(mask, reg, pfx) \ 94 + ({ \ 95 + __BF_FIELD_CHECK_MASK(mask, 0U, pfx); \ 96 + (typeof(mask))(((reg) & (mask)) >> __bf_shf(mask)); \ 78 97 }) 79 98 80 99 /** ··· 133 112 */ 134 113 #define FIELD_PREP(_mask, _val) \ 135 114 ({ \ 136 - __BF_FIELD_CHECK(_mask, 0ULL, _val, "FIELD_PREP: "); \ 137 - ((typeof(_mask))(_val) << __bf_shf(_mask)) & (_mask); \ 115 + __BF_FIELD_CHECK_REG(_mask, 0ULL, "FIELD_PREP: "); \ 116 + __FIELD_PREP(_mask, _val, "FIELD_PREP: "); \ 138 117 }) 139 118 140 119 #define __BF_CHECK_POW2(n) BUILD_BUG_ON_ZERO(((n) & ((n) - 1)) != 0) ··· 173 152 */ 174 153 #define FIELD_GET(_mask, _reg) \ 175 154 ({ \ 176 - __BF_FIELD_CHECK(_mask, _reg, 0U, "FIELD_GET: "); \ 177 - (typeof(_mask))(((_reg) & (_mask)) >> __bf_shf(_mask)); \ 155 + __BF_FIELD_CHECK_REG(_mask, _reg, "FIELD_GET: "); \ 156 + __FIELD_GET(_mask, _reg, "FIELD_GET: "); \ 178 157 }) 179 158 180 159 /** ··· 240 219 __MAKE_OP(64) 241 220 #undef __MAKE_OP 242 221 #undef ____MAKE_OP 222 + 223 + #define __field_prep(mask, val) \ 224 + ({ \ 225 + __auto_type __mask = (mask); \ 226 + typeof(__mask) __val = (val); \ 227 + unsigned int __shift = BITS_PER_TYPE(__mask) <= 32 ? \ 228 + __ffs(__mask) : __ffs64(__mask); \ 229 + (__val << __shift) & __mask; \ 230 + }) 231 + 232 + #define __field_get(mask, reg) \ 233 + ({ \ 234 + __auto_type __mask = (mask); \ 235 + typeof(__mask) __reg = (reg); \ 236 + unsigned int __shift = BITS_PER_TYPE(__mask) <= 32 ? \ 237 + __ffs(__mask) : __ffs64(__mask); \ 238 + (__reg & __mask) >> __shift; \ 239 + }) 240 + 241 + /** 242 + * field_prep() - prepare a bitfield element 243 + * @mask: shifted mask defining the field's length and position, must be 244 + * non-zero 245 + * @val: value to put in the field 246 + * 247 + * Return: field value masked and shifted to its final destination 248 + * 249 + * field_prep() masks and shifts up the value. The result should be 250 + * combined with other fields of the bitfield using logical OR. 251 + * Unlike FIELD_PREP(), @mask is not limited to a compile-time constant. 252 + * Typical usage patterns are a value stored in a table, or calculated by 253 + * shifting a constant by a variable number of bits. 254 + * If you want to ensure that @mask is a compile-time constant, please use 255 + * FIELD_PREP() directly instead. 256 + */ 257 + #define field_prep(mask, val) \ 258 + (__builtin_constant_p(mask) ? __FIELD_PREP(mask, val, "field_prep: ") \ 259 + : __field_prep(mask, val)) 260 + 261 + /** 262 + * field_get() - extract a bitfield element 263 + * @mask: shifted mask defining the field's length and position, must be 264 + * non-zero 265 + * @reg: value of entire bitfield 266 + * 267 + * Return: extracted field value 268 + * 269 + * field_get() extracts the field specified by @mask from the 270 + * bitfield passed in as @reg by masking and shifting it down. 271 + * Unlike FIELD_GET(), @mask is not limited to a compile-time constant. 272 + * Typical usage patterns are a value stored in a table, or calculated by 273 + * shifting a constant by a variable number of bits. 274 + * If you want to ensure that @mask is a compile-time constant, please use 275 + * FIELD_GET() directly instead. 276 + */ 277 + #define field_get(mask, reg) \ 278 + (__builtin_constant_p(mask) ? __FIELD_GET(mask, reg, "field_get: ") \ 279 + : __field_get(mask, reg)) 243 280 244 281 #endif
+7 -5
include/linux/cpumask.h
··· 7 7 * set of CPUs in a system, one bit position per CPU number. In general, 8 8 * only nr_cpu_ids (<= NR_CPUS) bits are valid. 9 9 */ 10 - #include <linux/cleanup.h> 11 - #include <linux/kernel.h> 12 - #include <linux/bitmap.h> 13 - #include <linux/cpumask_types.h> 14 10 #include <linux/atomic.h> 15 - #include <linux/bug.h> 11 + #include <linux/bitmap.h> 12 + #include <linux/cleanup.h> 13 + #include <linux/cpumask_types.h> 16 14 #include <linux/gfp_types.h> 17 15 #include <linux/numa.h> 16 + #include <linux/threads.h> 17 + #include <linux/types.h> 18 + 19 + #include <asm/bug.h> 18 20 19 21 /** 20 22 * cpumask_pr_args - printf args to output a cpumask
+4 -5
include/linux/nodemask.h
··· 245 245 } 246 246 247 247 /* FIXME: better would be to fix all architectures to never return 248 - > MAX_NUMNODES, then the silly min_ts could be dropped. */ 248 + > MAX_NUMNODES, then the silly min()s could be dropped. */ 249 249 250 250 #define first_node(src) __first_node(&(src)) 251 251 static __always_inline unsigned int __first_node(const nodemask_t *srcp) 252 252 { 253 - return min_t(unsigned int, MAX_NUMNODES, find_first_bit(srcp->bits, MAX_NUMNODES)); 253 + return min(MAX_NUMNODES, find_first_bit(srcp->bits, MAX_NUMNODES)); 254 254 } 255 255 256 256 #define next_node(n, src) __next_node((n), &(src)) 257 257 static __always_inline unsigned int __next_node(int n, const nodemask_t *srcp) 258 258 { 259 - return min_t(unsigned int, MAX_NUMNODES, find_next_bit(srcp->bits, MAX_NUMNODES, n+1)); 259 + return min(MAX_NUMNODES, find_next_bit(srcp->bits, MAX_NUMNODES, n+1)); 260 260 } 261 261 262 262 /* ··· 293 293 #define first_unset_node(mask) __first_unset_node(&(mask)) 294 294 static __always_inline unsigned int __first_unset_node(const nodemask_t *maskp) 295 295 { 296 - return min_t(unsigned int, MAX_NUMNODES, 297 - find_first_zero_bit(maskp->bits, MAX_NUMNODES)); 296 + return min(MAX_NUMNODES, find_first_zero_bit(maskp->bits, MAX_NUMNODES)); 298 297 } 299 298 300 299 #define NODE_MASK_LAST_WORD BITMAP_LAST_WORD_MASK(MAX_NUMNODES)
+2 -2
lib/hweight.c
··· 4 4 #include <asm/types.h> 5 5 6 6 /** 7 - * hweightN - returns the hamming weight of a N-bit word 8 - * @x: the word to weigh 7 + * DOC: __sw_hweightN - returns the hamming weight of a N-bit word 8 + * @w: the word to weigh 9 9 * 10 10 * The Hamming Weight of a number is the total number of bits set in it. 11 11 */
+29 -14
rust/kernel/bitmap.rs
··· 12 12 use crate::pr_err; 13 13 use core::ptr::NonNull; 14 14 15 - const BITS_PER_LONG: usize = bindings::BITS_PER_LONG as usize; 16 - 17 15 /// Represents a C bitmap. Wraps underlying C bitmap API. 18 16 /// 19 17 /// # Invariants ··· 147 149 /// 148 150 /// # Invariants 149 151 /// 150 - /// * `nbits` is `<= i32::MAX` and never changes. 151 - /// * if `nbits <= bindings::BITS_PER_LONG`, then `repr` is a `usize`. 152 + /// * `nbits` is `<= MAX_LEN`. 153 + /// * if `nbits <= MAX_INLINE_LEN`, then `repr` is a `usize`. 152 154 /// * otherwise, `repr` holds a non-null pointer to an initialized 153 155 /// array of `unsigned long` that is large enough to hold `nbits` bits. 154 156 pub struct BitmapVec { 155 157 /// Representation of bitmap. 156 158 repr: BitmapRepr, 157 - /// Length of this bitmap. Must be `<= i32::MAX`. 159 + /// Length of this bitmap. Must be `<= MAX_LEN`. 158 160 nbits: usize, 159 161 } 160 162 ··· 162 164 type Target = Bitmap; 163 165 164 166 fn deref(&self) -> &Bitmap { 165 - let ptr = if self.nbits <= BITS_PER_LONG { 167 + let ptr = if self.nbits <= BitmapVec::MAX_INLINE_LEN { 166 168 // SAFETY: Bitmap is represented inline. 167 169 #[allow(unused_unsafe, reason = "Safe since Rust 1.92.0")] 168 170 unsafe { ··· 181 183 182 184 impl core::ops::DerefMut for BitmapVec { 183 185 fn deref_mut(&mut self) -> &mut Bitmap { 184 - let ptr = if self.nbits <= BITS_PER_LONG { 186 + let ptr = if self.nbits <= BitmapVec::MAX_INLINE_LEN { 185 187 // SAFETY: Bitmap is represented inline. 186 188 #[allow(unused_unsafe, reason = "Safe since Rust 1.92.0")] 187 189 unsafe { ··· 211 213 212 214 impl Drop for BitmapVec { 213 215 fn drop(&mut self) { 214 - if self.nbits <= BITS_PER_LONG { 216 + if self.nbits <= BitmapVec::MAX_INLINE_LEN { 215 217 return; 216 218 } 217 219 // SAFETY: `self.ptr` was returned by the C `bitmap_zalloc`. ··· 224 226 } 225 227 226 228 impl BitmapVec { 229 + /// The maximum possible length of a `BitmapVec`. 230 + pub const MAX_LEN: usize = i32::MAX as usize; 231 + 232 + /// The maximum length that uses the inline representation. 233 + pub const MAX_INLINE_LEN: usize = usize::BITS as usize; 234 + 235 + /// Construct a longest possible inline [`BitmapVec`]. 236 + #[inline] 237 + pub fn new_inline() -> Self { 238 + // INVARIANT: `nbits <= MAX_INLINE_LEN`, so an inline bitmap is the right repr. 239 + BitmapVec { 240 + repr: BitmapRepr { bitmap: 0 }, 241 + nbits: BitmapVec::MAX_INLINE_LEN, 242 + } 243 + } 244 + 227 245 /// Constructs a new [`BitmapVec`]. 228 246 /// 229 247 /// Fails with [`AllocError`] when the [`BitmapVec`] could not be allocated. This 230 - /// includes the case when `nbits` is greater than `i32::MAX`. 248 + /// includes the case when `nbits` is greater than `MAX_LEN`. 231 249 #[inline] 232 250 pub fn new(nbits: usize, flags: Flags) -> Result<Self, AllocError> { 233 - if nbits <= BITS_PER_LONG { 251 + if nbits <= BitmapVec::MAX_INLINE_LEN { 234 252 return Ok(BitmapVec { 235 253 repr: BitmapRepr { bitmap: 0 }, 236 254 nbits, 237 255 }); 238 256 } 239 - if nbits > i32::MAX.try_into().unwrap() { 257 + if nbits > Self::MAX_LEN { 240 258 return Err(AllocError); 241 259 } 242 260 let nbits_u32 = u32::try_from(nbits).unwrap(); 243 - // SAFETY: `BITS_PER_LONG < nbits` and `nbits <= i32::MAX`. 261 + // SAFETY: `MAX_INLINE_LEN < nbits` and `nbits <= MAX_LEN`. 244 262 let ptr = unsafe { bindings::bitmap_zalloc(nbits_u32, flags.as_raw()) }; 245 263 let ptr = NonNull::new(ptr).ok_or(AllocError)?; 246 264 // INVARIANT: `ptr` returned by C `bitmap_zalloc` and `nbits` checked. ··· 509 495 #[test] 510 496 fn bitmap_borrow() { 511 497 let fake_bitmap: [usize; 2] = [0, 0]; 498 + let fake_bitmap_len = 2 * usize::BITS as usize; 512 499 // SAFETY: `fake_c_bitmap` is an array of expected length. 513 - let b = unsafe { Bitmap::from_raw(fake_bitmap.as_ptr(), 2 * BITS_PER_LONG) }; 514 - assert_eq!(2 * BITS_PER_LONG, b.len()); 500 + let b = unsafe { Bitmap::from_raw(fake_bitmap.as_ptr(), fake_bitmap_len) }; 501 + assert_eq!(fake_bitmap_len, b.len()); 515 502 assert_eq!(None, b.next_bit(0)); 516 503 } 517 504
+105 -36
rust/kernel/id_pool.rs
··· 7 7 use crate::alloc::{AllocError, Flags}; 8 8 use crate::bitmap::BitmapVec; 9 9 10 - const BITS_PER_LONG: usize = bindings::BITS_PER_LONG as usize; 11 - 12 10 /// Represents a dynamic ID pool backed by a [`BitmapVec`]. 13 11 /// 14 12 /// Clients acquire and release IDs from unset bits in a bitmap. ··· 23 25 /// Basic usage 24 26 /// 25 27 /// ``` 26 - /// use kernel::alloc::{AllocError, flags::GFP_KERNEL}; 27 - /// use kernel::id_pool::IdPool; 28 + /// use kernel::alloc::AllocError; 29 + /// use kernel::id_pool::{IdPool, UnusedId}; 28 30 /// 29 - /// let mut pool = IdPool::new(64, GFP_KERNEL)?; 31 + /// let mut pool = IdPool::with_capacity(64, GFP_KERNEL)?; 30 32 /// for i in 0..64 { 31 - /// assert_eq!(i, pool.acquire_next_id(i).ok_or(ENOSPC)?); 33 + /// assert_eq!(i, pool.find_unused_id(i).ok_or(ENOSPC)?.acquire()); 32 34 /// } 33 35 /// 34 36 /// pool.release_id(23); 35 - /// assert_eq!(23, pool.acquire_next_id(0).ok_or(ENOSPC)?); 37 + /// assert_eq!(23, pool.find_unused_id(0).ok_or(ENOSPC)?.acquire()); 36 38 /// 37 - /// assert_eq!(None, pool.acquire_next_id(0)); // time to realloc. 39 + /// assert!(pool.find_unused_id(0).is_none()); // time to realloc. 38 40 /// let resizer = pool.grow_request().ok_or(ENOSPC)?.realloc(GFP_KERNEL)?; 39 41 /// pool.grow(resizer); 40 42 /// 41 - /// assert_eq!(pool.acquire_next_id(0), Some(64)); 43 + /// assert_eq!(pool.find_unused_id(0).ok_or(ENOSPC)?.acquire(), 64); 42 44 /// # Ok::<(), Error>(()) 43 45 /// ``` 44 46 /// ··· 52 54 /// fn get_id_maybe_realloc(guarded_pool: &SpinLock<IdPool>) -> Result<usize, AllocError> { 53 55 /// let mut pool = guarded_pool.lock(); 54 56 /// loop { 55 - /// match pool.acquire_next_id(0) { 56 - /// Some(index) => return Ok(index), 57 + /// match pool.find_unused_id(0) { 58 + /// Some(index) => return Ok(index.acquire()), 57 59 /// None => { 58 60 /// let alloc_request = pool.grow_request(); 59 61 /// drop(pool); ··· 95 97 impl IdPool { 96 98 /// Constructs a new [`IdPool`]. 97 99 /// 98 - /// A capacity below [`BITS_PER_LONG`] is adjusted to 99 - /// [`BITS_PER_LONG`]. 100 + /// The pool will have a capacity of [`MAX_INLINE_LEN`]. 100 101 /// 101 - /// [`BITS_PER_LONG`]: srctree/include/asm-generic/bitsperlong.h 102 + /// [`MAX_INLINE_LEN`]: BitmapVec::MAX_INLINE_LEN 102 103 #[inline] 103 - pub fn new(num_ids: usize, flags: Flags) -> Result<Self, AllocError> { 104 - let num_ids = core::cmp::max(num_ids, BITS_PER_LONG); 104 + pub fn new() -> Self { 105 + Self { 106 + map: BitmapVec::new_inline(), 107 + } 108 + } 109 + 110 + /// Constructs a new [`IdPool`] with space for a specific number of bits. 111 + /// 112 + /// A capacity below [`MAX_INLINE_LEN`] is adjusted to [`MAX_INLINE_LEN`]. 113 + /// 114 + /// [`MAX_INLINE_LEN`]: BitmapVec::MAX_INLINE_LEN 115 + #[inline] 116 + pub fn with_capacity(num_ids: usize, flags: Flags) -> Result<Self, AllocError> { 117 + let num_ids = usize::max(num_ids, BitmapVec::MAX_INLINE_LEN); 105 118 let map = BitmapVec::new(num_ids, flags)?; 106 119 Ok(Self { map }) 107 120 } ··· 125 116 126 117 /// Returns a [`ReallocRequest`] if the [`IdPool`] can be shrunk, [`None`] otherwise. 127 118 /// 128 - /// The capacity of an [`IdPool`] cannot be shrunk below [`BITS_PER_LONG`]. 119 + /// The capacity of an [`IdPool`] cannot be shrunk below [`MAX_INLINE_LEN`]. 129 120 /// 130 - /// [`BITS_PER_LONG`]: srctree/include/asm-generic/bitsperlong.h 121 + /// [`MAX_INLINE_LEN`]: BitmapVec::MAX_INLINE_LEN 131 122 /// 132 123 /// # Examples 133 124 /// 134 125 /// ``` 135 - /// use kernel::alloc::{AllocError, flags::GFP_KERNEL}; 136 - /// use kernel::id_pool::{ReallocRequest, IdPool}; 126 + /// use kernel::{ 127 + /// alloc::AllocError, 128 + /// bitmap::BitmapVec, 129 + /// id_pool::{ 130 + /// IdPool, 131 + /// ReallocRequest, 132 + /// }, 133 + /// }; 137 134 /// 138 - /// let mut pool = IdPool::new(1024, GFP_KERNEL)?; 135 + /// let mut pool = IdPool::with_capacity(1024, GFP_KERNEL)?; 139 136 /// let alloc_request = pool.shrink_request().ok_or(AllocError)?; 140 137 /// let resizer = alloc_request.realloc(GFP_KERNEL)?; 141 138 /// pool.shrink(resizer); 142 - /// assert_eq!(pool.capacity(), kernel::bindings::BITS_PER_LONG as usize); 139 + /// assert_eq!(pool.capacity(), BitmapVec::MAX_INLINE_LEN); 143 140 /// # Ok::<(), AllocError>(()) 144 141 /// ``` 145 142 #[inline] 146 143 pub fn shrink_request(&self) -> Option<ReallocRequest> { 147 144 let cap = self.capacity(); 148 - // Shrinking below [`BITS_PER_LONG`] is never possible. 149 - if cap <= BITS_PER_LONG { 145 + // Shrinking below `MAX_INLINE_LEN` is never possible. 146 + if cap <= BitmapVec::MAX_INLINE_LEN { 150 147 return None; 151 148 } 152 149 // Determine if the bitmap can shrink based on the position of ··· 161 146 // bitmap should shrink to half its current size. 162 147 let Some(bit) = self.map.last_bit() else { 163 148 return Some(ReallocRequest { 164 - num_ids: BITS_PER_LONG, 149 + num_ids: BitmapVec::MAX_INLINE_LEN, 165 150 }); 166 151 }; 167 152 if bit >= (cap / 4) { 168 153 return None; 169 154 } 170 - let num_ids = usize::max(BITS_PER_LONG, cap / 2); 155 + let num_ids = usize::max(BitmapVec::MAX_INLINE_LEN, cap / 2); 171 156 Some(ReallocRequest { num_ids }) 172 157 } 173 158 ··· 192 177 193 178 /// Returns a [`ReallocRequest`] for growing this [`IdPool`], if possible. 194 179 /// 195 - /// The capacity of an [`IdPool`] cannot be grown above [`i32::MAX`]. 180 + /// The capacity of an [`IdPool`] cannot be grown above [`MAX_LEN`]. 181 + /// 182 + /// [`MAX_LEN`]: BitmapVec::MAX_LEN 196 183 #[inline] 197 184 pub fn grow_request(&self) -> Option<ReallocRequest> { 198 185 let num_ids = self.capacity() * 2; 199 - if num_ids > i32::MAX.try_into().unwrap() { 186 + if num_ids > BitmapVec::MAX_LEN { 200 187 return None; 201 188 } 202 189 Some(ReallocRequest { num_ids }) ··· 221 204 self.map = resizer.new; 222 205 } 223 206 224 - /// Acquires a new ID by finding and setting the next zero bit in the 225 - /// bitmap. 207 + /// Finds an unused ID in the bitmap. 226 208 /// 227 209 /// Upon success, returns its index. Otherwise, returns [`None`] 228 210 /// to indicate that a [`Self::grow_request`] is needed. 229 211 #[inline] 230 - pub fn acquire_next_id(&mut self, offset: usize) -> Option<usize> { 231 - let next_zero_bit = self.map.next_zero_bit(offset); 232 - if let Some(nr) = next_zero_bit { 233 - self.map.set_bit(nr); 234 - } 235 - next_zero_bit 212 + #[must_use] 213 + pub fn find_unused_id(&mut self, offset: usize) -> Option<UnusedId<'_>> { 214 + // INVARIANT: `next_zero_bit()` returns None or an integer less than `map.len()` 215 + Some(UnusedId { 216 + id: self.map.next_zero_bit(offset)?, 217 + pool: self, 218 + }) 236 219 } 237 220 238 221 /// Releases an ID. 239 222 #[inline] 240 223 pub fn release_id(&mut self, id: usize) { 241 224 self.map.clear_bit(id); 225 + } 226 + } 227 + 228 + /// Represents an unused id in an [`IdPool`]. 229 + /// 230 + /// # Invariants 231 + /// 232 + /// The value of `id` is less than `pool.map.len()`. 233 + pub struct UnusedId<'pool> { 234 + id: usize, 235 + pool: &'pool mut IdPool, 236 + } 237 + 238 + impl<'pool> UnusedId<'pool> { 239 + /// Get the unused id as an usize. 240 + /// 241 + /// Be aware that the id has not yet been acquired in the pool. The 242 + /// [`acquire`] method must be called to prevent others from taking the id. 243 + /// 244 + /// [`acquire`]: UnusedId::acquire() 245 + #[inline] 246 + #[must_use] 247 + pub fn as_usize(&self) -> usize { 248 + self.id 249 + } 250 + 251 + /// Get the unused id as an u32. 252 + /// 253 + /// Be aware that the id has not yet been acquired in the pool. The 254 + /// [`acquire`] method must be called to prevent others from taking the id. 255 + /// 256 + /// [`acquire`]: UnusedId::acquire() 257 + #[inline] 258 + #[must_use] 259 + pub fn as_u32(&self) -> u32 { 260 + // CAST: By the type invariants: 261 + // `self.id < pool.map.len() <= BitmapVec::MAX_LEN = i32::MAX`. 262 + self.id as u32 263 + } 264 + 265 + /// Acquire the unused id. 266 + #[inline] 267 + pub fn acquire(self) -> usize { 268 + self.pool.map.set_bit(self.id); 269 + self.id 270 + } 271 + } 272 + 273 + impl Default for IdPool { 274 + #[inline] 275 + fn default() -> Self { 276 + Self::new() 242 277 } 243 278 }
-4
sound/usb/mixer_quirks.c
··· 3416 3416 #define RME_DIGIFACE_REGISTER(reg, mask) (((reg) << 16) | (mask)) 3417 3417 #define RME_DIGIFACE_INVERT BIT(31) 3418 3418 3419 - /* Nonconst helpers */ 3420 - #define field_get(_mask, _reg) (((_reg) & (_mask)) >> (ffs(_mask) - 1)) 3421 - #define field_prep(_mask, _val) (((_val) << (ffs(_mask) - 1)) & (_mask)) 3422 - 3423 3419 static int snd_rme_digiface_write_reg(struct snd_kcontrol *kcontrol, int item, u16 mask, u16 val) 3424 3420 { 3425 3421 struct usb_mixer_elem_list *list = snd_kcontrol_chip(kcontrol);