net: eth-uclass: change state before stop() in eth_halt()
PATCH 1/1 net: eth-uclass: change state before stop() in eth_halt()
In eth_halt(), change the private uclass state before calling stop() instead of afterwards, to avoid writing to memory which may have been freed during stop().
In the ethernet gadget implementation, the gadget device gets probed during start() and removed during stop(), which includes freeing uclass_priv_ to which priv is pointing. Writing to priv after stop() may corrupt the fd member of struct malloc_chunk, which represents the freed block, and could cause hard-to-debug crashes on subsequent calls to malloc()/free().
--- net/eth-uclass.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/eth-uclass.c +1 -1
@@ -342,9 +342,9 @@ void eth_halt(void)if (!priv || !priv->running)return ; -eth_get_ops (current)->stop (current); priv->state =ETH_STATE_PASSIVE ; priv->running = false; +eth_get_ops (current)->stop (current); }int eth_is_active (struct udevice *dev)
Signature · 1 line
2.38.1
Quoted 24 lines · On Wed, 30 Nov 2022 17:42:25 +0100 Niel Fourie <[email protected]> wrote:
In
eth_halt(), change the private uclass state before callingstop()instead of afterwards, to avoid writing to memory which may have been freed duringstop().In the ethernet gadget implementation, the gadget device gets probed during
start()and removed duringstop(), which includes freeinguclass_priv_to whichprivis pointing. Writing toprivafterstop()may corrupt thefdmember ofstruct malloc_chunk, which represents the freed block, and could cause hard-to-debug crashes on subsequent calls to malloc()/free().Signed-off-by Niel Fourie <[email protected]>Cc Ramon Fried <[email protected]>Cc Marek Vasut <[email protected]>Cc Lukasz Majewski <[email protected]>--- net/eth-uclass.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)net/eth-uclass.c +0
-0
net/eth-uclass.c void eth_halt(void) · line 350
Quoted 10 lines
net/eth-uclass.c +1
-1@@ -342,9 +342,9 @@ void eth_halt(void)if (!priv || !priv->running)return ; -eth_get_ops (current)->stop (current); priv->state =ETH_STATE_PASSIVE ; priv->running = false; +eth_get_ops (current)->stop (current); }int eth_is_active (struct udevice *dev)
Best regards,
Lukasz Majewski
--
DENX Software Engineering GmbH, Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-59 Fax: (+49)-8142-66989-80 Email: [email protected]
Quoted 36 lines · Lukasz Majewski
Quoted 33 lines · On Wed, 30 Nov 2022 17:42:25 +0100 Niel Fourie <[email protected]> wrote:
In
eth_halt(), change the private uclass state before callingstop()instead of afterwards, to avoid writing to memory which may have been freed duringstop().In the ethernet gadget implementation, the gadget device gets probed during
start()and removed duringstop(), which includes freeinguclass_priv_to whichprivis pointing. Writing toprivafterstop()may corrupt thefdmember ofstruct malloc_chunk, which represents the freed block, and could cause hard-to-debug crashes on subsequent calls to malloc()/free().Signed-off-by Niel Fourie <[email protected]>Cc Ramon Fried <[email protected]>Cc Marek Vasut <[email protected]>Cc Lukasz Majewski <[email protected]>--- net/eth-uclass.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)net/eth-uclass.c +1
-1@@ -342,9 +342,9 @@ void eth_halt(void)if (!priv || !priv->running)return ; -eth_get_ops (current)->stop (current); priv->state =ETH_STATE_PASSIVE ; priv->running = false; +eth_get_ops (current)->stop (current); }
Quoted 3 lines
net/eth-uclass.c void eth_halt(void) · line 350
Quoted 1 line
net/eth-uclass.c +0
-0int eth_is_active (struct udevice *dev)Reviewed-by Lukasz Majewski <[email protected]>
How come nobody triggered this problem with regular ethernet in U-Boot ?
If this is isolated to USB gadget ethernet, then please do not hack around this in core networking code, but rather fix the USB ethernet gadget itself. It seems that gadget code should not unregister the gadget in drivers/usb/gadget/ether.c _usb_eth_halt() , at least not fully.
Hi Marek,
Quoted 39 lines · Marek Vasut
Quoted 22 lines · Lukasz Majewski
Quoted 19 lines · On Wed, 30 Nov 2022 17:42:25 +0100 Niel Fourie <[email protected]> wrote:
In
eth_halt(), change the private uclass state before callingstop()instead of afterwards, to avoid writing to memory which may have been freed duringstop().In the ethernet gadget implementation, the gadget device gets probed during
start()and removed duringstop(), which includes freeinguclass_priv_to whichprivis pointing. Writing toprivafterstop()may corrupt thefdmember ofstruct malloc_chunk, which represents the freed block, and could cause hard-to-debug crashes on subsequent calls to malloc()/free().Signed-off-by Niel Fourie <[email protected]>Cc Ramon Fried <[email protected]>Cc Marek Vasut <[email protected]>Cc Lukasz Majewski <[email protected]>---
net/eth-uclass.c| 2 +-1 file changed, 1 insertion(+), 1 deletion(-)Quoted 15 lines
net/eth-uclass.c void eth_halt(void) · line 348
Quoted 13 lines
net/eth-uclass.c +1
-1@@ -342,9 +342,9 @@ void eth_halt(void) if (!priv || !priv->running) return; - eth_get_ops(current)->stop(current); priv->state = ETH_STATE_PASSIVE; priv->running = false; + eth_get_ops(current)->stop(current); } int eth_is_active(struct udevice *dev)Reviewed-by Lukasz Majewski <[email protected]>
Quoted 3 lines
How come nobody triggered this problem with regular ethernet in U-Boot ?
If this is isolated to USB gadget ethernet, then please do not hack around this in core networking code, but rather fix the USB ethernet gadget itself. It seems that gadget code should not unregister the gadget in
drivers/usb/gadget/ether.c_usb_eth_halt(), at least not fully.
The reason is simple, the regular ethernet drivers do not get removed on stop() like for the gadget ethernet driver, and in their case priv is still valid.
I agree on your point that reworking the ethernet gadget code would be preferable, but this would be a much bigger effort (and if I were to do it, I would probably introduce even more bugs). I am not certain whether this would not also affect the non-DM gadget implementation as well, which still contain drivers like ci_udc.c which does not appear to have been ported to DM gadget yet? (I only see DM USB there...)
That said, I am not certain whether this is not also bug, as I am not certain whether the assumption that priv should be available after stop() is valid or not.
The documentation at https://source.denx.de/u-boot/u-boot/-/blob/master/doc/develop/driver-model/ethernet.rst?plain=1#L135 states:
The **stop** function should turn off / disable the hardware and place it back in its reset state. It can be called at any time (before any call to the related start() function), so make sure it can handle this sort of thing.
In such a complete reset state I am not certain whether the assumption that priv should exist is still valid, at least not without another call to dev_get_uclass_priv() and revalidating it first?
Lastly, this assumption that priv is still valid is rather new and it was introduced here:
https://source.denx.de/u-boot/u-boot/-/commit/fa795f452541ce07b33be603de36cac3c5d7dfcf
This commit appears to be a workaround for drivers which cannot deal with stop() being called at any time as required in the above quoted documentation.
I would consider adding a workaround to a workaround in this case to be the lesser evil, as tracking down this bug in the first place was like looking for a needle in a haystack. This change would at least save everybody else from strange crashes in particular configurations without any negative impact. But this is fortunately not my decision. :)
Best regards,
Niel Fourie
Signature · 3 lines
DENX Software Engineering GmbH, Managing Director: Wolfgang Denk HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany Phone: +49-8142-66989-21 Fax: +49-8142-66989-80 Email: [email protected]
Quoted 93 lines · Niel Fourie
Hi Marek,
Quoted 39 lines · Marek Vasut
Quoted 22 lines · Lukasz Majewski
Quoted 19 lines · On Wed, 30 Nov 2022 17:42:25 +0100 Niel Fourie <[email protected]> wrote:
In
eth_halt(), change the private uclass state before callingstop()instead of afterwards, to avoid writing to memory which may have been freed duringstop().In the ethernet gadget implementation, the gadget device gets probed during
start()and removed duringstop(), which includes freeinguclass_priv_to whichprivis pointing. Writing toprivafterstop()may corrupt thefdmember ofstruct malloc_chunk, which represents the freed block, and could cause hard-to-debug crashes on subsequent calls to malloc()/free().Signed-off-by Niel Fourie <[email protected]>Cc Ramon Fried <[email protected]>Cc Marek Vasut <[email protected]>Cc Lukasz Majewski <[email protected]>--- net/eth-uclass.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)Quoted 15 lines
net/eth-uclass.c void eth_halt(void) · line 348
Quoted 13 lines
net/eth-uclass.c +1
-1@@ -342,9 +342,9 @@ void eth_halt(void)if (!priv || !priv->running)return ; -eth_get_ops (current)->stop (current); priv->state =ETH_STATE_PASSIVE ; priv->running = false; +eth_get_ops (current)->stop (current); }int eth_is_active (struct udevice *dev)Reviewed-by Lukasz Majewski <[email protected]>Quoted 6 lines
How come nobody triggered this problem with regular ethernet in U-Boot ?
If this is isolated to USB gadget ethernet, then please do not hack around this in core networking code, but rather fix the USB ethernet gadget itself. It seems that gadget code should not unregister the gadget in
drivers/usb/gadget/ether.c_usb_eth_halt(), at least not fully.The reason is simple, the regular ethernet drivers do not get removed on
stop()like for the gadget ethernet driver, and in their caseprivis still valid.I agree on your point that reworking the ethernet gadget code would be preferable, but this would be a much bigger effort (and if I were to do it, I would probably introduce even more bugs). I am not certain whether this would not also affect the non-DM gadget implementation as well, which still contain drivers like
ci_udc.cwhich does not appear to have been ported to DM gadget yet? (I only see DM USB there...)That said, I am not certain whether this is not also bug, as I am not certain whether the assumption that
privshould be available afterstop()is valid or not.The documentation at
https://source.denx.de/u-boot/u-boot/-/blob/master/doc/develop/driver-model/ethernet.rst?plain=1#L135
states:The **stop** function should turn off / disable the hardware and place it back in its reset state. It can be called at any time (before any call to the related
start()function), so make sure it can handle this sort of thing.In such a complete reset state I am not certain whether the assumption that
privshould exist is still valid, at least not without another call todev_get_uclass_priv()and revalidating it first?Lastly, this assumption that priv is still valid is rather new and it was introduced here:
https://source.denx.de/u-boot/u-boot/-/commit/fa795f452541ce07b33be603de36cac3c5d7dfcf
This commit appears to be a workaround for drivers which cannot deal with
stop()being called at any time as required in the above quoted documentation.I would consider adding a workaround to a workaround in this case to be the lesser evil, as tracking down this bug in the first place was like looking for a needle in a haystack. This change would at least save everybody else from strange crashes in particular configurations without any negative impact. But this is fortunately not my decision. :)
Quoted 7 lines
Best regards,
Niel Fourie--
DENX Software Engineering GmbH, Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: +49-8142-66989-21 Fax: +49-8142-66989-80 Email: [email protected]
I will accept this patch, but please add some comment in the code about why the order is important.
Quoted 93 lines · Ramon Fried
Quoted 92 lines · Niel Fourie
Hi Marek,
Quoted 39 lines · Marek Vasut
Quoted 22 lines · Lukasz Majewski
Quoted 19 lines · On Wed, 30 Nov 2022 17:42:25 +0100 Niel Fourie <[email protected]> wrote:
In
eth_halt(), change the private uclass state before callingstop()instead of afterwards, to avoid writing to memory which may have been freed duringstop().In the ethernet gadget implementation, the gadget device gets probed during
start()and removed duringstop(), which includes freeinguclass_priv_to whichprivis pointing. Writing toprivafterstop()may corrupt thefdmember ofstruct malloc_chunk, which represents the freed block, and could cause hard-to-debug crashes on subsequent calls to malloc()/free().Signed-off-by Niel Fourie <[email protected]>Cc Ramon Fried <[email protected]>Cc Marek Vasut <[email protected]>Cc Lukasz Majewski <[email protected]>--- net/eth-uclass.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)Quoted 15 lines
net/eth-uclass.c void eth_halt(void) · line 348
Quoted 13 lines
net/eth-uclass.c +1
-1@@ -342,9 +342,9 @@ void eth_halt(void)if (!priv || !priv->running)return ; -eth_get_ops (current)->stop (current); priv->state =ETH_STATE_PASSIVE ; priv->running = false; +eth_get_ops (current)->stop (current); }int eth_is_active (struct udevice *dev)Reviewed-by Lukasz Majewski <[email protected]>Quoted 6 lines
How come nobody triggered this problem with regular ethernet in U-Boot ?
If this is isolated to USB gadget ethernet, then please do not hack around this in core networking code, but rather fix the USB ethernet gadget itself. It seems that gadget code should not unregister the gadget in
drivers/usb/gadget/ether.c_usb_eth_halt(), at least not fully.The reason is simple, the regular ethernet drivers do not get removed on
stop()like for the gadget ethernet driver, and in their caseprivis still valid.I agree on your point that reworking the ethernet gadget code would be preferable, but this would be a much bigger effort (and if I were to do it, I would probably introduce even more bugs). I am not certain whether this would not also affect the non-DM gadget implementation as well, which still contain drivers like
ci_udc.cwhich does not appear to have been ported to DM gadget yet? (I only see DM USB there...)That said, I am not certain whether this is not also bug, as I am not certain whether the assumption that
privshould be available afterstop()is valid or not.The documentation at
https://source.denx.de/u-boot/u-boot/-/blob/master/doc/develop/driver-model/ethernet.rst?plain=1#L135
states:The **stop** function should turn off / disable the hardware and place it back in its reset state. It can be called at any time (before any call to the related
start()function), so make sure it can handle this sort of thing.In such a complete reset state I am not certain whether the assumption that
privshould exist is still valid, at least not without another call todev_get_uclass_priv()and revalidating it first?Lastly, this assumption that priv is still valid is rather new and it was introduced here:
https://source.denx.de/u-boot/u-boot/-/commit/fa795f452541ce07b33be603de36cac3c5d7dfcf
This commit appears to be a workaround for drivers which cannot deal with
stop()being called at any time as required in the above quoted documentation.I would consider adding a workaround to a workaround in this case to be the lesser evil, as tracking down this bug in the first place was like looking for a needle in a haystack. This change would at least save everybody else from strange crashes in particular configurations without any negative impact. But this is fortunately not my decision. :)
[...]
Quoted 2 lines
I will accept this patch, but please add some comment in the code about why the order is important.
Let me pause you here, see my previous reply. I'm afraid this breaks drivers/net/ldpaa_eth/ldpaa_eth.c , which checks priv->state in its -> stop callback implementation, so if priv->state is changed _before_ the ->stop() callback is called, the ldpaa would never be stopped. I proposed a fix on the USB gadget side -- keep the gadget device allocated -- I believe that's the correct approach.
Quoted 1 line · Niel Fourie
Hi Marek,
Hi,
Quoted 40 lines · Marek Vasut
Quoted 39 lines · Lukasz Majewski
Quoted 37 lines
Quoted 24 lines · On Wed, 30 Nov 2022 17:42:25 +0100 Niel Fourie <[email protected]> wrote:
In
eth_halt(), change the private uclass state before callingstop()instead of afterwards, to avoid writing to memory which may have been freed duringstop().In the ethernet gadget implementation, the gadget device gets probed during
start()and removed duringstop(), which includes freeinguclass_priv_to whichprivis pointing. Writing toprivafterstop()may corrupt thefdmember ofstruct malloc_chunk, which represents the freed block, and could cause hard-to-debug crashes on subsequent calls to malloc()/free().Signed-off-by Niel Fourie <[email protected]>Cc Ramon Fried <[email protected]>Cc Marek Vasut <[email protected]>Cc Lukasz Majewski <[email protected]>---
net/eth-uclass.c| 2 +-1 file changed, 1 insertion(+), 1 deletion(-)net/eth-uclass.c +0
-0net/eth-uclass.c void eth_halt(void) · line 348
Quoted 8 lines
net/eth-uclass.c +1
-1@@ -342,9 +342,9 @@ void eth_halt(void) if (!priv || !priv->running) return; - eth_get_ops(current)->stop(current); priv->state = ETH_STATE_PASSIVE; priv->running = false; + eth_get_ops(current)->stop(current); } int eth_is_active(struct udevice *dev)Reviewed-by Lukasz Majewski <[email protected]>
Quoted 5 lines
Quoted 3 lines
How come nobody triggered this problem with regular ethernet in U-Boot ?
If this is isolated to USB gadget ethernet, then please do not hack around this in core networking code, but rather fix the USB ethernet gadget itself. It seems that gadget code should not unregister the gadget in
drivers/usb/gadget/ether.c_usb_eth_halt(), at least not fully.The reason is simple, the regular ethernet drivers do not get removed on
stop()like for the gadget ethernet driver, and in their caseprivis still valid.
The suggestion for a proper fix is in the last paragraph above -- do not unregister the usb ethernet gadget device in halt(), keep the gadget device registered. Then the priv pointer would be valid. (*)
Quoted 7 lines
I agree on your point that reworking the ethernet gadget code would be preferable, but this would be a much bigger effort (and if I were to do it, I would probably introduce even more bugs). I am not certain whether this would not also affect the non-DM gadget implementation as well, which still contain drivers like
ci_udc.cwhich does not appear to have been ported to DM gadget yet? (I only see DM USB there...)That said, I am not certain whether this is not also bug, as I am not certain whether the assumption that
privshould be available afterstop()is valid or not.The documentation at https://source.denx.de/u-boot/u-boot/-/blob/master/doc/develop/driver-model/ethernet.rst?plain=1#L135 states:
The **stop** function should turn off / disable the hardware and place it back in its reset state. It can be called at any time (before any call to the related
start()function), so make sure it can handle this sort of thing.
The ->stop callback is supposed to stop the interface, turn off its DMA, but NOT deallocate the device (and its associated data) behind it.
Quoted 1 line
In such a complete reset state I am not certain whether the assumption that
privshould exist is still valid, at least not without another call todev_get_uclass_priv()and revalidating it first?
In case a device is probe()d, its private data are also allocated and available, so yes, 'priv' pointer should still be valid.
Quoted 3 lines
Lastly, this assumption that priv is still valid is rather new and it was introduced here:
https://source.denx.de/u-boot/u-boot/-/commit/fa795f452541ce07b33be603de36cac3c5d7dfcf
I disagree, the device private data are valid during the entire lifespan of the device. That assumption has been baked into the driver model itself and far predates that commit.
In case of a usb ethernet, the lifespan of the device starts with the 'bind' command which triggers the ->bind callback, and first use which triggers its ->probe callback. The lifespan ends with 'unbind' command or OS boot, which triggers ->remove callback and ->unbind callbacks.
Quoted 1 line
This commit appears to be a workaround for drivers which cannot deal with
stop()being called at any time as required in the above quoted documentation.
This commit prevents network device ->start() from being called multiple times, which is a valid precaution, as calling start while the interface is already up would interfere with existing connection (e.g. the netconsole as mentioned in the commit message). That does not seem to be a workaround to me.
Quoted 1 line
I would consider adding a workaround to a workaround in this case to be the lesser evil, as tracking down this bug in the first place was like looking for a needle in a haystack. This change would at least save everybody else from strange crashes in particular configurations without any negative impact. But this is fortunately not my decision. :)
Commit fa795f45254 ("net: eth-uclass: avoid running start() twice without stop()") is as far as I can tell unrelated to this change and does not seem to me like a workaround.
It does however show that this patch introduces a bug -- this patch changes the order in which priv->state = ETH_STATE_PASSIVE; is assigned from _after_ the ->stop callback to _before_ the -> stop callback. This breaks drivers/net/ldpaa_eth/ldpaa_eth.c which checks the priv->state in its ->stop callback, either on its own in non-DM case, or in eth_is_active() implementation in DM case. With this patch, the interface would never be stopped in the ->stop callback, because the condition (net_dev->state == ETH_STATE_PASSIVE) test in the ldpaa stop callback implementation would always be true.
The proper fix is in the usb ethernet gadget code, see (*) above, let's not pile workarounds onto already difficult to maintain code.
[...]
Hi Marek,
Quoted 59 lines · Marek Vasut
Quoted 1 line · Niel Fourie
Hi Marek,
Hi,
Quoted 40 lines · Marek Vasut
Quoted 39 lines · Lukasz Majewski
Quoted 37 lines
Quoted 24 lines · On Wed, 30 Nov 2022 17:42:25 +0100 Niel Fourie <[email protected]> wrote:
In
eth_halt(), change the private uclass state before callingstop()instead of afterwards, to avoid writing to memory which may have been freed duringstop().In the ethernet gadget implementation, the gadget device gets probed during
start()and removed duringstop(), which includes freeinguclass_priv_to whichprivis pointing. Writing toprivafterstop()may corrupt thefdmember ofstruct malloc_chunk, which represents the freed block, and could cause hard-to-debug crashes on subsequent calls to malloc()/free().Signed-off-by Niel Fourie <[email protected]>Cc Ramon Fried <[email protected]>Cc Marek Vasut <[email protected]>Cc Lukasz Majewski <[email protected]>---
net/eth-uclass.c| 2 +-1 file changed, 1 insertion(+), 1 deletion(-)net/eth-uclass.c +0
-0net/eth-uclass.c void eth_halt(void) · line 348
Quoted 8 lines
net/eth-uclass.c +1
-1@@ -342,9 +342,9 @@ void eth_halt(void) if (!priv || !priv->running) return; - eth_get_ops(current)->stop(current); priv->state = ETH_STATE_PASSIVE; priv->running = false; + eth_get_ops(current)->stop(current); } int eth_is_active(struct udevice *dev)Reviewed-by Lukasz Majewski <[email protected]>Quoted 5 lines
Quoted 3 lines
How come nobody triggered this problem with regular ethernet in U-Boot ?
If this is isolated to USB gadget ethernet, then please do not hack around this in core networking code, but rather fix the USB ethernet gadget itself. It seems that gadget code should not unregister the gadget in
drivers/usb/gadget/ether.c_usb_eth_halt(), at least not fully.The reason is simple, the regular ethernet drivers do not get removed on
stop()like for the gadget ethernet driver, and in their caseprivis still valid.The suggestion for a proper fix is in the last paragraph above -- do not unregister the usb ethernet gadget device in
halt(), keep the gadget device registered. Then the priv pointer would be valid. (*)Quoted 6 lines
I agree on your point that reworking the ethernet gadget code would be preferable, but this would be a much bigger effort (and if I were to do it, I would probably introduce even more bugs). I am not certain whether this would not also affect the non-DM gadget implementation as well, which still contain drivers like
ci_udc.cwhich does not appear to have been ported to DM gadget yet? (I only see DM USB there...)That said, I am not certain whether this is not also bug, as I am not certain whether the assumption that
privshould be available afterstop()is valid or not.The documentation at https://source.denx.de/u-boot/u-boot/-/blob/master/doc/develop/driver-model/ethernet.rst?plain=1#L135 states:
Quoted 7 lines
Quoted 1 line
The **stop** function should turn off / disable the hardware and place it back in its reset state. It can be called at any time (before any call to the related
start()function), so make sure it can handle this sort of thing.The ->stop callback is supposed to stop the interface, turn off its DMA, but NOT deallocate the device (and its associated data) behind it.
Quoted 1 line
In such a complete reset state I am not certain whether the assumption that
privshould exist is still valid, at least not without another call todev_get_uclass_priv()and revalidating it first?In case a device is probe()d, its private data are also allocated and available, so yes, 'priv' pointer should still be valid.
Granted, the usb gadget driver implementation is problematic, and it definitely belongs on the TODO list.
That being said, priv not being valid at this stage is not a new problem, as validation for it after stop() was explicitly added in commit c3211708 ("net: eth-uclass: Fix for DM USB ethernet support") for this reason 4 years ago. Fortunately in that case, it just fixed a null pointer de-reference, not corruption of freed memory.
Quoted 11 lines
Quoted 3 lines
Lastly, this assumption that priv is still valid is rather new and it was introduced here:
https://source.denx.de/u-boot/u-boot/-/commit/fa795f452541ce07b33be603de36cac3c5d7dfcf
I disagree, the device private data are valid during the entire lifespan of the device. That assumption has been baked into the driver model itself and far predates that commit.
In case of a usb ethernet, the lifespan of the device starts with the 'bind' command which triggers the ->bind callback, and first use which triggers its ->probe callback. The lifespan ends with 'unbind' command or OS boot, which triggers ->remove callback and ->unbind callbacks.
Quoted 1 line
This commit appears to be a workaround for drivers which cannot deal with
stop()being called at any time as required in the above quoted documentation.This commit prevents network device ->start() from being called multiple times, which is a valid precaution, as calling start while the interface is already up would interfere with existing connection (e.g. the netconsole as mentioned in the commit message). That does not seem to be a workaround to me.
Ah yes, the commit message indeed states that some drivers (like fec_mxc) cannot deal with multiple start() calls, not multiple stop() calls. My mistake, good point.
But preventing multiple start()s getting called is not what the code change in this commit is actually doing. It is instead inhibiting calling stop() on devices for which priv is not valid or for which priv->running is false. Therefore it is avoiding calling stop() on un-started devices.
Quoted 3 lines
Quoted 1 line
I would consider adding a workaround to a workaround in this case to be the lesser evil, as tracking down this bug in the first place was like looking for a needle in a haystack. This change would at least save everybody else from strange crashes in particular configurations without any negative impact. But this is fortunately not my decision. :)
Commit
fa795f45254("net: eth-uclass: avoid runningstart()twice withoutstop()") is as far as I can tell unrelated to this change and does not seem to me like a workaround.
It does introduce writing to priv after stop() is called without (re-)validating priv first, though.
The code previously first called stop(), then called dev_get_uclass_priv() to get priv and then validated priv before writing to it, which avoids exactly the problem of priv no longer being valid.
As mentioned above, this was explicitly addressed by commit c3211708 ("net: eth-uclass: Fix for DM USB ethernet support").
Quoted 2 lines
It does however show that this patch introduces a bug -- this patch changes the order in which
priv->state=ETH_STATE_PASSIVE; is assigned from _after_ the ->stop callback to _before_ the -> stop callback. This breaksdrivers/net/ldpaa_eth/ldpaa_eth.cwhich checks thepriv->statein its ->stop callback, either on its own in non-DM case, or ineth_is_active()implementation in DM case. With this patch, the interface would never be stopped in the ->stop callback, because the condition (net_dev->state==ETH_STATE_PASSIVE) test in the ldpaa stop callback implementation would always be true.
In drivers/net/ldpaa_eth/ldpaa_eth.c:ldpaa_eth_stop(), priv is of type
struct ldpaa_eth_priv*, defined in drivers/net/ldpaa_eth/ldpaa_eth.h and is accessed using dev_get_priv().
In net/eth-uclass.c:eht_halt(), priv is of type struct eth_device_priv* and defined in the same .c file, and is accessed using dev_get_uclass_priv(). As the structure is local to this file, nothing outside of this file should have any knowledge of its contents, and changing of the order of the calls should only impact this file.
I sincerely hope that these two are not interfering with each other, otherwise we have much bigger problems...
Quoted 3 lines
The proper fix is in the usb ethernet gadget code, see (*) above, let's not pile workarounds onto already difficult to maintain code.
[...]
Agreed. The problem is unfortunately just that I don't see that happening simply to fix this bug.
Best regards,
Niel Fourie
Signature · 3 lines
DENX Software Engineering GmbH, Managing Director: Wolfgang Denk HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany Phone: +49-8142-66989-21 Fax: +49-8142-66989-80 Email: [email protected]
Hi Marek,
On 05/12/2022 13:06, Niel Fourie wrote:
[...]
Quoted 10 lines
Quoted 2 lines
It does however show that this patch introduces a bug -- this patch changes the order in which
priv->state=ETH_STATE_PASSIVE; is assigned from _after_ the ->stop callback to _before_ the -> stop callback. This breaksdrivers/net/ldpaa_eth/ldpaa_eth.cwhich checks thepriv->statein its ->stop callback, either on its own in non-DM case, or ineth_is_active()implementation in DM case. With this patch, the interface would never be stopped in the ->stop callback, because the condition (net_dev->state==ETH_STATE_PASSIVE) test in the ldpaa stop callback implementation would always be true.In drivers/net/ldpaa_eth/ldpaa_eth.c:ldpaa_eth_stop(), priv is of type
struct ldpaa_eth_priv*, defined indrivers/net/ldpaa_eth/ldpaa_eth.hand is accessed usingdev_get_priv().In net/eth-uclass.c:eht_halt(), priv is of type struct eth_device_priv* and defined in the same .c file, and is accessed using
dev_get_uclass_priv(). As the structure is local to this file, nothing outside of this file should have any knowledge of its contents, and changing of the order of the calls should only impact this file.I sincerely hope that these two are not interfering with each other, otherwise we have much bigger problems...
Shucks, I was thrown off by the the fact that net_dev is of type struct eth_device, and its member state is separate from struct eth_device_state and its member state, that I missed the implication of eth_is_active() *setting* the value of struct eth_device_priv's state not *reading* it.
Well spotted, you are correct. The patch in its current form would introduce that bug. Thank you for finding that.
Adding back the call to dev_get_uclass_priv() to get priv and validating it again *after* stop() as it was done before commit fa795f45254 ("net: eth-uclass: avoid running start() twice without stop()") would fix this, and perhaps also make the issue with stop() and Ethernet gadget more obvious. A comment on why it needs to be repeated would also be useful. Would this be an acceptable improvement?
I agree that fixing the USB ethernet gadget is still the best solution, but until that happens, we could at least limit everyone's pain.
Best regards,
Niel Fourie
Signature · 3 lines
DENX Software Engineering GmbH, Managing Director: Wolfgang Denk HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany Phone: +49-8142-66989-21 Fax: +49-8142-66989-80 Email: [email protected]
Quoted 1 line · Niel Fourie
Hi Marek,
Hi,
Quoted 9 lines · Niel Fourie
[...]
Quoted 6 lines
Quoted 2 lines
It does however show that this patch introduces a bug -- this patch changes the order in which
priv->state=ETH_STATE_PASSIVE; is assigned from _after_ the ->stop callback to _before_ the -> stop callback. This breaksdrivers/net/ldpaa_eth/ldpaa_eth.cwhich checks thepriv->statein its ->stop callback, either on its own in non-DM case, or ineth_is_active()implementation in DM case. With this patch, the interface would never be stopped in the ->stop callback, because the condition (net_dev->state==ETH_STATE_PASSIVE) test in the ldpaa stop callback implementation would always be true.In drivers/net/ldpaa_eth/ldpaa_eth.c:ldpaa_eth_stop(), priv is of type
struct ldpaa_eth_priv*, defined indrivers/net/ldpaa_eth/ldpaa_eth.hand is accessed usingdev_get_priv().
Quoted 8 lines
Quoted 4 lines
In net/eth-uclass.c:eht_halt(), priv is of type struct eth_device_priv* and defined in the same .c file, and is accessed using
dev_get_uclass_priv(). As the structure is local to this file, nothing outside of this file should have any knowledge of its contents, and changing of the order of the calls should only impact this file.I sincerely hope that these two are not interfering with each other, otherwise we have much bigger problems...
Shucks, I was thrown off by the the fact that
net_devis of type structeth_device, and its member state is separate from structeth_device_stateand its member state, that I missed the implication ofeth_is_active()*setting* the value of struct eth_device_priv's state not *reading* it.Well spotted, you are correct. The patch in its current form would introduce that bug. Thank you for finding that.
Good, so we agree this patch introduces a bug.
Quoted 3 lines
Adding back the call to
dev_get_uclass_priv()to get priv and validating it again *after*stop()as it was done before commitfa795f45254("net: eth-uclass: avoid runningstart()twice withoutstop()") would fix this, and perhaps also make the issue withstop()and Ethernet gadget more obvious. A comment on why it needs to be repeated would also be useful. Would this be an acceptable improvement?I agree that fixing the USB ethernet gadget is still the best solution, but until that happens, we could at least limit everyone's pain.
See my reply to the previous email.
Keep the usb gadget device around, that should not be hard to implement and that should fix this problem once and for all, and for the future too.
On 12/5/22 13:06, Niel Fourie wrote:
Hi,
[...]
Quoted 18 lines
Quoted 18 lines
Quoted 5 lines
Quoted 3 lines
How come nobody triggered this problem with regular ethernet in U-Boot ?
If this is isolated to USB gadget ethernet, then please do not hack around this in core networking code, but rather fix the USB ethernet gadget itself. It seems that gadget code should not unregister the gadget in
drivers/usb/gadget/ether.c_usb_eth_halt(), at least not fully.The reason is simple, the regular ethernet drivers do not get removed on
stop()like for the gadget ethernet driver, and in their caseprivis still valid.The suggestion for a proper fix is in the last paragraph above -- do not unregister the usb ethernet gadget device in
halt(), keep the gadget device registered. Then the priv pointer would be valid. (*)Quoted 7 lines
I agree on your point that reworking the ethernet gadget code would be preferable, but this would be a much bigger effort (and if I were to do it, I would probably introduce even more bugs). I am not certain whether this would not also affect the non-DM gadget implementation as well, which still contain drivers like
ci_udc.cwhich does not appear to have been ported to DM gadget yet? (I only see DM USB there...)That said, I am not certain whether this is not also bug, as I am not certain whether the assumption that
privshould be available afterstop()is valid or not.The documentation at https://source.denx.de/u-boot/u-boot/-/blob/master/doc/develop/driver-model/ethernet.rst?plain=1#L135 states:
The **stop** function should turn off / disable the hardware and place it back in its reset state. It can be called at any time (before any call to the related
start()function), so make sure it can handle this sort of thing.The ->stop callback is supposed to stop the interface, turn off its DMA, but NOT deallocate the device (and its associated data) behind it.
Quoted 7 lines
Quoted 3 lines
Quoted 1 line
In such a complete reset state I am not certain whether the assumption that
privshould exist is still valid, at least not without another call todev_get_uclass_priv()and revalidating it first?In case a device is probe()d, its private data are also allocated and available, so yes, 'priv' pointer should still be valid.
Granted, the usb gadget driver implementation is problematic, and it definitely belongs on the TODO list.
That being said, priv not being valid at this stage is not a new problem, as validation for it after
stop()was explicitly added in commitc3211708("net: eth-uclass: Fix for DM USB ethernet support") for this reason 4 years ago. Fortunately in that case, it just fixed a null pointer de-reference, not corruption of freed memory.
I believe the aforementioned patch is already also wrong, since the priv pointer would be invalid for gadget ethernet driver got removed in the stop callback.
Quoted 8 lines
Quoted 8 lines
Quoted 3 lines
Lastly, this assumption that priv is still valid is rather new and it was introduced here:
https://source.denx.de/u-boot/u-boot/-/commit/fa795f452541ce07b33be603de36cac3c5d7dfcf
I disagree, the device private data are valid during the entire lifespan of the device. That assumption has been baked into the driver model itself and far predates that commit.
In case of a usb ethernet, the lifespan of the device starts with the 'bind' command which triggers the ->bind callback, and first use which triggers its ->probe callback. The lifespan ends with 'unbind' command or OS boot, which triggers ->remove callback and ->unbind callbacks.
Quoted 7 lines
Quoted 3 lines
Quoted 1 line
This commit appears to be a workaround for drivers which cannot deal with
stop()being called at any time as required in the above quoted documentation.This commit prevents network device ->start() from being called multiple times, which is a valid precaution, as calling start while the interface is already up would interfere with existing connection (e.g. the netconsole as mentioned in the commit message). That does not seem to be a workaround to me.
Ah yes, the commit message indeed states that some drivers (like
fec_mxc) cannot deal with multiplestart()calls, not multiplestop()calls. My mistake, good point.But preventing multiple start()s getting called is not what the code change in this commit is actually doing. It is instead inhibiting calling
stop()on devices for which priv is not valid or for whichpriv->runningis false. Therefore it is avoiding callingstop()on un-started devices.
I wonder if such a patch is needed to fix it ?
net/eth-uclass.c +1 -1
@@ -298,7 +298,7 @@ int eth_init(void)if (current) {debug ("Trying %s\n" , current->name); -if (device_active (current)) { +if (!eth_is_active (current)) {
Quoted 9 lines
Quoted 3 lines
Quoted 1 line
I would consider adding a workaround to a workaround in this case to be the lesser evil, as tracking down this bug in the first place was like looking for a needle in a haystack. This change would at least save everybody else from strange crashes in particular configurations without any negative impact. But this is fortunately not my decision. :)
Commit
fa795f45254("net: eth-uclass: avoid runningstart()twice withoutstop()") is as far as I can tell unrelated to this change and does not seem to me like a workaround.It does introduce writing to priv after
stop()is called without (re-)validating priv first, though.The code previously first called
stop(), then calleddev_get_uclass_priv()to get priv and then validated priv before writing to it, which avoids exactly the problem of priv no longer being valid.As mentioned above, this was explicitly addressed by commit
c3211708("net: eth-uclass: Fix for DM USB ethernet support").
I'm afraid this is just piling workarounds on top of workarounds.
Quoted 7 lines
Quoted 2 lines
It does however show that this patch introduces a bug -- this patch changes the order in which
priv->state=ETH_STATE_PASSIVE; is assigned from _after_ the ->stop callback to _before_ the -> stop callback. This breaksdrivers/net/ldpaa_eth/ldpaa_eth.cwhich checks thepriv->statein its ->stop callback, either on its own in non-DM case, or ineth_is_active()implementation in DM case. With this patch, the interface would never be stopped in the ->stop callback, because the condition (net_dev->state==ETH_STATE_PASSIVE) test in the ldpaa stop callback implementation would always be true.In drivers/net/ldpaa_eth/ldpaa_eth.c:ldpaa_eth_stop(), priv is of type
struct ldpaa_eth_priv*, defined indrivers/net/ldpaa_eth/ldpaa_eth.hand is accessed usingdev_get_priv().In net/eth-uclass.c:eht_halt(), priv is of type struct eth_device_priv* and defined in the same .c file, and is accessed using
dev_get_uclass_priv(). As the structure is local to this file, nothing outside of this file should have any knowledge of its contents, and changing of the order of the calls should only impact this file.
As you already found out yourself in subsequent email:
655 static void ldpaa_eth_stop(struct udevice *dev) 656 {
657 struct ldpaa_eth_priv *priv = dev_get_priv(dev);
...
666 #ifdef CONFIG_DM_ETH
667 if (!eth_is_active(dev))
...
The eth_is_active():
350 int eth_is_active(struct udevice *dev)
351 {
352 struct eth_device_priv *priv;
353
354 if (!dev || !device_active(dev))
355 return 0;
356
357 priv = dev_get_uclass_priv(dev);
358 return priv->state == ETH_STATE_ACTIVE;
359 }
does access dev_get_uclass_priv(dev)->priv .
Quoted 6 lines
I sincerely hope that these two are not interfering with each other, otherwise we have much bigger problems...
Quoted 3 lines
The proper fix is in the usb ethernet gadget code, see (*) above, let's not pile workarounds onto already difficult to maintain code.
[...]
Agreed. The problem is unfortunately just that I don't see that happening simply to fix this bug.
Please, do not pile workarounds on top of workarounds, there is too many of those already as you found out above. Each one just slaps something on, which later on breaks and causes pain a few years down the line. This is not manageable in the long run.
The proper fix I believe is (*) above and it should not be hard to implement that one.