From: Jinseob Kim <kimjinseob88@gmail.com>
To: yury.norov@gmail.com
Cc: linux@rasmusvillemoes.dk, akpm@linux-foundation.org,
linux-kernel@vger.kernel.org
Subject: [PATCH 1/2] bitmap: ignore unused tail bits in bitmap_cut()
Date: Mon, 23 Nov 2026 13:31:53 +0900 [thread overview]
Message-ID: <df5afaa49465f0990ac4e0078af39c6943de4090.1795407826.git.kimjinseob88@gmail.com> (raw)
In-Reply-To: <cover.1795407826.git.kimjinseob88@gmail.com>
When nbits is not a multiple of BITS_PER_LONG, bitmap_cut() copies
the entire last word. A subsequent right shift can move unused tail
bits into the valid result. For example, cutting bit 0 from a 65-bit
bitmap can move storage bit 65 into output bit 64.
The bitmap API requires unused tail bits to have no effect on the
result. Mask the copied destination word before shifting. Keep the
existing pre-memmove() read of the bits preceding the cut so that
overlapping buffers continue to work.
The last existing test case removes all valid bits above bit 15.
Change its expected value to 0x0000ffff, which contains only the
preserved bits.
Fixes: 2092767168f0 ("bitmap: Introduce bitmap_cut(): cut bits and shift remaining")
Signed-off-by: Jinseob Kim <kimjinseob88@gmail.com>
---
lib/bitmap.c | 2 ++
lib/test_bitmap.c | 2 +-
2 files changed, 3 insertions(+), 1 deletion(-)
diff --git a/lib/bitmap.c b/lib/bitmap.c
index d1cb8a507..50cef429e 100644
--- a/lib/bitmap.c
+++ b/lib/bitmap.c
@@ -210,6 +210,8 @@ void bitmap_cut(unsigned long *dst, const unsigned long *src,
}
memmove(dst, src, len * sizeof(*dst));
+ if (nbits % BITS_PER_LONG)
+ dst[len - 1] &= BITMAP_LAST_WORD_MASK(nbits);
while (cut--) {
for (i = first / BITS_PER_LONG; i < len; i++) {
diff --git a/lib/test_bitmap.c b/lib/test_bitmap.c
index ba09d057f..eb925d576 100644
--- a/lib/test_bitmap.c
+++ b/lib/test_bitmap.c
@@ -1138,7 +1138,7 @@ static struct test_bitmap_cut test_cut[] = {
},
{ 16, BITS_PER_LONG * 2 + 1, BITS_PER_LONG * 2 + 1 + 16,
{ 0x0000ffffUL, 0x5a5a5a5aUL, 0x5a5a5a5aUL, 0x5a5a5a5aUL },
- { 0x2d2dffffUL, },
+ { 0x0000ffffUL, },
},
};
next prev parent reply other threads:[~2026-11-23 4:33 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-23 4:31 [PATCH 0/2] bitmap: keep unused tail bits out of bitmap_cut() results Jinseob Kim
2026-11-23 4:31 ` Jinseob Kim [this message]
2026-11-23 4:31 ` [PATCH 2/2] lib/test_bitmap: cover unused tail bits in bitmap_cut() Jinseob Kim
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=df5afaa49465f0990ac4e0078af39c6943de4090.1795407826.git.kimjinseob88@gmail.com \
--to=kimjinseob88@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@rasmusvillemoes.dk \
--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®