stream_open.cocci 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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 =~ "^wait_.*";
  34. @@
  35. block_f(...) {
  36. ... when exists
  37. wait(...)
  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 =~ "^wait_.*";
  47. @@
  48. readstream(...)
  49. {
  50. ... when exists
  51. wait(...)
  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. };
  119. @ has_noop_llseek @
  120. identifier fops0.fops;
  121. @@
  122. struct file_operations fops = {
  123. .llseek = noop_llseek,
  124. };
  125. @ has_mmap @
  126. identifier fops0.fops;
  127. identifier mmap_f;
  128. @@
  129. struct file_operations fops = {
  130. .mmap = mmap_f,
  131. };
  132. @ has_copy_file_range @
  133. identifier fops0.fops;
  134. identifier copy_file_range_f;
  135. @@
  136. struct file_operations fops = {
  137. .copy_file_range = copy_file_range_f,
  138. };
  139. @ has_remap_file_range @
  140. identifier fops0.fops;
  141. identifier remap_file_range_f;
  142. @@
  143. struct file_operations fops = {
  144. .remap_file_range = remap_file_range_f,
  145. };
  146. @ has_splice_read @
  147. identifier fops0.fops;
  148. identifier splice_read_f;
  149. @@
  150. struct file_operations fops = {
  151. .splice_read = splice_read_f,
  152. };
  153. @ has_splice_write @
  154. identifier fops0.fops;
  155. identifier splice_write_f;
  156. @@
  157. struct file_operations fops = {
  158. .splice_write = splice_write_f,
  159. };
  160. // file_operations that is candidate for stream_open conversion - it does not
  161. // use mmap and other methods that assume @offset access to file.
  162. //
  163. // XXX for simplicity require no .{read/write}_iter and no .splice_{read/write} for now.
  164. // XXX maybe_steam.fops cannot be used in other rules - it gives "bad rule maybe_stream or bad variable fops".
  165. @ maybe_stream depends on (!has_llseek || has_no_llseek || has_noop_llseek) && !has_mmap && !has_copy_file_range && !has_remap_file_range && !has_read_iter && !has_write_iter && !has_splice_read && !has_splice_write @
  166. identifier fops0.fops;
  167. @@
  168. struct file_operations fops = {
  169. };
  170. // ---- conversions ----
  171. // XXX .open = nonseekable_open -> .open = stream_open
  172. // XXX .open = func -> openfunc -> nonseekable_open
  173. // read & write
  174. //
  175. // if both are used in the same file_operations together with an opener -
  176. // under that conditions we can use stream_open instead of nonseekable_open.
  177. @ fops_rw depends on maybe_stream @
  178. identifier fops0.fops, openfunc;
  179. identifier stream_reader.readstream;
  180. identifier stream_writer.writestream;
  181. @@
  182. struct file_operations fops = {
  183. .open = openfunc,
  184. .read = readstream,
  185. .write = writestream,
  186. };
  187. @ report_rw depends on report @
  188. identifier fops_rw.openfunc;
  189. position p1;
  190. @@
  191. openfunc(...) {
  192. <...
  193. nonseekable_open@p1
  194. ...>
  195. }
  196. @ script:python depends on report && reader_blocks @
  197. fops << fops0.fops;
  198. p << report_rw.p1;
  199. @@
  200. coccilib.report.print_report(p[0],
  201. "ERROR: %s: .read() can deadlock .write(); change nonseekable_open -> stream_open to fix." % (fops,))
  202. @ script:python depends on report && !reader_blocks @
  203. fops << fops0.fops;
  204. p << report_rw.p1;
  205. @@
  206. coccilib.report.print_report(p[0],
  207. "WARNING: %s: .read() and .write() have stream semantic; safe to change nonseekable_open -> stream_open." % (fops,))
  208. @ explain_rw_deadlocked depends on explain && reader_blocks @
  209. identifier fops_rw.openfunc;
  210. @@
  211. openfunc(...) {
  212. <...
  213. - nonseekable_open
  214. + nonseekable_open /* read & write (was deadlock) */
  215. ...>
  216. }
  217. @ explain_rw_nodeadlock depends on explain && !reader_blocks @
  218. identifier fops_rw.openfunc;
  219. @@
  220. openfunc(...) {
  221. <...
  222. - nonseekable_open
  223. + nonseekable_open /* read & write (no direct deadlock) */
  224. ...>
  225. }
  226. @ patch_rw depends on patch @
  227. identifier fops_rw.openfunc;
  228. @@
  229. openfunc(...) {
  230. <...
  231. - nonseekable_open
  232. + stream_open
  233. ...>
  234. }
  235. // read, but not write
  236. @ fops_r depends on maybe_stream && !has_write @
  237. identifier fops0.fops, openfunc;
  238. identifier stream_reader.readstream;
  239. @@
  240. struct file_operations fops = {
  241. .open = openfunc,
  242. .read = readstream,
  243. };
  244. @ report_r depends on report @
  245. identifier fops_r.openfunc;
  246. position p1;
  247. @@
  248. openfunc(...) {
  249. <...
  250. nonseekable_open@p1
  251. ...>
  252. }
  253. @ script:python depends on report @
  254. fops << fops0.fops;
  255. p << report_r.p1;
  256. @@
  257. coccilib.report.print_report(p[0],
  258. "WARNING: %s: .read() has stream semantic; safe to change nonseekable_open -> stream_open." % (fops,))
  259. @ explain_r depends on explain @
  260. identifier fops_r.openfunc;
  261. @@
  262. openfunc(...) {
  263. <...
  264. - nonseekable_open
  265. + nonseekable_open /* read only */
  266. ...>
  267. }
  268. @ patch_r depends on patch @
  269. identifier fops_r.openfunc;
  270. @@
  271. openfunc(...) {
  272. <...
  273. - nonseekable_open
  274. + stream_open
  275. ...>
  276. }
  277. // write, but not read
  278. @ fops_w depends on maybe_stream && !has_read @
  279. identifier fops0.fops, openfunc;
  280. identifier stream_writer.writestream;
  281. @@
  282. struct file_operations fops = {
  283. .open = openfunc,
  284. .write = writestream,
  285. };
  286. @ report_w depends on report @
  287. identifier fops_w.openfunc;
  288. position p1;
  289. @@
  290. openfunc(...) {
  291. <...
  292. nonseekable_open@p1
  293. ...>
  294. }
  295. @ script:python depends on report @
  296. fops << fops0.fops;
  297. p << report_w.p1;
  298. @@
  299. coccilib.report.print_report(p[0],
  300. "WARNING: %s: .write() has stream semantic; safe to change nonseekable_open -> stream_open." % (fops,))
  301. @ explain_w depends on explain @
  302. identifier fops_w.openfunc;
  303. @@
  304. openfunc(...) {
  305. <...
  306. - nonseekable_open
  307. + nonseekable_open /* write only */
  308. ...>
  309. }
  310. @ patch_w depends on patch @
  311. identifier fops_w.openfunc;
  312. @@
  313. openfunc(...) {
  314. <...
  315. - nonseekable_open
  316. + stream_open
  317. ...>
  318. }
  319. // no read, no write - don't change anything