Skip to content

Commit

Permalink
sbitmap: randomize initial alloc_hint values
Browse files Browse the repository at this point in the history
In order to get good cache behavior from a sbitmap, we want each CPU to
stick to its own cacheline(s) as much as possible. This might happen
naturally as the bitmap gets filled up and the alloc_hint values spread
out, but we really want this behavior from the start. blk-mq apparently
intended to do this, but the code to do this was never wired up. Get rid
of the dead code and make it part of the sbitmap library.

Signed-off-by: Omar Sandoval <osandov@fb.com>
Signed-off-by: Jens Axboe <axboe@fb.com>
  • Loading branch information
osandov authored and axboe committed Sep 17, 2016
1 parent f4a644d commit 98d9541
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 9 deletions.
8 changes: 0 additions & 8 deletions block/blk-mq-tag.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/random.h>

#include <linux/blk-mq.h>
#include "blk.h"
Expand Down Expand Up @@ -419,13 +418,6 @@ void blk_mq_free_tags(struct blk_mq_tags *tags)
kfree(tags);
}

void blk_mq_tag_init_last_tag(struct blk_mq_tags *tags, unsigned int *tag)
{
unsigned int depth = tags->nr_tags - tags->nr_reserved_tags;

*tag = prandom_u32() % depth;
}

int blk_mq_tag_update_depth(struct blk_mq_tags *tags, unsigned int tdepth)
{
tdepth -= tags->nr_reserved_tags;
Expand Down
1 change: 0 additions & 1 deletion block/blk-mq-tag.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ extern void blk_mq_put_tag(struct blk_mq_hw_ctx *hctx, struct blk_mq_ctx *ctx,
unsigned int tag);
extern bool blk_mq_has_free_tags(struct blk_mq_tags *tags);
extern ssize_t blk_mq_tag_sysfs_show(struct blk_mq_tags *tags, char *page);
extern void blk_mq_tag_init_last_tag(struct blk_mq_tags *tags, unsigned int *last_tag);
extern int blk_mq_tag_update_depth(struct blk_mq_tags *tags, unsigned int depth);
extern void blk_mq_tag_wakeup_all(struct blk_mq_tags *tags, bool);
void blk_mq_queue_tag_busy_iter(struct request_queue *q, busy_iter_fn *fn,
Expand Down
6 changes: 6 additions & 0 deletions lib/sbitmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

#include <linux/random.h>
#include <linux/sbitmap.h>

int sbitmap_init_node(struct sbitmap *sb, unsigned int depth, int shift,
Expand Down Expand Up @@ -211,6 +212,11 @@ int sbitmap_queue_init_node(struct sbitmap_queue *sbq, unsigned int depth,
return -ENOMEM;
}

if (depth && !round_robin) {
for_each_possible_cpu(i)
*per_cpu_ptr(sbq->alloc_hint, i) = prandom_u32() % depth;
}

sbq->wake_batch = sbq_calc_wake_batch(depth);
atomic_set(&sbq->wake_index, 0);

Expand Down

0 comments on commit 98d9541

Please sign in to comment.