Skip to content

Commit 5230ef6

Browse files
nefigtutbwhacks
authored andcommitted
ptp: fix the race between the release of ptp_clock and cdev
commit a33121e upstream. In a case when a ptp chardev (like /dev/ptp0) is open but an underlying device is removed, closing this file leads to a race. This reproduces easily in a kvm virtual machine: ts# cat openptp0.c int main() { ... fp = fopen("/dev/ptp0", "r"); ... sleep(10); } ts# uname -r 5.5.0-rc3-46cf053e ts# cat /proc/cmdline ... slub_debug=FZP ts# modprobe ptp_kvm ts# ./openptp0 & [1] 670 opened /dev/ptp0, sleeping 10s... ts# rmmod ptp_kvm ts# ls /dev/ptp* ls: cannot access '/dev/ptp*': No such file or directory ts# ...woken up [ 48.010809] general protection fault: 0000 [#1] SMP [ 48.012502] CPU: 6 PID: 658 Comm: openptp0 Not tainted 5.5.0-rc3-46cf053e #25 [ 48.014624] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), ... [ 48.016270] RIP: 0010:module_put.part.0+0x7/0x80 [ 48.017939] RSP: 0018:ffffb3850073be00 EFLAGS: 00010202 [ 48.018339] RAX: 000000006b6b6b6b RBX: 6b6b6b6b6b6b6b6b RCX: ffff89a476c00ad0 [ 48.018936] RDX: fffff65a08d3ea08 RSI: 0000000000000247 RDI: 6b6b6b6b6b6b6b6b [ 48.019470] ... ^^^ a slub poison [ 48.023854] Call Trace: [ 48.024050] __fput+0x21f/0x240 [ 48.024288] task_work_run+0x79/0x90 [ 48.024555] do_exit+0x2af/0xab0 [ 48.024799] ? vfs_write+0x16a/0x190 [ 48.025082] do_group_exit+0x35/0x90 [ 48.025387] __x64_sys_exit_group+0xf/0x10 [ 48.025737] do_syscall_64+0x3d/0x130 [ 48.026056] entry_SYSCALL_64_after_hwframe+0x44/0xa9 [ 48.026479] RIP: 0033:0x7f53b12082f6 [ 48.026792] ... [ 48.030945] Modules linked in: ptp i6300esb watchdog [last unloaded: ptp_kvm] [ 48.045001] Fixing recursive fault but reboot is needed! This happens in: static void __fput(struct file *file) { ... if (file->f_op->release) file->f_op->release(inode, file); <<< cdev is kfree'd here if (unlikely(S_ISCHR(inode->i_mode) && inode->i_cdev != NULL && !(mode & FMODE_PATH))) { cdev_put(inode->i_cdev); <<< cdev fields are accessed here Namely: __fput() posix_clock_release() kref_put(&clk->kref, delete_clock) <<< the last reference delete_clock() delete_ptp_clock() kfree(ptp) <<< cdev is embedded in ptp cdev_put module_put(p->owner) <<< *p is kfree'd, bang! Here cdev is embedded in posix_clock which is embedded in ptp_clock. The race happens because ptp_clock's lifetime is controlled by two refcounts: kref and cdev.kobj in posix_clock. This is wrong. Make ptp_clock's sysfs device a parent of cdev with cdev_device_add() created especially for such cases. This way the parent device with its ptp_clock is not released until all references to the cdev are released. This adds a requirement that an initialized but not exposed struct device should be provided to posix_clock_register() by a caller instead of a simple dev_t. This approach was adopted from the commit 72139df ("watchdog: Fix the race between the release of watchdog_core_data and cdev"). See details of the implementation in the commit 233ed09 ("chardev: add helper function to register char devs with a struct device"). Link: https://lore.kernel.org/linux-fsdevel/[email protected]/T/#u Analyzed-by: Stephen Johnston <[email protected]> Analyzed-by: Vern Lovejoy <[email protected]> Signed-off-by: Vladis Dronov <[email protected]> Acked-by: Richard Cochran <[email protected]> Signed-off-by: David S. Miller <[email protected]> Signed-off-by: Ben Hutchings <[email protected]>
1 parent 4561df9 commit 5230ef6

File tree

4 files changed

+39
-44
lines changed

4 files changed

+39
-44
lines changed

