From: Alexander Potapenko <glider@google.com>
To: glider@google.com, catalin.marinas@arm.com, will@kernel.org,
pcc@google.com, andreyknvl@gmail.com,
andriy.shevchenko@linux.intel.com, linux@rasmusvillemoes.dk,
yury.norov@gmail.com
Cc: linux-kernel@vger.kernel.org,
linux-arm-kernel@lists.infradead.org, eugenis@google.com
Subject: [v2 1/5] lib/bitmap: add bitmap_{set,get}_value_unaligned()
Date: Thu, 13 Jul 2023 14:57:01 +0200 [thread overview]
Message-ID: <20230713125706.2884502-2-glider@google.com> (raw)
In-Reply-To: <20230713125706.2884502-1-glider@google.com>
The two new functions allow setting/getting values of length up to
BITS_PER_LONG bits at arbitrary position in the bitmap.
Suggested-by: Yury Norov <yury.norov@gmail.com>
Signed-off-by: Alexander Potapenko <glider@google.com>
---
include/linux/bitmap.h | 63 ++++++++++++++++++++++++++++++++++++++++++
1 file changed, 63 insertions(+)
diff --git a/include/linux/bitmap.h b/include/linux/bitmap.h
index 03644237e1efb..8e36ce07bafd4 100644
--- a/include/linux/bitmap.h
+++ b/include/linux/bitmap.h
@@ -77,6 +77,8 @@ struct device;
* bitmap_to_arr64(buf, src, nbits) Copy nbits from buf to u64[] dst
* 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
+ * bitmap_get_value_unaligned(map, start, nbits) Get value up to BITS_PER_LONG
+ * bitmap_set_value_unaligned(map, value, start, nbits) Set value up to BITS_PER_LONG
*
* Note, bitmap_zero() and bitmap_fill() operate over the region of
* unsigned longs, that is, bits behind bitmap till the unsigned long
@@ -583,6 +585,35 @@ static inline unsigned long bitmap_get_value8(const unsigned long *map,
return (map[index] >> offset) & 0xFF;
}
+/**
+ * bitmap_get_value_unaligned - get an @nbits-bit value within a memory region
+ * @map: address to the bitmap memory region
+ * @start: bit offset of the value; may not be a multiple of 8
+ * @nbits: number of bits to get
+ *
+ * Returns the @nbits-sized value located at the @start bit offset within the
+ * @map memory region.
+ */
+static inline unsigned long bitmap_get_value_unaligned(const unsigned long *map,
+ unsigned long start,
+ unsigned long nbits)
+{
+ const size_t index = BIT_WORD(start);
+ const unsigned long offset = start % BITS_PER_LONG;
+ const unsigned long carry = (offset + nbits) % BITS_PER_LONG;
+ unsigned long hi, lo, result;
+
+ if (offset + nbits <= BITS_PER_LONG) {
+ result = map[index] >> (BITS_PER_LONG - offset - nbits);
+ return result & BITMAP_LAST_WORD_MASK(nbits);
+ }
+
+ hi = map[index] & BITMAP_LAST_WORD_MASK(BITS_PER_LONG - offset);
+ lo = map[index + 1] & BITMAP_FIRST_WORD_MASK(BITS_PER_LONG - carry);
+ lo >>= (BITS_PER_LONG - carry);
+ return (hi << carry) | lo;
+}
+
/**
* bitmap_set_value8 - set an 8-bit value within a memory region
* @map: address to the bitmap memory region
@@ -599,6 +630,38 @@ static inline void bitmap_set_value8(unsigned long *map, unsigned long value,
map[index] |= value << offset;
}
+/**
+ * bitmap_set_value_unaligned - set an @nbits-bit value within a memory region
+ * @map: address to the bitmap memory region
+ * @value: the value up to BITS_PER_LONG bits (will be clamped to @nbits)
+ * @start: bit offset of the value; may not be a multiple of 8
+ * @nbits: number of bits to set
+ */
+static inline void bitmap_set_value_unaligned(unsigned long *map,
+ unsigned long value,
+ unsigned long start,
+ unsigned long nbits)
+{
+ const size_t index = BIT_WORD(start);
+ const unsigned long offset = start % BITS_PER_LONG;
+ unsigned long mask = BITMAP_LAST_WORD_MASK(nbits);
+ const unsigned long carry = (offset + nbits) % BITS_PER_LONG;
+
+ value &= mask;
+ if (offset + nbits <= BITS_PER_LONG) {
+ value <<= (BITS_PER_LONG - offset - nbits);
+ mask <<= (BITS_PER_LONG - offset - nbits);
+ map[index] &= ~mask;
+ map[index] |= value;
+ return;
+ }
+ map[index] &= ~BITMAP_LAST_WORD_MASK(BITS_PER_LONG - offset);
+ map[index] |= (value >> (carry));
+ value &= BITMAP_LAST_WORD_MASK(carry);
+ map[index + 1] &= ~BITMAP_FIRST_WORD_MASK(BITS_PER_LONG - carry);
+ map[index + 1] |= value << (BITS_PER_LONG - carry);
+}
+
#endif /* __ASSEMBLY__ */
#endif /* __LINUX_BITMAP_H */
--
2.41.0.255.g8b1d071c50-goog
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
WARNING: multiple messages have this Message-ID (diff)
From: Alexander Potapenko <glider@google.com>
To: glider@google.com, catalin.marinas@arm.com, will@kernel.org,
pcc@google.com, andreyknvl@gmail.com,
andriy.shevchenko@linux.intel.com, linux@rasmusvillemoes.dk,
yury.norov@gmail.com
Cc: linux-kernel@vger.kernel.org,
linux-arm-kernel@lists.infradead.org, eugenis@google.com
Subject: [v2 1/5] lib/bitmap: add bitmap_{set,get}_value_unaligned()
Date: Thu, 13 Jul 2023 14:57:01 +0200 [thread overview]
Message-ID: <20230713125706.2884502-2-glider@google.com> (raw)
In-Reply-To: <20230713125706.2884502-1-glider@google.com>
The two new functions allow setting/getting values of length up to
BITS_PER_LONG bits at arbitrary position in the bitmap.
Suggested-by: Yury Norov <yury.norov@gmail.com>
Signed-off-by: Alexander Potapenko <glider@google.com>
---
include/linux/bitmap.h | 63 ++++++++++++++++++++++++++++++++++++++++++
1 file changed, 63 insertions(+)
diff --git a/include/linux/bitmap.h b/include/linux/bitmap.h
index 03644237e1efb..8e36ce07bafd4 100644
--- a/include/linux/bitmap.h
+++ b/include/linux/bitmap.h
@@ -77,6 +77,8 @@ struct device;
* bitmap_to_arr64(buf, src, nbits) Copy nbits from buf to u64[] dst
* 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
+ * bitmap_get_value_unaligned(map, start, nbits) Get value up to BITS_PER_LONG
+ * bitmap_set_value_unaligned(map, value, start, nbits) Set value up to BITS_PER_LONG
*
* Note, bitmap_zero() and bitmap_fill() operate over the region of
* unsigned longs, that is, bits behind bitmap till the unsigned long
@@ -583,6 +585,35 @@ static inline unsigned long bitmap_get_value8(const unsigned long *map,
return (map[index] >> offset) & 0xFF;
}
+/**
+ * bitmap_get_value_unaligned - get an @nbits-bit value within a memory region
+ * @map: address to the bitmap memory region
+ * @start: bit offset of the value; may not be a multiple of 8
+ * @nbits: number of bits to get
+ *
+ * Returns the @nbits-sized value located at the @start bit offset within the
+ * @map memory region.
+ */
+static inline unsigned long bitmap_get_value_unaligned(const unsigned long *map,
+ unsigned long start,
+ unsigned long nbits)
+{
+ const size_t index = BIT_WORD(start);
+ const unsigned long offset = start % BITS_PER_LONG;
+ const unsigned long carry = (offset + nbits) % BITS_PER_LONG;
+ unsigned long hi, lo, result;
+
+ if (offset + nbits <= BITS_PER_LONG) {
+ result = map[index] >> (BITS_PER_LONG - offset - nbits);
+ return result & BITMAP_LAST_WORD_MASK(nbits);
+ }
+
+ hi = map[index] & BITMAP_LAST_WORD_MASK(BITS_PER_LONG - offset);
+ lo = map[index + 1] & BITMAP_FIRST_WORD_MASK(BITS_PER_LONG - carry);
+ lo >>= (BITS_PER_LONG - carry);
+ return (hi << carry) | lo;
+}
+
/**
* bitmap_set_value8 - set an 8-bit value within a memory region
* @map: address to the bitmap memory region
@@ -599,6 +630,38 @@ static inline void bitmap_set_value8(unsigned long *map, unsigned long value,
map[index] |= value << offset;
}
+/**
+ * bitmap_set_value_unaligned - set an @nbits-bit value within a memory region
+ * @map: address to the bitmap memory region
+ * @value: the value up to BITS_PER_LONG bits (will be clamped to @nbits)
+ * @start: bit offset of the value; may not be a multiple of 8
+ * @nbits: number of bits to set
+ */
+static inline void bitmap_set_value_unaligned(unsigned long *map,
+ unsigned long value,
+ unsigned long start,
+ unsigned long nbits)
+{
+ const size_t index = BIT_WORD(start);
+ const unsigned long offset = start % BITS_PER_LONG;
+ unsigned long mask = BITMAP_LAST_WORD_MASK(nbits);
+ const unsigned long carry = (offset + nbits) % BITS_PER_LONG;
+
+ value &= mask;
+ if (offset + nbits <= BITS_PER_LONG) {
+ value <<= (BITS_PER_LONG - offset - nbits);
+ mask <<= (BITS_PER_LONG - offset - nbits);
+ map[index] &= ~mask;
+ map[index] |= value;
+ return;
+ }
+ map[index] &= ~BITMAP_LAST_WORD_MASK(BITS_PER_LONG - offset);
+ map[index] |= (value >> (carry));
+ value &= BITMAP_LAST_WORD_MASK(carry);
+ map[index + 1] &= ~BITMAP_FIRST_WORD_MASK(BITS_PER_LONG - carry);
+ map[index + 1] |= value << (BITS_PER_LONG - carry);
+}
+
#endif /* __ASSEMBLY__ */
#endif /* __LINUX_BITMAP_H */
--
2.41.0.255.g8b1d071c50-goog
next prev parent reply other threads:[~2023-07-13 12:58 UTC|newest]
Thread overview: 40+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-07-13 12:57 [v2 0/5] Implement MTE tag compression for swapped pages Alexander Potapenko
2023-07-13 12:57 ` Alexander Potapenko
2023-07-13 12:57 ` Alexander Potapenko [this message]
2023-07-13 12:57 ` [v2 1/5] lib/bitmap: add bitmap_{set,get}_value_unaligned() Alexander Potapenko
2023-07-13 17:28 ` Andy Shevchenko
2023-07-13 17:28 ` Andy Shevchenko
2023-07-13 18:05 ` Alexander Potapenko
2023-07-13 18:05 ` Alexander Potapenko
2023-07-14 8:04 ` Andy Shevchenko
2023-07-14 8:04 ` Andy Shevchenko
2023-07-14 11:19 ` William Breathitt Gray
2023-07-14 11:19 ` William Breathitt Gray
2023-07-14 11:28 ` Andy Shevchenko
2023-07-14 11:28 ` Andy Shevchenko
2023-07-14 12:07 ` Alexander Potapenko
2023-07-14 12:07 ` Alexander Potapenko
2023-07-14 12:30 ` Andy Shevchenko
2023-07-14 12:30 ` Andy Shevchenko
2023-07-13 12:57 ` [v2 2/5] lib/test_bitmap: add tests for bitmap_{set,get}_value_unaligned Alexander Potapenko
2023-07-13 12:57 ` Alexander Potapenko
2023-07-13 12:57 ` [v2 3/5] arm64: mte: implement CONFIG_ARM64_MTE_COMP Alexander Potapenko
2023-07-13 12:57 ` Alexander Potapenko
2023-07-13 16:37 ` Alexander Potapenko
2023-07-13 16:37 ` Alexander Potapenko
2023-07-13 17:23 ` Andy Shevchenko
2023-07-13 17:23 ` Andy Shevchenko
2023-07-13 19:27 ` Yury Norov
2023-07-13 19:27 ` Yury Norov
2023-07-14 8:01 ` Andy Shevchenko
2023-07-14 8:01 ` Andy Shevchenko
2023-07-14 9:25 ` Alexander Potapenko
2023-07-14 9:25 ` Alexander Potapenko
2023-07-14 10:47 ` Andy Shevchenko
2023-07-14 10:47 ` Andy Shevchenko
2023-07-14 11:17 ` Alexander Potapenko
2023-07-14 11:17 ` Alexander Potapenko
2023-07-13 12:57 ` [v2 4/5] arm64: mte: add a test for MTE tags compression Alexander Potapenko
2023-07-13 12:57 ` Alexander Potapenko
2023-07-13 12:57 ` [v2 5/5] arm64: mte: add compression support to mteswap.c Alexander Potapenko
2023-07-13 12:57 ` Alexander Potapenko
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=20230713125706.2884502-2-glider@google.com \
--to=glider@google.com \
--cc=andreyknvl@gmail.com \
--cc=andriy.shevchenko@linux.intel.com \
--cc=catalin.marinas@arm.com \
--cc=eugenis@google.com \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@rasmusvillemoes.dk \
--cc=pcc@google.com \
--cc=will@kernel.org \
--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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.