ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / Documentation / vm / hugetlbpage.txt
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 of modern architectures.  For example, IA-32
5 architecture supports 4K and 4M (2M in PAE mode) page sizes, IA-64
6 architecture supports multiple page sizes 4K, 8K, 64K, 256K, 1M, 4M, 16M,
7 256M.  A TLB is a cache of virtual-to-physical translations.  Typically this
8 is a very scarce resource on processor.  Operating systems try to make best
9 use of limited number of TLB resources.  This optimization is more critical
10 now as bigger and bigger physical memories (several GBs) are more readily
11 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
16 First the Linux kernel needs to be built with CONFIG_HUGETLB_PAGE (present
17 under Processor types and feature)  and CONFIG_HUGETLBFS (present under file
18 system option on config menu) config options.
19
20 The kernel built with hugepage support should show the number of configured
21 hugepages in the system by running the "cat /proc/meminfo" command.  
22
23 /proc/meminfo also provides information about the total number of hugetlb
24 pages configured in the kernel.  It also displays information about the
25 number of free hugetlb pages at any time.  It also displays information about
26 the configured hugepage size - this is needed for generating the proper
27 alignment and size of the arguments to the above system calls.
28
29 The output of "cat /proc/meminfo" will have output like:
30
31 .....
32 HugePages_Total: xxx
33 HugePages_Free:  yyy
34 Hugepagesize:    zzz KB
35
36 /proc/filesystems should also show a filesystem of type "hugetlbfs" configured
37 in the kernel.
38
39 /proc/sys/vm/nr_hugepages indicates the current number of configured hugetlb
40 pages in the kernel.  Super user can dynamically request more (or free some
41 pre-configured) hugepages. 
42 The allocation( or deallocation) of hugetlb pages is posible only if there are
43 enough physically contiguous free pages in system (freeing of hugepages is
44 possible only if there are enough hugetlb pages free that can be transfered 
45 back to regular memory pool).
46
47 Pages that are used as hugetlb pages are reserved inside the kernel and can
48 not be used for other purposes. 
49
50 Once the kernel with Hugetlb page support is built and running, a user can
51 use either the mmap system call or shared memory system calls to start using
52 the huge pages.  It is required that the system administrator preallocate
53 enough memory for huge page purposes.  
54
55 Use the following command to dynamically allocate/deallocate hugepages:
56
57         echo 20 > /proc/sys/vm/nr_hugepages
58
59 This command will try to configure 20 hugepages in the system.  The success
60 or failure of allocation depends on the amount of physically contiguous
61 memory that is preset in system at this time.  System administrators may want
62 to put this command in one of the local rc init file.  This will enable the
63 kernel to request huge pages early in the boot process (when the possibility
64 of getting physical contiguous pages is still very high).
65
66 If the user applications are going to request hugepages using mmap system
67 call, then it is required that system administrator mount a file system of
68 type hugetlbfs:
69
70         mount none /mnt/huge -t hugetlbfs <uid=value> <gid=value> <mode=value>
71                  <size=value> <nr_inodes=value>
72
73 This command mounts a (pseudo) filesystem of type hugetlbfs on the directory
74 /mnt/huge.  Any files created on /mnt/huge uses hugepages.  The uid and gid
75 options sets the owner and group of the root of the file system.  By default
76 the uid and gid of the current process are taken.  The mode option sets the
77 mode of root of file system to value & 0777.  This value is given in octal.
78 By default the value 0755 is picked. The size option sets the maximum value of
79 memory (huge pages) allowed for that filesystem (/mnt/huge). The size is
80 rounded down to HPAGE_SIZE.  The option nr_inode sets the maximum number of
81 inodes that /mnt/huge can use.  If the size or nr_inode options are not
82 provided on command line then no limits are set.  For size and nr_inodes
83 options, you can use [G|g]/[M|m]/[K|k] to represent giga/mega/kilo. For 
84 example, size=2K has the same meaning as size=2048. An example is given at 
85 the end of this document. 
86
87 read and write system calls are not supported on files that reside on hugetlb
88 file systems.
89
90 A regular chown, chgrp and chmod commands (with right permissions) could be
91 used to change the file attributes on hugetlbfs.
92
93 Also, it is important to note that no such mount command is required if the
94 applications are going to use only shmat/shmget system calls.  It is possible
95 for same or different applications to use any combination of mmaps and shm*
96 calls.  Though the mount of filesystem will be required for using mmaps.
97
98 /* Example of using hugepage in user application using Sys V shared memory
99  * system calls.  In this example, app is requesting memory of size 256MB that
100  * is backed by huge pages.  Application uses the flag SHM_HUGETLB in shmget
101  * system call to informt the kernel that it is requesting hugepages.  For
102  * IA-64 architecture, Linux kernel reserves Region number 4 for hugepages.
103  * That means the addresses starting with 0x800000....will need to be
104  * specified.
105  */
106 #include <sys/types.h>
107 #include <sys/shm.h>
108 #include <sys/types.h>
109 #include <sys/mman.h>
110 #include <errno.h>
111
112 extern int errno;
113 #define SHM_HUGETLB 04000
114 #define LPAGE_SIZE      (256UL*1024UL*1024UL)
115 #define         dprintf(x)  printf(x)
116 #define ADDR (0x8000000000000000UL)
117 main()
118 {
119         int shmid;
120         int     i, j, k;
121         volatile        char    *shmaddr;
122
123         if ((shmid =shmget(2, LPAGE_SIZE, SHM_HUGETLB|IPC_CREAT|SHM_R|SHM_W ))
124 < 0) {
125                 perror("Failure:");
126                 exit(1);
127         }
128         printf("shmid: 0x%x\n", shmid);
129         shmaddr = shmat(shmid, (void *)ADDR, SHM_RND) ;
130         if (errno != 0) {
131                 perror("Shared Memory Attach Failure:");
132                 exit(2);
133         }
134         printf("shmaddr: %p\n", shmaddr);
135
136         dprintf("Starting the writes:\n");
137         for (i=0;i<LPAGE_SIZE;i++) {
138                 shmaddr[i] = (char) (i);
139                 if (!(i%(1024*1024))) dprintf(".");
140         }
141         dprintf("\n");
142         dprintf("Starting the Check...");
143         for (i=0; i<LPAGE_SIZE;i++)
144                 if (shmaddr[i] != (char)i)
145                         printf("\nIndex %d mismatched.");
146         dprintf("Done.\n");
147         if (shmdt((const void *)shmaddr) != 0) {
148                 perror("Detached Failure:");
149                 exit (3);
150         }
151 }
152 *******************************************************************
153 *******************************************************************
154
155
156 /* Example of using hugepage in user application using mmap 
157  * system call.  Before running this application, make sure that
158  * administrator has mounted the hugetlbfs (on some directory like /mnt) using
159  * the command mount -t hugetlbfs nodev /mnt
160  * In this example, app is requesting memory of size 256MB that
161  * is backed by huge pages.  Application uses the flag SHM_HUGETLB in shmget
162  * system call to informt the kernel that it is requesting hugepages.  For
163  * IA-64 architecture, Linux kernel reserves Region number 4 for hugepages.
164  * That means the addresses starting with 0x800000....will need to be
165  * specified.
166  */
167 #include <unistd.h>
168 #include <stdio.h>
169 #include <sys/mman.h>
170 #include <fcntl.h>
171 #include <errno.h>
172
173 #define FILE_NAME "/mnt/hugepagefile"
174 #define LENGTH (256*1024*1024)
175 #define PROTECTION (PROT_READ | PROT_WRITE)
176 #define FLAGS   MAP_SHARED |MAP_FIXED
177 #define ADDRESS (char *)(0x60000000UL + 0x8000000000000000UL)
178
179 extern errno;
180
181 check_bytes(char *addr)
182 {
183         printf("First hex is %x\n", *((unsigned int *)addr));
184 }
185
186 write_bytes(char *addr)
187 {
188         int i;
189         for (i=0;i<LENGTH;i++)
190                 *(addr+i)=(char)i;
191 }
192 read_bytes(char *addr)
193 {
194         int i;
195         check_bytes(addr);
196         for (i=0;i<LENGTH;i++)
197                 if (*(addr+i)!=(char)i) {
198                         printf("Mismatch at %d\n", i);
199                         break;
200                 }
201 }
202 main()
203 {
204         unsigned long addr = 0;
205         int fd ;
206
207         fd = open(FILE_NAME, O_CREAT|O_RDWR, 0755);
208         if (fd < 0) {
209                 perror("Open failed");
210                 exit(errno);
211         }
212         addr = (unsigned long)mmap(ADDRESS, LENGTH, PROTECTION, FLAGS, fd, 0);
213         if (errno != 0)
214                 perror("mmap failed");
215         printf("Returned address is %p\n", addr);
216         check_bytes((char*)addr);
217         write_bytes((char*)addr);
218         read_bytes((char *)addr);
219 }