All of lore.kernel.org
 help / color / mirror / Atom feed
* qemu-user: avoid allocations to convert stuff when not necessary
@ 2023-04-09  8:52 Michael Tokarev
  2023-04-09 14:04 ` Warner Losh
  2023-04-09 16:27 ` Richard Henderson
  0 siblings, 2 replies; 5+ messages in thread
From: Michael Tokarev @ 2023-04-09  8:52 UTC (permalink / raw)
  To: QEMU Developers; +Cc: Laurent Vivier

Hi!

In the qemu-user case, we allocate various structures and arrays
for conversion of data between host and guest byte orders and sizes.
But it is actually not necessary to do such allocation when the
*size* is the same, and only byte order is different, because the
conversion can be done in-place.  Does it make any sense to avoid'
allocations in such cases?

There are 2 issues with this though. First is that in some cases,
the data being converted is const, and we may end up writing to a
data resides in a read-only segment, is it ever possible?  And
second - it is not entirely clear what to do in case the syscall
returned error.

Thanks,

/mjt


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: qemu-user: avoid allocations to convert stuff when not necessary
  2023-04-09  8:52 qemu-user: avoid allocations to convert stuff when not necessary Michael Tokarev
@ 2023-04-09 14:04 ` Warner Losh
  2023-04-09 14:56   ` Michael Tokarev
  2023-04-09 16:27 ` Richard Henderson
  1 sibling, 1 reply; 5+ messages in thread
From: Warner Losh @ 2023-04-09 14:04 UTC (permalink / raw)
  To: Michael Tokarev; +Cc: QEMU Developers, Laurent Vivier

[-- Attachment #1: Type: text/plain, Size: 1223 bytes --]

On Sun, Apr 9, 2023 at 2:53 AM Michael Tokarev <mjt@tls.msk.ru> wrote:

> Hi!
>
> In the qemu-user case, we allocate various structures and arrays
> for conversion of data between host and guest byte orders and sizes.
> But it is actually not necessary to do such allocation when the
> *size* is the same, and only byte order is different, because the
> conversion can be done in-place.  Does it make any sense to avoid'
> allocations in such cases?
>
> There are 2 issues with this though. First is that in some cases,
> the data being converted is const, and we may end up writing to a
> data resides in a read-only segment, is it ever possible?  And
> second - it is not entirely clear what to do in case the syscall
> returned error.
>

I don't think you can reliably do it in place. What if another thread in the
guest reads the data after you've converted it? It will get the wrong
answer.
I think you have to copy when endian mismatches, just like when alignment,
data size or layout differences are present. You'd need to convert it back
after the system call as well, which can cause problems, especially
if the system call needs multiple steps to emulate for whatever reason.

Warner

[-- Attachment #2: Type: text/html, Size: 1662 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: qemu-user: avoid allocations to convert stuff when not necessary
  2023-04-09 14:04 ` Warner Losh
@ 2023-04-09 14:56   ` Michael Tokarev
  0 siblings, 0 replies; 5+ messages in thread
From: Michael Tokarev @ 2023-04-09 14:56 UTC (permalink / raw)
  To: Warner Losh; +Cc: QEMU Developers, Laurent Vivier

09.04.2023 17:04, Warner Losh пишет:
> On Sun, Apr 9, 2023 at 2:53 AM Michael Tokarev <mjt@tls.msk.ru <mailto:mjt@tls.msk.ru>> wrote:
> 
>     Hi!
> 
>     In the qemu-user case, we allocate various structures and arrays
>     for conversion of data between host and guest byte orders and sizes.
>     But it is actually not necessary to do such allocation when the
>     *size* is the same, and only byte order is different, because the
>     conversion can be done in-place.  Does it make any sense to avoid'
>     allocations in such cases?
> 
>     There are 2 issues with this though. First is that in some cases,
>     the data being converted is const, and we may end up writing to a
>     data resides in a read-only segment, is it ever possible?  And
>     second - it is not entirely clear what to do in case the syscall
>     returned error.
> 
> 
> I don't think you can reliably do it in place. What if another thread in the
> guest reads the data after you've converted it? It will get the wrong answer.
> I think you have to copy when endian mismatches, just like when alignment,
> data size or layout differences are present. You'd need to convert it back
> after the system call as well, which can cause problems, especially
> if the system call needs multiple steps to emulate for whatever reason.

Han. I thought more about syscalls which accept in-out parameters.
Such as poll(fd, n, pfd) which uses pfd array for both input and
output. Or just for output, like getgroups() - for this one, if
sizeof(gid_t) == sizeof(target_gid_t), there's no need to allocate
anything, the supplied array can be used, and we need just a loop
after syscall returned, to convert each gid_t to target_gid_t.

For pure-input syscalls, like setgroups(), it is better to perform
allocation IMHO. If not only to avoid a case when RO-segment is
used.

/mjt


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: qemu-user: avoid allocations to convert stuff when not necessary
  2023-04-09  8:52 qemu-user: avoid allocations to convert stuff when not necessary Michael Tokarev
  2023-04-09 14:04 ` Warner Losh
@ 2023-04-09 16:27 ` Richard Henderson
  2023-04-10  7:55   ` Michael Tokarev
  1 sibling, 1 reply; 5+ messages in thread
From: Richard Henderson @ 2023-04-09 16:27 UTC (permalink / raw)
  To: Michael Tokarev, QEMU Developers; +Cc: Laurent Vivier

On 4/9/23 01:52, Michael Tokarev wrote:
> Hi!
> 
> In the qemu-user case, we allocate various structures and arrays
> for conversion of data between host and guest byte orders and sizes.
> But it is actually not necessary to do such allocation when the
> *size* is the same, and only byte order is different, because the
> conversion can be done in-place.  Does it make any sense to avoid'
> allocations in such cases?

Alignment can also change.  This is especially visible with m68k guest, where even 'int' 
is 2-byte aligned.

So, no.  It's best to just allocate and convert always.


r~


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: qemu-user: avoid allocations to convert stuff when not necessary
  2023-04-09 16:27 ` Richard Henderson
@ 2023-04-10  7:55   ` Michael Tokarev
  0 siblings, 0 replies; 5+ messages in thread
From: Michael Tokarev @ 2023-04-10  7:55 UTC (permalink / raw)
  To: Richard Henderson, QEMU Developers; +Cc: Laurent Vivier

09.04.2023 19:27, Richard Henderson пишет:
> On 4/9/23 01:52, Michael Tokarev wrote:
>> Hi!
>>
>> In the qemu-user case, we allocate various structures and arrays
>> for conversion of data between host and guest byte orders and sizes.
>> But it is actually not necessary to do such allocation when the
>> *size* is the same, and only byte order is different, because the
>> conversion can be done in-place.  Does it make any sense to avoid'
>> allocations in such cases?
> 
> Alignment can also change.  This is especially visible with m68k guest, where even 'int' is 2-byte aligned.

I didn't think about the alignment. So such optimization has less and
less chances to happen. I see.

> So, no.  It's best to just allocate and convert always.

Yes, this makes good sense.

Thanks!

/mjt



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2023-04-10  7:56 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-04-09  8:52 qemu-user: avoid allocations to convert stuff when not necessary Michael Tokarev
2023-04-09 14:04 ` Warner Losh
2023-04-09 14:56   ` Michael Tokarev
2023-04-09 16:27 ` Richard Henderson
2023-04-10  7:55   ` Michael Tokarev

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.