All of lore.kernel.org
 help / color / mirror / Atom feed
From: Peter Zijlstra <peterz@infradead.org>
To: Lin Yujun <linyujun809@huawei.com>
Cc: 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,
	gustavoars@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 v2] x86/events:Use struct_size() helper in kzalloc()
Date: Thu, 19 May 2022 09:52:20 +0200	[thread overview]
Message-ID: <20220519075220.GD2578@worktop.programming.kicks-ass.net> (raw)
In-Reply-To: <20220519023600.241591-1-linyujun809@huawei.com>

On Thu, May 19, 2022 at 10:36:00AM +0800, Lin Yujun wrote:
> Make use of the struct_size() helper instead of an open-coded version,
> in order to avoid any potential type mistakes or integer overflows that,
> in the worst scenario, could lead to heap overflows.
> 
> Signed-off-by: Lin Yujun <linyujun809@huawei.com>
> ---
>  arch/x86/events/rapl.c | 4 +---
>  1 file changed, 1 insertion(+), 3 deletions(-)
> 
> diff --git a/arch/x86/events/rapl.c b/arch/x86/events/rapl.c
> index 77e3a47af5ad..8da003e02010 100644
> --- a/arch/x86/events/rapl.c
> +++ b/arch/x86/events/rapl.c
> @@ -683,10 +683,8 @@ static const struct attribute_group *rapl_attr_update[] = {
>  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 *);
> -	rapl_pmus = kzalloc(size, GFP_KERNEL);
> +	rapl_pmus = kzalloc(struct_size(rapl_pmus, pmus, maxdie), GFP_KERNEL);

So I really hate that thing; it's pointless obfuscation. If you're
really worried about the type confusion, write it like:

	size = sizeof(*rapl_pmus) + sizeof(rapl_pmus->pmus[0]) * maxdie;

or something. Otherwise, just go away.

      reply	other threads:[~2022-05-19  7:52 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-19  2:36 [PATCH -next v2] x86/events:Use struct_size() helper in kzalloc() Lin Yujun
2022-05-19  7:52 ` Peter Zijlstra [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=20220519075220.GD2578@worktop.programming.kicks-ass.net \
    --to=peterz@infradead.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=gustavoars@kernel.org \
    --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=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 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.