Commit 3c0da103 authored by Ivan Vecera's avatar Ivan Vecera Committed by Paolo Abeni
Browse files

dpll: Add reference count tracking support



Add support for the REF_TRACKER infrastructure to the DPLL subsystem.

When enabled, this allows developers to track and debug reference counting
leaks or imbalances for dpll_device and dpll_pin objects. It records stack
traces for every get/put operation and exposes this information via
debugfs at:
  /sys/kernel/debug/ref_tracker/dpll_device_*
  /sys/kernel/debug/ref_tracker/dpll_pin_*

The following API changes are made to support this:
1. dpll_device_get() / dpll_device_put() now accept a 'dpll_tracker *'
   (which is a typedef to 'struct ref_tracker *' when enabled, or an empty
   struct otherwise).
2. dpll_pin_get() / dpll_pin_put() and fwnode_dpll_pin_find() similarly
   accept the tracker argument.
3. Internal registration structures now hold a tracker to associate the
   reference held by the registration with the specific owner.

All existing in-tree drivers (ice, mlx5, ptp_ocp, zl3073x) are updated
to pass NULL for the new tracker argument, maintaining current behavior
while enabling future debugging capabilities.

Reviewed-by: default avatarAleksandr Loktionov <aleksandr.loktionov@intel.com>
Co-developed-by: default avatarPetr Oros <poros@redhat.com>
Signed-off-by: default avatarPetr Oros <poros@redhat.com>
Signed-off-by: default avatarIvan Vecera <ivecera@redhat.com>
Reviewed-by: default avatarArkadiusz Kubalewski <arkadiusz.kubalewski@intel.com>
Link: https://patch.msgid.link/20260203174002.705176-8-ivecera@redhat.com


Signed-off-by: default avatarPaolo Abeni <pabeni@redhat.com>
parent 729f5e01
Loading
Loading
Loading
Loading
+15 −0
Original line number Diff line number Diff line
@@ -8,6 +8,21 @@ menu "DPLL device support"
config DPLL
	bool

config DPLL_REFCNT_TRACKER
	bool "DPLL reference count tracking"
	depends on DEBUG_KERNEL && STACKTRACE_SUPPORT && DPLL
	select REF_TRACKER
	help
	  Enable reference count tracking for DPLL devices and pins.
	  This helps debugging reference leaks and use-after-free bugs
	  by recording stack traces for each get/put operation.

	  The tracking information is exposed via debugfs at:
	    /sys/kernel/debug/ref_tracker/dpll_device_*
	    /sys/kernel/debug/ref_tracker/dpll_pin_*

	  If unsure, say N.

source "drivers/dpll/zl3073x/Kconfig"

endmenu
+76 −22
Original line number Diff line number Diff line
@@ -41,6 +41,7 @@ struct dpll_device_registration {
	struct list_head list;
	const struct dpll_device_ops *ops;
	void *priv;
	dpll_tracker tracker;
};

struct dpll_pin_registration {
@@ -48,6 +49,7 @@ struct dpll_pin_registration {
	const struct dpll_pin_ops *ops;
	void *priv;
	void *cookie;
	dpll_tracker tracker;
};

static int call_dpll_notifiers(unsigned long action, void *info)
@@ -83,33 +85,68 @@ void dpll_pin_notify(struct dpll_pin *pin, unsigned long action)
	call_dpll_notifiers(action, &info);
}

static void __dpll_device_hold(struct dpll_device *dpll)
static void dpll_device_tracker_alloc(struct dpll_device *dpll,
				      dpll_tracker *tracker)
{
#ifdef CONFIG_DPLL_REFCNT_TRACKER
	ref_tracker_alloc(&dpll->refcnt_tracker, tracker, GFP_KERNEL);
#endif
}

