423-v6.1-0003-mtd-add-ECC-error-accounting-for-each-read-request.patch 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. From 2ed18d818d1f7492172f8dd5904344c7d367e8ed Mon Sep 17 00:00:00 2001
  2. From: =?UTF-8?q?Micha=C5=82=20K=C4=99pie=C5=84?= <[email protected]>
  3. Date: Wed, 29 Jun 2022 14:57:36 +0200
  4. Subject: [PATCH 3/4] mtd: add ECC error accounting for each read request
  5. MIME-Version: 1.0
  6. Content-Type: text/plain; charset=UTF-8
  7. Content-Transfer-Encoding: 8bit
  8. Extend struct mtd_req_stats with two new fields holding the number of
  9. corrected bitflips and uncorrectable errors detected during a read
  10. operation. This is a prerequisite for ultimately passing those counters
  11. to user space, where they can be useful to applications for making
  12. better-informed choices about moving data around.
  13. Unlike 'max_bitflips' (which is set - in a common code path - to the
  14. return value of a function called while the MTD device's mutex is held),
  15. these counters have to be maintained in each MTD driver which defines
  16. the '_read_oob' callback because the statistics need to be calculated
  17. while the MTD device's mutex is held.
  18. Suggested-by: Boris Brezillon <[email protected]>
  19. Signed-off-by: Michał Kępień <[email protected]>
  20. Signed-off-by: Miquel Raynal <[email protected]>
  21. Link: https://lore.kernel.org/linux-mtd/[email protected]
  22. ---
  23. drivers/mtd/devices/docg3.c | 8 ++++++++
  24. drivers/mtd/nand/onenand/onenand_base.c | 12 ++++++++++++
  25. drivers/mtd/nand/raw/nand_base.c | 10 ++++++++++
  26. drivers/mtd/nand/spi/core.c | 10 ++++++++++
  27. include/linux/mtd/mtd.h | 2 ++
  28. 5 files changed, 42 insertions(+)
  29. --- a/drivers/mtd/devices/docg3.c
  30. +++ b/drivers/mtd/devices/docg3.c
  31. @@ -871,6 +871,7 @@ static int doc_read_oob(struct mtd_info
  32. u8 *buf = ops->datbuf;
  33. size_t len, ooblen, nbdata, nboob;
  34. u8 hwecc[DOC_ECC_BCH_SIZE], eccconf1;
  35. + struct mtd_ecc_stats old_stats;
  36. int max_bitflips = 0;
  37. if (buf)
  38. @@ -895,6 +896,7 @@ static int doc_read_oob(struct mtd_info
  39. ret = 0;
  40. skip = from % DOC_LAYOUT_PAGE_SIZE;
  41. mutex_lock(&docg3->cascade->lock);
  42. + old_stats = mtd->ecc_stats;
  43. while (ret >= 0 && (len > 0 || ooblen > 0)) {
  44. calc_block_sector(from - skip, &block0, &block1, &page, &ofs,
  45. docg3->reliable);
  46. @@ -966,6 +968,12 @@ static int doc_read_oob(struct mtd_info
  47. }
  48. out:
  49. + if (ops->stats) {
  50. + ops->stats->uncorrectable_errors +=
  51. + mtd->ecc_stats.failed - old_stats.failed;
  52. + ops->stats->corrected_bitflips +=
  53. + mtd->ecc_stats.corrected - old_stats.corrected;
  54. + }
  55. mutex_unlock(&docg3->cascade->lock);
  56. return ret;
  57. err_in_read:
  58. --- a/drivers/mtd/nand/onenand/onenand_base.c
  59. +++ b/drivers/mtd/nand/onenand/onenand_base.c
  60. @@ -1440,6 +1440,7 @@ static int onenand_read_oob(struct mtd_i
  61. struct mtd_oob_ops *ops)
  62. {
  63. struct onenand_chip *this = mtd->priv;
  64. + struct mtd_ecc_stats old_stats;
  65. int ret;
  66. switch (ops->mode) {
  67. @@ -1453,12 +1454,23 @@ static int onenand_read_oob(struct mtd_i
  68. }
  69. onenand_get_device(mtd, FL_READING);
  70. +
  71. + old_stats = mtd->ecc_stats;
  72. +
  73. if (ops->datbuf)
  74. ret = ONENAND_IS_4KB_PAGE(this) ?
  75. onenand_mlc_read_ops_nolock(mtd, from, ops) :
  76. onenand_read_ops_nolock(mtd, from, ops);
  77. else
  78. ret = onenand_read_oob_nolock(mtd, from, ops);
  79. +
  80. + if (ops->stats) {
  81. + ops->stats->uncorrectable_errors +=
  82. + mtd->ecc_stats.failed - old_stats.failed;
  83. + ops->stats->corrected_bitflips +=
  84. + mtd->ecc_stats.corrected - old_stats.corrected;
  85. + }
  86. +
  87. onenand_release_device(mtd);
  88. return ret;
  89. --- a/drivers/mtd/nand/raw/nand_base.c
  90. +++ b/drivers/mtd/nand/raw/nand_base.c
  91. @@ -3815,6 +3815,7 @@ static int nand_read_oob(struct mtd_info
  92. struct mtd_oob_ops *ops)
  93. {
  94. struct nand_chip *chip = mtd_to_nand(mtd);
  95. + struct mtd_ecc_stats old_stats;
  96. int ret;
  97. ops->retlen = 0;
  98. @@ -3826,11 +3827,20 @@ static int nand_read_oob(struct mtd_info
  99. nand_get_device(chip);
  100. + old_stats = mtd->ecc_stats;
  101. +
  102. if (!ops->datbuf)
  103. ret = nand_do_read_oob(chip, from, ops);
  104. else
  105. ret = nand_do_read_ops(chip, from, ops);
  106. + if (ops->stats) {
  107. + ops->stats->uncorrectable_errors +=
  108. + mtd->ecc_stats.failed - old_stats.failed;
  109. + ops->stats->corrected_bitflips +=
  110. + mtd->ecc_stats.corrected - old_stats.corrected;
  111. + }
  112. +
  113. nand_release_device(chip);
  114. return ret;
  115. }
  116. --- a/drivers/mtd/nand/spi/core.c
  117. +++ b/drivers/mtd/nand/spi/core.c
  118. @@ -629,6 +629,7 @@ static int spinand_mtd_read(struct mtd_i
  119. {
  120. struct spinand_device *spinand = mtd_to_spinand(mtd);
  121. struct nand_device *nand = mtd_to_nanddev(mtd);
  122. + struct mtd_ecc_stats old_stats;
  123. unsigned int max_bitflips = 0;
  124. struct nand_io_iter iter;
  125. bool disable_ecc = false;
  126. @@ -640,6 +641,8 @@ static int spinand_mtd_read(struct mtd_i
  127. mutex_lock(&spinand->lock);
  128. + old_stats = mtd->ecc_stats;
  129. +
  130. nanddev_io_for_each_page(nand, NAND_PAGE_READ, from, ops, &iter) {
  131. if (disable_ecc)
  132. iter.req.mode = MTD_OPS_RAW;
  133. @@ -662,6 +665,13 @@ static int spinand_mtd_read(struct mtd_i
  134. ops->oobretlen += iter.req.ooblen;
  135. }
  136. + if (ops->stats) {
  137. + ops->stats->uncorrectable_errors +=
  138. + mtd->ecc_stats.failed - old_stats.failed;
  139. + ops->stats->corrected_bitflips +=
  140. + mtd->ecc_stats.corrected - old_stats.corrected;
  141. + }
  142. +
  143. mutex_unlock(&spinand->lock);
  144. if (ecc_failed && !ret)
  145. --- a/include/linux/mtd/mtd.h
  146. +++ b/include/linux/mtd/mtd.h
  147. @@ -41,6 +41,8 @@ struct mtd_erase_region_info {
  148. };
  149. struct mtd_req_stats {
  150. + unsigned int uncorrectable_errors;
  151. + unsigned int corrected_bitflips;
  152. unsigned int max_bitflips;
  153. };