Skip to contentSkip to conversation

fbdev: Adjustments for fb_alloc_cmap_gfp()

PATCH In review Markus Elfring 2 participants 5 messages May 23, 2023 – May 25, 2023

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

cover fbdev: Adjustments for fb_alloc_cmap_gfp()

From: Markus Elfring <[email protected]>
Date: Tue, 23 May 2023 22:04:33 +0200

A few update suggestions were taken into account
from static source code analysis.

Markus Elfring (2):
Move two variable assignments
Convert a variable initialisation into a later assignment

 drivers/video/fbdev/core/fbcmap.c | 22 ++++++++++++++--------
 1 file changed, 14 insertions(+), 8 deletions(-)

--
2.40.1

#1 lore raw

PATCH 1/2 fbdev: Move two variable assignments in fb_alloc_cmap_gfp()

From: Markus Elfring <[email protected]>
Date: Tue, 23 May 2023 21:30:29 +0200

Move the assignment for the local variables “size” and “flags” because the computed values were only used in a single if branch.

---
 drivers/video/fbdev/core/fbcmap.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/video/fbdev/core/fbcmap.c +4 -3
index ff09e57f3c38..5c1075ed28ab 100644
@@ -91,16 +91,17 @@ static const struct fb_cmap default_16_colors = {
 
 int fb_alloc_cmap_gfp(struct fb_cmap *cmap, int len, int transp, gfp_t flags)
 {
-	int size = len * sizeof(u16);
 	int ret = -ENOMEM;
 
-	flags |= __GFP_NOWARN;
-
 	if (cmap->len != len) {
+		int size;
+
 		fb_dealloc_cmap(cmap);
 		if (!len)
 			return 0;
 
+		size = len * sizeof(u16);
+		flags |= __GFP_NOWARN;
 		cmap->red = kzalloc(size, flags);
 		if (!cmap->red)
 			goto fail;

--
2.40.1

maintainer
#2 lore raw

Re: [PATCH 1/2] fbdev: Move two variable assignments in fb_alloc_cmap_gfp()

Quoted 5 lines · Markus Elfring

From: Markus Elfring <[email protected]>
Date: Tue, 23 May 2023 21:30:29 +0200

Move the assignment for the local variables “size” and “flags” because the computed values were only used in a single if branch.

Please do not move such variables without real need.
It makes backporting (of this and maybe follow-up patches) much more complicated and the compiler will optimize it anyway.

Thanks!
Helge

Quoted 33 lines
---
  drivers/video/fbdev/core/fbcmap.c | 7 ++++---
  1 file changed, 4 insertions(+), 3 deletions(-)
drivers/video/fbdev/core/fbcmap.c +4 -4
index ff09e57f3c38..5c1075ed28ab 100644
@@ -91,16 +91,17 @@ static const struct fb_cmap default_16_colors = {
 
  int fb_alloc_cmap_gfp(struct fb_cmap *cmap, int len, int transp, gfp_t flags)
  {
-	int size = len * sizeof(u16);
  	int ret = -ENOMEM;
 
-	flags |= __GFP_NOWARN;
-
  	if (cmap->len != len) {
+		int size;
+
  		fb_dealloc_cmap(cmap);
  		if (!len)
  			return 0;
 
+		size = len * sizeof(u16);
+		flags |= __GFP_NOWARN;
  		cmap->red = kzalloc(size, flags);
  		if (!cmap->red)
  			goto fail;
--

2.40.1

#3 lore raw

Re: [cocci] [PATCH 1/2] fbdev: Move two variable assignments in fb_alloc_cmap_gfp()

Quoted 4 lines
Quoted 2 lines

Move the assignment for the local variables “size” and “flags” because the computed values were only used in a single if branch.

Please do not move such variables without real need.

Is there a need to explain desirable effects better?

Quoted 1 line

It makes backporting (of this and maybe follow-up patches) much more complicated

I suggest to reconsider such development concerns a bit more.
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/log/drivers/video/fbdev/core/fbcmap.c?h=v6.4-rc3

Quoted 1 line

and the compiler will optimize it anyway.

How much do expectations fit to supported and documented software optimisations?

Regards,
Markus

#4 lore raw

PATCH 2/2 fbdev: Convert a variable initialisation into a later assignment in fb_alloc_cmap_gfp()

In reply to Markus Elfring

From: Markus Elfring <[email protected]>
Date: Tue, 23 May 2023 21:56:55 +0200

* Assign the value “-ENOMEM” to the local variable “ret” only for the exception handling.

* Use an additional label.

---
 drivers/video/fbdev/core/fbcmap.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)
drivers/video/fbdev/core/fbcmap.c +10 -5
index 5c1075ed28ab..fbe82a64cf73 100644
@@ -91,7 +91,7 @@ static const struct fb_cmap default_16_colors = {
 
 int fb_alloc_cmap_gfp(struct fb_cmap *cmap, int len, int transp, gfp_t flags)
 {
-	int ret = -ENOMEM;
+	int ret;
 
 	if (cmap->len != len) {
 		int size;
@@ -104,17 +104,20 @@ int fb_alloc_cmap_gfp(struct fb_cmap *cmap, int len, int transp, gfp_t flags)
 		flags |= __GFP_NOWARN;
 		cmap->red = kzalloc(size, flags);
 		if (!cmap->red)
-			goto fail;
+			goto e_nomem;
+
 		cmap->green = kzalloc(size, flags);
 		if (!cmap->green)
-			goto fail;
+			goto e_nomem;
+
 		cmap->blue = kzalloc(size, flags);
 		if (!cmap->blue)
-			goto fail;
+			goto e_nomem;
+
 		if (transp) {
 			cmap->transp = kzalloc(size, flags);
 			if (!cmap->transp)
-				goto fail;
+				goto e_nomem;
 		} else {
 			cmap->transp = NULL;
 		}
@@ -126,6 +129,8 @@ int fb_alloc_cmap_gfp(struct fb_cmap *cmap, int len, int transp, gfp_t flags)
 		goto fail;
 	return 0;
 
+e_nomem:
+	ret = -ENOMEM;
 fail:
 	fb_dealloc_cmap(cmap);
 	return ret;

--
2.40.1