Skip to contentSkip to conversation

auditsc: Use kzalloc instead of kmalloc+memset.

PATCH In review Rakib Mullick 2 participants 3 messages Apr 7, 2013 – Apr 9, 2013 Audit

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 auditsc: Use kzalloc instead of kmalloc+memset.

In function audit_alloc_context(), use kzalloc, instead of kmalloc+memset. Patch also renames audit_zero_context() to
audit_set_context(), to represent it's inner workings properly.

---

kernel/auditsc.c +4 -4
index a371f85..f5b6dc5 100644
@@ -1034,10 +1034,9 @@ static inline void audit_free_aux(struct audit_context *context)
 	}
 }
 
-static inline void audit_zero_context(struct audit_context *context,
+static inline void audit_set_context(struct audit_context *context,
 				      enum audit_state state)
 {
-	memset(context, 0, sizeof(*context));
 	context->state      = state;
 	context->prio = state == AUDIT_RECORD_CONTEXT ? ~0ULL : 0;
 }
@@ -1046,9 +1045,10 @@ static inline struct audit_context *audit_alloc_context(enum audit_state state)
 {
 	struct audit_context *context;
 
-	if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
+	context = kzalloc(sizeof(*context), GFP_KERNEL);
+	if (!context)
 		return NULL;
-	audit_zero_context(context, state);
+	audit_set_context(context, state);
 	INIT_LIST_HEAD(&context->killed_trees);
 	INIT_LIST_HEAD(&context->names_list);
 	return context;
#1 lore raw
Quoted 22 lines · On Sun, 07 Apr 2013 16:14:18 +0600 Rakib Mullick <[email protected]>…

In function audit_alloc_context(), use kzalloc, instead of kmalloc+memset. Patch also renames audit_zero_context() to
audit_set_context(), to represent it's inner workings properly.

...

kernel/auditsc.c +1 -2
@@ -1034,10 +1034,9 @@ static inline void audit_free_aux(struct audit_context *context)
 	}
 }
 
-static inline void audit_zero_context(struct audit_context *context,
+static inline void audit_set_context(struct audit_context *context,
 				      enum audit_state state)
 {
-	memset(context, 0, sizeof(*context));
 	context->state      = state;
 	context->prio = state == AUDIT_RECORD_CONTEXT ? ~0ULL : 0;
 }
@@ -1046,9 +1045,10 @@ static inline struct audit_context *audit_alloc_context(enum audit_state state)
 {
 	struct audit_context *context;

kernel/auditsc.c static inline struct audit_context *audit_alloc_context(enum audit_state state) · line 1054

Quoted 10 lines
kernel/auditsc.c +3 -2
 
-	if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
+	context = kzalloc(sizeof(*context), GFP_KERNEL);
+	if (!context)
 		return NULL;
-	audit_zero_context(context, state);
+	audit_set_context(context, state);
 	INIT_LIST_HEAD(&context->killed_trees);
 	INIT_LIST_HEAD(&context->names_list);
 	return context;

Fair enough. I'd go futher...

From: Andrew Morton <[email protected]>
Subject: auditsc-use-kzalloc-instead-of-kmallocmemset-fix

remove audit_set_context() altogether - fold it into its caller

Cc Al Viro <[email protected]>
Cc Eric Paris <[email protected]>
Cc Rakib Mullick <[email protected]>

---

 kernel/auditsc.c |   10 ++--------
 1 file changed, 2 insertions(+), 8 deletions(-)
kernel/auditsc.c renamed from kernel/auditsc.c~auditsc-use-kzalloc-instead-of-kmallocmemset-fix +2 -8
diff -puN kernel/auditsc.c~auditsc-use-kzalloc-instead-of-kmallocmemset-fix kernel/auditsc.c
@@ -1034,13 +1034,6 @@ static inline void audit_free_aux(struct
 	}
 }
 
-static inline void audit_set_context(struct audit_context *context,
-				      enum audit_state state)
-{
-	context->state      = state;
-	context->prio = state == AUDIT_RECORD_CONTEXT ? ~0ULL : 0;
-}
-
 static inline struct audit_context *audit_alloc_context(enum audit_state state)
 {
 	struct audit_context *context;
@@ -1048,7 +1041,8 @@ static inline struct audit_context *audi
 	context = kzalloc(sizeof(*context), GFP_KERNEL);
 	if (!context)
 		return NULL;
-	audit_set_context(context, state);
+	context->state = state;
+	context->prio = state == AUDIT_RECORD_CONTEXT ? ~0ULL : 0;
 	INIT_LIST_HEAD(&context->killed_trees);
 	INIT_LIST_HEAD(&context->names_list);
 	return context;

_

#2 lore raw
Quoted 44 lines · Andrew Morton

Fair enough. I'd go futher...

From: Andrew Morton <[email protected]>
Subject: auditsc-use-kzalloc-instead-of-kmallocmemset-fix

remove audit_set_context() altogether - fold it into its caller

Cc Al Viro <[email protected]>
Cc Eric Paris <[email protected]>
Cc Rakib Mullick <[email protected]>

---

 kernel/auditsc.c |   10 ++--------
 1 file changed, 2 insertions(+), 8 deletions(-)

diff -puN kernel/auditsc.c~auditsc-use-kzalloc-instead-of-kmallocmemset-fix kernel/auditsc.c

kernel/auditsc.c renamed from kernel/auditsc.c~auditsc-use-kzalloc-instead-of-kmallocmemset-fix +2 -8
@@ -1034,13 +1034,6 @@ static inline void audit_free_aux(struct
        }
 }
 
-static inline void audit_set_context(struct audit_context *context,
-                                     enum audit_state state)
-{
-       context->state      = state;
-       context->prio = state == AUDIT_RECORD_CONTEXT ? ~0ULL : 0;
-}
-
 static inline struct audit_context *audit_alloc_context(enum audit_state state)
 {
        struct audit_context *context;
@@ -1048,7 +1041,8 @@ static inline struct audit_context *audi
        context = kzalloc(sizeof(*context), GFP_KERNEL);
        if (!context)
                return NULL;
-       audit_set_context(context, state);
+       context->state = state;
+       context->prio = state == AUDIT_RECORD_CONTEXT ? ~0ULL : 0;
        INIT_LIST_HEAD(&context->killed_trees);
        INIT_LIST_HEAD(&context->names_list);
        return context;
Quoted 2 lines

_

Yes, this one is better than my patch and it's due to its diff stat.

Thanks,
Rakib.