lib/sbitmap.c
Source file repositories/reference/linux-study-clean/lib/sbitmap.c
File Facts
- System
- Linux kernel
- Corpus path
lib/sbitmap.c- Extension
.c- Size
- 19766 bytes
- Lines
- 803
- Domain
- Kernel Services
- Bucket
- lib
- Inferred role
- Kernel Services: exported/initcall integration point
- Status
- integration implementation candidate
Why This File Exists
Shared kernel service surface used by multiple subsystems, including helpers, cryptography, virtualization support, and async I/O infrastructure.
- Shared kernel service surface used by multiple subsystems, including helpers, cryptography, virtualization support, and async I/O infrastructure.
- Exports symbols or registers init work; inspect boot/module ordering and who consumes the exported contract.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/sched.hlinux/random.hlinux/sbitmap.hlinux/seq_file.h
Detected Declarations
function Copyrightfunction update_alloc_hint_before_getfunction update_alloc_hint_after_getfunction sbitmap_deferred_clearfunction sbitmap_init_nodefunction sbitmap_resizefunction __sbitmap_get_wordfunction sbitmap_find_bit_in_wordfunction __map_depth_with_shallowfunction sbitmap_find_bitfunction __sbitmap_getfunction sbitmap_getfunction __sbitmap_get_shallowfunction sbitmap_get_shallowfunction sbitmap_any_bit_setfunction __sbitmap_weightfunction sbitmap_clearedfunction sbitmap_weightfunction sbitmap_showfunction emit_bytefunction sbitmap_bitmap_showfunction sbq_calc_wake_batchfunction sbitmap_queue_init_nodefunction sbitmap_queue_update_wake_batchfunction sbitmap_queue_recalculate_wake_batchfunction sbitmap_queue_resizefunction __sbitmap_queue_getfunction __sbitmap_queue_get_batchfunction sbitmap_queue_get_shallowfunction sbitmap_queue_min_shallow_depthfunction __sbitmap_queue_wake_upfunction sbitmap_queue_wake_upfunction sbitmap_update_cpu_hintfunction sbitmap_queue_clear_batchfunction sbitmap_queue_clearfunction sbitmap_queue_wake_allfunction sbitmap_queue_showfunction sbitmap_add_wait_queuefunction sbitmap_del_wait_queuefunction sbitmap_prepare_to_waitfunction sbitmap_finish_waitexport sbitmap_init_nodeexport sbitmap_resizeexport sbitmap_getexport sbitmap_any_bit_setexport sbitmap_weightexport sbitmap_showexport sbitmap_bitmap_show
Annotated Snippet
if (unlikely(nr >= depth)) {
/*
* We started with an offset, and we didn't reset the
* offset to 0 in a failure case, so start from 0 to
* exhaust the map.
*/
if (hint && wrap) {
hint = 0;
continue;
}
return -1;
}
if (!test_and_set_bit_lock(nr, word))
break;
hint = nr + 1;
if (hint >= depth - 1)
hint = 0;
}
return nr;
}
static int sbitmap_find_bit_in_word(struct sbitmap_word *map,
unsigned int depth,
unsigned int alloc_hint,
bool wrap)
{
int nr;
do {
nr = __sbitmap_get_word(&map->word, depth,
alloc_hint, wrap);
if (nr != -1)
break;
if (!sbitmap_deferred_clear(map, depth, alloc_hint, wrap))
break;
} while (1);
return nr;
}
static unsigned int __map_depth_with_shallow(const struct sbitmap *sb,
int index,
unsigned int shallow_depth)
{
u64 shallow_word_depth;
unsigned int word_depth, reminder;
word_depth = __map_depth(sb, index);
if (shallow_depth >= sb->depth)
return word_depth;
shallow_word_depth = word_depth * shallow_depth;
reminder = do_div(shallow_word_depth, sb->depth);
if (reminder >= (index + 1) * word_depth)
shallow_word_depth++;
return (unsigned int)shallow_word_depth;
}
static int sbitmap_find_bit(struct sbitmap *sb,
unsigned int shallow_depth,
unsigned int index,
unsigned int alloc_hint,
bool wrap)
{
unsigned int i;
int nr = -1;
for (i = 0; i < sb->map_nr; i++) {
unsigned int depth = __map_depth_with_shallow(sb, index,
shallow_depth);
if (depth)
nr = sbitmap_find_bit_in_word(&sb->map[index], depth,
alloc_hint, wrap);
if (nr != -1) {
nr += index << sb->shift;
break;
}
/* Jump to next index. */
alloc_hint = 0;
if (++index >= sb->map_nr)
index = 0;
}
Annotation
- Immediate include surface: `linux/sched.h`, `linux/random.h`, `linux/sbitmap.h`, `linux/seq_file.h`.
- Detected declarations: `function Copyright`, `function update_alloc_hint_before_get`, `function update_alloc_hint_after_get`, `function sbitmap_deferred_clear`, `function sbitmap_init_node`, `function sbitmap_resize`, `function __sbitmap_get_word`, `function sbitmap_find_bit_in_word`, `function __map_depth_with_shallow`, `function sbitmap_find_bit`.
- Atlas domain: Kernel Services / lib.
- Implementation status: integration implementation candidate.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.