2017-06-08 10:05:14

by Binoy Jayan

[permalink] [raw]
Subject: [PATCH 0/3] ngene: Replace semaphores with mutexes

These are a set of patches which removes semaphores from ngene.
These are part of a bigger effort to eliminate unwanted semaphores
from the linux kernel.

Binoy Jayan (3):
media: ngene: Replace semaphore cmd_mutex with mutex
media: ngene: Replace semaphore stream_mutex with mutex
media: ngene: Replace semaphore i2c_switch_mutex with mutex

drivers/media/pci/ngene/ngene-core.c | 28 ++++++++++++++--------------
drivers/media/pci/ngene/ngene-i2c.c | 6 +++---
drivers/media/pci/ngene/ngene.h | 6 +++---
3 files changed, 20 insertions(+), 20 deletions(-)

--
Binoy Jayan


2017-06-08 10:05:22

by Binoy Jayan

[permalink] [raw]
Subject: [PATCH 2/3] media: ngene: Replace semaphore stream_mutex with mutex

The semaphore 'stream_mutex' is used as a simple mutex, so
it should be written as one. Semaphores are going away in the future.

Signed-off-by: Binoy Jayan <[email protected]>
---
drivers/media/pci/ngene/ngene-core.c | 14 +++++++-------
drivers/media/pci/ngene/ngene.h | 2 +-
2 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/drivers/media/pci/ngene/ngene-core.c b/drivers/media/pci/ngene/ngene-core.c
index dfbd1e0..59f2e5f 100644
--- a/drivers/media/pci/ngene/ngene-core.c
+++ b/drivers/media/pci/ngene/ngene-core.c
@@ -560,7 +560,7 @@ static int ngene_command_stream_control(struct ngene *dev, u8 stream,
u16 BsSPI = ((stream & 1) ? 0x9800 : 0x9700);
u16 BsSDO = 0x9B00;

- down(&dev->stream_mutex);
+ mutex_lock(&dev->stream_mutex);
memset(&com, 0, sizeof(com));
com.cmd.hdr.Opcode = CMD_CONTROL;
com.cmd.hdr.Length = sizeof(struct FW_STREAM_CONTROL) - 2;
@@ -587,16 +587,16 @@ static int ngene_command_stream_control(struct ngene *dev, u8 stream,
chan->HWState = HWSTATE_STOP;
spin_unlock_irq(&chan->state_lock);
if (ngene_command(dev, &com) < 0) {
- up(&dev->stream_mutex);
+ mutex_unlock(&dev->stream_mutex);
return -1;
}
/* clear_buffers(chan); */
flush_buffers(chan);
- up(&dev->stream_mutex);
+ mutex_unlock(&dev->stream_mutex);
return 0;
}
spin_unlock_irq(&chan->state_lock);
- up(&dev->stream_mutex);
+ mutex_unlock(&dev->stream_mutex);
return 0;
}

@@ -693,10 +693,10 @@ static int ngene_command_stream_control(struct ngene *dev, u8 stream,
spin_unlock_irq(&chan->state_lock);

if (ngene_command(dev, &com) < 0) {
- up(&dev->stream_mutex);
+ mutex_unlock(&dev->stream_mutex);
return -1;
}
- up(&dev->stream_mutex);
+ mutex_unlock(&dev->stream_mutex);
return 0;
}

