Skip to content

Commit f0b7f8a

Browse files
andy-shevkees
authored andcommitted
lib/string_helpers: Add flags param to string_get_size()
The new flags parameter allows controlling - Whether or not the units suffix is separated by a space, for compatibility with sort -h - Whether or not to append a B suffix - we're not always printing bytes. Co-developed-by: Kent Overstreet <[email protected]> Signed-off-by: Kent Overstreet <[email protected]> Signed-off-by: Andy Shevchenko <[email protected]> Reviewed-by: Kent Overstreet <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Kees Cook <[email protected]>
1 parent aaa8736 commit f0b7f8a

File tree

3 files changed

+77
-25
lines changed

3 files changed

+77
-25
lines changed

include/linux/string_helpers.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,18 @@ static inline bool string_is_terminated(const char *s, int len)
1717
return memchr(s, '\0', len) ? true : false;
1818
}
1919

20-
/* Descriptions of the types of units to
21-
* print in */
20+
/* Descriptions of the types of units to print in */
2221
enum string_size_units {
2322
STRING_UNITS_10, /* use powers of 10^3 (standard SI) */
2423
STRING_UNITS_2, /* use binary powers of 2^10 */
24+
STRING_UNITS_MASK = BIT(0),
25+
26+
/* Modifiers */
27+
STRING_UNITS_NO_SPACE = BIT(30),
28+
STRING_UNITS_NO_BYTES = BIT(31),
2529
};
2630

