Skip to contentSkip to conversation

Do not disable driver and bus shutdown hook when class shutdown hook is set.

PATCH Superseded Michal Suchanek 4 participants 8 messages Aug 9, 2017 – Aug 11, 2017

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Do not disable driver and bus shutdown hook when class shutdown hook is set.

Disabling the driver hook by setting class hook is totally sound design not prone to error as evidenced by the single implementation of the class hook.

Fixes d1bd4a792d39 ("tpm: Issue a TPM2_Shutdown for TPM2 devices.")
Fixes f77af1516584 ("Add "shutdown" to "struct class".")
---
 drivers/base/core.c         | 3 ++-
 drivers/char/tpm/tpm-chip.c | 9 +--------
 2 files changed, 3 insertions(+), 9 deletions(-)
drivers/base/core.c +2 -1
index 755451f684bc..2cf752dc1421 100644
@@ -2668,7 +2668,8 @@ void device_shutdown(void)
 			if (initcall_debug)
 				dev_info(dev, "shutdown\n");
 			dev->class->shutdown(dev);
-		} else if (dev->bus && dev->bus->shutdown) {
+		}
+		if (dev->bus && dev->bus->shutdown) {
 			if (initcall_debug)
 				dev_info(dev, "shutdown\n");
 			dev->bus->shutdown(dev);
drivers/char/tpm/tpm-chip.c +1 -8
index 67ec9d3d04f5..edf8fa553f5f 100644
@@ -164,14 +164,7 @@ static int tpm_class_shutdown(struct device *dev)
 		chip->ops = NULL;
 		up_write(&chip->ops_sem);
 	}
-	/* Allow bus- and device-specific code to run. Note: since chip->ops
-	 * is NULL, more-specific shutdown code will not be able to issue TPM
-	 * commands.
-	 */
-	if (dev->bus && dev->bus->shutdown)
-		dev->bus->shutdown(dev);
-	else if (dev->driver && dev->driver->shutdown)
-		dev->driver->shutdown(dev);
+
 	return 0;
 }
 
Signature · 1 line
2.10.2
#1 lore raw
Quoted 3 lines · Michal Suchanek

Disabling the driver hook by setting class hook is totally sound design not prone to error as evidenced by the single implementation of the class hook.

It was done this was for consistency, if you look at the full code:

                if (dev->class && dev->class->shutdown) {
                        if (initcall_debug)
                                dev_info(dev, "shutdown\n");
                        dev->class->shutdown(dev);
                } else if (dev->bus && dev->bus->shutdown) {
                        if (initcall_debug)
                                dev_info(dev, "shutdown\n");
                        dev->bus->shutdown(dev);
                } else if (dev->driver && dev->driver->shutdown) {
                        if (initcall_debug)
                                dev_info(dev, "shutdown\n");
                        dev->driver->shutdown(dev);
                }

The bus disables the driver callback, on the expectation that the bus implementation will do it.

Existing bus implementations do properly chain to driver shutdown (eg look at mmc_bus_shutdown) and it appears to have been written like this so that the bus can insert code before and after calling the driver shutdown.

Making class act differently from bus seems very confusing, IHMO, which why the TPM patch was written to follow the existing pattern.

Jason

#2 lore raw
Quoted 21 lines · On Wed, 9 Aug 2017 15:52:02 -0600 Jason Gunthorpe…
Quoted 3 lines · Michal Suchanek

Disabling the driver hook by setting class hook is totally sound design not prone to error as evidenced by the single implementation of the class hook.

It was done this was for consistency, if you look at the full code:

                if (dev->class && dev->class->shutdown) {
                        if (initcall_debug)
                                dev_info(dev, "shutdown\n");
                        dev->class->shutdown(dev);
                } else if (dev->bus && dev->bus->shutdown) {
                        if (initcall_debug)
                                dev_info(dev, "shutdown\n");
                        dev->bus->shutdown(dev);
                } else if (dev->driver && dev->driver->shutdown) {
                        if (initcall_debug)
                                dev_info(dev, "shutdown\n");
                        dev->driver->shutdown(dev);
                }
Quoted 2 lines

The bus disables the driver callback, on the expectation that the bus implementation will do it.

Which is totally sound design not prone to errors.

Quoted 3 lines

Existing bus implementations do properly chain to driver shutdown (eg look at mmc_bus_shutdown) and it appears to have been written like

Neither isa nor ibmebus does. These are two random buses I tried to look at.

Quoted 2 lines

