Skip to content

Commit

Permalink
*: convert stream-like files -> stream_open, even if they use noop_ll…
Browse files Browse the repository at this point in the history
…seek

This patch continues 10dce8a (fs: stream_open - opener for
stream-like files so that read and write can run simultaneously without
deadlock) and c5bf68f (*: convert stream-like files from
nonseekable_open -> stream_open) and teaches steam_open.cocci to
consider files as being stream-like not only if they have
.llseek=no_llseek, but also if they have .llseek=noop_llseek.

This is safe to do: the comment about noop_llseek says

	This is an implementation of ->llseek useable for the rare special case when
	userspace expects the seek to succeed but the (device) file is actually not
	able to perform the seek. In this case you use noop_llseek() instead of
	falling back to the default implementation of ->llseek.

and in general noop_llseek was massively added to drivers in 6038f37
(llseek: automatically add .llseek fop) when changing default for NULL .llseek
from NOP to no_llseek with the idea to avoid breaking compatibility, if
maybe some user-space program was using lseek on a device without caring
about the result, but caring if it was an error or not.

Amended semantic patch produces two changes when applied tree-wide:

        drivers/hid/hid-sensor-custom.c:690:8-24: WARNING: hid_sensor_custom_fops: .read() has stream semantic; safe to change nonseekable_open -> stream_open.
        drivers/input/mousedev.c:564:1-17: ERROR: mousedev_fops: .read() can deadlock .write(); change nonseekable_open -> stream_open to fix.

Cc: Julia Lawall <[email protected]>
Cc: Jan Blunck <[email protected]>
Cc: Arnd Bergmann <[email protected]>
Cc: Jiri Kosina <[email protected]>
Cc: Jonathan Cameron <[email protected]>
Cc: Srinivas Pandruvada <[email protected]>
Cc: Benjamin Tissoires <[email protected]>
Cc: Dmitry Torokhov <[email protected]>
Signed-off-by: Kirill Smelkov <[email protected]>
  • Loading branch information
navytux committed Jul 14, 2019
1 parent 0ecfebd commit 3975b09
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
2 changes: 1 addition & 1 deletion drivers/hid/hid-sensor-custom.c
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,7 @@ static int hid_sensor_custom_open(struct inode *inode, struct file *file)
if (test_and_set_bit(0, &sensor_inst->misc_opened))
return -EBUSY;

return nonseekable_open(inode, file);
return stream_open(inode, file);
}

static __poll_t hid_sensor_custom_poll(struct file *file,
Expand Down
2 changes: 1 addition & 1 deletion drivers/input/mousedev.c
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ static int mousedev_open(struct inode *inode, struct file *file)
goto err_free_client;

file->private_data = client;
nonseekable_open(inode, file);
stream_open(inode, file);

return 0;

Expand Down
9 changes: 8 additions & 1 deletion scripts/coccinelle/api/stream_open.cocci
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,13 @@ identifier fops0.fops;
.llseek = no_llseek,
};

@ has_noop_llseek @
identifier fops0.fops;
@@
struct file_operations fops = {
.llseek = noop_llseek,
};

@ has_mmap @
identifier fops0.fops;
identifier mmap_f;
Expand Down Expand Up @@ -180,7 +187,7 @@ identifier splice_write_f;
//
// XXX for simplicity require no .{read/write}_iter and no .splice_{read/write} for now.
// XXX maybe_steam.fops cannot be used in other rules - it gives "bad rule maybe_stream or bad variable fops".
@ 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 @
@ 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 @
identifier fops0.fops;
@@
struct file_operations fops = {
Expand Down

0 comments on commit 3975b09

Please sign in to comment.