Skip to contentSkip to conversation

use DIV_ROUND_UP helper macro for calculations

PATCH In review Wu Bo 4 participants 8 messages May 25, 2021 – Jun 3, 2021

Subsystems (2)
lore
Share & tools
lore

Wu Bo

author
#0 lore raw

cover use DIV_ROUND_UP helper macro for calculations

This patchset is replace open coded divisor calculations with the DIV_ROUND_UP kernel macro for better readability.

Wu Bo (2):
crypto: af_alg - use DIV_ROUND_UP helper macro for calculations
fs: direct-io: use DIV_ROUND_UP helper macro for calculations

 crypto/af_alg.c | 2 +-
 fs/direct-io.c  | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
Signature · 1 line
1.8.3.1
author
#1 lore raw

PATCH 1/2 crypto: af_alg - use DIV_ROUND_UP helper macro for calculations

From: Wu Bo <[email protected]>

Replace open coded divisor calculations with the DIV_ROUND_UP kernel macro for better readability.

---
 crypto/af_alg.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
crypto/af_alg.c +1 -1
index 18cc82d..8bd288d 100644
@@ -411,7 +411,7 @@ int af_alg_make_sg(struct af_alg_sgl *sgl, struct iov_iter *iter, int len)
 	if (n < 0)
 		return n;
 
-	npages = (off + n + PAGE_SIZE - 1) >> PAGE_SHIFT;
+	npages = DIV_ROUND_UP(off + n, PAGE_SIZE);
 	if (WARN_ON(npages == 0))
 		return -EINVAL;
 	/* Add one extra for linking */
Signature · 1 line
1.8.3.1
#2 lore raw

Re: [PATCH 1/2] crypto: af_alg - use DIV_ROUND_UP helper macro for calculations

Wu Bo <[email protected]> a écrit :

Quoted 15 lines

From: Wu Bo <[email protected]>

Replace open coded divisor calculations with the DIV_ROUND_UP kernel macro for better readability.

---
 crypto/af_alg.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
crypto/af_alg.c +0 -0
index 18cc82d..8bd288d 100644

