LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
* 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]
@ 2021-07-12 19:19 kernel test robot
  2021-07-12 19:41 ` Matthew Wilcox
  0 siblings, 1 reply; 2+ messages in thread
From: kernel test robot @ 2021-07-12 19:19 UTC (permalink / raw)
  To: Mike Rapoport
  Cc: kbuild-all, linux-kernel, Andrew Morton, Linux Memory Management List

tree:   https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git master
head:   e73f0f0ee7541171d89f2e2491130c7771ba58d3
commit: 36d40290c8f71daf1ba5567ab14574f36b9b8d6a alpha: switch from DISCONTIGMEM to SPARSEMEM
date:   7 months ago
compiler: alpha-linux-gcc (GCC) 9.3.0

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <lkp@intel.com>


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);
    ^

vim +145 mm/sparse.c

4ca644d970bf25 Dave Hansen    2005-10-29  136  
30c253e6da655d Andy Whitcroft 2006-06-23  137  /*
30c253e6da655d Andy Whitcroft 2006-06-23  138   * During early boot, before section_mem_map is used for an actual
30c253e6da655d Andy Whitcroft 2006-06-23  139   * mem_map, we use section_mem_map to store the section's NUMA
30c253e6da655d Andy Whitcroft 2006-06-23  140   * node.  This keeps us from having to use another data structure.  The
30c253e6da655d Andy Whitcroft 2006-06-23  141   * node information is cleared just before we store the real mem_map.
30c253e6da655d Andy Whitcroft 2006-06-23  142   */
30c253e6da655d Andy Whitcroft 2006-06-23  143  static inline unsigned long sparse_encode_early_nid(int nid)
30c253e6da655d Andy Whitcroft 2006-06-23  144  {
30c253e6da655d Andy Whitcroft 2006-06-23 @145  	return (nid << SECTION_NID_SHIFT);
30c253e6da655d Andy Whitcroft 2006-06-23  146  }
30c253e6da655d Andy Whitcroft 2006-06-23  147  

:::::: The code at line 145 was first introduced by commit
:::::: 30c253e6da655d73eb8bfe2adca9b8f4d82fb81e [PATCH] sparsemem: record nid during memory present

:::::: TO: Andy Whitcroft <apw@shadowen.org>
:::::: CC: Linus Torvalds <torvalds@g5.osdl.org>

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all@lists.01.org

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: 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]
  2021-07-12 19:19 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] kernel test robot
@ 2021-07-12 19:41 ` Matthew Wilcox
  0 siblings, 0 replies; 2+ messages in thread
From: Matthew Wilcox @ 2021-07-12 19:41 UTC (permalink / raw)
  To: kernel test robot
  Cc: Mike Rapoport, kbuild-all, linux-kernel, Andrew Morton,
	Linux Memory Management List

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)

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-07-12 19:42 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-12 19:19 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] kernel test robot
2021-07-12 19:41 ` Matthew Wilcox

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).