[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <CAHp75VdG4r95ZU8G9TfL+jkT63+Gppb8w5TRvAtCR_pAk0o=NA@mail.gmail.com>
Date: Wed, 24 Jun 2020 17:46:33 +0300
From: Andy Shevchenko <andy.shevchenko@...il.com>
To: Kent Gibson <warthog618@...il.com>
Cc: Linux Kernel Mailing List <linux-kernel@...r.kernel.org>,
"open list:GPIO SUBSYSTEM" <linux-gpio@...r.kernel.org>,
Bartosz Golaszewski <bgolaszewski@...libre.com>,
Linus Walleij <linus.walleij@...aro.org>
Subject: Re: [PATCH 10/22] gpiolib: cdev: fix minor race in GET_LINEINFO_WATCH
On Tue, Jun 23, 2020 at 7:03 AM Kent Gibson <warthog618@...il.com> wrote:
>
> Merge separate usage of test_bit/set_bit into test_and_set_bit to remove
> the possibility of a race between the test and set.
>
> Similarly test_bit and clear_bit.
>
> In the existing code it is possible for two threads to race past the
> test_bit and then set or clear the watch bit, and neither return EBUSY.
I stumbled over this myself, but...
> - if (test_bit(hwgpio, gcdev->watched_lines))
> + if (test_and_set_bit(hwgpio, gcdev->watched_lines))
> return -EBUSY;
>
> gpio_desc_to_lineinfo(desc, &lineinfo);
> @@ -897,7 +897,6 @@ static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
> if (copy_to_user(ip, &lineinfo, sizeof(lineinfo)))
> return -EFAULT;
>
> - set_bit(hwgpio, gcdev->watched_lines);
> return 0;
...I think it's not an equivalent despite races involved. If you set
bit and return error code, you will have the wrong state.
...
> - if (!test_bit(hwgpio, gcdev->watched_lines))
> + if (!test_and_clear_bit(hwgpio, gcdev->watched_lines))
> return -EBUSY;
>
> - clear_bit(hwgpio, gcdev->watched_lines);
> return 0;
OTOH, this is okay as long as we have no code in between. So, I really
prefer something better to do such checks.
(Alas, I can't come up with a proposal right now)
--
With Best Regards,
Andy Shevchenko
Powered by blists - more mailing lists