public inbox for netdev@vger.kernel.org
 help / color / mirror / Atom feed
From: Stefan Metzmacher <metze@samba.org>
To: David Laight <david.laight.linux@gmail.com>
Cc: linux-kernel@vger.kernel.org,
	Dmitry Safonov <0x7f454c46@gmail.com>,
	Dmitry Safonov <dima@arista.com>,
	Francesco Ruggeri <fruggeri@arista.com>,
	Salam Noureddine <noureddine@arista.com>,
	David Ahern <dsahern@kernel.org>,
	"David S . Miller" <davem@davemloft.net>,
	Michal Luczaj <mhal@rbox.co>, David Wei <dw@davidwei.uk>,
	Luiz Augusto von Dentz <luiz.von.dentz@intel.com>,
	Luiz Augusto von Dentz <luiz.dentz@gmail.com>,
	Marcel Holtmann <marcel@holtmann.org>,
	Xin Long <lucien.xin@gmail.com>,
	Eric Dumazet <edumazet@google.com>,
	Kuniyuki Iwashima <kuniyu@google.com>,
	Paolo Abeni <pabeni@redhat.com>,
	Willem de Bruijn <willemb@google.com>,
	Neal Cardwell <ncardwell@google.com>,
	Jakub Kicinski <kuba@kernel.org>, Simon Horman <horms@kernel.org>,
	Aleksa Sarai <cyphar@cyphar.com>,
	Christian Brauner <brauner@kernel.org>,
	Kees Cook <keescook@chromium.org>,
	netdev@vger.kernel.org, linux-bluetooth@vger.kernel.org
Subject: Re: [PATCH 3/5] uaccess: add copy_struct_{from,to}_bounce_buffer() helpers
Date: Thu, 9 Apr 2026 10:47:29 +0200	[thread overview]
Message-ID: <fb21f224-310e-4744-8216-62fd93f36955@samba.org> (raw)
In-Reply-To: <20260407192540.321f3879@pumpkin>

Hi David,

> On Tue,  7 Apr 2026 18:03:15 +0200
> Stefan Metzmacher <metze@samba.org> wrote:
> 
>> These are similar to copy_struct_{from,to}_user() but operate
>> on kernel buffers instead of user buffers.
>>
>> They can be used when there is a temporary bounce buffer used,
>> e.g. in msg_control or similar places.
>>
>> It allows us to have the same logic to handle old vs. current
>> and current vs. new structures in the same compatible way.
>>
>> copy_struct_from_sockptr() will also be able to
>> use copy_struct_from_bounce_buffer() for the kernel
>> case as follow us patch.
>>
>> I'll use this in my IPPROTO_SMBDIRECT work,
>> but maybe it will also be useful for others...
>> IPPROTO_QUIC will likely also use it.
>>
>> Cc: Dmitry Safonov <0x7f454c46@gmail.com>
>> Cc: Dmitry Safonov <dima@arista.com>
>> Cc: Francesco Ruggeri <fruggeri@arista.com>
>> Cc: Salam Noureddine <noureddine@arista.com>
>> Cc: David Ahern <dsahern@kernel.org>
>> Cc: David S. Miller <davem@davemloft.net>
>> Cc: Michal Luczaj <mhal@rbox.co>
>> Cc: David Wei <dw@davidwei.uk>
>> Cc: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
>> Cc: Luiz Augusto von Dentz <luiz.dentz@gmail.com>
>> Cc: Marcel Holtmann <marcel@holtmann.org>
>> Cc: Xin Long <lucien.xin@gmail.com>
>> Cc: Eric Dumazet <edumazet@google.com>
>> Cc: Kuniyuki Iwashima <kuniyu@google.com>
>> Cc: Paolo Abeni <pabeni@redhat.com>
>> Cc: Willem de Bruijn <willemb@google.com>
>> Cc: Neal Cardwell <ncardwell@google.com>
>> Cc: Jakub Kicinski <kuba@kernel.org>
>> Cc: Simon Horman <horms@kernel.org>
>> Cc: Aleksa Sarai <cyphar@cyphar.com>
>> Cc: Christian Brauner <brauner@kernel.org>
>> CC: Kees Cook <keescook@chromium.org>
>> Cc: netdev@vger.kernel.org
>> Cc: linux-bluetooth@vger.kernel.org
>> Cc: linux-kernel@vger.kernel.org
>> Signed-off-by: Stefan Metzmacher <metze@samba.org>
>> ---
>>   include/linux/uaccess.h | 63 +++++++++++++++++++++++++++++++++++++++++
>>   1 file changed, 63 insertions(+)
>>
>> diff --git a/include/linux/uaccess.h b/include/linux/uaccess.h
>> index 1234b5fa4761..a6cd4f48bb99 100644
>> --- a/include/linux/uaccess.h
>> +++ b/include/linux/uaccess.h
>> @@ -513,6 +513,69 @@ copy_struct_to_user(void __user *dst, size_t usize, const void *src,
>>   	return 0;
>>   }
>>   
>> +static __always_inline void
>> +__copy_struct_generic_bounce_buffer(void *dst, size_t dstsize,
>> +				    const void *src, size_t srcsize,
>> +				    bool *ignored_trailing)
>> +{
>> +	size_t size = min(dstsize, srcsize);
>> +	size_t rest = max(dstsize, srcsize) - size;
>> +
>> +	/* Deal with trailing bytes. */
>> +	if (dstsize > srcsize)
>> +		memset(dst + size, 0, rest);
>> +	if (ignored_trailing)
>> +		*ignored_trailing = dstsize < srcsize &&
>> +			memchr_inv(src + size, 0, rest) != NULL;
>> +	/* Copy the interoperable parts of the struct. */
>> +	memcpy(dst, src, size);
>> +}
> 
> Return 'ignored_trailing' rather than pass by reference.

