Re: I/O and memory managment

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Martin Maletinsky wrote:
> 
> Hi,
> 
> I more or less understand the Linux memory management system, but I have a very limited understanding of how I/O works. I am interested in understanding, how I/O is
> synchronized with the memory managment, in particular I have the following questions:
> 
> (1) When a process wants to read data from an I/O device (e.g. disk) into a buffer in it's address space, (I assume) the pages containing that buffer must be 'locked', i.e.
> prevented from being paged out, while the data transfer is going on. As far as I understood from quickly browsing through the 'Managing I/O Devices' chapter in
> 'Understanding the Linux Kernel', this is achieved by setting the PG_locked flag in the corresponding page frame descriptor. Is this correct?

Yes.

> (2) How is I/O synchronized with the copy-on-write mechanism? Imagine the following scenario:
> 
> (i) Two threads A and B share their virtual address space (i.e. the CLONE_VM flag was set when A called clone() to create B).
> 
> (ii) Thread A starts reading data from a hard disk into a buffer BUF which lies within virtual page VP_1 (which at that moment corresponds to the physical page PP_1). In
> order to service the read request, the physical page PP_1 is locked (?) and DMA is set up, to transfer data from the harddisk into the correct location within physical page
> PP_1.
> 
> (iii) Thread A sleeps until termination of it's read request.

You really mean "process" here, because threads share VM (so
no copy-on-write, etc.)

> (iv) Meanwhile thread B calls fork(), which results in write protection of the virtual page VP_1 (in order to enforce a copy on write at a subsequent write access).

If Process B tries to write VP_1, it will get a page fault (either
due to the page not yet being mapped, or due to the copy-on-write
state), find the page in the page cache, and get it its own copy
mapped into its VM. Unless we -know- a page is shareable, the writing
process always gets its own copy, and any mappings to the old
page are left unchanged.

> (v) Thread B writes to the virtual page VP_1 (outside the buffer BUF). This results in a page fault, and subsequently in a copy on write (COW) of the virtual page VP_1. As
> a consequence of the COW, the content of PP_1 is copied into a new physical page PP_2 and virtual page VP_1 is associated to PP_2 (by modifying the respective page table
> entry for thread A and B).

Nope. B gets its own copy, A uses the existing one. So A's
read() completes normally.

Cheers,

-- Joe
# "You know how many remote castles there are along the
#  gorges? You can't MOVE for remote castles!" - Lu Tze re. Uberwald
# Linux MM docs:
http://home.earthlink.net/~jknapka/linux-mm/vmoutline.html
-
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
IRC Channel:   irc.openprojects.net / #kernelnewbies
Web Page:      http://www.kernelnewbies.org/


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux