2

"Understanding the Linux Virtual Memory Manager" (2007) by Mel Gorman (here's a link to the book chapter) describes the flags of a memory region (vm_area_struct):

VM_WRITE - Pages may be written

VM_MAYWRITE - Allow the VM_WRITE flag to be set

I don't understand why Linux needs these two flags, rather than just one of them. From the description above, it sounds like VM_MAYWRITE be set while VM_WRITE is not. In what situations? And how does the Linux kernel behave differently in these situations?

For example, the COW mechanism detects COW-protected pages according to their VM_MAYWRITE flag (see the source code). Doesn't the Linux kernel set the VM_WRITE flag when the VM_MAYWRITE is set? If yes, why not having a single flag and have it set from the beginning?

Simple.guy
  • 362
  • 3
  • 15

1 Answers1

4

Memory protection levels can be changed and the kernel uses the VM_MAY% flags to determine if such changes are allowable.

From the description above, it sounds like VM_MAYWRITE be set while VM_WRITE is not. In what situations?

In the situation that memory is initially read-only and you want to make it writable.

For example, the COW mechanism detects COW-protected pages according to their VM_MAYWRITE flag (see the source code). Doesn't the Linux kernel set the VM_WRITE flag when the VM_MAYWRITE is set? If yes, why not having a single flag and have it set from the beginning?

No, it doesn't. is_cow_mapping() isn't checking that the memory is writable, it's checking that the memory can be made writable via mprotect(). If it can't, then it's clearly not a COW mapping!

HolyHoratio
  • 537
  • 2
  • 9