Skip to contentSkip to conversation

zsmalloc: use U suffix for negative literals being shifted

PATCH Superseded Nick Desaulniers 5 participants 10 messages Dec 24, 2017 – Jan 11, 2018

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

PATCH 1/1 zsmalloc: use U suffix for negative literals being shifted

Fixes warnings about shifting unsigned literals being undefined behavior.

---
 mm/zsmalloc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/zsmalloc.c +1 -1
index 685049a..5d31458 100644
@@ -1056,7 +1056,7 @@ static void init_zspage(struct size_class *class, struct zspage *zspage)
 			 * Reset OBJ_TAG_BITS bit to last link to tell
 			 * whether it's allocated object or not.
 			 */
-			link->next = -1 << OBJ_TAG_BITS;
+			link->next = -1U << OBJ_TAG_BITS;
 		}
 		kunmap_atomic(vaddr);
 		page = next_page;
Signature · 7 lines
2.7.4

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#1 lore raw
Quoted 2 lines · Nick Desaulniers

Fixes warnings about shifting unsigned literals being undefined behavior.

Do you mean signed literals?

mm/zsmalloc.c static void init_zspage(struct size_class *class, struct zspage *zspage) · line 1060

Quoted 4 lines
mm/zsmalloc.c +1 -1
 			 */
-			link->next = -1 << OBJ_TAG_BITS;
+			link->next = -1U << OBJ_TAG_BITS;
 		}

I don't understand what -1U means. Seems like a contradiction in terms, a negative unsigned number. Is this supposed to be ~0U?

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#2 lore raw
Quoted 5 lines · Matthew Wilcox
Quoted 2 lines · Nick Desaulniers

Fixes warnings about shifting unsigned literals being undefined behavior.

Do you mean signed literals?

A sorry, s/unsigned/negative signed/g. The warning is:

mm/zsmalloc.c:1059:20: warning: shifting a negative signed value is undefined

      [-Wshift-negative-value]
                        link->next = -1 << OBJ_TAG_BITS;
                                     ~~ ^
Quoted 8 lines

mm/zsmalloc.c static void init_zspage(struct size_class *class, struct zspage *zspage) · line 1060

Quoted 5 lines
                       */
mm/zsmalloc.c +1 -1
-                     link->next = -1 << OBJ_TAG_BITS;
+                     link->next = -1U << OBJ_TAG_BITS;
              }

I don't understand what -1U means. Seems like a contradiction in terms, a negative unsigned number. Is this supposed to be ~0U?

$ ag \\-1U[^L]

The code base is full of that literal. I think of it as:

(unsigned) -1

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#3 lore raw

Hello,
What are the next steps for this patch?

Note: the MAINTAINERS file does not contain a T: (tree) entry for ZSMALLOC, so I can't check to see if this has already been merged or not. Unless you work out of the mm tree?

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

maintainer
#4 lore raw
In reply to Nick Desaulniers

Hello,

