Skip to content

Commit 1c8d484

Browse files
mairacanalakpm00
authored andcommitted
mm: move get_order_from_str() to internal.h
In order to implement a kernel parameter similar to ``thp_anon=`` for shmem, we'll need the function ``get_order_from_str()``. Instead of duplicating the function, move the function to a shared header, in which both mm/shmem.c and mm/huge_memory.c will be able to use it. Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Maíra Canal <[email protected]> Reviewed-by: Baolin Wang <[email protected]> Cc: Barry Song <[email protected]> Cc: David Hildenbrand <[email protected]> Cc: Hugh Dickins <[email protected]> Cc: Jonathan Corbet <[email protected]> Cc: Lance Yang <[email protected]> Cc: Ryan Roberts <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 9490428 commit 1c8d484

File tree

2 files changed

+37
-23
lines changed

2 files changed

+37
-23
lines changed

mm/huge_memory.c

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -958,26 +958,6 @@ static int __init setup_transparent_hugepage(char *str)
958958
}
959959
__setup("transparent_hugepage=", setup_transparent_hugepage);
960960

961-
static inline int get_order_from_str(const char *size_str)
962-
{
963-
unsigned long size;
964-
char *endptr;
965-
int order;
966-
967-
size = memparse(size_str, &endptr);
968-
969-
if (!is_power_of_2(size))
970-
goto err;
971-
order = get_order(size);
972-
if (BIT(order) & ~THP_ORDERS_ALL_ANON)
973-
goto err;
974-
975-
return order;
976-
err:
977-
pr_err("invalid size %s in thp_anon boot parameter\n", size_str);
978-
return -EINVAL;
979-
}
980-
981961
static char str_dup[PAGE_SIZE] __initdata;
982962
static int __init setup_thp_anon(char *str)
983963
{
@@ -1007,10 +987,22 @@ static int __init setup_thp_anon(char *str)
1007987
start_size = strsep(&subtoken, "-");
1008988
end_size = subtoken;
1009989

1010-
start = get_order_from_str(start_size);
1011-
end = get_order_from_str(end_size);
990+
start = get_order_from_str(start_size, THP_ORDERS_ALL_ANON);
991+
end = get_order_from_str(end_size, THP_ORDERS_ALL_ANON);
1012992
} else {
1013-
start = end = get_order_from_str(subtoken);
993+
start_size = end_size = subtoken;
994+
start = end = get_order_from_str(subtoken,
995+
THP_ORDERS_ALL_ANON);
996+
}
997+
998+
if (start == -EINVAL) {
999+
pr_err("invalid size %s in thp_anon boot parameter\n", start_size);
1000+
goto err;
1001+
}
1002+
1003+
if (end == -EINVAL) {
1004+
pr_err("invalid size %s in thp_anon boot parameter\n", end_size);
1005+
goto err;
10141006
}
10151007

10161008
if (start < 0 || end < 0 || start > end)

mm/internal.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1291,6 +1291,28 @@ static inline bool alloc_zeroed(void)
12911291
&init_on_alloc);
12921292
}
12931293

1294+
/*
1295+
* Parses a string with mem suffixes into its order. Useful to parse kernel
1296+
* parameters.
1297+
*/
1298+
static inline int get_order_from_str(const char *size_str,
1299+
unsigned long valid_orders)
1300+
{
1301+
unsigned long size;
1302+
char *endptr;
1303+
int order;
1304+
1305+
size = memparse(size_str, &endptr);
1306+
1307+
if (!is_power_of_2(size))
1308+
return -EINVAL;
1309+
order = get_order(size);
1310+
if (BIT(order) & ~valid_orders)
1311+
return -EINVAL;
1312+
1313+
return order;
1314+
}
1315+
12941316
enum {
12951317
/* mark page accessed */
12961318
FOLL_TOUCH = 1 << 16,

0 commit comments

Comments
 (0)