kernel: kprobes: Use struct_size()
authorRuan Jinjie <ruanjinjie@huawei.com>
Tue, 25 Jul 2023 19:54:24 +0000 (19:54 +0000)
committerMasami Hiramatsu (Google) <mhiramat@kernel.org>
Wed, 23 Aug 2023 00:38:17 +0000 (09:38 +0900)
Use struct_size() instead of hand-writing it, when allocating a structure
with a flex array.

This is less verbose.

Link: https://lore.kernel.org/all/20230725195424.3469242-1-ruanjinjie@huawei.com/
Signed-off-by: Ruan Jinjie <ruanjinjie@huawei.com>
Acked-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
kernel/kprobes.c

index ca385b61d546722161f3c720fdf3198f4e9445d1..0c6185aefaef576c55fb324e6d30a8e2c0050d1f 100644 (file)
@@ -2232,8 +2232,7 @@ int register_kretprobe(struct kretprobe *rp)
                return -ENOMEM;
 
        for (i = 0; i < rp->maxactive; i++) {
-               inst = kzalloc(sizeof(struct kretprobe_instance) +
-                              rp->data_size, GFP_KERNEL);
+               inst = kzalloc(struct_size(inst, data, rp->data_size), GFP_KERNEL);
                if (inst == NULL) {
                        rethook_free(rp->rh);
                        rp->rh = NULL;
@@ -2256,8 +2255,7 @@ int register_kretprobe(struct kretprobe *rp)
 
        rp->rph->rp = rp;
        for (i = 0; i < rp->maxactive; i++) {
-               inst = kzalloc(sizeof(struct kretprobe_instance) +
-                              rp->data_size, GFP_KERNEL);
+               inst = kzalloc(struct_size(inst, data, rp->data_size), GFP_KERNEL);
                if (inst == NULL) {
                        refcount_set(&rp->rph->ref, i);
                        free_rp_inst(rp);