Skip to contentSkip to conversation

block/merge: remove unnecessary min() with UINT_MAX

PATCH Applied Caleb Sander Mateos 4 participants 5 messages Feb 14, 2025 – Feb 16, 2025 Block layer

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 block/merge: remove unnecessary min() with UINT_MAX

In bvec_split_segs(), max_bytes is an unsigned, so it must be less than or equal to UINT_MAX. Remove the unnecessary min().

Prior to commit 67927d220150 ("block/merge: count bytes instead of sectors"), the min() was with UINT_MAX >> 9, so it did have an effect.

---
 block/blk-merge.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
block/blk-merge.c +1 -1
index 15cd231d560c..39b738c0e4c9 100644
@@ -268,11 +268,11 @@ static inline unsigned get_max_segment_size(const struct queue_limits *lim,
  */
 static bool bvec_split_segs(const struct queue_limits *lim,
 		const struct bio_vec *bv, unsigned *nsegs, unsigned *bytes,
 		unsigned max_segs, unsigned max_bytes)
 {
-	unsigned max_len = min(max_bytes, UINT_MAX) - *bytes;
+	unsigned max_len = max_bytes - *bytes;
 	unsigned len = min(bv->bv_len, max_len);
 	unsigned total_len = 0;
 	unsigned seg_size = 0;
 
 	while (len && *nsegs < max_segs) {
Signature · 1 line
2.45.2
maintainer Applied
#1 lore raw
Quoted 8 lines · Caleb Sander Mateos

In bvec_split_segs(), max_bytes is an unsigned, so it must be less than or equal to UINT_MAX. Remove the unnecessary min().

Prior to commit 67927d220150 ("block/merge: count bytes instead of sectors"), the min() was with UINT_MAX >> 9, so it did have an effect.

[...]

Applied, thanks!

[1/1] block/merge: remove unnecessary min() with UINT_MAX

      commit: 43c70b104093c324b1a000762ce943d16ce788f9

Best regards,

Signature · 1 line
Jens Axboe
#3 lore raw
In reply to Caleb Sander Mateos
Quoted 16 lines · On Fri, 14 Feb 2025 12:36:36 -0700 Caleb Sander Mateos…

In bvec_split_segs(), max_bytes is an unsigned, so it must be less than or equal to UINT_MAX. Remove the unnecessary min().

Prior to commit 67927d220150 ("block/merge: count bytes instead of sectors"), the min() was with UINT_MAX >> 9, so it did have an effect.

---
 block/blk-merge.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
block/blk-merge.c +0 -0
index 15cd231d560c..39b738c0e4c9 100644

block/blk-merge.c static inline unsigned get_max_segment_size(const struct queue_limits *lim, · line 273

Quoted 7 lines
block/blk-merge.c +1 -1
@@ -268,11 +268,11 @@ static inline unsigned get_max_segment_size(const struct queue_limits *lim,
  */
 static bool bvec_split_segs(const struct queue_limits *lim,
 		const struct bio_vec *bv, unsigned *nsegs, unsigned *bytes,
 		unsigned max_segs, unsigned max_bytes)
 {
-	unsigned max_len = min(max_bytes, UINT_MAX) - *bytes;
+	unsigned max_len = max_bytes - *bytes;

More interestingly, what stops *bytes being larger than max_bytes?

David

Quoted 5 lines
block/blk-merge.c +0 -0
 	unsigned len = min(bv->bv_len, max_len);
 	unsigned total_len = 0;
 	unsigned seg_size = 0;
 
 	while (len && *nsegs < max_segs) {
#4 lore raw
Quoted 15 lines · David Laight
Quoted 11 lines · On Fri, 14 Feb 2025 12:36:36 -0700 Caleb Sander Mateos…

In bvec_split_segs(), max_bytes is an unsigned, so it must be less than or equal to UINT_MAX. Remove the unnecessary min().

Prior to commit 67927d220150 ("block/merge: count bytes instead of sectors"), the min() was with UINT_MAX >> 9, so it did have an effect.

---
 block/blk-merge.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 14 lines

block/blk-merge.c static inline unsigned get_max_segment_size(const struct queue_limits *lim, · line 273

Quoted 12 lines
block/blk-merge.c +1 -1
index 15cd231d560c..39b738c0e4c9 100644
@@ -268,11 +268,11 @@ static inline unsigned get_max_segment_size(const struct queue_limits *lim,
  */
 static bool bvec_split_segs(const struct queue_limits *lim,
              const struct bio_vec *bv, unsigned *nsegs, unsigned *bytes,
              unsigned max_segs, unsigned max_bytes)
 {
-     unsigned max_len = min(max_bytes, UINT_MAX) - *bytes;
+     unsigned max_len = max_bytes - *bytes;

More interestingly, what stops *bytes being larger than max_bytes?

bvec_split_segs() has two callers, bio_split_rw_at() and blk_recalc_rq_segments(). In both, *bytes = 0 originally. bio_split_rw_at() has another code path which increments *bytes, but it makes sure not to exceed max_bytes. So when bvec_split_segs() is called, *bytes <= max_bytes. The logic in bvec_split_segs() won't increment *bytes by more than len, which is at most max_len = max_bytes - *bytes. So inductively, *bytes <= max_bytes before every call to bvec_split_segs().

Quoted 9 lines

David

Quoted 5 lines
block/blk-merge.c +0 -0
      unsigned len = min(bv->bv_len, max_len);
      unsigned total_len = 0;
      unsigned seg_size = 0;
 
      while (len && *nsegs < max_segs) {