PCI/PTM: fix refcount leak in pci_enable_ptm()
Subsystems (2)
PATCH 1/1 PCI/PTM: fix refcount leak in pci_enable_ptm()
When pci_enable_ptm() enables PTM for a device, it first recursively calls itself on the parent. This increments the parent's ptm_enable_cnt. If the subsequent __pci_enable_ptm() call on the child fails, the error path only decrements the child's ptm_enable_cnt but leaves the parent's counter elevated. That refcount is never balanced, leading to a leak.
Fix this by calling pci_disable_ptm() for the parent on the error path, reverting the parent's enable state. Add the call right after the child's counter is decremented.
--- drivers/pci/pcie/ptm.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/pci/pcie/ptm.c +2 -0
@@ -201,6 +201,8 @@ int pci_enable_ptm(struct pci_dev *dev) rc =__pci_enable_ptm (dev);if (rc) { +if (!dev->ptm_root) +pci_disable_ptm (parent);atomic_dec (&dev->ptm_enable_cnt);return rc; }
Signature · 1 line
2.34.1
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
New issues:
- [Low] The patch introduces a strict compilation failure because the variable parent is referenced outside of its declared scope in the error path.
Pre-existing issues:
- [High] Concurrent calls to pci_enable_ptm() can lead to a lockless TOCTOU race condition where a caller incorrectly assumes PTM is enabled before the hardware initialization completes. --
commit 98f3a56b29bf607b6f9174e20f676b1549c2aaaa
Author: Wentao Liang <[email protected]>
PCI/PTM: fix refcount leak in pci_enable_ptm()
This commit addresses a refcount leak in pci_enable_ptm() when the function fails to enable PTM on a child device. It does this by adding a call to pci_disable_ptm() for the parent device in the error path.
drivers/pci/pcie/ptm.c int pci_enable_ptm(struct pci_dev *dev) · line 202
Quoted 7 lines
drivers/pci/pcie/ptm.c +0
-0@@ -201,6 +201,8 @@ int pci_enable_ptm(struct pci_dev *dev) rc =__pci_enable_ptm (dev);
[Severity: High]
This is a pre-existing issue, but can the lockless check just above this call lead to a race condition?
Looking at pci_enable_ptm(), the counter check bypasses hardware configuration locklessly:
if (atomic_inc_return(&dev->ptm_enable_cnt) > 1)
return 0;
rc = __pci_enable_ptm(dev);If multiple downstream devices probe concurrently, two threads might call pci_enable_ptm() on their shared parent simultaneously. If the second thread increments the counter to 2 and returns immediately, it will assume the parent is fully initialized. It could then proceed to configure the child before the first thread finishes hardware configuration in __pci_enable_ptm(), potentially causing Unsupported Request errors or bus resets.
drivers/pci/pcie/ptm.c int pci_enable_ptm(struct pci_dev *dev) · line 205
Quoted 3 lines
drivers/pci/pcie/ptm.c +2
-0if (rc) { +if (!dev->ptm_root) +pci_disable_ptm (parent);
[Severity: Low]
Does this cause a build failure? The variable parent is referenced here, but looking at pci_enable_ptm(), parent is declared locally inside an earlier if block:
if (!dev->ptm_root) {
struct pci_dev *parent;
parent = pci_upstream_ptm(dev);
...
}This makes parent out of scope in this error path, which will break the build.
Quoted 3 lines
drivers/pci/pcie/ptm.c +0
-0atomic_dec (&dev->ptm_enable_cnt);return rc; }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
kernel test robot
build botkernel test robot noticed the following build errors:
Hi Wentao,
kernel test robot noticed the following build errors:
[auto build test ERROR on pci/next]
[also build test ERROR on pci/for-linus westeri-thunderbolt/next linus/master v7.1 next-20260616] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Wentao-Liang/PCI-PTM-fix-refcount-leak-in-pci_enable_ptm/20260617-043856 base: https://git.kernel.org/pub/scm/linux/kernel/git/pci/pci.git next patch link: https://lore.kernel.org/r/20260616141733.1688264-1-vulab%40iscas.ac.cn patch subject: [PATCH] PCI/PTM: fix refcount leak in pci_enable_ptm() config: powerpc-randconfig-002-20260617 (https://download.01.org/0day-ci/archive/20260617/[email protected]/config) compiler: clang version 17.0.6 (https://github.com/llvm/llvm-project 6009708b4367171ccdbf4b5905cb6a803753fe18) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260617/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
>> drivers/pci/pcie/ptm.c:204:20: error: use of undeclared identifier 'parent'
204 | pci_disable_ptm(parent);
| ^1 error generated.
vim +/parent +204 drivers/pci/pcie/ptm.c
162
163 /**
164 * pci_enable_ptm() - Enable Precision Time Measurement
165 * @dev: PCI device
166 *
167 * Enable Precision Time Measurement for @dev.
168 *
169 * Return: zero if successful, or -EINVAL if @dev lacks a PTM Capability or
170 * is not a PTM Root and lacks an upstream path of PTM-enabled devices.
171 */
172 int pci_enable_ptm(struct pci_dev *dev)
173 {
174 int rc;
175 char clock_desc[8];
176
177 /*
178 * A device uses local PTM Messages to request time information
179 * from a PTM Root that's farther upstream. Every device along
180 * the path must support PTM and have it enabled so it can
181 * handle the messages. Therefore, if this device is not a PTM
182 * Root, the upstream link partner must have PTM enabled before
183 * we can enable PTM.
184 */
185 if (!dev->ptm_root) {
186 struct pci_dev *parent;
187
188 parent = pci_upstream_ptm(dev);
189 if (!parent)
190 return -EINVAL;
191 /* Enable PTM for the parent */
192 rc = pci_enable_ptm(parent);
193 if (rc)
194 return rc;
195 }
196
197 /* Already enabled? */
198 if (atomic_inc_return(&dev->ptm_enable_cnt) > 1)
199 return 0;
200
201 rc = __pci_enable_ptm(dev);
202 if (rc) {
203 if (!dev->ptm_root)
> 204 pci_disable_ptm(parent);
205 atomic_dec(&dev->ptm_enable_cnt);
206 return rc;
207 }
208
209 switch (dev->ptm_granularity) {
210 case 0:
211 snprintf(clock_desc, sizeof(clock_desc), "unknown");
212 break;
213 case 255:
214 snprintf(clock_desc, sizeof(clock_desc), ">254ns");
215 break;
216 default:
217 snprintf(clock_desc, sizeof(clock_desc), "%uns",
218 dev->ptm_granularity);
219 break;
220 }
221 pci_info(dev, "PTM enabled%s, %s granularity\n",
222 dev->ptm_root ? " (root)" : "", clock_desc);
223
224 return 0;
225 }
226 EXPORT_SYMBOL(pci_enable_ptm);
227
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
kernel test robot
build botkernel test robot noticed the following build errors:
Hi Wentao,
kernel test robot noticed the following build errors:
[auto build test ERROR on pci/next]
[also build test ERROR on pci/for-linus westeri-thunderbolt/next linus/master v7.1 next-20260616] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Wentao-Liang/PCI-PTM-fix-refcount-leak-in-pci_enable_ptm/20260617-043856 base: https://git.kernel.org/pub/scm/linux/kernel/git/pci/pci.git next patch link: https://lore.kernel.org/r/20260616141733.1688264-1-vulab%40iscas.ac.cn patch subject: [PATCH] PCI/PTM: fix refcount leak in pci_enable_ptm() config: s390-randconfig-r072-20260617 (https://download.01.org/0day-ci/archive/20260617/[email protected]/config) compiler: s390-linux-gcc (GCC) 8.5.0 smatch: v0.5.0-9185-gbcc58b9c reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260617/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
drivers/pci/pcie/ptm.c: In function 'pci_enable_ptm':
>> drivers/pci/pcie/ptm.c:204:20: error: 'parent' undeclared (first use in this function); did you mean 'xa_parent'?
pci_disable_ptm(parent);
^~~~~~
xa_parent drivers/pci/pcie/ptm.c:204:20: note: each undeclared identifier is reported only once for each function it appears in
vim +204 drivers/pci/pcie/ptm.c
162
163 /**
164 * pci_enable_ptm() - Enable Precision Time Measurement
165 * @dev: PCI device
166 *
167 * Enable Precision Time Measurement for @dev.
168 *
169 * Return: zero if successful, or -EINVAL if @dev lacks a PTM Capability or
170 * is not a PTM Root and lacks an upstream path of PTM-enabled devices.
171 */
172 int pci_enable_ptm(struct pci_dev *dev)
173 {
174 int rc;
175 char clock_desc[8];
176
177 /*
178 * A device uses local PTM Messages to request time information
179 * from a PTM Root that's farther upstream. Every device along
180 * the path must support PTM and have it enabled so it can
181 * handle the messages. Therefore, if this device is not a PTM
182 * Root, the upstream link partner must have PTM enabled before
183 * we can enable PTM.
184 */
185 if (!dev->ptm_root) {
186 struct pci_dev *parent;
187
188 parent = pci_upstream_ptm(dev);
189 if (!parent)
190 return -EINVAL;
191 /* Enable PTM for the parent */
192 rc = pci_enable_ptm(parent);
193 if (rc)
194 return rc;
195 }
196
197 /* Already enabled? */
198 if (atomic_inc_return(&dev->ptm_enable_cnt) > 1)
199 return 0;
200
201 rc = __pci_enable_ptm(dev);
202 if (rc) {
203 if (!dev->ptm_root)
> 204 pci_disable_ptm(parent);
205 atomic_dec(&dev->ptm_enable_cnt);
206 return rc;
207 }
208
209 switch (dev->ptm_granularity) {
210 case 0:
211 snprintf(clock_desc, sizeof(clock_desc), "unknown");
212 break;
213 case 255:
214 snprintf(clock_desc, sizeof(clock_desc), ">254ns");
215 break;
216 default:
217 snprintf(clock_desc, sizeof(clock_desc), "%uns",
218 dev->ptm_granularity);
219 break;
220 }
221 pci_info(dev, "PTM enabled%s, %s granularity\n",
222 dev->ptm_root ? " (root)" : "", clock_desc);
223
224 return 0;
225 }
226 EXPORT_SYMBOL(pci_enable_ptm);
227
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki