2012-09-08 01:53:45

by Wei Yongjun

[permalink] [raw]
Subject: [PATCH v2] NFC: pn544_hci: move the dereference below the NULL test

From: Wei Yongjun <[email protected]>

The dereference should be moved below the NULL test.

spatch with a semantic match is used to found this.
(http://coccinelle.lip6.fr/)

Signed-off-by: Wei Yongjun <[email protected]>
---
drivers/nfc/pn544_hci.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/nfc/pn544_hci.c b/drivers/nfc/pn544_hci.c
index aa71807..7b20f2d 100644
--- a/drivers/nfc/pn544_hci.c
+++ b/drivers/nfc/pn544_hci.c
@@ -341,13 +341,14 @@ flush:
static irqreturn_t pn544_hci_irq_thread_fn(int irq, void *dev_id)
{
struct pn544_hci_info *info = dev_id;
- struct i2c_client *client = info->i2c_dev;
+ struct i2c_client *client;
struct sk_buff *skb = NULL;
int r;

BUG_ON(!info);
BUG_ON(irq != info->i2c_dev->irq);

+ client = info->i2c_dev;
dev_dbg(&client->dev, "IRQ\n");

if (info->hard_fault != 0)




2012-09-10 13:10:20

by Kalle Valo

[permalink] [raw]
Subject: Re: [PATCH v2] NFC: pn544_hci: move the dereference below the NULL test

Wei Yongjun <[email protected]> writes:

> - BUG_ON(!info);
> - BUG_ON(irq != info->i2c_dev->irq);
> + if (!info || irq != info->i2c_dev->irq) {
> + WARN_ON_ONCE(1);
> + return IRQ_NONE;
> + }

You can even simplify it to this:

if (WARN_ON_ONCE(!info || irq != info->i2c_dev->irq))
return IRQ_NONE;

I don't know if it matters here but if it's important to know which test
is failing having two different tests makes that easier, as the line
number will be different:

if (WARN_ON_ONCE(!info))
return IRQ_NONE;

if (WARN_ON_ONCE(irq != info->i2c_dev->irq))
return IRQ_NONE;

--
Kalle Valo

2012-09-08 12:37:49

by Larry Finger

[permalink] [raw]
Subject: Re: [PATCH v2] NFC: pn544_hci: move the dereference below the NULL test

On 09/07/2012 10:34 PM, Wei Yongjun wrote:
> On 09/08/2012 10:03 AM, Larry Finger wrote:
>> On 09/07/2012 08:53 PM, Wei Yongjun wrote:
>>> From: Wei Yongjun <[email protected]>
>>>
>>> The dereference should be moved below the NULL test.
>>>
>>> spatch with a semantic match is used to found this.
>>> (http://coccinelle.lip6.fr/)
>>>
>>> Signed-off-by: Wei Yongjun <[email protected]>
>>> ---
>>>
>>
>> As long as you are reworking this section, doesn't BUG_ON seem a little harsh? I do not think the kernel should ever be crashed because a non-essential driver is messed up. As the patch would be a totally different change, it should be a new submission, but I think each of these conditions should trigger a WARN_ON_ONCE, and return IRQ_NONE when they occur.
>>
> You means the should be like this?
>
> diff --git a/drivers/nfc/pn544_hci.c b/drivers/nfc/pn544_hci.c
> index aa71807..e42b100 100644
> --- a/drivers/nfc/pn544_hci.c
> +++ b/drivers/nfc/pn544_hci.c
> @@ -341,13 +341,16 @@ flush:
> static irqreturn_t pn544_hci_irq_thread_fn(int irq, void *dev_id)
> {
> struct pn544_hci_info *info = dev_id;
> - struct i2c_client *client = info->i2c_dev;
> + struct i2c_client *client;
> struct sk_buff *skb = NULL;
> int r;
>
> - BUG_ON(!info);
> - BUG_ON(irq != info->i2c_dev->irq);
> + if (!info || irq != info->i2c_dev->irq) {
> + WARN_ON_ONCE(1);
> + return IRQ_NONE;
> + }
>
> + client = info->i2c_dev;
> dev_dbg(&client->dev, "IRQ\n");
>
> if (info->hard_fault != 0)

Yes, that looks better to me.

Larry



2012-09-08 02:03:47

by Larry Finger

[permalink] [raw]
Subject: Re: [PATCH v2] NFC: pn544_hci: move the dereference below the NULL test

On 09/07/2012 08:53 PM, Wei Yongjun wrote:
> From: Wei Yongjun <[email protected]>
>
> The dereference should be moved below the NULL test.
>
> spatch with a semantic match is used to found this.
> (http://coccinelle.lip6.fr/)
>
> Signed-off-by: Wei Yongjun <[email protected]>
> ---
> drivers/nfc/pn544_hci.c | 3 ++-
> 1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/nfc/pn544_hci.c b/drivers/nfc/pn544_hci.c
> index aa71807..7b20f2d 100644
> --- a/drivers/nfc/pn544_hci.c
> +++ b/drivers/nfc/pn544_hci.c
> @@ -341,13 +341,14 @@ flush:
> static irqreturn_t pn544_hci_irq_thread_fn(int irq, void *dev_id)
> {
> struct pn544_hci_info *info = dev_id;
> - struct i2c_client *client = info->i2c_dev;
> + struct i2c_client *client;
> struct sk_buff *skb = NULL;
> int r;
>
> BUG_ON(!info);
> BUG_ON(irq != info->i2c_dev->irq);
>
> + client = info->i2c_dev;

As long as you are reworking this section, doesn't BUG_ON seem a little harsh? I
do not think the kernel should ever be crashed because a non-essential driver is
messed up. As the patch would be a totally different change, it should be a new
submission, but I think each of these conditions should trigger a WARN_ON_ONCE,
and return IRQ_NONE when they occur.

Larry

> dev_dbg(&client->dev, "IRQ\n");
>
> if (info->hard_fault != 0)
>
>
> --
> To unsubscribe from this list: send the line "unsubscribe linux-wireless" in
> the body of a message to [email protected]
> More majordomo info at http://vger.kernel.org/majordomo-info.html
>


2012-09-08 03:34:36

by Wei Yongjun

[permalink] [raw]
Subject: Re: [PATCH v2] NFC: pn544_hci: move the dereference below the NULL test

On 09/08/2012 10:03 AM, Larry Finger wrote:
> On 09/07/2012 08:53 PM, Wei Yongjun wrote:
>> From: Wei Yongjun <[email protected]>
>>
>> The dereference should be moved below the NULL test.
>>
>> spatch with a semantic match is used to found this.
>> (http://coccinelle.lip6.fr/)
>>
>> Signed-off-by: Wei Yongjun <[email protected]>
>> ---
>>
>
> As long as you are reworking this section, doesn't BUG_ON seem a little harsh? I do not think the kernel should ever be crashed because a non-essential driver is messed up. As the patch would be a totally different change, it should be a new submission, but I think each of these conditions should trigger a WARN_ON_ONCE, and return IRQ_NONE when they occur.
>
You means the should be like this?

diff --git a/drivers/nfc/pn544_hci.c b/drivers/nfc/pn544_hci.c
index aa71807..e42b100 100644
--- a/drivers/nfc/pn544_hci.c
+++ b/drivers/nfc/pn544_hci.c
@@ -341,13 +341,16 @@ flush:
static irqreturn_t pn544_hci_irq_thread_fn(int irq, void *dev_id)
{
struct pn544_hci_info *info = dev_id;
- struct i2c_client *client = info->i2c_dev;
+ struct i2c_client *client;
struct sk_buff *skb = NULL;
int r;

- BUG_ON(!info);
- BUG_ON(irq != info->i2c_dev->irq);
+ if (!info || irq != info->i2c_dev->irq) {
+ WARN_ON_ONCE(1);
+ return IRQ_NONE;
+ }

+ client = info->i2c_dev;
dev_dbg(&client->dev, "IRQ\n");

if (info->hard_fault != 0)