static void dpll_device_tracker_free(struct dpll_device *dpll,
				     dpll_tracker *tracker)
{
#ifdef CONFIG_DPLL_REFCNT_TRACKER
	ref_tracker_free(&dpll->refcnt_tracker, tracker);
#endif
}

static void __dpll_device_hold(struct dpll_device *dpll, dpll_tracker *tracker)
{
	dpll_device_tracker_alloc(dpll, tracker);
	refcount_inc(&dpll->refcount);
}

static void __dpll_device_put(struct dpll_device *dpll)
static void __dpll_device_put(struct dpll_device *dpll, dpll_tracker *tracker)
{
	dpll_device_tracker_free(dpll, tracker);
	if (refcount_dec_and_test(&dpll->refcount)) {
		ASSERT_DPLL_NOT_REGISTERED(dpll);
		WARN_ON_ONCE(!xa_empty(&dpll->pin_refs));
		xa_destroy(&dpll->pin_refs);
		xa_erase(&dpll_device_xa, dpll->id);
		WARN_ON(!list_empty(&dpll->registration_list));
		ref_tracker_dir_exit(&dpll->refcnt_tracker);
		kfree(dpll);
	}
}

static void __dpll_pin_hold(struct dpll_pin *pin)
static void dpll_pin_tracker_alloc(struct dpll_pin *pin, dpll_tracker *tracker)
{
#ifdef CONFIG_DPLL_REFCNT_TRACKER
	ref_tracker_alloc(&pin->refcnt_tracker, tracker, GFP_KERNEL);
#endif
}

static void dpll_pin_tracker_free(struct dpll_pin *pin, dpll_tracker *tracker)
{
#ifdef CONFIG_DPLL_REFCNT_TRACKER
	ref_tracker_free(&pin->refcnt_tracker, tracker);
#endif
}

static void __dpll_pin_hold(struct dpll_pin *pin, dpll_tracker *tracker)
{
	dpll_pin_tracker_alloc(pin, tracker);
	refcount_inc(&pin->refcount);
}

static void dpll_pin_idx_free(u32 pin_idx);
static void dpll_pin_prop_free(struct dpll_pin_properties *prop);

