All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Jörn Engel" <joern@barelysecure.org>
To: Pei Xiao <xiaopei01@kylinos.cn>
Cc: Miquel Raynal <miquel.raynal@bootlin.com>,
	syzbot <syzbot+b320a4d5f65a61dbbf89@syzkaller.appspotmail.com>,
	joern@lazybastard.org, linux-kernel@vger.kernel.org,
	linux-mtd@lists.infradead.org, richard@nod.at,
	syzkaller-bugs@googlegroups.com, vigneshr@ti.com
Subject: Re: [syzbot] [mtd?] divide error in block2mtd_setup2
Date: Tue, 11 Aug 2026 11:19:54 -0700	[thread overview]
Message-ID: <antnynHuZ2W8JmCc@cashel.logfs.org> (raw)
In-Reply-To: <bbbff0ba-09c4-49dc-9aac-371c0c7d6902@kylinos.cn>

On Tue, Aug 11, 2026 at 11:02:53AM +0800, Pei Xiao wrote:
> > 
> Thank you for your reply. I am waiting for a reply on whether the cast
> on 'long' needs to be removed. Once that is confirmed, I would be happy
> to do it. Thank you.

It doesn't need to be removed, but it should be removed.  Any
unnecessary code should be removed in general and unnecessary casts in
particular should.

One of better parts of the C language is that casts are hardly ever
necessary.  Which means that any remaining necessary casts should
immediately draw attention of the reader - something tricky and
potentially dangerous is going on here.

But you stop paying attention when things become too common.  Which
means that unnecessary casts are not just noise, they actively do harm.


In this particular case, we can simply test whether the cast makes any
difference:

#include <assert.h>
#include <stdio.h>

int main(void)
{
	for (int i=0; i<=1<<16; i++) {
		for (int k=1; k<=1<<16; k++) {
			long long size = (long long)i << 32;
			int erase_size = k;

			long long a = size % erase_size;
			long long b = (long)size % erase_size;
			assert(a==b);
		}
	}
}

Compile with optimizations and the compiler decides to remove the loop.
Unless you suspect a compiler bug, I guess that settles the question. ;)

Jörn

--
Semper ubi sub ubi ubique.
-- latin pun

WARNING: multiple messages have this Message-ID (diff)
From: "Jörn Engel" <joern@barelysecure.org>
To: Pei Xiao <xiaopei01@kylinos.cn>
Cc: Miquel Raynal <miquel.raynal@bootlin.com>,
	syzbot <syzbot+b320a4d5f65a61dbbf89@syzkaller.appspotmail.com>,
	joern@lazybastard.org, linux-kernel@vger.kernel.org,
	linux-mtd@lists.infradead.org, richard@nod.at,
	syzkaller-bugs@googlegroups.com, vigneshr@ti.com
Subject: Re: [syzbot] [mtd?] divide error in block2mtd_setup2
Date: Tue, 11 Aug 2026 11:19:54 -0700	[thread overview]
Message-ID: <antnynHuZ2W8JmCc@cashel.logfs.org> (raw)
In-Reply-To: <bbbff0ba-09c4-49dc-9aac-371c0c7d6902@kylinos.cn>

On Tue, Aug 11, 2026 at 11:02:53AM +0800, Pei Xiao wrote:
> > 
> Thank you for your reply. I am waiting for a reply on whether the cast
> on 'long' needs to be removed. Once that is confirmed, I would be happy
> to do it. Thank you.

It doesn't need to be removed, but it should be removed.  Any
unnecessary code should be removed in general and unnecessary casts in
particular should.

One of better parts of the C language is that casts are hardly ever
necessary.  Which means that any remaining necessary casts should
immediately draw attention of the reader - something tricky and
potentially dangerous is going on here.

But you stop paying attention when things become too common.  Which
means that unnecessary casts are not just noise, they actively do harm.


In this particular case, we can simply test whether the cast makes any
difference:

#include <assert.h>
#include <stdio.h>

int main(void)
{
	for (int i=0; i<=1<<16; i++) {
		for (int k=1; k<=1<<16; k++) {
			long long size = (long long)i << 32;
			int erase_size = k;

			long long a = size % erase_size;
			long long b = (long)size % erase_size;
			assert(a==b);
		}
	}
}

Compile with optimizations and the compiler decides to remove the loop.
Unless you suspect a compiler bug, I guess that settles the question. ;)

Jörn

--
Semper ubi sub ubi ubique.
-- latin pun

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

  reply	other threads:[~2026-08-11 18:20 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-10  0:34 [syzbot] [mtd?] divide error in block2mtd_setup2 syzbot
2026-08-10  0:34 ` syzbot
2026-08-10  2:03 ` Jörn Engel
2026-08-10  2:03   ` Jörn Engel
2026-08-10  9:17   ` Pei Xiao
2026-08-10  9:17     ` Pei Xiao
2026-08-10 12:40     ` Miquel Raynal
2026-08-10 12:40       ` Miquel Raynal
2026-08-11  3:02       ` Pei Xiao
2026-08-11  3:02         ` Pei Xiao
2026-08-11 18:19         ` Jörn Engel [this message]
2026-08-11 18:19           ` Jörn Engel
2026-08-12  1:45           ` Pei Xiao
2026-08-12  1:45             ` Pei Xiao
2026-08-12  2:51             ` Jörn Engel
2026-08-12  2:51               ` Jörn Engel
2026-08-10 16:27   ` Jörn Engel
2026-08-10 16:27     ` Jörn Engel
2026-08-11  1:21     ` Pei Xiao
2026-08-11  1:21       ` Pei Xiao
2026-08-10  9:12 ` [PATCH] mtd: block2mtd: Fix divide error when erase_size is zero Pei Xiao
2026-08-10  9:12   ` Pei Xiao
2026-08-11 17:15 ` [syzbot] [mtd?] divide error in block2mtd_setup2 syzbot
2026-08-11 17:15   ` syzbot
2026-08-12  1:41   ` [PATCH v2] mtd: block2mtd: Fix divide error when erase_size is zero Pei Xiao
2026-08-12  2:05     ` [syzbot] [mtd?] divide error in block2mtd_setup2 syzbot
2026-08-12  1:41 ` Forwarded: [PATCH v2] mtd: block2mtd: Fix divide error when erase_size is zero syzbot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=antnynHuZ2W8JmCc@cashel.logfs.org \
    --to=joern@barelysecure.org \
    --cc=joern@lazybastard.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mtd@lists.infradead.org \
    --cc=miquel.raynal@bootlin.com \
    --cc=richard@nod.at \
    --cc=syzbot+b320a4d5f65a61dbbf89@syzkaller.appspotmail.com \
    --cc=syzkaller-bugs@googlegroups.com \
    --cc=vigneshr@ti.com \
    --cc=xiaopei01@kylinos.cn \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.