Make all it87 drivers SMP safe
v6 cover Make all it87 drivers SMP safe
There are 3 different drivers that touch the it87 hardware registers. The 3 drivers have been written independently and access the it87 hardware registers assuming they are the only driver accessing it. This change attempts to serialize access to the hardware by using "request_muxed_region" macro defined by Alan Cox. Call to this macro will hold off the requestor if the resource is currently busy. The use of the above macro makes it possible to get rid of spinlocks in it8712f_wdt.c and it87_wdt.c watchdog drivers. This also greatly simplifies the implementation of it87_wdt.c driver.
01 - Changes to it87 watchdog driver to use "request_muxed_region"
drivers/watchdog/it8712f_wdt.c drivers/watchdog/it87_wdt.c
02 - Chages to hwmon it87 driver to use "request_muxed_region"
drivers/hwmon/it87.c
drivers/hwmon/it87.c | 30 +++++++++++++- drivers/watchdog/it8712f_wdt.c | 50 +++++++++++++++++++++--- drivers/watchdog/it87_wdt.c | 80 +++++++++++++++++++++------------------ 3 files changed, 113 insertions(+), 47 deletions(-)
Patch History:
v6:
- Pay attention to value returned by request_muxed_region. The first call to request_muxed_region will attempt 10 times to reserve the region before it gives up. This will typically get called from the driver init routines. If this succeeds then subsequent calls wait forever for the resource to be available.
v5:
- Remove unnecessary while from superio_enter.
v4:
- Remove extra braces in superio_enter routines.
v3:
- Totally abandon the spinlock based approach and use "request_muxed_region" to hold off requestors if the resource is busy.
v2:
- More verbose patch headers. Add In-Reply-To: field.
_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
PATCH v6 1/2 Use "request_muxed_region" in it87 watchdog drivers
01 - Changes to it87 watchdog driver to use "request_muxed_region" Serialize access to the hardware by using "request_muxed_region" macro defined by Alan Cox. Call to this macro will hold off the requestor if the resource is currently busy. The first call to request_muxed_region will attempt 10 times to reserve the region before it gives up. This will typically get called from the driver init routines. If this succeeds then subsequent calls wait forever for the resource to be available.
The use of the above macro makes it possible to get rid of spinlocks in it8712f_wdt.c and it87_wdt.c watchdog drivers. This also greatly simplifies the implementation of it87_wdt.c driver.
---
drivers/watchdog/it8712f_wdt.c +43 -7
@@ -51,7 +51,6 @@ MODULE_PARM_DESC(nowayout, "Disable watchdog shutdown on close");static unsigned long wdt_open;static unsigned expect_close; -static spinlock_t io_lock;static unsigned char revision;/* Dog Food address - We use the game port address */
@@ -121,9 +120,46 @@ static inline void superio_select(int ldn)outb (ldn,VAL ); } -static inline void superio_enter (void ) +static inline int +try_superio_enter (void ) { -spin_lock (&io_lock); +int num_tries =10 ; +/* +* Try to reserve REG and REG + 1 for exclusive access. +* Give up after 10 attempts. +*/ +while (num_tries--) { +if (!request_muxed_region (REG ,2 ,NAME )) { +if (num_tries) +continue ; + +/* +* Someone is holding the region. Give up. +*/ +pr_err ("I/O address 0x%04x already in use\n" ,REG ); +return -EBUSY ; + } + +break ; + } + +outb (0x87 ,REG ); +outb (0x01 ,REG ); +outb (0x55 ,REG ); +outb (0x55 ,REG ); +return 0 ; +} + +static inline void +superio_enter (void ) +{ +/* +* Reserve REG and REG + 1 for exclusive access. +* Wait forever if you have to to gain access. +*/ +while (!request_muxed_region (REG ,2 ,NAME )) +continue ; +outb (0x87 ,REG );outb (0x01 ,REG );outb (0x55 ,REG );
@@ -134,7 +170,7 @@ static inline void superio_exit(void) {outb (0x02 ,REG );outb (0x02 ,VAL ); -spin_unlock (&io_lock); +release_region (REG ,2 ); }static inline void it8712f_wdt_ping (void )
@@ -341,7 +377,9 @@ static int __init it8712f_wdt_find(unsigned short *address)int err = -ENODEV ;int chip_type; -superio_enter (); +if (try_superio_enter ()) +return -EBUSY ; + chip_type =superio_inw (DEVID );if (chip_type !=IT8712F_DEVID ) goto exit;
@@ -382,8 +420,6 @@ static int __init it8712f_wdt_init(void) {int err =0 ; -spin_lock_init (&io_lock); -if (it8712f_wdt_find (&address))return -ENODEV ;
drivers/watchdog/it87_wdt.c +43 -37
224 lines not on this page · raw message
Signature · 7 lines
1.7.3.1 _______________________________________________ lm-sensors mailing list [email protected] http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
Re: [PATCH v6 1/2] Use "request_muxed_region" in it87 watchdog drivers
Quoted 46 lines · Nat Gurumoorthy
01 - Changes to it87 watchdog driver to use "
request_muxed_region" Serialize access to the hardware by using "request_muxed_region" macro defined by Alan Cox. Call to this macro will hold off the requestor if the resource is currently busy. The first call torequest_muxed_regionwill attempt 10 times to reserve the region before it gives up. This will typically get called from the driver init routines. If this succeeds then subsequent calls wait forever for the resource to be available.The use of the above macro makes it possible to get rid of spinlocks in
it8712f_wdt.candit87_wdt.cwatchdog drivers. This also greatly simplifies the implementation ofit87_wdt.cdriver.Signed-off-by Nat Gurumoorthy <[email protected]>---
drivers/watchdog/it8712f_wdt.c +12
-3@@ -51,7 +51,6 @@ MODULE_PARM_DESC(nowayout, "Disable watchdog shutdown on close");static unsigned long wdt_open;static unsigned expect_close; -static spinlock_t io_lock;static unsigned char revision;/* Dog Food address - We use the game port address */ @@ -121,9 +120,46 @@ static inline void superio_select(int ldn)outb (ldn,VAL ); } -static inline void superio_enter (void ) +static inline int +try_superio_enter (void ) { -spin_lock (&io_lock); +int num_tries =10 ; +/* +* Try to reserve REG and REG + 1 for exclusive access. +* Give up after 10 attempts. +*/ +while (num_tries--) { +if (!request_muxed_region (REG ,2 ,NAME )) { +if (num_tries) +continue ; +
drivers/watchdog/it8712f_wdt.c static inline void superio_select(int ldn) · line 145
Quoted 10 lines
drivers/watchdog/it8712f_wdt.c +10
-0+/* +* Someone is holding the region. Give up. +*/ +pr_err ("I/O address 0x%04x already in use\n" ,REG ); +return -EBUSY ; + } + +break ; + } +
This is way too complicated. Just return an error if request_muxed_region fails, like all other callers of request_muxed_region do.
Guenter
_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
Re: [PATCH v6 1/2] Use "request_muxed_region" in it87 watchdog drivers
Quoted 4 lines · Guenter Roeck
This is way too complicated. Just return an error if
request_muxed_regionfails, like all other callers ofrequest_muxed_regiondo.
Guenter,
This maybe a little complicated but it is a solution that will
correctly deal with the following scenario:
Hardware: it8712f
cpu: Multicore
kernel: smp
Modules being loaded during initialization: it8712f_wtd and w83697hf
The problem is one of the cores is halfway through initializing it8712f_wdt and another core is just starting to run the "w83697hf_check_wdt" which is going to call "request_region" to reserve 0x2e 0x2f region. Just after that call on the other core it8712f_wdt_init calls it8712f_wdt_disable which calls superio_enter which will call "request_muxed_region" for the exact same region. This is guaranteed to fail because as of now w83697hf is one of the many non compliant drivers who are calling "request_region". The w83697hf will do the probe and conclude there is no w83697hf chip in the system and abort rest of the driver initialization. Meanwhile the it8712f_wtd driver initialization will fail. This will be one of those intermittent failures that make most kernel device driver writers prematurely bald :-).
The way I have reworked the driver will survive the above scenario.
Regards
Nat
Quoted 4 lines
Guenter
Signature · 7 lines
Regards Nat Gurumoorthy AB6SJ _______________________________________________ lm-sensors mailing list [email protected] http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
Re: [PATCH v6 1/2] Use "request_muxed_region" in it87 watchdog drivers
Quoted 28 lines · Natarajan Gurumoorthy
Quoted 4 lines · Guenter Roeck
This is way too complicated. Just return an error if
request_muxed_regionfails, like all other callers ofrequest_muxed_regiondo.Guenter,
This maybe a little complicated but it is a solution that will
correctly deal with the following scenario:
Hardware: it8712f
cpu: Multicore
kernel: smp
Modules being loaded during initialization:it8712f_wtdand w83697hfThe problem is one of the cores is halfway through initializing
it8712f_wdtand another core is just starting to run the "w83697hf_check_wdt" which is going to call "request_region" to reserve0x2e0x2fregion. Just after that call on the other coreit8712f_wdt_initcallsit8712f_wdt_disablewhich callssuperio_enterwhich will call "request_muxed_region" for the exact same region. This is guaranteed to fail because as of now w83697hf is one of the many non compliant drivers who are calling "request_region". The w83697hf will do the probe and conclude there is no w83697hf chip in the system and abort rest of the driver initialization. Meanwhile theit8712f_wtddriver initialization will fail. This will be one of those intermittent failures that make most kernel device driver writers prematurely bald :-).
Quoted 2 lines
The way I have reworked the driver will survive the above scenario.
Unless I am missing something, the problem is that there is another driver calling request_region() instead of request_muxed_region().
w83697hf_check_wdt() (and other drivers doing the same) should call request_muxed_region(). Otherwise, the same problem could happen with the caller(s) of request_region() - those calls could fail as well. Plus, other callers of request_muxed_reason() not implementing your retry code could end up with the same problem, ie with spurious failures.
I think we will have to find a solution which does not require retries when calling request_muxed_region(). On the other side, others know this code much better than I do, so maybe someone has a better solution.
Thanks,
Guenter
_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
Re: [PATCH v6 1/2] Use "request_muxed_region" in it87 watchdog drivers
Quoted 9 lines · Guenter Roeck
Unless I am missing something, the problem is that there is another driver calling
request_region()instead ofrequest_muxed_region().
w83697hf_check_wdt()(and other drivers doing the same) should callrequest_muxed_region(). Otherwise, the same problem could happen with the caller(s) ofrequest_region()- those calls could fail as well. Plus, other callers ofrequest_muxed_reason()not implementing your retry code could end up with the same problem, ie with spurious failures.
Quoted 4 lines
I think we will have to find a solution which does not require retries when calling
request_muxed_region(). On the other side, others know this code much better than I do, so maybe someone has a better solution.
Guenter,
Your conclusion is on the money. request_muxed_region will fail if
someone else called request_region for the same region. The knife cuts both ways and reguest_region will fail when a request_muxed_region calll has been made for the same zone. I am not sure what happens when the second driver calls request_muxed_region for only part of the region that has been reserved by another driver. This is of course one of the problems. The other problem is that there are drivers which don't call either of those 2 interfaces and just go ahead and clobber the 0x2e 0x2f zone because they think they own all of it. I created a list drivers in hwmon and watchdog directories that need to be fixed in one of my earlier emails. John Delvare replied that are other drivers in parport that all need fixing. I got started on this because we use 2 different it87 drivers (hwmon/it87.c and watchdog/it8712f_wdt.c) concurrently and had to solve this problem.
Regards
Nat
Quoted 3 lines
Thanks,
Guenter
Signature · 7 lines
Regards Nat Gurumoorthy AB6SJ _______________________________________________ lm-sensors mailing list [email protected] http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
Re: [PATCH v6 1/2] Use "request_muxed_region" in it87 watchdog drivers
Quoted 15 lines · Natarajan Gurumoorthy
Quoted 9 lines · Guenter Roeck
Unless I am missing something, the problem is that there is another driver calling
request_region()instead ofrequest_muxed_region().
w83697hf_check_wdt()(and other drivers doing the same) should callrequest_muxed_region(). Otherwise, the same problem could happen with the caller(s) ofrequest_region()- those calls could fail as well. Plus, other callers ofrequest_muxed_reason()not implementing your retry code could end up with the same problem, ie with spurious failures.Quoted 4 lines
I think we will have to find a solution which does not require retries when calling
request_muxed_region(). On the other side, others know this code much better than I do, so maybe someone has a better solution.
Quoted 16 lines
Guenter,
Your conclusion is on the money.request_muxed_regionwill fail if
someone else calledrequest_regionfor the same region. The knife cuts both ways andreguest_regionwill fail when arequest_muxed_regioncalll has been made for the same zone. I am not sure what happens when the second driver callsrequest_muxed_regionfor only part of the region that has been reserved by another driver. This is of course one of the problems. The other problem is that there are drivers which don't call either of those 2 interfaces and just go ahead and clobber the0x2e0x2fzone because they think they own all of it. I created a list drivers in hwmon and watchdog directories that need to be fixed in one of my earlier emails. John Delvare replied that are other drivers in parport that all need fixing. I got started on this because we use 2 different it87 drivers (hwmon/it87.candwatchdog/it8712f_wdt.c) concurrently and had to solve this problem.
I have seen the list. I don't think you should fix everything in one go. First step might be to get the w83697hf and it87 to work together, then go from there.
Is there a reason for loading (or trying to load) both the it87 and the w83697hf driver at the same time ? Those drivers are usually only loaded if the respective chip is known to exist. If there is no reason to try loading both drivers, a simple workaround would be to not do it.
Thanks,
Guenter
_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
Re: [PATCH v6 1/2] Use "request_muxed_region" in it87 watchdog drivers
Quoted 9 lines · Guenter Roeck
I have seen the list. I don't think you should fix everything in one go. First step might be to get the w83697hf and it87 to work together, then go from there.
Is there a reason for loading (or trying to load) both the it87 and the w83697hf driver at the same time ? Those drivers are usually only loaded if the respective chip is known to exist. If there is no reason to try loading both drivers, a simple workaround would be to not do it.
Guenter,
I agree the above should never happen. The only way the 2
drivers will be loaded at the same time is a misconfigured kernel where these 2 drivers get built and the rc scripts end up loading them too. If we are agreed that I suggest we make the superio_enter routine be the following:
static inline voidsuperio_enter(void)
{
/*
* Reserve REG and REG + 1 for exclusive access.
*/
while (!request_muxed_region(REG, 2, WATCHDOG_NAME))
continue;
outb(0x87, REG);
outb(0x01, REG);
outb(0x55, REG);
outb(0x55, REG);
}What I am suggesting is not returning an error and instead keep calling "request_muxed_region" till it succeeds. If superio_enter returns an error then we will have to rewrite a large chunk of it8712f_wdt to deal with it. There are 8-9 calls to superio_enter. We will have supreio_enter returning errors at awkward places in the driver where the current logic has no code to deal with errors.
In case of properly written drivers the while loop will eventually exit. I agree this is ugly as sin but it limits the perturbation of the driver. Feedback please.
Regards
Nat
Quoted 4 lines
Thanks,
Guenter
Signature · 7 lines
Regards Nat Gurumoorthy AB6SJ _______________________________________________ lm-sensors mailing list [email protected] http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
Re: [PATCH v6 1/2] Use "request_muxed_region" in it87 watchdog drivers
Quoted 18 lines · Natarajan Gurumoorthy
Quoted 9 lines · Guenter Roeck
I have seen the list. I don't think you should fix everything in one go. First step might be to get the w83697hf and it87 to work together, then go from there.
Is there a reason for loading (or trying to load) both the it87 and the w83697hf driver at the same time ? Those drivers are usually only loaded if the respective chip is known to exist. If there is no reason to try loading both drivers, a simple workaround would be to not do it.
Guenter,
I agree the above should never happen. The only way the 2
drivers will be loaded at the same time is a misconfigured kernel where these 2 drivers get built and the rc scripts end up loading them too. If we are agreed that I suggest we make thesuperio_enterroutine be the following:
Quoted 9 lines
static inline void
superio_enter(void){ /* * Reserve REG and REG + 1 for exclusive access. */ while (!request_muxed_region(REG, 2, WATCHDOG_NAME)) continue;
At least for my part, I would not agree to that. If another driver misbehaves and does not release the region, one of your CPU cores will be stuck in an endless loop.
Quoted 6 lines
outb(0x87, REG); outb(0x01, REG); outb(0x55, REG); outb(0x55, REG); }
Quoted 7 lines
What I am suggesting is not returning an error and instead keep calling "
request_muxed_region" till it succeeds. Ifsuperio_enterreturns an error then we will have to rewrite a large chunk ofit8712f_wdtto deal with it. There are 8-9 calls tosuperio_enter. We will havesupreio_enterreturning errors at awkward places in the driver where the current logic has no code to deal with errors.
Can't help it. I browsed through it earlier and didn't think it was that bad. Just pass the error on to the next level until you can return it.
Guenter
Quoted 4 lines
In case of properly written drivers the while loop will eventually exit. I agree this is ugly as sin but it limits the perturbation of the driver. Feedback please.
Quoted 14 lines
Regards
NatQuoted 4 lines
Thanks,
GuenterSignature · 2 lines
Regards Nat Gurumoorthy AB6SJ
_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
Re: [lm-sensors] [PATCH v6 1/2] Use "request_muxed_region" in it87 watchdog drivers
In reply to Natarajan GurumoorthyHi,
Quoted 18 lines · Natarajan Gurumoorthy
Quoted 9 lines · Guenter Roeck
I have seen the list. I don't think you should fix everything in one go. First step might be to get the w83697hf and it87 to work together, then go from there.
Is there a reason for loading (or trying to load) both the it87 and the w83697hf driver at the same time ? Those drivers are usually only loaded if the respective chip is known to exist. If there is no reason to try loading both drivers, a simple workaround would be to not do it.
Guenter,
I agree the above should never happen. The only way the 2
drivers will be loaded at the same time is a misconfigured kernel where these 2 drivers get built and the rc scripts end up loading them too. If we are agreed that I suggest we make thesuperio_enterroutine be the following:
Quoted 9 lines
static inline void
superio_enter(void){ /* * Reserve REG and REG + 1 for exclusive access. */ while (!request_muxed_region(REG, 2, WATCHDOG_NAME)) continue;
Natajaran,
Please stop being so stubborn. XXX people have told you not to put a while loop here, but simply to check for an error and propagate it (in all places where superio_enter gets called).
Your bogus argument of the driver then failing to load when another non muxed superio driver loads is really far fetched. No system ever has 2 super io chips. So as long as all ite drivers use the muxed version all will be fine.
And if some unforeseen scenario happens where the request_muxed_region fails, we will get a bug report about a driver failing to load (*). Which is a lot better for the end user then being stuck in an endless loop.
Regards,
Hans
*) And deal with that then.
_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
PATCH v6 2/2 Use "request_muxed_region" in it87 hwmon drivers
In reply to Nat Gurumoorthy02 - Chages to hwmon it87 driver to use "request_muxed_region" Serialize access to the hardware by using "request_muxed_region" macro defined by Alan Cox. Call to this macro will hold off the requestor if the resource is currently busy. The first call to request_muxed_region will attempt 10 times to reserve the region before it gives up. This will typically get called from the driver init routines. If this succeeds then subsequent calls wait forever for the resource to be available.
---
drivers/hwmon/it87.c +28 -4
@@ -108,13 +108,34 @@ superio_select(int ldn)outb (ldn,VAL ); } -static inline void -superio_enter (void ) -{ +static inline int +try_superio_enter (void ) +{ +int num_tries =10 ; +/* +* Try to reserve REG and REG + 1 for exclusive access. +* Give up after 10 attempts. +*/ +while (num_tries--) { +if (!request_muxed_region (REG ,2 ,DRVNAME )) { +if (num_tries) +continue ; + +/* +* Someone is holding the region. Give up. +*/ +pr_err ("I/O address 0x%04x already in use\n" ,REG ); +return -EBUSY ; + } + +break ; + } +outb (0x87 ,REG );outb (0x01 ,REG );outb (0x55 ,REG );outb (0x55 ,REG ); +return 0 ; } static inline void
@@ -122,6 +143,7 @@ superio_exit(void) {outb (0x02 ,REG );outb (0x02 ,VAL ); +release_region (REG ,2 ); }/* Logical device 4 registers */
@@ -1546,7 +1568,9 @@ static int __init it87_find(unsigned short *address,u16 chip_type;const char *board_vendor, *board_name; -superio_enter (); +if (try_superio_enter ()) +return -EBUSY ; + chip_type = force_id ? force_id :superio_inw (DEVID );switch (chip_type) {
Signature · 7 lines
1.7.3.1 _______________________________________________ lm-sensors mailing list [email protected] http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
Quoted 14 lines · Nat Gurumoorthy
There are 3 different drivers that touch the it87 hardware registers. The 3 drivers have been written independently and access the it87 hardware registers assuming they are the only driver accessing it. This change attempts to serialize access to the hardware by using "
request_muxed_region" macro defined by Alan Cox. Call to this macro will hold off the requestor if the resource is currently busy. The use of the above macro makes it possible to get rid of spinlocks init8712f_wdt.candit87_wdt.cwatchdog drivers. This also greatly simplifies the implementation ofit87_wdt.cdriver.01 - Changes to it87 watchdog driver to use "
request_muxed_region"drivers/watchdog/it8712f_wdt.cdrivers/watchdog/it87_wdt.c
Quoted 7 lines
02 - Chages to hwmon it87 driver to use "
request_muxed_region"drivers/hwmon/it87.cdrivers/hwmon/it87.c | 30 +++++++++++++- drivers/watchdog/it8712f_wdt.c | 50 +++++++++++++++++++++--- drivers/watchdog/it87_wdt.c | 80 +++++++++++++++++++++------------------ 3 files changed, 113 insertions(+), 47 deletions(-)
FYI,
$ head -n 4 drivers/media/rc/ite-cir.c /* * Driver for ITE Tech Inc. IT8712F/IT8512 CIR * * Copyright (C) 2010 Juan Jesús García de Soria <[email protected]>
Signature · 2 lines
Jarod Wilson [email protected]