All of lore.kernel.org
 help / color / mirror / Atom feed
From: Alexey Dobriyan <adobriyan@gmail.com>
To: Xiu Jianfeng <xiujianfeng@huawei.com>
Cc: akpm@linux-foundation.org, keescook@chromium.org,
	laniel_francis@privacyrequired.com,
	andriy.shevchenko@linux.intel.com, linux@roeck-us.net,
	andreyknvl@gmail.com, dja@axtens.net, ast@kernel.org,
	daniel@iogearbox.net, andrii@kernel.org, kafai@fb.com,
	songliubraving@fb.com, yhs@fb.com, john.fastabend@gmail.com,
	kpsingh@kernel.org, linux-kernel@vger.kernel.org,
	netdev@vger.kernel.org, bpf@vger.kernel.org
Subject: Re: [PATCH -next 1/2] string.h: Introduce memset_range() for wiping members
Date: Wed, 8 Dec 2021 20:12:27 +0300	[thread overview]
Message-ID: <YbDne/nYsVai5pCV@localhost.localdomain> (raw)
In-Reply-To: <20211208030451.219751-2-xiujianfeng@huawei.com>

On Wed, Dec 08, 2021 at 11:04:50AM +0800, Xiu Jianfeng wrote:
> Motivated by memset_after() and memset_startat(), introduce a new helper,
> memset_range() that takes the target struct instance, the byte to write,
> and two member names where zeroing should start and end.
> 
> Signed-off-by: Xiu Jianfeng <xiujianfeng@huawei.com>
> ---
>  include/linux/string.h | 20 ++++++++++++++++++++
>  lib/memcpy_kunit.c     | 12 ++++++++++++
>  2 files changed, 32 insertions(+)
> 
> diff --git a/include/linux/string.h b/include/linux/string.h
> index b6572aeca2f5..7f19863253f2 100644
> --- a/include/linux/string.h
> +++ b/include/linux/string.h
> @@ -291,6 +291,26 @@ void memcpy_and_pad(void *dest, size_t dest_len, const void *src, size_t count,
>  	       sizeof(*(obj)) - offsetof(typeof(*(obj)), member));	\
>  })
>  
> +/**
> + * memset_range - Set a value ranging from member1 to member2, boundary included.
> + *
> + * @obj: Address of target struct instance
> + * @v: Byte value to repeatedly write
> + * @member1: struct member to start writing at
> + * @member2: struct member where writing should stop
> + *
> + */
> +#define memset_range(obj, v, member_1, member_2)			\
> +({									\
> +	u8 *__ptr = (u8 *)(obj);					\
> +	typeof(v) __val = (v);						\
> +	BUILD_BUG_ON(offsetof(typeof(*(obj)), member_1) >		\
> +		     offsetof(typeof(*(obj)), member_2));		\
> +	memset(__ptr + offsetof(typeof(*(obj)), member_1), __val,	\
> +	       offsetofend(typeof(*(obj)), member_2) -			\
> +	       offsetof(typeof(*(obj)), member_1));			\
> +})

"u8*" should be "void*" as kernel legitimises pointer arithmetic on void*
and there is no dereference.

__val is redundant, just toss "v" into memset(), it will do the right
thing. In fact, toss "__ptr" as well, it is simply unnecessary.

All previous memsets are the same...

  parent reply	other threads:[~2021-12-08 17:12 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-12-08  3:04 [PATCH -next 0/2] Introduce memset_range() helper for wiping members Xiu Jianfeng
2021-12-08  3:04 ` [PATCH -next 1/2] string.h: Introduce memset_range() " Xiu Jianfeng
2021-12-08  4:28   ` Andrew Morton
2021-12-08 10:30     ` xiujianfeng
2021-12-08 23:44       ` Andrew Morton
2021-12-09  5:17         ` Kees Cook
2021-12-09  6:29           ` xiujianfeng
2021-12-09  6:29         ` xiujianfeng
2021-12-08 17:12   ` Alexey Dobriyan [this message]
2021-12-08  3:04 ` [PATCH -next 2/2] bpf: use memset_range helper in __mark_reg_known Xiu Jianfeng
2021-12-08  5:27 ` [PATCH -next 0/2] Introduce memset_range() helper for wiping members Kees Cook
2021-12-08 10:30   ` xiujianfeng

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=YbDne/nYsVai5pCV@localhost.localdomain \
    --to=adobriyan@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=andreyknvl@gmail.com \
    --cc=andrii@kernel.org \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=dja@axtens.net \
    --cc=john.fastabend@gmail.com \
    --cc=kafai@fb.com \
    --cc=keescook@chromium.org \
    --cc=kpsingh@kernel.org \
    --cc=laniel_francis@privacyrequired.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@roeck-us.net \
    --cc=netdev@vger.kernel.org \
    --cc=songliubraving@fb.com \
    --cc=xiujianfeng@huawei.com \
    --cc=yhs@fb.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 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.