Commit 97597dc08f58e25bc74154b7d1c387a4c0432950

Authored by Geert Uytterhoeven
Committed by Jens Axboe
1 parent f8c5e94486

block: Do not call sector_div() with a 64-bit divisor

do_div() (called by sector_div() if CONFIG_LBDAF=y) is meant for divisions
of 64-bit number by 32-bit numbers.  Passing 64-bit divisor types caused
issues in the past on 32-bit platforms, cfr. commit
ea077b1b96e073eac5c3c5590529e964767fc5f7 ("m68k: Truncate base in
do_div()").

As queue_limits.max_discard_sectors and .discard_granularity are unsigned
int, max_discard_sectors and granularity should be unsigned int.
As bdev_discard_alignment() returns int, alignment should be int.
Now 2 calls to sector_div() can be replaced by 32-bit arithmetic:
  - The 64-bit modulo operation can become a 32-bit modulo operation,
  - The 64-bit division and multiplication can be replaced by a 32-bit
    modulo operation and a subtraction.

Signed-off-by: Geert Uytterhoeven <geert@linux-m68k.org>
Signed-off-by: Jens Axboe <axboe@kernel.dk>

Showing 1 changed file with 4 additions and 6 deletions Side-by-side Diff

... ... @@ -43,8 +43,8 @@
43 43 DECLARE_COMPLETION_ONSTACK(wait);
44 44 struct request_queue *q = bdev_get_queue(bdev);
45 45 int type = REQ_WRITE | REQ_DISCARD;
46   - sector_t max_discard_sectors;
47   - sector_t granularity, alignment;
  46 + unsigned int max_discard_sectors, granularity;
  47 + int alignment;
48 48 struct bio_batch bb;
49 49 struct bio *bio;
50 50 int ret = 0;
51 51  
... ... @@ -58,16 +58,14 @@
58 58  
59 59 /* Zero-sector (unknown) and one-sector granularities are the same. */
60 60 granularity = max(q->limits.discard_granularity >> 9, 1U);
61   - alignment = bdev_discard_alignment(bdev) >> 9;
62   - alignment = sector_div(alignment, granularity);
  61 + alignment = (bdev_discard_alignment(bdev) >> 9) % granularity;
63 62  
64 63 /*
65 64 * Ensure that max_discard_sectors is of the proper
66 65 * granularity, so that requests stay aligned after a split.
67 66 */
68 67 max_discard_sectors = min(q->limits.max_discard_sectors, UINT_MAX >> 9);
69   - sector_div(max_discard_sectors, granularity);
70   - max_discard_sectors *= granularity;
  68 + max_discard_sectors -= max_discard_sectors % granularity;
71 69 if (unlikely(!max_discard_sectors)) {
72 70 /* Avoid infinite loop below. Being cautious never hurts. */
73 71 return -EOPNOTSUPP;