Re: [PATCH v4 1/2] tty: serial: dz: convert atomic_* to refcount_* APIs for map_guard

From: Deepak R Varma
Date: Wed Jan 04 2023 - 04:00:40 EST


On Wed, Jan 04, 2023 at 09:28:13AM +0100, Greg Kroah-Hartman wrote:
> On Tue, Jan 03, 2023 at 03:35:15PM +0530, Deepak R Varma wrote:
> > > > - printk(KERN_ERR
> > > > - "dz: Unable to reserve MMIO resource\n");
> > > > + refcount_inc(&mux->map_guard);
> > > > + if (refcount_read(&mux->map_guard) == 1) {
> > >
> > > This is now racy, right?
> >
> > Hello Jiri,
> > Thank you for the feedback. You are correct. I have split a single instruction
> > in two (or more?) instructions potentially resulting in race conditions. I
> > looked through the refcount_* APIs but did not find a direct match.
> >
> >
> > Can you please comment if the the following variation will avoid race condition?
> >
> > if (!refcount_add_not_zero(1, &mux->map_guard)) {
> > refcount_inc(&mux->map_guard);
> > ...
> > }
>
> What do you think? The onus is on you to prove the conversion is
> correct, otherwise, why do the conversion at all?

Hello Greg,
Okay. Sounds good. I think the revised approach should be safer. I will work on
finding a means to prove that.

>
> Actually, why do this at all, what is the goal here? And how was this
> tested?

The objective here is to migrate to specific and improved APIs that are already
proved to be better for different reasons as mentioned in the patch log
messages. This is as per the Linux Kernel documentation.

In terms of testing, First, I did a compile and build test of the changes.
I also wrote separate small dummy modules and tested the API transformation.
However, these modules were standalone and limited in complexity and intensity.
I will try to make these more intense, multithreaded and run the test again.

Thank you as always :)
./drv

>
> thanks,
>
> greg k-h