27-
int string_get_size(u64 size, u64 blk_size, enum string_size_units units,
31+
int string_get_size(u64 size, u64 blk_size, const enum string_size_units units,
2832
char *buf, int len);
2933

3034
int parse_int_array_user(const char __user *from, size_t count, int **array);

lib/string_helpers.c

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
* string_get_size - get the size in the specified units
2626
* @size: The size to be converted in blocks
2727
* @blk_size: Size of the block (use 1 for size in bytes)
28-
* @units: units to use (powers of 1000 or 1024)
28+
* @units: Units to use (powers of 1000 or 1024), whether to include space separator
2929
* @buf: buffer to format to
3030
* @len: length of buffer
3131
*
@@ -39,11 +39,12 @@
3939
int string_get_size(u64 size, u64 blk_size, const enum string_size_units units,
4040
char *buf, int len)
4141
{
42+
enum string_size_units units_base = units & STRING_UNITS_MASK;
4243
static const char *const units_10[] = {
43-
"B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"
44+
"", "k", "M", "G", "T", "P", "E", "Z", "Y",
4445
};
4546
static const char *const units_2[] = {
46-
"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"
47+
"", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi",
4748
};
4849
static const char *const *const units_str[] = {
4950
[STRING_UNITS_10] = units_10,
@@ -68,7 +69,7 @@ int string_get_size(u64 size, u64 blk_size, const enum string_size_units units,
6869

6970
/* This is Napier's algorithm. Reduce the original block size to
7071
*
71-
* coefficient * divisor[units]^i
72+
* coefficient * divisor[units_base]^i
7273
*
7374
* we do the reduction so both coefficients are just under 32 bits so
7475
* that multiplying them together won't overflow 64 bits and we keep
@@ -78,12 +79,12 @@ int string_get_size(u64 size, u64 blk_size, const enum string_size_units units,
7879
* precision is in the coefficients.
7980
*/
8081
while (blk_size >> 32) {
81-
do_div(blk_size, divisor[units]);
82+
do_div(blk_size, divisor[units_base]);
8283
i++;
8384
}
8485

8586
while (size >> 32) {
86-
do_div(size, divisor[units]);
87+
do_div(size, divisor[units_base]);
8788
i++;
8889
}
8990

@@ -92,8 +93,8 @@ int string_get_size(u64 size, u64 blk_size, const enum string_size_units units,
9293
size *= blk_size;
9394

9495
/* and logarithmically reduce it until it's just under the divisor */
95-
while (size >= divisor[units]) {
96-
remainder = do_div(size, divisor[units]);
96+
while (size >= divisor[units_base]) {
97+
remainder = do_div(size, divisor[units_base]);
9798
i++;
9899
}
99100

@@ -103,10 +104,10 @@ int string_get_size(u64 size, u64 blk_size, const enum string_size_units units,
103104
for (j = 0; sf_cap*10 < 1000; j++)
104105
sf_cap *= 10;
105106

106-
if (units == STRING_UNITS_2) {
107+
if (units_base == STRING_UNITS_2) {
107108
/* express the remainder as a decimal. It's currently the
108109
* numerator of a fraction whose denominator is
109-
* divisor[units], which is 1 << 10 for STRING_UNITS_2 */
110+
* divisor[units_base], which is 1 << 10 for STRING_UNITS_2 */
110111
remainder *= 1000;
111112
remainder >>= 10;
112113
}
@@ -128,10 +129,12 @@ int string_get_size(u64 size, u64 blk_size, const enum string_size_units units,
128129
if (i >= ARRAY_SIZE(units_2))
129130
unit = "UNK";
130131
else
131-
unit = units_str[units][i];
132+
unit = units_str[units_base][i];
132133

133-
return snprintf(buf, len, "%u%s %s", (u32)size,
134-
tmp, unit);
134+
return snprintf(buf, len, "%u%s%s%s%s", (u32)size, tmp,
135+
(units & STRING_UNITS_NO_SPACE) ? "" : " ",
136+
unit,
137+
(units & STRING_UNITS_NO_BYTES) ? "" : "B");
135138
}
136139
EXPORT_SYMBOL(string_get_size);
137140

lib/test-string_helpers.c

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*/
44
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
55

6+
#include <linux/array_size.h>
67
#include <linux/init.h>
78
#include <linux/kernel.h>
89
#include <linux/slab.h>
@@ -500,21 +501,65 @@ static __init void test_string_get_size_check(const char *units,
500501
pr_warn("expected: '%s', got '%s'\n", exp, res);
501502
}
502503

503-
static __init void __test_string_get_size(const u64 size, const u64 blk_size,
504-
const char *exp_result10,
505-
const char *exp_result2)
504+
static __init void __strchrcut(char *dst, const char *src, const char *cut)
505+
{
506+
const char *from = src;
507+
size_t len;
508+
509+
do {
510+
len = strcspn(from, cut);
511+
memcpy(dst, from, len);
512+
dst += len;
513+
from += len;
514+
} while (*from++);
515+
*dst = '\0';
516+
}
517+
518+
static __init void __test_string_get_size_one(const u64 size, const u64 blk_size,
519+
const char *exp_result10,
520+
const char *exp_result2,
521+
enum string_size_units units,
522+
const char *cut)
506523
{
507524
char buf10[string_get_size_maxbuf];
508525
char buf2[string_get_size_maxbuf];
526+
char exp10[string_get_size_maxbuf];
527+
char exp2[string_get_size_maxbuf];
528+
char prefix10[64];
529+
char prefix2[64];
530+
531+
sprintf(prefix10, "STRING_UNITS_10 [%s]", cut);
532+
sprintf(prefix2, "STRING_UNITS_2 [%s]", cut);
533+
534+
__strchrcut(exp10, exp_result10, cut);
535+
__strchrcut(exp2, exp_result2, cut);
509536

510-
string_get_size(size, blk_size, STRING_UNITS_10, buf10, sizeof(buf10));
511-
string_get_size(size, blk_size, STRING_UNITS_2, buf2, sizeof(buf2));
537+
string_get_size(size, blk_size, STRING_UNITS_10 | units, buf10, sizeof(buf10));
538+
string_get_size(size, blk_size, STRING_UNITS_2 | units, buf2, sizeof(buf2));
512539

513-
test_string_get_size_check("STRING_UNITS_10", exp_result10, buf10,
514-
size, blk_size);
540+
test_string_get_size_check(prefix10, exp10, buf10, size, blk_size);
541+
test_string_get_size_check(prefix2, exp2, buf2, size, blk_size);
542+
}
543+
544+
static __init void __test_string_get_size(const u64 size, const u64 blk_size,
545+
const char *exp_result10,
546+
const char *exp_result2)
547+
{
548+
struct {
549+
enum string_size_units units;
550+
const char *cut;
551+
} get_size_test_cases[] = {
552+
{ 0, "" },
553+
{ STRING_UNITS_NO_SPACE, " " },
554+
{ STRING_UNITS_NO_SPACE | STRING_UNITS_NO_BYTES, " B" },
555+
{ STRING_UNITS_NO_BYTES, "B" },
556+
};
557+
int i;
515558

516-
test_string_get_size_check("STRING_UNITS_2", exp_result2, buf2,
517-
size, blk_size);
559+
for (i = 0; i < ARRAY_SIZE(get_size_test_cases); i++)
560+
__test_string_get_size_one(size, blk_size, exp_result10, exp_result2,
561+
get_size_test_cases[i].units,
562+
get_size_test_cases[i].cut);
518563
}
519564

520565
static __init void test_string_get_size(void)

0 commit comments

Comments
 (0)