Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

tools/testing/vma: add unit tests flag empty, diff_pair, and[_mask]

Add VMA unit tests to assert that:

* vma_flags_empty()
* vma_flags_diff_pair()
* vma_flags_and_mask()
* vma_flags_and()

All function as expected.

In additional to the added tests, in order to make testing easier, add
vma_flags_same_mask() and vma_flags_same() for testing only. If/when
these are required in kernel code, they can be moved over.

Also add ASSERT_FLAGS_[NOT_]SAME[_MASK](), ASSERT_FLAGS_[NON]EMPTY() test
helpers to make asserting flag state easier and more convenient.

Link: https://lkml.kernel.org/r/471ce7ceb1d32e5fc9c0660966b9eacdf899b4d1.1774034900.git.ljs@kernel.org
Signed-off-by: Lorenzo Stoakes (Oracle) <ljs@kernel.org>
Cc: Albert Ou <aou@eecs.berkeley.edu>
Cc: Alexander Gordeev <agordeev@linux.ibm.com>
Cc: Alexandre Ghiti <alex@ghiti.fr>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Cc: Anton Ivanov <anton.ivanov@cambridgegreys.com>
Cc: "Borislav Petkov (AMD)" <bp@alien8.de>
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: Chengming Zhou <chengming.zhou@linux.dev>
Cc: Christian Borntraeger <borntraeger@linux.ibm.com>
Cc: Christian Brauner <brauner@kernel.org>
Cc: David Hildenbrand <david@kernel.org>
Cc: Dinh Nguyen <dinguyen@kernel.org>
Cc: Heiko Carstens <hca@linux.ibm.com>
Cc: "H. Peter Anvin" <hpa@zytor.com>
Cc: Huacai Chen <chenhuacai@kernel.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Jan Kara <jack@suse.cz>
Cc: Jann Horn <jannh@google.com>
Cc: Johannes Berg <johannes@sipsolutions.net>
Cc: Kees Cook <kees@kernel.org>
Cc: Liam Howlett <liam.howlett@oracle.com>
Cc: Madhavan Srinivasan <maddy@linux.ibm.com>
Cc: Michael Ellerman <mpe@ellerman.id.au>
Cc: Michal Hocko <mhocko@suse.com>
Cc: Mike Rapoport <rppt@kernel.org>
Cc: Nicholas Piggin <npiggin@gmail.com>
Cc: Ondrej Mosnacek <omosnace@redhat.com>
Cc: Palmer Dabbelt <palmer@dabbelt.com>
Cc: Paul Moore <paul@paul-moore.com>
Cc: Pedro Falcato <pfalcato@suse.de>
Cc: Richard Weinberger <richard@nod.at>
Cc: Russell King <linux@armlinux.org.uk>
Cc: Stephen Smalley <stephen.smalley.work@gmail.com>
Cc: Suren Baghdasaryan <surenb@google.com>
Cc: Sven Schnelle <svens@linux.ibm.com>
Cc: Thomas Bogendoerfer <tsbogend@alpha.franken.de>
Cc: Vasily Gorbik <gor@linux.ibm.com>
Cc: Vineet Gupta <vgupta@kernel.org>
Cc: Vlastimil Babka (SUSE) <vbabka@kernel.org>
Cc: WANG Xuerui <kernel@xen0n.name>
Cc: Will Deacon <will@kernel.org>
Cc: xu xin <xu.xin16@zte.com.cn>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Lorenzo Stoakes (Oracle) and committed by
Andrew Morton
e4fd34b8 6bc0987d

