diff mbox series

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

Message ID 1621930520-515336-2-git-send-email-wubo40@huawei.com (mailing list archive)
State New, archived
Headers show
Series use DIV_ROUND_UP helper macro for calculations | expand

Commit Message

Wu Bo May 25, 2021, 8:15 a.m. UTC
From: Wu Bo <wubo40@huawei.com>

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

Signed-off-by: Wu Bo <wubo40@huawei.com>
---
 crypto/af_alg.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Christophe Leroy May 25, 2021, 8:37 a.m. UTC | #1
Wu Bo <wubo40@huawei.com> a écrit :

> From: Wu Bo <wubo40@huawei.com>
>
> Replace open coded divisor calculations with the DIV_ROUND_UP kernel
> macro for better readability.
>
> Signed-off-by: Wu Bo <wubo40@huawei.com>
> ---
>  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
Dave Chinner May 26, 2021, 8:31 a.m. UTC | #2
On Tue, May 25, 2021 at 10:37:44AM +0200, Christophe Leroy wrote:
> Wu Bo <wubo40@huawei.com> a écrit :
> 
> > From: Wu Bo <wubo40@huawei.com>
> > 
> > Replace open coded divisor calculations with the DIV_ROUND_UP kernel
> > macro for better readability.
> > 
> > Signed-off-by: Wu Bo <wubo40@huawei.com>
> > ---
> >  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.
Herbert Xu June 3, 2021, 12:30 p.m. UTC | #3
On Tue, May 25, 2021 at 04:15:19PM +0800, Wu Bo wrote:
> From: Wu Bo <wubo40@huawei.com>
> 
> Replace open coded divisor calculations with the DIV_ROUND_UP kernel
> macro for better readability.
> 
> Signed-off-by: Wu Bo <wubo40@huawei.com>
> ---
>  crypto/af_alg.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)

Patch applied.  Thanks.
diff mbox series

Patch

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 */