From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 64975C07E99 for ; Mon, 12 Jul 2021 19:42:22 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 04DAB61242 for ; Mon, 12 Jul 2021 19:42:21 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 04DAB61242 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 211BF6B0011; Mon, 12 Jul 2021 15:42:22 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1C2086B0073; Mon, 12 Jul 2021 15:42:22 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 046866B0095; Mon, 12 Jul 2021 15:42:21 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0190.hostedemail.com [216.40.44.190]) by kanga.kvack.org (Postfix) with ESMTP id CFB506B0011 for ; Mon, 12 Jul 2021 15:42:21 -0400 (EDT) Received: from smtpin19.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id E9A71267C1 for ; Mon, 12 Jul 2021 19:42:20 +0000 (UTC) X-FDA: 78354957240.19.6020983 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) by imf25.hostedemail.com (Postfix) with ESMTP id A9408B000291 for ; Mon, 12 Jul 2021 19:42:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=V7+FGrqCE97oONDoHC8Zj/lJXxDhp+XiIcFymDp6zkc=; b=fmgfD2OZAc4ss68kfVkZ5H9h7c udmWGMs2sqNQGSK3Cy+rNrOKrkQvJzaKQwXrAafRFSIcFOz3WeTmhkx+UjF4V9/8QrSJ+RUt3RkjJ D5gxwQce3gN6EXUXOnuQ/soa0oIWR1M4BW5NQ9ITvfMCf6A1LCZWT5KKLiP2LwM4kJ6AFy9iED4gh C74UORY7jBC+2L9xHwRT+1ofjze0GsWIJCZ3FkSRqSqiocMw2MAeI2qeq8URziFFtR+SdQK58QbzE qxoUAmYwCyjn825+os/3JqWCSLUoD60FHN793oH8FNCrO6Bioi028RMr9SfDWN8icbRhm2mdPIMNr omi8j1ww==; Received: from willy by casper.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1m31oH-000Nh9-Qk; Mon, 12 Jul 2021 19:41:49 +0000 Date: Mon, 12 Jul 2021 20:41:45 +0100 From: Matthew Wilcox To: kernel test robot Cc: Mike Rapoport , kbuild-all@lists.01.org, linux-kernel@vger.kernel.org, Andrew Morton , Linux Memory Management List Subject: 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] Message-ID: References: <202107130348.6LsVT9Nc-lkp@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <202107130348.6LsVT9Nc-lkp@intel.com> X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: A9408B000291 X-Stat-Signature: w5ynma6ttmwo5d3gqa9x9cbqqyjy555w Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=fmgfD2OZ; dmarc=none; spf=none (imf25.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org X-HE-Tag: 1626118940-513582 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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)