[PATCH] Fix calculus of bitmap_scnprintf_len()

Previous thread: [PATCH] bitops: simplify generic bit finding functions by Harvey Harrison on Sunday, April 27, 2008 - 4:19 pm. (27 messages)

Next thread: Breakage caused by unreviewed patch in x86 tree by James Bottomley on Sunday, April 27, 2008 - 4:51 pm. (51 messages)
To: <mingo@...>
Cc: <linux-kernel@...>, <torvalds@...>, <akpm@...>, Bert Wesarg <bert.wesarg@...>, Mike Travis <travis@...>, Paul Jackson <pj@...>
Date: Sunday, April 27, 2008 - 4:43 pm

The function bitmap_scnprintf_len() is currently not used, but the returned
value is 4 times larger than needed. This value is also only a good upper
bound. Which should be mentioned in the comment.

The correct number of chars needed for the buffer, exluding any new line and
terminating zero is:

int bitmap_scnprintf_len(int len)
{
return /* complete hunks with commas */
((len / CHUNKSZ) * 9)
/* partial hunk, 4 bits in one char */
+ (((len % CHUNKSZ) + 3) / 4)
/* one less comma, if no partial hunk */
- !(len % CHUNKSZ);
}

Signed-off-by: Bert Wesarg <bert.wesarg@googlemail.com>
Cc: Mike Travis <travis@sgi.com>
Cc: Paul Jackson <pj@sgi.com>

---
lib/bitmap.c | 2 +-
1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/lib/bitmap.c b/lib/bitmap.c
index a6939e1..47e6f0f 100644
--- a/lib/bitmap.c
+++ b/lib/bitmap.c
@@ -325,7 +325,7 @@ int bitmap_scnprintf_len(unsigned int len)
/* we need 9 chars per word for 32 bit words (8 hexdigits + sep/null) */
int bitslen = ALIGN(len, CHUNKSZ);
int wordlen = CHUNKSZ / 4;
- int buflen = (bitslen / wordlen) * (wordlen + 1) * sizeof(char);
+ int buflen = (bitslen / CHUNKSZ) * (wordlen + 1);

return buflen;
}
--
1.5.4

--

To: Bert Wesarg <bert.wesarg@...>
Cc: <mingo@...>, <linux-kernel@...>, <torvalds@...>, <akpm@...>, <bert.wesarg@...>, <travis@...>
Date: Sunday, April 27, 2008 - 5:01 pm

How about we just remove that function?

--
I won't rest till it's the best ...
Programmer, Linux Scalability
Paul Jackson <pj@sgi.com> 1.940.382.4214
--

To: Paul Jackson <pj@...>
Cc: Bert Wesarg <bert.wesarg@...>, <mingo@...>, <linux-kernel@...>, <torvalds@...>, <akpm@...>, <travis@...>
Date: Monday, April 28, 2008 - 2:14 am

I am afraid no. See:

include/linux/cpumask.h:292: return bitmap_scnprintf_len(len);

--

To: WANG Cong <xiyou.wangcong@...>
Cc: <bert.wesarg@...>, <mingo@...>, <linux-kernel@...>, <torvalds@...>, <akpm@...>, <travis@...>
Date: Monday, April 28, 2008 - 9:13 am

Good point.

Then how about we also remove from cpumask.h:

#define cpumask_scnprintf_len(len) \
__cpumask_scnprintf_len((len))
static inline int __cpumask_scnprintf_len(int len)
{
return bitmap_scnprintf_len(len);
}

--
I won't rest till it's the best ...
Programmer, Linux Scalability
Paul Jackson <pj@sgi.com> 1.940.382.4214
--

To: Paul Jackson <pj@...>
Cc: WANG Cong <xiyou.wangcong@...>, <bert.wesarg@...>, <mingo@...>, <linux-kernel@...>, <torvalds@...>, <akpm@...>
Date: Monday, April 28, 2008 - 1:09 pm

That's fine with me. A later version of the patch did have
the function removed but it didn't get picked up. The other
changes there were to use function pointers instead of the
flag variable to select list or mask output format, and the
addition of mask variants for the cpu/{present,possible,
online,system} map outputs.

I'll dig that one back up and resubmit it.

Thanks,
Mike
--

To: Mike Travis <travis@...>
Cc: Paul Jackson <pj@...>, WANG Cong <xiyou.wangcong@...>, <mingo@...>, <linux-kernel@...>, <torvalds@...>, <akpm@...>
Date: Monday, April 28, 2008 - 1:31 pm

I'm fine with this too. I did a hasty audit of cpumask_scnprintf()
users, and no one can use these functions.

But one last note to the cpumask_scnprintf_len() macro: this macro
should really not have an argument, it should be forced to NR_CPUS.
Else a user could have a too small buffer for the call to
cpumask_scnprintf(), which always calls bitmap_scnprintf() with
Fine.

Regards.
--

Previous thread: [PATCH] bitops: simplify generic bit finding functions by Harvey Harrison on Sunday, April 27, 2008 - 4:19 pm. (27 messages)

Next thread: Breakage caused by unreviewed patch in x86 tree by James Bottomley on Sunday, April 27, 2008 - 4:51 pm. (51 messages)