badrange.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright(c) 2017 Intel Corporation. All rights reserved.
  4. */
  5. #include <linux/libnvdimm.h>
  6. #include <linux/badblocks.h>
  7. #include <linux/export.h>
  8. #include <linux/module.h>
  9. #include <linux/blkdev.h>
  10. #include <linux/device.h>
  11. #include <linux/ctype.h>
  12. #include <linux/ndctl.h>
  13. #include <linux/mutex.h>
  14. #include <linux/slab.h>
  15. #include <linux/io.h>
  16. #include "nd-core.h"
  17. #include "nd.h"
  18. void badrange_init(struct badrange *badrange)
  19. {
  20. INIT_LIST_HEAD(&badrange->list);
  21. spin_lock_init(&badrange->lock);
  22. }
  23. EXPORT_SYMBOL_GPL(badrange_init);
  24. static void append_badrange_entry(struct badrange *badrange,
  25. struct badrange_entry *bre, u64 addr, u64 length)
  26. {
  27. lockdep_assert_held(&badrange->lock);
  28. bre->start = addr;
  29. bre->length = length;
  30. list_add_tail(&bre->list, &badrange->list);
  31. }
  32. static int alloc_and_append_badrange_entry(struct badrange *badrange,
  33. u64 addr, u64 length, gfp_t flags)
  34. {
  35. struct badrange_entry *bre;
  36. bre = kzalloc(sizeof(*bre), flags);
  37. if (!bre)
  38. return -ENOMEM;
  39. append_badrange_entry(badrange, bre, addr, length);
  40. return 0;
  41. }
  42. static int add_badrange(struct badrange *badrange, u64 addr, u64 length)
  43. {
  44. struct badrange_entry *bre, *bre_new;
  45. spin_unlock(&badrange->lock);
  46. bre_new = kzalloc(sizeof(*bre_new), GFP_KERNEL);
  47. spin_lock(&badrange->lock);
  48. if (list_empty(&badrange->list)) {
  49. if (!bre_new)
  50. return -ENOMEM;
  51. append_badrange_entry(badrange, bre_new, addr, length);
  52. return 0;
  53. }
  54. /*
  55. * There is a chance this is a duplicate, check for those first.
  56. * This will be the common case as ARS_STATUS returns all known
  57. * errors in the SPA space, and we can't query it per region
  58. */
  59. list_for_each_entry(bre, &badrange->list, list)
  60. if (bre->start == addr) {
  61. /* If length has changed, update this list entry */
  62. if (bre->length != length)
  63. bre->length = length;
  64. kfree(bre_new);
  65. return 0;
  66. }
  67. /*
  68. * If not a duplicate or a simple length update, add the entry as is,
  69. * as any overlapping ranges will get resolved when the list is consumed
  70. * and converted to badblocks
  71. */
  72. if (!bre_new)
  73. return -ENOMEM;
  74. append_badrange_entry(badrange, bre_new, addr, length);
  75. return 0;
  76. }
  77. int badrange_add(struct badrange *badrange, u64 addr, u64 length)
  78. {
  79. int rc;
  80. spin_lock(&badrange->lock);
  81. rc = add_badrange(badrange, addr, length);
  82. spin_unlock(&badrange->lock);
  83. return rc;
  84. }
  85. EXPORT_SYMBOL_GPL(badrange_add);
  86. void badrange_forget(struct badrange *badrange, phys_addr_t start,
  87. unsigned int len)
  88. {
  89. struct list_head *badrange_list = &badrange->list;
  90. u64 clr_end = start + len - 1;
  91. struct badrange_entry *bre, *next;
  92. spin_lock(&badrange->lock);
  93. /*
  94. * [start, clr_end] is the badrange interval being cleared.
  95. * [bre->start, bre_end] is the badrange_list entry we're comparing
  96. * the above interval against. The badrange list entry may need
  97. * to be modified (update either start or length), deleted, or
  98. * split into two based on the overlap characteristics
  99. */
  100. list_for_each_entry_safe(bre, next, badrange_list, list) {
  101. u64 bre_end = bre->start + bre->length - 1;
  102. /* Skip intervals with no intersection */
  103. if (bre_end < start)
  104. continue;
  105. if (bre->start > clr_end)
  106. continue;
  107. /* Delete completely overlapped badrange entries */
  108. if ((bre->start >= start) && (bre_end <= clr_end)) {
  109. list_del(&bre->list);
  110. kfree(bre);
  111. continue;
  112. }
  113. /* Adjust start point of partially cleared entries */
  114. if ((start <= bre->start) && (clr_end > bre->start)) {
  115. bre->length -= clr_end - bre->start + 1;
  116. bre->start = clr_end + 1;
  117. continue;
  118. }
  119. /* Adjust bre->length for partial clearing at the tail end */
  120. if ((bre->start < start) && (bre_end <= clr_end)) {
  121. /* bre->start remains the same */
  122. bre->length = start - bre->start;
  123. continue;
  124. }
  125. /*
  126. * If clearing in the middle of an entry, we split it into
  127. * two by modifying the current entry to represent one half of
  128. * the split, and adding a new entry for the second half.
  129. */
  130. if ((bre->start < start) && (bre_end > clr_end)) {
  131. u64 new_start = clr_end + 1;
  132. u64 new_len = bre_end - new_start + 1;
  133. /* Add new entry covering the right half */
  134. alloc_and_append_badrange_entry(badrange, new_start,
  135. new_len, GFP_NOWAIT);
  136. /* Adjust this entry to cover the left half */
  137. bre->length = start - bre->start;
  138. continue;
  139. }
  140. }
  141. spin_unlock(&badrange->lock);
  142. }
  143. EXPORT_SYMBOL_GPL(badrange_forget);
  144. static void set_badblock(struct badblocks *bb, sector_t s, int num)
  145. {
  146. dev_dbg(bb->dev, "Found a bad range (0x%llx, 0x%llx)\n",
  147. (u64) s * 512, (u64) num * 512);
  148. /* this isn't an error as the hardware will still throw an exception */
  149. if (badblocks_set(bb, s, num, 1))
  150. dev_info_once(bb->dev, "%s: failed for sector %llx\n",
  151. __func__, (u64) s);
  152. }
  153. /**
  154. * __add_badblock_range() - Convert a physical address range to bad sectors
  155. * @bb: badblocks instance to populate
  156. * @ns_offset: namespace offset where the error range begins (in bytes)
  157. * @len: number of bytes of badrange to be added
  158. *
  159. * This assumes that the range provided with (ns_offset, len) is within
  160. * the bounds of physical addresses for this namespace, i.e. lies in the
  161. * interval [ns_start, ns_start + ns_size)
  162. */
  163. static void __add_badblock_range(struct badblocks *bb, u64 ns_offset, u64 len)
  164. {
  165. const unsigned int sector_size = 512;
  166. sector_t start_sector, end_sector;
  167. u64 num_sectors;
  168. u32 rem;
  169. start_sector = div_u64(ns_offset, sector_size);
  170. end_sector = div_u64_rem(ns_offset + len, sector_size, &rem);
  171. if (rem)
  172. end_sector++;
  173. num_sectors = end_sector - start_sector;
  174. if (unlikely(num_sectors > (u64)INT_MAX)) {
  175. u64 remaining = num_sectors;
  176. sector_t s = start_sector;
  177. while (remaining) {
  178. int done = min_t(u64, remaining, INT_MAX);
  179. set_badblock(bb, s, done);
  180. remaining -= done;
  181. s += done;
  182. }
  183. } else
  184. set_badblock(bb, start_sector, num_sectors);
  185. }
  186. static void badblocks_populate(struct badrange *badrange,
  187. struct badblocks *bb, const struct range *range)
  188. {
  189. struct badrange_entry *bre;
  190. if (list_empty(&badrange->list))
  191. return;
  192. list_for_each_entry(bre, &badrange->list, list) {
  193. u64 bre_end = bre->start + bre->length - 1;
  194. /* Discard intervals with no intersection */
  195. if (bre_end < range->start)
  196. continue;
  197. if (bre->start > range->end)
  198. continue;
  199. /* Deal with any overlap after start of the namespace */
  200. if (bre->start >= range->start) {
  201. u64 start = bre->start;
  202. u64 len;
  203. if (bre_end <= range->end)
  204. len = bre->length;
  205. else
  206. len = range->start + range_len(range)
  207. - bre->start;
  208. __add_badblock_range(bb, start - range->start, len);
  209. continue;
  210. }
  211. /*
  212. * Deal with overlap for badrange starting before
  213. * the namespace.
  214. */
  215. if (bre->start < range->start) {
  216. u64 len;
  217. if (bre_end < range->end)
  218. len = bre->start + bre->length - range->start;
  219. else
  220. len = range_len(range);
  221. __add_badblock_range(bb, 0, len);
  222. }
  223. }
  224. }
  225. /**
  226. * nvdimm_badblocks_populate() - Convert a list of badranges to badblocks
  227. * @region: parent region of the range to interrogate
  228. * @bb: badblocks instance to populate
  229. * @res: resource range to consider
  230. *
  231. * The badrange list generated during bus initialization may contain
  232. * multiple, possibly overlapping physical address ranges. Compare each
  233. * of these ranges to the resource range currently being initialized,
  234. * and add badblocks entries for all matching sub-ranges
  235. */
  236. void nvdimm_badblocks_populate(struct nd_region *nd_region,
  237. struct badblocks *bb, const struct range *range)
  238. {
  239. struct nvdimm_bus *nvdimm_bus;
  240. if (!is_memory(&nd_region->dev)) {
  241. dev_WARN_ONCE(&nd_region->dev, 1,
  242. "%s only valid for pmem regions\n", __func__);
  243. return;
  244. }
  245. nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev);
  246. nvdimm_bus_lock(&nvdimm_bus->dev);
  247. badblocks_populate(&nvdimm_bus->badrange, bb, range);
  248. nvdimm_bus_unlock(&nvdimm_bus->dev);
  249. }
  250. EXPORT_SYMBOL_GPL(nvdimm_badblocks_populate);