mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Alexander Lobakin <alexandr.lobakin@intel.com>
To: "David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@google.com>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>
Cc: Yury Norov <yury.norov@gmail.com>,
	Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
	Rasmus Villemoes <linux@rasmusvillemoes.dk>,
	Michal Swiatkowski <michal.swiatkowski@linux.intel.com>,
	Maciej Fijalkowski <maciej.fijalkowski@intel.com>,
	Alexander Lobakin <alexandr.lobakin@intel.com>,
	netdev@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH v2 net-next 2/6] bitmap: add a couple more helpers to work with arrays of u32s
Date: Tue, 18 Oct 2022 16:00:23 +0200	[thread overview]
Message-ID: <20221018140027.48086-3-alexandr.lobakin@intel.com> (raw)
In-Reply-To: <20221018140027.48086-1-alexandr.lobakin@intel.com>

Add two new functions to work on arr32s:

* bitmap_arr32_size() - takes number of bits to be stored in arr32
  and returns number of bytes required to store such arr32, can be
  useful when allocating memory for arr32 containers;
* bitmap_validate_arr32() - takes pointer to an arr32 and its size
  in bytes, plus expected number of bits. Ensures that the size is
  valid (must be a multiply of `sizeof(u32)`) and no bits past the
  number is set.

Also add BITMAP_TO_U64() macro to help return a u64 from
a DECLARE_BITMAP(1-64) (it may pick one or two longs depending
on the platform).

Signed-off-by: Alexander Lobakin <alexandr.lobakin@intel.com>
---
 include/linux/bitmap.h | 20 +++++++++++++++++++-
 lib/bitmap.c           | 40 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 59 insertions(+), 1 deletion(-)

diff --git a/include/linux/bitmap.h b/include/linux/bitmap.h
index 79d12e0f748b..c737b0fe2f41 100644
--- a/include/linux/bitmap.h
+++ b/include/linux/bitmap.h
@@ -7,7 +7,7 @@
 #include <linux/align.h>
 #include <linux/bitops.h>
 #include <linux/find.h>
-#include <linux/limits.h>
+#include <linux/overflow.h>
 #include <linux/string.h>
 #include <linux/types.h>
 
@@ -75,6 +75,8 @@ struct device;
  *  bitmap_from_arr64(dst, buf, nbits)          Copy nbits from u64[] buf to dst
  *  bitmap_to_arr32(buf, src, nbits)            Copy nbits from buf to u32[] dst
  *  bitmap_to_arr64(buf, src, nbits)            Copy nbits from buf to u64[] dst
+ *  bitmap_validate_arr32(buf, len, nbits)      Validate u32[] buf of len bytes
+ *  bitmap_arr32_size(nbits)                    Get size of u32[] arr for nbits
  *  bitmap_get_value8(map, start)               Get 8bit value from map at start
  *  bitmap_set_value8(map, value, start)        Set 8bit value to map at start
  *
@@ -324,6 +326,20 @@ static inline void bitmap_to_arr32(u32 *buf, const unsigned long *bitmap,
 		__bitmap_to_arr32(buf, bitmap, nbits);
 }
 
+bool bitmap_validate_arr32(const u32 *arr, size_t len, size_t nbits);
+
+/**
+ * bitmap_arr32_size - determine the size of array of u32s for a number of bits
+ * @nbits: number of bits to store in the array
+ *
+ * Returns the size in bytes of a u32s-array needed to carry the specified
+ * number of bits.
+ */
+static inline size_t bitmap_arr32_size(size_t nbits)
+{
+	return array_size(BITS_TO_U32(nbits), sizeof(u32));
+}
+
 /*
  * On 64-bit systems bitmaps are represented as u64 arrays internally. On LE32
  * machines the order of hi and lo parts of numbers match the bitmap structure.
@@ -571,9 +587,11 @@ static inline void bitmap_next_set_region(unsigned long *bitmap,
  */
 #if __BITS_PER_LONG == 64
 #define BITMAP_FROM_U64(n) (n)
+#define BITMAP_TO_U64(map) ((u64)(map)[0])
 #else
 #define BITMAP_FROM_U64(n) ((unsigned long) ((u64)(n) & ULONG_MAX)), \
 				((unsigned long) ((u64)(n) >> 32))
