692 lines · 16.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> /* * See if we have deferred clears that we can batch move */static inline bool sbitmap_deferred_clear(struct sbitmap *sb, int index){	unsigned long mask, val;	bool ret = false;	unsigned long flags; 	spin_lock_irqsave(&sb->map[index].swap_lock, flags); 	if (!sb->map[index].cleared)		goto out_unlock; 	/*	 * First get a stable cleared mask, setting the old mask to 0.	 */	mask = xchg(&sb->map[index].cleared, 0); 	/*	 * Now clear the masked bits in our free word	 */	do {		val = sb->map[index].word;	} while (cmpxchg(&sb->map[index].word, val, val & ~mask) != val); 	ret = true;out_unlock:	spin_unlock_irqrestore(&sb->map[index].swap_lock, flags);	return ret;} int sbitmap_init_node(struct sbitmap *sb, unsigned int depth, int shift,		      gfp_t flags, int node){	unsigned int bits_per_word;	unsigned int i; 	if (shift < 0) {		shift = ilog2(BITS_PER_LONG);		/*		 * If the bitmap is small, shrink the number of bits per word so		 * we spread over a few cachelines, at least. If less than 4		 * bits, just forget about it, it's not going to work optimally		 * anyway.		 */		if (depth >= 4) {			while ((4U << shift) > depth)				shift--;		}	}	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); 	if (depth == 0) {		sb->map = NULL;		return 0;	} 	sb->map = kcalloc_node(sb->map_nr, sizeof(*sb->map), flags, node);	if (!sb->map)		return -ENOMEM; 	for (i = 0; i < sb->map_nr; i++) {		sb->map[i].depth = min(depth, bits_per_word);		depth -= sb->map[i].depth;		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, i); 	sb->depth = depth;	sb->map_nr = DIV_ROUND_UP(sb->depth, bits_per_word); 	for (i = 0; i < sb->map_nr; i++) {		sb->map[i].depth = min(depth, bits_per_word);		depth -= sb->map[i].depth;	}}EXPORT_SYMBOL_GPL(sbitmap_resize); static int __sbitmap_get_word(unsigned long *word, unsigned long depth,			      unsigned int hint, bool wrap){	unsigned int orig_hint = hint;	int nr; 	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 (orig_hint && hint && wrap) {				hint = orig_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_index(struct sbitmap *sb, int index,				     unsigned int alloc_hint, bool round_robin){	int nr; 	do {		nr = __sbitmap_get_word(&sb->map[index].word,					sb->map[index].depth, alloc_hint,					!round_robin);		if (nr != -1)			break;		if (!sbitmap_deferred_clear(sb, index))			break;	} while (1); 	return nr;} int sbitmap_get(struct sbitmap *sb, unsigned int alloc_hint, bool round_robin){	unsigned int i, index;	int nr = -1; 	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 (round_robin)		alloc_hint = SB_NR_TO_BIT(sb, alloc_hint);	else		alloc_hint = 0; 	for (i = 0; i < sb->map_nr; i++) {		nr = sbitmap_find_bit_in_index(sb, index, alloc_hint,						round_robin);		if (nr != -1) {			nr += index << sb->shift;			break;		} 		/* Jump to next index. */		alloc_hint = 0;		if (++index >= sb->map_nr)			index = 0;	} 	return nr;}EXPORT_SYMBOL_GPL(sbitmap_get); int sbitmap_get_shallow(struct sbitmap *sb, unsigned int alloc_hint,			unsigned long shallow_depth){	unsigned int i, index;	int nr = -1; 	index = SB_NR_TO_INDEX(sb, alloc_hint); 	for (i = 0; i < sb->map_nr; i++) {again:		nr = __sbitmap_get_word(&sb->map[index].word,					min(sb->map[index].depth, shallow_depth),					SB_NR_TO_BIT(sb, alloc_hint), true);		if (nr != -1) {			nr += index << sb->shift;			break;		} 		if (sbitmap_deferred_clear(sb, index))			goto again; 		/* Jump to next index. */		index++;		alloc_hint = index << sb->shift; 		if (index >= sb->map_nr) {			index = 0;			alloc_hint = 0;		}	} 	return nr;}EXPORT_SYMBOL_GPL(sbitmap_get_shallow); 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]; 		if (set)			weight += bitmap_weight(&word->word, word->depth);		else			weight += bitmap_weight(&word->cleared, word->depth);	}	return weight;} static unsigned int sbitmap_weight(const struct sbitmap *sb){	return __sbitmap_weight(sb, true);} static unsigned int sbitmap_cleared(const struct sbitmap *sb){	return __sbitmap_weight(sb, false);} 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) - sbitmap_cleared(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 = READ_ONCE(sb->map[i].depth); 		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){	unsigned int wake_batch;	unsigned int shallow_depth; 	/*	 * For each batch, we wake up one queue. We need to make sure that our	 * batch size is small enough that the full depth of the bitmap,	 * potentially limited by a shallow depth, is enough to wake up all of	 * the queues.	 *	 * Each full word of the bitmap has bits_per_word bits, and there might	 * be a partial word. There are depth / bits_per_word full words and	 * depth % bits_per_word bits left over. In bitwise arithmetic:	 *	 * bits_per_word = 1 << shift	 * depth / bits_per_word = depth >> shift	 * depth % bits_per_word = depth & ((1 << shift) - 1)	 *	 * Each word can be limited to sbq->min_shallow_depth bits.	 */	shallow_depth = min(1U << sbq->sb.shift, sbq->min_shallow_depth);	depth = ((depth >> sbq->sb.shift) * shallow_depth +		 min(depth & ((1U << sbq->sb.shift) - 1), shallow_depth));	wake_batch = clamp_t(unsigned int, depth / SBQ_WAIT_QUEUES, 1,			     SBQ_WAKE_BATCH); 	return 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);	if (ret)		return ret; 	sbq->alloc_hint = alloc_percpu_gfp(unsigned int, flags);	if (!sbq->alloc_hint) {		sbitmap_free(&sbq->sb);		return -ENOMEM;	} 	if (depth && !round_robin) {		for_each_possible_cpu(i)			*per_cpu_ptr(sbq->alloc_hint, i) = prandom_u32() % depth;	} 	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); 	sbq->ws = kzalloc_node(SBQ_WAIT_QUEUES * sizeof(*sbq->ws), flags, node);	if (!sbq->ws) {		free_percpu(sbq->alloc_hint);		sbitmap_free(&sbq->sb);		return -ENOMEM;	} 	for (i = 0; i < SBQ_WAIT_QUEUES; i++) {		init_waitqueue_head(&sbq->ws[i].wait);		atomic_set(&sbq->ws[i].wait_cnt, sbq->wake_batch);	} 	sbq->round_robin = round_robin;	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 = sbq_calc_wake_batch(sbq, depth);	int i; 	if (sbq->wake_batch != wake_batch) {		WRITE_ONCE(sbq->wake_batch, wake_batch);		/*		 * Pairs with the memory barrier in sbitmap_queue_wake_up()		 * to ensure that the batch size is updated before the wait		 * counts.		 */		smp_mb();		for (i = 0; i < SBQ_WAIT_QUEUES; i++)			atomic_set(&sbq->ws[i].wait_cnt, 1);	}} void sbitmap_queue_resize(struct sbitmap_queue *sbq, unsigned int depth){	sbitmap_queue_update_wake_batch(sbq, depth);	sbitmap_resize(&sbq->sb, depth);}EXPORT_SYMBOL_GPL(sbitmap_queue_resize); int __sbitmap_queue_get(struct sbitmap_queue *sbq){	unsigned int hint, depth;	int nr; 	hint = this_cpu_read(*sbq->alloc_hint);	depth = READ_ONCE(sbq->sb.depth);	if (unlikely(hint >= depth)) {		hint = depth ? prandom_u32() % depth : 0;		this_cpu_write(*sbq->alloc_hint, hint);	}	nr = sbitmap_get(&sbq->sb, hint, sbq->round_robin); 	if (nr == -1) {		/* If the map is full, a hint won't do us much good. */		this_cpu_write(*sbq->alloc_hint, 0);	} else if (nr == hint || unlikely(sbq->round_robin)) {		/* Only update the hint if we used it. */		hint = nr + 1;		if (hint >= depth - 1)			hint = 0;		this_cpu_write(*sbq->alloc_hint, hint);	} 	return nr;}EXPORT_SYMBOL_GPL(__sbitmap_queue_get); int __sbitmap_queue_get_shallow(struct sbitmap_queue *sbq,				unsigned int shallow_depth){	unsigned int hint, depth;	int nr; 	WARN_ON_ONCE(shallow_depth < sbq->min_shallow_depth); 	hint = this_cpu_read(*sbq->alloc_hint);	depth = READ_ONCE(sbq->sb.depth);	if (unlikely(hint >= depth)) {		hint = depth ? prandom_u32() % depth : 0;		this_cpu_write(*sbq->alloc_hint, hint);	}	nr = sbitmap_get_shallow(&sbq->sb, hint, shallow_depth); 	if (nr == -1) {		/* If the map is full, a hint won't do us much good. */		this_cpu_write(*sbq->alloc_hint, 0);	} else if (nr == hint || unlikely(sbq->round_robin)) {		/* Only update the hint if we used it. */		hint = nr + 1;		if (hint >= depth - 1)			hint = 0;		this_cpu_write(*sbq->alloc_hint, hint);	} 	return nr;}EXPORT_SYMBOL_GPL(__sbitmap_queue_get_shallow); void sbitmap_queue_min_shallow_depth(struct sbitmap_queue *sbq,				     unsigned int min_shallow_depth){	sbq->min_shallow_depth = min_shallow_depth;	sbitmap_queue_update_wake_batch(sbq, sbq->sb.depth);}EXPORT_SYMBOL_GPL(sbitmap_queue_min_shallow_depth); static struct sbq_wait_state *sbq_wake_ptr(struct sbitmap_queue *sbq){	int i, wake_index; 	if (!atomic_read(&sbq->ws_active))		return NULL; 	wake_index = atomic_read(&sbq->wake_index);	for (i = 0; i < SBQ_WAIT_QUEUES; i++) {		struct sbq_wait_state *ws = &sbq->ws[wake_index];

Lines 1–500 of 692.