multipath.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2017-2018 Christoph Hellwig.
  4. */
  5. #include <linux/backing-dev.h>
  6. #include <linux/moduleparam.h>
  7. #include <linux/vmalloc.h>
  8. #include <trace/events/block.h>
  9. #include "nvme.h"
  10. bool multipath = true;
  11. module_param(multipath, bool, 0444);
  12. MODULE_PARM_DESC(multipath,
  13. "turn on native support for multiple controllers per subsystem");
  14. static const char *nvme_iopolicy_names[] = {
  15. [NVME_IOPOLICY_NUMA] = "numa",
  16. [NVME_IOPOLICY_RR] = "round-robin",
  17. [NVME_IOPOLICY_QD] = "queue-depth",
  18. };
  19. static int iopolicy = NVME_IOPOLICY_NUMA;
  20. static int nvme_set_iopolicy(const char *val, const struct kernel_param *kp)
  21. {
  22. if (!val)
  23. return -EINVAL;
  24. if (!strncmp(val, "numa", 4))
  25. iopolicy = NVME_IOPOLICY_NUMA;
  26. else if (!strncmp(val, "round-robin", 11))
  27. iopolicy = NVME_IOPOLICY_RR;
  28. else if (!strncmp(val, "queue-depth", 11))
  29. iopolicy = NVME_IOPOLICY_QD;
  30. else
  31. return -EINVAL;
  32. return 0;
  33. }
  34. static int nvme_get_iopolicy(char *buf, const struct kernel_param *kp)
  35. {
  36. return sprintf(buf, "%s\n", nvme_iopolicy_names[iopolicy]);
  37. }
  38. module_param_call(iopolicy, nvme_set_iopolicy, nvme_get_iopolicy,
  39. &iopolicy, 0644);
  40. MODULE_PARM_DESC(iopolicy,
  41. "Default multipath I/O policy; 'numa' (default), 'round-robin' or 'queue-depth'");
  42. void nvme_mpath_default_iopolicy(struct nvme_subsystem *subsys)
  43. {
  44. subsys->iopolicy = iopolicy;
  45. }
  46. void nvme_mpath_unfreeze(struct nvme_subsystem *subsys)
  47. {
  48. struct nvme_ns_head *h;
  49. lockdep_assert_held(&subsys->lock);
  50. list_for_each_entry(h, &subsys->nsheads, entry)
  51. if (h->disk)
  52. blk_mq_unfreeze_queue(h->disk->queue);
  53. }
  54. void nvme_mpath_wait_freeze(struct nvme_subsystem *subsys)
  55. {
  56. struct nvme_ns_head *h;
  57. lockdep_assert_held(&subsys->lock);
  58. list_for_each_entry(h, &subsys->nsheads, entry)
  59. if (h->disk)
  60. blk_mq_freeze_queue_wait(h->disk->queue);
  61. }
  62. void nvme_mpath_start_freeze(struct nvme_subsystem *subsys)
  63. {
  64. struct nvme_ns_head *h;
  65. lockdep_assert_held(&subsys->lock);
  66. list_for_each_entry(h, &subsys->nsheads, entry)
  67. if (h->disk)
  68. blk_freeze_queue_start(h->disk->queue);
  69. }
  70. void nvme_failover_req(struct request *req)
  71. {
  72. struct nvme_ns *ns = req->q->queuedata;
  73. u16 status = nvme_req(req)->status & NVME_SCT_SC_MASK;
  74. unsigned long flags;
  75. struct bio *bio;
  76. nvme_mpath_clear_current_path(ns);
  77. /*
  78. * If we got back an ANA error, we know the controller is alive but not
  79. * ready to serve this namespace. Kick of a re-read of the ANA
  80. * information page, and just try any other available path for now.
  81. */
  82. if (nvme_is_ana_error(status) && ns->ctrl->ana_log_buf) {
  83. set_bit(NVME_NS_ANA_PENDING, &ns->flags);
  84. queue_work(nvme_wq, &ns->ctrl->ana_work);
  85. }
  86. spin_lock_irqsave(&ns->head->requeue_lock, flags);
  87. for (bio = req->bio; bio; bio = bio->bi_next) {
  88. bio_set_dev(bio, ns->head->disk->part0);
  89. if (bio->bi_opf & REQ_POLLED) {
  90. bio->bi_opf &= ~REQ_POLLED;
  91. bio->bi_cookie = BLK_QC_T_NONE;
  92. }
  93. /*
  94. * The alternate request queue that we may end up submitting
  95. * the bio to may be frozen temporarily, in this case REQ_NOWAIT
  96. * will fail the I/O immediately with EAGAIN to the issuer.
  97. * We are not in the issuer context which cannot block. Clear
  98. * the flag to avoid spurious EAGAIN I/O failures.
  99. */
  100. bio->bi_opf &= ~REQ_NOWAIT;
  101. }
  102. blk_steal_bios(&ns->head->requeue_list, req);
  103. spin_unlock_irqrestore(&ns->head->requeue_lock, flags);
  104. nvme_req(req)->status = 0;
  105. nvme_end_req(req);
  106. kblockd_schedule_work(&ns->head->requeue_work);
  107. }
  108. void nvme_mpath_start_request(struct request *rq)
  109. {
  110. struct nvme_ns *ns = rq->q->queuedata;
  111. struct gendisk *disk = ns->head->disk;
  112. if ((READ_ONCE(ns->head->subsys->iopolicy) == NVME_IOPOLICY_QD) &&
  113. !(nvme_req(rq)->flags & NVME_MPATH_CNT_ACTIVE)) {
  114. atomic_inc(&ns->ctrl->nr_active);
  115. nvme_req(rq)->flags |= NVME_MPATH_CNT_ACTIVE;
  116. }
  117. if (!blk_queue_io_stat(disk->queue) || blk_rq_is_passthrough(rq) ||
  118. (nvme_req(rq)->flags & NVME_MPATH_IO_STATS))
  119. return;
  120. nvme_req(rq)->flags |= NVME_MPATH_IO_STATS;
  121. nvme_req(rq)->start_time = bdev_start_io_acct(disk->part0, req_op(rq),
  122. jiffies);
  123. }
  124. EXPORT_SYMBOL_GPL(nvme_mpath_start_request);
  125. void nvme_mpath_end_request(struct request *rq)
  126. {
  127. struct nvme_ns *ns = rq->q->queuedata;
  128. if (nvme_req(rq)->flags & NVME_MPATH_CNT_ACTIVE)
  129. atomic_dec_if_positive(&ns->ctrl->nr_active);
  130. if (!(nvme_req(rq)->flags & NVME_MPATH_IO_STATS))
  131. return;
  132. bdev_end_io_acct(ns->head->disk->part0, req_op(rq),
  133. blk_rq_bytes(rq) >> SECTOR_SHIFT,
  134. nvme_req(rq)->start_time);
  135. }
  136. void nvme_kick_requeue_lists(struct nvme_ctrl *ctrl)
  137. {
  138. struct nvme_ns *ns;
  139. int srcu_idx;
  140. srcu_idx = srcu_read_lock(&ctrl->srcu);
  141. list_for_each_entry_srcu(ns, &ctrl->namespaces, list,
  142. srcu_read_lock_held(&ctrl->srcu)) {
  143. if (!ns->head->disk)
  144. continue;
  145. kblockd_schedule_work(&ns->head->requeue_work);
  146. if (nvme_ctrl_state(ns->ctrl) == NVME_CTRL_LIVE)
  147. disk_uevent(ns->head->disk, KOBJ_CHANGE);
  148. }
  149. srcu_read_unlock(&ctrl->srcu, srcu_idx);
  150. }
  151. static const char *nvme_ana_state_names[] = {
  152. [0] = "invalid state",
  153. [NVME_ANA_OPTIMIZED] = "optimized",
  154. [NVME_ANA_NONOPTIMIZED] = "non-optimized",
  155. [NVME_ANA_INACCESSIBLE] = "inaccessible",
  156. [NVME_ANA_PERSISTENT_LOSS] = "persistent-loss",
  157. [NVME_ANA_CHANGE] = "change",
  158. };
  159. bool nvme_mpath_clear_current_path(struct nvme_ns *ns)
  160. {
  161. struct nvme_ns_head *head = ns->head;
  162. bool changed = false;
  163. int node;
  164. if (!head)
  165. goto out;
  166. for_each_node(node) {
  167. if (ns == rcu_access_pointer(head->current_path[node])) {
  168. rcu_assign_pointer(head->current_path[node], NULL);
  169. changed = true;
  170. }
  171. }
  172. out:
  173. return changed;
  174. }
  175. void nvme_mpath_clear_ctrl_paths(struct nvme_ctrl *ctrl)
  176. {
  177. struct nvme_ns *ns;
  178. int srcu_idx;
  179. srcu_idx = srcu_read_lock(&ctrl->srcu);
  180. list_for_each_entry_srcu(ns, &ctrl->namespaces, list,
  181. srcu_read_lock_held(&ctrl->srcu)) {
  182. nvme_mpath_clear_current_path(ns);
  183. kblockd_schedule_work(&ns->head->requeue_work);
  184. }
  185. srcu_read_unlock(&ctrl->srcu, srcu_idx);
  186. }
  187. void nvme_mpath_revalidate_paths(struct nvme_ns *ns)
  188. {
  189. struct nvme_ns_head *head = ns->head;
  190. sector_t capacity = get_capacity(head->disk);
  191. int node;
  192. int srcu_idx;
  193. srcu_idx = srcu_read_lock(&head->srcu);
  194. list_for_each_entry_srcu(ns, &head->list, siblings,
  195. srcu_read_lock_held(&head->srcu)) {
  196. if (capacity != get_capacity(ns->disk))
  197. clear_bit(NVME_NS_READY, &ns->flags);
  198. }
  199. srcu_read_unlock(&head->srcu, srcu_idx);
  200. for_each_node(node)
  201. rcu_assign_pointer(head->current_path[node], NULL);
  202. kblockd_schedule_work(&head->requeue_work);
  203. }
  204. static bool nvme_path_is_disabled(struct nvme_ns *ns)
  205. {
  206. enum nvme_ctrl_state state = nvme_ctrl_state(ns->ctrl);
  207. /*
  208. * We don't treat NVME_CTRL_DELETING as a disabled path as I/O should
  209. * still be able to complete assuming that the controller is connected.
  210. * Otherwise it will fail immediately and return to the requeue list.
  211. */
  212. if (state != NVME_CTRL_LIVE && state != NVME_CTRL_DELETING)
  213. return true;
  214. if (test_bit(NVME_NS_ANA_PENDING, &ns->flags) ||
  215. !test_bit(NVME_NS_READY, &ns->flags))
  216. return true;
  217. return false;
  218. }
  219. static struct nvme_ns *__nvme_find_path(struct nvme_ns_head *head, int node)
  220. {
  221. int found_distance = INT_MAX, fallback_distance = INT_MAX, distance;
  222. struct nvme_ns *found = NULL, *fallback = NULL, *ns;
  223. list_for_each_entry_srcu(ns, &head->list, siblings,
  224. srcu_read_lock_held(&head->srcu)) {
  225. if (nvme_path_is_disabled(ns))
  226. continue;
  227. if (ns->ctrl->numa_node != NUMA_NO_NODE &&
  228. READ_ONCE(head->subsys->iopolicy) == NVME_IOPOLICY_NUMA)
  229. distance = node_distance(node, ns->ctrl->numa_node);
  230. else
  231. distance = LOCAL_DISTANCE;
  232. switch (ns->ana_state) {
  233. case NVME_ANA_OPTIMIZED:
  234. if (distance < found_distance) {
  235. found_distance = distance;
  236. found = ns;
  237. }
  238. break;
  239. case NVME_ANA_NONOPTIMIZED:
  240. if (distance < fallback_distance) {
  241. fallback_distance = distance;
  242. fallback = ns;
  243. }
  244. break;
  245. default:
  246. break;
  247. }
  248. }
  249. if (!found)
  250. found = fallback;
  251. if (found)
  252. rcu_assign_pointer(head->current_path[node], found);
  253. return found;
  254. }
  255. static struct nvme_ns *nvme_next_ns(struct nvme_ns_head *head,
  256. struct nvme_ns *ns)
  257. {
  258. ns = list_next_or_null_rcu(&head->list, &ns->siblings, struct nvme_ns,
  259. siblings);
  260. if (ns)
  261. return ns;
  262. return list_first_or_null_rcu(&head->list, struct nvme_ns, siblings);
  263. }
  264. static struct nvme_ns *nvme_round_robin_path(struct nvme_ns_head *head)
  265. {
  266. struct nvme_ns *ns, *found = NULL;
  267. int node = numa_node_id();
  268. struct nvme_ns *old = srcu_dereference(head->current_path[node],
  269. &head->srcu);
  270. if (unlikely(!old))
  271. return __nvme_find_path(head, node);
  272. if (list_is_singular(&head->list)) {
  273. if (nvme_path_is_disabled(old))
  274. return NULL;
  275. return old;
  276. }
  277. for (ns = nvme_next_ns(head, old);
  278. ns && ns != old;
  279. ns = nvme_next_ns(head, ns)) {
  280. if (nvme_path_is_disabled(ns))
  281. continue;
  282. if (ns->ana_state == NVME_ANA_OPTIMIZED) {
  283. found = ns;
  284. goto out;
  285. }
  286. if (ns->ana_state == NVME_ANA_NONOPTIMIZED)
  287. found = ns;
  288. }
  289. /*
  290. * The loop above skips the current path for round-robin semantics.
  291. * Fall back to the current path if either:
  292. * - no other optimized path found and current is optimized,
  293. * - no other usable path found and current is usable.
  294. */
  295. if (!nvme_path_is_disabled(old) &&
  296. (old->ana_state == NVME_ANA_OPTIMIZED ||
  297. (!found && old->ana_state == NVME_ANA_NONOPTIMIZED)))
  298. return old;
  299. if (!found)
  300. return NULL;
  301. out:
  302. rcu_assign_pointer(head->current_path[node], found);
  303. return found;
  304. }
  305. static struct nvme_ns *nvme_queue_depth_path(struct nvme_ns_head *head)
  306. {
  307. struct nvme_ns *best_opt = NULL, *best_nonopt = NULL, *ns;
  308. unsigned int min_depth_opt = UINT_MAX, min_depth_nonopt = UINT_MAX;
  309. unsigned int depth;
  310. list_for_each_entry_srcu(ns, &head->list, siblings,
  311. srcu_read_lock_held(&head->srcu)) {
  312. if (nvme_path_is_disabled(ns))
  313. continue;
  314. depth = atomic_read(&ns->ctrl->nr_active);
  315. switch (ns->ana_state) {
  316. case NVME_ANA_OPTIMIZED:
  317. if (depth < min_depth_opt) {
  318. min_depth_opt = depth;
  319. best_opt = ns;
  320. }
  321. break;
  322. case NVME_ANA_NONOPTIMIZED:
  323. if (depth < min_depth_nonopt) {
  324. min_depth_nonopt = depth;
  325. best_nonopt = ns;
  326. }
  327. break;
  328. default:
  329. break;
  330. }
  331. if (min_depth_opt == 0)
  332. return best_opt;
  333. }
  334. return best_opt ? best_opt : best_nonopt;
  335. }
  336. static inline bool nvme_path_is_optimized(struct nvme_ns *ns)
  337. {
  338. return nvme_ctrl_state(ns->ctrl) == NVME_CTRL_LIVE &&
  339. ns->ana_state == NVME_ANA_OPTIMIZED;
  340. }
  341. static struct nvme_ns *nvme_numa_path(struct nvme_ns_head *head)
  342. {
  343. int node = numa_node_id();
  344. struct nvme_ns *ns;
  345. ns = srcu_dereference(head->current_path[node], &head->srcu);
  346. if (unlikely(!ns))
  347. return __nvme_find_path(head, node);
  348. if (unlikely(!nvme_path_is_optimized(ns)))
  349. return __nvme_find_path(head, node);
  350. return ns;
  351. }
  352. inline struct nvme_ns *nvme_find_path(struct nvme_ns_head *head)
  353. {
  354. switch (READ_ONCE(head->subsys->iopolicy)) {
  355. case NVME_IOPOLICY_QD:
  356. return nvme_queue_depth_path(head);
  357. case NVME_IOPOLICY_RR:
  358. return nvme_round_robin_path(head);
  359. default:
  360. return nvme_numa_path(head);
  361. }
  362. }
  363. static bool nvme_available_path(struct nvme_ns_head *head)
  364. {
  365. struct nvme_ns *ns;
  366. if (!test_bit(NVME_NSHEAD_DISK_LIVE, &head->flags))
  367. return false;
  368. list_for_each_entry_srcu(ns, &head->list, siblings,
  369. srcu_read_lock_held(&head->srcu)) {
  370. if (test_bit(NVME_CTRL_FAILFAST_EXPIRED, &ns->ctrl->flags))
  371. continue;
  372. switch (nvme_ctrl_state(ns->ctrl)) {
  373. case NVME_CTRL_LIVE:
  374. case NVME_CTRL_RESETTING:
  375. case NVME_CTRL_CONNECTING:
  376. return true;
  377. default:
  378. break;
  379. }
  380. }
  381. return false;
  382. }
  383. static void nvme_ns_head_submit_bio(struct bio *bio)
  384. {
  385. struct nvme_ns_head *head = bio->bi_bdev->bd_disk->private_data;
  386. struct device *dev = disk_to_dev(head->disk);
  387. struct nvme_ns *ns;
  388. int srcu_idx;
  389. /*
  390. * The namespace might be going away and the bio might be moved to a
  391. * different queue via blk_steal_bios(), so we need to use the bio_split
  392. * pool from the original queue to allocate the bvecs from.
  393. */
  394. bio = bio_split_to_limits(bio);
  395. if (!bio)
  396. return;
  397. srcu_idx = srcu_read_lock(&head->srcu);
  398. ns = nvme_find_path(head);
  399. if (likely(ns)) {
  400. bio_set_dev(bio, ns->disk->part0);
  401. bio->bi_opf |= REQ_NVME_MPATH;
  402. trace_block_bio_remap(bio, disk_devt(ns->head->disk),
  403. bio->bi_iter.bi_sector);
  404. submit_bio_noacct(bio);
  405. } else if (nvme_available_path(head)) {
  406. dev_warn_ratelimited(dev, "no usable path - requeuing I/O\n");
  407. spin_lock_irq(&head->requeue_lock);
  408. bio_list_add(&head->requeue_list, bio);
  409. spin_unlock_irq(&head->requeue_lock);
  410. } else {
  411. dev_warn_ratelimited(dev, "no available path - failing I/O\n");
  412. bio_io_error(bio);
  413. }
  414. srcu_read_unlock(&head->srcu, srcu_idx);
  415. }
  416. static int nvme_ns_head_open(struct gendisk *disk, blk_mode_t mode)
  417. {
  418. if (!nvme_tryget_ns_head(disk->private_data))
  419. return -ENXIO;
  420. return 0;
  421. }
  422. static void nvme_ns_head_release(struct gendisk *disk)
  423. {
  424. nvme_put_ns_head(disk->private_data);
  425. }
  426. static int nvme_ns_head_get_unique_id(struct gendisk *disk, u8 id[16],
  427. enum blk_unique_id type)
  428. {
  429. struct nvme_ns_head *head = disk->private_data;
  430. struct nvme_ns *ns;
  431. int srcu_idx, ret = -EWOULDBLOCK;
  432. srcu_idx = srcu_read_lock(&head->srcu);
  433. ns = nvme_find_path(head);
  434. if (ns)
  435. ret = nvme_ns_get_unique_id(ns, id, type);
  436. srcu_read_unlock(&head->srcu, srcu_idx);
  437. return ret;
  438. }
  439. #ifdef CONFIG_BLK_DEV_ZONED
  440. static int nvme_ns_head_report_zones(struct gendisk *disk, sector_t sector,
  441. unsigned int nr_zones, report_zones_cb cb, void *data)
  442. {
  443. struct nvme_ns_head *head = disk->private_data;
  444. struct nvme_ns *ns;
  445. int srcu_idx, ret = -EWOULDBLOCK;
  446. srcu_idx = srcu_read_lock(&head->srcu);
  447. ns = nvme_find_path(head);
  448. if (ns)
  449. ret = nvme_ns_report_zones(ns, sector, nr_zones, cb, data);
  450. srcu_read_unlock(&head->srcu, srcu_idx);
  451. return ret;
  452. }
  453. #else
  454. #define nvme_ns_head_report_zones NULL
  455. #endif /* CONFIG_BLK_DEV_ZONED */
  456. const struct block_device_operations nvme_ns_head_ops = {
  457. .owner = THIS_MODULE,
  458. .submit_bio = nvme_ns_head_submit_bio,
  459. .open = nvme_ns_head_open,
  460. .release = nvme_ns_head_release,
  461. .ioctl = nvme_ns_head_ioctl,
  462. .compat_ioctl = blkdev_compat_ptr_ioctl,
  463. .getgeo = nvme_getgeo,
  464. .get_unique_id = nvme_ns_head_get_unique_id,
  465. .report_zones = nvme_ns_head_report_zones,
  466. .pr_ops = &nvme_pr_ops,
  467. };
  468. static inline struct nvme_ns_head *cdev_to_ns_head(struct cdev *cdev)
  469. {
  470. return container_of(cdev, struct nvme_ns_head, cdev);
  471. }
  472. static int nvme_ns_head_chr_open(struct inode *inode, struct file *file)
  473. {
  474. if (!nvme_tryget_ns_head(cdev_to_ns_head(inode->i_cdev)))
  475. return -ENXIO;
  476. return 0;
  477. }
  478. static int nvme_ns_head_chr_release(struct inode *inode, struct file *file)
  479. {
  480. nvme_put_ns_head(cdev_to_ns_head(inode->i_cdev));
  481. return 0;
  482. }
  483. static const struct file_operations nvme_ns_head_chr_fops = {
  484. .owner = THIS_MODULE,
  485. .open = nvme_ns_head_chr_open,
  486. .release = nvme_ns_head_chr_release,
  487. .unlocked_ioctl = nvme_ns_head_chr_ioctl,
  488. .compat_ioctl = compat_ptr_ioctl,
  489. .uring_cmd = nvme_ns_head_chr_uring_cmd,
  490. .uring_cmd_iopoll = nvme_ns_chr_uring_cmd_iopoll,
  491. };
  492. static int nvme_add_ns_head_cdev(struct nvme_ns_head *head)
  493. {
  494. int ret;
  495. head->cdev_device.parent = &head->subsys->dev;
  496. ret = dev_set_name(&head->cdev_device, "ng%dn%d",
  497. head->subsys->instance, head->instance);
  498. if (ret)
  499. return ret;
  500. ret = nvme_cdev_add(&head->cdev, &head->cdev_device,
  501. &nvme_ns_head_chr_fops, THIS_MODULE);
  502. return ret;
  503. }
  504. static void nvme_partition_scan_work(struct work_struct *work)
  505. {
  506. struct nvme_ns_head *head =
  507. container_of(work, struct nvme_ns_head, partition_scan_work);
  508. if (WARN_ON_ONCE(!test_and_clear_bit(GD_SUPPRESS_PART_SCAN,
  509. &head->disk->state)))
  510. return;
  511. mutex_lock(&head->disk->open_mutex);
  512. bdev_disk_changed(head->disk, false);
  513. mutex_unlock(&head->disk->open_mutex);
  514. }
  515. static void nvme_requeue_work(struct work_struct *work)
  516. {
  517. struct nvme_ns_head *head =
  518. container_of(work, struct nvme_ns_head, requeue_work);
  519. struct bio *bio, *next;
  520. spin_lock_irq(&head->requeue_lock);
  521. next = bio_list_get(&head->requeue_list);
  522. spin_unlock_irq(&head->requeue_lock);
  523. while ((bio = next) != NULL) {
  524. next = bio->bi_next;
  525. bio->bi_next = NULL;
  526. submit_bio_noacct(bio);
  527. }
  528. }
  529. int nvme_mpath_alloc_disk(struct nvme_ctrl *ctrl, struct nvme_ns_head *head)
  530. {
  531. struct queue_limits lim;
  532. mutex_init(&head->lock);
  533. bio_list_init(&head->requeue_list);
  534. spin_lock_init(&head->requeue_lock);
  535. INIT_WORK(&head->requeue_work, nvme_requeue_work);
  536. INIT_WORK(&head->partition_scan_work, nvme_partition_scan_work);
  537. /*
  538. * Add a multipath node if the subsystems supports multiple controllers.
  539. * We also do this for private namespaces as the namespace sharing flag
  540. * could change after a rescan.
  541. */
  542. if (!(ctrl->subsys->cmic & NVME_CTRL_CMIC_MULTI_CTRL) ||
  543. !nvme_is_unique_nsid(ctrl, head) || !multipath)
  544. return 0;
  545. blk_set_stacking_limits(&lim);
  546. lim.dma_alignment = 3;
  547. lim.features |= BLK_FEAT_IO_STAT | BLK_FEAT_NOWAIT | BLK_FEAT_POLL;
  548. if (head->ids.csi == NVME_CSI_ZNS)
  549. lim.features |= BLK_FEAT_ZONED;
  550. else
  551. lim.max_zone_append_sectors = 0;
  552. head->disk = blk_alloc_disk(&lim, ctrl->numa_node);
  553. if (IS_ERR(head->disk))
  554. return PTR_ERR(head->disk);
  555. head->disk->fops = &nvme_ns_head_ops;
  556. head->disk->private_data = head;
  557. /*
  558. * We need to suppress the partition scan from occuring within the
  559. * controller's scan_work context. If a path error occurs here, the IO
  560. * will wait until a path becomes available or all paths are torn down,
  561. * but that action also occurs within scan_work, so it would deadlock.
  562. * Defer the partion scan to a different context that does not block
  563. * scan_work.
  564. */
  565. set_bit(GD_SUPPRESS_PART_SCAN, &head->disk->state);
  566. sprintf(head->disk->disk_name, "nvme%dn%d",
  567. ctrl->subsys->instance, head->instance);
  568. return 0;
  569. }
  570. static void nvme_mpath_set_live(struct nvme_ns *ns)
  571. {
  572. struct nvme_ns_head *head = ns->head;
  573. int rc;
  574. if (!head->disk)
  575. return;
  576. /*
  577. * test_and_set_bit() is used because it is protecting against two nvme
  578. * paths simultaneously calling device_add_disk() on the same namespace
  579. * head.
  580. */
  581. if (!test_and_set_bit(NVME_NSHEAD_DISK_LIVE, &head->flags)) {
  582. rc = device_add_disk(&head->subsys->dev, head->disk,
  583. nvme_ns_attr_groups);
  584. if (rc) {
  585. clear_bit(NVME_NSHEAD_DISK_LIVE, &head->flags);
  586. return;
  587. }
  588. nvme_add_ns_head_cdev(head);
  589. kblockd_schedule_work(&head->partition_scan_work);
  590. }
  591. mutex_lock(&head->lock);
  592. if (nvme_path_is_optimized(ns)) {
  593. int node, srcu_idx;
  594. srcu_idx = srcu_read_lock(&head->srcu);
  595. for_each_online_node(node)
  596. __nvme_find_path(head, node);
  597. srcu_read_unlock(&head->srcu, srcu_idx);
  598. }
  599. mutex_unlock(&head->lock);
  600. synchronize_srcu(&head->srcu);
  601. kblockd_schedule_work(&head->requeue_work);
  602. }
  603. static int nvme_parse_ana_log(struct nvme_ctrl *ctrl, void *data,
  604. int (*cb)(struct nvme_ctrl *ctrl, struct nvme_ana_group_desc *,
  605. void *))
  606. {
  607. void *base = ctrl->ana_log_buf;
  608. size_t offset = sizeof(struct nvme_ana_rsp_hdr);
  609. int error, i;
  610. lockdep_assert_held(&ctrl->ana_lock);
  611. for (i = 0; i < le16_to_cpu(ctrl->ana_log_buf->ngrps); i++) {
  612. struct nvme_ana_group_desc *desc = base + offset;
  613. u32 nr_nsids;
  614. size_t nsid_buf_size;
  615. if (WARN_ON_ONCE(offset > ctrl->ana_log_size - sizeof(*desc)))
  616. return -EINVAL;
  617. nr_nsids = le32_to_cpu(desc->nnsids);
  618. nsid_buf_size = flex_array_size(desc, nsids, nr_nsids);
  619. if (WARN_ON_ONCE(desc->grpid == 0))
  620. return -EINVAL;
  621. if (WARN_ON_ONCE(le32_to_cpu(desc->grpid) > ctrl->anagrpmax))
  622. return -EINVAL;
  623. if (WARN_ON_ONCE(desc->state == 0))
  624. return -EINVAL;
  625. if (WARN_ON_ONCE(desc->state > NVME_ANA_CHANGE))
  626. return -EINVAL;
  627. offset += sizeof(*desc);
  628. if (WARN_ON_ONCE(offset > ctrl->ana_log_size - nsid_buf_size))
  629. return -EINVAL;
  630. error = cb(ctrl, desc, data);
  631. if (error)
  632. return error;
  633. offset += nsid_buf_size;
  634. }
  635. return 0;
  636. }
  637. static inline bool nvme_state_is_live(enum nvme_ana_state state)
  638. {
  639. return state == NVME_ANA_OPTIMIZED || state == NVME_ANA_NONOPTIMIZED;
  640. }
  641. static void nvme_update_ns_ana_state(struct nvme_ana_group_desc *desc,
  642. struct nvme_ns *ns)
  643. {
  644. ns->ana_grpid = le32_to_cpu(desc->grpid);
  645. ns->ana_state = desc->state;
  646. clear_bit(NVME_NS_ANA_PENDING, &ns->flags);
  647. /*
  648. * nvme_mpath_set_live() will trigger I/O to the multipath path device
  649. * and in turn to this path device. However we cannot accept this I/O
  650. * if the controller is not live. This may deadlock if called from
  651. * nvme_mpath_init_identify() and the ctrl will never complete
  652. * initialization, preventing I/O from completing. For this case we
  653. * will reprocess the ANA log page in nvme_mpath_update() once the
  654. * controller is ready.
  655. */
  656. if (nvme_state_is_live(ns->ana_state) &&
  657. nvme_ctrl_state(ns->ctrl) == NVME_CTRL_LIVE)
  658. nvme_mpath_set_live(ns);
  659. }
  660. static int nvme_update_ana_state(struct nvme_ctrl *ctrl,
  661. struct nvme_ana_group_desc *desc, void *data)
  662. {
  663. u32 nr_nsids = le32_to_cpu(desc->nnsids), n = 0;
  664. unsigned *nr_change_groups = data;
  665. struct nvme_ns *ns;
  666. int srcu_idx;
  667. dev_dbg(ctrl->device, "ANA group %d: %s.\n",
  668. le32_to_cpu(desc->grpid),
  669. nvme_ana_state_names[desc->state]);
  670. if (desc->state == NVME_ANA_CHANGE)
  671. (*nr_change_groups)++;
  672. if (!nr_nsids)
  673. return 0;
  674. srcu_idx = srcu_read_lock(&ctrl->srcu);
  675. list_for_each_entry_srcu(ns, &ctrl->namespaces, list,
  676. srcu_read_lock_held(&ctrl->srcu)) {
  677. unsigned nsid;
  678. again:
  679. nsid = le32_to_cpu(desc->nsids[n]);
  680. if (ns->head->ns_id < nsid)
  681. continue;
  682. if (ns->head->ns_id == nsid)
  683. nvme_update_ns_ana_state(desc, ns);
  684. if (++n == nr_nsids)
  685. break;
  686. if (ns->head->ns_id > nsid)
  687. goto again;
  688. }
  689. srcu_read_unlock(&ctrl->srcu, srcu_idx);
  690. return 0;
  691. }
  692. static int nvme_read_ana_log(struct nvme_ctrl *ctrl)
  693. {
  694. u32 nr_change_groups = 0;
  695. int error;
  696. mutex_lock(&ctrl->ana_lock);
  697. error = nvme_get_log(ctrl, NVME_NSID_ALL, NVME_LOG_ANA, 0, NVME_CSI_NVM,
  698. ctrl->ana_log_buf, ctrl->ana_log_size, 0);
  699. if (error) {
  700. dev_warn(ctrl->device, "Failed to get ANA log: %d\n", error);
  701. goto out_unlock;
  702. }
  703. error = nvme_parse_ana_log(ctrl, &nr_change_groups,
  704. nvme_update_ana_state);
  705. if (error)
  706. goto out_unlock;
  707. /*
  708. * In theory we should have an ANATT timer per group as they might enter
  709. * the change state at different times. But that is a lot of overhead
  710. * just to protect against a target that keeps entering new changes
  711. * states while never finishing previous ones. But we'll still
  712. * eventually time out once all groups are in change state, so this
  713. * isn't a big deal.
  714. *
  715. * We also double the ANATT value to provide some slack for transports
  716. * or AEN processing overhead.
  717. */
  718. if (nr_change_groups)
  719. mod_timer(&ctrl->anatt_timer, ctrl->anatt * HZ * 2 + jiffies);
  720. else
  721. del_timer_sync(&ctrl->anatt_timer);
  722. out_unlock:
  723. mutex_unlock(&ctrl->ana_lock);
  724. return error;
  725. }
  726. static void nvme_ana_work(struct work_struct *work)
  727. {
  728. struct nvme_ctrl *ctrl = container_of(work, struct nvme_ctrl, ana_work);
  729. if (nvme_ctrl_state(ctrl) != NVME_CTRL_LIVE)
  730. return;
  731. nvme_read_ana_log(ctrl);
  732. }
  733. void nvme_mpath_update(struct nvme_ctrl *ctrl)
  734. {
  735. u32 nr_change_groups = 0;
  736. if (!ctrl->ana_log_buf)
  737. return;
  738. mutex_lock(&ctrl->ana_lock);
  739. nvme_parse_ana_log(ctrl, &nr_change_groups, nvme_update_ana_state);
  740. mutex_unlock(&ctrl->ana_lock);
  741. }
  742. static void nvme_anatt_timeout(struct timer_list *t)
  743. {
  744. struct nvme_ctrl *ctrl = from_timer(ctrl, t, anatt_timer);
  745. dev_info(ctrl->device, "ANATT timeout, resetting controller.\n");
  746. nvme_reset_ctrl(ctrl);
  747. }
  748. void nvme_mpath_stop(struct nvme_ctrl *ctrl)
  749. {
  750. if (!nvme_ctrl_use_ana(ctrl))
  751. return;
  752. del_timer_sync(&ctrl->anatt_timer);
  753. cancel_work_sync(&ctrl->ana_work);
  754. }
  755. #define SUBSYS_ATTR_RW(_name, _mode, _show, _store) \
  756. struct device_attribute subsys_attr_##_name = \
  757. __ATTR(_name, _mode, _show, _store)
  758. static ssize_t nvme_subsys_iopolicy_show(struct device *dev,
  759. struct device_attribute *attr, char *buf)
  760. {
  761. struct nvme_subsystem *subsys =
  762. container_of(dev, struct nvme_subsystem, dev);
  763. return sysfs_emit(buf, "%s\n",
  764. nvme_iopolicy_names[READ_ONCE(subsys->iopolicy)]);
  765. }
  766. static void nvme_subsys_iopolicy_update(struct nvme_subsystem *subsys,
  767. int iopolicy)
  768. {
  769. struct nvme_ctrl *ctrl;
  770. int old_iopolicy = READ_ONCE(subsys->iopolicy);
  771. if (old_iopolicy == iopolicy)
  772. return;
  773. WRITE_ONCE(subsys->iopolicy, iopolicy);
  774. /* iopolicy changes clear the mpath by design */
  775. mutex_lock(&nvme_subsystems_lock);
  776. list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry)
  777. nvme_mpath_clear_ctrl_paths(ctrl);
  778. mutex_unlock(&nvme_subsystems_lock);
  779. pr_notice("subsysnqn %s iopolicy changed from %s to %s\n",
  780. subsys->subnqn,
  781. nvme_iopolicy_names[old_iopolicy],
  782. nvme_iopolicy_names[iopolicy]);
  783. }
  784. static ssize_t nvme_subsys_iopolicy_store(struct device *dev,
  785. struct device_attribute *attr, const char *buf, size_t count)
  786. {
  787. struct nvme_subsystem *subsys =
  788. container_of(dev, struct nvme_subsystem, dev);
  789. int i;
  790. for (i = 0; i < ARRAY_SIZE(nvme_iopolicy_names); i++) {
  791. if (sysfs_streq(buf, nvme_iopolicy_names[i])) {
  792. nvme_subsys_iopolicy_update(subsys, i);
  793. return count;
  794. }
  795. }
  796. return -EINVAL;
  797. }
  798. SUBSYS_ATTR_RW(iopolicy, S_IRUGO | S_IWUSR,
  799. nvme_subsys_iopolicy_show, nvme_subsys_iopolicy_store);
  800. static ssize_t ana_grpid_show(struct device *dev, struct device_attribute *attr,
  801. char *buf)
  802. {
  803. return sysfs_emit(buf, "%d\n", nvme_get_ns_from_dev(dev)->ana_grpid);
  804. }
  805. DEVICE_ATTR_RO(ana_grpid);
  806. static ssize_t ana_state_show(struct device *dev, struct device_attribute *attr,
  807. char *buf)
  808. {
  809. struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
  810. return sysfs_emit(buf, "%s\n", nvme_ana_state_names[ns->ana_state]);
  811. }
  812. DEVICE_ATTR_RO(ana_state);
  813. static int nvme_lookup_ana_group_desc(struct nvme_ctrl *ctrl,
  814. struct nvme_ana_group_desc *desc, void *data)
  815. {
  816. struct nvme_ana_group_desc *dst = data;
  817. if (desc->grpid != dst->grpid)
  818. return 0;
  819. *dst = *desc;
  820. return -ENXIO; /* just break out of the loop */
  821. }
  822. void nvme_mpath_add_disk(struct nvme_ns *ns, __le32 anagrpid)
  823. {
  824. if (nvme_ctrl_use_ana(ns->ctrl)) {
  825. struct nvme_ana_group_desc desc = {
  826. .grpid = anagrpid,
  827. .state = 0,
  828. };
  829. mutex_lock(&ns->ctrl->ana_lock);
  830. ns->ana_grpid = le32_to_cpu(anagrpid);
  831. nvme_parse_ana_log(ns->ctrl, &desc, nvme_lookup_ana_group_desc);
  832. mutex_unlock(&ns->ctrl->ana_lock);
  833. if (desc.state) {
  834. /* found the group desc: update */
  835. nvme_update_ns_ana_state(&desc, ns);
  836. } else {
  837. /* group desc not found: trigger a re-read */
  838. set_bit(NVME_NS_ANA_PENDING, &ns->flags);
  839. queue_work(nvme_wq, &ns->ctrl->ana_work);
  840. }
  841. } else {
  842. ns->ana_state = NVME_ANA_OPTIMIZED;
  843. nvme_mpath_set_live(ns);
  844. }
  845. #ifdef CONFIG_BLK_DEV_ZONED
  846. if (blk_queue_is_zoned(ns->queue) && ns->head->disk)
  847. ns->head->disk->nr_zones = ns->disk->nr_zones;
  848. #endif
  849. }
  850. void nvme_mpath_shutdown_disk(struct nvme_ns_head *head)
  851. {
  852. if (!head->disk)
  853. return;
  854. if (test_and_clear_bit(NVME_NSHEAD_DISK_LIVE, &head->flags)) {
  855. nvme_cdev_del(&head->cdev, &head->cdev_device);
  856. /*
  857. * requeue I/O after NVME_NSHEAD_DISK_LIVE has been cleared
  858. * to allow multipath to fail all I/O.
  859. */
  860. synchronize_srcu(&head->srcu);
  861. kblockd_schedule_work(&head->requeue_work);
  862. del_gendisk(head->disk);
  863. }
  864. }
  865. void nvme_mpath_remove_disk(struct nvme_ns_head *head)
  866. {
  867. if (!head->disk)
  868. return;
  869. /* make sure all pending bios are cleaned up */
  870. kblockd_schedule_work(&head->requeue_work);
  871. flush_work(&head->requeue_work);
  872. flush_work(&head->partition_scan_work);
  873. put_disk(head->disk);
  874. }
  875. void nvme_mpath_init_ctrl(struct nvme_ctrl *ctrl)
  876. {
  877. mutex_init(&ctrl->ana_lock);
  878. timer_setup(&ctrl->anatt_timer, nvme_anatt_timeout, 0);
  879. INIT_WORK(&ctrl->ana_work, nvme_ana_work);
  880. }
  881. int nvme_mpath_init_identify(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id)
  882. {
  883. size_t max_transfer_size = ctrl->max_hw_sectors << SECTOR_SHIFT;
  884. size_t ana_log_size;
  885. int error = 0;
  886. /* check if multipath is enabled and we have the capability */
  887. if (!multipath || !ctrl->subsys ||
  888. !(ctrl->subsys->cmic & NVME_CTRL_CMIC_ANA))
  889. return 0;
  890. /* initialize this in the identify path to cover controller resets */
  891. atomic_set(&ctrl->nr_active, 0);
  892. if (!ctrl->max_namespaces ||
  893. ctrl->max_namespaces > le32_to_cpu(id->nn)) {
  894. dev_err(ctrl->device,
  895. "Invalid MNAN value %u\n", ctrl->max_namespaces);
  896. return -EINVAL;
  897. }
  898. ctrl->anacap = id->anacap;
  899. ctrl->anatt = id->anatt;
  900. ctrl->nanagrpid = le32_to_cpu(id->nanagrpid);
  901. ctrl->anagrpmax = le32_to_cpu(id->anagrpmax);
  902. ana_log_size = sizeof(struct nvme_ana_rsp_hdr) +
  903. ctrl->nanagrpid * sizeof(struct nvme_ana_group_desc) +
  904. ctrl->max_namespaces * sizeof(__le32);
  905. if (ana_log_size > max_transfer_size) {
  906. dev_err(ctrl->device,
  907. "ANA log page size (%zd) larger than MDTS (%zd).\n",
  908. ana_log_size, max_transfer_size);
  909. dev_err(ctrl->device, "disabling ANA support.\n");
  910. goto out_uninit;
  911. }
  912. if (ana_log_size > ctrl->ana_log_size) {
  913. nvme_mpath_stop(ctrl);
  914. nvme_mpath_uninit(ctrl);
  915. ctrl->ana_log_buf = kvmalloc(ana_log_size, GFP_KERNEL);
  916. if (!ctrl->ana_log_buf)
  917. return -ENOMEM;
  918. }
  919. ctrl->ana_log_size = ana_log_size;
  920. error = nvme_read_ana_log(ctrl);
  921. if (error)
  922. goto out_uninit;
  923. return 0;
  924. out_uninit:
  925. nvme_mpath_uninit(ctrl);
  926. return error;
  927. }
  928. void nvme_mpath_uninit(struct nvme_ctrl *ctrl)
  929. {
  930. kvfree(ctrl->ana_log_buf);
  931. ctrl->ana_log_buf = NULL;
  932. ctrl->ana_log_size = 0;
  933. }