This is a free preview of a paid episode. To hear more, visit blog.codingconfessions.com
In the last video, we discussed demand paging. Now, let’s move towards an even more interesting topic: copy-on-write (CoW). Just like demand paging, CoW is the kernel’s internal mechanism with implications for the performance of user-space systems. It enables multiple processes to share data in RAM between them in read-only mode. The interesting bit is that the processes themselves are not aware of this sharing, as far as they are concerned they are executing as if they are the only ones working with that data. Of course, this works as long as the processes are reading the data. When one of them needs to do a write to this shared data, the kernel needs to make a copy before the write can happen, hence the name “copy-on-write”!
This is a very wide and deep topic, so I’m going to split into multiple videos. This first video goes deep inside the kernel to explain what CoW is, how the kernel implements it, and for that we will take the example of mmap to read and write files.
Following are some of the major sections in the video with the timestamps to help you navigate. Also, I recommend watching the video at higher speed to get a better experience.
* (00:00) Why CoW matters: memory use, page faults, and unpredictable latency in data-intensive applications.
* (06:34) The page-table picture: how different processes can map the same physical frame.
* (11:44) CoW in one diagram: share a page while reading; make a private copy when writing.
* (15:06) Mapping a file with mmap: the call’s arguments, including MAP_PRIVATE.
* (21:31) What mmap creates: a virtual address range and VMA, before the file page is maps into the process.
* (25:03) The first read: address translation, a page fault, and how the kernel resolves it.
* (32:25) The page cache: where file data is held in RAM and why another process can reuse it.
* (38:43) A second process maps the file: its own page fault leads to the same cached physical page.
* (43:11) A private write: why writing to that shared file page would violate MAP_PRIVATE.
* (45:51) Write protection and CoW: how a read-only PTE causes a write fault and the kernel gives the writer a private copy.
* (49:07) What happens next: the second process can also get a copy; later writes to an already private page proceed without another CoW fault.
A minor correction note: At about 46 minutes, when I say mappings of page-cache pages are read-only, I mean the MAP_PRIVATE mappings in this example. A writable MAP_SHARED mapping can modify a cached file page, which is later written back to the file.
If you are new to this series, it is based on my ebook called “Virtual Memory from First Principles”. It is available to read for free online and also available to purchase from Gumroad (PDF/Epub) and Amazon (Kindle edition).
And, if you want to watch the previous videos in this series, the following is what has been published so far:
* What is virtual memory and why do we need it
* Size of the virtual address space of a process
* Address space of a process
* Structure of page tables and how page table walks work
* What is inside a page table entry
* Size of a physical address
* Demand paging
* Demand Paging in Action: mmap, Page Faults, and RSS
Audio and video play straight from the show's own hosting. PodSubs stores and serves no media.
Transcript
From the show. Tap a time to play from there.
SPEAKER_00Hey everyone, this is Abhinav. So for the last few weeks, we have been doing this series of videos on virtual memory and we have reached an interesting part where we have started to discuss topics that are very relevant to engineers who are responsible for building, designing and maintaining large scale data intensive applications. The kind of applications, you know, which have to deal with a ton of memory.
SPEAKER_00And these topics are important to understand because you tend to see many issues in production systems where you observe spurious latency spikes, spurious drops in throughput, spikes in memory usage, and If you are the one who has to analyze and debug these issues, it can be really mysterious if you don't understand how memory really works under
SPEAKER_00the hood, you know, under the hood of your system. So, at the virtual memory level, at the hardware level, and so on. So, understanding those issues, it is really important to be able to debug this kind of problem. So, for example, in the last video, we talked about demand paging. Which is very important to understand if you are someone who has to optimize memory usage, improve performance and so on. Similarly, in this video, I want to talk about copy and write,
SPEAKER_00which is another internal mechanism of the kernel that enables multiple processes to be able to share data between them. And which is very interesting because when we think about So one process executes on the system as if it is the only single entity running on that system. But in reality, there are multiple processes running in parallel. But the mental model is that one process is memory is its own private memory. So no one is actually reading or writing that same memory as this process.
SPEAKER_00But copy and write actually makes this possible where even though these processes are not aware, but they... Or they could be in reality sharing some data between them. And this really happens very commonly. It's not some obscure detail. And no... And this is a very, and it has both sides, you know, positives and negatives. So the positives are that copy and write actually improves the memory utilization because if multiple processes are working with the same data, it helps.
SPEAKER_00It cuts down the, you know, memory pressure or it actually improves the memory utilization because rather than each process having its own private copy of that data, they are working with a single copy. But it has downsides as well. And those downsides are majorly related to performance. So one thing is that this sharing of data works as long as these processes are simply reading the data. Whenever one process wants to do a write,
SPEAKER_00the kernel has to make a copy, which is why it's called copy and write. And that copy makes the... You know, system unpredictable. So whenever, when a copy will be made, you can't really tell. It all depends on a variety of things. You know, many things are happening behind the scenes. So it depends. Sometimes a copy will be made. Sometimes a copy will not be needed to be made. So sometimes whenever you do a write, it just goes through like as if very fast. But sometimes it takes a while because it causes a page fault.
SPEAKER_00The kernel has to make a copy of the data and then the write has to be made. So it can be slow. So this is why you see all these spikes in latency or all those things. So understanding, you know, so it's both... or it could be a bad thing depending on which side you are on. So, now, this is a very broad and deep topic to discuss. So, it has implications, you know, from the user space and from the kernel side as well because from
SPEAKER_00the user space side, we have to understand what are the ways, what are the things that we do in the user space that could cause copy and write, you know, trigger copy and write, And then on the kernel side, we have to understand how, you know, how the kernel actually implements copy and write and what are the various things that happen, how it is actually done so that we can connect the dots. So like what action in the user space could be causing a copy and write and how could we avoid that?
SPEAKER_00So understanding both is important. So as a result, this is, you know, This is going to take a bit of time. So I'm going to split this video into two parts. This particular part is going to be focused on just explaining copy and write. And we will cover the MMAP system call. So MMAP is one way... Or one pathway where copy and write is actually triggered or copy and write happens behind the scenes. So we'll discuss that in this video.
SPEAKER_00And then another pathway is through the fork system call, which is used for creating a new process. So we'll talk about that in the next video. And I will include a bonus detail most probably in the next video where we talk about fork. That is how Instagram... Takes advantage of copy and write to improve the memory usage of their systems. So I'll talk about that as part of the next video as well. And then maybe we'll do a hands-on demo as well.
SPEAKER_00So we'll write a small program and we'll understand what are the various debugging tools that a Linux kernel gives us to be able to analyze issues related to copy and write. So we'll do that.
SPEAKER_01So let's start copy and write.
SPEAKER_00So I'll start from the process side. So I'll start on the user space side. So, you know, what does it really mean? But, you know, what do I really mean by copy and write? And then once we have the user side picture clear, then we'll go inside the panel through the mMap system call and see, you know, how all things become possible. So we were talking about virtual memory for many weeks. At this point of time,
SPEAKER_00I assume that all of us understand what virtual pages are or how a process's virtual address space looks like. How does it look like? And what a page table is. So we know that processes... Address space is essentially a continuous sequence of addresses.
SPEAKER_01So I drew the address space as a straight line to represent that. And basically this is...
SPEAKER_00So this address space goes from 0 to 2 to the power 48 minus 1. So 2 to the power 48 minus 1 because... On Linux x86 platform, the virtual address size by default is 48 bits. So that is the usual address space. Now this address space is actually managed by the kernel at a higher granularity, which is at the level of virtual page. So one virtual page is 4KB. So you can think of this address space being split into 4KB chunks.
SPEAKER_00One 4KB chunk is one page. Now, this is all virtual. So this is just an abstraction inside a kernel or the process's memory. But this is not real memory. So each virtual page actually is mapped to a physical frame. So physical frame is essentially a very similar thing. But it is real. It is physical.
SPEAKER_01So if you think... Sorry. So if we...
SPEAKER_00Draw the physical memory of our system, so if I draw another line to represent that, so this will also, this is my RAM, this will, this consists of real physical addresses, those addresses also go from 0 to some power of 2, depending on how much memory I have installed on the system. But this physical memory is also managed by the kernel at the level So this one frame is the same size as a virtual page in this case.
SPEAKER_00I mean, mostly because the kernel has to map one virtual page to a physical frame. So both have to have the same size.
SPEAKER_01I'm not sure why this line keeps happening.
SPEAKER_00So, 4KB and this is a frame or called a physical frame as well. So, and this mapping is maintained, mapping of a virtual page
SPEAKER_01to a physical frame is done through a page table. So, the kernel maintains a page table. So I'll draw this page table as a flat table here, just to conceptualize. And I'll also draw our RAM.
SPEAKER_00I'll abbreviate page table as PT throughout this video. So this is our page table. We can think of this consisting of
SPEAKER_01a bunch of virtual pages. And RAM is also organized as a bunch of physical frames. So now, this table basically maps a virtual page to a physical frame. Like this. So this is how, you know, your virtual pages are mapped to real physical frames.
SPEAKER_01So that is how a processor's
SPEAKER_00address space looks
SPEAKER_01like.
SPEAKER_00Now, here the assumption is that, you know, one process works in isolation. So it is not aware of anything else running on the system. So it runs as if it is the only thing running on the system. And one of the... So one assumption that we... Property of this, you know, process model is that the process executes as if it owns the entire memory. But in reality, it is only, you know, working with virtual memory.
SPEAKER_00And then that virtual addresses space is the processes in private virtual addresses space. And... And we also believe that, you know, whatever data one process is reading or writing in memory is not visible to any other entity running on the system. So whether, you know, any other process running on the system. So that's the assumption that we make. But in reality, multiple processes could be sharing these frames amongst them without knowing.
SPEAKER_00So this is done by the kernel itself. We'll talk about that, how that happens, but yes. So this is what I mean. So let's say I have another process running.
SPEAKER_01If I draw its page table.
SPEAKER_00So let's say one of its virtual pages is mapped to this frame, this dash frame. So I can see that both two processes are actually mapping to the same frame. Now, these two processes, even though when they are running, they are not aware of this mapping, like they are not aware that they are actually reading the same data that the other process is reading, but that is what is happening. So, there is not a violation, but this all works because both these processes are actually just reading this data.
SPEAKER_00So, there is no problem with that. Problem comes when one of these processes actually wants to write to this data. Then this becomes problematic because the isolation property is that one process's memory is its own private memory. So if it is modifying something, some data in memory, that modification should not be visible to anyone else. But here, the second process could be seeing that because it is also mapping to the same frame.
SPEAKER_00And which is why the kernel has to implement copy-and-write. So it is not possible to do this sharing without having copy-and-write mechanism in place, which is why copy-and-write is needed. And copy-and-write is essentially whenever one of these processes wants to write to a shared frame. Before that write happens, the kernel has to make a copy
SPEAKER_01of that frame. Again, sorry about this. This line keeps coming. So if the kernel has to make a copy of this frame, then it has to update the page table of this process.
SPEAKER_00So this page table entry is now mapping to this copy. Now the write will happen in this copy rather than the shared frame. That is what copy and write is. So, the kernel can do this sharing as long as it is able to keep a tab when a write is happening and before the write happens, it is able to make a copy and then the write happens on the copy. So, that's copy and write in a nutshell from a very high level view. Now, it may not be very clear when the kernel decides that it is safe to share frames
SPEAKER_00between multiple processes. How does it figure out How does it detect a write being done and then making a copy at that point of time? How all of that happens? For that, we have to go deeper inside a kernel,
SPEAKER_01understand those mechanisms. So, let's do that. So, I'll take the example of the mmap system
SPEAKER_00call to explain the kernels internals here. So mmap is a system call we talked about in the last video as well. So mmap is a system call that allows user space to allocate more memory. It's a memory allocation system call. So we specify how much memory we want to allocate, how many bytes we want.
This is a free preview of a paid episode. To hear more, visit blog.codingconfessions.com In the last video, we learned about copy-on-write (CoW) and understood its mechanics by digging inside the mmap pathway in the...
This is a free preview of a paid episode. To hear more, visit blog.codingconfessions.com In the previous video in our virtual memory series, we discussed demand paging. But that was mostly theoretical, explaining how...
This is a free preview of a paid episode. To hear more, visit blog.codingconfessions.com Welcome back to the video series on virtual memory based on my article on the same topic. It is also available in ebook format on...
This is a free preview of a paid episode. To hear more, visit blog.codingconfessions.com This is a short aside in our virtual memory series. We have talked extensively about the size of a virtual address and the virtual...
This is a free preview of a paid episode. To hear more, visit blog.codingconfessions.com This is the 5th video in our virtual memory series. In the previous video, we learned about the page table and how the hardware...