za-fork.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2022 ARM Limited.
  4. * Original author: Mark Brown <broonie@kernel.org>
  5. */
  6. // SPDX-License-Identifier: GPL-2.0-only
  7. #include <linux/sched.h>
  8. #include <linux/wait.h>
  9. #include "kselftest.h"
  10. #define EXPECTED_TESTS 1
  11. int fork_test(void);
  12. int verify_fork(void);
  13. /*
  14. * If we fork the value in the parent should be unchanged and the
  15. * child should start with the same value. This is called from the
  16. * fork_test() asm function.
  17. */
  18. int fork_test_c(void)
  19. {
  20. pid_t newpid, waiting;
  21. int child_status, parent_result;
  22. newpid = fork();
  23. if (newpid == 0) {
  24. /* In child */
  25. if (!verify_fork()) {
  26. ksft_print_msg("ZA state invalid in child\n");
  27. exit(0);
  28. } else {
  29. exit(1);
  30. }
  31. }
  32. if (newpid < 0) {
  33. ksft_print_msg("fork() failed: %d\n", newpid);
  34. return 0;
  35. }
  36. parent_result = verify_fork();
  37. if (!parent_result)
  38. ksft_print_msg("ZA state invalid in parent\n");
  39. for (;;) {
  40. waiting = waitpid(newpid, &child_status, 0);
  41. if (waiting < 0) {
  42. if (errno == EINTR)
  43. continue;
  44. ksft_print_msg("waitpid() failed: %d\n", errno);
  45. return 0;
  46. }
  47. if (waiting != newpid) {
  48. ksft_print_msg("waitpid() returned wrong PID\n");
  49. return 0;
  50. }
  51. if (!WIFEXITED(child_status)) {
  52. ksft_print_msg("child did not exit\n");
  53. return 0;
  54. }
  55. return WEXITSTATUS(child_status) && parent_result;
  56. }
  57. }
  58. int main(int argc, char **argv)
  59. {
  60. int ret, i;
  61. ksft_print_header();
  62. ksft_set_plan(EXPECTED_TESTS);
  63. ksft_print_msg("PID: %d\n", getpid());
  64. /*
  65. * This test is run with nolibc which doesn't support hwcap and
  66. * it's probably disproportionate to implement so instead check
  67. * for the default vector length configuration in /proc.
  68. */
  69. ret = open("/proc/sys/abi/sme_default_vector_length", O_RDONLY, 0);
  70. if (ret >= 0) {
  71. ksft_test_result(fork_test(), "fork_test\n");
  72. } else {
  73. ksft_print_msg("SME not supported\n");
  74. for (i = 0; i < EXPECTED_TESTS; i++) {
  75. ksft_test_result_skip("fork_test\n");
  76. }
  77. }
  78. ksft_finished();
  79. return 0;
  80. }