phy: tegra: Don't use device-managed API to allocate ports
authorThierry Reding <treding@nvidia.com>
Thu, 19 Mar 2020 10:52:13 +0000 (11:52 +0100)
committerThierry Reding <treding@nvidia.com>
Thu, 19 Mar 2020 13:00:05 +0000 (14:00 +0100)
commite78fdbad1e902f422a7a0452cce8378d2652f219
tree43e31763ea49d61dc5e32c033b4214c070229f4f
parent2f8da84def73e1dd89385146e1dbb2ae2c8e0a6a
phy: tegra: Don't use device-managed API to allocate ports

The device-managed allocation API doesn't work well with the life-cycle
of device objects. Since ports have device objects allocated within, it
can lead to situations where these devices need to stay around until
after their parent pad controller has been unbound from its driver. The
device-managed memory allocated for the port objects will, however, get
freed when the pad controller unbinds from the driver. This can cause
use-after-free errors down the road.

Note that the device is deleted as part of the driver unbind operation,
so there isn't much that can be done with it after that point, but the
memory still needs to stay around to ensure none of the references are
invalidated.

One situation where this arises is when a VBUS supply is associated with
a USB 2 or 3 port. When that supply is released using regulator_put() an
SRCU call will queue the release of the device link connecting the port
and the regulator after a grace period. This means that the regulator is
going to keep on to the last reference of the port device even after the
pad controller driver was unbound (which is when the memory backing the
port device is freed).

Fix this by allocating port objects using non-device-managed memory. Add
release callbacks for these objects so that their memory gets freed when
the last reference goes away. This decouples the port devices' lifetime
from the "active" lifetime of the pad controller (i.e. the time during
which the pad controller driver owns the device).

Signed-off-by: Thierry Reding <treding@nvidia.com>
drivers/phy/tegra/xusb-tegra124.c
drivers/phy/tegra/xusb-tegra186.c
drivers/phy/tegra/xusb-tegra210.c
drivers/phy/tegra/xusb.c
drivers/phy/tegra/xusb.h