gpiolib: Defer failed gpio requests by default
PATCH 1/1 gpiolib: Defer failed gpio requests by default
Since users must be explicitly provided with a GPIO number in order to request one the overwhelmingly common case for failing to request will be that the required GPIO driver has not yet registered and we should therefore defer until it has registered.
In order to avoid having to code this logic in individual drivers have gpio_request() return -EPROBE_DEFER when failing to look up the GPIO. Drivers which don't want this behaviour can override it if they desire.
--- drivers/gpio/gpiolib.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpio/gpiolib.c +1 -1
@@ -1186,7 +1186,7 @@ int gpio_request(unsigned gpio, const char *label) {struct gpio_desc *desc;struct gpio_chip *chip; -int status = -EINVAL ; +int status = -EPROBE_DEFER ;unsigned long flags;spin_lock_irqsave (&gpio_lock, flags);
Signature · 1 line
1.7.10
Quoted 10 lines · Mark Brown
Since users must be explicitly provided with a GPIO number in order to request one the overwhelmingly common case for failing to request will be that the required GPIO driver has not yet registered and we should therefore defer until it has registered.
In order to avoid having to code this logic in individual drivers have
gpio_request()return -EPROBE_DEFER when failing to look up the GPIO. Drivers which don't want this behaviour can override it if they desire.Signed-off-by Mark Brown <[email protected]>
[cc'ing a bunch of people who I think have some knowledge about the PM issues... I know this is a long email, but I think this problem needs to be looked at by folks more familiar with suspend/resume than I]
This one is problematic.... well, not the patch itself but the way the larger driver mode works with suspend/resume looks really wrong to me. dpm_list keeps track of all the 'active' driver that need to be suspended. However, it assumes that the list are in the order that they were probed and therefore it can can walk the list backwards when suspending.
The assumption is already kind of bogus because the list is in the order that devices are added, not in the order that they are probed. Probe order has pretty much no relationship with registration order. Probe order effectively is based on driver initialization order and module load order. So the ordering when suspending may be completely different from the order devices were initialized. I think there are probably all kinds of implicit dependency bugs hiding in that whole scheme.
There are the device_pm_move_* functions for manipulating the dpm_list and trying to 'fix' the ordering, but I'm not confident that they are effective in the way they are used. Mostly it seems to be used in the path of removing a device.
/anyway/ the reason I'm saying this is that devices are *supposed* to be in the correct order for suspend/resume. With the implicit dependencies expected by deferred probe, it becomes really important that devices are correctly ordered in the list. The only way this can be done is to move the device to the end of the list. The agreement when this was last discussed on the ML was to make the drivers do an explicit move immediately after it has validated all its dependencies. That is why it doesn't work to return -EPROBE_DEFER by default from helpers; because it becomes really hard to audit if the drivers are doing the right thing.
I see two solutions to this though;
1) add a new .preprobe hook to device drivers that will respect -EPROBE_DEFER and ignore -EPROBE_DEFER on the regular .probe. Probing a driver will call both, but if the .preprobe hook is populated, then it will also reorder dpm_list between the two calls.
- This still requires modifying drivers, but at least it is auditable by mere-mortal reviewers.
- It also means all bus_type code has to add a new hook. Blech.
2) simply force devices to the end of dpm_list before each probe attempt (provided that it doesn't have any children).
- I've avoided this because it adds a claim of the dpm_list_mtx mutex on every single probe call and adds a lot more manipulation of the list....
- However, it should make your patch here actually safe. If a device gets deferred, it will be guaranteed to be at the end of the list because it gets moved there on every probe attempt.
- also after reading through the code (see my notes below; I'm a bit frustrated with the whole thing) I think it is probably the right thing to do.
Will you be at Connect? I could use some time sitting in a room with smart people to dig through this code and talk out what it should be doing.
Grumpily yours,
g.
My notes on the issue:device_pm_add()
- is only called by device_add()
- so that is easy to auditdevice_pm_remove()
- similarly is only called by device_remove()
device_pm_move_{before,after,last}()
- Only called by device_move()device_move()
- Used to reparent a device
- only 4 in-tree users:
- drivers/media/video/pvrusb2
- uses it to *remove* any parents to a device
- this is a weird driver; I don't know why it is unparenting it's devices
- drivers/s390/cio/device.c
- Used to change subchannel of a device.
- net/bluetooth/hci_sysfs.c & net/bluetooth/rfcomm/tty.c
- 2 callsites used to unparent a device before removing the parent. This actually looks rather wrong to me; but I might be missing something.
- 1 call site is used to set a new parent for the rfcomm device after opening the tty.
As for /using/ dpm_list, I see 2 locations:dpm_complete()
- entries in dpm_prepared_list are spliced into dpm_list.dpm_prepare()
- moves entries into dpm_prepared_list
- Hmmm... So what happens when a device is moved but it is already in dpm_prepared_list? Will it accidentally put /back/ onto the dpm_list when it shouldn't be? I don't see any protections against this.
- It *looks* like dev->power.is_{prepared,suspended} is intended to protect that list, but I don't see it being used by the core.
There are other lists too that devices can be on instead of dpm_list:
- dpm_suspended_list
- dpm_late_early_list
- dpm_noirq_list
Blech! None of this is ******* documented. How is this all supposed to work and stay consistent?!?
Misc:
- to_device is the magic macro that goes from the device->power.entry list item.
- searching for power.entry also finds the relevant modifications.
Quoted 20 lines
--- drivers/gpio/gpiolib.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/gpio/gpiolib.c +1
-1@@ -1186,7 +1186,7 @@ int gpio_request(unsigned gpio, const char *label) {struct gpio_desc *desc;struct gpio_chip *chip; -int status = -EINVAL ; +int status = -EPROBE_DEFER ;unsigned long flags;spin_lock_irqsave (&gpio_lock, flags);Signature · 1 line
1.7.10
Signature · 2 lines
Grant Likely, B.Sc, P.Eng. Secret Lab Technologies, Ltd.
Quoted 6 lines · Grant Likely
be done is to move the device to the end of the list. The agreement when this was last discussed on the ML was to make the drivers do an explicit move immediately after it has validated all its dependencies. That is why it doesn't work to return -EPROBE_DEFER by default from helpers; because it becomes really hard to audit if the drivers are doing the right thing.
Ah, oh dear - I missed that bit of the discussion I'm afraid and had thought we'd gone with your option 2 below. This means that the ASoC usage is currently not helping anything (though it's no worse than what we had before), and the regulator framework usage won't do the right thing (though in practice I don't expect it to actually go off and there's fun and games associated with that if we actually need to suspend regulators from software anyway).
Quoted 9 lines
I see two solutions to this though;
1) add a new .preprobe hook to device drivers that will respect -EPROBE_DEFER and ignore -EPROBE_DEFER on the regular .probe. Probing a driver will call both, but if the .preprobe hook is populated, then it will also reorderdpm_listbetween the two calls.
- This still requires modifying drivers, but at least it is auditable by mere-mortal reviewers.
- It also means allbus_typecode has to add a new hook. Blech.
Yeah, this seems very painful, especially the bit where we have to change a good selection of buses and drivers to implement and use it.
Quoted 11 lines
2) simply force devices to the end of
dpm_listbefore each probe attempt (provided that it doesn't have any children).
- I've avoided this because it adds a claim of thedpm_list_mtxmutex on every single probe call and adds a lot more manipulation of the list....
- However, it should make your patch here actually safe. If a device gets deferred, it will be guaranteed to be at the end of the list because it gets moved there on every probe attempt.
- also after reading through the code (see my notes below; I'm a bit frustrated with the whole thing) I think it is probably the right thing to do.
This does feel much more robust to me than manually reordering in drivers, we'll have to see if it causes a performance problem though and if it does perhaps we can handle it.
I guess the other thing is that if we get a lot of drivers implementing deferral and we stop playing around with initcall stuff then we'll loose a reasonable proportion of the advantage of not taking the mutex and reordering.
Either of these options would avoid the surprise that currently exists with the API.
Quoted 3 lines
Will you be at Connect? I could use some time sitting in a room with smart people to dig through this code and talk out what it should be doing.
I won't be unfortunately.
Quoted 23 lines · Grant Likely
/anyway/ the reason I'm saying this is that devices are *supposed* to be in the correct order for suspend/resume. With the implicit dependencies expected by deferred probe, it becomes really important that devices are correctly ordered in the list. The only way this can be done is to move the device to the end of the list. The agreement when this was last discussed on the ML was to make the drivers do an explicit move immediately after it has validated all its dependencies. That is why it doesn't work to return -EPROBE_DEFER by default from helpers; because it becomes really hard to audit if the drivers are doing the right thing.
I see two solutions to this though;
1) add a new .preprobe hook to device drivers that will respect -EPROBE_DEFER and ignore -EPROBE_DEFER on the regular .probe. Probing a driver will call both, but if the .preprobe hook is populated, then it will also reorderdpm_listbetween the two calls.
- This still requires modifying drivers, but at least it is auditable by mere-mortal reviewers.
- It also means allbus_typecode has to add a new hook. Blech.
2) simply force devices to the end ofdpm_listbefore each probe attempt (provided that it doesn't have any children).
- I've avoided this because it adds a claim of thedpm_list_mtx
Quoted 8 lines
mutex on every single probe call and adds a lot more manipulation of the list....
- However, it should make your patch here actually safe. If a device gets deferred, it will be guaranteed to be at the end of the list because it gets moved there on every probe attempt.
- also after reading through the code (see my notes below; I'm a bit frustrated with the whole thing) I think it is probably the right thing to do.
2) seems like the best approach. A little extra list manipulation shouldn't hurt anything (the bits don't wear out...).
Quoted 9 lines
As for /using/
dpm_list, I see 2 locations:dpm_complete()
- entries indpm_prepared_listare spliced intodpm_list.dpm_prepare()
- moves entries intodpm_prepared_list
- Hmmm... So what happens when a device is moved but it is already indpm_prepared_list? Will it accidentally put /back/ onto thedpm_listwhen it shouldn't be? I don't see any protections against this.
It should never happen. A device gets put on the dpm_prepared_list after its .prepare method is called, and one of the things that method is supposed to do is guarantee that no more children will be registered beneath the device. No registrations means no probes -- or at least, it did until the deferred probing mechanism was created.
It may turn out to be necessary to make the deferred probing threads freezable, so they will not run while the dpm lists are being used.
Quoted 3 lines
- It *looks* like dev->power.is_{prepared,suspended} is intended to protect that list, but I don't see it being used by the core.
IIRC, those flags is used mainly for error detection and reporting (also for error recovery, when a suspend is aborted in the middle). They don't protect anything.
Quoted 7 lines
There are other lists too that devices can be on instead of
dpm_list:
-dpm_suspended_list
-dpm_late_early_list
-dpm_noirq_listBlech! None of this is ******* documented. How is this all supposed to work and stay consistent?!?
That's not entirely fair. The overall process is documented in Documentation/power/devices.txt. The list headers themselves are supposed to be private to drivers/base/power/main.c, although they currently aren't declared static for some reason -- an oversight?
Alan Stern