6

On a Linux machine, we can enable swap by commands like the following

sudo fallocate -l 500M /data/swapfile
sudo chmod 600 /data/swapfile
sudo mkswap /data/swapfile
sudo swapon /data/swapfile

But even when this is not enabled, the kernel still does paging when a page is not in memory.

We can verify this by running the sar -B 1 30 command on a machine without setting any swap file.

03:08:40 AM  pgpgin/s pgpgout/s   fault/s  majflt/s  pgfree/s pgscank/s pgscand/s pgsteal/s    %vmeff
03:08:41 AM      0.00      0.00      3.00      0.00     44.00      0.00      0.00      0.00      0.00
03:08:42 AM      0.00      0.00     19.00      0.00     30.00      0.00      0.00      0.00      0.00
03:08:43 AM      0.00      0.00      0.00      0.00      3.00      0.00      0.00      0.00      0.00
03:08:44 AM     24.00      0.00      2.00      1.00      7.00      0.00      0.00      0.00      0.00
03:08:45 AM    364.00     60.00     18.00      3.00      4.00      0.00      0.00      0.00      0.00
03:08:46 AM    140.00      0.00    392.00      2.00    243.00      0.00      0.00      0.00      0.00

There is still majflt which will trigger paging out data to the disk paging in data from the disk.

My Questions is:

  • Can we say there are two types of swapping on the OS?
  • How do the two mechanisms work differently?
  • If there is always a paging mechanism working, why is there still a need to enable swap manually?

I know some people said:

Swapping refers to copying the entire process address space, or at any rate, the non-shareable-text data segment, out to the swap device, or back, in one go (typically disk).

Whereas paging refers to copying in/out one or more pages of the address space. In particular, this is at a much finer grain. For example, there are ~250,000 4 KB pages in a 1 GB RAM address space.

However, in the book Understanding the Linux Virtual Memory Manager,it doesn't seem to be this way in Linux.

Strictly speaking, Linux does not swap as “swapping” refers to coping an entire process address space to disk and “paging” to copying out individual pages. Linux actually implements paging as modern hardware supports it, but traditionally has called it swapping in discussions and documentation. To be consistent with the Linux usage of the word, we too will refer to it as swapping.

Could someone shed some light on this? Thanks!

  • You seem to have done your homework, so kudos on that. Are you sure you're reading the output from sar correctly? Since programs are loaded into memory as "page faults" mapped to the executable on disk, you'll always have MAJFLTs. – hackerb9 Mar 27 '20 at 06:04
  • 1
    By the way, I've never found sar -B to be helpful. I suggest vmstat 1 -SM for a live view of your swapfile activity. Just look at the si/so (swap in / swap out) columns. – hackerb9 Mar 27 '20 at 06:18
  • Oh, and to answer your question about why the need for a swapfile if we have paging: Paging occurs when we access a memory mapped file or (as I mentioned) running an executable. A swapfile is what is needed when you run out of RAM as it provides a place to page out parts of a running process. – hackerb9 Mar 27 '20 at 06:21
  • 1
    Hi! @hackerb9 Yes, I know the si/so in vmstat shows the activity in swap file.

    So based on what you said, can I say there are 2 swapping mechanisms?

    And could you give me more detail on how the two mechanisms works? Or some good references are welcomed :)

    – John the Traveler Mar 27 '20 at 06:29
  • I'll leave it to someone more expert to give you a proper answer. It's been a long time since I knew the intricacies of virtual memory, TLBs, and MMUs. But I can tell you that I think of Linux as having both "paging" — a method of mapping memory with disk — and "swapping" — by which I mean the paging the kernel does when it has detected memory pressure and needs to move processes out of RAM. Paging happens all the time (as you saw with sar). To me, swapping only happens in extreme circumstances. – hackerb9 Mar 27 '20 at 06:52
  • @hackerb9 thanks for sharing what you know! – John the Traveler Mar 27 '20 at 07:05
  • Here's a helpful article which debunks some of the myths of swap which I admit I held: https://chrisdown.name/2018/01/02/in-defence-of-swap.html – hackerb9 Mar 27 '20 at 07:57
  • @hackerb9 This is a very useful reference! many thx! – John the Traveler Mar 27 '20 at 08:55
  • 1
    @JohntheTraveler please don't copy and paste the same question everywhere https://serverfault.com/questions/1008629/linux-the-difference-between-paging-on-major-page-fault-and-swapping-enabled – c4f4t0r Apr 02 '20 at 18:33
  • @c4f4t0r Thank you for the reminder! Will be more careful next time – John the Traveler Apr 10 '20 at 01:19

2 Answers2

5

There is still majflt which will trigger paging out data to the disk.

It’s the other way round: major fault are page faults which can only be addressed by paging in data from a disk.

Can we say there are two types of swapping on the OS?

