hint = this_cpu_read(*sb->alloc_hint); if (unlikely(hint >= depth)) {
hint = depth ? get_random_u32_below(depth) : 0;
this_cpu_write(*sb->alloc_hint, hint);
}
return hint;
}
staticinlinevoid update_alloc_hint_after_get(struct sbitmap *sb, unsignedint depth, unsignedint hint, unsignedint nr)
{ if (nr == -1) { /* If the map is full, a hint won't do us much good. */
this_cpu_write(*sb->alloc_hint, 0);
} elseif (nr == hint || unlikely(sb->round_robin)) { /* Only update the hint if we used it. */
hint = nr + 1; if (hint >= depth - 1)
hint = 0;
this_cpu_write(*sb->alloc_hint, hint);
}
}
/* * See if we have deferred clears that we can batch move
*/ staticinlinebool sbitmap_deferred_clear(struct sbitmap_word *map, unsignedint depth, unsignedint alloc_hint, bool wrap)
{ unsignedlong mask, word_mask;
guard(raw_spinlock_irqsave)(&map->swap_lock);
if (!map->cleared) { if (depth == 0) returnfalse;
word_mask = (~0UL) >> (BITS_PER_LONG - depth); /* * The current behavior is to always retry after moving * ->cleared to word, and we change it to retry in case * of any free bits. To avoid an infinite loop, we need * to take wrap & alloc_hint into account, otherwise a * soft lockup may occur.
*/ if (!wrap && alloc_hint)
word_mask &= ~((1UL << alloc_hint) - 1);
/* * First get a stable cleared mask, setting the old mask to 0.
*/
mask = xchg(&map->cleared, 0);
/* * Now clear the masked bits in our free word
*/
atomic_long_andnot(mask, (atomic_long_t *)&map->word);
BUILD_BUG_ON(sizeof(atomic_long_t) != sizeof(map->word)); returntrue;
}
int sbitmap_init_node(struct sbitmap *sb, unsignedint depth, int shift,
gfp_t flags, int node, bool round_robin, bool alloc_hint)
{ unsignedint bits_per_word; int i;
if (shift < 0)
shift = sbitmap_calculate_shift(depth);
/* don't wrap if starting from 0 */
wrap = wrap && hint;
while (1) {
nr = find_next_zero_bit(word, depth, hint); 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;
}
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);
/* * Unless we're doing round robin tag allocation, just use the * alloc_hint to find the right word index. No point in looping * twice in find_next_zero_bit() for that case.
*/ if (sb->round_robin)
alloc_hint = SB_NR_TO_BIT(sb, alloc_hint); else
alloc_hint = 0;
/** * sbitmap_get_shallow() - Try to allocate a free bit from a &struct sbitmap, * limiting the depth used from each word. * @sb: Bitmap to allocate from. * @shallow_depth: The maximum number of bits to allocate from the bitmap. * * This rather specific operation allows for having multiple users with * different allocation limits. E.g., there can be a high-priority class that * uses sbitmap_get() and a low-priority class that uses sbitmap_get_shallow() * with a @shallow_depth of (sb->depth >> 1). Then, the low-priority * class can only allocate half of the total bits in the bitmap, preventing it * from starving out the high-priority class. * * Return: Non-negative allocated bit number if successful, -1 otherwise.
*/ staticint sbitmap_get_shallow(struct sbitmap *sb, unsignedlong shallow_depth)
{ int nr; unsignedint hint, depth;
if (WARN_ON_ONCE(unlikely(!sb->alloc_hint))) return -1;
depth = READ_ONCE(sb->depth);
hint = update_alloc_hint_before_get(sb, depth);
nr = __sbitmap_get_shallow(sb, hint, shallow_depth);
update_alloc_hint_after_get(sb, depth, hint, nr);
staticvoid __sbitmap_queue_wake_up(struct sbitmap_queue *sbq, int nr)
{ int i, wake_index, woken;
if (!atomic_read(&sbq->ws_active)) return;
wake_index = atomic_read(&sbq->wake_index); for (i = 0; i < SBQ_WAIT_QUEUES; i++) { struct sbq_wait_state *ws = &sbq->ws[wake_index];
/* * Advance the index before checking the current queue. * It improves fairness, by ensuring the queue doesn't * need to be fully emptied before trying to wake up * from the next one.
*/
wake_index = sbq_index_inc(wake_index);
if (waitqueue_active(&ws->wait)) {
woken = wake_up_nr(&ws->wait, nr); if (woken == nr) break;
nr -= woken;
}
}
if (wake_index != atomic_read(&sbq->wake_index))
atomic_set(&sbq->wake_index, wake_index);
}
staticinlinevoid sbitmap_update_cpu_hint(struct sbitmap *sb, int cpu, int tag)
{ if (likely(!sb->round_robin && tag < sb->depth))
data_race(*per_cpu_ptr(sb->alloc_hint, cpu) = tag);
}
void sbitmap_queue_clear_batch(struct sbitmap_queue *sbq, int offset, int *tags, int nr_tags)
{ struct sbitmap *sb = &sbq->sb; unsignedlong *addr = NULL; unsignedlong mask = 0; int i;
smp_mb__before_atomic(); for (i = 0; i < nr_tags; i++) { constint tag = tags[i] - offset; unsignedlong *this_addr;
void sbitmap_queue_clear(struct sbitmap_queue *sbq, unsignedint nr, unsignedint cpu)
{ /* * Once the clear bit is set, the bit may be allocated out. * * Orders READ/WRITE on the associated instance(such as request * of blk_mq) by this bit for avoiding race with re-allocation, * and its pair is the memory barrier implied in __sbitmap_get_word. * * One invariant is that the clear bit has to be zero when the bit * is in use.
*/
smp_mb__before_atomic();
sbitmap_deferred_clear_bit(&sbq->sb, nr);
/* * Pairs with the memory barrier in set_current_state() to ensure the * proper ordering of clear_bit_unlock()/waitqueue_active() in the waker * and test_and_set_bit_lock()/prepare_to_wait()/finish_wait() in the * waiter. See the comment on waitqueue_active().
*/
smp_mb__after_atomic();
sbitmap_queue_wake_up(sbq, 1);
sbitmap_update_cpu_hint(&sbq->sb, cpu, nr);
}
EXPORT_SYMBOL_GPL(sbitmap_queue_clear);
void sbitmap_queue_wake_all(struct sbitmap_queue *sbq)
{ int i, wake_index;
/* * Pairs with the memory barrier in set_current_state() like in * sbitmap_queue_wake_up().
*/
smp_mb();
wake_index = atomic_read(&sbq->wake_index); for (i = 0; i < SBQ_WAIT_QUEUES; i++) { struct sbq_wait_state *ws = &sbq->ws[wake_index];
if (waitqueue_active(&ws->wait))
wake_up(&ws->wait);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.