stream_open.cocci 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Author: Kirill Smelkov (kirr@nexedi.com)
  3. //
  4. // Search for stream-like files that are using nonseekable_open and convert
  5. // them to stream_open. A stream-like file is a file that does not use ppos in
  6. // its read and write. Rationale for the conversion is to avoid deadlock in
  7. // between read and write.
  8. virtual report
  9. virtual patch
  10. virtual explain // explain decisions in the patch (SPFLAGS="-D explain")
  11. // stream-like reader & writer - ones that do not depend on f_pos.
  12. @ stream_reader @
  13. identifier readstream, ppos;
  14. identifier f, buf, len;
  15. type loff_t;
  16. @@
  17. ssize_t readstream(struct file *f, char *buf, size_t len, loff_t *ppos)
  18. {
  19. ... when != ppos
  20. }
  21. @ stream_writer @
  22. identifier writestream, ppos;
  23. identifier f, buf, len;
  24. type loff_t;
  25. @@
  26. ssize_t writestream(struct file *f, const char *buf, size_t len, loff_t *ppos)
  27. {
  28. ... when != ppos
  29. }
  30. // a function that blocks
  31. @ blocks @
  32. identifier block_f;
  33. identifier wait_event =~ "^wait_event_.*";
  34. @@
  35. block_f(...) {
  36. ... when exists
  37. wait_event(...)
  38. ... when exists
  39. }
  40. // stream_reader that can block inside.
  41. //
  42. // XXX wait_* can be called not directly from current function (e.g. func -> f -> g -> wait())
  43. // XXX currently reader_blocks supports only direct and 1-level indirect cases.
  44. @ reader_blocks_direct @
  45. identifier stream_reader.readstream;
  46. identifier wait_event =~ "^wait_event_.*";
  47. @@
  48. readstream(...)
  49. {
  50. ... when exists
  51. wait_event(...)
  52. ... when exists
  53. }
  54. @ reader_blocks_1 @
  55. identifier stream_reader.readstream;
  56. identifier blocks.block_f;
  57. @@
  58. readstream(...)
  59. {
  60. ... when exists
  61. block_f(...)
  62. ... when exists
  63. }
  64. @ reader_blocks depends on reader_blocks_direct || reader_blocks_1 @
  65. identifier stream_reader.readstream;
  66. @@
  67. readstream(...) {
  68. ...
  69. }
  70. // file_operations + whether they have _any_ .read, .write, .llseek ... at all.
  71. //
  72. // XXX add support for file_operations xxx[N] = ... (sound/core/pcm_native.c)
  73. @ fops0 @
  74. identifier fops;
  75. @@
  76. struct file_operations fops = {
  77. ...
  78. };
  79. @ has_read @
  80. identifier fops0.fops;
  81. identifier read_f;
  82. @@
  83. struct file_operations fops = {
  84. .read = read_f,
  85. };
  86. @ has_read_iter @
  87. identifier fops0.fops;
  88. identifier read_iter_f;
  89. @@
  90. struct file_operations fops = {
  91. .read_iter = read_iter_f,
  92. };
  93. @ has_write @
  94. identifier fops0.fops;
  95. identifier write_f;
  96. @@
  97. struct file_operations fops = {
  98. .write = write_f,
  99. };
  100. @ has_write_iter @
  101. identifier fops0.fops;
  102. identifier write_iter_f;
  103. @@
  104. struct file_operations fops = {
  105. .write_iter = write_iter_f,
  106. };
  107. @ has_llseek @
  108. identifier fops0.fops;
  109. identifier llseek_f;
  110. @@
  111. struct file_operations fops = {
  112. .llseek = llseek_f,
  113. };
  114. @ has_no_llseek @
  115. identifier fops0.fops;
  116. @@
  117. struct file_operations fops = {
  118. .llseek = no_llseek,
  119. };
  120. @ has_mmap @
  121. identifier fops0.fops;
  122. identifier mmap_f;
  123. @@
  124. struct file_operations fops = {
  125. .mmap = mmap_f,
  126. };
  127. @ has_copy_file_range @
  128. identifier fops0.fops;
  129. identifier copy_file_range_f;
  130. @@
  131. struct file_operations fops = {
  132. .copy_file_range = copy_file_range_f,
  133. };
  134. @ has_remap_file_range @
  135. identifier fops0.fops;
  136. identifier remap_file_range_f;
  137. @@
  138. struct file_operations fops = {
  139. .remap_file_range = remap_file_range_f,
  140. };
  141. @ has_splice_read @
  142. identifier fops0.fops;
  143. identifier splice_read_f;
  144. @@
  145. struct file_operations fops = {
  146. .splice_read = splice_read_f,
  147. };
  148. @ has_splice_write @
  149. identifier fops0.fops;
  150. identifier splice_write_f;
  151. @@
  152. struct file_operations fops = {
  153. .splice_write = splice_write_f,
  154. };
  155. // file_operations that is candidate for stream_open conversion - it does not
  156. // use mmap and other methods that assume @offset access to file.
  157. //
  158. // XXX for simplicity require no .{read/write}_iter and no .splice_{read/write} for now.
  159. // XXX maybe_steam.fops cannot be used in other rules - it gives "bad rule maybe_stream or bad variable fops".
  160. @ maybe_stream depends on (!has_llseek || has_no_llseek) && !has_mmap && !has_copy_file_range && !has_remap_file_range && !has_read_iter && !has_write_iter && !has_splice_read && !has_splice_write @
  161. identifier fops0.fops;
  162. @@
  163. struct file_operations fops = {
  164. };
  165. // ---- conversions ----
  166. // XXX .open = nonseekable_open -> .open = stream_open
  167. // XXX .open = func -> openfunc -> nonseekable_open
  168. // read & write
  169. //
  170. // if both are used in the same file_operations together with an opener -
  171. // under that conditions we can use stream_open instead of nonseekable_open.
  172. @ fops_rw depends on maybe_stream @
  173. identifier fops0.fops, openfunc;
  174. identifier stream_reader.readstream;
  175. identifier stream_writer.writestream;
  176. @@
  177. struct file_operations fops = {
  178. .open = openfunc,
  179. .read = readstream,
  180. .write = writestream,
  181. };
  182. @ report_rw depends on report @
  183. identifier fops_rw.openfunc;
  184. position p1;
  185. @@
  186. openfunc(...) {
  187. <...
  188. nonseekable_open@p1
  189. ...>
  190. }
  191. @ script:python depends on report && reader_blocks @
  192. fops << fops0.fops;
  193. p << report_rw.p1;
  194. @@
  195. coccilib.report.print_report(p[0],
  196. "ERROR: %s: .read() can deadlock .write(); change nonseekable_open -> stream_open to fix." % (fops,))
  197. @ script:python depends on report && !reader_blocks @
  198. fops << fops0.fops;
  199. p << report_rw.p1;
  200. @@
  201. coccilib.report.print_report(p[0],
  202. "WARNING: %s: .read() and .write() have stream semantic; safe to change nonseekable_open -> stream_open." % (fops,))
  203. @ explain_rw_deadlocked depends on explain && reader_blocks @
  204. identifier fops_rw.openfunc;
  205. @@
  206. openfunc(...) {
  207. <...
  208. - nonseekable_open
  209. + nonseekable_open /* read & write (was deadlock) */
  210. ...>
  211. }
  212. @ explain_rw_nodeadlock depends on explain && !reader_blocks @
  213. identifier fops_rw.openfunc;
  214. @@
  215. openfunc(...) {
  216. <...
  217. - nonseekable_open
  218. + nonseekable_open /* read & write (no direct deadlock) */
  219. ...>
  220. }
  221. @ patch_rw depends on patch @
  222. identifier fops_rw.openfunc;
  223. @@
  224. openfunc(...) {
  225. <...
  226. - nonseekable_open
  227. + stream_open
  228. ...>
  229. }
  230. // read, but not write
  231. @ fops_r depends on maybe_stream && !has_write @
  232. identifier fops0.fops, openfunc;
  233. identifier stream_reader.readstream;
  234. @@
  235. struct file_operations fops = {
  236. .open = openfunc,
  237. .read = readstream,
  238. };
  239. @ report_r depends on report @
  240. identifier fops_r.openfunc;
  241. position p1;
  242. @@
  243. openfunc(...) {
  244. <...
  245. nonseekable_open@p1
  246. ...>
  247. }
  248. @ script:python depends on report @
  249. fops << fops0.fops;
  250. p << report_r.p1;
  251. @@
  252. coccilib.report.print_report(p[0],
  253. "WARNING: %s: .read() has stream semantic; safe to change nonseekable_open -> stream_open." % (fops,))
  254. @ explain_r depends on explain @
  255. identifier fops_r.openfunc;
  256. @@
  257. openfunc(...) {
  258. <...
  259. - nonseekable_open
  260. + nonseekable_open /* read only */
  261. ...>
  262. }
  263. @ patch_r depends on patch @
  264. identifier fops_r.openfunc;
  265. @@
  266. openfunc(...) {
  267. <...
  268. - nonseekable_open
  269. + stream_open
  270. ...>
  271. }
  272. // write, but not read
  273. @ fops_w depends on maybe_stream && !has_read @
  274. identifier fops0.fops, openfunc;
  275. identifier stream_writer.writestream;
  276. @@
  277. struct file_operations fops = {
  278. .open = openfunc,
  279. .write = writestream,
  280. };
  281. @ report_w depends on report @
  282. identifier fops_w.openfunc;
  283. position p1;
  284. @@
  285. openfunc(...) {
  286. <...
  287. nonseekable_open@p1
  288. ...>
  289. }
  290. @ script:python depends on report @
  291. fops << fops0.fops;
  292. p << report_w.p1;
  293. @@
  294. coccilib.report.print_report(p[0],
  295. "WARNING: %s: .write() has stream semantic; safe to change nonseekable_open -> stream_open." % (fops,))
  296. @ explain_w depends on explain @
  297. identifier fops_w.openfunc;
  298. @@
  299. openfunc(...) {
  300. <...
  301. - nonseekable_open
  302. + nonseekable_open /* write only */
  303. ...>
  304. }
  305. @ patch_w depends on patch @
  306. identifier fops_w.openfunc;
  307. @@
  308. openfunc(...) {
  309. <...
  310. - nonseekable_open
  311. + stream_open
  312. ...>
  313. }
  314. // no read, no write - don't change anything