Skip to content

Commit

Permalink
block: Do not call sector_div() with a 64-bit divisor
Browse files Browse the repository at this point in the history
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
ea077b1 ("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 <[email protected]>
Signed-off-by: Jens Axboe <[email protected]>
  • Loading branch information
geertu authored and axboe committed Nov 8, 2013
1 parent f8c5e94 commit 97597dc
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions block/blk-lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ int blkdev_issue_discard(struct block_device *bdev, sector_t sector,
DECLARE_COMPLETION_ONSTACK(wait);
struct request_queue *q = bdev_get_queue(bdev);
int type = REQ_WRITE | REQ_DISCARD;
sector_t max_discard_sectors;
sector_t granularity, alignment;
unsigned int max_discard_sectors, granularity;
int alignment;
struct bio_batch bb;
struct bio *bio;
int ret = 0;
Expand All @@ -58,16 +58,14 @@ int blkdev_issue_discard(struct block_device *bdev, sector_t sector,

/* Zero-sector (unknown) and one-sector granularities are the same. */
granularity = max(q->limits.discard_granularity >> 9, 1U);
alignment = bdev_discard_alignment(bdev) >> 9;
alignment = sector_div(alignment, granularity);
alignment = (bdev_discard_alignment(bdev) >> 9) % granularity;

/*
* Ensure that max_discard_sectors is of the proper
* granularity, so that requests stay aligned after a split.
*/
max_discard_sectors = min(q->limits.max_discard_sectors, UINT_MAX >> 9);
sector_div(max_discard_sectors, granularity);
max_discard_sectors *= granularity;
max_discard_sectors -= max_discard_sectors % granularity;
if (unlikely(!max_discard_sectors)) {
/* Avoid infinite loop below. Being cautious never hurts. */
return -EOPNOTSUPP;
Expand Down

0 comments on commit 97597dc

Please sign in to comment.