Not quite; the distinction here is that paging out (copying the content of memory “somewhere else” so pages can be discarded) can use different backing stores. Pages in memory have different targets for paging out: mapped files are typically their own backing store, most other pages need a swap file or partition as backing store. Swapping refers to the latter. When the kernel needs to free a page of memory, if it wants to free an unmodified page from a file it can simply discard it, knowing that the page can be restored from the file whenever it’s needed. When it needs to discard a modified page from a file, if that file is mapped read/write with no sharing, the page will be written to the file before being discarded; again, the kernel then knows it can restore the page from the file. Anything else needs some other form of storage, or it can’t be discarded.

How do the two mechanisms work differently?

See above.

If there is always a paging mechanism working, why is there still a need to enable swap manually?

See above, mostly. The point of swap is to provide a backing store for pages which don’t have their own backing store already.

See also Why does Linux need swap space in a VM?

Stephen Kitt
  • 434,908
  • Hi @Stephen Could I ask one more follow-up question? Do these swapping operations backed with different stores have names for us to refer to? – John the Traveler Mar 27 '20 at 09:17
  • 1
    Not really. My answer isn’t quite accurate, I’ll rephrase it; the general term used when copying the content of memory to “somewhere else” is paging out, and swapping is used specifically when paging out to a swap file or partition. – Stephen Kitt Mar 27 '20 at 09:32
0

Your two citations have the historical/original meaning of swap. wikipedia Paging:

In the 1960s, swapping was an early virtual memory technique. An entire program would be "swapped out" (or "rolled out") from RAM to disk, and another one would be swapped in (or rolled in).

The Intel Manual is explicit:

When paging is implemented properly in the operating-system or executive, the swapping of pages between physical memory and the disk is transparent to the correct execution of a program.

(It goes on explaining it transparently works also for 16-bit virtual 8086 mode...)

AMD has also very readable parts:

Finally, system software can use the paging mechanism to map multiple, large virtual-address spaces into a much smaller amount of physical memory. Each application can use the entire 32-bit or 64-bit virtual-address space. System software actively maps the most-frequently-used virtual-memory pages into the available pool of physical-memory pages. The least-frequently-used virtual-memory pages are swapped out to the hard drive. This process is known as demand-paged virtual memory.

It does not consider a setup without swapping out to hard drive. In that case the LFUs would have to be written back to the file (if any) or dropped (if clean).


A little bit related is this new 64 instruction:

The SWAPGS instruction provides a fast method for system software to load a pointer to system data structures. SWAPGS can be used upon entering system-software routines as a result of a SYSCALL instruction or as a result of an interrupt or exception.

...

This exchange allows the system-kernel software to quickly access kernel data structures by using the GS segment-override prefix during memory references.

It always goes back to this segmentation:

Although segmentation provides a great deal of flexibility in relocating and protecting software and data, it is often more efficient to handle memory isolation and relocation with a combination of software and hardware paging support. For this reason, most modern system software bypasses the segmentation features. However, segmentation cannot be completely disabled, and an understanding of the segmentation mechanism is important to implementing long-mode system software.


So you can say there is this page-wise swapping out of RAM, and the context-switch side of swapping, reflected by the 60s implementation, but also the modern SWAPGS instruction.

  • Thanks for sharing the history. So in the modern system, are the two terms "paging" "swapping" somewhat interchangeable? – John the Traveler Mar 27 '20 at 09:13
  • 1
    SWAPGS has absolutely nothing to do with swapping as discussed here. – Stephen Kitt Mar 27 '20 at 09:29
  • @JohntheTraveler They are very related in Linux; but I wouldn't call that interchangeable -- maybe "paging in/out" comes closer, depending on the context. –  Mar 27 '20 at 09:55
  • "program swapping" (also called "roll-in/roll-out") actually, the original SO citation is very precise. Just like the CPU Manuals from Intel and AMD. –  Mar 27 '20 at 10:07
  • So in the modern system:
    • When we talk about -"swapping", it mostly means "swapping backed by a swap file for memory allocations and anonymous mmaps". Not the "program roll-in/roll-out", right?

    • And when we say "paging", it means the writing to/reading from the disk in the VM architecture.

    Does my understanding sound okay?

    – John the Traveler Mar 27 '20 at 12:09
  • @John that’s close enough. Paging is more general, but your definitions provide a good enough approximation to get started, at least when considering memory v. disk. (The more general definition is that paging is the management of the virtual address space in terms of pages, where each page can be mapped differently or in fact not at all, and have various attributes including protection levels, read/write/execute,etc. But that’s getting out of scope for this particular question.) – Stephen Kitt Mar 27 '20 at 12:54
  • @StephenKitt thanks for the summing up! It feels great to be enlightened a bit :) – John the Traveler Mar 27 '20 at 13:43