[PATCH 0/2] use DIV_ROUND_UP helper macro for calculations

STALE1902d

8 messages, 4 authors, 2021-06-03 · open the first message on its own page

[PATCH 0/2] use DIV_ROUND_UP helper macro for calculations

From: Wu Bo <hidden>
Date: 2021-05-25 08:08:27

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

-- 
1.8.3.1

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

From: Wu Bo <hidden>
Date: 2021-05-25 08:08:30

From: Wu Bo <redacted>

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

Signed-off-by: Wu Bo <redacted>
---
 crypto/af_alg.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/crypto/af_alg.c b/crypto/af_alg.c
index 18cc82d..8bd288d 100644
--- a/crypto/af_alg.c
+++ b/crypto/af_alg.c
@@ -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 */
-- 
1.8.3.1

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

From: Wu Bo <hidden>
Date: 2021-05-25 08:08:36

From: Wu Bo <redacted>

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

Signed-off-by: Wu Bo <redacted>
---
 fs/direct-io.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/fs/direct-io.c b/fs/direct-io.c
index b2e86e7..6e7d402 100644
--- a/fs/direct-io.c
+++ b/fs/direct-io.c
@@ -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;
 	}
-- 
1.8.3.1

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

From: Christophe Leroy <hidden>
Date: 2021-05-25 08:34:07

Wu Bo [off-list ref] a écrit :
quoted hunk
From: Wu Bo <redacted>

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

Signed-off-by: Wu Bo <redacted>
---
 crypto/af_alg.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/crypto/af_alg.c b/crypto/af_alg.c
index 18cc82d..8bd288d 100644
--- a/crypto/af_alg.c
+++ b/crypto/af_alg.c
@@ -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()
 	if (WARN_ON(npages == 0))
 		return -EINVAL;
 	/* Add one extra for linking */
--
1.8.3.1

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

From: Christophe Leroy <hidden>
Date: 2021-05-25 08:34:14

Wu Bo [off-list ref] a écrit :
quoted hunk
From: Wu Bo <redacted>

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

Signed-off-by: Wu Bo <redacted>
---
 fs/direct-io.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/fs/direct-io.c b/fs/direct-io.c
index b2e86e7..6e7d402 100644
--- a/fs/direct-io.c
+++ b/fs/direct-io.c
@@ -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.

 		sdio->to = ((ret - 1) & (PAGE_SIZE - 1)) + 1;
 		return 0;
 	}
--
1.8.3.1

Re: [PATCH 0/2] use DIV_ROUND_UP helper macro for calculations

From: Christophe Leroy <hidden>
Date: 2021-05-25 08:36:38

Wu Bo [off-list ref] a écrit :
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.
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

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

From: Dave Chinner <david@fromorbit.com>
Date: 2021-05-26 08:31:45

On Tue, May 25, 2021 at 10:37:44AM +0200, Christophe Leroy wrote:
Wu Bo [off-list ref] a écrit :
quoted
From: Wu Bo <redacted>

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

Signed-off-by: Wu Bo <redacted>
---
 crypto/af_alg.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/crypto/af_alg.c b/crypto/af_alg.c
index 18cc82d..8bd288d 100644
--- a/crypto/af_alg.c
+++ b/crypto/af_alg.c
@@ -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()
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.
-- 
Dave Chinner
david@fromorbit.com

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

From: Herbert Xu <herbert@gondor.apana.org.au>
Date: 2021-06-03 12:30:17

On Tue, May 25, 2021 at 04:15:19PM +0800, Wu Bo wrote:
From: Wu Bo <redacted>

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

Signed-off-by: Wu Bo <redacted>
---
 crypto/af_alg.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Patch applied.  Thanks.
-- 
Email: Herbert Xu [off-list ref]
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help