802 lines · 19.3 KB · C
RawDownload
// SPDX-License-Identifier: GPL-2.0-only/* * Copyright (C) 2016 Facebook * Copyright (C) 2013-2014 Jens Axboe */ #include <linux/sched.h>#include <linux/random.h>#include <linux/sbitmap.h>#include <linux/seq_file.h> static int init_alloc_hint(struct sbitmap *sb, gfp_t flags){	unsigned depth = sb->depth; 	sb->alloc_hint = alloc_percpu_gfp(unsigned int, flags);	if (!sb->alloc_hint)		return -ENOMEM; 	if (depth && !sb->round_robin) {		int i; 		for_each_possible_cpu(i)			*per_cpu_ptr(sb->alloc_hint, i) = get_random_u32_below(depth);	}	return 0;} static inline unsigned update_alloc_hint_before_get(struct sbitmap *sb,						    unsigned int depth){	unsigned hint; 	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;} static inline void update_alloc_hint_after_get(struct sbitmap *sb,					       unsigned int depth,					       unsigned int hint,					       unsigned int nr){	if (nr == -1) {		/* If the map is full, a hint won't do us much good. */		this_cpu_write(*sb->alloc_hint, 0);	} else if (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 */static inline bool sbitmap_deferred_clear(struct sbitmap_word *map,		unsigned int depth, unsigned int alloc_hint, bool wrap){	unsigned long mask, word_mask; 	guard(raw_spinlock_irqsave)(&map->swap_lock); 	if (!map->cleared) {		if (depth == 0)			return false; 		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); 		return (READ_ONCE(map->word) & word_mask) != word_mask;	} 	/*	 * 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));	return true;} int sbitmap_init_node(struct sbitmap *sb, unsigned int depth, int shift,		      gfp_t flags, int node, bool round_robin,		      bool alloc_hint){	unsigned int bits_per_word;	int i; 	if (shift < 0)		shift = sbitmap_calculate_shift(depth); 	bits_per_word = 1U << shift;	if (bits_per_word > BITS_PER_LONG)		return -EINVAL; 	sb->shift = shift;	sb->depth = depth;	sb->map_nr = DIV_ROUND_UP(sb->depth, bits_per_word);	sb->round_robin = round_robin; 	if (depth == 0) {		sb->map = NULL;		return 0;	} 	if (alloc_hint) {		if (init_alloc_hint(sb, flags))			return -ENOMEM;	} else {		sb->alloc_hint = NULL;	} 	sb->map = kvzalloc_node(sb->map_nr * sizeof(*sb->map), flags, node);	if (!sb->map) {		free_percpu(sb->alloc_hint);		return -ENOMEM;	} 	for (i = 0; i < sb->map_nr; i++)		raw_spin_lock_init(&sb->map[i].swap_lock); 	return 0;}EXPORT_SYMBOL_GPL(sbitmap_init_node); void sbitmap_resize(struct sbitmap *sb, unsigned int depth){	unsigned int bits_per_word = 1U << sb->shift;	unsigned int i; 	for (i = 0; i < sb->map_nr; i++)		sbitmap_deferred_clear(&sb->map[i], 0, 0, 0); 	sb->depth = depth;	sb->map_nr = DIV_ROUND_UP(sb->depth, bits_per_word);}EXPORT_SYMBOL_GPL(sbitmap_resize); static int __sbitmap_get_word(unsigned long *word, unsigned long depth,			      unsigned int hint, bool wrap){	int nr; 	/* 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;		} 		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;	} 	return nr;} static int __sbitmap_get(struct sbitmap *sb, unsigned int alloc_hint){	unsigned int index; 	index = SB_NR_TO_INDEX(sb, alloc_hint); 	/*	 * 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; 	return sbitmap_find_bit(sb, UINT_MAX, index, alloc_hint,				!sb->round_robin);} int sbitmap_get(struct sbitmap *sb){	int nr;	unsigned int 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(sb, hint);	update_alloc_hint_after_get(sb, depth, hint, nr); 	return nr;}EXPORT_SYMBOL_GPL(sbitmap_get); static int __sbitmap_get_shallow(struct sbitmap *sb,				 unsigned int alloc_hint,				 unsigned long shallow_depth){	unsigned int index; 	index = SB_NR_TO_INDEX(sb, alloc_hint);	alloc_hint = SB_NR_TO_BIT(sb, alloc_hint); 	return sbitmap_find_bit(sb, shallow_depth, index, alloc_hint, true);} /** * 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. */static int sbitmap_get_shallow(struct sbitmap *sb, unsigned long shallow_depth){	int nr;	unsigned int 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); 	return nr;} bool sbitmap_any_bit_set(const struct sbitmap *sb){	unsigned int i; 	for (i = 0; i < sb->map_nr; i++) {		if (sb->map[i].word & ~sb->map[i].cleared)			return true;	}	return false;}EXPORT_SYMBOL_GPL(sbitmap_any_bit_set); static unsigned int __sbitmap_weight(const struct sbitmap *sb, bool set){	unsigned int i, weight = 0; 	for (i = 0; i < sb->map_nr; i++) {		const struct sbitmap_word *word = &sb->map[i];		unsigned int word_depth = __map_depth(sb, i); 		if (set)			weight += bitmap_weight(&word->word, word_depth);		else			weight += bitmap_weight(&word->cleared, word_depth);	}	return weight;} static unsigned int sbitmap_cleared(const struct sbitmap *sb){	return __sbitmap_weight(sb, false);} unsigned int sbitmap_weight(const struct sbitmap *sb){	return __sbitmap_weight(sb, true) - sbitmap_cleared(sb);}EXPORT_SYMBOL_GPL(sbitmap_weight); void sbitmap_show(struct sbitmap *sb, struct seq_file *m){	seq_printf(m, "depth=%u\n", sb->depth);	seq_printf(m, "busy=%u\n", sbitmap_weight(sb));	seq_printf(m, "cleared=%u\n", sbitmap_cleared(sb));	seq_printf(m, "bits_per_word=%u\n", 1U << sb->shift);	seq_printf(m, "map_nr=%u\n", sb->map_nr);}EXPORT_SYMBOL_GPL(sbitmap_show); static inline void emit_byte(struct seq_file *m, unsigned int offset, u8 byte){	if ((offset & 0xf) == 0) {		if (offset != 0)			seq_putc(m, '\n');		seq_printf(m, "%08x:", offset);	}	if ((offset & 0x1) == 0)		seq_putc(m, ' ');	seq_printf(m, "%02x", byte);} void sbitmap_bitmap_show(struct sbitmap *sb, struct seq_file *m){	u8 byte = 0;	unsigned int byte_bits = 0;	unsigned int offset = 0;	int i; 	for (i = 0; i < sb->map_nr; i++) {		unsigned long word = READ_ONCE(sb->map[i].word);		unsigned long cleared = READ_ONCE(sb->map[i].cleared);		unsigned int word_bits = __map_depth(sb, i); 		word &= ~cleared; 		while (word_bits > 0) {			unsigned int bits = min(8 - byte_bits, word_bits); 			byte |= (word & (BIT(bits) - 1)) << byte_bits;			byte_bits += bits;			if (byte_bits == 8) {				emit_byte(m, offset, byte);				byte = 0;				byte_bits = 0;				offset++;			}			word >>= bits;			word_bits -= bits;		}	}	if (byte_bits) {		emit_byte(m, offset, byte);		offset++;	}	if (offset)		seq_putc(m, '\n');}EXPORT_SYMBOL_GPL(sbitmap_bitmap_show); static unsigned int sbq_calc_wake_batch(struct sbitmap_queue *sbq,					unsigned int depth){	return clamp_t(unsigned int,		       min(depth, sbq->min_shallow_depth) / SBQ_WAIT_QUEUES,		       1, SBQ_WAKE_BATCH);} int sbitmap_queue_init_node(struct sbitmap_queue *sbq, unsigned int depth,			    int shift, bool round_robin, gfp_t flags, int node){	int ret;	int i; 	ret = sbitmap_init_node(&sbq->sb, depth, shift, flags, node,				round_robin, true);	if (ret)		return ret; 	sbq->min_shallow_depth = UINT_MAX;	sbq->wake_batch = sbq_calc_wake_batch(sbq, depth);	atomic_set(&sbq->wake_index, 0);	atomic_set(&sbq->ws_active, 0);	atomic_set(&sbq->completion_cnt, 0);	atomic_set(&sbq->wakeup_cnt, 0); 	sbq->ws = kzalloc_node(SBQ_WAIT_QUEUES * sizeof(*sbq->ws), flags, node);	if (!sbq->ws) {		sbitmap_free(&sbq->sb);		return -ENOMEM;	} 	for (i = 0; i < SBQ_WAIT_QUEUES; i++)		init_waitqueue_head(&sbq->ws[i].wait); 	return 0;}EXPORT_SYMBOL_GPL(sbitmap_queue_init_node); static void sbitmap_queue_update_wake_batch(struct sbitmap_queue *sbq,					    unsigned int depth){	unsigned int wake_batch; 	wake_batch = sbq_calc_wake_batch(sbq, depth);	if (sbq->wake_batch != wake_batch)		WRITE_ONCE(sbq->wake_batch, wake_batch);} void sbitmap_queue_recalculate_wake_batch(struct sbitmap_queue *sbq,					    unsigned int users){	unsigned int wake_batch;	unsigned int depth = (sbq->sb.depth + users - 1) / users; 	wake_batch = clamp_val(depth / SBQ_WAIT_QUEUES,			1, SBQ_WAKE_BATCH); 	WRITE_ONCE(sbq->wake_batch, wake_batch);}EXPORT_SYMBOL_GPL(sbitmap_queue_recalculate_wake_batch);

Lines 1–500 of 802.