Skip to contentSkip to conversation

Staging: media: atomisp: Use kmalloc_array instead of kmalloc

PATCH Superseded Georgiana Chelu 2 participants 6 messages Sep 23, 2017 – Sep 25, 2017

Subsystems (4)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Staging: media: atomisp: Use kmalloc_array instead of kmalloc

Prefer kmalloc_array over kmalloc with multiply
because kmalloc_array performs additional checks
before memory allocation.

Fix the following issues reported by :
* WARNING: Prefer kmalloc_array over kmalloc with multiply
* CHECK: Alignment should match open parenthesis

---
 drivers/staging/media/atomisp/pci/atomisp2/hmm/hmm_bo.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/staging/media/atomisp/pci/atomisp2/hmm/hmm_bo.c +3 -3
index 11162f595fc7..22dbd79fe123 100644
@@ -725,8 +725,8 @@ static int alloc_private_pages(struct hmm_buffer_object *bo,
 
 	pgnr = bo->pgnr;
 
-	bo->page_obj = kmalloc(sizeof(struct hmm_page_object) * pgnr,
-				GFP_KERNEL);
+	bo->page_obj = kmalloc_array(pgnr, sizeof(struct hmm_page_object),
+				     GFP_KERNEL);
 	if (unlikely(!bo->page_obj)) {
 		dev_err(atomisp_dev, "out of memory for bo->page_obj\n");
 		return -ENOMEM;
@@ -990,7 +990,7 @@ static int alloc_user_pages(struct hmm_buffer_object *bo,
 	struct vm_area_struct *vma;
 	struct page **pages;
 
-	pages = kmalloc(sizeof(struct page *) * bo->pgnr, GFP_KERNEL);
+	pages = kmalloc_array(bo->pgnr, sizeof(struct page *), GFP_KERNEL);
 	if (unlikely(!pages)) {
 		dev_err(atomisp_dev, "out of memory for pages...\n");
 		return -ENOMEM;
Signature · 1 line
2.11.0
#1 lore raw
Quoted 19 lines · Georgiana Chelu

Prefer kmalloc_array over kmalloc with multiply
because kmalloc_array performs additional checks
before memory allocation.

Fix the following issues reported by :
* WARNING: Prefer kmalloc_array over kmalloc with multiply
* CHECK: Alignment should match open parenthesis

---
 drivers/staging/media/atomisp/pci/atomisp2/hmm/hmm_bo.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/staging/media/atomisp/pci/atomisp2/hmm/hmm_bo.c +0 -0
index 11162f595fc7..22dbd79fe123 100644

drivers/staging/media/atomisp/pci/atomisp2/hmm/hmm_bo.c static int alloc_private_pages(struct hmm_buffer_object *bo, · line 731

Quoted 9 lines
drivers/staging/media/atomisp/pci/atomisp2/hmm/hmm_bo.c +2 -2
@@ -725,8 +725,8 @@ static int alloc_private_pages(struct hmm_buffer_object *bo,
 
 	pgnr = bo->pgnr;
 
-	bo->page_obj = kmalloc(sizeof(struct hmm_page_object) * pgnr,
-				GFP_KERNEL);
+	bo->page_obj = kmalloc_array(pgnr, sizeof(struct hmm_page_object),
+				     GFP_KERNEL);
 	if (unlikely(!bo->page_obj)) {
 		dev_err(atomisp_dev, "out of memory for bo->page_obj\n");

If you want to make a series on this code, the error message here and in the next case are not necessary - kmalloc will generate a backtrace on failure that will make the same information available. Alse, the unlikely's do not look necessary. The compiler can figure it out for itself.

drivers/staging/media/atomisp/pci/atomisp2/hmm/hmm_bo.c static int alloc_user_pages(struct hmm_buffer_object *bo, · line 996

Quoted 10 lines
drivers/staging/media/atomisp/pci/atomisp2/hmm/hmm_bo.c +1 -1
 		return -ENOMEM;
@@ -990,7 +990,7 @@ static int alloc_user_pages(struct hmm_buffer_object *bo,
 	struct vm_area_struct *vma;
 	struct page **pages;
 
-	pages = kmalloc(sizeof(struct page *) * bo->pgnr, GFP_KERNEL);
+	pages = kmalloc_array(bo->pgnr, sizeof(struct page *), GFP_KERNEL);
 	if (unlikely(!pages)) {
 		dev_err(atomisp_dev, "out of memory for pages...\n");
 		return -ENOMEM;

It looks like you have missed a call just under this one?

julia

Quoted 10 lines

--
2.11.0

--
You received this message because you are subscribed to the Google Groups "outreachy-kernel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected].
To view this discussion on the web visit https://groups.google.com/d/msgid/outreachy-kernel/20170923232419.21625-1-georgiana.chelu93%40gmail.com.
For more options, visit https://groups.google.com/d/optout.

#2 lore raw
Quoted 26 lines · Julia Lawall
Quoted 22 lines · Georgiana Chelu

Prefer kmalloc_array over kmalloc with multiply
because kmalloc_array performs additional checks
before memory allocation.

Fix the following issues reported by :
* WARNING: Prefer kmalloc_array over kmalloc with multiply
* CHECK: Alignment should match open parenthesis

---
 drivers/staging/media/atomisp/pci/atomisp2/hmm/hmm_bo.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/staging/media/atomisp/pci/atomisp2/hmm/hmm_bo.c +0 -0
index 11162f595fc7..22dbd79fe123 100644
@@ -725,8 +725,8 @@ static int alloc_private_pages(struct hmm_buffer_object *bo,
 
      pgnr = bo->pgnr;
 
Quoted 13 lines

drivers/staging/media/atomisp/pci/atomisp2/hmm/hmm_bo.c static int alloc_private_pages(struct hmm_buffer_object *bo, · line 731

Quoted 6 lines
drivers/staging/media/atomisp/pci/atomisp2/hmm/hmm_bo.c +2 -2
-     bo->page_obj = kmalloc(sizeof(struct hmm_page_object) * pgnr,
-                             GFP_KERNEL);
+     bo->page_obj = kmalloc_array(pgnr, sizeof(struct hmm_page_object),
+                                  GFP_KERNEL);
      if (unlikely(!bo->page_obj)) {
              dev_err(atomisp_dev, "out of memory for bo->page_obj\n");

If you want to make a series on this code, the error message here and in the next case are not necessary - kmalloc will generate a backtrace on failure that will make the same information available. Alse, the unlikely's do not look necessary. The compiler can figure it out for itself.

Hi Julia,

I have done some research regarding kmalloc and compiler optimization. Here is my understanding of the change you suggested to me.

The memory allocation in kmalloc function is done using the Slab Allocator and it is the allocator job to improve the branch prediction with the likely/unlikely macros.
Also, it's safe to do the prediction on a lower level because any mistake will cost
more CPU cycles.

Please, tell me if it is correct. Thank you!

I will resend this patch as a patch set. Should I change the version?

Thanks,
Georgiana

Quoted 5 lines
Quoted 5 lines
drivers/staging/media/atomisp/pci/atomisp2/hmm/hmm_bo.c +0 -0
              return -ENOMEM;
@@ -990,7 +990,7 @@ static int alloc_user_pages(struct hmm_buffer_object *bo,
      struct vm_area_struct *vma;
      struct page **pages;
 
Quoted 7 lines

drivers/staging/media/atomisp/pci/atomisp2/hmm/hmm_bo.c static int alloc_user_pages(struct hmm_buffer_object *bo, · line 996

Quoted 5 lines
drivers/staging/media/atomisp/pci/atomisp2/hmm/hmm_bo.c +1 -1
-     pages = kmalloc(sizeof(struct page *) * bo->pgnr, GFP_KERNEL);
+     pages = kmalloc_array(bo->pgnr, sizeof(struct page *), GFP_KERNEL);
      if (unlikely(!pages)) {
              dev_err(atomisp_dev, "out of memory for pages...\n");
              return -ENOMEM;

It looks like you have missed a call just under this one?

I will resend it as a patch set with a

Quoted 13 lines

julia

Quoted 10 lines

--
2.11.0

--
You received this message because you are subscribed to the Google Groups "outreachy-kernel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected].
To view this discussion on the web visit https://groups.google.com/d/msgid/outreachy-kernel/20170923232419.21625-1-georgiana.chelu93%40gmail.com.
For more options, visit https://groups.google.com/d/optout.

#3 lore raw
Quoted 41 lines · Georgiana Chelu
Quoted 26 lines · Julia Lawall
Quoted 22 lines · Georgiana Chelu

Prefer kmalloc_array over kmalloc with multiply
because kmalloc_array performs additional checks
before memory allocation.

Fix the following issues reported by :
* WARNING: Prefer kmalloc_array over kmalloc with multiply
* CHECK: Alignment should match open parenthesis

---
 drivers/staging/media/atomisp/pci/atomisp2/hmm/hmm_bo.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/staging/media/atomisp/pci/atomisp2/hmm/hmm_bo.c +0 -0
index 11162f595fc7..22dbd79fe123 100644
@@ -725,8 +725,8 @@ static int alloc_private_pages(struct hmm_buffer_object *bo,
 
      pgnr = bo->pgnr;
 
Quoted 13 lines

drivers/staging/media/atomisp/pci/atomisp2/hmm/hmm_bo.c static int alloc_private_pages(struct hmm_buffer_object *bo, · line 731

Quoted 6 lines
drivers/staging/media/atomisp/pci/atomisp2/hmm/hmm_bo.c +2 -2
-     bo->page_obj = kmalloc(sizeof(struct hmm_page_object) * pgnr,
-                             GFP_KERNEL);
+     bo->page_obj = kmalloc_array(pgnr, sizeof(struct hmm_page_object),
+                                  GFP_KERNEL);
      if (unlikely(!bo->page_obj)) {
              dev_err(atomisp_dev, "out of memory for bo->page_obj\n");

If you want to make a series on this code, the error message here and in the next case are not necessary - kmalloc will generate a backtrace on failure that will make the same information available. Alse, the unlikely's do not look necessary. The compiler can figure it out for itself.

Quoted 8 lines

Hi Julia,

I have done some research regarding kmalloc and compiler optimization. Here is my understanding of the change you suggested to me.

The memory allocation in kmalloc function is done using the Slab Allocator and it is the allocator job to improve the branch prediction with the likely/unlikely macros.

I'm puzzled by the latter point. I don't think that the allocator would affect the branch prediction at the call site. But it is a pretty standard strategy for a compiler to realize that a small if in the middle of a function that ends in a immediate return is not likely to be taken.

julia

Quoted 42 lines

Also, it's safe to do the prediction on a lower level because any mistake will cost
more CPU cycles.

Please, tell me if it is correct. Thank you!

I will resend this patch as a patch set. Should I change the version?

Thanks,
Georgiana

Quoted 5 lines
Quoted 5 lines
drivers/staging/media/atomisp/pci/atomisp2/hmm/hmm_bo.c +0 -0
              return -ENOMEM;
@@ -990,7 +990,7 @@ static int alloc_user_pages(struct hmm_buffer_object *bo,
      struct vm_area_struct *vma;
      struct page **pages;
 
Quoted 7 lines

drivers/staging/media/atomisp/pci/atomisp2/hmm/hmm_bo.c static int alloc_user_pages(struct hmm_buffer_object *bo, · line 996

Quoted 5 lines
drivers/staging/media/atomisp/pci/atomisp2/hmm/hmm_bo.c +1 -1
-     pages = kmalloc(sizeof(struct page *) * bo->pgnr, GFP_KERNEL);
+     pages = kmalloc_array(bo->pgnr, sizeof(struct page *), GFP_KERNEL);
      if (unlikely(!pages)) {
              dev_err(atomisp_dev, "out of memory for pages...\n");
              return -ENOMEM;

It looks like you have missed a call just under this one?

I will resend it as a patch set with a

Quoted 13 lines

julia

Quoted 10 lines

--
2.11.0

--
You received this message because you are subscribed to the Google Groups "outreachy-kernel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected].
To view this discussion on the web visit https://groups.google.com/d/msgid/outreachy-kernel/20170923232419.21625-1-georgiana.chelu93%40gmail.com.
For more options, visit https://groups.google.com/d/optout.

#4 lore raw
In reply to Georgiana Chelu
Quoted 53 lines · Georgiana Chelu
Quoted 26 lines · Julia Lawall
Quoted 22 lines · Georgiana Chelu

Prefer kmalloc_array over kmalloc with multiply
because kmalloc_array performs additional checks
before memory allocation.

Fix the following issues reported by :
* WARNING: Prefer kmalloc_array over kmalloc with multiply
* CHECK: Alignment should match open parenthesis

---
 drivers/staging/media/atomisp/pci/atomisp2/hmm/hmm_bo.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/staging/media/atomisp/pci/atomisp2/hmm/hmm_bo.c +0 -0
index 11162f595fc7..22dbd79fe123 100644
@@ -725,8 +725,8 @@ static int alloc_private_pages(struct hmm_buffer_object *bo,
 
      pgnr = bo->pgnr;
 
Quoted 13 lines

drivers/staging/media/atomisp/pci/atomisp2/hmm/hmm_bo.c static int alloc_private_pages(struct hmm_buffer_object *bo, · line 731

Quoted 6 lines
drivers/staging/media/atomisp/pci/atomisp2/hmm/hmm_bo.c +2 -2
-     bo->page_obj = kmalloc(sizeof(struct hmm_page_object) * pgnr,
-                             GFP_KERNEL);
+     bo->page_obj = kmalloc_array(pgnr, sizeof(struct hmm_page_object),
+                                  GFP_KERNEL);
      if (unlikely(!bo->page_obj)) {
              dev_err(atomisp_dev, "out of memory for bo->page_obj\n");

If you want to make a series on this code, the error message here and in the next case are not necessary - kmalloc will generate a backtrace on failure that will make the same information available. Alse, the unlikely's do not look necessary. The compiler can figure it out for itself.

Hi Julia,

I have done some research regarding kmalloc and compiler optimization. Here is my understanding of the change you suggested to me.

The memory allocation in kmalloc function is done using the Slab Allocator and it is the allocator job to improve the branch prediction with the likely/unlikely macros.
Also, it's safe to do the prediction on a lower level because any mistake will cost
more CPU cycles.

Quoted 4 lines

Please, tell me if it is correct. Thank you!

I will resend this patch as a patch set. Should I change the version?

I think v2 would be good. State explicitly that you enlarged a single patch into a series, so it is clear what is going on. Otherwise, Greg will see the series version and the standalone version and wonder what is going on.

julia

Quoted 41 lines

Thanks,
Georgiana

Quoted 5 lines
Quoted 5 lines
drivers/staging/media/atomisp/pci/atomisp2/hmm/hmm_bo.c +0 -0
              return -ENOMEM;
@@ -990,7 +990,7 @@ static int alloc_user_pages(struct hmm_buffer_object *bo,
      struct vm_area_struct *vma;
      struct page **pages;
 
Quoted 7 lines

drivers/staging/media/atomisp/pci/atomisp2/hmm/hmm_bo.c static int alloc_user_pages(struct hmm_buffer_object *bo, · line 996

Quoted 5 lines
drivers/staging/media/atomisp/pci/atomisp2/hmm/hmm_bo.c +1 -1
-     pages = kmalloc(sizeof(struct page *) * bo->pgnr, GFP_KERNEL);
+     pages = kmalloc_array(bo->pgnr, sizeof(struct page *), GFP_KERNEL);
      if (unlikely(!pages)) {
              dev_err(atomisp_dev, "out of memory for pages...\n");
              return -ENOMEM;

It looks like you have missed a call just under this one?

I will resend it as a patch set with a

Quoted 13 lines

julia

Quoted 10 lines

--
2.11.0

--
You received this message because you are subscribed to the Google Groups "outreachy-kernel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected].
To view this discussion on the web visit https://groups.google.com/d/msgid/outreachy-kernel/20170923232419.21625-1-georgiana.chelu93%40gmail.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "outreachy-kernel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit https://groups.google.com/d/msgid/outreachy-kernel/CALta04w00_XEDmGfUfFa%3DSq3JsObV6eJcQ-sCS-zG%2BuFEwbu3g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

#5 lore raw
Quoted 62 lines · Julia Lawall
Quoted 53 lines · Georgiana Chelu
Quoted 26 lines · Julia Lawall
Quoted 22 lines · Georgiana Chelu

Prefer kmalloc_array over kmalloc with multiply
because kmalloc_array performs additional checks
before memory allocation.

Fix the following issues reported by :
* WARNING: Prefer kmalloc_array over kmalloc with multiply
* CHECK: Alignment should match open parenthesis

---
 drivers/staging/media/atomisp/pci/atomisp2/hmm/hmm_bo.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/staging/media/atomisp/pci/atomisp2/hmm/hmm_bo.c +0 -0
index 11162f595fc7..22dbd79fe123 100644
@@ -725,8 +725,8 @@ static int alloc_private_pages(struct hmm_buffer_object *bo,
 
      pgnr = bo->pgnr;
 
Quoted 13 lines

drivers/staging/media/atomisp/pci/atomisp2/hmm/hmm_bo.c static int alloc_private_pages(struct hmm_buffer_object *bo, · line 731

Quoted 6 lines
drivers/staging/media/atomisp/pci/atomisp2/hmm/hmm_bo.c +2 -2
-     bo->page_obj = kmalloc(sizeof(struct hmm_page_object) * pgnr,
-                             GFP_KERNEL);
+     bo->page_obj = kmalloc_array(pgnr, sizeof(struct hmm_page_object),
+                                  GFP_KERNEL);
      if (unlikely(!bo->page_obj)) {
              dev_err(atomisp_dev, "out of memory for bo->page_obj\n");

If you want to make a series on this code, the error message here and in the next case are not necessary - kmalloc will generate a backtrace on failure that will make the same information available. Alse, the unlikely's do not look necessary. The compiler can figure it out for itself.

Hi Julia,

I have done some research regarding kmalloc and compiler optimization. Here is my understanding of the change you suggested to me.

The memory allocation in kmalloc function is done using the Slab Allocator and it is the allocator job to improve the branch prediction with the likely/unlikely macros.
Also, it's safe to do the prediction on a lower level because any mistake will cost
more CPU cycles.

Quoted 4 lines

Please, tell me if it is correct. Thank you!

I will resend this patch as a patch set. Should I change the version?

Quoted 6 lines

I think v2 would be good. State explicitly that you enlarged a single patch into a series, so it is clear what is going on. Otherwise, Greg will see the series version and the standalone version and wonder what is going on.

julia

I intend to fix more warnings from this file. So, I will include more patches in the second version of the patch set. In this way, Greg will know the order to apply them.

Thanks,
Georgiana

Quoted 42 lines
Quoted 42 lines

Thanks,
Georgiana

Quoted 5 lines
Quoted 5 lines
drivers/staging/media/atomisp/pci/atomisp2/hmm/hmm_bo.c +0 -0
              return -ENOMEM;
@@ -990,7 +990,7 @@ static int alloc_user_pages(struct hmm_buffer_object *bo,
      struct vm_area_struct *vma;
      struct page **pages;
 
Quoted 7 lines

drivers/staging/media/atomisp/pci/atomisp2/hmm/hmm_bo.c static int alloc_user_pages(struct hmm_buffer_object *bo, · line 996

Quoted 5 lines
drivers/staging/media/atomisp/pci/atomisp2/hmm/hmm_bo.c +1 -1
-     pages = kmalloc(sizeof(struct page *) * bo->pgnr, GFP_KERNEL);
+     pages = kmalloc_array(bo->pgnr, sizeof(struct page *), GFP_KERNEL);
      if (unlikely(!pages)) {
              dev_err(atomisp_dev, "out of memory for pages...\n");
              return -ENOMEM;

It looks like you have missed a call just under this one?

I will resend it as a patch set with a

Quoted 13 lines

julia

Quoted 10 lines

--
2.11.0

--
You received this message because you are subscribed to the Google Groups "outreachy-kernel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected].
To view this discussion on the web visit https://groups.google.com/d/msgid/outreachy-kernel/20170923232419.21625-1-georgiana.chelu93%40gmail.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "outreachy-kernel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit https://groups.google.com/d/msgid/outreachy-kernel/CALta04w00_XEDmGfUfFa%3DSq3JsObV6eJcQ-sCS-zG%2BuFEwbu3g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.