blk-mq-cpu.c 1.6 KB
Newer Older
1 2 3 4 5
/*
 * CPU notifier helper code for blk-mq
 *
 * Copyright (C) 2013-2014 Jens Axboe
 */
6 7 8 9 10 11 12 13 14 15 16 17 18
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/blkdev.h>
#include <linux/list.h>
#include <linux/llist.h>
#include <linux/smp.h>
#include <linux/cpu.h>

#include <linux/blk-mq.h>
#include "blk-mq.h"

static LIST_HEAD(blk_mq_cpu_notify_list);
19
static DEFINE_RAW_SPINLOCK(blk_mq_cpu_notify_lock);
20

21 22
static int blk_mq_main_cpu_notify(struct notifier_block *self,
				  unsigned long action, void *hcpu)
23 24 25
{
	unsigned int cpu = (unsigned long) hcpu;
	struct blk_mq_cpu_notifier *notify;
26
	int ret = NOTIFY_OK;
27

28
	raw_spin_lock(&blk_mq_cpu_notify_lock);
29

30 31 32 33 34
	list_for_each_entry(notify, &blk_mq_cpu_notify_list, list) {
		ret = notify->notify(notify->data, action, cpu);
		if (ret != NOTIFY_OK)
			break;
	}
35

36
	raw_spin_unlock(&blk_mq_cpu_notify_lock);
37
	return ret;
38 39 40 41 42 43
}

void blk_mq_register_cpu_notifier(struct blk_mq_cpu_notifier *notifier)
{
	BUG_ON(!notifier->notify);

44
	raw_spin_lock(&blk_mq_cpu_notify_lock);
45
	list_add_tail(&notifier->list, &blk_mq_cpu_notify_list);
46
	raw_spin_unlock(&blk_mq_cpu_notify_lock);
47 48 49 50
}

void blk_mq_unregister_cpu_notifier(struct blk_mq_cpu_notifier *notifier)
{
51
	raw_spin_lock(&blk_mq_cpu_notify_lock);
52
	list_del(&notifier->list);
53
	raw_spin_unlock(&blk_mq_cpu_notify_lock);
54 55 56
}

void blk_mq_init_cpu_notifier(struct blk_mq_cpu_notifier *notifier,
57
			      int (*fn)(void *, unsigned long, unsigned int),
58 59 60 61 62 63 64 65
			      void *data)
{
	notifier->notify = fn;
	notifier->data = data;
}

void __init blk_mq_cpu_init(void)
{
66
	hotcpu_notifier(blk_mq_main_cpu_notify, 0);
67
}