1 // SPDX-License-Identifier: GPL-2.0
2 // Author: Kirill Smelkov (kirr@nexedi.com)
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.
11 virtual explain // explain decisions in the patch (SPFLAGS="-D explain")
13 // stream-like reader & writer - ones that do not depend on f_pos.
15 identifier readstream, ppos;
16 identifier f, buf, len;
19 ssize_t readstream(struct file *f, char *buf, size_t len, loff_t *ppos)
25 identifier writestream, ppos;
26 identifier f, buf, len;
29 ssize_t writestream(struct file *f, const char *buf, size_t len, loff_t *ppos)
35 // a function that blocks
38 identifier wait =~ "^wait_.*";
46 // stream_reader that can block inside.
48 // XXX wait_* can be called not directly from current function (e.g. func -> f -> g -> wait())
49 // XXX currently reader_blocks supports only direct and 1-level indirect cases.
50 @ reader_blocks_direct @
51 identifier stream_reader.readstream;
52 identifier wait =~ "^wait_.*";
62 identifier stream_reader.readstream;
63 identifier blocks.block_f;
72 @ reader_blocks depends on reader_blocks_direct || reader_blocks_1 @
73 identifier stream_reader.readstream;
80 // file_operations + whether they have _any_ .read, .write, .llseek ... at all.
82 // XXX add support for file_operations xxx[N] = ... (sound/core/pcm_native.c)
86 struct file_operations fops = {
91 identifier fops0.fops;
94 struct file_operations fops = {
99 identifier fops0.fops;
100 identifier read_iter_f;
102 struct file_operations fops = {
103 .read_iter = read_iter_f,
107 identifier fops0.fops;
110 struct file_operations fops = {
115 identifier fops0.fops;
116 identifier write_iter_f;
118 struct file_operations fops = {
119 .write_iter = write_iter_f,
123 identifier fops0.fops;
126 struct file_operations fops = {
131 identifier fops0.fops;
133 struct file_operations fops = {
137 identifier fops0.fops;
139 struct file_operations fops = {
140 .llseek = noop_llseek,
144 identifier fops0.fops;
147 struct file_operations fops = {
151 @ has_copy_file_range @
152 identifier fops0.fops;
153 identifier copy_file_range_f;
155 struct file_operations fops = {
156 .copy_file_range = copy_file_range_f,
159 @ has_remap_file_range @
160 identifier fops0.fops;
161 identifier remap_file_range_f;
163 struct file_operations fops = {
164 .remap_file_range = remap_file_range_f,
168 identifier fops0.fops;
169 identifier splice_read_f;
171 struct file_operations fops = {
172 .splice_read = splice_read_f,
176 identifier fops0.fops;
177 identifier splice_write_f;
179 struct file_operations fops = {
180 .splice_write = splice_write_f,
184 // file_operations that is candidate for stream_open conversion - it does not
185 // use mmap and other methods that assume @offset access to file.
187 // XXX for simplicity require no .{read/write}_iter and no .splice_{read/write} for now.
188 // XXX maybe_steam.fops cannot be used in other rules - it gives "bad rule maybe_stream or bad variable fops".
189 @ 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 @
190 identifier fops0.fops;
192 struct file_operations fops = {
196 // ---- conversions ----
198 // XXX .open = nonseekable_open -> .open = stream_open
199 // XXX .open = func -> openfunc -> nonseekable_open
203 // if both are used in the same file_operations together with an opener -
204 // under that conditions we can use stream_open instead of nonseekable_open.
205 @ fops_rw depends on maybe_stream @
206 identifier fops0.fops, openfunc;
207 identifier stream_reader.readstream;
208 identifier stream_writer.writestream;
210 struct file_operations fops = {
213 .write = writestream,
216 @ report_rw depends on report @
217 identifier fops_rw.openfunc;
226 @ script:python depends on report && reader_blocks @
230 coccilib.report.print_report(p[0],
231 "ERROR: %s: .read() can deadlock .write(); change nonseekable_open -> stream_open to fix." % (fops,))
233 @ script:python depends on report && !reader_blocks @
237 coccilib.report.print_report(p[0],
238 "WARNING: %s: .read() and .write() have stream semantic; safe to change nonseekable_open -> stream_open." % (fops,))
241 @ explain_rw_deadlocked depends on explain && reader_blocks @
242 identifier fops_rw.openfunc;
247 + nonseekable_open /* read & write (was deadlock) */
252 @ explain_rw_nodeadlock depends on explain && !reader_blocks @
253 identifier fops_rw.openfunc;
258 + nonseekable_open /* read & write (no direct deadlock) */
262 @ patch_rw depends on patch @
263 identifier fops_rw.openfunc;
273 // read, but not write
274 @ fops_r depends on maybe_stream && !has_write @
275 identifier fops0.fops, openfunc;
276 identifier stream_reader.readstream;
278 struct file_operations fops = {
283 @ report_r depends on report @
284 identifier fops_r.openfunc;
293 @ script:python depends on report @
297 coccilib.report.print_report(p[0],
298 "WARNING: %s: .read() has stream semantic; safe to change nonseekable_open -> stream_open." % (fops,))
300 @ explain_r depends on explain @
301 identifier fops_r.openfunc;
306 + nonseekable_open /* read only */
310 @ patch_r depends on patch @
311 identifier fops_r.openfunc;
321 // write, but not read
322 @ fops_w depends on maybe_stream && !has_read @
323 identifier fops0.fops, openfunc;
324 identifier stream_writer.writestream;
326 struct file_operations fops = {
328 .write = writestream,
331 @ report_w depends on report @
332 identifier fops_w.openfunc;
341 @ script:python depends on report @
345 coccilib.report.print_report(p[0],
346 "WARNING: %s: .write() has stream semantic; safe to change nonseekable_open -> stream_open." % (fops,))
348 @ explain_w depends on explain @
349 identifier fops_w.openfunc;
354 + nonseekable_open /* write only */
358 @ patch_w depends on patch @
359 identifier fops_w.openfunc;
369 // no read, no write - don't change anything