+#define BITMAP_TO_U64(map) (((u64)(map)[1] << 32) | (u64)(map)[0])
 #endif
 
 /**
diff --git a/lib/bitmap.c b/lib/bitmap.c
index e3eb12ff1637..e0045ecf34d6 100644
--- a/lib/bitmap.c
+++ b/lib/bitmap.c
@@ -1495,6 +1495,46 @@ void __bitmap_to_arr32(u32 *buf, const unsigned long *bitmap, unsigned int nbits
 EXPORT_SYMBOL(__bitmap_to_arr32);
 #endif
 
+/**
+ * bitmap_validate_arr32 - perform validation of a u32-array bitmap
+ * @arr: array of u32s, the dest bitmap
+ * @len: length of the array, in bytes
+ * @nbits: expected/supported number of bits in the bitmap
+ *
+ * Returns true if the array passes the checks (see below), false otherwise.
+ */
+bool bitmap_validate_arr32(const u32 *arr, size_t len, size_t nbits)
+{
+	size_t word = (nbits - 1) / BITS_PER_TYPE(u32);
+	u32 pos = (nbits - 1) % BITS_PER_TYPE(u32);
+
+	/* Must consist of 1...n full u32s */
+	if (!len || len % sizeof(u32))
+		return false;
+
+	/*
+	 * If the array is shorter than expected, assume we support
+	 * all of the bits set there.
+	 */
+	if (word >= len / sizeof(u32))
+		return true;
+
+	/* Last word must not contain any bits past the expected number */
+	if (arr[word] & (u32)~GENMASK(pos, 0))
+		return false;
+
+	/*
+	 * If the array is longer than expected, make sure all the bytes
+	 * past the expected length are zeroed.
+	 */
+	len -= bitmap_arr32_size(nbits);
+	if (memchr_inv(&arr[word + 1], 0, len))
+		return false;
+
+	return true;
+}
+EXPORT_SYMBOL(bitmap_validate_arr32);
+
 #if (BITS_PER_LONG == 32) && defined(__BIG_ENDIAN)
 /**
  * bitmap_from_arr64 - copy the contents of u64 array of bits to bitmap
-- 
2.37.3


  parent reply	other threads:[~2022-10-18 14:02 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-18 14:00 [PATCH v2 net-next 0/6] netlink: add universal 'bigint' attribute type Alexander Lobakin
2022-10-18 14:00 ` [PATCH v2 net-next 1/6] bitmap: try to optimize arr32 <-> bitmap on 64-bit LEs Alexander Lobakin
2022-10-18 22:41   ` Yury Norov
2022-10-19 15:21     ` Alexander Lobakin
2022-10-18 14:00 ` Alexander Lobakin [this message]
2022-10-20  0:21   ` [PATCH v2 net-next 2/6] bitmap: add a couple more helpers to work with arrays of u32s Yury Norov
2022-10-20 13:18     ` Andy Shevchenko
2022-10-20 15:31       ` Yury Norov
2022-10-18 14:00 ` [PATCH v2 net-next 3/6] lib/test_bitmap: verify intermediate arr32 when converting <-> bitmap Alexander Lobakin
2022-10-18 14:00 ` [PATCH v2 net-next 4/6] lib/test_bitmap: test the newly added arr32 functions Alexander Lobakin
2022-10-18 14:00 ` [PATCH v2 net-next 5/6] bitops: make BYTES_TO_BITS() treewide-available Alexander Lobakin
2022-10-18 19:55   ` Jakub Kicinski
2022-10-19 15:26     ` Alexander Lobakin
2022-10-18 14:00 ` [PATCH v2 net-next 6/6] netlink: add universal 'bigint' attribute type Alexander Lobakin
2022-10-18 14:13   ` Andy Shevchenko
2022-10-18 19:53   ` Jakub Kicinski
2022-10-19 15:34     ` Alexander Lobakin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221018140027.48086-3-alexandr.lobakin@intel.com \
    --to=alexandr.lobakin@intel.com \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@rasmusvillemoes.dk \
    --cc=maciej.fijalkowski@intel.com \
    --cc=michal.swiatkowski@linux.intel.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=yury.norov@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®