mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Juergen Gross <jgross@suse.com>
To: Christophe JAILLET <christophe.jaillet@wanadoo.fr>,
	Tu Dinh <ngoc-tu.dinh@vates.tech>,
	Abinash <abinashlalotra@gmail.com>
Cc: sstabellini@kernel.org, oleksandr_tyshchenko@epam.com,
	xen-devel@lists.xenproject.org, linux-kernel@vger.kernel.org,
	Abinash Singh <abinashsinghlalotra@gmail.com>
Subject: Re: [RFC PATCH] xen/gntdev: reduce stack usage by dynamically allocating gntdev_copy_batch
Date: Fri, 4 Jul 2025 08:05:55 +0200	[thread overview]
Message-ID: <f8335a71-f9af-4911-96fa-46e7088acaf8@suse.com> (raw)
In-Reply-To: <f1c68345-a511-46b8-964c-a00bb62274ba@wanadoo.fr>


[-- Attachment #1.1.1: Type: text/plain, Size: 3497 bytes --]

On 03.07.25 19:35, Christophe JAILLET wrote:
> Le 03/07/2025 à 07:22, Jürgen Groß a écrit :
>> On 03.07.25 00:42, Tu Dinh wrote:
>>> On 01/07/2025 23:53, Abinash wrote:
>>>> Hi ,
>>>>
>>>> Thanks for pointing that out.
>>>>
>>>> I haven’t measured the performance impact yet — my main focus was on
>>>> getting rid of the stack usage warning triggered by LLVM due to
>>>> inlining. But you're right, gntdev_ioctl_grant_copy() is on a hot
>>>> path, and calling kmalloc() there could definitely slow things down,
>>>> especially under memory pressure.
>>>>
>>>> I’ll run some benchmarks to compare the current approach with the
>>>> dynamic allocation, and also look into alternatives — maybe
>>>> pre-allocating the struct or limiting inlining instead. If you have
>>>> any ideas or suggestions on how best to approach this, I’d be happy to
>>>> hear them.
>>>>
>>>> Do you have any suggestions on how to test the performance?
>>>>
>>>> Best,
>>>> Abinash
>>>>
>>>>
>>>
>>> Preallocating may work but I'd be wary of synchronization if the
>>> preallocated struct is shared.
>>>
>>> I'd look at optimizing status[] which should save quite a few bytes.
>>>
>>> Reducing GNTDEV_COPY_BATCH could be a last resort, but that may also
>>> impact performance.
>>
>> IMHO the most promising way would be to dynamically allocate the struct, but
>> don't free it at the end of the ioctl. Instead it could be put into a list
>> anchored in struct gntdev_priv, so freeing would be done only at close() time.
>>
>> Synchronization would be minimal (just for taking a free struct from the list
>> or putting it back again), while memory usage would be basically just as needed,
>> depending on the number of concurrent threads using the same file descriptor
>> for the ioctl.
>>
>> This approach would even allow to raise GNTDEV_COPY_BATCH, maybe resulting even
>> in a gain of performance.
>>
>> I'll write a patch implementing the allocation scheme.
>>
>>
>> Juergen
> 
> It may be an overkill, but sometimes we see pattern that try to keep the best of 
> the 2 worlds. Something like:
> 
> 
> static struct gntdev_copy_batch static_batch;
> static struct mutex my_mutex;
> 
> static long gntdev_ioctl_grant_copy(...)
> {
>      struct gntdev_copy_batch *dynamic_batch = NULL;
>      struct gntdev_copy_batch *batch;
> 
>      ...
> 
>      if (mutex_trylock(&my_mutex)) {
>          /*
>           * No concurrent access?
>           * Use a shared static variable to avoid an allocation
>           */
>          batch = &static_batch;
>      else {
>          /* otherwise, we need some fresh memory */
>          dynamic_batch = kmalloc(sizeof(*batch), GFP_KERNEL);
>          if (!batch)
>              return -ENOMEM;
> 
>          batch = dynamic_batch;
>      }
> 
>      /* do stuff with 'batch' */
>      ...
> 
> free_batch:
>      if (!dynamic_batch)
>          mutex_unlock(&my_mutex);
>      else
>          kfree(dynamic_batch);
>       return ret;
>   }
> 
> 
> Just my 2c.

Thanks for the remark, but this won't help much. gntdev_ioctl_grant_copy()
is e.g. used by qemu for doing disk I/O on behalf of a Xen guest. This
means that it is very likely to be used concurrently, so just optimizing
for a single thread won't be enough.


Juergen

[-- Attachment #1.1.2: OpenPGP public key --]
[-- Type: application/pgp-keys, Size: 3743 bytes --]

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 495 bytes --]

      reply	other threads:[~2025-07-04  6:06 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-29 20:42 Abinash Singh
2025-06-30 10:35 ` Tu Dinh
2025-07-01 21:53   ` Abinash
2025-07-02 22:42     ` Tu Dinh
2025-07-03  5:22       ` Jürgen Groß
2025-07-03 17:35         ` Christophe JAILLET
2025-07-04  6:05           ` Juergen Gross [this message]

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=f8335a71-f9af-4911-96fa-46e7088acaf8@suse.com \
    --to=jgross@suse.com \
    --cc=abinashlalotra@gmail.com \
    --cc=abinashsinghlalotra@gmail.com \
    --cc=christophe.jaillet@wanadoo.fr \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ngoc-tu.dinh@vates.tech \
    --cc=oleksandr_tyshchenko@epam.com \
    --cc=sstabellini@kernel.org \
    --cc=xen-devel@lists.xenproject.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

all inboxes | Powered by JetHome®