[tip: timers/core] posix-clock: Initialize cdev state before publishing

From: tip-bot2 for Haoli Zhang

Date: Mon Nov 30 2026 - 10:12:41 EST


The following commit has been merged into the timers/core branch of tip:

Commit-ID: 8db547d3ecd3a1ca528839c43a37371330835eea
Gitweb: https://git.kernel.org/tip/8db547d3ecd3a1ca528839c43a37371330835eea
Author: Haoli Zhang <26031212468@xxxxxxxxxxxxxxxxx>
AuthorDate: Mon, 16 Nov 2026 11:28:53 +08:00
Committer: Thomas Gleixner <tglx@xxxxxxxxxx>
CommitterDate: Mon, 30 Nov 2026 16:08:42 +01:00

posix-clock: Initialize cdev state before publishing

cdev_device_add() may expose the cdev before it returns. Initialize
clk->cdev.owner and clk->dev before calling it, so an open racing with
registration takes the matching module and device references.

If cdev_device_add() fails after cdev_add() made the cdev available,
open files may still exist. Mark the clock as a zombie under rwsem before
returning the error, so subsequent read, poll, ioctl, and dynamic clock
operations on those files fail while the caller tears down registration
state. Keep clk->dev valid so the release path can drop any reference
acquired by a successful open.

Fixes: a33121e5487b ("ptp: fix the race between the release of ptp_clock and cdev")
Reported-by: syzbot+667788430769ef9ddf25@xxxxxxxxxxxxxxxxxxxxxxxxx
Co-developed-by: Jinning Zhang <25031212117@xxxxxxxxxxxxxxxxx>
Signed-off-by: Jinning Zhang <25031212117@xxxxxxxxxxxxxxxxx>
Co-developed-by: Bin Yu <byu@xxxxxxxxxxxxx>
Signed-off-by: Bin Yu <byu@xxxxxxxxxxxxx>
Signed-off-by: Haoli Zhang <26031212468@xxxxxxxxxxxxxxxxx>
Signed-off-by: Thomas Gleixner <tglx@xxxxxxxxxx>
Cc: stable@xxxxxxxxxxxxxxx
Link: https://patch.msgid.link/20261116032853.825-1-26031212468@xxxxxxxxxxxxxxxxx
Closes: https://syzbot.org/bug?extid=667788430769ef9ddf25
---
kernel/time/posix-clock.c | 8 ++++++--
1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/kernel/time/posix-clock.c b/kernel/time/posix-clock.c
index dab3729..0874a66 100644
--- a/kernel/time/posix-clock.c
+++ b/kernel/time/posix-clock.c
@@ -164,14 +164,18 @@ int posix_clock_register(struct posix_clock *clk, struct device *dev)
init_rwsem(&clk->rwsem);

cdev_init(&clk->cdev, &posix_clock_file_operations);
+ /* cdev_device_add() can expose the cdev before it returns. */
+ clk->cdev.owner = clk->ops.owner;
+ clk->dev = dev;
err = cdev_device_add(&clk->cdev, dev);
if (err) {
pr_err("%s unable to add device %d:%d\n",
dev_name(dev), MAJOR(dev->devt), MINOR(dev->devt));
+ down_write(&clk->rwsem);
+ clk->zombie = true;
+ up_write(&clk->rwsem);
return err;
}
- clk->cdev.owner = clk->ops.owner;
- clk->dev = dev;

return 0;
}