null_blk_zoned.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/vmalloc.h>
  3. #include <linux/bitmap.h>
  4. #include "null_blk.h"
  5. #define CREATE_TRACE_POINTS
  6. #include "null_blk_trace.h"
  7. #define MB_TO_SECTS(mb) (((sector_t)mb * SZ_1M) >> SECTOR_SHIFT)
  8. static inline unsigned int null_zone_no(struct nullb_device *dev, sector_t sect)
  9. {
  10. return sect >> ilog2(dev->zone_size_sects);
  11. }
  12. int null_init_zoned_dev(struct nullb_device *dev, struct request_queue *q)
  13. {
  14. sector_t dev_capacity_sects, zone_capacity_sects;
  15. sector_t sector = 0;
  16. unsigned int i;
  17. if (!is_power_of_2(dev->zone_size)) {
  18. pr_err("zone_size must be power-of-two\n");
  19. return -EINVAL;
  20. }
  21. if (dev->zone_size > dev->size) {
  22. pr_err("Zone size larger than device capacity\n");
  23. return -EINVAL;
  24. }
  25. if (!dev->zone_capacity)
  26. dev->zone_capacity = dev->zone_size;
  27. if (dev->zone_capacity > dev->zone_size) {
  28. pr_err("null_blk: zone capacity (%lu MB) larger than zone size (%lu MB)\n",
  29. dev->zone_capacity, dev->zone_size);
  30. return -EINVAL;
  31. }
  32. zone_capacity_sects = MB_TO_SECTS(dev->zone_capacity);
  33. dev_capacity_sects = MB_TO_SECTS(dev->size);
  34. dev->zone_size_sects = MB_TO_SECTS(dev->zone_size);
  35. dev->nr_zones = dev_capacity_sects >> ilog2(dev->zone_size_sects);
  36. if (dev_capacity_sects & (dev->zone_size_sects - 1))
  37. dev->nr_zones++;
  38. dev->zones = kvmalloc_array(dev->nr_zones, sizeof(struct blk_zone),
  39. GFP_KERNEL | __GFP_ZERO);
  40. if (!dev->zones)
  41. return -ENOMEM;
  42. /*
  43. * With memory backing, the zone_lock spinlock needs to be temporarily
  44. * released to avoid scheduling in atomic context. To guarantee zone
  45. * information protection, use a bitmap to lock zones with
  46. * wait_on_bit_lock_io(). Sleeping on the lock is OK as memory backing
  47. * implies that the queue is marked with BLK_MQ_F_BLOCKING.
  48. */
  49. spin_lock_init(&dev->zone_lock);
  50. if (dev->memory_backed) {
  51. dev->zone_locks = bitmap_zalloc(dev->nr_zones, GFP_KERNEL);
  52. if (!dev->zone_locks) {
  53. kvfree(dev->zones);
  54. return -ENOMEM;
  55. }
  56. }
  57. if (dev->zone_nr_conv >= dev->nr_zones) {
  58. dev->zone_nr_conv = dev->nr_zones - 1;
  59. pr_info("changed the number of conventional zones to %u",
  60. dev->zone_nr_conv);
  61. }
  62. /* Max active zones has to be < nbr of seq zones in order to be enforceable */
  63. if (dev->zone_max_active >= dev->nr_zones - dev->zone_nr_conv) {
  64. dev->zone_max_active = 0;
  65. pr_info("zone_max_active limit disabled, limit >= zone count\n");
  66. }
  67. /* Max open zones has to be <= max active zones */
  68. if (dev->zone_max_active && dev->zone_max_open > dev->zone_max_active) {
  69. dev->zone_max_open = dev->zone_max_active;
  70. pr_info("changed the maximum number of open zones to %u\n",
  71. dev->nr_zones);
  72. } else if (dev->zone_max_open >= dev->nr_zones - dev->zone_nr_conv) {
  73. dev->zone_max_open = 0;
  74. pr_info("zone_max_open limit disabled, limit >= zone count\n");
  75. }
  76. for (i = 0; i < dev->zone_nr_conv; i++) {
  77. struct blk_zone *zone = &dev->zones[i];
  78. zone->start = sector;
  79. zone->len = dev->zone_size_sects;
  80. zone->capacity = zone->len;
  81. zone->wp = zone->start + zone->len;
  82. zone->type = BLK_ZONE_TYPE_CONVENTIONAL;
  83. zone->cond = BLK_ZONE_COND_NOT_WP;
  84. sector += dev->zone_size_sects;
  85. }
  86. for (i = dev->zone_nr_conv; i < dev->nr_zones; i++) {
  87. struct blk_zone *zone = &dev->zones[i];
  88. zone->start = zone->wp = sector;
  89. if (zone->start + dev->zone_size_sects > dev_capacity_sects)
  90. zone->len = dev_capacity_sects - zone->start;
  91. else
  92. zone->len = dev->zone_size_sects;
  93. zone->capacity =
  94. min_t(sector_t, zone->len, zone_capacity_sects);
  95. zone->type = BLK_ZONE_TYPE_SEQWRITE_REQ;
  96. zone->cond = BLK_ZONE_COND_EMPTY;
  97. sector += dev->zone_size_sects;
  98. }
  99. q->limits.zoned = BLK_ZONED_HM;
  100. blk_queue_flag_set(QUEUE_FLAG_ZONE_RESETALL, q);
  101. blk_queue_required_elevator_features(q, ELEVATOR_F_ZBD_SEQ_WRITE);
  102. return 0;
  103. }
  104. int null_register_zoned_dev(struct nullb *nullb)
  105. {
  106. struct nullb_device *dev = nullb->dev;
  107. struct request_queue *q = nullb->q;
  108. if (queue_is_mq(q)) {
  109. int ret = blk_revalidate_disk_zones(nullb->disk, NULL);
  110. if (ret)
  111. return ret;
  112. } else {
  113. blk_queue_chunk_sectors(q, dev->zone_size_sects);
  114. q->nr_zones = blkdev_nr_zones(nullb->disk);
  115. }
  116. blk_queue_max_zone_append_sectors(q, dev->zone_size_sects);
  117. blk_queue_max_open_zones(q, dev->zone_max_open);
  118. blk_queue_max_active_zones(q, dev->zone_max_active);
  119. return 0;
  120. }
  121. void null_free_zoned_dev(struct nullb_device *dev)
  122. {
  123. bitmap_free(dev->zone_locks);
  124. kvfree(dev->zones);
  125. dev->zones = NULL;
  126. }
  127. static inline void null_lock_zone(struct nullb_device *dev, unsigned int zno)
  128. {
  129. if (dev->memory_backed)
  130. wait_on_bit_lock_io(dev->zone_locks, zno, TASK_UNINTERRUPTIBLE);
  131. spin_lock_irq(&dev->zone_lock);
  132. }
  133. static inline void null_unlock_zone(struct nullb_device *dev, unsigned int zno)
  134. {
  135. spin_unlock_irq(&dev->zone_lock);
  136. if (dev->memory_backed)
  137. clear_and_wake_up_bit(zno, dev->zone_locks);
  138. }
  139. int null_report_zones(struct gendisk *disk, sector_t sector,
  140. unsigned int nr_zones, report_zones_cb cb, void *data)
  141. {
  142. struct nullb *nullb = disk->private_data;
  143. struct nullb_device *dev = nullb->dev;
  144. unsigned int first_zone, i, zno;
  145. struct blk_zone zone;
  146. int error;
  147. first_zone = null_zone_no(dev, sector);
  148. if (first_zone >= dev->nr_zones)
  149. return 0;
  150. nr_zones = min(nr_zones, dev->nr_zones - first_zone);
  151. trace_nullb_report_zones(nullb, nr_zones);
  152. zno = first_zone;
  153. for (i = 0; i < nr_zones; i++, zno++) {
  154. /*
  155. * Stacked DM target drivers will remap the zone information by
  156. * modifying the zone information passed to the report callback.
  157. * So use a local copy to avoid corruption of the device zone
  158. * array.
  159. */
  160. null_lock_zone(dev, zno);
  161. memcpy(&zone, &dev->zones[zno], sizeof(struct blk_zone));
  162. null_unlock_zone(dev, zno);
  163. error = cb(&zone, i, data);
  164. if (error)
  165. return error;
  166. }
  167. return nr_zones;
  168. }
  169. /*
  170. * This is called in the case of memory backing from null_process_cmd()
  171. * with the target zone already locked.
  172. */
  173. size_t null_zone_valid_read_len(struct nullb *nullb,
  174. sector_t sector, unsigned int len)
  175. {
  176. struct nullb_device *dev = nullb->dev;
  177. struct blk_zone *zone = &dev->zones[null_zone_no(dev, sector)];
  178. unsigned int nr_sectors = len >> SECTOR_SHIFT;
  179. /* Read must be below the write pointer position */
  180. if (zone->type == BLK_ZONE_TYPE_CONVENTIONAL ||
  181. sector + nr_sectors <= zone->wp)
  182. return len;
  183. if (sector > zone->wp)
  184. return 0;
  185. return (zone->wp - sector) << SECTOR_SHIFT;
  186. }
  187. static blk_status_t null_close_zone(struct nullb_device *dev, struct blk_zone *zone)
  188. {
  189. if (zone->type == BLK_ZONE_TYPE_CONVENTIONAL)
  190. return BLK_STS_IOERR;
  191. switch (zone->cond) {
  192. case BLK_ZONE_COND_CLOSED:
  193. /* close operation on closed is not an error */
  194. return BLK_STS_OK;
  195. case BLK_ZONE_COND_IMP_OPEN:
  196. dev->nr_zones_imp_open--;
  197. break;
  198. case BLK_ZONE_COND_EXP_OPEN:
  199. dev->nr_zones_exp_open--;
  200. break;
  201. case BLK_ZONE_COND_EMPTY:
  202. case BLK_ZONE_COND_FULL:
  203. default:
  204. return BLK_STS_IOERR;
  205. }
  206. if (zone->wp == zone->start) {
  207. zone->cond = BLK_ZONE_COND_EMPTY;
  208. } else {
  209. zone->cond = BLK_ZONE_COND_CLOSED;
  210. dev->nr_zones_closed++;
  211. }
  212. return BLK_STS_OK;
  213. }
  214. static void null_close_first_imp_zone(struct nullb_device *dev)
  215. {
  216. unsigned int i;
  217. for (i = dev->zone_nr_conv; i < dev->nr_zones; i++) {
  218. if (dev->zones[i].cond == BLK_ZONE_COND_IMP_OPEN) {
  219. null_close_zone(dev, &dev->zones[i]);
  220. return;
  221. }
  222. }
  223. }
  224. static blk_status_t null_check_active(struct nullb_device *dev)
  225. {
  226. if (!dev->zone_max_active)
  227. return BLK_STS_OK;
  228. if (dev->nr_zones_exp_open + dev->nr_zones_imp_open +
  229. dev->nr_zones_closed < dev->zone_max_active)
  230. return BLK_STS_OK;
  231. return BLK_STS_ZONE_ACTIVE_RESOURCE;
  232. }
  233. static blk_status_t null_check_open(struct nullb_device *dev)
  234. {
  235. if (!dev->zone_max_open)
  236. return BLK_STS_OK;
  237. if (dev->nr_zones_exp_open + dev->nr_zones_imp_open < dev->zone_max_open)
  238. return BLK_STS_OK;
  239. if (dev->nr_zones_imp_open) {
  240. if (null_check_active(dev) == BLK_STS_OK) {
  241. null_close_first_imp_zone(dev);
  242. return BLK_STS_OK;
  243. }
  244. }
  245. return BLK_STS_ZONE_OPEN_RESOURCE;
  246. }
  247. /*
  248. * This function matches the manage open zone resources function in the ZBC standard,
  249. * with the addition of max active zones support (added in the ZNS standard).
  250. *
  251. * The function determines if a zone can transition to implicit open or explicit open,
  252. * while maintaining the max open zone (and max active zone) limit(s). It may close an
  253. * implicit open zone in order to make additional zone resources available.
  254. *
  255. * ZBC states that an implicit open zone shall be closed only if there is not
  256. * room within the open limit. However, with the addition of an active limit,
  257. * it is not certain that closing an implicit open zone will allow a new zone
  258. * to be opened, since we might already be at the active limit capacity.
  259. */
  260. static blk_status_t null_check_zone_resources(struct nullb_device *dev, struct blk_zone *zone)
  261. {
  262. blk_status_t ret;
  263. switch (zone->cond) {
  264. case BLK_ZONE_COND_EMPTY:
  265. ret = null_check_active(dev);
  266. if (ret != BLK_STS_OK)
  267. return ret;
  268. fallthrough;
  269. case BLK_ZONE_COND_CLOSED:
  270. return null_check_open(dev);
  271. default:
  272. /* Should never be called for other states */
  273. WARN_ON(1);
  274. return BLK_STS_IOERR;
  275. }
  276. }
  277. static blk_status_t null_zone_write(struct nullb_cmd *cmd, sector_t sector,
  278. unsigned int nr_sectors, bool append)
  279. {
  280. struct nullb_device *dev = cmd->nq->dev;
  281. unsigned int zno = null_zone_no(dev, sector);
  282. struct blk_zone *zone = &dev->zones[zno];
  283. blk_status_t ret;
  284. trace_nullb_zone_op(cmd, zno, zone->cond);
  285. if (zone->type == BLK_ZONE_TYPE_CONVENTIONAL) {
  286. if (append)
  287. return BLK_STS_IOERR;
  288. return null_process_cmd(cmd, REQ_OP_WRITE, sector, nr_sectors);
  289. }
  290. null_lock_zone(dev, zno);
  291. switch (zone->cond) {
  292. case BLK_ZONE_COND_FULL:
  293. /* Cannot write to a full zone */
  294. ret = BLK_STS_IOERR;
  295. goto unlock;
  296. case BLK_ZONE_COND_EMPTY:
  297. case BLK_ZONE_COND_CLOSED:
  298. ret = null_check_zone_resources(dev, zone);
  299. if (ret != BLK_STS_OK)
  300. goto unlock;
  301. break;
  302. case BLK_ZONE_COND_IMP_OPEN:
  303. case BLK_ZONE_COND_EXP_OPEN:
  304. break;
  305. default:
  306. /* Invalid zone condition */
  307. ret = BLK_STS_IOERR;
  308. goto unlock;
  309. }
  310. /*
  311. * Regular writes must be at the write pointer position.
  312. * Zone append writes are automatically issued at the write
  313. * pointer and the position returned using the request or BIO
  314. * sector.
  315. */
  316. if (append) {
  317. sector = zone->wp;
  318. if (cmd->bio)
  319. cmd->bio->bi_iter.bi_sector = sector;
  320. else
  321. cmd->rq->__sector = sector;
  322. } else if (sector != zone->wp) {
  323. ret = BLK_STS_IOERR;
  324. goto unlock;
  325. }
  326. if (zone->wp + nr_sectors > zone->start + zone->capacity) {
  327. ret = BLK_STS_IOERR;
  328. goto unlock;
  329. }
  330. if (zone->cond == BLK_ZONE_COND_CLOSED) {
  331. dev->nr_zones_closed--;
  332. dev->nr_zones_imp_open++;
  333. } else if (zone->cond == BLK_ZONE_COND_EMPTY) {
  334. dev->nr_zones_imp_open++;
  335. }
  336. if (zone->cond != BLK_ZONE_COND_EXP_OPEN)
  337. zone->cond = BLK_ZONE_COND_IMP_OPEN;
  338. /*
  339. * Memory backing allocation may sleep: release the zone_lock spinlock
  340. * to avoid scheduling in atomic context. Zone operation atomicity is
  341. * still guaranteed through the zone_locks bitmap.
  342. */
  343. if (dev->memory_backed)
  344. spin_unlock_irq(&dev->zone_lock);
  345. ret = null_process_cmd(cmd, REQ_OP_WRITE, sector, nr_sectors);
  346. if (dev->memory_backed)
  347. spin_lock_irq(&dev->zone_lock);
  348. if (ret != BLK_STS_OK)
  349. goto unlock;
  350. zone->wp += nr_sectors;
  351. if (zone->wp == zone->start + zone->capacity) {
  352. if (zone->cond == BLK_ZONE_COND_EXP_OPEN)
  353. dev->nr_zones_exp_open--;
  354. else if (zone->cond == BLK_ZONE_COND_IMP_OPEN)
  355. dev->nr_zones_imp_open--;
  356. zone->cond = BLK_ZONE_COND_FULL;
  357. }
  358. ret = BLK_STS_OK;
  359. unlock:
  360. null_unlock_zone(dev, zno);
  361. return ret;
  362. }
  363. static blk_status_t null_open_zone(struct nullb_device *dev, struct blk_zone *zone)
  364. {
  365. blk_status_t ret;
  366. if (zone->type == BLK_ZONE_TYPE_CONVENTIONAL)
  367. return BLK_STS_IOERR;
  368. switch (zone->cond) {
  369. case BLK_ZONE_COND_EXP_OPEN:
  370. /* open operation on exp open is not an error */
  371. return BLK_STS_OK;
  372. case BLK_ZONE_COND_EMPTY:
  373. ret = null_check_zone_resources(dev, zone);
  374. if (ret != BLK_STS_OK)
  375. return ret;
  376. break;
  377. case BLK_ZONE_COND_IMP_OPEN:
  378. dev->nr_zones_imp_open--;
  379. break;
  380. case BLK_ZONE_COND_CLOSED:
  381. ret = null_check_zone_resources(dev, zone);
  382. if (ret != BLK_STS_OK)
  383. return ret;
  384. dev->nr_zones_closed--;
  385. break;
  386. case BLK_ZONE_COND_FULL:
  387. default:
  388. return BLK_STS_IOERR;
  389. }
  390. zone->cond = BLK_ZONE_COND_EXP_OPEN;
  391. dev->nr_zones_exp_open++;
  392. return BLK_STS_OK;
  393. }
  394. static blk_status_t null_finish_zone(struct nullb_device *dev, struct blk_zone *zone)
  395. {
  396. blk_status_t ret;
  397. if (zone->type == BLK_ZONE_TYPE_CONVENTIONAL)
  398. return BLK_STS_IOERR;
  399. switch (zone->cond) {
  400. case BLK_ZONE_COND_FULL:
  401. /* finish operation on full is not an error */
  402. return BLK_STS_OK;
  403. case BLK_ZONE_COND_EMPTY:
  404. ret = null_check_zone_resources(dev, zone);
  405. if (ret != BLK_STS_OK)
  406. return ret;
  407. break;
  408. case BLK_ZONE_COND_IMP_OPEN:
  409. dev->nr_zones_imp_open--;
  410. break;
  411. case BLK_ZONE_COND_EXP_OPEN:
  412. dev->nr_zones_exp_open--;
  413. break;
  414. case BLK_ZONE_COND_CLOSED:
  415. ret = null_check_zone_resources(dev, zone);
  416. if (ret != BLK_STS_OK)
  417. return ret;
  418. dev->nr_zones_closed--;
  419. break;
  420. default:
  421. return BLK_STS_IOERR;
  422. }
  423. zone->cond = BLK_ZONE_COND_FULL;
  424. zone->wp = zone->start + zone->len;
  425. return BLK_STS_OK;
  426. }
  427. static blk_status_t null_reset_zone(struct nullb_device *dev, struct blk_zone *zone)
  428. {
  429. if (zone->type == BLK_ZONE_TYPE_CONVENTIONAL)
  430. return BLK_STS_IOERR;
  431. switch (zone->cond) {
  432. case BLK_ZONE_COND_EMPTY:
  433. /* reset operation on empty is not an error */
  434. return BLK_STS_OK;
  435. case BLK_ZONE_COND_IMP_OPEN:
  436. dev->nr_zones_imp_open--;
  437. break;
  438. case BLK_ZONE_COND_EXP_OPEN:
  439. dev->nr_zones_exp_open--;
  440. break;
  441. case BLK_ZONE_COND_CLOSED:
  442. dev->nr_zones_closed--;
  443. break;
  444. case BLK_ZONE_COND_FULL:
  445. break;
  446. default:
  447. return BLK_STS_IOERR;
  448. }
  449. zone->cond = BLK_ZONE_COND_EMPTY;
  450. zone->wp = zone->start;
  451. return BLK_STS_OK;
  452. }
  453. static blk_status_t null_zone_mgmt(struct nullb_cmd *cmd, enum req_opf op,
  454. sector_t sector)
  455. {
  456. struct nullb_device *dev = cmd->nq->dev;
  457. unsigned int zone_no;
  458. struct blk_zone *zone;
  459. blk_status_t ret;
  460. size_t i;
  461. if (op == REQ_OP_ZONE_RESET_ALL) {
  462. for (i = dev->zone_nr_conv; i < dev->nr_zones; i++) {
  463. null_lock_zone(dev, i);
  464. zone = &dev->zones[i];
  465. if (zone->cond != BLK_ZONE_COND_EMPTY) {
  466. null_reset_zone(dev, zone);
  467. trace_nullb_zone_op(cmd, i, zone->cond);
  468. }
  469. null_unlock_zone(dev, i);
  470. }
  471. return BLK_STS_OK;
  472. }
  473. zone_no = null_zone_no(dev, sector);
  474. zone = &dev->zones[zone_no];
  475. null_lock_zone(dev, zone_no);
  476. switch (op) {
  477. case REQ_OP_ZONE_RESET:
  478. ret = null_reset_zone(dev, zone);
  479. break;
  480. case REQ_OP_ZONE_OPEN:
  481. ret = null_open_zone(dev, zone);
  482. break;
  483. case REQ_OP_ZONE_CLOSE:
  484. ret = null_close_zone(dev, zone);
  485. break;
  486. case REQ_OP_ZONE_FINISH:
  487. ret = null_finish_zone(dev, zone);
  488. break;
  489. default:
  490. ret = BLK_STS_NOTSUPP;
  491. break;
  492. }
  493. if (ret == BLK_STS_OK)
  494. trace_nullb_zone_op(cmd, zone_no, zone->cond);
  495. null_unlock_zone(dev, zone_no);
  496. return ret;
  497. }
  498. blk_status_t null_process_zoned_cmd(struct nullb_cmd *cmd, enum req_opf op,
  499. sector_t sector, sector_t nr_sectors)
  500. {
  501. struct nullb_device *dev = cmd->nq->dev;
  502. unsigned int zno = null_zone_no(dev, sector);
  503. blk_status_t sts;
  504. switch (op) {
  505. case REQ_OP_WRITE:
  506. sts = null_zone_write(cmd, sector, nr_sectors, false);
  507. break;
  508. case REQ_OP_ZONE_APPEND:
  509. sts = null_zone_write(cmd, sector, nr_sectors, true);
  510. break;
  511. case REQ_OP_ZONE_RESET:
  512. case REQ_OP_ZONE_RESET_ALL:
  513. case REQ_OP_ZONE_OPEN:
  514. case REQ_OP_ZONE_CLOSE:
  515. case REQ_OP_ZONE_FINISH:
  516. sts = null_zone_mgmt(cmd, op, sector);
  517. break;
  518. default:
  519. null_lock_zone(dev, zno);
  520. sts = null_process_cmd(cmd, op, sector, nr_sectors);
  521. null_unlock_zone(dev, zno);
  522. }
  523. return sts;
  524. }