public inbox for linux-i2c@vger.kernel.org
 help / color / mirror / Atom feed
From: Ray Jui <ray.jui@broadcom.com>
To: Florian Fainelli <f.fainelli@gmail.com>,
	Dhananjay Phadke <dphadke@linux.microsoft.com>
Cc: bcm-kernel-feedback-list@broadcom.com, linux-i2c@vger.kernel.org,
	linux-kernel@vger.kernel.org, rayagonda.kokatanur@broadcom.com,
	rjui@broadcom.com, wsa@kernel.org
Subject: Re: [PATCH v2] i2c: iproc: fix race between client unreg and isr
Date: Mon, 10 Aug 2020 14:17:10 -0700	[thread overview]
Message-ID: <18cf439a-8fde-02b0-31b6-9ac42f7e972c@broadcom.com> (raw)
In-Reply-To: <0f7063f5-8ca7-f469-574b-82382d49e266@gmail.com>



On 8/8/2020 7:47 AM, Florian Fainelli wrote:
> 
> 
> On 8/7/2020 8:55 PM, Dhananjay Phadke wrote:
>> On 8/7/2020, Florian Fainelli wrote:
>>>> When i2c client unregisters, synchronize irq before setting
>>>> iproc_i2c->slave to NULL.
>>>>
>>>> (1) disable_irq()
>>>> (2) Mask event enable bits in control reg
>>>> (3) Erase slave address (avoid further writes to rx fifo)
>>>> (4) Flush tx and rx FIFOs
>>>> (5) Clear pending event (interrupt) bits in status reg
>>>> (6) enable_irq()
>>>> (7) Set client pointer to NULL
>>>>
>>>
>>>> @@ -1091,6 +1091,17 @@ static int bcm_iproc_i2c_unreg_slave(struct i2c_client *slave)
>>>>  	tmp &= ~BIT(S_CFG_EN_NIC_SMB_ADDR3_SHIFT);
>>>>  	iproc_i2c_wr_reg(iproc_i2c, S_CFG_SMBUS_ADDR_OFFSET, tmp);
>>>>  
>>>> +	/* flush TX/RX FIFOs */
>>>> +	tmp = (BIT(S_FIFO_RX_FLUSH_SHIFT) | BIT(S_FIFO_TX_FLUSH_SHIFT));
>>>> +	iproc_i2c_wr_reg(iproc_i2c, S_FIFO_CTRL_OFFSET, tmp);
>>>> +
>>>> +	/* clear all pending slave interrupts */
>>>> +	iproc_i2c_wr_reg(iproc_i2c, IS_OFFSET, ISR_MASK_SLAVE);
>>>> +
>>>> +	enable_irq(iproc_i2c->irq);
>>>> +
>>>> +	iproc_i2c->slave = NULL;
>>>
>>> There is nothing that checks on iproc_i2c->slave being valid within the
>>> interrupt handler, we assume that the pointer is valid which is fin,
>>> however non functional it may be, it may feel more natural to move the
>>> assignment before the enable_irq()?
>>
>> As far as the teardown sequence ensures no more interrupts arrive after
>> enable_irq() and they are enabled only after setting pointer during
>> client register(); checking for NULL in ISR isn't necessary. 
> 
> Agreed.
> 

Okay I think we all agree that this teardown sequence will guarantee
that no further "slave" interrupts will be fired after it.

>>
>> If The teardown sequence doesn't guarantee quiescing of interrupts,
>> setting NULL before or after enable_irq() is equally vulnerable.
> 
> The teardown sequence is sort of a critical section if we may say, so
> ensuring that everything happens within it and that enable_irq() is the
> last operation would seem more natural to me at least. Thanks
> 

I tend to agree with Florian here.

1. Enable/Disable IRQ is done on the interrupt line for both master and
slave (or even other peripherals that share the same interrupt line,
although that is not the case here since this interrupt is dedicated to
I2C in all iProc based SoCs).

2. The tear down sequence here wrapped by disable/enable_irq is slave
specific

The effect of 1. is temporary, and the purpose of it is to ensure slave
interrupts are quiesced properly at the end of the sequence.

If we consider both 1. and 2., I agree with Florian that while the end
result is the same, it is indeed more natural to wrap the entire slave
tear down sequence within disable/enable irq.

Thanks,

Ray

WARNING: multiple messages have this Message-ID (diff)
From: Dhananjay Phadke <dphadke@linux.microsoft.com>
To: ray.jui@broadcom.com
Cc: bcm-kernel-feedback-list@broadcom.com,
	dphadke@linux.microsoft.com, f.fainelli@gmail.com,
	linux-i2c@vger.kernel.org, linux-kernel@vger.kernel.org,
	rayagonda.kokatanur@broadcom.com, rjui@broadcom.com,
	wsa@kernel.org
