Skip to content

Commit 0b509d8

Browse files
tomvanbraeckelgregkh
authored andcommitted
misc: pass miscdevice through file's private_data
Make the miscdevice accessible through the file's private_data. Previously, this was done only when an open() file operation had been registered. If no custom open() file operation was defined, private_data was set to NULL. This subtle quirk was confusing, to the point where kernel code registered *empty* file open operations to have private_data point to the misc device structure and avoid duplicating that logic. And it could easily lead to bugs, where the addition or removal of a custom open() file operation surprisingly changes the initial value of a file's private_data structure. To resolve this, we now place the miscdevice in the file's private_data member unconditionally when open() is called. Signed-off-by: Tom Van Braeckel <tomvanbraeckel@gmail.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 16c9c8e commit 0b509d8

1 file changed

Lines changed: 8 additions & 3 deletions

File tree

‎drivers/char/misc.c‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,17 @@ static int misc_open(struct inode * inode, struct file * file)
140140
goto fail;
141141
}
142142

143+
/*
144+
* Place the miscdevice in the file's
145+
* private_data so it can be used by the
146+
* file operations, including f_op->open below
147+
*/
148+
file->private_data = c;
149+
143150
err = 0;
144151
replace_fops(file, new_fops);
145-
if (file->f_op->open) {
146-
file->private_data = c;
152+
if (file->f_op->open)
147153
err = file->f_op->open(inode,file);
148-
}
149154
fail:
150155
mutex_unlock(&misc_mtx);
151156
return err;

0 commit comments

Comments
 (0)