]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - share/doc/papers/newvm/1.t
dts: Import DTS from Linux 5.6
[FreeBSD/FreeBSD.git] / share / doc / papers / newvm / 1.t
1 .\" Copyright (c) 1986 The Regents of the University of California.
2 .\" All rights reserved.
3 .\"
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
6 .\" are met:
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\"    notice, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\"    notice, this list of conditions and the following disclaimer in the
11 .\"    documentation and/or other materials provided with the distribution.
12 .\" 3. Neither the name of the University nor the names of its contributors
13 .\"    may be used to endorse or promote products derived from this software
14 .\"    without specific prior written permission.
15 .\"
16 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 .\" SUCH DAMAGE.
27 .\"
28 .\"     @(#)1.t 5.1 (Berkeley) 4/16/91
29 .\" $FreeBSD$
30 .\"
31 .NH
32 Motivations for a New Virtual Memory System
33 .PP
34 The virtual memory system distributed with Berkeley UNIX has served
35 its design goals admirably well over the ten years of its existence.
36 However the relentless advance of technology has begun to render it
37 obsolete.
38 This section of the paper describes the current design,
39 points out the current technological trends,
40 and attempts to define the new design considerations that should
41 be taken into account in a new virtual memory design.
42 .NH 2
43 Implementation of 4.3BSD virtual memory
44 .PP
45 All Berkeley Software Distributions through 4.3BSD
46 have used the same virtual memory design.
47 All processes, whether active or sleeping, have some amount of
48 virtual address space associated with them.
49 This virtual address space
50 is the combination of the amount of address space with which they initially
51 started plus any stack or heap expansions that they have made.
52 All requests for address space are allocated from available swap space
53 at the time that they are first made;
54 if there is insufficient swap space left to honor the allocation,
55 the system call requesting the address space fails synchronously.
56 Thus, the limit to available virtual memory is established by the
57 amount of swap space allocated to the system.
58 .PP
59 Memory pages are used in a sort of shell game to contain the
60 contents of recently accessed locations.
61 As a process first references a location
62 a new page is allocated and filled either with initialized data or
63 zeros (for new stack and break pages).
64 As the supply of free pages begins to run out, dirty pages are
65 pushed to the previously allocated swap space so that they can be reused
66 to contain newly faulted pages.
67 If a previously accessed page that has been pushed to swap is once
68 again used, a free page is reallocated and filled from the swap area
69 [Babaoglu79], [Someren84].
70 .NH 2
71 Design assumptions for 4.3BSD virtual memory
72 .PP
73 The design criteria for the current virtual memory implementation
74 were made in 1979.
75 At that time the cost of memory was about a thousand times greater per
76 byte than magnetic disks.
77 Most machines were used as centralized time sharing machines.
78 These machines had far more disk storage than they had memory
79 and given the cost tradeoff between memory and disk storage,
80 wanted to make maximal use of the memory even at the cost of
81 wasting some of the disk space or generating extra disk I/O.
82 .PP
83 The primary motivation for virtual memory was to allow the
84 system to run individual programs whose address space exceeded
85 the memory capacity of the machine.
86 Thus the virtual memory capability allowed programs to be run that
87 could not have been run on a swap based system.
88 Equally important in the large central timesharing environment
89 was the ability to allow the sum of the memory requirements of
90 all active processes to exceed the amount of physical memory on
91 the machine.
92 The expected mode of operation for which the system was tuned
93 was to have the sum of active virtual memory be one and a half
94 to two times the physical memory on the machine.
95 .PP
96 At the time that the virtual memory system was designed,
97 most machines ran with little or no networking.
98 All the file systems were contained on disks that were
99 directly connected to the machine.
100 Similarly all the disk space devoted to swap space was also
101 directly connected.
102 Thus the speed and latency with which file systems could be accessed
103 were roughly equivalent to the speed and latency with which swap
104 space could be accessed.
105 Given the high cost of memory there was little incentive to have
106 the kernel keep track of the contents of the swap area once a process
107 exited since it could almost as easily and quickly be reread from the
108 file system.
109 .NH 2
110 New influences
111 .PP
112 In the ten years since the current virtual memory system was designed,
113 many technological advances have occurred.
114 One effect of the technological revolution is that the
115 micro-processor has become powerful enough to allow users to have their
116 own personal workstations.
117 Thus the computing environment is moving away from a purely centralized
118 time sharing model to an environment in which users have a
119 computer on their desk.
120 This workstation is linked through a network to a centralized
121 pool of machines that provide filing, computing, and spooling services.
122 The workstations tend to have a large quantity of memory, 
123 but little or no disk space.
124 Because users do not want to be bothered with backing up their disks,
125 and because of the difficulty of having a centralized administration
126 backing up hundreds of small disks, these local disks are typically 
127 used only for temporary storage and as swap space.
128 Long term storage is managed by the central file server.
129 .PP
130 Another major technical advance has been in all levels of storage capacity.
131 In the last ten years we have experienced a factor of four decrease in the
132 cost per byte of disk storage.
133 In this same period of time the cost per byte of memory has dropped
134 by a factor of a hundred!
135 Thus the cost per byte of memory compared to the cost per byte of disk is
136 approaching a difference of only about a factor of ten.
137 The effect of this change is that the way in which a machine is used
138 is beginning to change dramatically.
139 As the amount of physical memory on machines increases and the number of
140 users per machine decreases, the expected
141 mode of operation is changing from that of supporting more active virtual 
142 memory than physical memory to that of having a surplus of memory that can
143 be used for other purposes.
144 .PP
145 Because many machines will have more physical memory than they do swap
146 space (with diskless workstations as an extreme example!),
147 it is no longer reasonable to limit the maximum virtual memory
148 to the amount of swap space as is done in the current design.
149 Consequently, the new design will allow the maximum virtual memory
150 to be the sum of physical memory plus swap space.
151 For machines with no swap space, the maximum virtual memory will
152 be governed by the amount of physical memory.
153 .PP
154 Another effect of the current technology is that the latency and overhead
155 associated with accessing the file system is considerably higher
156 since the access must be over the network
157 rather than to a locally-attached disk.
158 One use of the surplus memory would be to
159 maintain a cache of recently used files;
160 repeated uses of these files would require at most a verification from
161 the file server that the data was up to date.
162 Under the current design, file caching is done by the buffer pool,
163 while the free memory is maintained in a separate pool.
164 The new design should have only a single memory pool so that any
165 free memory can be used to cache recently accessed files.
166 .PP
167 Another portion of the memory will be used to keep track of the contents
168 of the blocks on any locally-attached swap space analogously
169 to the way that memory pages are handled.
170 Thus inactive swap blocks can also be used to cache less-recently-used
171 file data.
172 Since the swap disk is locally attached, it can be much more quickly
173 accessed than a remotely located file system.
174 This design allows the user to simply allocate their entire local disk
175 to swap space, thus allowing the system to decide what files should
176 be cached to maximize its usefulness.
177 This design has two major benefits.
178 It relieves the user of deciding what files
179 should be kept in a small local file system.
180 It also insures that all modified files are migrated back to the
181 file server in a timely fashion, thus eliminating the need to dump
182 the local disk or push the files manually.
183 .NH
184 User Interface
185 .PP
186 This section outlines our new virtual memory interface as it is
187 currently envisioned.
188 The details of the system call interface are contained in Appendix A.
189 .NH 2
190 Regions
191 .PP
192 The virtual memory interface is designed to support both large,
193 sparse address spaces as well as small, densely-used address spaces.
194 In this context, ``small'' is an address space roughly the
195 size of the physical memory on the machine, 
196 while ``large'' may extend up to the maximum addressability of the machine.
197 A process may divide its address space up into a number of regions.
198 Initially a process begins with four regions; 
199 a shared read-only fill-on-demand region with its text,
200 a private fill-on-demand region for its initialized data,
201 a private zero-fill-on-demand region for its uninitialized data and heap,
202 and a private zero-fill-on-demand region for its stack.
203 In addition to these regions, a process may allocate new ones.
204 The regions may not overlap and the system may impose an alignment
205 constraint, but the size of the region should not be limited
206 beyond the constraints of the size of the virtual address space.
207 .PP
208 Each new region may be mapped either as private or shared.
209 When it is privately mapped, changes to the contents of the region
210 are not reflected to any other process that map the same region.
211 Regions may be mapped read-only or read-write.
212 As an example, a shared library would be implemented as two regions;
213 a shared read-only region for the text, and a private read-write
214 region for the global variables associated with the library.
215 .PP
216 A region may be allocated with one of several allocation strategies.
217 It may map some memory hardware on the machine such as a frame buffer.
218 Since the hardware is responsible for storing the data,
219 such regions must be exclusive use if they are privately mapped.
220 .PP
221 A region can map all or part of a file.
222 As the pages are first accessed, the region is filled in with the
223 appropriate part of the file.
224 If the region is mapped read-write and shared, changes to the
225 contents of the region are reflected back into the contents of the file.
226 If the region is read-write but private,
227 changes to the region are copied to a private page that is not
228 visible to other processes mapping the file,
229 and these modified pages are not reflected back to the file.
230 .PP
231 The final type of region is ``anonymous memory''.
232 Uninitialed data uses such a region, privately mapped;
233 it is zero-fill-on-demand and its contents are abandoned
234 when the last reference is dropped.
235 Unlike a region that is mapped from a file,
236 the contents of an anonymous region will never be read from or
237 written to a disk unless memory is short and part of the region
238 must be paged to a swap area.
239 If one of these regions is mapped shared,
240 then all processes see the changes in the region.
241 This difference has important performance considerations;
242 the overhead of reading, flushing, and possibly allocating a file
243 is much higher than simply zeroing memory.
244 .PP
245 If several processes wish to share a region,
246 then they must have some way of rendezvousing.
247 For a mapped file this is easy;
248 the name of the file is used as the rendezvous point.
249 However, processes may not need the semantics of mapped files
250 nor be willing to pay the overhead associated with them.
251 For anonymous memory they must use some other rendezvous point.
252 Our current interface allows processes to associate a
253 descriptor with a region, which it may then pass to other
254 processes that wish to attach to the region.
255 Such a descriptor may be bound into the UNIX file system
256 name space so that other processes can find it just as
257 they would with a mapped file.
258 .NH 2
259 Shared memory as high speed interprocess communication
260 .PP
261 The primary use envisioned for shared memory is to
262 provide a high speed interprocess communication (IPC) mechanism
263 between cooperating processes.
264 Existing IPC mechanisms (\fIi.e.\fP pipes, sockets, or streams)
265 require a system call to hand off a set
266 of data destined for another process, and another system call
267 by the recipient process to receive the data.
268 Even if the data can be transferred by remapping the data pages
269 to avoid a memory to memory copy, the overhead of doing the system
270 calls limits the throughput of all but the largest transfers.
271 Shared memory, by contrast, allows processes to share data at any
272 level of granularity without system intervention.
273 .PP
274 However, to maintain all but the simplest of data structures,
275 the processes must serialize their modifications to shared
276 data structures if they are to avoid corrupting them.
277 This serialization is typically done with semaphores.
278 Unfortunately, most implementations of semaphores are
279 done with system calls. 
280 Thus processes are once again limited by the need to do two
281 system calls per transaction, one to lock the semaphore, the
282 second to release it.
283 The net effect is that the shared memory model provides little if 
284 any improvement in interprocess bandwidth.
285 .PP
286 To achieve a significant improvement in interprocess bandwidth
287 requires a large decrease in the number of system calls needed to
288 achieve the interaction.
289 In profiling applications that use
290 serialization locks such as the UNIX kernel,
291 one typically finds that most locks are not contested.
292 Thus if one can find a way to avoid doing a system call in the case
293 in which a lock is not contested,
294 one would expect to be able to dramatically reduce the number
295 of system calls needed to achieve serialization.
296 .PP
297 In our design, cooperating processes manage their semaphores
298 in their own address space.
299 In the typical case, a process executes an atomic test-and-set instruction
300 to acquire a lock, finds it free, and thus is able to get it.
301 Only in the (rare) case where the lock is already set does the process
302 need to do a system call to wait for the lock to clear.
303 When a process is finished with a lock, 
304 it can clear the lock itself.
305 Only if the ``WANT'' flag for the lock has been set is
306 it necessary for the process to do a system call to cause the other
307 process(es) to be awakened.
308 .PP
309 Another issue that must be considered is portability.
310 Some computers require access to special hardware to implement
311 atomic interprocessor test-and-set. 
312 For such machines the setting and clearing of locks would
313 all have to be done with system calls;
314 applications could still use the same interface without change,
315 though they would tend to run slowly.
316 .PP
317 The other issue of compatibility is with System V's semaphore
318 implementation.
319 Since the System V interface has been in existence for several years,
320 and applications have been built that depend on this interface,
321 it is important that this interface also be available.
322 Although the interface is based on system calls for both setting and
323 clearing locks,
324 the same interface can be obtained using our interface without
325 system calls in most cases.
326 .PP
327 This implementation can be achieved as follows.
328 System V allows entire sets of semaphores to be set concurrently.
329 If any of the locks are unavailable, the process is put to sleep
330 until they all become available.
331 Under our paradigm, a single additional semaphore is defined
332 that serializes access to the set of semaphores being simulated.
333 Once obtained in the usual way, the set of semaphores can be
334 inspected to see if the desired ones are available.
335 If they are available, they are set, the guardian semaphore
336 is released and the process proceeds.
337 If one or more of the requested set is not available,
338 the guardian semaphore is released and the process selects an
339 unavailable semaphores for which to wait.
340 On being reawakened, the whole selection process must be repeated.
341 .PP
342 In all the above examples, there appears to be a race condition.
343 Between the time that the process finds that a semaphore is locked,
344 and the time that it manages to call the system to sleep on the
345 semaphore another process may unlock the semaphore and issue a wakeup call.
346 Luckily the race can be avoided.
347 The insight that is critical is that the process and the kernel agree
348 on the physical byte of memory that is being used for the semaphore.
349 The system call to put a process to sleep takes a pointer
350 to the desired semaphore as its argument so that once inside
351 the kernel, the kernel can repeat the test-and-set.
352 If the lock has cleared
353 (and possibly the wakeup issued) between the time that the process
354 did the test-and-set and eventually got into the sleep request system call,
355 then the kernel immediately resumes the process rather than putting
356 it to sleep.
357 Thus the only problem to solve is how the kernel interlocks between testing
358 a semaphore and going to sleep;
359 this problem has already been solved on existing systems.
360 .NH
361 References
362 .sp
363 .IP [Babaoglu79] 20
364 Babaoglu, O., and Joy, W.,
365 ``Data Structures Added in the Berkeley Virtual Memory Extensions
366 to the UNIX Operating System''
367 Computer Systems Research Group, Dept of EECS, University of California,
368 Berkeley, CA 94720, USA, November 1979.
369 .IP [Someren84] 20
370 Someren, J. van,
371 ``Paging in Berkeley UNIX'',
372 Laboratorium voor schakeltechniek en techneik v.d. 
373 informatieverwerkende machines,
374 Codenummer 051560-44(1984)01, February 1984.