this so that the bus can insert code before and after calling the driver shutdown.

So basically there is bus pre-shutdown and post-shutdown hook jumbled together in one function. While I can understand the concept of post-shutdown hook I wonder what gross hack would require a pre-shutdown hook.

Quoted 3 lines

Making class act differently from bus seems very confusing, IHMO, which why the TPM patch was written to follow the existing pattern.

The Linux development process at its best. There is poor design implemented so when touching the code it is extended to worse because it is smaller patch more likely to get past maintainers than fixing the mess.

If you argue the existing way is the best could you please give an actual technical argument for implementing the hooks this way?

Thanks

Michal

#3 lore raw
Quoted 4 lines · Michal Suchánek
Quoted 2 lines

The bus disables the driver callback, on the expectation that the bus implementation will do it.

Which is totally sound design not prone to errors.

Well, I agree it isn't the easiest...

Quoted 5 lines
Quoted 2 lines

Existing bus implementations do properly chain to driver shutdown (eg look at mmc_bus_shutdown) and it appears to have been written like

Neither isa nor ibmebus does. These are two random buses I tried to look at.

I'm not following, I see this:

static void ibmebus_bus_device_shutdown(struct device *dev)

{
        struct platform_device *of_dev = to_platform_device(dev);
        struct platform_driver *drv = to_platform_driver(dev->driver);

        if (dev->driver && drv->shutdown)
                drv->shutdown(of_dev);
}

It looks to me like in this case the struct device_driver shutdown is not used, and instead the struct platform_driver shutdown is called.

Quoted 5 lines
Quoted 2 lines

this so that the bus can insert code before and after calling the driver shutdown.

So basically there is bus pre-shutdown and post-shutdown hook jumbled together in one function.

and a redirect, apparently.

Quoted 2 lines

While I can understand the concept of post-shutdown hook I wonder what gross hack would require a pre-shutdown hook.

TPM requires pre-shutdown. It fences off access to the TPM so the TPM can have a clean shutdown. We cannot do a clean TPM shutdown if there is a possibility of another transaction being send to the TPM. TPM's have non-volatile state and record if they were not shut down properly, so doing this is actually quite important.

Quoted 2 lines

The Linux development process at its best. There is poor design implemented so when touching the code it is extended to worse because

I'm not sure I completely agree, there is obviously a lot going on with bus->shutdown.

If you want to go ahead with your patch then please also rename the class shutdown to shutdown_pre to make it clear it is doing something different.

Quoted 2 lines

it is smaller patch more likely to get past maintainers than fixing the mess.

Yes, this is probably true, the TPM fix needed to be back ported to -stable.

Jason

#4 lore raw
Quoted 19 lines · Jason Gunthorpe
Quoted 5 lines · Michal Suchánek
Quoted 2 lines

Existing bus implementations do properly chain to driver shutdown (eg look at mmc_bus_shutdown) and it appears to have been written like

Neither isa nor ibmebus does. These are two random buses I tried to look at.

I'm not following, I see this:

static void ibmebus_bus_device_shutdown(struct device *dev)

{
        struct platform_device *of_dev = to_platform_device(dev);
        struct platform_driver *drv = to_platform_driver(dev->driver);

        if (dev->driver && drv->shutdown)
                drv->shutdown(of_dev);
}
Quoted 2 lines

It looks to me like in this case the struct device_driver shutdown is not used, and instead the struct platform_driver shutdown is called.

And it is not used even if a device driver sets it and expects it to run.

Quoted 7 lines
Quoted 6 lines
Quoted 3 lines

this so that the bus can insert code before and after calling the driver shutdown.

So basically there is bus pre-shutdown and post-shutdown hook jumbled together in one function.

Quoted 6 lines

and a redirect, apparently.

Quoted 2 lines

While I can understand the concept of post-shutdown hook I wonder what gross hack would require a pre-shutdown hook.

TPM requires pre-shutdown.

Yes, a class pre-shutdown. Not a bus pre-shutdown, however.

I have no idea what business has a bus driver before a device shuts down.

Quoted 10 lines
Quoted 3 lines

The Linux development process at its best. There is poor design implemented so when touching the code it is extended to worse because

I'm not sure I completely agree, there is obviously a lot going on with bus->shutdown.

If you want to go ahead with your patch then please also rename the class shutdown to shutdown_pre to make it clear it is doing something different.

That makes sense.

Thanks

Michal

#5 lore raw
Quoted 15 lines · Michal Suchánek
Quoted 14 lines · Jason Gunthorpe
Quoted 5 lines · Michal Suchánek
Quoted 2 lines

