Re: [PATCH 6/8] IB/hns: Replace counting semaphore event_sem with wait condition

From: Arnd Bergmann
Date: Mon Oct 17 2016 - 16:30:40 EST


On Monday, October 17, 2016 10:01:00 PM CEST Binoy Jayan wrote:
> --- a/drivers/infiniband/hw/hns/hns_roce_cmd.c
> +++ b/drivers/infiniband/hw/hns/hns_roce_cmd.c
> @@ -248,10 +248,14 @@ static int hns_roce_cmd_mbox_wait(struct hns_roce_dev *hr_dev, u64 in_param,
> {
> int ret = 0;
>
> - down(&hr_dev->cmd.event_sem);
> + wait_event(hr_dev->cmd.event_sem.wq,
> + atomic_add_unless(&hr_dev->cmd.event_sem.count, -1, 0));
> +
> ret = __hns_roce_cmd_mbox_wait(hr_dev, in_param, out_param,
> in_modifier, op_modifier, op, timeout);
> - up(&hr_dev->cmd.event_sem);
> +
> + if (atomic_inc_return(&hr_dev->cmd.event_sem.count) == 1)
> + wake_up(&hr_dev->cmd.event_sem.wq);
>
> return ret;
> }

This is the only interesting use of the event_sem that cares about
the counting and it protects the __hns_roce_cmd_mbox_wait() from being
entered too often. The count here is the number of size of the
hr_dev->cmd.context[] array.

However, that function already use a spinlock to protect that array
and pick the correct context. I think changing the inner function
to handle the case of 'no context available' by using a waitqueue
without counting anything would be a reasonable transformation
away from the semaphore.

Arnd