Re: [PATCH] IB/srp: Use struct_size() in kzalloc()

From: Bart Van Assche
Date: Tue Jan 08 2019 - 15:33:04 EST


On Tue, 2019-01-08 at 10:08 -0600, Gustavo A. R. Silva wrote:
+AD4 One of the more common cases of allocation size calculations is finding the
+AD4 size of a structure that has a zero-sized array at the end, along with memory
+AD4 for some number of elements for that array. For example:
+AD4
+AD4 struct foo +AHs
+AD4 int stuff+ADs
+AD4 void +ACo-entry+AFsAXQA7
+AD4 +AH0AOw
+AD4
+AD4 instance +AD0 kzalloc(sizeof(struct foo) +- sizeof(void +ACo) +ACo count, GFP+AF8-KERNEL)+ADs
+AD4
+AD4 Instead of leaving these open-coded and prone to type mistakes, we can now
+AD4 use the new struct+AF8-size() helper:
+AD4
+AD4 instance +AD0 kzalloc(struct+AF8-size(instance, entry, count), GFP+AF8-KERNEL)+ADs
+AD4
+AD4 This code was detected with the help of Coccinelle.
+AD4
+AD4 Signed-off-by: Gustavo A. R. Silva +ADw-gustavo+AEA-embeddedor.com+AD4
+AD4 ---
+AD4 drivers/infiniband/ulp/srp/ib+AF8-srp.c +AHw 3 +---
+AD4 1 file changed, 1 insertion(+-), 2 deletions(-)
+AD4
+AD4 diff --git a/drivers/infiniband/ulp/srp/ib+AF8-srp.c b/drivers/infiniband/ulp/srp/ib+AF8-srp.c
+AD4 index 31d91538bbf4..0f855294ff3f 100644
+AD4 --- a/drivers/infiniband/ulp/srp/ib+AF8-srp.c
+AD4 +-+-+- b/drivers/infiniband/ulp/srp/ib+AF8-srp.c
+AD4 +AEAAQA -443,8 +-443,7 +AEAAQA static struct srp+AF8-fr+AF8-pool +ACo-srp+AF8-create+AF8-fr+AF8-pool(struct ib+AF8-device +ACo-device,
+AD4 if (pool+AF8-size +ADwAPQ 0)
+AD4 goto err+ADs
+AD4 ret +AD0 -ENOMEM+ADs
+AD4 - pool +AD0 kzalloc(sizeof(struct srp+AF8-fr+AF8-pool) +-
+AD4 - pool+AF8-size +ACo sizeof(struct srp+AF8-fr+AF8-desc), GFP+AF8-KERNEL)+ADs
+AD4 +- pool +AD0 kzalloc(struct+AF8-size(pool, desc, pool+AF8-size), GFP+AF8-KERNEL)+ADs
+AD4 if (+ACE-pool)
+AD4 goto err+ADs
+AD4 pool-+AD4-size +AD0 pool+AF8-size+ADs

Reviewed-by: Bart Van Assche +ADw-bvanassche+AEA-acm.org+AD4-