Existing bus implementations do properly chain to driver shutdown (eg look at mmc_bus_shutdown) and it appears to have been written like

Neither isa nor ibmebus does. These are two random buses I tried to look at.

I'm not following, I see this:

static void ibmebus_bus_device_shutdown(struct device *dev)

{
        struct platform_device *of_dev = to_platform_device(dev);
        struct platform_driver *drv = to_platform_driver(dev->driver);
Quoted 8 lines
Quoted 6 lines
        if (dev->driver && drv->shutdown)
                drv->shutdown(of_dev);
}

It looks to me like in this case the struct device_driver shutdown is not used, and instead the struct platform_driver shutdown is called.

And it is not used even if a device driver sets it and expects it to run.

Which is the kind of landmine it is best avoided in drivers/, so it would be nice to get WARN_ON() during device register when dev->shutdown() methods *that are going to be ignored* because of class/bus handlers are non-NULL...

Either that, or the device->shutdown() methods should always be called, and drivers that should not/need not have them should be fixed...

Signature · 1 line
  Henrique Holschuh
#6 lore raw
Quoted 26 lines · On Fri, 11 Aug 2017 12:28:57 -0300 Henrique de Moraes Holschuh…
Quoted 25 lines · Michal Suchánek
Quoted 23 lines · Jason Gunthorpe
Quoted 6 lines · Michal Suchánek
Quoted 3 lines

Existing bus implementations do properly chain to driver shutdown (eg look at mmc_bus_shutdown) and it appears to have been written like

Neither isa nor ibmebus does. These are two random buses I tried to look at.

I'm not following, I see this:

static void ibmebus_bus_device_shutdown(struct device *dev) {
struct platform_device *of_dev = to_platform_device(dev); struct platform_driver *drv =
to_platform_driver(dev->driver);

        if (dev->driver && drv->shutdown)
                drv->shutdown(of_dev);
}

It looks to me like in this case the struct device_driver shutdown is not used, and instead the struct platform_driver shutdown is called.

Quoted 7 lines
Quoted 2 lines

And it is not used even if a device driver sets it and expects it to run.

Which is the kind of landmine it is best avoided in drivers/, so it would be nice to get WARN_ON() during device register when dev->shutdown() methods *that are going to be ignored* because of class/bus handlers are non-NULL...

We actually have the warning:

int driver_register(struct device_driver *drv)

{
        int ret;
        struct device_driver *other;

        BUG_ON(!drv->bus->p);

        if ((drv->bus->probe && drv->probe) ||
            (drv->bus->remove && drv->remove) ||
            (drv->bus->shutdown && drv->shutdown))
                printk(KERN_WARNING "Driver '%s' needs updating -
        please use " "bus_type methods\n", drv->name);

So presumably setting both bus and driver shutdown is bogus while setting both class and bus shutdown is fine and expected. So the logic in the initial class shutdown addition that disables driver and bus shutdown is bogus - there is no reason to disable them.

Given that nobody pointed out this shows how well understood this interface is :/

Thanks

Michal

#7 lore raw
In reply to Jason Gunthorpe
Quoted 29 lines · Jason Gunthorpe
Quoted 3 lines · Michal Suchanek

Disabling the driver hook by setting class hook is totally sound design not prone to error as evidenced by the single implementation of the class hook.

It was done this was for consistency, if you look at the full code:

                if (dev->class && dev->class->shutdown) {
                        if (initcall_debug)
                                dev_info(dev, "shutdown\n");
                        dev->class->shutdown(dev);
                } else if (dev->bus && dev->bus->shutdown) {
                        if (initcall_debug)
                                dev_info(dev, "shutdown\n");
                        dev->bus->shutdown(dev);
                } else if (dev->driver && dev->driver->shutdown) {
                        if (initcall_debug)
                                dev_info(dev, "shutdown\n");
                        dev->driver->shutdown(dev);
                }

The bus disables the driver callback, on the expectation that the bus implementation will do it.

Existing bus implementations do properly chain to driver shutdown (eg look at mmc_bus_shutdown) and it appears to have been written like this so that the bus can insert code before and after calling the driver shutdown.

Quoted 4 lines

Making class act differently from bus seems very confusing, IHMO, which why the TPM patch was written to follow the existing pattern.

Jason

There's also more fundamental problem. There are Fixes tags but no real regression. Even if this patch made sense I would not consider it as a bug fix.

/Jarkko