drivers/ptp/ptp_clock.c

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,9 @@ static struct posix_clock_operations ptp_clock_ops = {
167167
.read = ptp_read,
168168
};
169169

170-
static void delete_ptp_clock(struct posix_clock *pc)
170+
static void ptp_clock_release(struct device *dev)
171171
{
172-
struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
172+
struct ptp_clock *ptp = container_of(dev, struct ptp_clock, dev);
173173

174174
mutex_destroy(&ptp->tsevq_mux);
175175
mutex_destroy(&ptp->pincfg_mux);
@@ -201,7 +201,6 @@ struct ptp_clock *ptp_clock_register(struct ptp_clock_info *info,
201201
}
202202

203203
ptp->clock.ops = ptp_clock_ops;
204-
ptp->clock.release = delete_ptp_clock;
205204
ptp->info = info;
206205
ptp->devid = MKDEV(major, index);
207206
ptp->index = index;
@@ -214,15 +213,6 @@ struct ptp_clock *ptp_clock_register(struct ptp_clock_info *info,
214213
if (err)
215214
goto no_pin_groups;
216215

217-
/* Create a new device in our class. */
218-
ptp->dev = device_create_with_groups(ptp_class, parent, ptp->devid,
219-
ptp, ptp->pin_attr_groups,
220-
"ptp%d", ptp->index);
221-
if (IS_ERR(ptp->dev)) {
222-
err = PTR_ERR(ptp->dev);
223-
goto no_device;
224-
}
225-
226216
/* Register a new PPS source. */
227217
if (info->pps) {
228218
struct pps_source_info pps;
@@ -238,8 +228,18 @@ struct ptp_clock *ptp_clock_register(struct ptp_clock_info *info,
238228
}
239229
}
240230

241-
/* Create a posix clock. */
242-
err = posix_clock_register(&ptp->clock, ptp->devid);
231+
/* Initialize a new device of our class in our clock structure. */
232+
device_initialize(&ptp->dev);
233+
ptp->dev.devt = ptp->devid;
234+
ptp->dev.class = ptp_class;
235+
ptp->dev.parent = parent;
236+
ptp->dev.groups = ptp->pin_attr_groups;
237+
ptp->dev.release = ptp_clock_release;
238+
dev_set_drvdata(&ptp->dev, ptp);
239+
dev_set_name(&ptp->dev, "ptp%d", ptp->index);
240+
241+
/* Create a posix clock and link it to the device. */
242+
err = posix_clock_register(&ptp->clock, &ptp->dev);
243243
if (err) {
244244
pr_err("failed to create posix clock\n");
245245
goto no_clock;
@@ -251,8 +251,6 @@ struct ptp_clock *ptp_clock_register(struct ptp_clock_info *info,
251251
if (ptp->pps_source)
252252
pps_unregister_source(ptp->pps_source);
253253
no_pps:
254-
device_destroy(ptp_class, ptp->devid);
255-
no_device:
256254
ptp_cleanup_pin_groups(ptp);
257255
no_pin_groups:
258256
mutex_destroy(&ptp->tsevq_mux);
@@ -273,7 +271,6 @@ int ptp_clock_unregister(struct ptp_clock *ptp)
273271
if (ptp->pps_source)
274272
pps_unregister_source(ptp->pps_source);
275273

276-
device_destroy(ptp_class, ptp->devid);
277274
ptp_cleanup_pin_groups(ptp);
278275

279276
posix_clock_unregister(&ptp->clock);

drivers/ptp/ptp_private.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ struct timestamp_event_queue {
4040

4141
struct ptp_clock {
4242
struct posix_clock clock;
43-
struct device *dev;
43+
struct device dev;
4444
struct ptp_clock_info *info;
4545
dev_t devid;
4646
int index; /* index into clocks.map */

include/linux/posix-clock.h

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,29 +104,32 @@ struct posix_clock_operations {
104104
*
105105
* @ops: Functional interface to the clock
106106
* @cdev: Character device instance for this clock
107-
* @kref: Reference count.
107+
* @dev: Pointer to the clock's device.
108108
* @rwsem: Protects the 'zombie' field from concurrent access.
109109
* @zombie: If 'zombie' is true, then the hardware has disappeared.
110-
* @release: A function to free the structure when the reference count reaches
111-
* zero. May be NULL if structure is statically allocated.
112110
*
113111
* Drivers should embed their struct posix_clock within a private
114112
* structure, obtaining a reference to it during callbacks using
115113
* container_of().
114+
*
115+
* Drivers should supply an initialized but not exposed struct device
116+
* to posix_clock_register(). It is used to manage lifetime of the
117+
* driver's private structure. It's 'release' field should be set to
118+
* a release function for this private structure.
116119
*/
117120
struct posix_clock {
118121
struct posix_clock_operations ops;
119122
struct cdev cdev;
120-
struct kref kref;
123+
struct device *dev;
121124
struct rw_semaphore rwsem;
122125
bool zombie;
123-
void (*release)(struct posix_clock *clk);
124126
};
125127

126128
/**
127129
* posix_clock_register() - register a new clock
128-
* @clk: Pointer to the clock. Caller must provide 'ops' and 'release'
129-
* @devid: Allocated device id
130+
* @clk: Pointer to the clock. Caller must provide 'ops' field
131+
* @dev: Pointer to the initialized device. Caller must provide
132+
* 'release' field
130133
*
131134
* A clock driver calls this function to register itself with the
132135
* clock device subsystem. If 'clk' points to dynamically allocated
@@ -135,7 +138,7 @@ struct posix_clock {
135138
*
136139
* Returns zero on success, non-zero otherwise.
137140
*/
138-
int posix_clock_register(struct posix_clock *clk, dev_t devid);
141+
int posix_clock_register(struct posix_clock *clk, struct device *dev);
139142

140143
/**
141144
* posix_clock_unregister() - unregister a clock

kernel/time/posix-clock.c

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@
2525
#include <linux/syscalls.h>
2626
#include <linux/uaccess.h>
2727

28-
static void delete_clock(struct kref *kref);
29-
3028
/*
3129
* Returns NULL if the posix_clock instance attached to 'fp' is old and stale.
3230
*/
@@ -168,7 +166,7 @@ static int posix_clock_open(struct inode *inode, struct file *fp)
168166
err = 0;
169167

170168
if (!err) {
171-
kref_get(&clk->kref);
169+
get_device(clk->dev);
172170
fp->private_data = clk;
173171
}
174172
out:
@@ -184,7 +182,7 @@ static int posix_clock_release(struct inode *inode, struct file *fp)
184182
if (clk->ops.release)
185183
err = clk->ops.release(clk);
186184

187-
kref_put(&clk->kref, delete_clock);
185+
put_device(clk->dev);
188186

189187
fp->private_data = NULL;
190188

@@ -206,38 +204,35 @@ static const struct file_operations posix_clock_file_operations = {
206204
#endif
207205
};
208206

209-
int posix_clock_register(struct posix_clock *clk, dev_t devid)
207+
int posix_clock_register(struct posix_clock *clk, struct device *dev)
210208
{
211209
int err;
212210

213-
kref_init(&clk->kref);
214211
init_rwsem(&clk->rwsem);
215212

216213
cdev_init(&clk->cdev, &posix_clock_file_operations);
214+
err = cdev_device_add(&clk->cdev, dev);
215+
if (err) {
216+
pr_err("%s unable to add device %d:%d\n",
217+
dev_name(dev), MAJOR(dev->devt), MINOR(dev->devt));
218+
return err;
219+
}
217220
clk->cdev.owner = clk->ops.owner;
218-
err = cdev_add(&clk->cdev, devid, 1);
221+
clk->dev = dev;
219222

220-
return err;
223+
return 0;
221224
}
222225
EXPORT_SYMBOL_GPL(posix_clock_register);
223226

224-
static void delete_clock(struct kref *kref)
225-
{
226-
struct posix_clock *clk = container_of(kref, struct posix_clock, kref);
227-
228-
if (clk->release)
229-
clk->release(clk);
230-
}
231-
232227
void posix_clock_unregister(struct posix_clock *clk)
233228
{
234-
cdev_del(&clk->cdev);
229+
cdev_device_del(&clk->cdev, clk->dev);
235230

236231
down_write(&clk->rwsem);
237232
clk->zombie = true;
238233
up_write(&clk->rwsem);
239234

240-
kref_put(&clk->kref, delete_clock);
235+
put_device(clk->dev);
241236
}
242237
EXPORT_SYMBOL_GPL(posix_clock_unregister);
243238

0 commit comments

Comments
 (0)