* [PATCH v2] kernel.h: Documentation for roundup() vs round_up()
@ 2018-07-03 2:16 Kees Cook
2018-07-03 2:28 ` Adam Borowski
0 siblings, 1 reply; 2+ messages in thread
From: Kees Cook @ 2018-07-03 2:16 UTC (permalink / raw)
To: Andrew Morton
Cc: Matthew Wilcox, Eric Biggers, Randy Dunlap, linux-doc,
linux-kernel
Things like commit 3619dec5103d ("dh key: fix rounding up KDF output
length") expose the lack of explicit documentation for roundup() vs
round_up(). At least we can try to document it better if anyone goes
looking.
Signed-off-by: Kees Cook <keescook@chromium.org>
---
v2: fix whitespace (willy)
---
include/linux/kernel.h | 35 ++++++++++++++++++++++++++++++++++-
1 file changed, 34 insertions(+), 1 deletion(-)
diff --git a/include/linux/kernel.h b/include/linux/kernel.h
index 941dc0a5a877..dce6ab779a83 100644
--- a/include/linux/kernel.h
+++ b/include/linux/kernel.h
@@ -85,7 +85,23 @@
* arguments just once each.
*/
#define __round_mask(x, y) ((__typeof__(x))((y)-1))
+/**
+ * round_up - round up to next specified power of 2
+ * @x: the value to round
+ * @y: multiple to round up to (must be a power of 2)
+ *
+ * Rounds @x up to next multiple of @y (which must be a power of 2).
+ * To perform arbitrary rounding up, use roundup() below.
+ */
#define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
+/**
+ * round_down - round down to next specified power of 2
+ * @x: the value to round
+ * @y: multiple to round down to (must be a power of 2)
+ *
+ * Rounds @x down to next multiple of @y (which must be a power of 2).
+ * To perform arbitrary rounding down, use rounddown() below.
+ */
#define round_down(x, y) ((x) & ~__round_mask(x, y))
/**
@@ -110,13 +126,30 @@
# define DIV_ROUND_UP_SECTOR_T(ll,d) DIV_ROUND_UP(ll,d)
#endif
-/* The `const' in roundup() prevents gcc-3.3 from calling __divdi3 */
+/**
+ * roundup - round up to the next specified multiple
+ * @x: the value to up
+ * @y: multiple to round up to (must be a power of 2)
+ *
+ * Rounds @x up to next multiple of @y. If @y will always be a power
+ * of 2, consider using the faster round_up().
+ *
+ * The `const' here prevents gcc-3.3 from calling __divdi3
+ */
#define roundup(x, y) ( \
{ \
const typeof(y) __y = y; \
(((x) + (__y - 1)) / __y) * __y; \
} \
)
+/**
+ * rounddown - round down to next specified multiple
+ * @x: the value to round
+ * @y: multiple to round down to
+ *
+ * Rounds @x down to next multiple of @y. If @y will always be a power
+ * of 2, consider using the faster round_down().
+ */
#define rounddown(x, y) ( \
{ \
typeof(x) __x = (x); \
--
2.17.1
--
Kees Cook
Pixel Security
--
To unsubscribe from this list: send the line "unsubscribe linux-doc" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH v2] kernel.h: Documentation for roundup() vs round_up()
2018-07-03 2:16 [PATCH v2] kernel.h: Documentation for roundup() vs round_up() Kees Cook
@ 2018-07-03 2:28 ` Adam Borowski
0 siblings, 0 replies; 2+ messages in thread
From: Adam Borowski @ 2018-07-03 2:28 UTC (permalink / raw)
To: Kees Cook
Cc: Andrew Morton, Matthew Wilcox, Eric Biggers, Randy Dunlap,
linux-doc, linux-kernel
On Mon, Jul 02, 2018 at 07:16:54PM -0700, Kees Cook wrote:
> Things like commit 3619dec5103d ("dh key: fix rounding up KDF output
> length") expose the lack of explicit documentation for roundup() vs
> round_up(). At least we can try to document it better if anyone goes
> looking.
>
> Signed-off-by: Kees Cook <keescook@chromium.org>
> ---
> +/**
> + * round_up - round up to next specified power of 2
> + * @x: the value to round
> + * @y: multiple to round up to (must be a power of 2)
^^^^^^^^^^^^^^^^^^^^
This matches the description -- so far, good.
> + *
> + * Rounds @x up to next multiple of @y (which must be a power of 2).
> + * To perform arbitrary rounding up, use roundup() below.
> + */
> +/**
> + * roundup - round up to the next specified multiple
> + * @x: the value to up
> + * @y: multiple to round up to (must be a power of 2)
^^^^^^^^^^^^^^^^^^^^
But isn't this supposed to differ from the above?
> + *
> + * Rounds @x up to next multiple of @y. If @y will always be a power
> + * of 2, consider using the faster round_up().
> + *
> + * The `const' here prevents gcc-3.3 from calling __divdi3
> + */
> #define roundup(x, y) ( \
> { \
> const typeof(y) __y = y; \
> (((x) + (__y - 1)) / __y) * __y; \
> } \
Meow!
--
⢀⣴⠾⠻⢶⣦⠀
⣾⠁⢰⠒⠀⣿⡁ Imagine there are bandits in your house, your kid is bleeding out,
⢿⡄⠘⠷⠚⠋⠀ the house is on fire, and seven big-ass trumpets are playing in the
⠈⠳⣄⠀⠀⠀⠀ sky. Your cat demands food. The priority should be obvious...
--
To unsubscribe from this list: send the line "unsubscribe linux-doc" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2018-07-03 2:28 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-07-03 2:16 [PATCH v2] kernel.h: Documentation for roundup() vs round_up() Kees Cook
2018-07-03 2:28 ` Adam Borowski
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).