crypto/af_alg.c int af_alg_make_sg(struct af_alg_sgl *sgl, · line 415

Quoted 6 lines
crypto/af_alg.c +1 -1
@@ -411,7 +411,7 @@ int af_alg_make_sg(struct af_alg_sgl *sgl,  
 struct iov_iter *iter, int len)
 	if (n < 0)
 		return n;
 
-	npages = (off + n + PAGE_SIZE - 1) >> PAGE_SHIFT;
+	npages = DIV_ROUND_UP(off + n, PAGE_SIZE);

You should use PFN_UP()

Quoted 5 lines
crypto/af_alg.c +0 -0
 	if (WARN_ON(npages == 0))
 		return -EINVAL;

/* Add one extra for linking */
--
1.8.3.1

#3 lore raw

Re: [PATCH 1/2] crypto: af_alg - use DIV_ROUND_UP helper macro for calculations

Quoted 21 lines · Christophe Leroy

Wu Bo <[email protected]> a écrit :

Quoted 19 lines

From: Wu Bo <[email protected]>

Replace open coded divisor calculations with the DIV_ROUND_UP kernel macro for better readability.

---
 crypto/af_alg.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
crypto/af_alg.c +0 -0
index 18cc82d..8bd288d 100644
@@ -411,7 +411,7 @@ int af_alg_make_sg(struct af_alg_sgl *sgl, struct
 iov_iter *iter, int len)
 	if (n < 0)
 		return n;
 
Quoted 4 lines

crypto/af_alg.c int af_alg_make_sg(struct af_alg_sgl *sgl, struct iov_iter *iter, int len) · line 414

Quoted 2 lines
crypto/af_alg.c +1 -1
-	npages = (off + n + PAGE_SIZE - 1) >> PAGE_SHIFT;
+	npages = DIV_ROUND_UP(off + n, PAGE_SIZE);

You should use PFN_UP()

No. We are not using pfns here - we're converting a byte count to a page count.

Besides, "PFN_UP" is a horrible, awful api. It does not decribe what it does and anyone who is not a mm developer will look at it and ask "what <the ....> does this do?" and have to go looking for it's definition to determine what it does. Yes, that's exactyl what I've just done, and I really wish I didn't because, well, it just reinforces how much we suck at APIs...

OTOH, what DIV_ROUND_UP() does is obvious, widely understood, self documenting and easy to determine if the usage is correct, which indeed this is.

The lesson: do not use whacky obscure, out of context macros when a simple, obvious, widely known macro will give the same result and make the code easier to understand.

Cheers,

Dave.

Signature · 2 lines
Dave Chinner
[email protected]
maintainer
#4 lore raw

Re: [PATCH 1/2] crypto: af_alg - use DIV_ROUND_UP helper macro for calculations

In reply to Wu Bo
Quoted 9 lines · Wu Bo

From: Wu Bo <[email protected]>

Replace open coded divisor calculations with the DIV_ROUND_UP kernel macro for better readability.

---
 crypto/af_alg.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Patch applied. Thanks.

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
author
#5 lore raw

PATCH 2/2 fs: direct-io: use DIV_ROUND_UP helper macro for calculations

In reply to Wu Bo

From: Wu Bo <[email protected]>

Replace open coded divisor calculations with the DIV_ROUND_UP kernel macro for better readability.

---
 fs/direct-io.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/direct-io.c +1 -1
index b2e86e7..6e7d402 100644
@@ -195,7 +195,7 @@ static inline int dio_refill_pages(struct dio *dio, struct dio_submit *sdio)
 		iov_iter_advance(sdio->iter, ret);
 		ret += sdio->from;
 		sdio->head = 0;
-		sdio->tail = (ret + PAGE_SIZE - 1) / PAGE_SIZE;
+		sdio->tail = DIV_ROUND_UP(ret, PAGE_SIZE);
 		sdio->to = ((ret - 1) & (PAGE_SIZE - 1)) + 1;
 		return 0;
 	}
Signature · 1 line
1.8.3.1
#6 lore raw

Re: [PATCH 2/2] fs: direct-io: use DIV_ROUND_UP helper macro for calculations

Wu Bo <[email protected]> a écrit :

Quoted 15 lines

From: Wu Bo <[email protected]>

Replace open coded divisor calculations with the DIV_ROUND_UP kernel macro for better readability.

---
 fs/direct-io.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/direct-io.c +0 -0
index b2e86e7..6e7d402 100644

fs/direct-io.c static inline int dio_refill_pages(struct dio · line 199

Quoted 6 lines
fs/direct-io.c +1 -1
@@ -195,7 +195,7 @@ static inline int dio_refill_pages(struct dio  
 *dio, struct dio_submit *sdio)
 		iov_iter_advance(sdio->iter, ret);
 		ret += sdio->from;
 		sdio->head = 0;
-		sdio->tail = (ret + PAGE_SIZE - 1) / PAGE_SIZE;
+		sdio->tail = DIV_ROUND_UP(ret, PAGE_SIZE);

Use PFN_UP() instead.

Quoted 5 lines
fs/direct-io.c +0 -0
 		sdio->to = ((ret - 1) & (PAGE_SIZE - 1)) + 1;
 		return 0;

}
--
1.8.3.1

#7 lore raw
In reply to Wu Bo

Wu Bo <[email protected]> a écrit :

Quoted 2 lines

This patchset is replace open coded divisor calculations with the DIV_ROUND_UP kernel macro for better readability.

We call it a series not a patchset.

PFN_UP() from pfn.h should be used instead of DIV_ROUND_UP() I believe.

Quoted 11 lines

Wu Bo (2):
crypto: af_alg - use DIV_ROUND_UP helper macro for calculations
fs: direct-io: use DIV_ROUND_UP helper macro for calculations

 crypto/af_alg.c | 2 +-
 fs/direct-io.c  | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

--
1.8.3.1