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=-16.6 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,INCLUDES_PATCH,MAILING_LIST_MULTI, MENTIONS_GIT_HOSTING,SPF_HELO_NONE,SPF_PASS,USER_AGENT_SANE_1 autolearn=ham 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 76FB8C43463 for ; Fri, 18 Sep 2020 16:17:34 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2F182238D6 for ; Fri, 18 Sep 2020 16:17:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1600445854; bh=vDSPwR6Bgtsv8lo5GFN8iaXaPRe2BXebWktgtOo/fdk=; h=Date:From:To:Cc:Subject:References:In-Reply-To:List-ID:From; b=fl2e7vB161EV1SLnDosk7xdfywKZXjn7wZeQJCnVRGV6xEE9lmnwRMgFwfM0VcNiE DMzFZlp0nAhVIUfxMbobaJg/S9YrERibAhLgZ92XAR0tLYGeE7Ov2yNQRT8rBHzOJm NwcH7XmSVYTTO3EegsSSaF/XhNe2dvCIHCTtzf+c= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726440AbgIRQRc (ORCPT ); Fri, 18 Sep 2020 12:17:32 -0400 Received: from mail.kernel.org ([198.145.29.99]:59392 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726415AbgIRQRa (ORCPT ); Fri, 18 Sep 2020 12:17:30 -0400 Received: from embeddedor (187-162-31-110.static.axtel.net [187.162.31.110]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 6D73220795; Fri, 18 Sep 2020 16:17:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1600445850; bh=vDSPwR6Bgtsv8lo5GFN8iaXaPRe2BXebWktgtOo/fdk=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=AJyl8b9xSfvFZR6HGhRH7nsb/iGQhawLhTeyE87loXOdqeZDCPnlqAnzziOzh2HKO tEmmVbzqbqqa1hgzl39wro5uexKcys69WtEYsVMnjGqqYJo1e8RdOXQmdcb6+lB6BK ZkSODultj5cguRFUnMJryTY07P3jVkmWZE8SKw2A= Date: Fri, 18 Sep 2020 11:23:05 -0500 From: "Gustavo A. R. Silva" To: Linus Torvalds Cc: Dennis Zhou , Tejun Heo , Christoph Lameter , Linux-MM , Linux Kernel Mailing List Subject: Re: [GIT PULL] percpu fix for v5.9-rc6 Message-ID: <20200918162305.GB25599@embeddedor> References: <20200917204514.GA2880159@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: User-Agent: Mutt/1.9.4 (2018-02-28) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Sep 17, 2020 at 06:05:13PM -0700, Linus Torvalds wrote: > On Thu, Sep 17, 2020 at 1:45 PM Dennis Zhou wrote: > > > > > > diff --git a/mm/percpu.c b/mm/percpu.c > > index f4709629e6de..1ed1a349eab8 100644 > > --- a/mm/percpu.c > > +++ b/mm/percpu.c > > @@ -1316,7 +1316,7 @@ static struct pcpu_chunk * __init pcpu_alloc_first_chunk(unsigned long tmp_addr, > > > > /* allocate chunk */ > > alloc_size = sizeof(struct pcpu_chunk) + > > - BITS_TO_LONGS(region_size >> PAGE_SHIFT); > > + BITS_TO_LONGS(region_size >> PAGE_SHIFT) * sizeof(unsigned long); > > Hmm. > > Wouldn't this be cleaner as > > alloc_size =struct_size(chunk, populated, > BITS_TO_LONGS(region_size >> PAGE_SHIFT) ); Yeah; the above is much better. Please, use that helper. > and looking at this, I realize that I thought we enabled warnings for > 'sizeof()' of flexible array structures to avoid these kinds of > mistakes, but that must clearly have happened only in a dream of mine. If you were to try to apply the sizeof() operator to the flexible-array member alone: sizeof(chunk->populated); you would get a warning because such arrays have incomplete type, see below: mm/percpu.c: In function ‘pcpu_alloc_first_chunk’: mm/percpu.c:1320:52: error: invalid application of ‘sizeof’ to incomplete type ‘long unsigned int[]’ 1320 | BITS_TO_LONGS(region_size >> PAGE_SHIFT) * sizeof(chunk->populated); | ^ However, in this case, sizeof() is being applied to the object type, which doesn't cause a warning, but still is an error-prone coding practice. For instance, this is the bugfix[1], for a 4-year old bug introduced by the combination of weak code and this commit[2]. This bug could have been prevented by either adopting better coding practices or through the use[3] of the recent struct_size() helper. So please, whenever you can use it, do so. :) Thanks -- Gustavo [1] https://git.kernel.org/linus/cffaaf0c816238c45cd2d06913476c83eb50f682 [2] https://git.kernel.org/linus/57384592c43375d2c9a14d82aebbdc95fdda9e9d [3] https://git.kernel.org/linus/553d66cb1e8667aadb57e3804775c5ce1724a49b