Revocable references vs. krefs
Revocable references vs. krefs
Posted Sep 25, 2025 18:18 UTC (Thu) by Alan.Stern (subscriber, #12437)Parent article: Revocable references for transient devices
Posted Sep 26, 2025 11:13 UTC (Fri)
by farnz (subscriber, #17727)
[Link] (6 responses)
So the advantage over krefs is simply that it's clear with a revocable reference that you have to check that the referent still exists every time you enter a critical section that needs access; you can, of course, implement this in an ad-hoc way for every piece of hardware that benefits from this, but it's clearer for reviewers if you implement it once and have a common pattern to reuse.
Posted Sep 26, 2025 14:54 UTC (Fri)
by Alan.Stern (subscriber, #12437)
[Link] (5 responses)
From your description, it seems like the other main difference is that with revocable references the data structure can usually be deallocated quite soon after the hardware goes away (delayed only by the length of an SRCU read-side critical section), whereas with krefs the data structure has to hang around for as long as anybody keeps a reference to it. This doesn't sound like a big deal, since the amount of memory used by these data structures tends not to be very large.
Overall my impression is that revocable references are somewhat harder to use than krefs with no compensating advantages.
Posted Sep 27, 2025 7:53 UTC (Sat)
by farnz (subscriber, #17727)
[Link] (4 responses)
That, in turn, makes a huge difference at code review time; you literally cannot forget to register with the device driver for notification that the hardware's gone away, because it's implicit in the design of revocable references.
Posted Sep 27, 2025 15:47 UTC (Sat)
by Alan.Stern (subscriber, #12437)
[Link] (3 responses)
Nevertheless, what you say leaves a strong impression that revocable references are best suited for scenarios other than registration of hot-removable devices. With removable devices, the subsystem to which the device is registered will keep its reference until the device is unregistered, and it will expect to be able to use the device whenever it wants, up until that time. It's not that the subsystem registers with the device driver for notifications; it's the other way around: The driver registers and unregisters the device with the subsystem. This does not pose any problems for code review, because reviewers always expect to see drivers both registering and unregistering their devices -- that is the accepted pattern and a reviewer would definitely notice if it weren't being followed.
All right, given that revocable references aren't well suited for registering removable devices with subsystems, then what are the intended use cases for revocable references?
Posted Sep 29, 2025 9:10 UTC (Mon)
by farnz (subscriber, #17727)
[Link] (2 responses)
You want the subsystem to stop doing work that depends on the removable device being present as quickly as possible - there is no point spending lots of compute on building a new PPP frame to send, or a new work item to pass across to the GPU, if the underlying hardware is gone. Instead, you want it to error out early and stop wasting time doing things that it's going to submit to a driver that's already deregistered from the subsystem.
Posted Sep 29, 2025 13:55 UTC (Mon)
by Alan.Stern (subscriber, #12437)
[Link] (1 responses)
Posted Sep 30, 2025 0:50 UTC (Tue)
by riking (subscriber, #95706)
[Link]
The difference to krefs is in intention. With krefs, you've got a data structure, and you are expected to keep it alive for as long as you want it, releasing it when you're finally finished with it. Revocable references work slightly differently; you're being given the right to claim a reference to a data structure for a short period of time (one RCU critical section), as long as you release it quickly. Outside the RCU critical section, the data structure can be destroyed, and you will not be able to claim it again.
Revocable references vs. krefs
Revocable references vs. krefs
I can't see anything in krefs that requires someone who's done a kref_get to register for notification that the underlying hardware is actually gone, nor is there anything that bounds the time between the hardware going away and the kref_put. With revocable references, that's implicit in the fact that, once the reference is revoked, you can't get access ever again.
Revocable references vs. krefs
Revocable references vs. krefs
The use case is where you have something claiming a removable device, like a PPP driver (for example); in other words, where the subsystem registers with the removable device, rather than the normal way round.
Revocable references vs. krefs
Revocable references vs. krefs
Revocable references vs. krefs
