lkml.org 
[lkml]   [2021]   [Jul]   [12]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
Patch in this message
/
Date
From
SubjectRe: mm/sparse.c:145:2: warning: int result is returned as long value. If the return value is long to avoid loss of information, then you have loss of information. [truncLongCastReturn]
On Tue, Jul 13, 2021 at 03:19:06AM +0800, kernel test robot wrote:
> cppcheck warnings: (new ones prefixed by >>)
> >> mm/sparse.c:145:2: warning: int result is returned as long value. If the return value is long to avoid loss of information, then you have loss of information. [truncLongCastReturn]
> return (nid << SECTION_NID_SHIFT);

Hrm.

include/linux/mmzone.h:#define SECTION_NID_SHIFT 3

I'm going to suggest that we don't allow for 2^29 node IDs in a single
system, so this doesn't actually represent a bug that needs to be fixed.
On the other hand, this type of bug continually bites us, and it would
be good to warn about this kind of thing. So in the spirit of silencing
the warning by doing the promotion that C should have specified in
the first place ...

--- 8< ---

[PATCH] Avoid a warning in sparse memory support

cppcheck warns that we're possibly losing information by shifting an int.
It's a false positive, because we don't allow for a NUMA node ID that
large, but if we ever change SECTION_NID_SHIFT, it could become a problem,
and in any case this is usually a legitimate warning. Fix it by adding
the necessary cast, which makes the compiler generate the right code.

diff --git a/mm/sparse.c b/mm/sparse.c
index 6326cdf36c4f..f17bd4f7caaa 100644
--- a/mm/sparse.c
+++ b/mm/sparse.c
@@ -143,7 +143,7 @@ unsigned long __section_nr(struct mem_section *ms)
*/
static inline unsigned long sparse_encode_early_nid(int nid)
{
- return (nid << SECTION_NID_SHIFT);
+ return ((unsigned long)nid << SECTION_NID_SHIFT);
}

static inline int sparse_early_nid(struct mem_section *section)
\
 
 \ /
  Last update: 2021-07-12 21:42    [W:0.037 / U:0.636 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site