linux-perf-users.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Gustavo A. R. Silva" <gustavoars@kernel.org>
To: Lin Yujun <linyujun809@huawei.com>
Cc: peterz@infradead.org, mingo@redhat.com, acme@kernel.org,
	mark.rutland@arm.com, alexander.shishkin@linux.intel.com,
	jolsa@kernel.org, namhyung@kernel.org, tglx@linutronix.de,
	bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org,
	hpa@zytor.com, linux-perf-users@vger.kernel.org,
	linux-kernel@vger.kernel.org, johnny.chenyi@huawei.com,
	chenjiahao16@huawei.com, chenlifu@huawei.com,
	lizhengyu3@huawei.com, liaochang1@huawei.com,
	wangzhu9@huawei.com, xuyihang@huawei.com, chris.zjh@huawei.com,
	zouyipeng@huawei.com
Subject: Re: [PATCH -next] x86/events:Use struct_size() helper in kzalloc()
Date: Wed, 18 May 2022 08:49:20 -0500	[thread overview]
Message-ID: <20220518134920.GA8361@embeddedor> (raw)
In-Reply-To: <20220518131626.109123-1-linyujun809@huawei.com>

On Wed, May 18, 2022 at 09:16:26PM +0800, Lin Yujun wrote:
> Replace sizeof() with struct_size() to avoid potential integer
> or heap overflow.

This is not exactly what the patch does.

Your patch is replacing the open-coded calculation of the total amount
of memory to be allocated for rapl_pmus with struct_size().

> 
> Also, address the following sparse warnings:
> arch/x86/events/rapl.c:685:16: warning: using sizeof on a
> flexible structure

This does not address this warning because struct_size()
still make use of sizeof on the struct-with-flex-array,
internally.

So, the mention to the sparse warning should be removed from
this changelog text.

> 
> Signed-off-by: Lin Yujun <linyujun809@huawei.com>
> ---
>  arch/x86/events/rapl.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/arch/x86/events/rapl.c b/arch/x86/events/rapl.c
> index 77e3a47af5ad..c7e79f0ac04f 100644
> --- a/arch/x86/events/rapl.c
> +++ b/arch/x86/events/rapl.c
> @@ -685,7 +685,7 @@ static int __init init_rapl_pmus(void)
>  	int maxdie = topology_max_packages() * topology_max_die_per_package();
>  	size_t size;
>  
> -	size = sizeof(*rapl_pmus) + maxdie * sizeof(struct rapl_pmu *);
> +	size = struct_size(rapl_pmus, pmus, maxdie);
>  	rapl_pmus = kzalloc(size, GFP_KERNEL);

It seems that in this case, size could be entirely replaced by
struct_size().

Thanks
--
Gustavo

  reply	other threads:[~2022-05-18 13:40 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-18 13:16 [PATCH -next] x86/events:Use struct_size() helper in kzalloc() Lin Yujun
2022-05-18 13:49 ` Gustavo A. R. Silva [this message]
2022-05-18 13:55   ` Peter Zijlstra
2022-05-18 16:03     ` Gustavo A. R. Silva
2022-05-18 16:54       ` Borislav Petkov
2022-05-19  2:30   ` linyujun (C)

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=20220518134920.GA8361@embeddedor \
    --to=gustavoars@kernel.org \
    --cc=acme@kernel.org \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=bp@alien8.de \
    --cc=chenjiahao16@huawei.com \
    --cc=chenlifu@huawei.com \
    --cc=chris.zjh@huawei.com \
    --cc=dave.hansen@linux.intel.com \
    --cc=hpa@zytor.com \
    --cc=johnny.chenyi@huawei.com \
    --cc=jolsa@kernel.org \
    --cc=liaochang1@huawei.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=linyujun809@huawei.com \
    --cc=lizhengyu3@huawei.com \
    --cc=mark.rutland@arm.com \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=peterz@infradead.org \
    --cc=tglx@linutronix.de \
    --cc=wangzhu9@huawei.com \
    --cc=x86@kernel.org \
    --cc=xuyihang@huawei.com \
    --cc=zouyipeng@huawei.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;
as well as URLs for NNTP newsgroup(s).