Skip to contentSkip to conversation

tracing/syscalls: Use kcalloc instead of kzalloc to allocate array

PATCH In review Thomas Meyer 1 participant 2 messages Dec 5, 2011 – Feb 17, 2012

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tracing/syscalls: Use kcalloc instead of kzalloc to allocate array

The advantage of kcalloc is, that will prevent integer overflows which could result from the multiplication of number of elements and size and it is also a bit nicer to read.

The semantic patch that makes this change is available in https://lkml.org/lkml/2011/11/25/107

---

kernel/trace/ftrace.c 2011-11-28 20:09:21.703638214 +0100 renamed from kernel/trace/ftrace.c 2011-11-13 11:08:12.084207678 +0100 +1 -1
diff -u -p a/kernel/trace/ftrace.c b/kernel/trace/ftrace.c
@@ -1131,7 +1131,7 @@ static struct ftrace_hash *alloc_ftrace_
 		return NULL;
 
 	size = 1 << size_bits;
-	hash->buckets = kzalloc(sizeof(*hash->buckets) * size, GFP_KERNEL);
+	hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
 
 	if (!hash->buckets) {
 		kfree(hash);
kernel/trace/trace_events_filter.c 2011-11-28 20:09:19.846949470 +0100 renamed from kernel/trace/trace_events_filter.c 2011-11-13 11:08:12.107541380 +0100 +3 -4
diff -u -p a/kernel/trace/trace_events_filter.c b/kernel/trace/trace_events_filter.c
@@ -679,7 +679,7 @@ find_event_field(struct ftrace_event_cal
 
 static int __alloc_pred_stack(struct pred_stack *stack, int n_preds)
 {
-	stack->preds = kzalloc(sizeof(*stack->preds)*(n_preds + 1), GFP_KERNEL);
+	stack->preds = kcalloc(n_preds + 1, sizeof(*stack->preds), GFP_KERNEL);
 	if (!stack->preds)
 		return -ENOMEM;
 	stack->index = n_preds;
@@ -820,8 +820,7 @@ static int __alloc_preds(struct event_fi
 	if (filter->preds)
 		__free_preds(filter);
 
-	filter->preds =
-		kzalloc(sizeof(*filter->preds) * n_preds, GFP_KERNEL);
+	filter->preds = kcalloc(n_preds, sizeof(*filter->preds), GFP_KERNEL);
 
 	if (!filter->preds)
 		return -ENOMEM;
@@ -1480,7 +1479,7 @@ static int fold_pred(struct filter_pred
 	children = count_leafs(preds, &preds[root->left]);
 	children += count_leafs(preds, &preds[root->right]);
 
-	root->ops = kzalloc(sizeof(*root->ops) * children, GFP_KERNEL);
+	root->ops = kcalloc(children, sizeof(*root->ops), GFP_KERNEL);
 	if (!root->ops)
 		return -ENOMEM;
 
kernel/trace/trace_syscalls.c 2011-11-28 20:09:23.100321500 +0100 renamed from kernel/trace/trace_syscalls.c 2011-11-13 11:08:12.124208310 +0100 +2 -2
diff -u -p a/kernel/trace/trace_syscalls.c b/kernel/trace/trace_syscalls.c
@@ -468,8 +468,8 @@ int __init init_ftrace_syscalls(void)
 	unsigned long addr;
 	int i;
 
-	syscalls_metadata = kzalloc(sizeof(*syscalls_metadata) *
-					NR_syscalls, GFP_KERNEL);
+	syscalls_metadata = kcalloc(NR_syscalls, sizeof(*syscalls_metadata),
+				    GFP_KERNEL);
 	if (!syscalls_metadata) {
 		WARN_ON(1);
 		return -ENOMEM;
#1 lore raw

[tip:perf/core] tracing/trivial: Use kcalloc instead of kzalloc to allocate array

Commit-ID:  47b0edcb599ea6eb9ef16d3a08932a0e01485293
Gitweb:     http://git.kernel.org/tip/47b0edcb599ea6eb9ef16d3a08932a0e01485293
Author:     Thomas Meyer <[email protected]>
AuthorDate: Tue, 29 Nov 2011 22:08:00 +0100
Committer:  Steven Rostedt <[email protected]>
CommitDate: Mon, 13 Feb 2012 13:48:11 -0500

tracing/trivial: Use kcalloc instead of kzalloc to allocate array

The advantage of kcalloc is, that will prevent integer overflows which could result from the multiplication of number of elements and size and it is also a bit nicer to read.

The semantic patch that makes this change is available in https://lkml.org/lkml/2011/11/25/107

---
 kernel/trace/ftrace.c              |    2 +-
 kernel/trace/trace_events_filter.c |    7 +++----
 kernel/trace/trace_syscalls.c      |    4 ++--
 3 files changed, 6 insertions(+), 7 deletions(-)
kernel/trace/ftrace.c +1 -1
index e2e0597..d1499e9 100644
@@ -1129,7 +1129,7 @@ static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
 		return NULL;
 
 	size = 1 << size_bits;
-	hash->buckets = kzalloc(sizeof(*hash->buckets) * size, GFP_KERNEL);
+	hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
 
 	if (!hash->buckets) {
 		kfree(hash);
kernel/trace/trace_events_filter.c +3 -4
index 24aee71..76afaee 100644
@@ -685,7 +685,7 @@ find_event_field(struct ftrace_event_call *call, char *name)
 
 static int __alloc_pred_stack(struct pred_stack *stack, int n_preds)
 {
-	stack->preds = kzalloc(sizeof(*stack->preds)*(n_preds + 1), GFP_KERNEL);
+	stack->preds = kcalloc(n_preds + 1, sizeof(*stack->preds), GFP_KERNEL);
 	if (!stack->preds)
 		return -ENOMEM;
 	stack->index = n_preds;
@@ -826,8 +826,7 @@ static int __alloc_preds(struct event_filter *filter, int n_preds)
 	if (filter->preds)
 		__free_preds(filter);
 
-	filter->preds =
-		kzalloc(sizeof(*filter->preds) * n_preds, GFP_KERNEL);
+	filter->preds = kcalloc(n_preds, sizeof(*filter->preds), GFP_KERNEL);
 
 	if (!filter->preds)
 		return -ENOMEM;
@@ -1486,7 +1485,7 @@ static int fold_pred(struct filter_pred *preds, struct filter_pred *root)
 	children = count_leafs(preds, &preds[root->left]);
 	children += count_leafs(preds, &preds[root->right]);
 
-	root->ops = kzalloc(sizeof(*root->ops) * children, GFP_KERNEL);
+	root->ops = kcalloc(children, sizeof(*root->ops), GFP_KERNEL);
 	if (!root->ops)
 		return -ENOMEM;
 
kernel/trace/trace_syscalls.c +2 -2
index cb65454..4350015 100644
@@ -468,8 +468,8 @@ int __init init_ftrace_syscalls(void)
 	unsigned long addr;
 	int i;
 
-	syscalls_metadata = kzalloc(sizeof(*syscalls_metadata) *
-					NR_syscalls, GFP_KERNEL);
+	syscalls_metadata = kcalloc(NR_syscalls, sizeof(*syscalls_metadata),
+				    GFP_KERNEL);
 	if (!syscalls_metadata) {
 		WARN_ON(1);
 		return -ENOMEM;