* [PATCH] i2c: altera: Fix race between xfer_msg and isr thread
@ 2020-04-10 1:06 Atsushi Nemoto
2020-04-16 19:49 ` Thor Thayer
2020-04-26 8:30 ` Wolfram Sang
0 siblings, 2 replies; 5+ messages in thread
From: Atsushi Nemoto @ 2020-04-10 1:06 UTC (permalink / raw)
To: Thor Thayer, linux-i2c; +Cc: tomonori.sakita
Use a mutex to protect access to idev->msg_len, idev->buf, etc. which
are modified by both altr_i2c_xfer_msg() and altr_i2c_isr().
Signed-off-by: Atsushi Nemoto <atsushi.nemoto@sord.co.jp>
---
drivers/i2c/busses/i2c-altera.c | 10 +++++++++-
1 file changed, 9 insertions(+), 1 deletion(-)
diff --git a/drivers/i2c/busses/i2c-altera.c b/drivers/i2c/busses/i2c-altera.c
index 20ef63820c77..3db7d77c5a1e 100644
--- a/drivers/i2c/busses/i2c-altera.c
+++ b/drivers/i2c/busses/i2c-altera.c
@@ -70,6 +70,7 @@
* @isr_mask: cached copy of local ISR enables.
* @isr_status: cached copy of local ISR status.
* @lock: spinlock for IRQ synchronization.
+ * @mutex: mutex for IRQ thread.
*/
struct altr_i2c_dev {
void __iomem *base;
@@ -86,6 +87,7 @@ struct altr_i2c_dev {
u32 isr_mask;
u32 isr_status;
spinlock_t lock; /* IRQ synchronization */
+ struct mutex mutex;
};
static void
@@ -245,10 +247,11 @@ static irqreturn_t altr_i2c_isr(int irq, void *_dev)
struct altr_i2c_dev *idev = _dev;
u32 status = idev->isr_status;
+ mutex_lock(&idev->mutex);
if (!idev->msg) {
dev_warn(idev->dev, "unexpected interrupt\n");
altr_i2c_int_clear(idev, ALTR_I2C_ALL_IRQ);
- return IRQ_HANDLED;
+ goto out;
}
read = (idev->msg->flags & I2C_M_RD) != 0;
@@ -301,6 +304,8 @@ static irqreturn_t altr_i2c_isr(int irq, void *_dev)
complete(&idev->msg_complete);
dev_dbg(idev->dev, "Message Complete\n");
}
+out:
+ mutex_unlock(&idev->mutex);
return IRQ_HANDLED;
}
@@ -312,6 +317,7 @@ static int altr_i2c_xfer_msg(struct altr_i2c_dev *idev, struct i2c_msg *msg)
u32 value;
u8 addr = i2c_8bit_addr_from_msg(msg);
+ mutex_lock(&idev->mutex);
idev->msg = msg;
idev->msg_len = msg->len;
idev->buf = msg->buf;
@@ -336,6 +342,7 @@ static int altr_i2c_xfer_msg(struct altr_i2c_dev *idev, struct i2c_msg *msg)
altr_i2c_int_enable(idev, imask, true);
altr_i2c_fill_tx_fifo(idev);
}
+ mutex_unlock(&idev->mutex);
time_left = wait_for_completion_timeout(&idev->msg_complete,
ALTR_I2C_XFER_TIMEOUT);
@@ -410,6 +417,7 @@ static int altr_i2c_probe(struct platform_device *pdev)
idev->dev = &pdev->dev;
init_completion(&idev->msg_complete);
spin_lock_init(&idev->lock);
+ mutex_init(&idev->mutex);
val = device_property_read_u32(idev->dev, "fifo-size",
&idev->fifo_size);
--
2.11.0
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH] i2c: altera: Fix race between xfer_msg and isr thread
2020-04-10 1:06 [PATCH] i2c: altera: Fix race between xfer_msg and isr thread Atsushi Nemoto
@ 2020-04-16 19:49 ` Thor Thayer
2020-04-26 8:30 ` Wolfram Sang
1 sibling, 0 replies; 5+ messages in thread
From: Thor Thayer @ 2020-04-16 19:49 UTC (permalink / raw)
To: Atsushi Nemoto, linux-i2c; +Cc: tomonori.sakita
On 4/9/20 8:06 PM, Atsushi Nemoto wrote:
> Use a mutex to protect access to idev->msg_len, idev->buf, etc. which
> are modified by both altr_i2c_xfer_msg() and altr_i2c_isr().
>
> Signed-off-by: Atsushi Nemoto <atsushi.nemoto@sord.co.jp>
> ---
Acked-by: Thor Thayer <thor.thayer@linux.intel.com>
> drivers/i2c/busses/i2c-altera.c | 10 +++++++++-
> 1 file changed, 9 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/i2c/busses/i2c-altera.c b/drivers/i2c/busses/i2c-altera.c
> index 20ef63820c77..3db7d77c5a1e 100644
> --- a/drivers/i2c/busses/i2c-altera.c
> +++ b/drivers/i2c/busses/i2c-altera.c
> @@ -70,6 +70,7 @@
> * @isr_mask: cached copy of local ISR enables.
> * @isr_status: cached copy of local ISR status.
> * @lock: spinlock for IRQ synchronization.
> + * @mutex: mutex for IRQ thread.
> */
> struct altr_i2c_dev {
> void __iomem *base;
> @@ -86,6 +87,7 @@ struct altr_i2c_dev {
> u32 isr_mask;
> u32 isr_status;
> spinlock_t lock; /* IRQ synchronization */
> + struct mutex mutex;
> };
>
> static void
> @@ -245,10 +247,11 @@ static irqreturn_t altr_i2c_isr(int irq, void *_dev)
> struct altr_i2c_dev *idev = _dev;
> u32 status = idev->isr_status;
>
> + mutex_lock(&idev->mutex);
> if (!idev->msg) {
> dev_warn(idev->dev, "unexpected interrupt\n");
> altr_i2c_int_clear(idev, ALTR_I2C_ALL_IRQ);
> - return IRQ_HANDLED;
> + goto out;
> }
> read = (idev->msg->flags & I2C_M_RD) != 0;
>
> @@ -301,6 +304,8 @@ static irqreturn_t altr_i2c_isr(int irq, void *_dev)
> complete(&idev->msg_complete);
> dev_dbg(idev->dev, "Message Complete\n");
> }
> +out:
> + mutex_unlock(&idev->mutex);
>
> return IRQ_HANDLED;
> }
> @@ -312,6 +317,7 @@ static int altr_i2c_xfer_msg(struct altr_i2c_dev *idev, struct i2c_msg *msg)
> u32 value;
> u8 addr = i2c_8bit_addr_from_msg(msg);
>
> + mutex_lock(&idev->mutex);
> idev->msg = msg;
> idev->msg_len = msg->len;
> idev->buf = msg->buf;
> @@ -336,6 +342,7 @@ static int altr_i2c_xfer_msg(struct altr_i2c_dev *idev, struct i2c_msg *msg)
> altr_i2c_int_enable(idev, imask, true);
> altr_i2c_fill_tx_fifo(idev);
> }
> + mutex_unlock(&idev->mutex);
>
> time_left = wait_for_completion_timeout(&idev->msg_complete,
> ALTR_I2C_XFER_TIMEOUT);
> @@ -410,6 +417,7 @@ static int altr_i2c_probe(struct platform_device *pdev)
> idev->dev = &pdev->dev;
> init_completion(&idev->msg_complete);
> spin_lock_init(&idev->lock);
> + mutex_init(&idev->mutex);
>
> val = device_property_read_u32(idev->dev, "fifo-size",
> &idev->fifo_size);
>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] i2c: altera: Fix race between xfer_msg and isr thread
2020-04-10 1:06 [PATCH] i2c: altera: Fix race between xfer_msg and isr thread Atsushi Nemoto
2020-04-16 19:49 ` Thor Thayer
@ 2020-04-26 8:30 ` Wolfram Sang
2020-04-26 10:39 ` Atsushi Nemoto
1 sibling, 1 reply; 5+ messages in thread
From: Wolfram Sang @ 2020-04-26 8:30 UTC (permalink / raw)
To: Atsushi Nemoto; +Cc: Thor Thayer, linux-i2c, tomonori.sakita
[-- Attachment #1: Type: text/plain, Size: 1286 bytes --]
On Fri, Apr 10, 2020 at 10:06:40AM +0900, Atsushi Nemoto wrote:
> Use a mutex to protect access to idev->msg_len, idev->buf, etc. which
> are modified by both altr_i2c_xfer_msg() and altr_i2c_isr().
>
> Signed-off-by: Atsushi Nemoto <atsushi.nemoto@sord.co.jp>
> ---
> drivers/i2c/busses/i2c-altera.c | 10 +++++++++-
> 1 file changed, 9 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/i2c/busses/i2c-altera.c b/drivers/i2c/busses/i2c-altera.c
> index 20ef63820c77..3db7d77c5a1e 100644
> --- a/drivers/i2c/busses/i2c-altera.c
> +++ b/drivers/i2c/busses/i2c-altera.c
> @@ -70,6 +70,7 @@
> * @isr_mask: cached copy of local ISR enables.
> * @isr_status: cached copy of local ISR status.
> * @lock: spinlock for IRQ synchronization.
> + * @mutex: mutex for IRQ thread.
I think the name 'mutex' is too unspecific. (Same goes for 'lock' above
which is not part of your patch, obviously.)
> */
> struct altr_i2c_dev {
> void __iomem *base;
> @@ -86,6 +87,7 @@ struct altr_i2c_dev {
> u32 isr_mask;
> u32 isr_status;
> spinlock_t lock; /* IRQ synchronization */
> + struct mutex mutex;
> };
Has it been checked if we really need both, the spinlock and the mutex?
From a glimpse, it looks like the spinlock became obsolete now.
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] i2c: altera: Fix race between xfer_msg and isr thread
2020-04-26 8:30 ` Wolfram Sang
@ 2020-04-26 10:39 ` Atsushi Nemoto
2020-05-08 13:10 ` Atsushi Nemoto
0 siblings, 1 reply; 5+ messages in thread
From: Atsushi Nemoto @ 2020-04-26 10:39 UTC (permalink / raw)
To: wsa; +Cc: atsushi.nemoto, thor.thayer, linux-i2c, tomonori.sakita
On Sun, 26 Apr 2020 10:30:31 +0200, Wolfram Sang <wsa@the-dreams.de> wrote:
>> --- a/drivers/i2c/busses/i2c-altera.c
>> +++ b/drivers/i2c/busses/i2c-altera.c
>> @@ -70,6 +70,7 @@
>> * @isr_mask: cached copy of local ISR enables.
>> * @isr_status: cached copy of local ISR status.
>> * @lock: spinlock for IRQ synchronization.
>> + * @mutex: mutex for IRQ thread.
>
> I think the name 'mutex' is too unspecific. (Same goes for 'lock' above
> which is not part of your patch, obviously.)
Then, how about 'isr_mutex' ? Should I resend a patch with new name?
>> */
>> struct altr_i2c_dev {
>> void __iomem *base;
>> @@ -86,6 +87,7 @@ struct altr_i2c_dev {
>> u32 isr_mask;
>> u32 isr_status;
>> spinlock_t lock; /* IRQ synchronization */
>> + struct mutex mutex;
>> };
>
> Has it been checked if we really need both, the spinlock and the mutex?
> From a glimpse, it looks like the spinlock became obsolete now.
Yes, but just dropping the spinlock will leak two code pathes unlocked:
1. altr_i2c_int_enable() at end of altr_i2c_init()
2. altr_i2c_int_enable() just after wait_for_completion_timeout()
We can kill the spinlock by adding some more mutex_lock, but I think
that is not a _fix_ but _cleanup_, so I would like to do in another
patch.
---
Atsushi Nemoto
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] i2c: altera: Fix race between xfer_msg and isr thread
2020-04-26 10:39 ` Atsushi Nemoto
@ 2020-05-08 13:10 ` Atsushi Nemoto
0 siblings, 0 replies; 5+ messages in thread
From: Atsushi Nemoto @ 2020-05-08 13:10 UTC (permalink / raw)
To: wsa; +Cc: anemo, thor.thayer, linux-i2c, tomonori.sakita
On Sun, 26 Apr 2020 19:39:16 +0900 (JST), Atsushi Nemoto <anemo@mba.ocn.ne.jp> wrote:
>>> + * @mutex: mutex for IRQ thread.
>>
>> I think the name 'mutex' is too unspecific. (Same goes for 'lock' above
>> which is not part of your patch, obviously.)
>
> Then, how about 'isr_mutex' ? Should I resend a patch with new name?
>
>>> */
>>> struct altr_i2c_dev {
>>> void __iomem *base;
>>> @@ -86,6 +87,7 @@ struct altr_i2c_dev {
>>> u32 isr_mask;
>>> u32 isr_status;
>>> spinlock_t lock; /* IRQ synchronization */
>>> + struct mutex mutex;
>>> };
>>
>> Has it been checked if we really need both, the spinlock and the mutex?
>> From a glimpse, it looks like the spinlock became obsolete now.
>
> Yes, but just dropping the spinlock will leak two code pathes unlocked:
>
> 1. altr_i2c_int_enable() at end of altr_i2c_init()
> 2. altr_i2c_int_enable() just after wait_for_completion_timeout()
>
> We can kill the spinlock by adding some more mutex_lock, but I think
> that is not a _fix_ but _cleanup_, so I would like to do in another
> patch.
I will send a revised patch renaming 'mutex' to 'isr_mutex' (with
Thor's Acked-by line) and a patch to cleanup the spinlock.
---
Atsushi Nemoto
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2020-05-08 13:10 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-04-10 1:06 [PATCH] i2c: altera: Fix race between xfer_msg and isr thread Atsushi Nemoto
2020-04-16 19:49 ` Thor Thayer
2020-04-26 8:30 ` Wolfram Sang
2020-04-26 10:39 ` Atsushi Nemoto
2020-05-08 13:10 ` Atsushi Nemoto
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).