11// SPDX-License-Identifier: GPL-2.0-or-later
22#include "tests/common.h"
33#include <string.h>
4+ #include <getopt.h>
5+ #include <linux/memory_hotplug.h>
6+ #include <linux/build_bug.h>
47
58#define INIT_MEMBLOCK_REGIONS 128
69#define INIT_MEMBLOCK_RESERVED_REGIONS INIT_MEMBLOCK_REGIONS
@@ -11,6 +14,27 @@ static struct test_memory memory_block;
1114static const char __maybe_unused * prefixes [PREFIXES_MAX ];
1215static int __maybe_unused nr_prefixes ;
1316
17+ static const char * short_opts = "mv" ;
18+ static const struct option long_opts [] = {
19+ {"movable-node" , 0 , NULL , 'm' },
20+ {"verbose" , 0 , NULL , 'v' },
21+ {NULL , 0 , NULL , 0 }
22+ };
23+
24+ static const char * const help_opts [] = {
25+ "disallow allocations from regions marked as hotplugged\n\t\t\t"
26+ "by simulating enabling the \"movable_node\" kernel\n\t\t\t"
27+ "parameter" ,
28+ "enable verbose output, which includes the name of the\n\t\t\t"
29+ "memblock function being tested, the name of the test,\n\t\t\t"
30+ "and whether the test passed or failed."
31+ };
32+
33+ static int verbose ;
34+
35+ /* sets global variable returned by movable_node_is_enabled() stub */
36+ bool movable_node_enabled ;
37+
1438void reset_memblock_regions (void )
1539{
1640 memset (memblock .memory .regions , 0 ,
@@ -51,7 +75,39 @@ void dummy_physical_memory_cleanup(void)
5175 free (memory_block .base );
5276}
5377
54- #ifdef VERBOSE
78+ static void usage (const char * prog )
79+ {
80+ BUILD_BUG_ON (ARRAY_SIZE (help_opts ) != ARRAY_SIZE (long_opts ) - 1 );
81+
82+ printf ("Usage: %s [-%s]\n" , prog , short_opts );
83+
84+ for (int i = 0 ; long_opts [i ].name ; i ++ ) {
85+ printf (" -%c, --%-12s\t%s\n" , long_opts [i ].val ,
86+ long_opts [i ].name , help_opts [i ]);
87+ }
88+
89+ exit (1 );
90+ }
91+
92+ void parse_args (int argc , char * * argv )
93+ {
94+ int c ;
95+
96+ while ((c = getopt_long_only (argc , argv , short_opts , long_opts ,
97+ NULL )) != -1 ) {
98+ switch (c ) {
99+ case 'm' :
100+ movable_node_enabled = true;
101+ break ;
102+ case 'v' :
103+ verbose = 1 ;
104+ break ;
105+ default :
106+ usage (argv [0 ]);
107+ }
108+ }
109+ }
110+
55111void print_prefixes (const char * postfix )
56112{
57113 for (int i = 0 ; i < nr_prefixes ; i ++ )
@@ -61,25 +117,31 @@ void print_prefixes(const char *postfix)
61117
62118void test_fail (void )
63119{
64- ksft_test_result_fail (": " );
65- print_prefixes ("failed\n" );
120+ if (verbose ) {
121+ ksft_test_result_fail (": " );
122+ print_prefixes ("failed\n" );
123+ }
66124}
67125
68126void test_pass (void )
69127{
70- ksft_test_result_pass (": " );
71- print_prefixes ("passed\n" );
128+ if (verbose ) {
129+ ksft_test_result_pass (": " );
130+ print_prefixes ("passed\n" );
131+ }
72132}
73133
74134void test_print (const char * fmt , ...)
75135{
76- int saved_errno = errno ;
77- va_list args ;
78-
79- va_start (args , fmt );
80- errno = saved_errno ;
81- vprintf (fmt , args );
82- va_end (args );
136+ if (verbose ) {
137+ int saved_errno = errno ;
138+ va_list args ;
139+
140+ va_start (args , fmt );
141+ errno = saved_errno ;
142+ vprintf (fmt , args );
143+ va_end (args );
144+ }
83145}
84146
85147void prefix_reset (void )
@@ -102,4 +164,3 @@ void prefix_pop(void)
102164 nr_prefixes -- ;
103165 }
104166}
105- #endif /* VERBOSE */
0 commit comments