static void __dpll_pin_put(struct dpll_pin *pin)
static void __dpll_pin_put(struct dpll_pin *pin, dpll_tracker *tracker)
{
	dpll_pin_tracker_free(pin, tracker);
	if (refcount_dec_and_test(&pin->refcount)) {
		xa_erase(&dpll_pin_xa, pin->id);
		xa_destroy(&pin->dpll_refs);
@@ -118,6 +155,7 @@ static void __dpll_pin_put(struct dpll_pin *pin)
		dpll_pin_prop_free(&pin->prop);
		fwnode_handle_put(pin->fwnode);
		dpll_pin_idx_free(pin->pin_idx);
		ref_tracker_dir_exit(&pin->refcnt_tracker);
		kfree_rcu(pin, rcu);
	}
}
@@ -191,7 +229,7 @@ dpll_xa_ref_pin_add(struct xarray *xa_pins, struct dpll_pin *pin,
	reg->ops = ops;
	reg->priv = priv;
	reg->cookie = cookie;
	__dpll_pin_hold(pin);
	__dpll_pin_hold(pin, &reg->tracker);
	if (ref_exists)
		refcount_inc(&ref->refcount);
	list_add_tail(&reg->list, &ref->registration_list);
@@ -214,7 +252,7 @@ static int dpll_xa_ref_pin_del(struct xarray *xa_pins, struct dpll_pin *pin,
		if (WARN_ON(!reg))
			return -EINVAL;
		list_del(&reg->list);
		__dpll_pin_put(pin);
		__dpll_pin_put(pin, &reg->tracker);
		kfree(reg);
		if (refcount_dec_and_test(&ref->refcount)) {
			xa_erase(xa_pins, i);
@@ -272,7 +310,7 @@ dpll_xa_ref_dpll_add(struct xarray *xa_dplls, struct dpll_device *dpll,
	reg->ops = ops;
	reg->priv = priv;
	reg->cookie = cookie;
	__dpll_device_hold(dpll);
	__dpll_device_hold(dpll, &reg->tracker);
	if (ref_exists)
		refcount_inc(&ref->refcount);
	list_add_tail(&reg->list, &ref->registration_list);
@@ -295,7 +333,7 @@ dpll_xa_ref_dpll_del(struct xarray *xa_dplls, struct dpll_device *dpll,
		if (WARN_ON(!reg))
			return;
		list_del(&reg->list);
		__dpll_device_put(dpll);
		__dpll_device_put(dpll, &reg->tracker);
		kfree(reg);
		if (refcount_dec_and_test(&ref->refcount)) {
			xa_erase(xa_dplls, i);
@@ -337,6 +375,7 @@ dpll_device_alloc(const u64 clock_id, u32 device_idx, struct module *module)
		return ERR_PTR(ret);
	}
	xa_init_flags(&dpll->pin_refs, XA_FLAGS_ALLOC);
	ref_tracker_dir_init(&dpll->refcnt_tracker, 128, "dpll_device");

	return dpll;
}
@@ -346,6 +385,7 @@ dpll_device_alloc(const u64 clock_id, u32 device_idx, struct module *module)
 * @clock_id: clock_id of creator
 * @device_idx: idx given by device driver
 * @module: reference to registering module
 * @tracker: tracking object for the acquired reference
 *
 * Get existing object of a dpll device, unique for given arguments.
 * Create new if doesn't exist yet.
@@ -356,7 +396,8 @@ dpll_device_alloc(const u64 clock_id, u32 device_idx, struct module *module)
 * * ERR_PTR(X) - error
 */
struct dpll_device *
dpll_device_get(u64 clock_id, u32 device_idx, struct module *module)
dpll_device_get(u64 clock_id, u32 device_idx, struct module *module,
		dpll_tracker *tracker)
{
	struct dpll_device *dpll, *ret = NULL;
	unsigned long index;
@@ -366,13 +407,17 @@ dpll_device_get(u64 clock_id, u32 device_idx, struct module *module)
		if (dpll->clock_id == clock_id &&
		    dpll->device_idx == device_idx &&
		    dpll->module == module) {
			__dpll_device_hold(dpll);
			__dpll_device_hold(dpll, tracker);
			ret = dpll;
			break;
		}
	}
	if (!ret)
	if (!ret) {
		ret = dpll_device_alloc(clock_id, device_idx, module);
		if (!IS_ERR(ret))
			dpll_device_tracker_alloc(ret, tracker);
	}

	mutex_unlock(&dpll_lock);

	return ret;
@@ -382,15 +427,16 @@ EXPORT_SYMBOL_GPL(dpll_device_get);
/**
 * dpll_device_put - decrease the refcount and free memory if possible
 * @dpll: dpll_device struct pointer
 * @tracker: tracking object for the acquired reference
 *
 * Context: Acquires a lock (dpll_lock)
 * Drop reference for a dpll device, if all references are gone, delete
 * dpll device object.
 */
void dpll_device_put(struct dpll_device *dpll)
void dpll_device_put(struct dpll_device *dpll, dpll_tracker *tracker)
{
	mutex_lock(&dpll_lock);
	__dpll_device_put(dpll);
	__dpll_device_put(dpll, tracker);
	mutex_unlock(&dpll_lock);
}
EXPORT_SYMBOL_GPL(dpll_device_put);
@@ -452,7 +498,7 @@ int dpll_device_register(struct dpll_device *dpll, enum dpll_type type,
	reg->ops = ops;
	reg->priv = priv;
	dpll->type = type;
	__dpll_device_hold(dpll);
	__dpll_device_hold(dpll, &reg->tracker);
	first_registration = list_empty(&dpll->registration_list);
	list_add_tail(&reg->list, &dpll->registration_list);
	if (!first_registration) {
@@ -492,7 +538,7 @@ void dpll_device_unregister(struct dpll_device *dpll,
		return;
	}
	list_del(&reg->list);
	__dpll_device_put(dpll);
	__dpll_device_put(dpll, &reg->tracker);
	kfree(reg);

	if (!list_empty(&dpll->registration_list)) {
@@ -622,6 +668,7 @@ dpll_pin_alloc(u64 clock_id, u32 pin_idx, struct module *module,
			      &dpll_pin_xa_id, GFP_KERNEL);
	if (ret < 0)
		goto err_xa_alloc;
	ref_tracker_dir_init(&pin->refcnt_tracker, 128, "dpll_pin");
	return pin;
err_xa_alloc:
	xa_destroy(&pin->dpll_refs);
@@ -683,6 +730,7 @@ EXPORT_SYMBOL_GPL(unregister_dpll_notifier);
 * @pin_idx: idx given by dev driver
 * @module: reference to registering module
 * @prop: dpll pin properties
 * @tracker: tracking object for the acquired reference
 *
 * Get existing object of a pin (unique for given arguments) or create new
 * if doesn't exist yet.
@@ -694,7 +742,7 @@ EXPORT_SYMBOL_GPL(unregister_dpll_notifier);
 */
struct dpll_pin *
dpll_pin_get(u64 clock_id, u32 pin_idx, struct module *module,
	     const struct dpll_pin_properties *prop)
	     const struct dpll_pin_properties *prop, dpll_tracker *tracker)
{
	struct dpll_pin *pos, *ret = NULL;
	unsigned long i;
@@ -704,13 +752,16 @@ dpll_pin_get(u64 clock_id, u32 pin_idx, struct module *module,
		if (pos->clock_id == clock_id &&
		    pos->pin_idx == pin_idx &&
		    pos->module == module) {
			__dpll_pin_hold(pos);
			__dpll_pin_hold(pos, tracker);
			ret = pos;
			break;
		}
	}
	if (!ret)
	if (!ret) {
		ret = dpll_pin_alloc(clock_id, pin_idx, module, prop);
		if (!IS_ERR(ret))
			dpll_pin_tracker_alloc(ret, tracker);
	}
	mutex_unlock(&dpll_lock);

	return ret;
@@ -720,15 +771,16 @@ EXPORT_SYMBOL_GPL(dpll_pin_get);
/**
 * dpll_pin_put - decrease the refcount and free memory if possible
 * @pin: pointer to a pin to be put
 * @tracker: tracking object for the acquired reference
 *
 * Drop reference for a pin, if all references are gone, delete pin object.
 *
 * Context: Acquires a lock (dpll_lock)
 */
void dpll_pin_put(struct dpll_pin *pin)
void dpll_pin_put(struct dpll_pin *pin, dpll_tracker *tracker)
{
	mutex_lock(&dpll_lock);
	__dpll_pin_put(pin);
	__dpll_pin_put(pin, tracker);
	mutex_unlock(&dpll_lock);
}
EXPORT_SYMBOL_GPL(dpll_pin_put);
@@ -752,6 +804,7 @@ EXPORT_SYMBOL_GPL(dpll_pin_fwnode_set);
/**
 * fwnode_dpll_pin_find - find dpll pin by firmware node reference
 * @fwnode: reference to firmware node
 * @tracker: tracking object for the acquired reference
 *
 * Get existing object of a pin that is associated with given firmware node
 * reference.
@@ -761,7 +814,8 @@ EXPORT_SYMBOL_GPL(dpll_pin_fwnode_set);
 * * valid dpll_pin pointer on success
 * * NULL when no such pin exists
 */
struct dpll_pin *fwnode_dpll_pin_find(struct fwnode_handle *fwnode)
struct dpll_pin *fwnode_dpll_pin_find(struct fwnode_handle *fwnode,
				      dpll_tracker *tracker)
{
	struct dpll_pin *pin, *ret = NULL;
	unsigned long index;
@@ -769,7 +823,7 @@ struct dpll_pin *fwnode_dpll_pin_find(struct fwnode_handle *fwnode)
	mutex_lock(&dpll_lock);
	xa_for_each(&dpll_pin_xa, index, pin) {
		if (pin->fwnode == fwnode) {
			__dpll_pin_hold(pin);
			__dpll_pin_hold(pin, tracker);
			ret = pin;
			break;
		}
+5 −0
Original line number Diff line number Diff line
@@ -10,6 +10,7 @@
#include <linux/dpll.h>
#include <linux/list.h>
#include <linux/refcount.h>
#include <linux/ref_tracker.h>
#include "dpll_nl.h"

#define DPLL_REGISTERED		XA_MARK_1
@@ -23,6 +24,7 @@
 * @type:		type of a dpll
 * @pin_refs:		stores pins registered within a dpll
 * @refcount:		refcount
 * @refcnt_tracker:	ref_tracker directory for debugging reference leaks
 * @registration_list:	list of registered ops and priv data of dpll owners
 **/
struct dpll_device {
@@ -33,6 +35,7 @@ struct dpll_device {
	enum dpll_type type;
	struct xarray pin_refs;
	refcount_t refcount;
	struct ref_tracker_dir refcnt_tracker;
	struct list_head registration_list;
};

@@ -48,6 +51,7 @@ struct dpll_device {
 * @ref_sync_pins:	hold references to pins for Reference SYNC feature
 * @prop:		pin properties copied from the registerer
 * @refcount:		refcount
 * @refcnt_tracker:	ref_tracker directory for debugging reference leaks
 * @rcu:		rcu_head for kfree_rcu()
 **/
struct dpll_pin {
@@ -61,6 +65,7 @@ struct dpll_pin {
	struct xarray ref_sync_pins;
	struct dpll_pin_properties prop;
	refcount_t refcount;
	struct ref_tracker_dir refcnt_tracker;
	struct rcu_head rcu;
};

+6 −6
Original line number Diff line number Diff line
@@ -1480,7 +1480,7 @@ zl3073x_dpll_pin_register(struct zl3073x_dpll_pin *pin, u32 index)

	/* Create or get existing DPLL pin */
	pin->dpll_pin = dpll_pin_get(zldpll->dev->clock_id, index, THIS_MODULE,
				     &props->dpll_props);
				     &props->dpll_props, NULL);
	if (IS_ERR(pin->dpll_pin)) {
		rc = PTR_ERR(pin->dpll_pin);
		goto err_pin_get;
@@ -1503,7 +1503,7 @@ zl3073x_dpll_pin_register(struct zl3073x_dpll_pin *pin, u32 index)
	return 0;

err_register:
	dpll_pin_put(pin->dpll_pin);
	dpll_pin_put(pin->dpll_pin, NULL);
err_prio_get:
	pin->dpll_pin = NULL;
err_pin_get:
@@ -1534,7 +1534,7 @@ zl3073x_dpll_pin_unregister(struct zl3073x_dpll_pin *pin)
	/* Unregister the pin */
	dpll_pin_unregister(zldpll->dpll_dev, pin->dpll_pin, ops, pin);

	dpll_pin_put(pin->dpll_pin);
	dpll_pin_put(pin->dpll_pin, NULL);
	pin->dpll_pin = NULL;
}

@@ -1708,7 +1708,7 @@ zl3073x_dpll_device_register(struct zl3073x_dpll *zldpll)
				       dpll_mode_refsel);

	zldpll->dpll_dev = dpll_device_get(zldev->clock_id, zldpll->id,
					   THIS_MODULE);
					   THIS_MODULE, NULL);
	if (IS_ERR(zldpll->dpll_dev)) {
		rc = PTR_ERR(zldpll->dpll_dev);
		zldpll->dpll_dev = NULL;
@@ -1720,7 +1720,7 @@ zl3073x_dpll_device_register(struct zl3073x_dpll *zldpll)
				  zl3073x_prop_dpll_type_get(zldev, zldpll->id),
				  &zl3073x_dpll_device_ops, zldpll);
	if (rc) {
		dpll_device_put(zldpll->dpll_dev);
		dpll_device_put(zldpll->dpll_dev, NULL);
		zldpll->dpll_dev = NULL;
	}

@@ -1743,7 +1743,7 @@ zl3073x_dpll_device_unregister(struct zl3073x_dpll *zldpll)

	dpll_device_unregister(zldpll->dpll_dev, &zl3073x_dpll_device_ops,
			       zldpll);
	dpll_device_put(zldpll->dpll_dev);
	dpll_device_put(zldpll->dpll_dev, NULL);
	zldpll->dpll_dev = NULL;
}

+7 −7
Original line number Diff line number Diff line
@@ -2814,7 +2814,7 @@ static void ice_dpll_release_pins(struct ice_dpll_pin *pins, int count)
	int i;

	for (i = 0; i < count; i++)
		dpll_pin_put(pins[i].pin);
		dpll_pin_put(pins[i].pin, NULL);
}

/**
@@ -2840,7 +2840,7 @@ ice_dpll_get_pins(struct ice_pf *pf, struct ice_dpll_pin *pins,

	for (i = 0; i < count; i++) {
		pins[i].pin = dpll_pin_get(clock_id, i + start_idx, THIS_MODULE,
					   &pins[i].prop);
					   &pins[i].prop, NULL);
		if (IS_ERR(pins[i].pin)) {
			ret = PTR_ERR(pins[i].pin);
			goto release_pins;
@@ -2851,7 +2851,7 @@ ice_dpll_get_pins(struct ice_pf *pf, struct ice_dpll_pin *pins,

release_pins:
	while (--i >= 0)
		dpll_pin_put(pins[i].pin);
		dpll_pin_put(pins[i].pin, NULL);
	return ret;
}

@@ -3037,7 +3037,7 @@ static void ice_dpll_deinit_rclk_pin(struct ice_pf *pf)
	if (WARN_ON_ONCE(!vsi || !vsi->netdev))
		return;
	dpll_netdev_pin_clear(vsi->netdev);
	dpll_pin_put(rclk->pin);
	dpll_pin_put(rclk->pin, NULL);
}

/**
@@ -3247,7 +3247,7 @@ ice_dpll_deinit_dpll(struct ice_pf *pf, struct ice_dpll *d, bool cgu)
{
	if (cgu)
		dpll_device_unregister(d->dpll, d->ops, d);
	dpll_device_put(d->dpll);
	dpll_device_put(d->dpll, NULL);
}

/**
@@ -3271,7 +3271,7 @@ ice_dpll_init_dpll(struct ice_pf *pf, struct ice_dpll *d, bool cgu,
	u64 clock_id = pf->dplls.clock_id;
	int ret;

	d->dpll = dpll_device_get(clock_id, d->dpll_idx, THIS_MODULE);
	d->dpll = dpll_device_get(clock_id, d->dpll_idx, THIS_MODULE, NULL);
	if (IS_ERR(d->dpll)) {
		ret = PTR_ERR(d->dpll);
		dev_err(ice_pf_to_dev(pf),
@@ -3287,7 +3287,7 @@ ice_dpll_init_dpll(struct ice_pf *pf, struct ice_dpll *d, bool cgu,
		ice_dpll_update_state(pf, d, true);
		ret = dpll_device_register(d->dpll, type, ops, d);
		if (ret) {
			dpll_device_put(d->dpll);
			dpll_device_put(d->dpll, NULL);
			return ret;
		}
		d->ops = ops;
Loading