From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: Deepak R Varma <drv@mailo.com>
Cc: "Reshetova, Elena" <elena.reshetova@intel.com>,
Jiri Slaby <jirislaby@kernel.org>,
"Maciej W. Rozycki" <macro@orcam.me.uk>,
"linux-serial@vger.kernel.org" <linux-serial@vger.kernel.org>,
"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
Saurabh Singh Sengar <ssengar@microsoft.com>,
Praveen Kumar <kumarpraveen@linux.microsoft.com>,
"ishkamiel@gmail.com" <ishkamiel@gmail.com>,
"keescook@chromium.org" <keescook@chromium.org>,
"dwindsor@gmail.com" <dwindsor@gmail.com>
Subject: Re: [PATCH v4 1/2] tty: serial: dz: convert atomic_* to refcount_* APIs for map_guard
Date: Tue, 10 Jan 2023 08:57:52 +0100 [thread overview]
Message-ID: <Y70agALZ+PX3ju4f@kroah.com> (raw)
In-Reply-To: <Y70YKqt+Ej4kU9+h@ubun2204.myguest.virtualbox.org>
On Tue, Jan 10, 2023 at 01:17:54PM +0530, Deepak R Varma wrote:
> On Tue, Jan 10, 2023 at 07:27:44AM +0000, Reshetova, Elena wrote:
> >
> > > On Tue, Jan 03, 2023 at 09:59:52AM +0100, Jiri Slaby wrote:
> > > > On 26. 12. 22, 7:21, Deepak R Varma wrote:
> > > > > The refcount_* APIs are designed to address known issues with the
> > > > > atomic_t APIs for reference counting. They provide following distinct
> > > > > advantages
> > > > > - protect the reference counters from overflow/underflow
> > > > > - avoid use-after-free errors
> > > > > - provide improved memory ordering guarantee schemes
> > > > > - neater and safer.
> > > >
> > > > Really? (see below)
> > > >
> > > > > --- a/drivers/tty/serial/dz.c
> > > > > +++ b/drivers/tty/serial/dz.c
> > > > ...
> > > > > @@ -687,23 +686,19 @@ static int dz_map_port(struct uart_port *uport)
> > > > > static int dz_request_port(struct uart_port *uport)
> > > > > {
> > > > > struct dz_mux *mux = to_dport(uport)->mux;
> > > > > - int map_guard;
> > > > > int ret;
> > > > >
> > > > > - map_guard = atomic_add_return(1, &mux->map_guard);
> > > > > - if (map_guard == 1) {
> > > > > - if (!request_mem_region(uport->mapbase, dec_kn_slot_size,
> > > > > - "dz")) {
> > > > > - atomic_add(-1, &mux->map_guard);
> > > > > - 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,
> > > I found this [1] commit which introduced similar transformation in a
> > > neighbouring driver. Can you please comment how is this different from the
> > > current patch proposal?
> > >
> > > [1] commit ID: 22a33651a56f ("convert sbd_duart.map_guard from atomic_t to
> > > refcount_t")
> > >
> > > On a side note, I have not been able to find an exact 1:1 map to the
> > > atomic_add_result API. I am wondering should we have one?
> >
>
> Hello Elena,
>
> > In past we have decided not to provide this API for refcount_t
> > because for truly correctly behaving reference counters it should not be needed
> > (vs atomics that cover a broader range of use cases).
>
> So, there is no FAA refcount wrapper? I think this is a pretty common need.
> Please correct me if I am wrong.
>
> > Can you use !refcount_inc_not_zero in the above case?
>
> I actually did try that but was not sure if truly addresses the objection.
> Please attached and let me know if you have a feedback on the alternate
> approach.
>
> Thank you,
> ./drv
>
>
> >
> > Best Regards,
> > Elena.
> ############## ORIGINAL CODE ##################################
> - map_guard = atomic_add_return(1, &mux->map_guard);
> - if (map_guard == 1) {
> - if (!request_mem_region(uport->mapbase, dec_kn_slot_size,
> - "dz")) {
> - atomic_add(-1, &mux->map_guard);
> - printk(KERN_ERR
> - "dz: Unable to reserve MMIO resource\n");
> return -EBUSY;
> }
> }
>
> ############## INITIAL APPROACH ##################################
> + refcount_inc(&mux->map_guard);
> + if (refcount_read(&mux->map_guard) == 1) {
> + if (!request_mem_region(uport->mapbase, dec_kn_slot_size, "dz")) {
> + refcount_dec(&mux->map_guard);
> + printk(KERN_ERR "dz: Unable to reserve MMIO resource\n");
> return -EBUSY;
> }
> }
>
> ############## ALTERNATE APPROACH ##################################
>
> + if (!refcount_inc_not_zero(&mux->map_guard)) {
> + refcount_inc(&mux->map_guard);
> + if (!request_mem_region(uport->mapbase, dec_kn_slot_size, "dz")) {
> + refcount_dec(&mux->map_guard);
> + printk(KERN_ERR "dz: Unable to reserve MMIO resource\n");
> return -EBUSY;
> }
> }
>
This feels odd to me, why not just use a normal lock instead?
thanks,
greg k-h
next prev parent reply other threads:[~2023-01-10 7:59 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-12-24 16:32 [PATCH v3 0/2] tty: serial: dz: convert atomic_* to refcount_* Deepak R Varma
2022-12-24 16:33 ` [PATCH v3 1/2] tty: serial: dz: convert atomic_* to refcount_* APIs for map_guard Deepak R Varma
2022-12-26 6:21 ` [PATCH v4 " Deepak R Varma
2023-01-03 8:59 ` Jiri Slaby
2023-01-03 10:05 ` Deepak R Varma
2023-01-04 8:28 ` Greg Kroah-Hartman
2023-01-04 8:59 ` Deepak R Varma
2023-01-10 6:19 ` Deepak R Varma
2023-01-10 7:27 ` Reshetova, Elena
2023-01-10 7:47 ` Deepak R Varma
2023-01-10 7:57 ` Greg Kroah-Hartman [this message]
2022-12-24 16:34 ` [PATCH 2/2] tty: serial: dz: convert atomic_* to refcount_* APIs for irq_guard Deepak R Varma
2022-12-26 6:21 ` [PATCH v4 " Deepak R Varma
2023-01-03 9:00 ` Jiri Slaby
2023-01-03 10:09 ` Deepak R Varma
2023-01-04 8:28 ` Greg Kroah-Hartman
2023-01-04 9:00 ` Deepak R Varma
2022-12-26 6:20 ` [PATCH v4 0/2] tty: serial: dz: convert atomic_* to refcount_* Deepak R Varma
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=Y70agALZ+PX3ju4f@kroah.com \
--to=gregkh@linuxfoundation.org \
--cc=drv@mailo.com \
--cc=dwindsor@gmail.com \
--cc=elena.reshetova@intel.com \
--cc=ishkamiel@gmail.com \
--cc=jirislaby@kernel.org \
--cc=keescook@chromium.org \
--cc=kumarpraveen@linux.microsoft.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-serial@vger.kernel.org \
--cc=macro@orcam.me.uk \
--cc=ssengar@microsoft.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).