@@ -1347,7 +1347,7 @@ static int ngene_start(struct ngene *dev)
init_waitqueue_head(&dev->tx_wq);
init_waitqueue_head(&dev->rx_wq);
mutex_init(&dev->cmd_mutex);
- sema_init(&dev->stream_mutex, 1);
+ mutex_init(&dev->stream_mutex);
sema_init(&dev->pll_mutex, 1);
sema_init(&dev->i2c_switch_mutex, 1);
spin_lock_init(&dev->cmd_lock);
diff --git a/drivers/media/pci/ngene/ngene.h b/drivers/media/pci/ngene/ngene.h
index e600b70..0dd15d6 100644
--- a/drivers/media/pci/ngene/ngene.h
+++ b/drivers/media/pci/ngene/ngene.h
@@ -763,7 +763,7 @@ struct ngene {
wait_queue_head_t cmd_wq;
int cmd_done;
struct mutex cmd_mutex;
- struct semaphore stream_mutex;
+ struct mutex stream_mutex;
struct semaphore pll_mutex;
struct semaphore i2c_switch_mutex;
int i2c_current_channel;
--
Binoy Jayan

2017-06-08 10:05:35

by Binoy Jayan

[permalink] [raw]
Subject: [PATCH 3/3] media: ngene: Replace semaphore i2c_switch_mutex with mutex

The semaphore 'i2c_switch_mutex' is used as a simple mutex, so
it should be written as one. Semaphores are going away in the future.

Signed-off-by: Binoy Jayan <[email protected]>
---
drivers/media/pci/ngene/ngene-core.c | 2 +-
drivers/media/pci/ngene/ngene-i2c.c | 6 +++---
drivers/media/pci/ngene/ngene.h | 2 +-
3 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/drivers/media/pci/ngene/ngene-core.c b/drivers/media/pci/ngene/ngene-core.c
index 59f2e5f..ca0c0f8 100644
--- a/drivers/media/pci/ngene/ngene-core.c
+++ b/drivers/media/pci/ngene/ngene-core.c
@@ -1349,7 +1349,7 @@ static int ngene_start(struct ngene *dev)
mutex_init(&dev->cmd_mutex);
mutex_init(&dev->stream_mutex);
sema_init(&dev->pll_mutex, 1);
- sema_init(&dev->i2c_switch_mutex, 1);
+ mutex_init(&dev->i2c_switch_mutex);
spin_lock_init(&dev->cmd_lock);
for (i = 0; i < MAX_STREAM; i++)
spin_lock_init(&dev->channel[i].state_lock);
diff --git a/drivers/media/pci/ngene/ngene-i2c.c b/drivers/media/pci/ngene/ngene-i2c.c
index cf39fcf..fbf3635 100644
--- a/drivers/media/pci/ngene/ngene-i2c.c
+++ b/drivers/media/pci/ngene/ngene-i2c.c
@@ -118,7 +118,7 @@ static int ngene_i2c_master_xfer(struct i2c_adapter *adapter,
(struct ngene_channel *)i2c_get_adapdata(adapter);
struct ngene *dev = chan->dev;

- down(&dev->i2c_switch_mutex);
+ mutex_lock(&dev->i2c_switch_mutex);
ngene_i2c_set_bus(dev, chan->number);

if (num == 2 && msg[1].flags & I2C_M_RD && !(msg[0].flags & I2C_M_RD))
@@ -136,11 +136,11 @@ static int ngene_i2c_master_xfer(struct i2c_adapter *adapter,
msg[0].buf, msg[0].len, 0))
goto done;

- up(&dev->i2c_switch_mutex);
+ mutex_unlock(&dev->i2c_switch_mutex);
return -EIO;

done:
- up(&dev->i2c_switch_mutex);
+ mutex_unlock(&dev->i2c_switch_mutex);
return num;
}

