1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /*
3 * Copyright (C) 2005-2006 David Gibson & Adam Litke, IBM Corporation.
4 * Author: David Gibson & Adam Litke
5 */
6
7 /*\
8 * [Description]
9 *
10 * Test shared memory behavior when multiple threads are attached to a
11 * segment. A segment is created and then children are spawned which
12 * attach, write, read (verify), and detach from the shared memory segment.
13 */
14
15 #include "tst_safe_sysv_ipc.h"
16 #include "hugetlb.h"
17
18 static int shmid = -1;
19
20 #define NR_HUGEPAGES 5
21 #define NUMPROCS MIN(50L, tst_ncpus_available())
22 #define MNTPOINT "hugetlbfs/"
23
24 static long hpage_size;
25
do_child(int thread,unsigned long size)26 static void do_child(int thread, unsigned long size)
27 {
28 volatile char *shmaddr;
29 int j;
30 unsigned long k;
31
32 for (j = 0; j < 5; j++) {
33 shmaddr = SAFE_SHMAT(shmid, 0, SHM_RND);
34
35 for (k = 0; k < size; k++)
36 shmaddr[k] = (char) (k);
37 for (k = 0; k < size; k++)
38 if (shmaddr[k] != (char)k) {
39 tst_res(TFAIL, "Thread %d, Offset %lu mismatch", thread, k);
40 goto cleanup;
41 }
42
43 SAFE_SHMDT((const void *)shmaddr);
44 }
45
46 cleanup:
47 exit(0);
48 }
49
run_test(void)50 static void run_test(void)
51 {
52 unsigned long size;
53 int pid;
54 int i;
55
56 size = hpage_size * NR_HUGEPAGES;
57 shmid = SAFE_SHMGET(2, size, SHM_HUGETLB|IPC_CREAT|SHM_R|SHM_W);
58
59 for (i = 0; i < NUMPROCS; i++) {
60 pid = SAFE_FORK();
61
62 if (pid == 0)
63 do_child(i, size);
64 }
65
66 tst_reap_children();
67 tst_res(TPASS, "Successfully tested shared hugetlb memory with multiple procs");
68 }
69
setup(void)70 static void setup(void)
71 {
72 hpage_size = tst_get_hugepage_size();
73 }
74
cleanup(void)75 static void cleanup(void)
76 {
77 if (shmid >= 0)
78 SAFE_SHMCTL(shmid, IPC_RMID, NULL);
79 }
80
81 static struct tst_test test = {
82 .needs_root = 1,
83 .mntpoint = MNTPOINT,
84 .needs_hugetlbfs = 1,
85 .needs_tmpdir = 1,
86 .forks_child = 1,
87 .setup = setup,
88 .cleanup = cleanup,
89 .test_all = run_test,
90 .hugepages = {NR_HUGEPAGES, TST_NEEDS},
91 };
92