Subject: Re: [PATCH v2] i2c: iproc: fix race between client unreg and isr
Date: Mon, 10 Aug 2020 15:22:49 -0700	[thread overview]
Message-ID: <18cf439a-8fde-02b0-31b6-9ac42f7e972c@broadcom.com> (raw)
Message-ID: <20200810222249.y9wIPdsOSP74Gj-p7IIE27ut3gP8dBe5F4qvvKA9sH4@z> (raw)
In-Reply-To: <18cf439a-8fde-02b0-31b6-9ac42f7e972c@broadcom.com>


On 8/10/2020 02:17 PM, Ray Jui wrote:
>> On 8/7/2020 8:55 PM, Dhananjay Phadke wrote:
>>> On 8/7/2020, Florian Fainelli wrote:
>>>>> When i2c client unregisters, synchronize irq before setting
>>>>> iproc_i2c->slave to NULL.
>>>>>
>>>>> (1) disable_irq()
>>>>> (2) Mask event enable bits in control reg
>>>>> (3) Erase slave address (avoid further writes to rx fifo)
>>>>> (4) Flush tx and rx FIFOs
>>>>> (5) Clear pending event (interrupt) bits in status reg
>>>>> (6) enable_irq()
>>>>> (7) Set client pointer to NULL
>>>>>
>>>>
>>>>> @@ -1091,6 +1091,17 @@ static int bcm_iproc_i2c_unreg_slave(struct i2c_client *slave)
>>>>>  	tmp &= ~BIT(S_CFG_EN_NIC_SMB_ADDR3_SHIFT);
>>>>>  	iproc_i2c_wr_reg(iproc_i2c, S_CFG_SMBUS_ADDR_OFFSET, tmp);
>>>>>  
>>>>> +	/* flush TX/RX FIFOs */
>>>>> +	tmp = (BIT(S_FIFO_RX_FLUSH_SHIFT) | BIT(S_FIFO_TX_FLUSH_SHIFT));
>>>>> +	iproc_i2c_wr_reg(iproc_i2c, S_FIFO_CTRL_OFFSET, tmp);
>>>>> +
>>>>> +	/* clear all pending slave interrupts */
>>>>> +	iproc_i2c_wr_reg(iproc_i2c, IS_OFFSET, ISR_MASK_SLAVE);
>>>>> +
>>>>> +	enable_irq(iproc_i2c->irq);
>>>>> +
>>>>> +	iproc_i2c->slave = NULL;
>>>>
>>>> There is nothing that checks on iproc_i2c->slave being valid within the
>>>> interrupt handler, we assume that the pointer is valid which is fin,
>>>> however non functional it may be, it may feel more natural to move the
>>>> assignment before the enable_irq()?
>>>
>>> As far as the teardown sequence ensures no more interrupts arrive after
>>> enable_irq() and they are enabled only after setting pointer during
>>> client register(); checking for NULL in ISR isn't necessary. 
>> 
>> Agreed.
>> 
>
>Okay I think we all agree that this teardown sequence will guarantee
>that no further "slave" interrupts will be fired after it.
>
>>>
>>> If The teardown sequence doesn't guarantee quiescing of interrupts,
>>> setting NULL before or after enable_irq() is equally vulnerable.
>> 
>> The teardown sequence is sort of a critical section if we may say, so
>> ensuring that everything happens within it and that enable_irq() is the
>> last operation would seem more natural to me at least. Thanks
>> 
>
>I tend to agree with Florian here.
>
>1. Enable/Disable IRQ is done on the interrupt line for both master and
>slave (or even other peripherals that share the same interrupt line,
>although that is not the case here since this interrupt is dedicated to
>I2C in all iProc based SoCs).
>
>2. The tear down sequence here wrapped by disable/enable_irq is slave
>specific
>
>The effect of 1. is temporary, and the purpose of it is to ensure slave
>interrupts are quiesced properly at the end of the sequence.
>
>If we consider both 1. and 2., I agree with Florian that while the end
>result is the same, it is indeed more natural to wrap the entire slave
>tear down sequence within disable/enable irq.

Ok, will send v3 with this change.

Thanks,
Dhananjay


  reply	other threads:[~2020-08-10 21:17 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-08-07 22:02 [PATCH v2] i2c: iproc: fix race between client unreg and isr Dhananjay Phadke
2020-08-08  0:35 ` Florian Fainelli
2020-08-08  3:55   ` Dhananjay Phadke
2020-08-08 14:47     ` Florian Fainelli
2020-08-10 21:17       ` Ray Jui [this message]
2020-08-10 22:22         ` Dhananjay Phadke

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=18cf439a-8fde-02b0-31b6-9ac42f7e972c@broadcom.com \
    --to=ray.jui@broadcom.com \
    --cc=bcm-kernel-feedback-list@broadcom.com \
    --cc=dphadke@linux.microsoft.com \
    --cc=f.fainelli@gmail.com \
    --cc=linux-i2c@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rayagonda.kokatanur@broadcom.com \
    --cc=rjui@broadcom.com \
    --cc=wsa@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox