Jens Axboe | 75bb462 | 2014-05-28 10:15:41 -0600 | [diff] [blame] | 1 | /* |
| 2 | * CPU notifier helper code for blk-mq |
| 3 | * |
| 4 | * Copyright (C) 2013-2014 Jens Axboe |
| 5 | */ |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 6 | #include <linux/kernel.h> |
| 7 | #include <linux/module.h> |
| 8 | #include <linux/init.h> |
| 9 | #include <linux/blkdev.h> |
| 10 | #include <linux/list.h> |
| 11 | #include <linux/llist.h> |
| 12 | #include <linux/smp.h> |
| 13 | #include <linux/cpu.h> |
| 14 | |
| 15 | #include <linux/blk-mq.h> |
| 16 | #include "blk-mq.h" |
| 17 | |
| 18 | static LIST_HEAD(blk_mq_cpu_notify_list); |
Mike Galbraith | 2a26ebe | 2014-03-03 05:57:26 +0100 | [diff] [blame] | 19 | static DEFINE_RAW_SPINLOCK(blk_mq_cpu_notify_lock); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 20 | |
Paul Gortmaker | f618ef7 | 2013-11-14 08:26:02 -0700 | [diff] [blame] | 21 | static int blk_mq_main_cpu_notify(struct notifier_block *self, |
| 22 | unsigned long action, void *hcpu) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 23 | { |
| 24 | unsigned int cpu = (unsigned long) hcpu; |
| 25 | struct blk_mq_cpu_notifier *notify; |
Jens Axboe | e814e71 | 2014-05-21 13:59:08 -0600 | [diff] [blame] | 26 | int ret = NOTIFY_OK; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 27 | |
Mike Galbraith | 2a26ebe | 2014-03-03 05:57:26 +0100 | [diff] [blame] | 28 | raw_spin_lock(&blk_mq_cpu_notify_lock); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 29 | |
Jens Axboe | e814e71 | 2014-05-21 13:59:08 -0600 | [diff] [blame] | 30 | list_for_each_entry(notify, &blk_mq_cpu_notify_list, list) { |
| 31 | ret = notify->notify(notify->data, action, cpu); |
| 32 | if (ret != NOTIFY_OK) |
| 33 | break; |
| 34 | } |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 35 | |
Mike Galbraith | 2a26ebe | 2014-03-03 05:57:26 +0100 | [diff] [blame] | 36 | raw_spin_unlock(&blk_mq_cpu_notify_lock); |
Jens Axboe | e814e71 | 2014-05-21 13:59:08 -0600 | [diff] [blame] | 37 | return ret; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 38 | } |
| 39 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 40 | void blk_mq_register_cpu_notifier(struct blk_mq_cpu_notifier *notifier) |
| 41 | { |
| 42 | BUG_ON(!notifier->notify); |
| 43 | |
Mike Galbraith | 2a26ebe | 2014-03-03 05:57:26 +0100 | [diff] [blame] | 44 | raw_spin_lock(&blk_mq_cpu_notify_lock); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 45 | list_add_tail(¬ifier->list, &blk_mq_cpu_notify_list); |
Mike Galbraith | 2a26ebe | 2014-03-03 05:57:26 +0100 | [diff] [blame] | 46 | raw_spin_unlock(&blk_mq_cpu_notify_lock); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | void blk_mq_unregister_cpu_notifier(struct blk_mq_cpu_notifier *notifier) |
| 50 | { |
Mike Galbraith | 2a26ebe | 2014-03-03 05:57:26 +0100 | [diff] [blame] | 51 | raw_spin_lock(&blk_mq_cpu_notify_lock); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 52 | list_del(¬ifier->list); |
Mike Galbraith | 2a26ebe | 2014-03-03 05:57:26 +0100 | [diff] [blame] | 53 | raw_spin_unlock(&blk_mq_cpu_notify_lock); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | void blk_mq_init_cpu_notifier(struct blk_mq_cpu_notifier *notifier, |
Jens Axboe | e814e71 | 2014-05-21 13:59:08 -0600 | [diff] [blame] | 57 | int (*fn)(void *, unsigned long, unsigned int), |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 58 | void *data) |
| 59 | { |
| 60 | notifier->notify = fn; |
| 61 | notifier->data = data; |
| 62 | } |
| 63 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 64 | void __init blk_mq_cpu_init(void) |
| 65 | { |
Andrew Morton | 381d3ee | 2014-01-28 09:52:01 -0700 | [diff] [blame] | 66 | hotcpu_notifier(blk_mq_main_cpu_notify, 0); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 67 | } |