linux-nilfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] nilfs2: Use div64_ul() instead of do_div()
@ 2024-03-06 14:25 Ryusuke Konishi
  0 siblings, 0 replies; 7+ messages in thread
From: Ryusuke Konishi @ 2024-03-06 14:25 UTC (permalink / raw)
  To: Andrew Morton; +Cc: linux-nilfs, linux-kernel

From: Thorsten Blum <thorsten.blum@toblux.com>

Fixes Coccinelle/coccicheck warnings reported by do_div.cocci.

Compared to do_div(), div64_ul() does not implicitly cast the divisor and
does not unnecessarily calculate the remainder.

Link: https://lkml.kernel.org/r/20240229210456.63234-2-thorsten.blum@toblux.com
Signed-off-by: Thorsten Blum <thorsten.blum@toblux.com>
Signed-off-by: Ryusuke Konishi <konishi.ryusuke@gmail.com>
---
Hi Andrew,

Could you please add this to the queue for the merge window?
It's a little late addition, so if this doesn't make it to the
upcoming merge window, please queue it for the next one.

Thanks,
Ryusuke Konishi

 fs/nilfs2/cpfile.c    | 2 +-
 fs/nilfs2/dat.c       | 2 +-
 fs/nilfs2/ioctl.c     | 4 ++--
 fs/nilfs2/sufile.c    | 2 +-
 fs/nilfs2/super.c     | 2 +-
 fs/nilfs2/the_nilfs.c | 2 +-
 6 files changed, 7 insertions(+), 7 deletions(-)

diff --git a/fs/nilfs2/cpfile.c b/fs/nilfs2/cpfile.c
index 2c57132584de..69a5cced1e84 100644
--- a/fs/nilfs2/cpfile.c
+++ b/fs/nilfs2/cpfile.c
@@ -28,7 +28,7 @@ nilfs_cpfile_get_blkoff(const struct inode *cpfile, __u64 cno)
 {
 	__u64 tcno = cno + NILFS_MDT(cpfile)->mi_first_entry_offset - 1;
 
-	do_div(tcno, nilfs_cpfile_checkpoints_per_block(cpfile));
+	tcno = div64_ul(tcno, nilfs_cpfile_checkpoints_per_block(cpfile));
 	return (unsigned long)tcno;
 }
 
