xref: /OK3568_Linux_fs/kernel/tools/testing/selftests/vm/hugepage-shm.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * hugepage-shm:
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Example of using huge page memory in a user application using Sys V shared
6*4882a593Smuzhiyun  * memory system calls.  In this example the app is requesting 256MB of
7*4882a593Smuzhiyun  * memory that is backed by huge pages.  The application uses the flag
8*4882a593Smuzhiyun  * SHM_HUGETLB in the shmget system call to inform the kernel that it is
9*4882a593Smuzhiyun  * requesting huge pages.
10*4882a593Smuzhiyun  *
11*4882a593Smuzhiyun  * For the ia64 architecture, the Linux kernel reserves Region number 4 for
12*4882a593Smuzhiyun  * huge pages.  That means that if one requires a fixed address, a huge page
13*4882a593Smuzhiyun  * aligned address starting with 0x800000... will be required.  If a fixed
14*4882a593Smuzhiyun  * address is not required, the kernel will select an address in the proper
15*4882a593Smuzhiyun  * range.
16*4882a593Smuzhiyun  * Other architectures, such as ppc64, i386 or x86_64 are not so constrained.
17*4882a593Smuzhiyun  *
18*4882a593Smuzhiyun  * Note: The default shared memory limit is quite low on many kernels,
19*4882a593Smuzhiyun  * you may need to increase it via:
20*4882a593Smuzhiyun  *
21*4882a593Smuzhiyun  * echo 268435456 > /proc/sys/kernel/shmmax
22*4882a593Smuzhiyun  *
23*4882a593Smuzhiyun  * This will increase the maximum size per shared memory segment to 256MB.
24*4882a593Smuzhiyun  * The other limit that you will hit eventually is shmall which is the
25*4882a593Smuzhiyun  * total amount of shared memory in pages. To set it to 16GB on a system
26*4882a593Smuzhiyun  * with a 4kB pagesize do:
27*4882a593Smuzhiyun  *
28*4882a593Smuzhiyun  * echo 4194304 > /proc/sys/kernel/shmall
29*4882a593Smuzhiyun  */
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun #include <stdlib.h>
32*4882a593Smuzhiyun #include <stdio.h>
33*4882a593Smuzhiyun #include <sys/types.h>
34*4882a593Smuzhiyun #include <sys/ipc.h>
35*4882a593Smuzhiyun #include <sys/shm.h>
36*4882a593Smuzhiyun #include <sys/mman.h>
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun #ifndef SHM_HUGETLB
39*4882a593Smuzhiyun #define SHM_HUGETLB 04000
40*4882a593Smuzhiyun #endif
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun #define LENGTH (256UL*1024*1024)
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun #define dprintf(x)  printf(x)
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun /* Only ia64 requires this */
47*4882a593Smuzhiyun #ifdef __ia64__
48*4882a593Smuzhiyun #define ADDR (void *)(0x8000000000000000UL)
49*4882a593Smuzhiyun #define SHMAT_FLAGS (SHM_RND)
50*4882a593Smuzhiyun #else
51*4882a593Smuzhiyun #define ADDR (void *)(0x0UL)
52*4882a593Smuzhiyun #define SHMAT_FLAGS (0)
53*4882a593Smuzhiyun #endif
54*4882a593Smuzhiyun 
main(void)55*4882a593Smuzhiyun int main(void)
56*4882a593Smuzhiyun {
57*4882a593Smuzhiyun 	int shmid;
58*4882a593Smuzhiyun 	unsigned long i;
59*4882a593Smuzhiyun 	char *shmaddr;
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun 	shmid = shmget(2, LENGTH, SHM_HUGETLB | IPC_CREAT | SHM_R | SHM_W);
62*4882a593Smuzhiyun 	if (shmid < 0) {
63*4882a593Smuzhiyun 		perror("shmget");
64*4882a593Smuzhiyun 		exit(1);
65*4882a593Smuzhiyun 	}
66*4882a593Smuzhiyun 	printf("shmid: 0x%x\n", shmid);
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun 	shmaddr = shmat(shmid, ADDR, SHMAT_FLAGS);
69*4882a593Smuzhiyun 	if (shmaddr == (char *)-1) {
70*4882a593Smuzhiyun 		perror("Shared memory attach failure");
71*4882a593Smuzhiyun 		shmctl(shmid, IPC_RMID, NULL);
72*4882a593Smuzhiyun 		exit(2);
73*4882a593Smuzhiyun 	}
74*4882a593Smuzhiyun 	printf("shmaddr: %p\n", shmaddr);
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun 	dprintf("Starting the writes:\n");
77*4882a593Smuzhiyun 	for (i = 0; i < LENGTH; i++) {
78*4882a593Smuzhiyun 		shmaddr[i] = (char)(i);
79*4882a593Smuzhiyun 		if (!(i % (1024 * 1024)))
80*4882a593Smuzhiyun 			dprintf(".");
81*4882a593Smuzhiyun 	}
82*4882a593Smuzhiyun 	dprintf("\n");
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun 	dprintf("Starting the Check...");
85*4882a593Smuzhiyun 	for (i = 0; i < LENGTH; i++)
86*4882a593Smuzhiyun 		if (shmaddr[i] != (char)i) {
87*4882a593Smuzhiyun 			printf("\nIndex %lu mismatched\n", i);
88*4882a593Smuzhiyun 			exit(3);
89*4882a593Smuzhiyun 		}
90*4882a593Smuzhiyun 	dprintf("Done.\n");
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun 	if (shmdt((const void *)shmaddr) != 0) {
93*4882a593Smuzhiyun 		perror("Detach failure");
94*4882a593Smuzhiyun 		shmctl(shmid, IPC_RMID, NULL);
95*4882a593Smuzhiyun 		exit(4);
96*4882a593Smuzhiyun 	}
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun 	shmctl(shmid, IPC_RMID, NULL);
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun 	return 0;
101*4882a593Smuzhiyun }
102