1212#include <fcntl.h>
1313#include <sys/ioctl.h>
1414#include <sys/stat.h>
15+ #include <sys/uio.h>
1516#include <unistd.h>
1617
1718#include "../kselftest_harness.h"
@@ -22,11 +23,6 @@ const char *enable_file = "/sys/kernel/tracing/events/user_events/__test_event/e
2223const char * trace_file = "/sys/kernel/tracing/trace" ;
2324const char * fmt_file = "/sys/kernel/tracing/events/user_events/__test_event/format" ;
2425
25- static inline int status_check (char * status_page , int status_bit )
26- {
27- return status_page [status_bit >> 3 ] & (1 << (status_bit & 7 ));
28- }
29-
3026static int trace_bytes (void )
3127{
3228 int fd = open (trace_file , O_RDONLY );
@@ -106,13 +102,23 @@ static int get_print_fmt(char *buffer, int len)
106102 return -1 ;
107103}
108104
109- static int clear (void )
105+ static int clear (int * check )
110106{
107+ struct user_unreg unreg = {0 };
108+
109+ unreg .size = sizeof (unreg );
110+ unreg .disable_bit = 31 ;
111+ unreg .disable_addr = (__u64 )check ;
112+
111113 int fd = open (data_file , O_RDWR );
112114
113115 if (fd == -1 )
114116 return -1 ;
115117
118+ if (ioctl (fd , DIAG_IOCSUNREG , & unreg ) == -1 )
119+ if (errno != ENOENT )
120+ return -1 ;
121+
116122 if (ioctl (fd , DIAG_IOCSDEL , "__test_event" ) == -1 )
117123 if (errno != ENOENT )
118124 return -1 ;
@@ -122,15 +128,15 @@ static int clear(void)
122128 return 0 ;
123129}
124130
125- static int check_print_fmt (const char * event , const char * expected )
131+ static int check_print_fmt (const char * event , const char * expected , int * check )
126132{
127133 struct user_reg reg = {0 };
128134 char print_fmt [256 ];
129135 int ret ;
130136 int fd ;
131137
132138 /* Ensure cleared */
133- ret = clear ();
139+ ret = clear (check );
134140
135141 if (ret != 0 )
136142 return ret ;
@@ -142,14 +148,19 @@ static int check_print_fmt(const char *event, const char *expected)
142148
143149 reg .size = sizeof (reg );
144150 reg .name_args = (__u64 )event ;
151+ reg .enable_bit = 31 ;
152+ reg .enable_addr = (__u64 )check ;
153+ reg .enable_size = sizeof (* check );
145154
146155 /* Register should work */
147156 ret = ioctl (fd , DIAG_IOCSREG , & reg );
148157
149158 close (fd );
150159
151- if (ret != 0 )
160+ if (ret != 0 ) {
161+ printf ("Reg failed in fmt\n" );
152162 return ret ;
163+ }
153164
154165 /* Ensure correct print_fmt */
155166 ret = get_print_fmt (print_fmt , sizeof (print_fmt ));
@@ -164,6 +175,7 @@ FIXTURE(user) {
164175 int status_fd ;
165176 int data_fd ;
166177 int enable_fd ;
178+ int check ;
167179};
168180
169181FIXTURE_SETUP (user ) {
@@ -185,71 +197,69 @@ FIXTURE_TEARDOWN(user) {
185197 close (self -> enable_fd );
186198 }
187199
188- ASSERT_EQ (0 , clear ());
200+ if (clear (& self -> check ) != 0 )
201+ printf ("WARNING: Clear didn't work!\n" );
189202}
190203
191204TEST_F (user , register_events ) {
192205 struct user_reg reg = {0 };
193- int page_size = sysconf (_SC_PAGESIZE );
194- char * status_page ;
206+ struct user_unreg unreg = {0 };
195207
196208 reg .size = sizeof (reg );
197209 reg .name_args = (__u64 )"__test_event u32 field1; u32 field2" ;
210+ reg .enable_bit = 31 ;
211+ reg .enable_addr = (__u64 )& self -> check ;
212+ reg .enable_size = sizeof (self -> check );
198213
199- status_page = mmap (NULL , page_size , PROT_READ , MAP_SHARED ,
200- self -> status_fd , 0 );
214+ unreg .size = sizeof (unreg );
215+ unreg .disable_bit = 31 ;
216+ unreg .disable_addr = (__u64 )& self -> check ;
201217
202218 /* Register should work */
203219 ASSERT_EQ (0 , ioctl (self -> data_fd , DIAG_IOCSREG , & reg ));
204220 ASSERT_EQ (0 , reg .write_index );
205- ASSERT_NE (0 , reg .status_bit );
206221
207222 /* Multiple registers should result in same index */
208223 ASSERT_EQ (0 , ioctl (self -> data_fd , DIAG_IOCSREG , & reg ));
209224 ASSERT_EQ (0 , reg .write_index );
210- ASSERT_NE (0 , reg .status_bit );
211225
212226 /* Ensure disabled */
213227 self -> enable_fd = open (enable_file , O_RDWR );
214228 ASSERT_NE (-1 , self -> enable_fd );
215229 ASSERT_NE (-1 , write (self -> enable_fd , "0" , sizeof ("0" )))
216230
217- /* MMAP should work and be zero'd */
218- ASSERT_NE (MAP_FAILED , status_page );
219- ASSERT_NE (NULL , status_page );
220- ASSERT_EQ (0 , status_check (status_page , reg .status_bit ));
221-
222231 /* Enable event and ensure bits updated in status */
223232 ASSERT_NE (-1 , write (self -> enable_fd , "1" , sizeof ("1" )))
224- ASSERT_NE ( 0 , status_check ( status_page , reg .status_bit ) );
233+ ASSERT_EQ ( 1 << reg .enable_bit , self -> check );
225234
226235 /* Disable event and ensure bits updated in status */
227236 ASSERT_NE (-1 , write (self -> enable_fd , "0" , sizeof ("0" )))
228- ASSERT_EQ (0 , status_check ( status_page , reg . status_bit ) );
237+ ASSERT_EQ (0 , self -> check );
229238
230239 /* File still open should return -EBUSY for delete */
231240 ASSERT_EQ (-1 , ioctl (self -> data_fd , DIAG_IOCSDEL , "__test_event" ));
232241 ASSERT_EQ (EBUSY , errno );
233242
234- /* Delete should work only after close */
243+ /* Unregister */
244+ ASSERT_EQ (0 , ioctl (self -> data_fd , DIAG_IOCSUNREG , & unreg ));
245+
246+ /* Delete should work only after close and unregister */
235247 close (self -> data_fd );
236248 self -> data_fd = open (data_file , O_RDWR );
237249 ASSERT_EQ (0 , ioctl (self -> data_fd , DIAG_IOCSDEL , "__test_event" ));
238-
239- /* Unmap should work */
240- ASSERT_EQ (0 , munmap (status_page , page_size ));
241250}
242251
243252TEST_F (user , write_events ) {
244253 struct user_reg reg = {0 };
245254 struct iovec io [3 ];
246255 __u32 field1 , field2 ;
247256 int before = 0 , after = 0 ;
248- int page_size = sysconf (_SC_PAGESIZE );
249- char * status_page ;
250257
251258 reg .size = sizeof (reg );
252259 reg .name_args = (__u64 )"__test_event u32 field1; u32 field2" ;
260+ reg .enable_bit = 31 ;
261+ reg .enable_addr = (__u64 )& self -> check ;
262+ reg .enable_size = sizeof (self -> check );
253263
254264 field1 = 1 ;
255265 field2 = 2 ;
@@ -261,18 +271,10 @@ TEST_F(user, write_events) {
261271 io [2 ].iov_base = & field2 ;
262272 io [2 ].iov_len = sizeof (field2 );
263273
264- status_page = mmap (NULL , page_size , PROT_READ , MAP_SHARED ,
265- self -> status_fd , 0 );
266-
267274 /* Register should work */
268275 ASSERT_EQ (0 , ioctl (self -> data_fd , DIAG_IOCSREG , & reg ));
269276 ASSERT_EQ (0 , reg .write_index );
270- ASSERT_NE (0 , reg .status_bit );
271-
272- /* MMAP should work and be zero'd */
273- ASSERT_NE (MAP_FAILED , status_page );
274- ASSERT_NE (NULL , status_page );
275- ASSERT_EQ (0 , status_check (status_page , reg .status_bit ));
277+ ASSERT_EQ (0 , self -> check );
276278
277279 /* Write should fail on invalid slot with ENOENT */
278280 io [0 ].iov_base = & field2 ;
@@ -287,7 +289,7 @@ TEST_F(user, write_events) {
287289 ASSERT_NE (-1 , write (self -> enable_fd , "1" , sizeof ("1" )))
288290
289291 /* Event should now be enabled */
290- ASSERT_NE (0 , status_check ( status_page , reg .status_bit ) );
292+ ASSERT_NE (1 << reg .enable_bit , self -> check );
291293
292294 /* Write should make it out to ftrace buffers */
293295 before = trace_bytes ();
@@ -304,6 +306,9 @@ TEST_F(user, write_fault) {
304306
305307 reg .size = sizeof (reg );
306308 reg .name_args = (__u64 )"__test_event u64 anon" ;
309+ reg .enable_bit = 31 ;
310+ reg .enable_addr = (__u64 )& self -> check ;
311+ reg .enable_size = sizeof (self -> check );
307312
308313 anon = mmap (NULL , l , PROT_READ , MAP_PRIVATE | MAP_ANONYMOUS , -1 , 0 );
309314 ASSERT_NE (MAP_FAILED , anon );
@@ -316,7 +321,6 @@ TEST_F(user, write_fault) {
316321 /* Register should work */
317322 ASSERT_EQ (0 , ioctl (self -> data_fd , DIAG_IOCSREG , & reg ));
318323 ASSERT_EQ (0 , reg .write_index );
319- ASSERT_NE (0 , reg .status_bit );
320324
321325 /* Write should work normally */
322326 ASSERT_NE (-1 , writev (self -> data_fd , (const struct iovec * )io , 2 ));
@@ -333,24 +337,17 @@ TEST_F(user, write_validator) {
333337 int loc , bytes ;
334338 char data [8 ];
335339 int before = 0 , after = 0 ;
336- int page_size = sysconf (_SC_PAGESIZE );
337- char * status_page ;
338-
339- status_page = mmap (NULL , page_size , PROT_READ , MAP_SHARED ,
340- self -> status_fd , 0 );
341340
342341 reg .size = sizeof (reg );
343342 reg .name_args = (__u64 )"__test_event __rel_loc char[] data" ;
343+ reg .enable_bit = 31 ;
344+ reg .enable_addr = (__u64 )& self -> check ;
345+ reg .enable_size = sizeof (self -> check );
344346
345347 /* Register should work */
346348 ASSERT_EQ (0 , ioctl (self -> data_fd , DIAG_IOCSREG , & reg ));
347349 ASSERT_EQ (0 , reg .write_index );
348- ASSERT_NE (0 , reg .status_bit );
349-
350- /* MMAP should work and be zero'd */
351- ASSERT_NE (MAP_FAILED , status_page );
352- ASSERT_NE (NULL , status_page );
353- ASSERT_EQ (0 , status_check (status_page , reg .status_bit ));
350+ ASSERT_EQ (0 , self -> check );
354351
355352 io [0 ].iov_base = & reg .write_index ;
356353 io [0 ].iov_len = sizeof (reg .write_index );
@@ -369,7 +366,7 @@ TEST_F(user, write_validator) {
369366 ASSERT_NE (-1 , write (self -> enable_fd , "1" , sizeof ("1" )))
370367
371368 /* Event should now be enabled */
372- ASSERT_NE ( 0 , status_check ( status_page , reg .status_bit ) );
369+ ASSERT_EQ ( 1 << reg .enable_bit , self -> check );
373370
374371 /* Full in-bounds write should work */
375372 before = trace_bytes ();
@@ -409,71 +406,88 @@ TEST_F(user, print_fmt) {
409406 int ret ;
410407
411408 ret = check_print_fmt ("__test_event __rel_loc char[] data" ,
412- "print fmt: \"data=%s\", __get_rel_str(data)" );
409+ "print fmt: \"data=%s\", __get_rel_str(data)" ,
410+ & self -> check );
413411 ASSERT_EQ (0 , ret );
414412
415413 ret = check_print_fmt ("__test_event __data_loc char[] data" ,
416- "print fmt: \"data=%s\", __get_str(data)" );
414+ "print fmt: \"data=%s\", __get_str(data)" ,
415+ & self -> check );
417416 ASSERT_EQ (0 , ret );
418417
419418 ret = check_print_fmt ("__test_event s64 data" ,
420- "print fmt: \"data=%lld\", REC->data" );
419+ "print fmt: \"data=%lld\", REC->data" ,
420+ & self -> check );
421421 ASSERT_EQ (0 , ret );
422422
423423 ret = check_print_fmt ("__test_event u64 data" ,
424- "print fmt: \"data=%llu\", REC->data" );
424+ "print fmt: \"data=%llu\", REC->data" ,
425+ & self -> check );
425426 ASSERT_EQ (0 , ret );
426427
427428 ret = check_print_fmt ("__test_event s32 data" ,
428- "print fmt: \"data=%d\", REC->data" );
429+ "print fmt: \"data=%d\", REC->data" ,
430+ & self -> check );
429431 ASSERT_EQ (0 , ret );
430432
431433 ret = check_print_fmt ("__test_event u32 data" ,
432- "print fmt: \"data=%u\", REC->data" );
434+ "print fmt: \"data=%u\", REC->data" ,
435+ & self -> check );
433436 ASSERT_EQ (0 , ret );
434437
435438 ret = check_print_fmt ("__test_event int data" ,
436- "print fmt: \"data=%d\", REC->data" );
439+ "print fmt: \"data=%d\", REC->data" ,
440+ & self -> check );
437441 ASSERT_EQ (0 , ret );
438442
439443 ret = check_print_fmt ("__test_event unsigned int data" ,
440- "print fmt: \"data=%u\", REC->data" );
444+ "print fmt: \"data=%u\", REC->data" ,
445+ & self -> check );
441446 ASSERT_EQ (0 , ret );
442447
443448 ret = check_print_fmt ("__test_event s16 data" ,
444- "print fmt: \"data=%d\", REC->data" );
449+ "print fmt: \"data=%d\", REC->data" ,
450+ & self -> check );
445451 ASSERT_EQ (0 , ret );
446452
447453 ret = check_print_fmt ("__test_event u16 data" ,
448- "print fmt: \"data=%u\", REC->data" );
454+ "print fmt: \"data=%u\", REC->data" ,
455+ & self -> check );
449456 ASSERT_EQ (0 , ret );
450457
451458 ret = check_print_fmt ("__test_event short data" ,
452- "print fmt: \"data=%d\", REC->data" );
459+ "print fmt: \"data=%d\", REC->data" ,
460+ & self -> check );
453461 ASSERT_EQ (0 , ret );
454462
455463 ret = check_print_fmt ("__test_event unsigned short data" ,
456- "print fmt: \"data=%u\", REC->data" );
464+ "print fmt: \"data=%u\", REC->data" ,
465+ & self -> check );
457466 ASSERT_EQ (0 , ret );
458467
459468 ret = check_print_fmt ("__test_event s8 data" ,
460- "print fmt: \"data=%d\", REC->data" );
469+ "print fmt: \"data=%d\", REC->data" ,
470+ & self -> check );
461471 ASSERT_EQ (0 , ret );
462472
463473 ret = check_print_fmt ("__test_event u8 data" ,
464- "print fmt: \"data=%u\", REC->data" );
474+ "print fmt: \"data=%u\", REC->data" ,
475+ & self -> check );
465476 ASSERT_EQ (0 , ret );
466477
467478 ret = check_print_fmt ("__test_event char data" ,
468- "print fmt: \"data=%d\", REC->data" );
479+ "print fmt: \"data=%d\", REC->data" ,
480+ & self -> check );
469481 ASSERT_EQ (0 , ret );
470482
471483 ret = check_print_fmt ("__test_event unsigned char data" ,
472- "print fmt: \"data=%u\", REC->data" );
484+ "print fmt: \"data=%u\", REC->data" ,
485+ & self -> check );
473486 ASSERT_EQ (0 , ret );
474487
475488 ret = check_print_fmt ("__test_event char[4] data" ,
476- "print fmt: \"data=%s\", REC->data" );
489+ "print fmt: \"data=%s\", REC->data" ,
490+ & self -> check );
477491 ASSERT_EQ (0 , ret );
478492}
479493
0 commit comments