diff --git a/drivers/media/pci/ngene/ngene.h b/drivers/media/pci/ngene/ngene.h
index 0dd15d6..7c7cd21 100644
--- a/drivers/media/pci/ngene/ngene.h
+++ b/drivers/media/pci/ngene/ngene.h
@@ -765,7 +765,7 @@ struct ngene {
struct mutex cmd_mutex;
struct mutex stream_mutex;
struct semaphore pll_mutex;
- struct semaphore i2c_switch_mutex;
+ struct mutex i2c_switch_mutex;
int i2c_current_channel;
int i2c_current_bus;
spinlock_t cmd_lock;
--
Binoy Jayan

2017-06-08 10:05:50

by Binoy Jayan

[permalink] [raw]
Subject: [PATCH 1/3] media: ngene: Replace semaphore cmd_mutex with mutex

The semaphore 'cmd_mutex' is used as a simple mutex, so
it should be written as one. Semaphores are going away in the future.

Signed-off-by: Binoy Jayan <[email protected]>
---
drivers/media/pci/ngene/ngene-core.c | 12 ++++++------
drivers/media/pci/ngene/ngene.h | 2 +-
2 files changed, 7 insertions(+), 7 deletions(-)

diff --git a/drivers/media/pci/ngene/ngene-core.c b/drivers/media/pci/ngene/ngene-core.c
index ce69e64..dfbd1e0 100644
--- a/drivers/media/pci/ngene/ngene-core.c
+++ b/drivers/media/pci/ngene/ngene-core.c
@@ -336,9 +336,9 @@ int ngene_command(struct ngene *dev, struct ngene_command *com)
{
int result;

- down(&dev->cmd_mutex);
+ mutex_lock(&dev->cmd_mutex);
result = ngene_command_mutex(dev, com);
- up(&dev->cmd_mutex);
+ mutex_unlock(&dev->cmd_mutex);
return result;
}

@@ -1283,7 +1283,7 @@ static int ngene_load_firm(struct ngene *dev)

static void ngene_stop(struct ngene *dev)
{
- down(&dev->cmd_mutex);
+ mutex_lock(&dev->cmd_mutex);
i2c_del_adapter(&(dev->channel[0].i2c_adapter));
i2c_del_adapter(&(dev->channel[1].i2c_adapter));
ngwritel(0, NGENE_INT_ENABLE);
@@ -1346,7 +1346,7 @@ static int ngene_start(struct ngene *dev)
init_waitqueue_head(&dev->cmd_wq);
init_waitqueue_head(&dev->tx_wq);
init_waitqueue_head(&dev->rx_wq);
- sema_init(&dev->cmd_mutex, 1);
+ mutex_init(&dev->cmd_mutex);
sema_init(&dev->stream_mutex, 1);
sema_init(&dev->pll_mutex, 1);
sema_init(&dev->i2c_switch_mutex, 1);
@@ -1606,10 +1606,10 @@ static void ngene_unlink(struct ngene *dev)
com.in_len = 3;
com.out_len = 1;

- down(&dev->cmd_mutex);
+ mutex_lock(&dev->cmd_mutex);
ngwritel(0, NGENE_INT_ENABLE);
ngene_command_mutex(dev, &com);
- up(&dev->cmd_mutex);
+ mutex_unlock(&dev->cmd_mutex);
}

void ngene_shutdown(struct pci_dev *pdev)
diff --git a/drivers/media/pci/ngene/ngene.h b/drivers/media/pci/ngene/ngene.h
index 10d8f74..e600b70 100644
--- a/drivers/media/pci/ngene/ngene.h
+++ b/drivers/media/pci/ngene/ngene.h
@@ -762,7 +762,7 @@ struct ngene {

wait_queue_head_t cmd_wq;
int cmd_done;
- struct semaphore cmd_mutex;
+ struct mutex cmd_mutex;
struct semaphore stream_mutex;
struct semaphore pll_mutex;
struct semaphore i2c_switch_mutex;
--
Binoy Jayan

2017-06-08 15:10:54

by Arnd Bergmann

[permalink] [raw]
Subject: Re: [PATCH 1/3] media: ngene: Replace semaphore cmd_mutex with mutex

On Thu, Jun 8, 2017 at 12:04 PM, Binoy Jayan <[email protected]> wrote:
> The semaphore 'cmd_mutex' is used as a simple mutex, so
> it should be written as one. Semaphores are going away in the future.
>
> Signed-off-by: Binoy Jayan <[email protected]>
> ---

> @@ -1283,7 +1283,7 @@ static int ngene_load_firm(struct ngene *dev)
>
> static void ngene_stop(struct ngene *dev)
> {
> - down(&dev->cmd_mutex);
> + mutex_lock(&dev->cmd_mutex);
> i2c_del_adapter(&(dev->channel[0].i2c_adapter));
> i2c_del_adapter(&(dev->channel[1].i2c_adapter));
> ngwritel(0, NGENE_INT_ENABLE);

Are you sure about this one? There is only one mutex_lock() and
then the structure gets freed without a corresponding mutex_unlock().

I suspect this violates some rules of mutexes, either when compile
testing with "make C=1", or when running with lockdep enabled.

Can we actually have a concurrently held mutex at the time we
get here? If not, using mutex_destroy() in place of the down()
may be the right answer.

Arnd

2017-06-08 15:17:53

by Arnd Bergmann

[permalink] [raw]
Subject: Re: [PATCH 3/3] media: ngene: Replace semaphore i2c_switch_mutex with mutex

On Thu, Jun 8, 2017 at 12:04 PM, Binoy Jayan <[email protected]> wrote:
> The semaphore 'i2c_switch_mutex' is used as a simple mutex, so
> it should be written as one. Semaphores are going away in the future.
>
> Signed-off-by: Binoy Jayan <[email protected]>

This one is obviously correct,

Reviewed-by: Arnd Bergmann <[email protected]>

2017-06-08 15:19:18

by Arnd Bergmann

[permalink] [raw]
Subject: Re: [PATCH 2/3] media: ngene: Replace semaphore stream_mutex with mutex

On Thu, Jun 8, 2017 at 12:04 PM, Binoy Jayan <[email protected]> wrote:
> The semaphore 'stream_mutex' is used as a simple mutex, so
> it should be written as one. Semaphores are going away in the future.
>
> Signed-off-by: Binoy Jayan <[email protected]>
> ---

Looks correct, though I wonder whether it would be nicer to move the
mutex_lock/unlock() to the caller to avoid repeating the unlock() five
times.

Either way,

Reviewed-by: Arnd Bergmann <[email protected]>

2017-06-09 04:37:22

by Binoy Jayan

[permalink] [raw]
Subject: Re: [PATCH 1/3] media: ngene: Replace semaphore cmd_mutex with mutex

On 8 June 2017 at 20:40, Arnd Bergmann <[email protected]> wrote:
> On Thu, Jun 8, 2017 at 12:04 PM, Binoy Jayan <[email protected]> wrote:
>> The semaphore 'cmd_mutex' is used as a simple mutex, so
>> it should be written as one. Semaphores are going away in the future.
>>
>> Signed-off-by: Binoy Jayan <[email protected]>
>> ---
>
>> @@ -1283,7 +1283,7 @@ static int ngene_load_firm(struct ngene *dev)
>>
>> static void ngene_stop(struct ngene *dev)
>> {
>> - down(&dev->cmd_mutex);
>> + mutex_lock(&dev->cmd_mutex);
>> i2c_del_adapter(&(dev->channel[0].i2c_adapter));
>> i2c_del_adapter(&(dev->channel[1].i2c_adapter));
>> ngwritel(0, NGENE_INT_ENABLE);
>
> Are you sure about this one? There is only one mutex_lock() and
> then the structure gets freed without a corresponding mutex_unlock().
>
> I suspect this violates some rules of mutexes, either when compile
> testing with "make C=1", or when running with lockdep enabled.
>
> Can we actually have a concurrently held mutex at the time we
> get here? If not, using mutex_destroy() in place of the down()
> may be the right answer.

I noticed the missing 'up' here, but may be semaphores do not have
to adhere to that rule? Thank you for pointing out that. I'll check the
concurrency part. By the way why do we need mutex_destoy?
To debug an aberrate condition?

Thanks,
Binoy

2017-06-09 10:36:45

by Arnd Bergmann

[permalink] [raw]
Subject: Re: [PATCH 1/3] media: ngene: Replace semaphore cmd_mutex with mutex

On Fri, Jun 9, 2017 at 6:37 AM, Binoy Jayan <[email protected]> wrote:
> On 8 June 2017 at 20:40, Arnd Bergmann <[email protected]> wrote:
>> On Thu, Jun 8, 2017 at 12:04 PM, Binoy Jayan <[email protected]> wrote:
>>> The semaphore 'cmd_mutex' is used as a simple mutex, so
>>> it should be written as one. Semaphores are going away in the future.
>>>
>>> Signed-off-by: Binoy Jayan <[email protected]>
>>> ---
>>
>>> @@ -1283,7 +1283,7 @@ static int ngene_load_firm(struct ngene *dev)
>>>
>>> static void ngene_stop(struct ngene *dev)
>>> {
>>> - down(&dev->cmd_mutex);
>>> + mutex_lock(&dev->cmd_mutex);
>>> i2c_del_adapter(&(dev->channel[0].i2c_adapter));
>>> i2c_del_adapter(&(dev->channel[1].i2c_adapter));
>>> ngwritel(0, NGENE_INT_ENABLE);
>>
>> Are you sure about this one? There is only one mutex_lock() and
>> then the structure gets freed without a corresponding mutex_unlock().
>>
>> I suspect this violates some rules of mutexes, either when compile
>> testing with "make C=1", or when running with lockdep enabled.
>>
>> Can we actually have a concurrently held mutex at the time we
>> get here? If not, using mutex_destroy() in place of the down()
>> may be the right answer.
>
> I noticed the missing 'up' here, but may be semaphores do not have
> to adhere to that rule?

The rules for semaphores are very lax, the up() and down() may
be in completely separate contexts, the up() can even happen from
an interrupt handler IIRC.

I read up on the sparse annotations now and found that it only
tracks spinlocks and rwlocks using the __acquires() annotation,
but not semaphores or mutexes.

I'm still not sure whether lockdep requires the mutex to be released
before it gets freed, the code may actually be fine, but it does
seem odd.

> Thank you for pointing out that. I'll check the
> concurrency part. By the way why do we need mutex_destoy?
> To debug an aberrate condition?

At first I suspected the down() here was added for the same
purpose as a mutex_destroy: to ensure that we are in a sane
state before we free the device structure, but the way they
achieve that is completely different.

However, if there is any way that a command may still be in
progress by the time we get to ngene_stop(), we may also
be lacking reference counting on the ngene structure here.
So far I haven't found any of those, and think the mutex_destroy()
is sufficient here as a debugging help.

Arnd

2017-06-13 08:58:51

by Binoy Jayan

[permalink] [raw]
Subject: Re: [PATCH 1/3] media: ngene: Replace semaphore cmd_mutex with mutex

Hi Arnd,

On 9 June 2017 at 16:06, Arnd Bergmann <[email protected]> wrote:

>> Thank you for pointing out that. I'll check the
>> concurrency part. By the way why do we need mutex_destoy?
>> To debug an aberrate condition?
>
> At first I suspected the down() here was added for the same
> purpose as a mutex_destroy: to ensure that we are in a sane
> state before we free the device structure, but the way they
> achieve that is completely different.
>
> However, if there is any way that a command may still be in
> progress by the time we get to ngene_stop(), we may also
> be lacking reference counting on the ngene structure here.
> So far I haven't found any of those, and think the mutex_destroy()
> is sufficient here as a debugging help.

I've made the necessary changes. Thank you for reviewing all the patches.

Regards,
Binoy