Sorry for the delay. I have missed this until now. ;-(

Quoted 13 lines · Nick Desaulniers

Fixes warnings about shifting unsigned literals being undefined behavior.

---
 mm/zsmalloc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/zsmalloc.c +0 -0
index 685049a..5d31458 100644

mm/zsmalloc.c static void init_zspage(struct size_class *class, struct zspage *zspage) · line 1059

Quoted 5 lines
mm/zsmalloc.c +1 -1
@@ -1056,7 +1056,7 @@ static void init_zspage(struct size_class *class, struct zspage *zspage)
                         * Reset OBJ_TAG_BITS bit to last link to tell
                         * whether it's allocated object or not.
                         */
-                       link->next = -1 << OBJ_TAG_BITS;
+                       link->next = -1U << OBJ_TAG_BITS;

-1UL?

Please, resend it with including Andrew Morton <[email protected]> who merges zsmalloc patch into his tree.

Thanks.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#5 lore raw
Quoted 4 lines · Minchan Kim

mm/zsmalloc.c static void init_zspage(struct size_class *class, struct zspage *zspage) · line 1059

Quoted 2 lines
mm/zsmalloc.c +1 -1
-                       link->next = -1 << OBJ_TAG_BITS;
+                       link->next = -1U << OBJ_TAG_BITS;

-1UL?

Oh, boy, shouldn't be rather GENMASK() / GENMASK_ULL() in a way how it's done, for example, here:
https://git.kernel.org/pub/scm/linux/kernel/git/linusw/linux-pinctrl.git/commit/?h=for-next&id=d2b3c353595a855794f8b9df5b5bdbe8deb0c413

Signature · 8 lines
With Best Regards,
Andy Shevchenko

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#6 lore raw
Quoted 1 line · Minchan Kim

Sorry for the delay. I have missed this until now. ;-(

No worries, figured patches would need a post holiday bump for review.

Quoted 7 lines

mm/zsmalloc.c static void init_zspage(struct size_class *class, struct zspage *zspage) · line 1059

Quoted 2 lines · Nick Desaulniers
mm/zsmalloc.c +1 -1
-                       link->next = -1 << OBJ_TAG_BITS;
+                       link->next = -1U << OBJ_TAG_BITS;

-1UL?

Oops, good catch.

Quoted 2 lines

Please, resend it with including Andrew Morton <[email protected]> who merges zsmalloc patch into his tree.

Will do.

Quoted 1 line · Andy Shevchenko

Oh, boy, shouldn't be rather GENMASK() / GENMASK_ULL() in a way how

Thanks for the suggestion. GENMASK(BITS_PER_LONG - 1, OBJ_TAG_BITS); is equivalent. Whether that is more readable, I'll wait for Minchan to decide. If that's preferred, I'll make sure to credit you with the Suggested-By tag in the commit message.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

maintainer
#7 lore raw

Hi Nick,

Quoted 20 lines · Nick Desaulniers
Quoted 1 line · Minchan Kim

Sorry for the delay. I have missed this until now. ;-(

No worries, figured patches would need a post holiday bump for review.

Quoted 7 lines

mm/zsmalloc.c static void init_zspage(struct size_class *class, struct zspage *zspage) · line 1059

Quoted 2 lines · Nick Desaulniers
mm/zsmalloc.c +1 -1
-                       link->next = -1 << OBJ_TAG_BITS;
+                       link->next = -1U << OBJ_TAG_BITS;

-1UL?

Oops, good catch.

Quoted 2 lines

Please, resend it with including Andrew Morton <[email protected]> who merges zsmalloc patch into his tree.

Will do.

Quoted 8 lines
Quoted 1 line · Andy Shevchenko

Oh, boy, shouldn't be rather GENMASK() / GENMASK_ULL() in a way how

Thanks for the suggestion. GENMASK(BITS_PER_LONG - 1, OBJ_TAG_BITS); is equivalent. Whether that is more readable, I'll wait for Minchan to decide. If that's preferred, I'll make sure to credit you with the Suggested-By tag in the commit message.

I don't see any benefit with GENMASK in our usecase. If it's not a good justfication, I'd like to use just -1UL which would be more readable without effort to understand new API.

Thanks.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#8 lore raw

PATCH v2 1/1 zsmalloc: use U suffix for negative literals being shifted

Fixes warnings about shifting unsigned literals being undefined behavior.

---
Changes since v1:
* Use L suffix in addition to U, as suggested (link->next is unsigned long).

 mm/zsmalloc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/zsmalloc.c +1 -1
index 683c065..b9040bd 100644
@@ -1057,7 +1057,7 @@ static void init_zspage(struct size_class *class, struct zspage *zspage)
 			 * Reset OBJ_TAG_BITS bit to last link to tell
 			 * whether it's allocated object or not.
 			 */
-			link->next = -1 << OBJ_TAG_BITS;
+			link->next = -1UL << OBJ_TAG_BITS;
 		}
 		kunmap_atomic(vaddr);
 		page = next_page;
Signature · 1 line
2.7.4
Reviewed-by on v2 1/1
#9 lore raw
Quoted 5 lines · Nick Desaulniers

Fixes warnings about shifting unsigned literals being undefined behavior.

looks good to me.

	-ss

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>