Linux USB
 help / color / mirror / Atom feed
From: Ingo Rohloff <ingo.rohloff@lauterbach.com>
To: Greg KH <gregkh@linuxfoundation.org>
Cc: balbi@kernel.org, linux-usb@vger.kernel.org
Subject: Re: [PATCH v2 1/1] usb: gadget: f_fs: Support zerocopy transfers via mmap.
Date: Tue, 8 Feb 2022 12:48:17 +0100	[thread overview]
Message-ID: <20220208124817.3bd884ba@ingpc2> (raw)
In-Reply-To: <YgJJi0q+/oV9SRq8@kroah.com>

Hello Greg,

On Tue, 8 Feb 2022 11:44:27 +0100
Greg KH <gregkh@linuxfoundation.org> wrote:

> On Wed, Feb 02, 2022 at 06:20:56PM +0100, Ingo Rohloff wrote:
> > This patch implements the same functionality for FunctionFS as
> > commit f7d34b445abc00e979b7 ("USB: Add support for usbfs zerocopy.")
> > did for USB host devio.c

> ...

> > +/* Check whether it's okay to allocate more memory for mmap */
> > +static int ffsm_increase_mmap_mem_usage(struct ffs_data *ffs, u64 amount)
> > +{
> > +	u64 lim;
> > +
> > +	lim = READ_ONCE(ffs->mmap_memory_mb);
> > +	lim <<= 20;
> > +
> > +	atomic64_add(amount, &ffs->mmap_mem_usage);
> > +
> > +	if (lim > 0 && atomic64_read(&ffs->mmap_mem_usage) > lim) {  
> 
> What prevents it from changing right after you read this?

Nothing.

First of all: I just used the same code as in "drivers/usb/core/devio.c"
functions "usbfs_increase_memory_usage()" and "usbfs_decrease_memory_usage()".

As far as I understand the code of these two functions, this code to imposes a
limit on the amount of kernel space memory, a user might allocate via "mmap"
calls.

The construction makes sure that when "ffsm_increase_mmap_mem_usage()" returns
successfully, it is guaranteed, that "mmap_mem_usage" *was* smaller/equal than 
"lim" at the time the atomic64_read() call was done.

Of course "mmap_mem_usage" might change immediately after the atomic64_read().
But as far as I analyzed, this still limits the total amount of memory
allocated, as long as you make sure that you do the allocation of memory
*after* "ffsm_increase_mmap_mem_usage()" returned successfully and you
deallocate the memory *before* you call "ffsm_decrease_mmap_mem_usage()".


> > +		atomic64_sub(amount, &ffs->mmap_mem_usage);  
> 
> Why not use a real lock instead of trying to do a fake one with this
> atomic variable?

I don't think there is a good reason using the "atomic" stuff:
I think this code path anyway is not hit that often (only when you mmap or
munmap buffers), so this should not have any noticeable impact on performance.

I just took the code from "drivers/usb/core/devio.c",
"usbfs_increase_memory_usage()".
I am still convinced it is correct.

You are of course right: You can easily use a lock here and this makes the
intention of the code a lot clearer I guess.

I will modify the patch accordingly.

so long
  Ingo

  reply	other threads:[~2022-02-08 11:55 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-02 17:20 [PATCH v2 0/1] Zerocopy for USB Gadget FunctionFS Ingo Rohloff
2022-02-02 17:20 ` [PATCH v2 1/1] usb: gadget: f_fs: Support zerocopy transfers via mmap Ingo Rohloff
2022-02-08 10:44   ` Greg KH
2022-02-08 11:48     ` Ingo Rohloff [this message]
2022-02-08 15:21       ` Alan Stern

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=20220208124817.3bd884ba@ingpc2 \
    --to=ingo.rohloff@lauterbach.com \
    --cc=balbi@kernel.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-usb@vger.kernel.org \
    /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