diff --git a/fs/nilfs2/dat.c b/fs/nilfs2/dat.c
index 8f71f8b0e218..180fc8d36213 100644
--- a/fs/nilfs2/dat.c
+++ b/fs/nilfs2/dat.c
@@ -460,7 +460,7 @@ ssize_t nilfs_dat_get_vinfo(struct inode *dat, void *buf, unsigned int visz,
 		kaddr = kmap_local_page(entry_bh->b_page);
 		/* last virtual block number in this block */
 		first = vinfo->vi_vblocknr;
-		do_div(first, entries_per_block);
+		first = div64_ul(first, entries_per_block);
 		first *= entries_per_block;
 		last = first + entries_per_block - 1;
 		for (j = i, n = 0;
diff --git a/fs/nilfs2/ioctl.c b/fs/nilfs2/ioctl.c
index cfb6aca5ec38..f1a01c191cf5 100644
--- a/fs/nilfs2/ioctl.c
+++ b/fs/nilfs2/ioctl.c
@@ -1111,7 +1111,7 @@ static int nilfs_ioctl_set_alloc_range(struct inode *inode, void __user *argp)
 	segbytes = nilfs->ns_blocks_per_segment * nilfs->ns_blocksize;
 
 	minseg = range[0] + segbytes - 1;
-	do_div(minseg, segbytes);
+	minseg = div64_ul(minseg, segbytes);
 
 	if (range[1] < 4096)
 		goto out;
@@ -1120,7 +1120,7 @@ static int nilfs_ioctl_set_alloc_range(struct inode *inode, void __user *argp)
 	if (maxseg < segbytes)
 		goto out;
 
-	do_div(maxseg, segbytes);
+	maxseg = div64_ul(maxseg, segbytes);
 	maxseg--;
 
 	ret = nilfs_sufile_set_alloc_range(nilfs->ns_sufile, minseg, maxseg);
diff --git a/fs/nilfs2/sufile.c b/fs/nilfs2/sufile.c
index abf05dc5750c..6748218be7c5 100644
--- a/fs/nilfs2/sufile.c
+++ b/fs/nilfs2/sufile.c
@@ -48,7 +48,7 @@ nilfs_sufile_get_blkoff(const struct inode *sufile, __u64 segnum)
 {
 	__u64 t = segnum + NILFS_MDT(sufile)->mi_first_entry_offset;
 
-	do_div(t, nilfs_sufile_segment_usages_per_block(sufile));
+	t = div64_ul(t, nilfs_sufile_segment_usages_per_block(sufile));
 	return (unsigned long)t;
 }
 
diff --git a/fs/nilfs2/super.c b/fs/nilfs2/super.c
index 5e630c179a1e..ac24ed109ce9 100644
--- a/fs/nilfs2/super.c
+++ b/fs/nilfs2/super.c
@@ -448,7 +448,7 @@ int nilfs_resize_fs(struct super_block *sb, __u64 newsize)
 
 	sb2off = NILFS_SB2_OFFSET_BYTES(newsize);
 	newnsegs = sb2off >> nilfs->ns_blocksize_bits;
-	do_div(newnsegs, nilfs->ns_blocks_per_segment);
+	newnsegs = div64_ul(newnsegs, nilfs->ns_blocks_per_segment);
 
 	ret = nilfs_sufile_resize(nilfs->ns_sufile, newnsegs);
 	up_write(&nilfs->ns_segctor_sem);
diff --git a/fs/nilfs2/the_nilfs.c b/fs/nilfs2/the_nilfs.c
index 71400496ed36..2ae2c1bbf6d1 100644
--- a/fs/nilfs2/the_nilfs.c
+++ b/fs/nilfs2/the_nilfs.c
@@ -413,7 +413,7 @@ static u64 nilfs_max_segment_count(struct the_nilfs *nilfs)
 {
 	u64 max_count = U64_MAX;
 
-	do_div(max_count, nilfs->ns_blocks_per_segment);
+	max_count = div64_ul(max_count, nilfs->ns_blocks_per_segment);
 	return min_t(u64, max_count, ULONG_MAX);
 }
 
-- 
2.34.1


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

* Re: [PATCH] nilfs2: Use div64_ul() instead of do_div()
  2024-02-29 21:14       ` Thorsten Blum
@ 2024-03-06 14:45         ` Ryusuke Konishi
  0 siblings, 0 replies; 7+ messages in thread
From: Ryusuke Konishi @ 2024-03-06 14:45 UTC (permalink / raw)
  To: Thorsten Blum; +Cc: linux-nilfs, linux-kernel

On Fri, Mar 1, 2024 at 6:15 AM Thorsten Blum wrote:
>
>
> > On Feb 29, 2024, at 21:40, Thorsten Blum <thorsten.blum@toblux.com> wrote:
> >
> >> On Feb 29, 2024, at 20:41, Thorsten Blum <thorsten.blum@toblux.com> wrote:
> >>
> >>> On Feb 29, 2024, at 19:45, Ryusuke Konishi <konishi.ryusuke@gmail.com> wrote:
> >>>
> >>> All of the fixes in this patch seem to be correct, but this doesn't
> >>> cover nilfs_resize_fs(), nilfs_max_segment_count(), and
> >>> nilfs_sb2_bad_offset(), which also have do_div() that doesn't use the
> >>> return value.
> >>
> >> For nilfs_sb2_bad_offset(), where the dividend is u64 and the divisor is u32, we
> >> would need a dedicated function like div64_u32() that doesn't calculate the
> >> remainder, which doesn't seem to exist. What do you think?
> >
> > Never mind, there is div_u64(u64, u32). I'll submit a v2 shortly.
>
> I left nilfs_sb2_bad_offset() unchanged in v2 because div_u64() still calculates
> the remainder.
>
> Thorsten

I got it.

I reviewed and tested the v2 patch and it was fine, so I sent it to
the -mm tree.

Thanks,
Ryusuke Konishi

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

* Re: [PATCH] nilfs2: Use div64_ul() instead of do_div()
  2024-02-29 20:40     ` Thorsten Blum
@ 2024-02-29 21:14       ` Thorsten Blum
  2024-03-06 14:45         ` Ryusuke Konishi
  0 siblings, 1 reply; 7+ messages in thread
From: Thorsten Blum @ 2024-02-29 21:14 UTC (permalink / raw)
  To: Ryusuke Konishi; +Cc: linux-nilfs, linux-kernel


> On Feb 29, 2024, at 21:40, Thorsten Blum <thorsten.blum@toblux.com> wrote:
> 
>> On Feb 29, 2024, at 20:41, Thorsten Blum <thorsten.blum@toblux.com> wrote:
>> 
>>> On Feb 29, 2024, at 19:45, Ryusuke Konishi <konishi.ryusuke@gmail.com> wrote:
>>> 
>>> All of the fixes in this patch seem to be correct, but this doesn't
>>> cover nilfs_resize_fs(), nilfs_max_segment_count(), and
>>> nilfs_sb2_bad_offset(), which also have do_div() that doesn't use the
>>> return value.
>> 
>> For nilfs_sb2_bad_offset(), where the dividend is u64 and the divisor is u32, we
>> would need a dedicated function like div64_u32() that doesn't calculate the
>> remainder, which doesn't seem to exist. What do you think?
> 
> Never mind, there is div_u64(u64, u32). I'll submit a v2 shortly.

I left nilfs_sb2_bad_offset() unchanged in v2 because div_u64() still calculates
the remainder.

Thorsten

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

* Re: [PATCH] nilfs2: Use div64_ul() instead of do_div()
  2024-02-29 19:41   ` Thorsten Blum
@ 2024-02-29 20:40     ` Thorsten Blum
  2024-02-29 21:14       ` Thorsten Blum
  0 siblings, 1 reply; 7+ messages in thread
From: Thorsten Blum @ 2024-02-29 20:40 UTC (permalink / raw)
  To: Ryusuke Konishi; +Cc: linux-nilfs, linux-kernel


> On Feb 29, 2024, at 20:41, Thorsten Blum <thorsten.blum@toblux.com> wrote:
> 
>> On Feb 29, 2024, at 19:45, Ryusuke Konishi <konishi.ryusuke@gmail.com> wrote:
>> 
>> All of the fixes in this patch seem to be correct, but this doesn't
>> cover nilfs_resize_fs(), nilfs_max_segment_count(), and
>> nilfs_sb2_bad_offset(), which also have do_div() that doesn't use the
>> return value.
> 
> For nilfs_sb2_bad_offset(), where the dividend is u64 and the divisor is u32, we
> would need a dedicated function like div64_u32() that doesn't calculate the
> remainder, which doesn't seem to exist. What do you think?

Never mind, there is div_u64(u64, u32). I'll submit a v2 shortly.

Thorsten

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

* Re: [PATCH] nilfs2: Use div64_ul() instead of do_div()
  2024-02-29 18:45 ` Ryusuke Konishi
@ 2024-02-29 19:41   ` Thorsten Blum
  2024-02-29 20:40     ` Thorsten Blum
  0 siblings, 1 reply; 7+ messages in thread
From: Thorsten Blum @ 2024-02-29 19:41 UTC (permalink / raw)
  To: Ryusuke Konishi; +Cc: linux-nilfs, linux-kernel


> On Feb 29, 2024, at 19:45, Ryusuke Konishi <konishi.ryusuke@gmail.com> wrote:
> 
> All of the fixes in this patch seem to be correct, but this doesn't
> cover nilfs_resize_fs(), nilfs_max_segment_count(), and
> nilfs_sb2_bad_offset(), which also have do_div() that doesn't use the
> return value.

I just tested this, and Coccinelle didn't report nilfs_resize_fs() or
nilfs_max_segment_count() because both divisors are fields of a struct. I will
refactor this and submit a v2.

For nilfs_sb2_bad_offset(), where the dividend is u64 and the divisor is u32, we
would need a dedicated function like div64_u32() that doesn't calculate the
remainder, which doesn't seem to exist. What do you think?

Thanks,
Thorsten

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

* Re: [PATCH] nilfs2: Use div64_ul() instead of do_div()
  2024-02-29 12:16 Thorsten Blum
@ 2024-02-29 18:45 ` Ryusuke Konishi
  2024-02-29 19:41   ` Thorsten Blum
  0 siblings, 1 reply; 7+ messages in thread
From: Ryusuke Konishi @ 2024-02-29 18:45 UTC (permalink / raw)
  To: Thorsten Blum; +Cc: linux-nilfs, linux-kernel

On Thu, Feb 29, 2024 at 9:19 PM Thorsten Blum wrote:
>
> Fixes Coccinelle/coccicheck warning reported by do_div.cocci.
>
> Compared to do_div(), div64_ul() does not implicitly cast the divisor and
> does not unnecessarily calculate the remainder.
>
> Signed-off-by: Thorsten Blum <thorsten.blum@toblux.com>
> ---
>  fs/nilfs2/cpfile.c | 2 +-
>  fs/nilfs2/dat.c    | 2 +-
>  fs/nilfs2/ioctl.c  | 4 ++--
>  fs/nilfs2/sufile.c | 2 +-
>  4 files changed, 5 insertions(+), 5 deletions(-)
>
> diff --git a/fs/nilfs2/cpfile.c b/fs/nilfs2/cpfile.c
> index 39136637f715..bafbdca1a17d 100644
> --- a/fs/nilfs2/cpfile.c
> +++ b/fs/nilfs2/cpfile.c
> @@ -28,7 +28,7 @@ nilfs_cpfile_get_blkoff(const struct inode *cpfile, __u64 cno)
>  {
>         __u64 tcno = cno + NILFS_MDT(cpfile)->mi_first_entry_offset - 1;
>
> -       do_div(tcno, nilfs_cpfile_checkpoints_per_block(cpfile));
> +       tcno = div64_ul(tcno, nilfs_cpfile_checkpoints_per_block(cpfile));
>         return (unsigned long)tcno;
>  }
>
> diff --git a/fs/nilfs2/dat.c b/fs/nilfs2/dat.c
> index 9cf6ba58f585..df5324b0c0cd 100644
> --- a/fs/nilfs2/dat.c
> +++ b/fs/nilfs2/dat.c
> @@ -460,7 +460,7 @@ ssize_t nilfs_dat_get_vinfo(struct inode *dat, void *buf, unsigned int visz,
>                 kaddr = kmap_atomic(entry_bh->b_page);
>                 /* last virtual block number in this block */
>                 first = vinfo->vi_vblocknr;
> -               do_div(first, entries_per_block);
> +               first = div64_ul(first, entries_per_block);
>                 first *= entries_per_block;
>                 last = first + entries_per_block - 1;
>                 for (j = i, n = 0;
> diff --git a/fs/nilfs2/ioctl.c b/fs/nilfs2/ioctl.c
> index cfb6aca5ec38..f1a01c191cf5 100644
> --- a/fs/nilfs2/ioctl.c
> +++ b/fs/nilfs2/ioctl.c
> @@ -1111,7 +1111,7 @@ static int nilfs_ioctl_set_alloc_range(struct inode *inode, void __user *argp)
>         segbytes = nilfs->ns_blocks_per_segment * nilfs->ns_blocksize;
>
>         minseg = range[0] + segbytes - 1;
> -       do_div(minseg, segbytes);
> +       minseg = div64_ul(minseg, segbytes);
>
>         if (range[1] < 4096)
>                 goto out;
> @@ -1120,7 +1120,7 @@ static int nilfs_ioctl_set_alloc_range(struct inode *inode, void __user *argp)
>         if (maxseg < segbytes)
>                 goto out;
>
> -       do_div(maxseg, segbytes);
> +       maxseg = div64_ul(maxseg, segbytes);
>         maxseg--;
>
>         ret = nilfs_sufile_set_alloc_range(nilfs->ns_sufile, minseg, maxseg);
> diff --git a/fs/nilfs2/sufile.c b/fs/nilfs2/sufile.c
> index 0a8119456c21..c02b523d9c7e 100644
> --- a/fs/nilfs2/sufile.c
> +++ b/fs/nilfs2/sufile.c
> @@ -48,7 +48,7 @@ nilfs_sufile_get_blkoff(const struct inode *sufile, __u64 segnum)
>  {
>         __u64 t = segnum + NILFS_MDT(sufile)->mi_first_entry_offset;
>
> -       do_div(t, nilfs_sufile_segment_usages_per_block(sufile));
> +       t = div64_ul(t, nilfs_sufile_segment_usages_per_block(sufile));
>         return (unsigned long)t;
>  }
>
> --
> 2.44.0
>

All of the fixes in this patch seem to be correct, but this doesn't
cover nilfs_resize_fs(), nilfs_max_segment_count(), and
nilfs_sb2_bad_offset(), which also have do_div() that doesn't use the
return value.

Did do_div.cocci make a difference ?

Thanks,
Ryusuke Konishi

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

* [PATCH] nilfs2: Use div64_ul() instead of do_div()
@ 2024-02-29 12:16 Thorsten Blum
  2024-02-29 18:45 ` Ryusuke Konishi
  0 siblings, 1 reply; 7+ messages in thread
From: Thorsten Blum @ 2024-02-29 12:16 UTC (permalink / raw)
  To: Ryusuke Konishi; +Cc: linux-nilfs, linux-kernel, Thorsten Blum

Fixes Coccinelle/coccicheck warning reported by do_div.cocci.

Compared to do_div(), div64_ul() does not implicitly cast the divisor and
does not unnecessarily calculate the remainder.

Signed-off-by: Thorsten Blum <thorsten.blum@toblux.com>
---
 fs/nilfs2/cpfile.c | 2 +-
 fs/nilfs2/dat.c    | 2 +-
 fs/nilfs2/ioctl.c  | 4 ++--
 fs/nilfs2/sufile.c | 2 +-
 4 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/fs/nilfs2/cpfile.c b/fs/nilfs2/cpfile.c
index 39136637f715..bafbdca1a17d 100644
--- a/fs/nilfs2/cpfile.c
+++ b/fs/nilfs2/cpfile.c
@@ -28,7 +28,7 @@ nilfs_cpfile_get_blkoff(const struct inode *cpfile, __u64 cno)
 {
 	__u64 tcno = cno + NILFS_MDT(cpfile)->mi_first_entry_offset - 1;
 
-	do_div(tcno, nilfs_cpfile_checkpoints_per_block(cpfile));
+	tcno = div64_ul(tcno, nilfs_cpfile_checkpoints_per_block(cpfile));
 	return (unsigned long)tcno;
 }
 
diff --git a/fs/nilfs2/dat.c b/fs/nilfs2/dat.c
index 9cf6ba58f585..df5324b0c0cd 100644
--- a/fs/nilfs2/dat.c
+++ b/fs/nilfs2/dat.c
@@ -460,7 +460,7 @@ ssize_t nilfs_dat_get_vinfo(struct inode *dat, void *buf, unsigned int visz,
 		kaddr = kmap_atomic(entry_bh->b_page);
 		/* last virtual block number in this block */
 		first = vinfo->vi_vblocknr;
-		do_div(first, entries_per_block);
+		first = div64_ul(first, entries_per_block);
 		first *= entries_per_block;
 		last = first + entries_per_block - 1;
 		for (j = i, n = 0;
diff --git a/fs/nilfs2/ioctl.c b/fs/nilfs2/ioctl.c
index cfb6aca5ec38..f1a01c191cf5 100644
--- a/fs/nilfs2/ioctl.c
+++ b/fs/nilfs2/ioctl.c
@@ -1111,7 +1111,7 @@ static int nilfs_ioctl_set_alloc_range(struct inode *inode, void __user *argp)
 	segbytes = nilfs->ns_blocks_per_segment * nilfs->ns_blocksize;
 
 	minseg = range[0] + segbytes - 1;
-	do_div(minseg, segbytes);
+	minseg = div64_ul(minseg, segbytes);
 
 	if (range[1] < 4096)
 		goto out;
@@ -1120,7 +1120,7 @@ static int nilfs_ioctl_set_alloc_range(struct inode *inode, void __user *argp)
 	if (maxseg < segbytes)
 		goto out;
 
-	do_div(maxseg, segbytes);
+	maxseg = div64_ul(maxseg, segbytes);
 	maxseg--;
 
 	ret = nilfs_sufile_set_alloc_range(nilfs->ns_sufile, minseg, maxseg);
diff --git a/fs/nilfs2/sufile.c b/fs/nilfs2/sufile.c
index 0a8119456c21..c02b523d9c7e 100644
--- a/fs/nilfs2/sufile.c
+++ b/fs/nilfs2/sufile.c
@@ -48,7 +48,7 @@ nilfs_sufile_get_blkoff(const struct inode *sufile, __u64 segnum)
 {
 	__u64 t = segnum + NILFS_MDT(sufile)->mi_first_entry_offset;
 
-	do_div(t, nilfs_sufile_segment_usages_per_block(sufile));
+	t = div64_ul(t, nilfs_sufile_segment_usages_per_block(sufile));
 	return (unsigned long)t;
 }
 
-- 
2.44.0


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

end of thread, other threads:[~2024-03-06 14:46 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-03-06 14:25 [PATCH] nilfs2: Use div64_ul() instead of do_div() Ryusuke Konishi
  -- strict thread matches above, loose matches on Subject: below --
2024-02-29 12:16 Thorsten Blum
2024-02-29 18:45 ` Ryusuke Konishi
2024-02-29 19:41   ` Thorsten Blum
2024-02-29 20:40     ` Thorsten Blum
2024-02-29 21:14       ` Thorsten Blum
2024-03-06 14:45         ` Ryusuke Konishi

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).