| From deb7eb64f0ce0c6f1a4cb2e3d5fe503cd942dea4 Mon Sep 17 00:00:00 2001 | |
| From: Sasha Levin <sashal@kernel.org> | |
| Date: Mon, 26 Oct 2026 12:11:28 -0600 | |
| Subject: ptp: unwind partially completed virtual clock creation | |
| From: James Hilliard <james.hilliard1@gmail.com> | |
| [ Upstream commit 3dea778260e3dddf30651a6913d87a42861346a2 ] | |
| n_vclocks_store() publishes the new clock count only after every child | |
| has been registered. If a later registration fails, it returns without | |
| unregistering the children already created by that request. Those clocks | |
| are left out of n_vclocks, and a subsequent request can overwrite their | |
| index entries without removing them. | |
| Unregister the newly created children in reverse order and clear their | |
| index entries on failure. Leave the previously installed clocks and | |
| their count unchanged. The n_vclocks mutex serializes this rollback with | |
| other sysfs changes, and parent removal drains the store before walking | |
| the children. | |
| This was found by code inspection while adding initial PHC sample error | |
| handling to virtual clock registration. | |
| Fixes: 73f37068d540 ("ptp: support ptp physical/virtual clocks conversion") | |
| Signed-off-by: James Hilliard <james.hilliard1@gmail.com> | |
| Link: https://patch.msgid.link/20261026-ptp-vclock-sampling-v6-2-e705612541f1@gmail.com | |
| Signed-off-by: Jakub Kicinski <kuba@kernel.org> | |
| Signed-off-by: Sasha Levin <sashal@kernel.org> | |
| --- | |
| drivers/ptp/ptp_sysfs.c | 8 +++++++- | |
| 1 file changed, 7 insertions(+), 1 deletion(-) | |
| diff --git a/drivers/ptp/ptp_sysfs.c b/drivers/ptp/ptp_sysfs.c | |
| index 1b4530be42a20..2d46f69e8c100 100644 | |
| --- a/drivers/ptp/ptp_sysfs.c | |
| +++ b/drivers/ptp/ptp_sysfs.c | |
| @@ -229,7 +229,7 @@ static ssize_t n_vclocks_store(struct device *dev, | |
| for (i = 0; i < num - ptp->n_vclocks; i++) { | |
| vclock = ptp_vclock_register(ptp); | |
| if (!vclock) | |
| - goto out; | |
| + goto err_register; | |
| *(ptp->vclock_index + ptp->n_vclocks + i) = | |
| vclock->clock->index; | |
| @@ -261,6 +261,12 @@ static ssize_t n_vclocks_store(struct device *dev, | |
| mutex_unlock(&ptp->n_vclocks_mux); | |
| return count; | |
| +err_register: | |
| + num = i; | |
| + if (num) | |
| + device_for_each_child_reverse(dev, &num, unregister_vclock); | |
| + for (num = 0; num < i; num++) | |
| + ptp->vclock_index[ptp->n_vclocks + num] = -1; | |
| out: | |
| mutex_unlock(&ptp->n_vclocks_mux); | |
| return err; | |
| -- | |
| 2.53.0 | |