Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | |
| 2 | The intent of this file is to give a brief summary of hugetlbpage support in |
| 3 | the Linux kernel. This support is built on top of multiple page size support |
| 4 | that is provided by most modern architectures. For example, i386 |
| 5 | architecture supports 4K and 4M (2M in PAE mode) page sizes, ia64 |
| 6 | architecture supports multiple page sizes 4K, 8K, 64K, 256K, 1M, 4M, 16M, |
| 7 | 256M and ppc64 supports 4K and 16M. A TLB is a cache of virtual-to-physical |
| 8 | translations. Typically this is a very scarce resource on processor. |
| 9 | Operating systems try to make best use of limited number of TLB resources. |
| 10 | This optimization is more critical now as bigger and bigger physical memories |
| 11 | (several GBs) are more readily available. |
| 12 | |
| 13 | Users can use the huge page support in Linux kernel by either using the mmap |
| 14 | system call or standard SYSv shared memory system calls (shmget, shmat). |
| 15 | |
Muli Ben-Yehuda | 5c7ad51 | 2005-11-07 00:59:42 -0800 | [diff] [blame] | 16 | First the Linux kernel needs to be built with the CONFIG_HUGETLBFS |
| 17 | (present under "File systems") and CONFIG_HUGETLB_PAGE (selected |
| 18 | automatically when CONFIG_HUGETLBFS is selected) configuration |
| 19 | options. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
Lee Schermerhorn | 41a25e7 | 2009-09-21 17:01:24 -0700 | [diff] [blame] | 21 | The kernel built with huge page support should show the number of configured |
| 22 | huge pages in the system by running the "cat /proc/meminfo" command. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
| 24 | /proc/meminfo also provides information about the total number of hugetlb |
| 25 | pages configured in the kernel. It also displays information about the |
| 26 | number of free hugetlb pages at any time. It also displays information about |
Lee Schermerhorn | 41a25e7 | 2009-09-21 17:01:24 -0700 | [diff] [blame] | 27 | the configured huge page size - this is needed for generating the proper |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | alignment and size of the arguments to the above system calls. |
| 29 | |
Randy Dunlap | 21a26d4 | 2006-04-10 22:53:04 -0700 | [diff] [blame] | 30 | The output of "cat /proc/meminfo" will have lines like: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | |
| 32 | ..... |
Nishanth Aravamudan | d5dbac8 | 2007-12-17 16:20:25 -0800 | [diff] [blame] | 33 | HugePages_Total: vvv |
| 34 | HugePages_Free: www |
| 35 | HugePages_Rsvd: xxx |
| 36 | HugePages_Surp: yyy |
Randy Dunlap | 5e12227 | 2006-04-18 22:21:51 -0700 | [diff] [blame] | 37 | Hugepagesize: zzz kB |
| 38 | |
| 39 | where: |
Lee Schermerhorn | 41a25e7 | 2009-09-21 17:01:24 -0700 | [diff] [blame] | 40 | HugePages_Total is the size of the pool of huge pages. |
| 41 | HugePages_Free is the number of huge pages in the pool that are not yet |
| 42 | allocated. |
| 43 | HugePages_Rsvd is short for "reserved," and is the number of huge pages for |
| 44 | which a commitment to allocate from the pool has been made, |
| 45 | but no allocation has yet been made. Reserved huge pages |
| 46 | guarantee that an application will be able to allocate a |
| 47 | huge page from the pool of huge pages at fault time. |
| 48 | HugePages_Surp is short for "surplus," and is the number of huge pages in |
| 49 | the pool above the value in /proc/sys/vm/nr_hugepages. The |
| 50 | maximum number of surplus huge pages is controlled by |
| 51 | /proc/sys/vm/nr_overcommit_hugepages. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
| 53 | /proc/filesystems should also show a filesystem of type "hugetlbfs" configured |
| 54 | in the kernel. |
| 55 | |
| 56 | /proc/sys/vm/nr_hugepages indicates the current number of configured hugetlb |
| 57 | pages in the kernel. Super user can dynamically request more (or free some |
Lee Schermerhorn | 41a25e7 | 2009-09-21 17:01:24 -0700 | [diff] [blame] | 58 | pre-configured) huge pages. |
Muli Ben-Yehuda | 5c7ad51 | 2005-11-07 00:59:42 -0800 | [diff] [blame] | 59 | The allocation (or deallocation) of hugetlb pages is possible only if there are |
Lee Schermerhorn | 41a25e7 | 2009-09-21 17:01:24 -0700 | [diff] [blame] | 60 | enough physically contiguous free pages in system (freeing of huge pages is |
Randy Dunlap | 21a26d4 | 2006-04-10 22:53:04 -0700 | [diff] [blame] | 61 | possible only if there are enough hugetlb pages free that can be transferred |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | back to regular memory pool). |
| 63 | |
Randy Dunlap | 21a26d4 | 2006-04-10 22:53:04 -0700 | [diff] [blame] | 64 | Pages that are used as hugetlb pages are reserved inside the kernel and cannot |
| 65 | be used for other purposes. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | |
| 67 | Once the kernel with Hugetlb page support is built and running, a user can |
| 68 | use either the mmap system call or shared memory system calls to start using |
| 69 | the huge pages. It is required that the system administrator preallocate |
Muli Ben-Yehuda | 5c7ad51 | 2005-11-07 00:59:42 -0800 | [diff] [blame] | 70 | enough memory for huge page purposes. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | |
Lee Schermerhorn | 41a25e7 | 2009-09-21 17:01:24 -0700 | [diff] [blame] | 72 | The administrator can preallocate huge pages on the kernel boot command line by |
| 73 | specifying the "hugepages=N" parameter, where 'N' = the number of huge pages |
| 74 | requested. This is the most reliable method for preallocating huge pages as |
| 75 | memory has not yet become fragmented. |
| 76 | |
| 77 | Some platforms support multiple huge page sizes. To preallocate huge pages |
| 78 | of a specific size, one must preceed the huge pages boot command parameters |
| 79 | with a huge page size selection parameter "hugepagesz=<size>". <size> must |
| 80 | be specified in bytes with optional scale suffix [kKmMgG]. The default huge |
| 81 | page size may be selected with the "default_hugepagesz=<size>" boot parameter. |
| 82 | |
| 83 | /proc/sys/vm/nr_hugepages indicates the current number of configured [default |
| 84 | size] hugetlb pages in the kernel. Super user can dynamically request more |
| 85 | (or free some pre-configured) huge pages. |
| 86 | |
| 87 | Use the following command to dynamically allocate/deallocate default sized |
| 88 | huge pages: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | |
| 90 | echo 20 > /proc/sys/vm/nr_hugepages |
| 91 | |
Lee Schermerhorn | 41a25e7 | 2009-09-21 17:01:24 -0700 | [diff] [blame] | 92 | This command will try to configure 20 default sized huge pages in the system. |
| 93 | On a NUMA platform, the kernel will attempt to distribute the huge page pool |
| 94 | over the all on-line nodes. These huge pages, allocated when nr_hugepages |
| 95 | is increased, are called "persistent huge pages". |
Nishanth Aravamudan | d5dbac8 | 2007-12-17 16:20:25 -0800 | [diff] [blame] | 96 | |
Lee Schermerhorn | 41a25e7 | 2009-09-21 17:01:24 -0700 | [diff] [blame] | 97 | The success or failure of huge page allocation depends on the amount of |
| 98 | physically contiguous memory that is preset in system at the time of the |
| 99 | allocation attempt. If the kernel is unable to allocate huge pages from |
| 100 | some nodes in a NUMA system, it will attempt to make up the difference by |
| 101 | allocating extra pages on other nodes with sufficient available contiguous |
| 102 | memory, if any. |
| 103 | |
| 104 | System administrators may want to put this command in one of the local rc init |
| 105 | files. This will enable the kernel to request huge pages early in the boot |
| 106 | process when the possibility of getting physical contiguous pages is still |
| 107 | very high. Administrators can verify the number of huge pages actually |
| 108 | allocated by checking the sysctl or meminfo. To check the per node |
| 109 | distribution of huge pages in a NUMA system, use: |
| 110 | |
| 111 | cat /sys/devices/system/node/node*/meminfo | fgrep Huge |
| 112 | |
| 113 | /proc/sys/vm/nr_overcommit_hugepages specifies how large the pool of |
| 114 | huge pages can grow, if more huge pages than /proc/sys/vm/nr_hugepages are |
| 115 | requested by applications. Writing any non-zero value into this file |
| 116 | indicates that the hugetlb subsystem is allowed to try to obtain "surplus" |
| 117 | huge pages from the buddy allocator, when the normal pool is exhausted. As |
| 118 | these surplus huge pages go out of use, they are freed back to the buddy |
Nishanth Aravamudan | d5dbac8 | 2007-12-17 16:20:25 -0800 | [diff] [blame] | 119 | allocator. |
| 120 | |
Lee Schermerhorn | 41a25e7 | 2009-09-21 17:01:24 -0700 | [diff] [blame] | 121 | When increasing the huge page pool size via nr_hugepages, any surplus |
| 122 | pages will first be promoted to persistent huge pages. Then, additional |
| 123 | huge pages will be allocated, if necessary and if possible, to fulfill |
| 124 | the new huge page pool size. |
| 125 | |
| 126 | The administrator may shrink the pool of preallocated huge pages for |
| 127 | the default huge page size by setting the nr_hugepages sysctl to a |
| 128 | smaller value. The kernel will attempt to balance the freeing of huge pages |
| 129 | across all on-line nodes. Any free huge pages on the selected nodes will |
| 130 | be freed back to the buddy allocator. |
| 131 | |
Nishanth Aravamudan | 423bec4 | 2008-04-15 14:34:43 -0700 | [diff] [blame] | 132 | Caveat: Shrinking the pool via nr_hugepages such that it becomes less |
Lee Schermerhorn | 41a25e7 | 2009-09-21 17:01:24 -0700 | [diff] [blame] | 133 | than the number of huge pages in use will convert the balance to surplus |
Nishanth Aravamudan | 423bec4 | 2008-04-15 14:34:43 -0700 | [diff] [blame] | 134 | huge pages even if it would exceed the overcommit value. As long as |
Nishanth Aravamudan | d5dbac8 | 2007-12-17 16:20:25 -0800 | [diff] [blame] | 135 | this condition holds, however, no more surplus huge pages will be |
| 136 | allowed on the system until one of the two sysctls are increased |
| 137 | sufficiently, or the surplus huge pages go out of use and are freed. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | |
Lee Schermerhorn | 41a25e7 | 2009-09-21 17:01:24 -0700 | [diff] [blame] | 139 | With support for multiple huge page pools at run-time available, much of |
| 140 | the huge page userspace interface has been duplicated in sysfs. The above |
| 141 | information applies to the default huge page size which will be |
| 142 | controlled by the /proc interfaces for backwards compatibility. The root |
| 143 | huge page control directory in sysfs is: |
Nishanth Aravamudan | a343787 | 2008-07-23 21:27:44 -0700 | [diff] [blame] | 144 | |
| 145 | /sys/kernel/mm/hugepages |
| 146 | |
Lee Schermerhorn | 41a25e7 | 2009-09-21 17:01:24 -0700 | [diff] [blame] | 147 | For each huge page size supported by the running kernel, a subdirectory |
Nishanth Aravamudan | a343787 | 2008-07-23 21:27:44 -0700 | [diff] [blame] | 148 | will exist, of the form |
| 149 | |
| 150 | hugepages-${size}kB |
| 151 | |
| 152 | Inside each of these directories, the same set of files will exist: |
| 153 | |
| 154 | nr_hugepages |
| 155 | nr_overcommit_hugepages |
| 156 | free_hugepages |
| 157 | resv_hugepages |
| 158 | surplus_hugepages |
| 159 | |
Lee Schermerhorn | 41a25e7 | 2009-09-21 17:01:24 -0700 | [diff] [blame] | 160 | which function as described above for the default huge page-sized case. |
Nishanth Aravamudan | a343787 | 2008-07-23 21:27:44 -0700 | [diff] [blame] | 161 | |
Lee Schermerhorn | 41a25e7 | 2009-09-21 17:01:24 -0700 | [diff] [blame] | 162 | If the user applications are going to request huge pages using mmap system |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | call, then it is required that system administrator mount a file system of |
| 164 | type hugetlbfs: |
| 165 | |
Randy Dunlap | e73a75f | 2007-07-15 23:40:52 -0700 | [diff] [blame] | 166 | mount -t hugetlbfs \ |
| 167 | -o uid=<value>,gid=<value>,mode=<value>,size=<value>,nr_inodes=<value> \ |
| 168 | none /mnt/huge |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | |
| 170 | This command mounts a (pseudo) filesystem of type hugetlbfs on the directory |
Lee Schermerhorn | 41a25e7 | 2009-09-21 17:01:24 -0700 | [diff] [blame] | 171 | /mnt/huge. Any files created on /mnt/huge uses huge pages. The uid and gid |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | options sets the owner and group of the root of the file system. By default |
| 173 | the uid and gid of the current process are taken. The mode option sets the |
| 174 | mode of root of file system to value & 0777. This value is given in octal. |
| 175 | By default the value 0755 is picked. The size option sets the maximum value of |
| 176 | memory (huge pages) allowed for that filesystem (/mnt/huge). The size is |
Randy Dunlap | 21a26d4 | 2006-04-10 22:53:04 -0700 | [diff] [blame] | 177 | rounded down to HPAGE_SIZE. The option nr_inodes sets the maximum number of |
Randy Dunlap | e73a75f | 2007-07-15 23:40:52 -0700 | [diff] [blame] | 178 | inodes that /mnt/huge can use. If the size or nr_inodes option is not |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | provided on command line then no limits are set. For size and nr_inodes |
Muli Ben-Yehuda | 5c7ad51 | 2005-11-07 00:59:42 -0800 | [diff] [blame] | 180 | options, you can use [G|g]/[M|m]/[K|k] to represent giga/mega/kilo. For |
Randy Dunlap | e73a75f | 2007-07-15 23:40:52 -0700 | [diff] [blame] | 181 | example, size=2K has the same meaning as size=2048. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | |
Nishanth Aravamudan | d5dbac8 | 2007-12-17 16:20:25 -0800 | [diff] [blame] | 183 | While read system calls are supported on files that reside on hugetlb |
| 184 | file systems, write system calls are not. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | |
Randy Dunlap | 21a26d4 | 2006-04-10 22:53:04 -0700 | [diff] [blame] | 186 | Regular chown, chgrp, and chmod commands (with right permissions) could be |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | used to change the file attributes on hugetlbfs. |
| 188 | |
| 189 | Also, it is important to note that no such mount command is required if the |
Eric B Munson | 94bf5ce | 2009-09-21 17:03:48 -0700 | [diff] [blame^] | 190 | applications are going to use only shmat/shmget system calls or mmap with |
| 191 | MAP_HUGETLB. Users who wish to use hugetlb page via shared memory segment |
| 192 | should be a member of a supplementary group and system admin needs to |
| 193 | configure that gid into /proc/sys/vm/hugetlb_shm_group. It is possible for |
| 194 | same or different applications to use any combination of mmaps and shm* |
| 195 | calls, though the mount of filesystem will be required for using mmap calls |
| 196 | without MAP_HUGETLB. For an example of how to use mmap with MAP_HUGETLB see |
| 197 | map_hugetlb.c. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | |
| 199 | ******************************************************************* |
| 200 | |
| 201 | /* |
Lee Schermerhorn | 41a25e7 | 2009-09-21 17:01:24 -0700 | [diff] [blame] | 202 | * Example of using huge page memory in a user application using Sys V shared |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | * memory system calls. In this example the app is requesting 256MB of |
| 204 | * memory that is backed by huge pages. The application uses the flag |
| 205 | * SHM_HUGETLB in the shmget system call to inform the kernel that it is |
Lee Schermerhorn | 41a25e7 | 2009-09-21 17:01:24 -0700 | [diff] [blame] | 206 | * requesting huge pages. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | * |
| 208 | * For the ia64 architecture, the Linux kernel reserves Region number 4 for |
Lee Schermerhorn | 41a25e7 | 2009-09-21 17:01:24 -0700 | [diff] [blame] | 209 | * huge pages. That means the addresses starting with 0x800000... will need |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | * to be specified. Specifying a fixed address is not required on ppc64, |
| 211 | * i386 or x86_64. |
| 212 | * |
| 213 | * Note: The default shared memory limit is quite low on many kernels, |
| 214 | * you may need to increase it via: |
| 215 | * |
| 216 | * echo 268435456 > /proc/sys/kernel/shmmax |
| 217 | * |
| 218 | * This will increase the maximum size per shared memory segment to 256MB. |
| 219 | * The other limit that you will hit eventually is shmall which is the |
| 220 | * total amount of shared memory in pages. To set it to 16GB on a system |
| 221 | * with a 4kB pagesize do: |
| 222 | * |
| 223 | * echo 4194304 > /proc/sys/kernel/shmall |
| 224 | */ |
| 225 | #include <stdlib.h> |
| 226 | #include <stdio.h> |
| 227 | #include <sys/types.h> |
| 228 | #include <sys/ipc.h> |
| 229 | #include <sys/shm.h> |
| 230 | #include <sys/mman.h> |
| 231 | |
| 232 | #ifndef SHM_HUGETLB |
| 233 | #define SHM_HUGETLB 04000 |
| 234 | #endif |
| 235 | |
| 236 | #define LENGTH (256UL*1024*1024) |
| 237 | |
| 238 | #define dprintf(x) printf(x) |
| 239 | |
| 240 | /* Only ia64 requires this */ |
| 241 | #ifdef __ia64__ |
| 242 | #define ADDR (void *)(0x8000000000000000UL) |
| 243 | #define SHMAT_FLAGS (SHM_RND) |
| 244 | #else |
| 245 | #define ADDR (void *)(0x0UL) |
| 246 | #define SHMAT_FLAGS (0) |
| 247 | #endif |
| 248 | |
| 249 | int main(void) |
| 250 | { |
| 251 | int shmid; |
| 252 | unsigned long i; |
| 253 | char *shmaddr; |
| 254 | |
| 255 | if ((shmid = shmget(2, LENGTH, |
| 256 | SHM_HUGETLB | IPC_CREAT | SHM_R | SHM_W)) < 0) { |
| 257 | perror("shmget"); |
| 258 | exit(1); |
| 259 | } |
| 260 | printf("shmid: 0x%x\n", shmid); |
| 261 | |
| 262 | shmaddr = shmat(shmid, ADDR, SHMAT_FLAGS); |
| 263 | if (shmaddr == (char *)-1) { |
| 264 | perror("Shared memory attach failure"); |
| 265 | shmctl(shmid, IPC_RMID, NULL); |
| 266 | exit(2); |
| 267 | } |
| 268 | printf("shmaddr: %p\n", shmaddr); |
| 269 | |
| 270 | dprintf("Starting the writes:\n"); |
| 271 | for (i = 0; i < LENGTH; i++) { |
| 272 | shmaddr[i] = (char)(i); |
| 273 | if (!(i % (1024 * 1024))) |
| 274 | dprintf("."); |
| 275 | } |
| 276 | dprintf("\n"); |
| 277 | |
| 278 | dprintf("Starting the Check..."); |
| 279 | for (i = 0; i < LENGTH; i++) |
| 280 | if (shmaddr[i] != (char)i) |
| 281 | printf("\nIndex %lu mismatched\n", i); |
| 282 | dprintf("Done.\n"); |
| 283 | |
| 284 | if (shmdt((const void *)shmaddr) != 0) { |
| 285 | perror("Detach failure"); |
| 286 | shmctl(shmid, IPC_RMID, NULL); |
| 287 | exit(3); |
| 288 | } |
| 289 | |
| 290 | shmctl(shmid, IPC_RMID, NULL); |
| 291 | |
| 292 | return 0; |
| 293 | } |
| 294 | |
| 295 | ******************************************************************* |
| 296 | |
| 297 | /* |
Lee Schermerhorn | 41a25e7 | 2009-09-21 17:01:24 -0700 | [diff] [blame] | 298 | * Example of using huge page memory in a user application using the mmap |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | * system call. Before running this application, make sure that the |
| 300 | * administrator has mounted the hugetlbfs filesystem (on some directory |
| 301 | * like /mnt) using the command mount -t hugetlbfs nodev /mnt. In this |
| 302 | * example, the app is requesting memory of size 256MB that is backed by |
| 303 | * huge pages. |
| 304 | * |
Lee Schermerhorn | 41a25e7 | 2009-09-21 17:01:24 -0700 | [diff] [blame] | 305 | * For ia64 architecture, Linux kernel reserves Region number 4 for huge pages. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | * That means the addresses starting with 0x800000... will need to be |
| 307 | * specified. Specifying a fixed address is not required on ppc64, i386 |
| 308 | * or x86_64. |
| 309 | */ |
| 310 | #include <stdlib.h> |
| 311 | #include <stdio.h> |
| 312 | #include <unistd.h> |
| 313 | #include <sys/mman.h> |
| 314 | #include <fcntl.h> |
| 315 | |
| 316 | #define FILE_NAME "/mnt/hugepagefile" |
| 317 | #define LENGTH (256UL*1024*1024) |
| 318 | #define PROTECTION (PROT_READ | PROT_WRITE) |
| 319 | |
| 320 | /* Only ia64 requires this */ |
| 321 | #ifdef __ia64__ |
| 322 | #define ADDR (void *)(0x8000000000000000UL) |
| 323 | #define FLAGS (MAP_SHARED | MAP_FIXED) |
| 324 | #else |
| 325 | #define ADDR (void *)(0x0UL) |
| 326 | #define FLAGS (MAP_SHARED) |
| 327 | #endif |
| 328 | |
| 329 | void check_bytes(char *addr) |
| 330 | { |
| 331 | printf("First hex is %x\n", *((unsigned int *)addr)); |
| 332 | } |
| 333 | |
| 334 | void write_bytes(char *addr) |
| 335 | { |
| 336 | unsigned long i; |
| 337 | |
| 338 | for (i = 0; i < LENGTH; i++) |
| 339 | *(addr + i) = (char)i; |
| 340 | } |
| 341 | |
| 342 | void read_bytes(char *addr) |
| 343 | { |
| 344 | unsigned long i; |
| 345 | |
| 346 | check_bytes(addr); |
| 347 | for (i = 0; i < LENGTH; i++) |
| 348 | if (*(addr + i) != (char)i) { |
| 349 | printf("Mismatch at %lu\n", i); |
| 350 | break; |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | int main(void) |
| 355 | { |
| 356 | void *addr; |
| 357 | int fd; |
| 358 | |
| 359 | fd = open(FILE_NAME, O_CREAT | O_RDWR, 0755); |
| 360 | if (fd < 0) { |
| 361 | perror("Open failed"); |
| 362 | exit(1); |
| 363 | } |
| 364 | |
| 365 | addr = mmap(ADDR, LENGTH, PROTECTION, FLAGS, fd, 0); |
| 366 | if (addr == MAP_FAILED) { |
| 367 | perror("mmap"); |
| 368 | unlink(FILE_NAME); |
| 369 | exit(1); |
| 370 | } |
| 371 | |
| 372 | printf("Returned address is %p\n", addr); |
| 373 | check_bytes(addr); |
| 374 | write_bytes(addr); |
| 375 | read_bytes(addr); |
| 376 | |
| 377 | munmap(addr, LENGTH); |
| 378 | close(fd); |
| 379 | unlink(FILE_NAME); |
| 380 | |
| 381 | return 0; |
| 382 | } |