public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Steven Rostedt <rostedt@goodmis.org>
To: linux-kernel@vger.kernel.org
Cc: Ingo Molnar <mingo@kernel.org>,
	Andrew Morton <akpm@linux-foundation.org>,
	"Gustavo A. R. Silva" <gustavo@embeddedor.com>
Subject: [for-next][PATCH 02/12] tracepoint: Use struct_size() in kmalloc()
Date: Tue, 25 Jun 2019 15:15:12 -0400	[thread overview]
Message-ID: <20190625191545.096711209@goodmis.org> (raw)
In-Reply-To: 20190625191510.599310671@goodmis.org

From: "Gustavo A. R. Silva" <gustavo@embeddedor.com>

One of the more common cases of allocation size calculations is finding
the size of a structure that has a zero-sized array at the end, along
with memory for some number of elements for that array. For example:

struct tp_probes {
	...
        struct tracepoint_func probes[0];
};

instance = kmalloc(sizeof(sizeof(struct tp_probes) +
			sizeof(struct tracepoint_func) * count, GFP_KERNEL);

Instead of leaving these open-coded and prone to type mistakes, we can
now use the new struct_size() helper:

instance = kmalloc(struct_size(instance, probes, count) GFP_KERNEL);

This code was detected with the help of Coccinelle.

Signed-off-by: Gustavo A. R. Silva <gustavo@embeddedor.com>
Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
---
 kernel/tracepoint.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/kernel/tracepoint.c b/kernel/tracepoint.c
index 46f2ab1e08a9..fb9353ed901b 100644
--- a/kernel/tracepoint.c
+++ b/kernel/tracepoint.c
@@ -68,8 +68,8 @@ struct tp_probes {
 
 static inline void *allocate_probes(int count)
 {
-	struct tp_probes *p  = kmalloc(count * sizeof(struct tracepoint_func)
-			+ sizeof(struct tp_probes), GFP_KERNEL);
+	struct tp_probes *p  = kmalloc(struct_size(p, probes, count),
+				       GFP_KERNEL);
 	return p == NULL ? NULL : p->probes;
 }
 
-- 
2.20.1



  parent reply	other threads:[~2019-06-25 19:16 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-06-25 19:15 [for-next][PATCH 00/12] tracing: More updates for 5.3 Steven Rostedt
2019-06-25 19:15 ` [for-next][PATCH 01/12] ring-buffer: Remove HAVE_64BIT_ALIGNED_ACCESS Steven Rostedt
2019-06-25 19:15 ` Steven Rostedt [this message]
2019-06-25 19:15 ` [for-next][PATCH 03/12] kprobes: Fix to init kprobes in subsys_initcall Steven Rostedt
2019-06-25 19:15 ` [for-next][PATCH 04/12] tracing/kprobe: Set print format right after parsed command Steven Rostedt
2019-06-25 19:15 ` [for-next][PATCH 05/12] tracing/uprobe: Set print format when parsing command Steven Rostedt
2019-06-25 19:15 ` [for-next][PATCH 06/12] tracing/probe: Add trace_probe init and free functions Steven Rostedt
2019-06-25 19:15 ` [for-next][PATCH 07/12] tracing/probe: Add trace_event_call register API for trace_probe Steven Rostedt
2019-06-25 19:15 ` [for-next][PATCH 08/12] tracing/probe: Add trace_event_file access APIs " Steven Rostedt
2019-06-25 19:15 ` [for-next][PATCH 09/12] tracing/probe: Add trace flag " Steven Rostedt
2019-06-25 19:15 ` [for-next][PATCH 10/12] tracing/probe: Add probe event name and group name accesses APIs Steven Rostedt
2019-06-25 19:15 ` [for-next][PATCH 11/12] tracing/probe: Add trace_event_call " Steven Rostedt
2019-06-25 19:15 ` [for-next][PATCH 12/12] tracing/kprobe: Check registered state using kprobe Steven Rostedt

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=20190625191545.096711209@goodmis.org \
    --to=rostedt@goodmis.org \
    --cc=akpm@linux-foundation.org \
    --cc=gustavo@embeddedor.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@kernel.org \
    /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