I also thought about that but it makes
the copy_struct_to_ case more complex.

I'm not sure but my guess would be that
the compiler would have the chance to skip the
ignore_trailing logic if (as all current callers do)
NULL is passed.

> And this is probably too big to inline.

In the next patch this replace open coded logic in
copy_struct_from_sockptr. So as all of copy_struct_*
consists of inline functions I thought it would be good to
keep it that way.

So unless there a real good reason to change it
I'd like to keep it as is.

Thanks!
metze

  reply	other threads:[~2026-04-09  8:47 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-07 16:03 [PATCH 0/5] uaccess/sockptr: copy_struct_ fixes and more helpers Stefan Metzmacher
2026-04-07 16:03 ` [PATCH 1/5] uaccess: fix ignored_trailing logic in copy_struct_to_user() Stefan Metzmacher
2026-04-09  6:33   ` Aleksa Sarai
2026-04-09  9:01     ` Stefan Metzmacher
2026-04-07 16:03 ` [PATCH 2/5] sockptr: fix usize check in copy_struct_from_sockptr() for user pointers Stefan Metzmacher
2026-04-09  6:37   ` Aleksa Sarai
2026-04-09  6:39     ` Aleksa Sarai
2026-04-07 16:03 ` [PATCH 3/5] uaccess: add copy_struct_{from,to}_bounce_buffer() helpers Stefan Metzmacher
2026-04-07 18:25   ` David Laight
2026-04-09  8:47     ` Stefan Metzmacher [this message]
2026-04-07 16:03 ` [PATCH 4/5] sockptr: let copy_struct_from_sockptr() use copy_struct_from_bounce_buffer() Stefan Metzmacher
2026-04-07 16:03 ` [PATCH 5/5] sockptr: introduce copy_struct_to_sockptr() Stefan Metzmacher
2026-04-09 13:05 ` [PATCH 0/5] uaccess/sockptr: copy_struct_ fixes and more helpers Christian Brauner

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=fb21f224-310e-4744-8216-62fd93f36955@samba.org \
    --to=metze@samba.org \
    --cc=0x7f454c46@gmail.com \
    --cc=brauner@kernel.org \
    --cc=cyphar@cyphar.com \
    --cc=davem@davemloft.net \
    --cc=david.laight.linux@gmail.com \
    --cc=dima@arista.com \
    --cc=dsahern@kernel.org \
    --cc=dw@davidwei.uk \
    --cc=edumazet@google.com \
    --cc=fruggeri@arista.com \
    --cc=horms@kernel.org \
    --cc=keescook@chromium.org \
    --cc=kuba@kernel.org \
    --cc=kuniyu@google.com \
    --cc=linux-bluetooth@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lucien.xin@gmail.com \
    --cc=luiz.dentz@gmail.com \
    --cc=luiz.von.dentz@intel.com \
    --cc=marcel@holtmann.org \
    --cc=mhal@rbox.co \
    --cc=ncardwell@google.com \
    --cc=netdev@vger.kernel.org \
    --cc=noureddine@arista.com \
    --cc=pabeni@redhat.com \
    --cc=willemb@google.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