Skip to contentSkip to conversation

hugetlb: Fix an error code in hugetlb_reserve_pages()

PATCH In review Dan Carpenter 5 participants 6 messages Dec 11, 2020 – Dec 14, 2020

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

PATCH 1/1 hugetlb: Fix an error code in hugetlb_reserve_pages()

Preserve the error code from region_add() instead of returning success.

Fixes 0db9d74ed884 ("hugetlb: disable region_add file_region coalescing")

---
From static analysis. Untested.

 mm/hugetlb.c | 1 +
 1 file changed, 1 insertion(+)
mm/hugetlb.c +1 -0
index 1f3bf1710b66..ac2e48b9f1d7 100644
@@ -5113,6 +5113,7 @@ int hugetlb_reserve_pages(struct inode *inode,
 
 		if (unlikely(add < 0)) {
 			hugetlb_acct_memory(h, -gbl_reserve);
+			ret = add;
 			goto out_put_pages;
 		} else if (unlikely(chg > add)) {
 			/*
Signature · 1 line
2.29.2
#1 lore raw

Hi Dan,

Quoted 16 lines · Dan Carpenter

Preserve the error code from region_add() instead of returning success.

Fixes 0db9d74ed884 ("hugetlb: disable region_add file_region coalescing")

---
From static analysis. Untested.

 mm/hugetlb.c | 1 +
 1 file changed, 1 insertion(+)
mm/hugetlb.c +0 -0
index 1f3bf1710b66..ac2e48b9f1d7 100644

mm/hugetlb.c int hugetlb_reserve_pages(struct inode *inode, · line 5116

Quoted 4 lines
mm/hugetlb.c +1 -0
@@ -5113,6 +5113,7 @@ int hugetlb_reserve_pages(struct inode *inode,
 
                if (unlikely(add < 0)) {
                        hugetlb_acct_memory(h, -gbl_reserve);
+                       ret = add;

This function returns int but ret is long type.
Does it need correction ?

Quoted 7 lines
mm/hugetlb.c +0 -1
                        goto out_put_pages;
                } else if (unlikely(chg > add)) {
                        /*
--

2.29.2

#2 lore raw
Quoted 8 lines · Souptick Joarder

mm/hugetlb.c int hugetlb_reserve_pages(struct inode *inode, · line 5116

Quoted 5 lines
mm/hugetlb.c +1 -0
@@ -5113,6 +5113,7 @@ int hugetlb_reserve_pages(struct inode *inode,
 
                if (unlikely(add < 0)) {
                        hugetlb_acct_memory(h, -gbl_reserve);
+                       ret = add;

This function returns int but ret is long type.
Does it need correction ?

I wouold say "no", because 'ret' isn't returned _by_ this function (*), its purpose is to capture the return value from other functions.

(*) OK, it is, if ret < 0. But ret < 0 really means "Is this an error number", which can be perfectly well represented in an int, short or long. char is too small ;-)

So the range of values which will be placed in 'ret' is (-4096-LONG_MAX]

#3 lore raw
In reply to Souptick Joarder
Quoted 20 lines · Souptick Joarder

Hi Dan,

Quoted 17 lines · Dan Carpenter

Preserve the error code from region_add() instead of returning success.

Fixes 0db9d74ed884 ("hugetlb: disable region_add file_region coalescing")

---
From static analysis. Untested.

 mm/hugetlb.c | 1 +
 1 file changed, 1 insertion(+)
mm/hugetlb.c +0 -0
index 1f3bf1710b66..ac2e48b9f1d7 100644
@@ -5113,6 +5113,7 @@ int hugetlb_reserve_pages(struct inode *inode,
 
Quoted 7 lines

mm/hugetlb.c int hugetlb_reserve_pages(struct inode *inode, · line 5116

Quoted 3 lines
mm/hugetlb.c +1 -0
                if (unlikely(add < 0)) {
                        hugetlb_acct_memory(h, -gbl_reserve);
+                       ret = add;

This function returns int but ret is long type.
Does it need correction ?

It doesn't *need* correction. The code works fine as-is. Smatch parses it correctly, also. Hard to say if making it "ret" makes the code simpler or more complicated for a human reader.

regards,
dan carpenter

Reviewed-by on v1 1/1
#4 lore raw
In reply to Dan Carpenter
Quoted 16 lines · Dan Carpenter

Preserve the error code from region_add() instead of returning success.

Fixes 0db9d74ed884 ("hugetlb: disable region_add file_region coalescing")

---
From static analysis. Untested.

 mm/hugetlb.c | 1 +
 1 file changed, 1 insertion(+)
mm/hugetlb.c +0 -0
index 1f3bf1710b66..ac2e48b9f1d7 100644

mm/hugetlb.c int hugetlb_reserve_pages(struct inode *inode, · line 5119

Quoted 7 lines
mm/hugetlb.c +1 -0
@@ -5113,6 +5113,7 @@ int hugetlb_reserve_pages(struct inode *inode,
 
 		if (unlikely(add < 0)) {
 			hugetlb_acct_memory(h, -gbl_reserve);
+			ret = add;
 			goto out_put_pages;
 		} else if (unlikely(chg > add)) {
 			/*

Was wioing if something like

return ret ? ret : add;

would be cleaner, but then I spotted "chg" ...

Signature · 3 lines
Thanks,

David / dhildenb
Reviewed-by on v1 1/1
#5 lore raw
In reply to Dan Carpenter
Quoted 6 lines · Dan Carpenter

Preserve the error code from region_add() instead of returning success.

Fixes 0db9d74ed884 ("hugetlb: disable region_add file_region coalescing")

---
From static analysis. Untested.

Thanks Dan.

Quoted 10 lines
 mm/hugetlb.c | 1 +
 1 file changed, 1 insertion(+)
mm/hugetlb.c +0 -0
index 1f3bf1710b66..ac2e48b9f1d7 100644

mm/hugetlb.c int hugetlb_reserve_pages(struct inode *inode, · line 5119

Quoted 7 lines
mm/hugetlb.c +1 -0
@@ -5113,6 +5113,7 @@ int hugetlb_reserve_pages(struct inode *inode,
 
 		if (unlikely(add < 0)) {
 			hugetlb_acct_memory(h, -gbl_reserve);
+			ret = add;
 			goto out_put_pages;
 		} else if (unlikely(chg > add)) {
 			/*

That error path is VERY unlikely to be taken, but is indeed incorrect.

When looking at this, I noticed that callers of hugetlb_reserve_pages only check for 0 or !0. This changed as the code evolved to add reservation cgroup support. The routine type can be changed to a bool and simplified some. I'll send that as a follow up patch not for stable.

Signature · 1 line
Mike Kravetz