423-v6.1-0004-mtdchar-add-MEMREAD-ioctl.patch 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. From 2c9745d36e04ac27161acd78514f647b9b587ad4 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:37 +0200
  4. Subject: [PATCH 4/4] mtdchar: add MEMREAD ioctl
  5. MIME-Version: 1.0
  6. Content-Type: text/plain; charset=UTF-8
  7. Content-Transfer-Encoding: 8bit
  8. User-space applications making use of MTD devices via /dev/mtd*
  9. character devices currently have limited capabilities for reading data:
  10. - only deprecated methods of accessing OOB layout information exist,
  11. - there is no way to explicitly specify MTD operation mode to use; it
  12. is auto-selected based on the MTD file mode (MTD_FILE_MODE_*) set
  13. for the character device; in particular, this prevents using
  14. MTD_OPS_AUTO_OOB for reads,
  15. - all existing user-space interfaces which cause mtd_read() or
  16. mtd_read_oob() to be called (via mtdchar_read() and
  17. mtdchar_read_oob(), respectively) return success even when those
  18. functions return -EUCLEAN or -EBADMSG; this renders user-space
  19. applications using these interfaces unaware of any corrected
  20. bitflips or uncorrectable ECC errors detected during reads.
  21. Note that the existing MEMWRITE ioctl allows the MTD operation mode to
  22. be explicitly set, allowing user-space applications to write page data
  23. and OOB data without requiring them to know anything about the OOB
  24. layout of the MTD device they are writing to (MTD_OPS_AUTO_OOB). Also,
  25. the MEMWRITE ioctl does not mangle the return value of mtd_write_oob().
  26. Add a new ioctl, MEMREAD, which addresses the above issues. It is
  27. intended to be a read-side counterpart of the existing MEMWRITE ioctl.
  28. Similarly to the latter, the read operation is performed in a loop which
  29. processes at most mtd->erasesize bytes in each iteration. This is done
  30. to prevent unbounded memory allocations caused by calling kmalloc() with
  31. the 'size' argument taken directly from the struct mtd_read_req provided
  32. by user space. However, the new ioctl is implemented so that the values
  33. it returns match those that would have been returned if just a single
  34. mtd_read_oob() call was issued to handle the entire read operation in
  35. one go.
  36. Note that while just returning -EUCLEAN or -EBADMSG to user space would
  37. already be a valid and useful indication of the ECC algorithm detecting
  38. errors during a read operation, that signal would not be granular enough
  39. to cover all use cases. For example, knowing the maximum number of
  40. bitflips detected in a single ECC step during a read operation performed
  41. on a given page may be useful when dealing with an MTD partition whose
  42. ECC layout varies across pages (e.g. a partition consisting of a
  43. bootloader area using a "custom" ECC layout followed by data pages using
  44. a "standard" ECC layout). To address that, include ECC statistics in
  45. the structure returned to user space by the new MEMREAD ioctl.
  46. Link: https://www.infradead.org/pipermail/linux-mtd/2016-April/067085.html
  47. Suggested-by: Boris Brezillon <[email protected]>
  48. Signed-off-by: Michał Kępień <[email protected]>
  49. Acked-by: Richard Weinberger <[email protected]>
  50. Signed-off-by: Miquel Raynal <[email protected]>
  51. Link: https://lore.kernel.org/linux-mtd/[email protected]
  52. ---
  53. drivers/mtd/mtdchar.c | 139 +++++++++++++++++++++++++++++++++++++
  54. include/uapi/mtd/mtd-abi.h | 64 +++++++++++++++--
  55. 2 files changed, 198 insertions(+), 5 deletions(-)
  56. --- a/drivers/mtd/mtdchar.c
  57. +++ b/drivers/mtd/mtdchar.c
  58. @@ -621,6 +621,137 @@ static int mtdchar_write_ioctl(struct mt
  59. return ret;
  60. }
  61. +static int mtdchar_read_ioctl(struct mtd_info *mtd,
  62. + struct mtd_read_req __user *argp)
  63. +{
  64. + struct mtd_info *master = mtd_get_master(mtd);
  65. + struct mtd_read_req req;
  66. + void __user *usr_data, *usr_oob;
  67. + uint8_t *datbuf = NULL, *oobbuf = NULL;
  68. + size_t datbuf_len, oobbuf_len;
  69. + size_t orig_len, orig_ooblen;
  70. + int ret = 0;
  71. +
  72. + if (copy_from_user(&req, argp, sizeof(req)))
  73. + return -EFAULT;
  74. +
  75. + orig_len = req.len;
  76. + orig_ooblen = req.ooblen;
  77. +
  78. + usr_data = (void __user *)(uintptr_t)req.usr_data;
  79. + usr_oob = (void __user *)(uintptr_t)req.usr_oob;
  80. +
  81. + if (!master->_read_oob)
  82. + return -EOPNOTSUPP;
  83. +
  84. + if (!usr_data)
  85. + req.len = 0;
  86. +
  87. + if (!usr_oob)
  88. + req.ooblen = 0;
  89. +
  90. + req.ecc_stats.uncorrectable_errors = 0;
  91. + req.ecc_stats.corrected_bitflips = 0;
  92. + req.ecc_stats.max_bitflips = 0;
  93. +
  94. + req.len &= 0xffffffff;
  95. + req.ooblen &= 0xffffffff;
  96. +
  97. + if (req.start + req.len > mtd->size) {
  98. + ret = -EINVAL;
  99. + goto out;
  100. + }
  101. +
  102. + datbuf_len = min_t(size_t, req.len, mtd->erasesize);
  103. + if (datbuf_len > 0) {
  104. + datbuf = kvmalloc(datbuf_len, GFP_KERNEL);
  105. + if (!datbuf) {
  106. + ret = -ENOMEM;
  107. + goto out;
  108. + }
  109. + }
  110. +
  111. + oobbuf_len = min_t(size_t, req.ooblen, mtd->erasesize);
  112. + if (oobbuf_len > 0) {
  113. + oobbuf = kvmalloc(oobbuf_len, GFP_KERNEL);
  114. + if (!oobbuf) {
  115. + ret = -ENOMEM;
  116. + goto out;
  117. + }
  118. + }
  119. +
  120. + while (req.len > 0 || (!usr_data && req.ooblen > 0)) {
  121. + struct mtd_req_stats stats;
  122. + struct mtd_oob_ops ops = {
  123. + .mode = req.mode,
  124. + .len = min_t(size_t, req.len, datbuf_len),
  125. + .ooblen = min_t(size_t, req.ooblen, oobbuf_len),
  126. + .datbuf = datbuf,
  127. + .oobbuf = oobbuf,
  128. + .stats = &stats,
  129. + };
  130. +
  131. + /*
  132. + * Shorten non-page-aligned, eraseblock-sized reads so that the
  133. + * read ends on an eraseblock boundary. This is necessary in
  134. + * order to prevent OOB data for some pages from being
  135. + * duplicated in the output of non-page-aligned reads requiring
  136. + * multiple mtd_read_oob() calls to be completed.
  137. + */
  138. + if (ops.len == mtd->erasesize)
  139. + ops.len -= mtd_mod_by_ws(req.start + ops.len, mtd);
  140. +
  141. + ret = mtd_read_oob(mtd, (loff_t)req.start, &ops);
  142. +
  143. + req.ecc_stats.uncorrectable_errors +=
  144. + stats.uncorrectable_errors;
  145. + req.ecc_stats.corrected_bitflips += stats.corrected_bitflips;
  146. + req.ecc_stats.max_bitflips =
  147. + max(req.ecc_stats.max_bitflips, stats.max_bitflips);
  148. +
  149. + if (ret && !mtd_is_bitflip_or_eccerr(ret))
  150. + break;
  151. +
  152. + if (copy_to_user(usr_data, ops.datbuf, ops.retlen) ||
  153. + copy_to_user(usr_oob, ops.oobbuf, ops.oobretlen)) {
  154. + ret = -EFAULT;
  155. + break;
  156. + }
  157. +
  158. + req.start += ops.retlen;
  159. + req.len -= ops.retlen;
  160. + usr_data += ops.retlen;
  161. +
  162. + req.ooblen -= ops.oobretlen;
  163. + usr_oob += ops.oobretlen;
  164. + }
  165. +
  166. + /*
  167. + * As multiple iterations of the above loop (and therefore multiple
  168. + * mtd_read_oob() calls) may be necessary to complete the read request,
  169. + * adjust the final return code to ensure it accounts for all detected
  170. + * ECC errors.
  171. + */
  172. + if (!ret || mtd_is_bitflip(ret)) {
  173. + if (req.ecc_stats.uncorrectable_errors > 0)
  174. + ret = -EBADMSG;
  175. + else if (req.ecc_stats.corrected_bitflips > 0)
  176. + ret = -EUCLEAN;
  177. + }
  178. +
  179. +out:
  180. + req.len = orig_len - req.len;
  181. + req.ooblen = orig_ooblen - req.ooblen;
  182. +
  183. + if (copy_to_user(argp, &req, sizeof(req)))
  184. + ret = -EFAULT;
  185. +
  186. + kvfree(datbuf);
  187. + kvfree(oobbuf);
  188. +
  189. + return ret;
  190. +}
  191. +
  192. static int mtdchar_ioctl(struct file *file, u_int cmd, u_long arg)
  193. {
  194. struct mtd_file_info *mfi = file->private_data;
  195. @@ -643,6 +774,7 @@ static int mtdchar_ioctl(struct file *fi
  196. case MEMGETINFO:
  197. case MEMREADOOB:
  198. case MEMREADOOB64:
  199. + case MEMREAD:
  200. case MEMISLOCKED:
  201. case MEMGETOOBSEL:
  202. case MEMGETBADBLOCK:
  203. @@ -817,6 +949,13 @@ static int mtdchar_ioctl(struct file *fi
  204. break;
  205. }
  206. + case MEMREAD:
  207. + {
  208. + ret = mtdchar_read_ioctl(mtd,
  209. + (struct mtd_read_req __user *)arg);
  210. + break;
  211. + }
  212. +
  213. case MEMLOCK:
  214. {
  215. struct erase_info_user einfo;
  216. --- a/include/uapi/mtd/mtd-abi.h
  217. +++ b/include/uapi/mtd/mtd-abi.h
  218. @@ -55,9 +55,9 @@ struct mtd_oob_buf64 {
  219. * @MTD_OPS_RAW: data are transferred as-is, with no error correction;
  220. * this mode implies %MTD_OPS_PLACE_OOB
  221. *
  222. - * These modes can be passed to ioctl(MEMWRITE) and are also used internally.
  223. - * See notes on "MTD file modes" for discussion on %MTD_OPS_RAW vs.
  224. - * %MTD_FILE_MODE_RAW.
  225. + * These modes can be passed to ioctl(MEMWRITE) and ioctl(MEMREAD); they are
  226. + * also used internally. See notes on "MTD file modes" for discussion on
  227. + * %MTD_OPS_RAW vs. %MTD_FILE_MODE_RAW.
  228. */
  229. enum {
  230. MTD_OPS_PLACE_OOB = 0,
  231. @@ -91,6 +91,53 @@ struct mtd_write_req {
  232. __u8 padding[7];
  233. };
  234. +/**
  235. + * struct mtd_read_req_ecc_stats - ECC statistics for a read operation
  236. + *
  237. + * @uncorrectable_errors: the number of uncorrectable errors that happened
  238. + * during the read operation
  239. + * @corrected_bitflips: the number of bitflips corrected during the read
  240. + * operation
  241. + * @max_bitflips: the maximum number of bitflips detected in any single ECC
  242. + * step for the data read during the operation; this information
  243. + * can be used to decide whether the data stored in a specific
  244. + * region of the MTD device should be moved somewhere else to
  245. + * avoid data loss.
  246. + */
  247. +struct mtd_read_req_ecc_stats {
  248. + __u32 uncorrectable_errors;
  249. + __u32 corrected_bitflips;
  250. + __u32 max_bitflips;
  251. +};
  252. +
  253. +/**
  254. + * struct mtd_read_req - data structure for requesting a read operation
  255. + *
  256. + * @start: start address
  257. + * @len: length of data buffer (only lower 32 bits are used)
  258. + * @ooblen: length of OOB buffer (only lower 32 bits are used)
  259. + * @usr_data: user-provided data buffer
  260. + * @usr_oob: user-provided OOB buffer
  261. + * @mode: MTD mode (see "MTD operation modes")
  262. + * @padding: reserved, must be set to 0
  263. + * @ecc_stats: ECC statistics for the read operation
  264. + *
  265. + * This structure supports ioctl(MEMREAD) operations, allowing data and/or OOB
  266. + * reads in various modes. To read from OOB-only, set @usr_data == NULL, and to
  267. + * read data-only, set @usr_oob == NULL. However, setting both @usr_data and
  268. + * @usr_oob to NULL is not allowed.
  269. + */
  270. +struct mtd_read_req {
  271. + __u64 start;
  272. + __u64 len;
  273. + __u64 ooblen;
  274. + __u64 usr_data;
  275. + __u64 usr_oob;
  276. + __u8 mode;
  277. + __u8 padding[7];
  278. + struct mtd_read_req_ecc_stats ecc_stats;
  279. +};
  280. +
  281. #define MTD_ABSENT 0
  282. #define MTD_RAM 1
  283. #define MTD_ROM 2
  284. @@ -207,6 +254,12 @@ struct otp_info {
  285. #define MEMWRITE _IOWR('M', 24, struct mtd_write_req)
  286. /* Erase a given range of user data (must be in mode %MTD_FILE_MODE_OTP_USER) */
  287. #define OTPERASE _IOW('M', 25, struct otp_info)
  288. +/*
  289. + * Most generic read interface; can read in-band and/or out-of-band in various
  290. + * modes (see "struct mtd_read_req"). This ioctl is not supported for flashes
  291. + * without OOB, e.g., NOR flash.
  292. + */
  293. +#define MEMREAD _IOWR('M', 26, struct mtd_read_req)
  294. /*
  295. * Obsolete legacy interface. Keep it in order not to break userspace
  296. @@ -270,8 +323,9 @@ struct mtd_ecc_stats {
  297. * Note: %MTD_FILE_MODE_RAW provides the same functionality as %MTD_OPS_RAW -
  298. * raw access to the flash, without error correction or autoplacement schemes.
  299. * Wherever possible, the MTD_OPS_* mode will override the MTD_FILE_MODE_* mode
  300. - * (e.g., when using ioctl(MEMWRITE)), but in some cases, the MTD_FILE_MODE is
  301. - * used out of necessity (e.g., `write()', ioctl(MEMWRITEOOB64)).
  302. + * (e.g., when using ioctl(MEMWRITE) or ioctl(MEMREAD)), but in some cases, the
  303. + * MTD_FILE_MODE is used out of necessity (e.g., `write()',
  304. + * ioctl(MEMWRITEOOB64)).
  305. */
  306. enum mtd_file_modes {
  307. MTD_FILE_MODE_NORMAL = MTD_OTP_OFF,