162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * hugepage-shm: 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * Example of using huge page memory in a user application using Sys V shared 662306a36Sopenharmony_ci * memory system calls. In this example the app is requesting 256MB of 762306a36Sopenharmony_ci * memory that is backed by huge pages. The application uses the flag 862306a36Sopenharmony_ci * SHM_HUGETLB in the shmget system call to inform the kernel that it is 962306a36Sopenharmony_ci * requesting huge pages. 1062306a36Sopenharmony_ci * 1162306a36Sopenharmony_ci * For the ia64 architecture, the Linux kernel reserves Region number 4 for 1262306a36Sopenharmony_ci * huge pages. That means that if one requires a fixed address, a huge page 1362306a36Sopenharmony_ci * aligned address starting with 0x800000... will be required. If a fixed 1462306a36Sopenharmony_ci * address is not required, the kernel will select an address in the proper 1562306a36Sopenharmony_ci * range. 1662306a36Sopenharmony_ci * Other architectures, such as ppc64, i386 or x86_64 are not so constrained. 1762306a36Sopenharmony_ci * 1862306a36Sopenharmony_ci * Note: The default shared memory limit is quite low on many kernels, 1962306a36Sopenharmony_ci * you may need to increase it via: 2062306a36Sopenharmony_ci * 2162306a36Sopenharmony_ci * echo 268435456 > /proc/sys/kernel/shmmax 2262306a36Sopenharmony_ci * 2362306a36Sopenharmony_ci * This will increase the maximum size per shared memory segment to 256MB. 2462306a36Sopenharmony_ci * The other limit that you will hit eventually is shmall which is the 2562306a36Sopenharmony_ci * total amount of shared memory in pages. To set it to 16GB on a system 2662306a36Sopenharmony_ci * with a 4kB pagesize do: 2762306a36Sopenharmony_ci * 2862306a36Sopenharmony_ci * echo 4194304 > /proc/sys/kernel/shmall 2962306a36Sopenharmony_ci */ 3062306a36Sopenharmony_ci 3162306a36Sopenharmony_ci#include <stdlib.h> 3262306a36Sopenharmony_ci#include <stdio.h> 3362306a36Sopenharmony_ci#include <sys/types.h> 3462306a36Sopenharmony_ci#include <sys/ipc.h> 3562306a36Sopenharmony_ci#include <sys/shm.h> 3662306a36Sopenharmony_ci#include <sys/mman.h> 3762306a36Sopenharmony_ci 3862306a36Sopenharmony_ci#define LENGTH (256UL*1024*1024) 3962306a36Sopenharmony_ci 4062306a36Sopenharmony_ci#define dprintf(x) printf(x) 4162306a36Sopenharmony_ci 4262306a36Sopenharmony_ci/* Only ia64 requires this */ 4362306a36Sopenharmony_ci#ifdef __ia64__ 4462306a36Sopenharmony_ci#define ADDR (void *)(0x8000000000000000UL) 4562306a36Sopenharmony_ci#define SHMAT_FLAGS (SHM_RND) 4662306a36Sopenharmony_ci#else 4762306a36Sopenharmony_ci#define ADDR (void *)(0x0UL) 4862306a36Sopenharmony_ci#define SHMAT_FLAGS (0) 4962306a36Sopenharmony_ci#endif 5062306a36Sopenharmony_ci 5162306a36Sopenharmony_ciint main(void) 5262306a36Sopenharmony_ci{ 5362306a36Sopenharmony_ci int shmid; 5462306a36Sopenharmony_ci unsigned long i; 5562306a36Sopenharmony_ci char *shmaddr; 5662306a36Sopenharmony_ci 5762306a36Sopenharmony_ci shmid = shmget(2, LENGTH, SHM_HUGETLB | IPC_CREAT | SHM_R | SHM_W); 5862306a36Sopenharmony_ci if (shmid < 0) { 5962306a36Sopenharmony_ci perror("shmget"); 6062306a36Sopenharmony_ci exit(1); 6162306a36Sopenharmony_ci } 6262306a36Sopenharmony_ci printf("shmid: 0x%x\n", shmid); 6362306a36Sopenharmony_ci 6462306a36Sopenharmony_ci shmaddr = shmat(shmid, ADDR, SHMAT_FLAGS); 6562306a36Sopenharmony_ci if (shmaddr == (char *)-1) { 6662306a36Sopenharmony_ci perror("Shared memory attach failure"); 6762306a36Sopenharmony_ci shmctl(shmid, IPC_RMID, NULL); 6862306a36Sopenharmony_ci exit(2); 6962306a36Sopenharmony_ci } 7062306a36Sopenharmony_ci printf("shmaddr: %p\n", shmaddr); 7162306a36Sopenharmony_ci 7262306a36Sopenharmony_ci dprintf("Starting the writes:\n"); 7362306a36Sopenharmony_ci for (i = 0; i < LENGTH; i++) { 7462306a36Sopenharmony_ci shmaddr[i] = (char)(i); 7562306a36Sopenharmony_ci if (!(i % (1024 * 1024))) 7662306a36Sopenharmony_ci dprintf("."); 7762306a36Sopenharmony_ci } 7862306a36Sopenharmony_ci dprintf("\n"); 7962306a36Sopenharmony_ci 8062306a36Sopenharmony_ci dprintf("Starting the Check..."); 8162306a36Sopenharmony_ci for (i = 0; i < LENGTH; i++) 8262306a36Sopenharmony_ci if (shmaddr[i] != (char)i) { 8362306a36Sopenharmony_ci printf("\nIndex %lu mismatched\n", i); 8462306a36Sopenharmony_ci exit(3); 8562306a36Sopenharmony_ci } 8662306a36Sopenharmony_ci dprintf("Done.\n"); 8762306a36Sopenharmony_ci 8862306a36Sopenharmony_ci if (shmdt((const void *)shmaddr) != 0) { 8962306a36Sopenharmony_ci perror("Detach failure"); 9062306a36Sopenharmony_ci shmctl(shmid, IPC_RMID, NULL); 9162306a36Sopenharmony_ci exit(4); 9262306a36Sopenharmony_ci } 9362306a36Sopenharmony_ci 9462306a36Sopenharmony_ci shmctl(shmid, IPC_RMID, NULL); 9562306a36Sopenharmony_ci 9662306a36Sopenharmony_ci return 0; 9762306a36Sopenharmony_ci} 98