+167
+12
tools/testing/vma/include/custom.h
··· 120 120 { 121 121 return PAGE_SIZE; 122 122 } 123 + 124 + /* Place here until needed in the kernel code. */ 125 + static __always_inline bool vma_flags_same_mask(vma_flags_t *flags, 126 + vma_flags_t flags_other) 127 + { 128 + const unsigned long *bitmap = flags->__vma_flags; 129 + const unsigned long *bitmap_other = flags_other.__vma_flags; 130 + 131 + return bitmap_equal(bitmap, bitmap_other, NUM_VMA_FLAG_BITS); 132 + } 133 + #define vma_flags_same(flags, ...) \ 134 + vma_flags_same_mask(flags, mk_vma_flags(__VA_ARGS__))
+18
tools/testing/vma/shared.h
··· 35 35 #define ASSERT_EQ(_val1, _val2) ASSERT_TRUE((_val1) == (_val2)) 36 36 #define ASSERT_NE(_val1, _val2) ASSERT_TRUE((_val1) != (_val2)) 37 37 38 + #define ASSERT_FLAGS_SAME_MASK(_flags, _flags_other) \ 39 + ASSERT_TRUE(vma_flags_same_mask((_flags), (_flags_other))) 40 + 41 + #define ASSERT_FLAGS_NOT_SAME_MASK(_flags, _flags_other) \ 42 + ASSERT_FALSE(vma_flags_same_mask((_flags), (_flags_other))) 43 + 44 + #define ASSERT_FLAGS_SAME(_flags, ...) \ 45 + ASSERT_TRUE(vma_flags_same(_flags, __VA_ARGS__)) 46 + 47 + #define ASSERT_FLAGS_NOT_SAME(_flags, ...) \ 48 + ASSERT_FALSE(vma_flags_same(_flags, __VA_ARGS__)) 49 + 50 + #define ASSERT_FLAGS_EMPTY(_flags) \ 51 + ASSERT_TRUE(vma_flags_empty(_flags)) 52 + 53 + #define ASSERT_FLAGS_NONEMPTY(_flags) \ 54 + ASSERT_FALSE(vma_flags_empty(_flags)) 55 + 38 56 #define IS_SET(_val, _flags) ((_val & _flags) == _flags) 39 57 40 58 extern bool fail_prealloc;
+137
tools/testing/vma/tests/vma.c
··· 363 363 return true; 364 364 } 365 365 366 + /* Ensure that vma_flags_empty() works correctly. */ 367 + static bool test_vma_flags_empty(void) 368 + { 369 + vma_flags_t flags = mk_vma_flags(VMA_READ_BIT, VMA_WRITE_BIT, 370 + VMA_EXEC_BIT, 64, 65); 371 + 372 + ASSERT_FLAGS_NONEMPTY(&flags); 373 + vma_flags_clear(&flags, VMA_READ_BIT, VMA_WRITE_BIT, VMA_EXEC_BIT); 374 + #if NUM_VMA_FLAG_BITS > 64 375 + ASSERT_FLAGS_NONEMPTY(&flags); 376 + vma_flags_clear(&flags, 64, 65); 377 + ASSERT_FLAGS_EMPTY(&flags); 378 + #else 379 + ASSERT_FLAGS_EMPTY(&flags); 380 + #endif 381 + 382 + return true; 383 + } 384 + 385 + /* Ensure that vma_flags_diff_pair() works correctly. */ 386 + static bool test_vma_flags_diff(void) 387 + { 388 + vma_flags_t flags1 = mk_vma_flags(VMA_READ_BIT, VMA_WRITE_BIT, 389 + VMA_EXEC_BIT, 64, 65); 390 + vma_flags_t flags2 = mk_vma_flags(VMA_READ_BIT, VMA_WRITE_BIT, 391 + VMA_EXEC_BIT, VMA_MAYWRITE_BIT, 392 + VMA_MAYEXEC_BIT, 64, 65, 66, 67); 393 + vma_flags_t diff = vma_flags_diff_pair(&flags1, &flags2); 394 + 395 + #if NUM_VMA_FLAG_BITS > 64 396 + ASSERT_FLAGS_SAME(&diff, VMA_MAYWRITE_BIT, VMA_MAYEXEC_BIT, 66, 67); 397 + #else 398 + ASSERT_FLAGS_SAME(&diff, VMA_MAYWRITE_BIT, VMA_MAYEXEC_BIT); 399 + #endif 400 + /* Should be the same even if re-ordered. */ 401 + diff = vma_flags_diff_pair(&flags2, &flags1); 402 + #if NUM_VMA_FLAG_BITS > 64 403 + ASSERT_FLAGS_SAME(&diff, VMA_MAYWRITE_BIT, VMA_MAYEXEC_BIT, 66, 67); 404 + #else 405 + ASSERT_FLAGS_SAME(&diff, VMA_MAYWRITE_BIT, VMA_MAYEXEC_BIT); 406 + #endif 407 + 408 + /* Should be no difference when applied against themselves. */ 409 + diff = vma_flags_diff_pair(&flags1, &flags1); 410 + ASSERT_FLAGS_EMPTY(&diff); 411 + diff = vma_flags_diff_pair(&flags2, &flags2); 412 + ASSERT_FLAGS_EMPTY(&diff); 413 + 414 + /* One set of flags against an empty one should equal the original. */ 415 + flags2 = EMPTY_VMA_FLAGS; 416 + diff = vma_flags_diff_pair(&flags1, &flags2); 417 + ASSERT_FLAGS_SAME_MASK(&diff, flags1); 418 + 419 + /* A subset should work too. */ 420 + flags2 = mk_vma_flags(VMA_READ_BIT, VMA_WRITE_BIT); 421 + diff = vma_flags_diff_pair(&flags1, &flags2); 422 + #if NUM_VMA_FLAG_BITS > 64 423 + ASSERT_FLAGS_SAME(&diff, VMA_EXEC_BIT, 64, 65); 424 + #else 425 + ASSERT_FLAGS_SAME(&diff, VMA_EXEC_BIT); 426 + #endif 427 + 428 + return true; 429 + } 430 + 431 + /* Ensure that vma_flags_and() and friends work correctly. */ 432 + static bool test_vma_flags_and(void) 433 + { 434 + vma_flags_t flags1 = mk_vma_flags(VMA_READ_BIT, VMA_WRITE_BIT, 435 + VMA_EXEC_BIT, 64, 65); 436 + vma_flags_t flags2 = mk_vma_flags(VMA_READ_BIT, VMA_WRITE_BIT, 437 + VMA_EXEC_BIT, VMA_MAYWRITE_BIT, 438 + VMA_MAYEXEC_BIT, 64, 65, 66, 67); 439 + vma_flags_t flags3 = mk_vma_flags(VMA_IO_BIT, VMA_MAYBE_GUARD_BIT, 440 + 68, 69); 441 + vma_flags_t and = vma_flags_and_mask(&flags1, flags2); 442 + 443 + #if NUM_VMA_FLAG_BITS > 64 444 + ASSERT_FLAGS_SAME(&and, VMA_READ_BIT, VMA_WRITE_BIT, VMA_EXEC_BIT, 445 + 64, 65); 446 + #else 447 + ASSERT_FLAGS_SAME(&and, VMA_READ_BIT, VMA_WRITE_BIT, VMA_EXEC_BIT); 448 + #endif 449 + 450 + and = vma_flags_and_mask(&flags1, flags1); 451 + ASSERT_FLAGS_SAME_MASK(&and, flags1); 452 + 453 + and = vma_flags_and_mask(&flags2, flags2); 454 + ASSERT_FLAGS_SAME_MASK(&and, flags2); 455 + 456 + and = vma_flags_and_mask(&flags1, flags3); 457 + ASSERT_FLAGS_EMPTY(&and); 458 + and = vma_flags_and_mask(&flags2, flags3); 459 + ASSERT_FLAGS_EMPTY(&and); 460 + 461 + and = vma_flags_and(&flags1, VMA_READ_BIT); 462 + ASSERT_FLAGS_SAME(&and, VMA_READ_BIT); 463 + 464 + and = vma_flags_and(&flags1, VMA_READ_BIT, VMA_WRITE_BIT); 465 + ASSERT_FLAGS_SAME(&and, VMA_READ_BIT, VMA_WRITE_BIT); 466 + 467 + and = vma_flags_and(&flags1, VMA_READ_BIT, VMA_WRITE_BIT, VMA_EXEC_BIT); 468 + ASSERT_FLAGS_SAME(&and, VMA_READ_BIT, VMA_WRITE_BIT, VMA_EXEC_BIT); 469 + 470 + #if NUM_VMA_FLAG_BITS > 64 471 + and = vma_flags_and(&flags1, VMA_READ_BIT, VMA_WRITE_BIT, VMA_EXEC_BIT, 472 + 64); 473 + ASSERT_FLAGS_SAME(&and, VMA_READ_BIT, VMA_WRITE_BIT, VMA_EXEC_BIT, 64); 474 + 475 + and = vma_flags_and(&flags1, VMA_READ_BIT, VMA_WRITE_BIT, VMA_EXEC_BIT, 476 + 64, 65); 477 + ASSERT_FLAGS_SAME(&and, VMA_READ_BIT, VMA_WRITE_BIT, VMA_EXEC_BIT, 64, 478 + 65); 479 + #endif 480 + 481 + /* And against some missing values. */ 482 + 483 + and = vma_flags_and(&flags1, VMA_READ_BIT, VMA_WRITE_BIT, VMA_EXEC_BIT, 484 + VMA_IO_BIT); 485 + ASSERT_FLAGS_SAME(&and, VMA_READ_BIT, VMA_WRITE_BIT, VMA_EXEC_BIT); 486 + 487 + and = vma_flags_and(&flags1, VMA_READ_BIT, VMA_WRITE_BIT, VMA_EXEC_BIT, 488 + VMA_IO_BIT, VMA_RAND_READ_BIT); 489 + ASSERT_FLAGS_SAME(&and, VMA_READ_BIT, VMA_WRITE_BIT, VMA_EXEC_BIT); 490 + 491 + #if NUM_VMA_FLAG_BITS > 64 492 + and = vma_flags_and(&flags1, VMA_READ_BIT, VMA_WRITE_BIT, VMA_EXEC_BIT, 493 + VMA_IO_BIT, VMA_RAND_READ_BIT, 69); 494 + ASSERT_FLAGS_SAME(&and, VMA_READ_BIT, VMA_WRITE_BIT, VMA_EXEC_BIT); 495 + #endif 496 + 497 + return true; 498 + } 499 + 366 500 static void run_vma_tests(int *num_tests, int *num_fail) 367 501 { 368 502 TEST(copy_vma); ··· 506 372 TEST(vma_flags_test); 507 373 TEST(vma_flags_test_any); 508 374 TEST(vma_flags_clear); 375 + TEST(vma_flags_empty); 376 + TEST(vma_flags_diff); 377 + TEST(vma_flags_and); 509 378 }