Message ID | 071e9f32c19a007f4922903282c9121898641400.1681671848.git.christophe.jaillet@wanadoo.fr |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp1707791vqo; Sun, 16 Apr 2023 12:22:57 -0700 (PDT) X-Google-Smtp-Source: AKy350ZmZtuJtwEJBU4xfL4Fo3cWVzylr6vCMeHx6xB3pfrqQfGxf3eSkSUbzXzV9NgxoVH3uCc5 X-Received: by 2002:a17:902:db09:b0:1a6:82ac:f277 with SMTP id m9-20020a170902db0900b001a682acf277mr11842324plx.14.1681672977630; Sun, 16 Apr 2023 12:22:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681672977; cv=none; d=google.com; s=arc-20160816; b=NdB8EEKFpFxe+UK/sd8tnCpF44UC5WDiQft/1T8rPbBD3QYc1LPed8rkjqktVd5hB+ Sx4/i2Aq/pRtjHND2WpqEKzy/4nzywcGK6inGwUe7zG/SYKvajHOzgY8wLsP39Kc8+aj HA5jNGJ1rSI3JvFyjJlojdmBiNce3fBTctQyptPVcDGmJ/W5XwaBn9C/uNaei2LiZr3G ylaEpQY2TvuQNbJQzheJOLpoQ4VPu+376ZhZduKWLYvkORN4yAHj/tZmqu/c1xbfz+Ik D6BLad1qWQQeUckTMkqinTV7O8Oj1E2Hi5PN5VAGOMo8fz3jd1hnNT05XkrJclmpCNMB Ju5w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=IVg6fEiAUlOm4eZf7hYcoOgAhxCwgPubM+tzVphnhWA=; b=ZvFdGPfj0GCR4WPPpGFDI/JnP2yhvXt/Rvx6QFp7Hbb5gUYxpEKE6BkQrII+kp8Bom JJuxKr4zWDiKl67+vnvJv6rYzC9lQKGL+00M+xZ9Tl8HEhNel8NGtXUr4ctnbwtI4bvl 1ImTUc5vMWDm+r8GZjO2UMI4ykHdwaFMLw1rIGxREuMIE9VWdmcql4CwwzrmYp+ge3Oj RFg2d66c3aIZ75z48GVvBFodWPsqmqac8ncCSKHg3q/iLBXeJvd2N88vAXcvkAmZGNYj JK7TQsKZFHeZU7QV4Nso/YuJ7S1twGLCFWxRLh3iJU+OzIfiydmPPfN6m8lEIOSCzSgo 4WBw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@orange.fr header.s=t20230301 header.b=stcSMDrg; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id jw16-20020a170903279000b001a6b0239806si5041096plb.406.2023.04.16.12.22.41; Sun, 16 Apr 2023 12:22:57 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@orange.fr header.s=t20230301 header.b=stcSMDrg; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229446AbjDPTFo (ORCPT <rfc822;yuanzuo1009@gmail.com> + 99 others); Sun, 16 Apr 2023 15:05:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52836 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229484AbjDPTFn (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Sun, 16 Apr 2023 15:05:43 -0400 Received: from smtp.smtpout.orange.fr (smtp-22.smtpout.orange.fr [80.12.242.22]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 066DC1FC3 for <linux-kernel@vger.kernel.org>; Sun, 16 Apr 2023 12:05:40 -0700 (PDT) Received: from pop-os.home ([86.243.2.178]) by smtp.orange.fr with ESMTPA id o7gupnSP89ijuo7gupFud6; Sun, 16 Apr 2023 21:05:39 +0200 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=orange.fr; s=t20230301; t=1681671939; bh=IVg6fEiAUlOm4eZf7hYcoOgAhxCwgPubM+tzVphnhWA=; h=From:To:Cc:Subject:Date; b=stcSMDrgerXIwGDnWbFTpJLich5sCmokXWajyYm+hTqyDWD1ySwkL80AfExFKq7Ei bK7BYo5b4Ff1B5tdqZJ7aT4fjEPnexivEfJdFVzbqv4PvnmheT9h9rNayHAowfz+eL I7KhMpmN21W8PHSti6gf5HunswVUeua7UZMD622kE+ajo0Gx/GResTiROqKUMaLiGj ZwAFborjvoKnKsSUh7W5DzJOMwo7G3zmDosgiDOU9DO76gwcWXiXMHHNc9i1KErbXZ LM/Ozs44yreCoWTzUahIRG2KBDIzvxCOaxg6ReIfKxz3nYp08I7njdCD+HvUpjLfhM v+cg+PSIV0JPA== X-ME-Helo: pop-os.home X-ME-Auth: Y2hyaXN0b3BoZS5qYWlsbGV0QHdhbmFkb28uZnI= X-ME-Date: Sun, 16 Apr 2023 21:05:39 +0200 X-ME-IP: 86.243.2.178 From: Christophe JAILLET <christophe.jaillet@wanadoo.fr> To: Yoshinori Sato <ysato@users.sourceforge.jp>, Rich Felker <dalias@libc.org>, John Paul Adrian Glaubitz <glaubitz@physik.fu-berlin.de> Cc: linux-kernel@vger.kernel.org, kernel-janitors@vger.kernel.org, Christophe JAILLET <christophe.jaillet@wanadoo.fr>, linux-sh@vger.kernel.org Subject: [PATCH RESEND] sh: sq: Use the bitmap API when applicable Date: Sun, 16 Apr 2023 21:05:14 +0200 Message-Id: <071e9f32c19a007f4922903282c9121898641400.1681671848.git.christophe.jaillet@wanadoo.fr> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_PASS, T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1763361924348058657?= X-GMAIL-MSGID: =?utf-8?q?1763361924348058657?= |
Series |
[RESEND] sh: sq: Use the bitmap API when applicable
|
|
Commit Message
Christophe JAILLET
April 16, 2023, 7:05 p.m. UTC
Using the bitmap API is less verbose than hand writing them.
It also improves the semantic.
Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
---
This is a resend of [1].
Now cross-compile tested with CONFIG_CPU_SUBTYPE_SH7770=y
[1]: https://lore.kernel.org/all/521788e22ad8f7a5058c154f068b061525321841.1656142814.git.christophe.jaillet@wanadoo.fr/
---
arch/sh/kernel/cpu/sh4/sq.c | 7 +++----
1 file changed, 3 insertions(+), 4 deletions(-)
Comments
Hi Christophe! On Sun, 2023-04-16 at 21:05 +0200, Christophe JAILLET wrote: > Using the bitmap API is less verbose than hand writing them. > It also improves the semantic. > > Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr> > --- > This is a resend of [1]. > > Now cross-compile tested with CONFIG_CPU_SUBTYPE_SH7770=y > > [1]: https://lore.kernel.org/all/521788e22ad8f7a5058c154f068b061525321841.1656142814.git.christophe.jaillet@wanadoo.fr/ > --- > arch/sh/kernel/cpu/sh4/sq.c | 7 +++---- > 1 file changed, 3 insertions(+), 4 deletions(-) > > diff --git a/arch/sh/kernel/cpu/sh4/sq.c b/arch/sh/kernel/cpu/sh4/sq.c > index 27f2e3da5aa2..d289e99dc118 100644 > --- a/arch/sh/kernel/cpu/sh4/sq.c > +++ b/arch/sh/kernel/cpu/sh4/sq.c > @@ -372,7 +372,6 @@ static struct subsys_interface sq_interface = { > static int __init sq_api_init(void) > { > unsigned int nr_pages = 0x04000000 >> PAGE_SHIFT; > - unsigned int size = (nr_pages + (BITS_PER_LONG - 1)) / BITS_PER_LONG; > int ret = -ENOMEM; > > printk(KERN_NOTICE "sq: Registering store queue API.\n"); > @@ -382,7 +381,7 @@ static int __init sq_api_init(void) > if (unlikely(!sq_cache)) > return ret; > > - sq_bitmap = kzalloc(size, GFP_KERNEL); > + sq_bitmap = bitmap_zalloc(nr_pages, GFP_KERNEL); > if (unlikely(!sq_bitmap)) > goto out; > > @@ -393,7 +392,7 @@ static int __init sq_api_init(void) > return 0; > > out: > - kfree(sq_bitmap); > + bitmap_free(sq_bitmap); > kmem_cache_destroy(sq_cache); > > return ret; > @@ -402,7 +401,7 @@ static int __init sq_api_init(void) > static void __exit sq_api_exit(void) > { > subsys_interface_unregister(&sq_interface); > - kfree(sq_bitmap); > + bitmap_free(sq_bitmap); > kmem_cache_destroy(sq_cache); > } > Thanks for resending this patch. I will try to review it next week and also boot-test it on my SH7785LCR evaluation board. I am not familiar with the bitmap API at the moment, so I might take a little longer to review this patch. Adrian
On Sun, Apr 16, 2023 at 9:10 PM Christophe JAILLET <christophe.jaillet@wanadoo.fr> wrote: > Using the bitmap API is less verbose than hand writing them. > It also improves the semantic. > > Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr> Reviewed-by: Geert Uytterhoeven <geert+renesas@glider.be> Gr{oetje,eeting}s, Geert
On 4/16/23 14:05, Christophe JAILLET wrote: > Using the bitmap API is less verbose than hand writing them. > It also improves the semantic. > > Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr> Tested-by: Rob Landley <rob@landley.net> It booted and ran for me. Rob
Hi Christophe! Thanks for your patch. The changes look good to me. However, I have one question, see below. On Sun, 2023-04-16 at 21:05 +0200, Christophe JAILLET wrote: > Using the bitmap API is less verbose than hand writing them. > It also improves the semantic. > > Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr> > --- > This is a resend of [1]. > > Now cross-compile tested with CONFIG_CPU_SUBTYPE_SH7770=y > > [1]: https://lore.kernel.org/all/521788e22ad8f7a5058c154f068b061525321841.1656142814.git.christophe.jaillet@wanadoo.fr/ > --- > arch/sh/kernel/cpu/sh4/sq.c | 7 +++---- > 1 file changed, 3 insertions(+), 4 deletions(-) > > diff --git a/arch/sh/kernel/cpu/sh4/sq.c b/arch/sh/kernel/cpu/sh4/sq.c > index 27f2e3da5aa2..d289e99dc118 100644 > --- a/arch/sh/kernel/cpu/sh4/sq.c > +++ b/arch/sh/kernel/cpu/sh4/sq.c > @@ -372,7 +372,6 @@ static struct subsys_interface sq_interface = { > static int __init sq_api_init(void) > { > unsigned int nr_pages = 0x04000000 >> PAGE_SHIFT; > - unsigned int size = (nr_pages + (BITS_PER_LONG - 1)) / BITS_PER_LONG; > int ret = -ENOMEM; > > printk(KERN_NOTICE "sq: Registering store queue API.\n"); > @@ -382,7 +381,7 @@ static int __init sq_api_init(void) > if (unlikely(!sq_cache)) > return ret; > > - sq_bitmap = kzalloc(size, GFP_KERNEL); > + sq_bitmap = bitmap_zalloc(nr_pages, GFP_KERNEL); > if (unlikely(!sq_bitmap)) > goto out; > I have look through other patches where k{z,c,m}alloc() were replaced with bitmap_zalloc() and I noticed that in the other cases such as [1], kcalloc() was used instead of kzalloc() in our cases with the element size set to sizeof(long) while kzalloc() is using an element size equal to a byte. Wouldn't that mean that the current code in sq is allocating a buffer that is too small by a factor of 1/sizeof(long) or am I missing something? @Geert: Do you have any idea? > @@ -393,7 +392,7 @@ static int __init sq_api_init(void) > return 0; > > out: > - kfree(sq_bitmap); > + bitmap_free(sq_bitmap); > kmem_cache_destroy(sq_cache); > > return ret; > @@ -402,7 +401,7 @@ static int __init sq_api_init(void) > static void __exit sq_api_exit(void) > { > subsys_interface_unregister(&sq_interface); > - kfree(sq_bitmap); > + bitmap_free(sq_bitmap); > kmem_cache_destroy(sq_cache); > } > Adrian > [1] https://lkml.org/lkml/2021/11/28/155
Hi Adrian, On Tue, Apr 18, 2023 at 8:36 AM John Paul Adrian Glaubitz <glaubitz@physik.fu-berlin.de> wrote: > Thanks for your patch. The changes look good to me. However, I have > one question, see below. > > On Sun, 2023-04-16 at 21:05 +0200, Christophe JAILLET wrote: > > Using the bitmap API is less verbose than hand writing them. > > It also improves the semantic. > > > > Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr> > > --- a/arch/sh/kernel/cpu/sh4/sq.c > > +++ b/arch/sh/kernel/cpu/sh4/sq.c > > @@ -372,7 +372,6 @@ static struct subsys_interface sq_interface = { > > static int __init sq_api_init(void) > > { > > unsigned int nr_pages = 0x04000000 >> PAGE_SHIFT; > > - unsigned int size = (nr_pages + (BITS_PER_LONG - 1)) / BITS_PER_LONG; > > int ret = -ENOMEM; > > > > printk(KERN_NOTICE "sq: Registering store queue API.\n"); > > @@ -382,7 +381,7 @@ static int __init sq_api_init(void) > > if (unlikely(!sq_cache)) > > return ret; > > > > - sq_bitmap = kzalloc(size, GFP_KERNEL); > > + sq_bitmap = bitmap_zalloc(nr_pages, GFP_KERNEL); > > if (unlikely(!sq_bitmap)) > > goto out; > > > > I have look through other patches where k{z,c,m}alloc() were replaced with > bitmap_zalloc() and I noticed that in the other cases such as [1], kcalloc() > was used instead of kzalloc() in our cases with the element size set to > sizeof(long) while kzalloc() is using an element size equal to a byte. > > Wouldn't that mean that the current code in sq is allocating a buffer that is > too small by a factor of 1/sizeof(long) or am I missing something? > > @Geert: Do you have any idea? Nice catch! Looking more deeply at the code, the intention is to allocate a bitmap with nr_pages bits, so the code fater Christophe's patch is correct. However, the old code is indeed wrong: (nr_pages + (BITS_PER_LONG - 1)) / BITS_PER_LONG The aim is to calculate the size in bytes, rounded up to an integral number of longs, but it lacks a final multiplication by BITS_PER_BYTE, so it's off by a factor of 4. Fixes: d7c30c682a278abe ("sh: Store Queue API rework.") As we didn't have bitmap_zalloc() until commit c42b65e363ce97a8 ("bitmap: Add bitmap_alloc(), bitmap_zalloc() and bitmap_free()") in v4.19, it would be good to fix the bug first in a separate patch, not using BTW, interesting how this got missed when fixing the other out-of-range bug in commit 9f650cf2b811cfb6 ("sh: Fix store queue bitmap end.", s/marc.theaimsgroup.com/marc.info/ when following the link). Gr{oetje,eeting}s, Geert
Hi Geert! On Tue, 2023-04-18 at 09:14 +0200, Geert Uytterhoeven wrote: > Hi Adrian, > > On Tue, Apr 18, 2023 at 8:36 AM John Paul Adrian Glaubitz > <glaubitz@physik.fu-berlin.de> wrote: > > Thanks for your patch. The changes look good to me. However, I have > > one question, see below. > > > > On Sun, 2023-04-16 at 21:05 +0200, Christophe JAILLET wrote: > > > Using the bitmap API is less verbose than hand writing them. > > > It also improves the semantic. > > > > > > Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr> > > > > --- a/arch/sh/kernel/cpu/sh4/sq.c > > > +++ b/arch/sh/kernel/cpu/sh4/sq.c > > > @@ -372,7 +372,6 @@ static struct subsys_interface sq_interface = { > > > static int __init sq_api_init(void) > > > { > > > unsigned int nr_pages = 0x04000000 >> PAGE_SHIFT; > > > - unsigned int size = (nr_pages + (BITS_PER_LONG - 1)) / BITS_PER_LONG; > > > int ret = -ENOMEM; > > > > > > printk(KERN_NOTICE "sq: Registering store queue API.\n"); > > > @@ -382,7 +381,7 @@ static int __init sq_api_init(void) > > > if (unlikely(!sq_cache)) > > > return ret; > > > > > > - sq_bitmap = kzalloc(size, GFP_KERNEL); > > > + sq_bitmap = bitmap_zalloc(nr_pages, GFP_KERNEL); > > > if (unlikely(!sq_bitmap)) > > > goto out; > > > > > > > I have look through other patches where k{z,c,m}alloc() were replaced with > > bitmap_zalloc() and I noticed that in the other cases such as [1], kcalloc() > > was used instead of kzalloc() in our cases with the element size set to > > sizeof(long) while kzalloc() is using an element size equal to a byte. > > > > Wouldn't that mean that the current code in sq is allocating a buffer that is > > too small by a factor of 1/sizeof(long) or am I missing something? > > > > @Geert: Do you have any idea? > > Nice catch! > > Looking more deeply at the code, the intention is to allocate a bitmap > with nr_pages bits, so the code fater Christophe's patch is correct. > However, the old code is indeed wrong: > > (nr_pages + (BITS_PER_LONG - 1)) / BITS_PER_LONG > > The aim is to calculate the size in bytes, rounded up to an integral > number of longs, but it lacks a final multiplication by BITS_PER_BYTE, > so it's off by a factor of 4. Yeah, that's what I understood from reading the code which is why I was wondering why the factor was missing. > Fixes: d7c30c682a278abe ("sh: Store Queue API rework.") > > As we didn't have bitmap_zalloc() until commit c42b65e363ce97a8 > ("bitmap: Add bitmap_alloc(), bitmap_zalloc() and bitmap_free()") > in v4.19, it would be good to fix the bug first in a separate patch, > not using I agree. Do you want to send a patch I can review? > BTW, interesting how this got missed when fixing the other out-of-range > bug in commit 9f650cf2b811cfb6 ("sh: Fix store queue bitmap end.", > s/marc.theaimsgroup.com/marc.info/ when following the link). Yeah. PS: Sorry for the slightly messy grammar in my previous mail, didn't have a coffee yet that early in the morning :-). Adrian
On Tue, Apr 18, 2023 at 08:36:40AM +0200, John Paul Adrian Glaubitz wrote: > Hi Christophe! > > Thanks for your patch. The changes look good to me. However, I have > one question, see below. > > On Sun, 2023-04-16 at 21:05 +0200, Christophe JAILLET wrote: > > Using the bitmap API is less verbose than hand writing them. > > It also improves the semantic. > > > > Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr> > > --- > > This is a resend of [1]. > > > > Now cross-compile tested with CONFIG_CPU_SUBTYPE_SH7770=y > > > > [1]: https://lore.kernel.org/all/521788e22ad8f7a5058c154f068b061525321841.1656142814.git.christophe.jaillet@wanadoo.fr/ > > --- > > arch/sh/kernel/cpu/sh4/sq.c | 7 +++---- > > 1 file changed, 3 insertions(+), 4 deletions(-) > > > > diff --git a/arch/sh/kernel/cpu/sh4/sq.c b/arch/sh/kernel/cpu/sh4/sq.c > > index 27f2e3da5aa2..d289e99dc118 100644 > > --- a/arch/sh/kernel/cpu/sh4/sq.c > > +++ b/arch/sh/kernel/cpu/sh4/sq.c > > @@ -372,7 +372,6 @@ static struct subsys_interface sq_interface = { > > static int __init sq_api_init(void) > > { > > unsigned int nr_pages = 0x04000000 >> PAGE_SHIFT; > > - unsigned int size = (nr_pages + (BITS_PER_LONG - 1)) / BITS_PER_LONG; > > int ret = -ENOMEM; > > > > printk(KERN_NOTICE "sq: Registering store queue API.\n"); > > @@ -382,7 +381,7 @@ static int __init sq_api_init(void) > > if (unlikely(!sq_cache)) > > return ret; > > > > - sq_bitmap = kzalloc(size, GFP_KERNEL); > > + sq_bitmap = bitmap_zalloc(nr_pages, GFP_KERNEL); > > if (unlikely(!sq_bitmap)) > > goto out; > > > > I have look through other patches where k{z,c,m}alloc() were replaced with > bitmap_zalloc() and I noticed that in the other cases such as [1], kcalloc() > was used instead of kzalloc() in our cases with the element size set to > sizeof(long) while kzalloc() is using an element size equal to a byte. > > Wouldn't that mean that the current code in sq is allocating a buffer that is > too small by a factor of 1/sizeof(long) or am I missing something? Yes. You are correct. The original code is buggy. size is the number of longs we need so kzalloc() is allocating a too small buffer. It should be kzalloc(size * sizeof(long), ... etc as you say. I have some unpublished Smatch stuff which tries to track "variable x is in terms of bit units or byte units etc." I will try to make a static checker rule for this. regards, dan carpenter
On Tue, Apr 18, 2023 at 10:30:01AM +0300, Dan Carpenter wrote: > I have some unpublished Smatch stuff which tries to track "variable x > is in terms of bit units or byte units etc." I will try to make a > static checker rule for this. Attached. It prints a warning like this: drivers/net/ethernet/broadcom/cnic.c:667 cnic_init_id_tbl() warn: allocating units of longs instead of bytes 'test_var' I'll test it out tonight. regards, dan carpenter
Hi Dan! On Tue, 2023-04-18 at 12:39 +0300, Dan Carpenter wrote: > On Tue, Apr 18, 2023 at 10:30:01AM +0300, Dan Carpenter wrote: > > I have some unpublished Smatch stuff which tries to track "variable x > > is in terms of bit units or byte units etc." I will try to make a > > static checker rule for this. > > Attached. It prints a warning like this: > > drivers/net/ethernet/broadcom/cnic.c:667 cnic_init_id_tbl() warn: allocating units of longs instead of bytes 'test_var' > > I'll test it out tonight. Nice job, thanks for creating this very handy script! Adrian
Le 18/04/2023 à 09:14, Geert Uytterhoeven a écrit : > > Nice catch! > > Looking more deeply at the code, the intention is to allocate a bitmap > with nr_pages bits, so the code fater Christophe's patch is correct. > However, the old code is indeed wrong: > > (nr_pages + (BITS_PER_LONG - 1)) / BITS_PER_LONG > > The aim is to calculate the size in bytes, rounded up to an integral > number of longs, but it lacks a final multiplication by BITS_PER_BYTE, > so it's off by a factor of 4. > > Fixes: d7c30c682a278abe ("sh: Store Queue API rework.") > > As we didn't have bitmap_zalloc() until commit c42b65e363ce97a8 > ("bitmap: Add bitmap_alloc(), bitmap_zalloc() and bitmap_free()") > in v4.19, it would be good to fix the bug first in a separate patch, > not using > > BTW, interesting how this got missed when fixing the other out-of-range > bug in commit 9f650cf2b811cfb6 ("sh: Fix store queue bitmap end.", > s/marc.theaimsgroup.com/marc.info/ when following the link). So, this means that this got unnoticed for 16 years? Waouh! I would never have thought that a "trivial" clean-up that I took time to repost could trigger such a thing! Again. Waouh! Maybe, 0x04000000 is way to big? Anyone knows where this value comes from? Could there have been some memory corruption in real world application? CJ > > Gr{oetje,eeting}s, > > Geert >
On Tue, Apr 18, 2023 at 12:39:43PM +0300, Dan Carpenter wrote: > On Tue, Apr 18, 2023 at 10:30:01AM +0300, Dan Carpenter wrote: > > I have some unpublished Smatch stuff which tries to track "variable x > > is in terms of bit units or byte units etc." I will try to make a > > static checker rule for this. > > Attached. It prints a warning like this: > > drivers/net/ethernet/broadcom/cnic.c:667 cnic_init_id_tbl() warn: allocating units of longs instead of bytes 'test_var' > > I'll test it out tonight. It didn't find any bugs. (Which is hardly surprising because most would be caught in testing). No false postives either. I imagine that people will introduce bugs like this in the future and now we'll see it when that happens. regards, dan carpenter
Hi Christophe! On Tue, 2023-04-18 at 20:05 +0200, Christophe JAILLET wrote: > Le 18/04/2023 à 09:14, Geert Uytterhoeven a écrit : > > > > Nice catch! > > > > Looking more deeply at the code, the intention is to allocate a bitmap > > with nr_pages bits, so the code fater Christophe's patch is correct. > > However, the old code is indeed wrong: > > > > (nr_pages + (BITS_PER_LONG - 1)) / BITS_PER_LONG > > > > The aim is to calculate the size in bytes, rounded up to an integral > > number of longs, but it lacks a final multiplication by BITS_PER_BYTE, > > so it's off by a factor of 4. > > > > Fixes: d7c30c682a278abe ("sh: Store Queue API rework.") > > > > As we didn't have bitmap_zalloc() until commit c42b65e363ce97a8 > > ("bitmap: Add bitmap_alloc(), bitmap_zalloc() and bitmap_free()") > > in v4.19, it would be good to fix the bug first in a separate patch, > > not using > > > > BTW, interesting how this got missed when fixing the other out-of-range > > bug in commit 9f650cf2b811cfb6 ("sh: Fix store queue bitmap end.", > > s/marc.theaimsgroup.com/marc.info/ when following the link). > > So, this means that this got unnoticed for 16 years? > Waouh! > > I would never have thought that a "trivial" clean-up that I took time to > repost could trigger such a thing! I have fixed the original bug in my for-next branch [1] now. Would you mind rebasing your patch on top of that branch and resend it? The reason why we're doing this is because we want to be able to backport the fix to older kernel versions such as 4.14 which don't have the bitmap API yet. Thanks, Adrian > [1] https://git.kernel.org/pub/scm/linux/kernel/git/glaubitz/sh-linux.git/log/?h=for-next
diff --git a/arch/sh/kernel/cpu/sh4/sq.c b/arch/sh/kernel/cpu/sh4/sq.c index 27f2e3da5aa2..d289e99dc118 100644 --- a/arch/sh/kernel/cpu/sh4/sq.c +++ b/arch/sh/kernel/cpu/sh4/sq.c @@ -372,7 +372,6 @@ static struct subsys_interface sq_interface = { static int __init sq_api_init(void) { unsigned int nr_pages = 0x04000000 >> PAGE_SHIFT; - unsigned int size = (nr_pages + (BITS_PER_LONG - 1)) / BITS_PER_LONG; int ret = -ENOMEM; printk(KERN_NOTICE "sq: Registering store queue API.\n"); @@ -382,7 +381,7 @@ static int __init sq_api_init(void) if (unlikely(!sq_cache)) return ret; - sq_bitmap = kzalloc(size, GFP_KERNEL); + sq_bitmap = bitmap_zalloc(nr_pages, GFP_KERNEL); if (unlikely(!sq_bitmap)) goto out; @@ -393,7 +392,7 @@ static int __init sq_api_init(void) return 0; out: - kfree(sq_bitmap); + bitmap_free(sq_bitmap); kmem_cache_destroy(sq_cache); return ret; @@ -402,7 +401,7 @@ static int __init sq_api_init(void) static void __exit sq_api_exit(void) { subsys_interface_unregister(&sq_interface); - kfree(sq_bitmap); + bitmap_free(sq_bitmap); kmem_cache_destroy(sq_cache); }