1717#include <malloc.h>
1818#include <stdbool.h>
1919#include "vm_util.h"
20+ #include "../kselftest.h"
2021
2122uint64_t pagesize ;
2223unsigned int pageshift ;
@@ -50,21 +51,19 @@ int is_backed_by_thp(char *vaddr, int pagemap_file, int kpageflags_file)
5051 return 0 ;
5152}
5253
53- static int write_file (const char * path , const char * buf , size_t buflen )
54+ static void write_file (const char * path , const char * buf , size_t buflen )
5455{
5556 int fd ;
5657 ssize_t numwritten ;
5758
5859 fd = open (path , O_WRONLY );
5960 if (fd == -1 )
60- return 0 ;
61+ ksft_exit_fail_msg ( "%s open failed: %s\n" , path , strerror ( errno )) ;
6162
6263 numwritten = write (fd , buf , buflen - 1 );
6364 close (fd );
6465 if (numwritten < 1 )
65- return 0 ;
66-
67- return (unsigned int ) numwritten ;
66+ ksft_exit_fail_msg ("Write failed\n" );
6867}
6968
7069static void write_debugfs (const char * fmt , ...)
@@ -77,15 +76,10 @@ static void write_debugfs(const char *fmt, ...)
7776 ret = vsnprintf (input , INPUT_MAX , fmt , argp );
7877 va_end (argp );
7978
80- if (ret >= INPUT_MAX ) {
81- printf ("%s: Debugfs input is too long\n" , __func__ );
82- exit (EXIT_FAILURE );
83- }
79+ if (ret >= INPUT_MAX )
80+ ksft_exit_fail_msg ("%s: Debugfs input is too long\n" , __func__ );
8481
85- if (!write_file (SPLIT_DEBUGFS , input , ret + 1 )) {
86- perror (SPLIT_DEBUGFS );
87- exit (EXIT_FAILURE );
88- }
82+ write_file (SPLIT_DEBUGFS , input , ret + 1 );
8983}
9084
9185void split_pmd_thp (void )
@@ -95,39 +89,30 @@ void split_pmd_thp(void)
9589 size_t i ;
9690
9791 one_page = memalign (pmd_pagesize , len );
98-
99- if (!one_page ) {
100- printf ("Fail to allocate memory\n" );
101- exit (EXIT_FAILURE );
102- }
92+ if (!one_page )
93+ ksft_exit_fail_msg ("Fail to allocate memory: %s\n" , strerror (errno ));
10394
10495 madvise (one_page , len , MADV_HUGEPAGE );
10596
10697 for (i = 0 ; i < len ; i ++ )
10798 one_page [i ] = (char )i ;
10899
109- if (!check_huge_anon (one_page , 4 , pmd_pagesize )) {
110- printf ("No THP is allocated\n" );
111- exit (EXIT_FAILURE );
112- }
100+ if (!check_huge_anon (one_page , 4 , pmd_pagesize ))
101+ ksft_exit_fail_msg ("No THP is allocated\n" );
113102
114103 /* split all THPs */
115104 write_debugfs (PID_FMT , getpid (), (uint64_t )one_page ,
116105 (uint64_t )one_page + len );
117106
118107 for (i = 0 ; i < len ; i ++ )
119- if (one_page [i ] != (char )i ) {
120- printf ("%ld byte corrupted\n" , i );
121- exit (EXIT_FAILURE );
122- }
108+ if (one_page [i ] != (char )i )
109+ ksft_exit_fail_msg ("%ld byte corrupted\n" , i );
123110
124111
125- if (!check_huge_anon (one_page , 0 , pmd_pagesize )) {
126- printf ("Still AnonHugePages not split\n" );
127- exit (EXIT_FAILURE );
128- }
112+ if (!check_huge_anon (one_page , 0 , pmd_pagesize ))
113+ ksft_exit_fail_msg ("Still AnonHugePages not split\n" );
129114
130- printf ("Split huge pages successful\n" );
115+ ksft_test_result_pass ("Split huge pages successful\n" );
131116 free (one_page );
132117}
133118
@@ -143,36 +128,29 @@ void split_pte_mapped_thp(void)
143128 int pagemap_fd ;
144129 int kpageflags_fd ;
145130
146- if (snprintf (pagemap_proc , 255 , pagemap_template , getpid ()) < 0 ) {
147- perror ("get pagemap proc error" );
148- exit (EXIT_FAILURE );
149- }
150- pagemap_fd = open (pagemap_proc , O_RDONLY );
131+ if (snprintf (pagemap_proc , 255 , pagemap_template , getpid ()) < 0 )
132+ ksft_exit_fail_msg ("get pagemap proc error: %s\n" , strerror (errno ));
151133
152- if (pagemap_fd == -1 ) {
153- perror ("read pagemap:" );
154- exit (EXIT_FAILURE );
155- }
134+ pagemap_fd = open (pagemap_proc , O_RDONLY );
135+ if (pagemap_fd == -1 )
136+ ksft_exit_fail_msg ("read pagemap: %s\n" , strerror (errno ));
156137
157138 kpageflags_fd = open (kpageflags_proc , O_RDONLY );
158-
159- if (kpageflags_fd == -1 ) {
160- perror ("read kpageflags:" );
161- exit (EXIT_FAILURE );
162- }
139+ if (kpageflags_fd == -1 )
140+ ksft_exit_fail_msg ("read kpageflags: %s\n" , strerror (errno ));
163141
164142 one_page = mmap ((void * )(1UL << 30 ), len , PROT_READ | PROT_WRITE ,
165143 MAP_ANONYMOUS | MAP_PRIVATE , -1 , 0 );
144+ if (one_page == MAP_FAILED )
145+ ksft_exit_fail_msg ("Fail to allocate memory: %s\n" , strerror (errno ));
166146
167147 madvise (one_page , len , MADV_HUGEPAGE );
168148
169149 for (i = 0 ; i < len ; i ++ )
170150 one_page [i ] = (char )i ;
171151
172- if (!check_huge_anon (one_page , 4 , pmd_pagesize )) {
173- printf ("No THP is allocated\n" );
174- exit (EXIT_FAILURE );
175- }
152+ if (!check_huge_anon (one_page , 4 , pmd_pagesize ))
153+ ksft_exit_fail_msg ("No THP is allocated\n" );
176154
177155 /* remap the first pagesize of first THP */
178156 pte_mapped = mremap (one_page , pagesize , pagesize , MREMAP_MAYMOVE );
@@ -183,10 +161,8 @@ void split_pte_mapped_thp(void)
183161 pagesize , pagesize ,
184162 MREMAP_MAYMOVE |MREMAP_FIXED ,
185163 pte_mapped + pagesize * i );
186- if (pte_mapped2 == (char * )-1 ) {
187- perror ("mremap failed" );
188- exit (EXIT_FAILURE );
189- }
164+ if (pte_mapped2 == MAP_FAILED )
165+ ksft_exit_fail_msg ("mremap failed: %s\n" , strerror (errno ));
190166 }
191167
192168 /* smap does not show THPs after mremap, use kpageflags instead */
@@ -196,10 +172,8 @@ void split_pte_mapped_thp(void)
196172 is_backed_by_thp (& pte_mapped [i ], pagemap_fd , kpageflags_fd ))
197173 thp_size ++ ;
198174
199- if (thp_size != 4 ) {
200- printf ("Some THPs are missing during mremap\n" );
201- exit (EXIT_FAILURE );
202- }
175+ if (thp_size != 4 )
176+ ksft_exit_fail_msg ("Some THPs are missing during mremap\n" );
203177
204178 /* split all remapped THPs */
205179 write_debugfs (PID_FMT , getpid (), (uint64_t )pte_mapped ,
@@ -208,21 +182,18 @@ void split_pte_mapped_thp(void)
208182 /* smap does not show THPs after mremap, use kpageflags instead */
209183 thp_size = 0 ;
210184 for (i = 0 ; i < pagesize * 4 ; i ++ ) {
211- if (pte_mapped [i ] != (char )i ) {
212- printf ("%ld byte corrupted\n" , i );
213- exit (EXIT_FAILURE );
214- }
185+ if (pte_mapped [i ] != (char )i )
186+ ksft_exit_fail_msg ("%ld byte corrupted\n" , i );
187+
215188 if (i % pagesize == 0 &&
216189 is_backed_by_thp (& pte_mapped [i ], pagemap_fd , kpageflags_fd ))
217190 thp_size ++ ;
218191 }
219192
220- if (thp_size ) {
221- printf ("Still %ld THPs not split\n" , thp_size );
222- exit (EXIT_FAILURE );
223- }
193+ if (thp_size )
194+ ksft_exit_fail_msg ("Still %ld THPs not split\n" , thp_size );
224195
225- printf ("Split PTE-mapped huge pages successful\n" );
196+ ksft_test_result_pass ("Split PTE-mapped huge pages successful\n" );
226197 munmap (one_page , len );
227198 close (pagemap_fd );
228199 close (kpageflags_fd );
@@ -238,24 +209,21 @@ void split_file_backed_thp(void)
238209 char testfile [INPUT_MAX ];
239210 uint64_t pgoff_start = 0 , pgoff_end = 1024 ;
240211
241- printf ("Please enable pr_debug in split_huge_pages_in_file() if you need more info.\n" );
212+ ksft_print_msg ("Please enable pr_debug in split_huge_pages_in_file() for more info.\n" );
242213
243214 status = mount ("tmpfs" , tmpfs_loc , "tmpfs" , 0 , "huge=always,size=4m" );
244215
245- if (status ) {
246- printf ("Unable to create a tmpfs for testing\n" );
247- exit (EXIT_FAILURE );
248- }
216+ if (status )
217+ ksft_exit_fail_msg ("Unable to create a tmpfs for testing\n" );
249218
250219 status = snprintf (testfile , INPUT_MAX , "%s/thp_file" , tmpfs_loc );
251220 if (status >= INPUT_MAX ) {
252- printf ("Fail to create file-backed THP split testing file\n" );
253- goto cleanup ;
221+ ksft_exit_fail_msg ("Fail to create file-backed THP split testing file\n" );
254222 }
255223
256224 fd = open (testfile , O_CREAT |O_WRONLY );
257225 if (fd == -1 ) {
258- perror ("Cannot open testing file\n " );
226+ ksft_perror ("Cannot open testing file" );
259227 goto cleanup ;
260228 }
261229
@@ -264,50 +232,59 @@ void split_file_backed_thp(void)
264232 close (fd );
265233
266234 if (num_written < 1 ) {
267- printf ("Fail to write data to testing file\n " );
235+ ksft_perror ("Fail to write data to testing file" );
268236 goto cleanup ;
269237 }
270238
271239 /* split the file-backed THP */
272240 write_debugfs (PATH_FMT , testfile , pgoff_start , pgoff_end );
273241
274242 status = unlink (testfile );
275- if (status )
276- perror ("Cannot remove testing file\n" );
243+ if (status ) {
244+ ksft_perror ("Cannot remove testing file" );
245+ goto cleanup ;
246+ }
277247
278- cleanup :
279248 status = umount (tmpfs_loc );
280249 if (status ) {
281- printf ( "Unable to umount %s\n" , tmpfs_loc );
282- exit ( EXIT_FAILURE );
250+ rmdir ( tmpfs_loc );
251+ ksft_exit_fail_msg ( "Unable to umount %s\n" , tmpfs_loc );
283252 }
253+
284254 status = rmdir (tmpfs_loc );
285- if (status ) {
286- perror ("cannot remove tmp dir" );
287- exit (EXIT_FAILURE );
288- }
255+ if (status )
256+ ksft_exit_fail_msg ("cannot remove tmp dir: %s\n" , strerror (errno ));
289257
290- printf ("file-backed THP split test done, please check dmesg for more information\n" );
258+ ksft_print_msg ("Please check dmesg for more information\n" );
259+ ksft_test_result_pass ("File-backed THP split test done\n" );
260+ return ;
261+
262+ cleanup :
263+ umount (tmpfs_loc );
264+ rmdir (tmpfs_loc );
265+ ksft_exit_fail_msg ("Error occurred\n" );
291266}
292267
293268int main (int argc , char * * argv )
294269{
270+ ksft_print_header ();
271+
295272 if (geteuid () != 0 ) {
296- printf ("Please run the benchmark as root\n" );
297- exit ( EXIT_FAILURE );
273+ ksft_print_msg ("Please run the benchmark as root\n" );
274+ ksft_finished ( );
298275 }
299276
277+ ksft_set_plan (3 );
278+
300279 pagesize = getpagesize ();
301280 pageshift = ffs (pagesize ) - 1 ;
302281 pmd_pagesize = read_pmd_pagesize ();
303- if (!pmd_pagesize ) {
304- printf ("Reading PMD pagesize failed\n" );
305- exit (EXIT_FAILURE );
306- }
282+ if (!pmd_pagesize )
283+ ksft_exit_fail_msg ("Reading PMD pagesize failed\n" );
307284
308285 split_pmd_thp ();
309286 split_pte_mapped_thp ();
310287 split_file_backed_thp ();
311288
312- return 0 ;
289+ ksft_finished () ;
313290}
0 commit comments