kernelnewbies.kernelnewbies.org archive mirror
 help / color / mirror / Atom feed
From: Okash Khawaja <okash.khawaja@gmail.com>
To: Lev Olshvang <levonshe@yandex.com>
Cc: linux-il <linux-il@cs.huji.ac.il>,
	"Valdis Klētnieks" <valdis.kletnieks@vt.edu>,
	kernelnewbies <kernelnewbies@kernelnewbies.org>
Subject: Re: What will happen if 2 processes map same physical page
Date: Thu, 21 Mar 2019 10:45:28 +0000	[thread overview]
Message-ID: <20190321104528.33471e38@narunkot> (raw)
In-Reply-To: <3935481553162177@sas2-ce04c18c415c.qloud-c.yandex.net>

On Thu, 21 Mar 2019 12:56:17 +0300
Lev Olshvang <levonshe@yandex.com> wrote:

> Hi Vaaldis,
> 
> Thanks for answer,
> I still wondering whether the kernel will allow write to a read-only
> page of shared library while it has mapped to several processes?
> Kernel knows that page's reference count >1,  will it allow
> mmap/mprotect to change page protection ? Or will it allow direct
> right by physical address? I suppose that CPU should raise page fault
> when write is made to read only page, 
> 
> What is the sequence  CPU raises page faul before write to page of
> after data is written Will  CPU wait until kernel will consider what
> to do , whether agree and change PTE  "writable " bit to 1 ? Or
> kernel may disagree and raise SEGFAULT?

Note that each process has its own PTE. So PTE in one process may say
the page is writable and PTE in another process may say it's read-only.

> 
> I checked in the handle_mm_fault()  calls for
> arch_vma_access_permitted() which just returns true on most
> architectures which is very strange and  contradicts my prediction of
> SEFFAULT. arch_vma_access_permitted() retutus true when is sees that
> access is made from foreign process?
> https://elixir.bootlin.com/linux/latest/ident/arch_vma_access_permitted
> 
> I am totally confused.
> 
> What do you think ?
> 
> Regards,
> Lev

It looks like there are two separate questions in the email.

1) Will kernel allow the same physical page to be mapped as read-only
in one process and as read-write in another process?

2) How page fault is generated?

Answer for first is yes. Same physical page can be mapped with
different permissions in two different processes. It means read-only
process will ultimately (hopefully very soon) notice changes made by
read-write process.

Answer for second question is a bit complicated. However there is a
trick to it. Once we know that, rest will become clear automaticaly.
The trick (at least for x86 systems) is that permissions are maintained
at two different levels:

- VMA level
- PTE level (or PUD level for larger page size but that is not relevant
  here)

When a page in memory is accessed, permission on corresponding VMA is
checked first. If the access is allowed by VMA then PTE permissions are
checked. Otherwise segfault is generated. If permissions at PTE level
don't match the access type then a page fault is generated. That's when
page fault hander kicks in and tries to resolve the problem by faulting
the page into RAM, copying the page in RAM (for copy-on-write) etc.

> 
> 
> 
> 20.03.2019, 20:08, "Valdis Klētnieks" <valdis.kletnieks@vt.edu>:
> > On Wed, 20 Mar 2019 16:42:39 +0300, Lev Olshvang said:  
> >>  The question is it ipossiblle in Linux/MMU/TLB that 2 processes
> >> map to the same physical address?  
> >
> > Totally possible. That's how mmap shared memory works, and why
> > shared libraries are possible.
> >  
> >>  Will CPU or TLB discover that second process tries to reach
> >> occupied physical page?  
> >
> > Well, the hardware won't discover it as a "second" process, it only
> > knows it's processing *this* memory access.
> >  
> >>  What if first process set page permission to read and second
> >> whats to write to this page ?  
> >
> > Perfectly OK - the two processes have separate page table mappings,
> > with separate permission bits. So (for example) physical page
> > 0x17F000 is mapped to virtual address 0x2034D000 with read-only
> > permission n process 1's page tables, and to virtual address
> > 0x98FF3000 with read-write permission in process 2's page tables.
> > No problem.
> >
> > (And before you ask, yes it's possible for process 2 to running on
> > one core doing a write to the page at the exact same time that
> > process 1 is doing a read on another core. Depending on the
> > hardware cache design, this may or may not get process 1 updated
> > data. This is why locking and memory barriers are important. See
> > Documentation/memory-barriers.txt for more details)
> >
> > "And then there's the Alpha" - a processor design that got much of
> > its speed by being weird about this stuff. :)
> >  
> >>  Perhaps during context switch all page access permissions of
> >> first process is flashed out from MMU ?  
> >
> > Actually, the kernel just points the MMU at a new set of page table
> > entries and lets the TLB reload as needed. In particular, on most
> > architectures, the kernel tries really hard to ensure that all
> > processes share at least part of their page table mappings so the
> > kernel is always mapped at the same place, meaning that there's a
> > better chance that on a syscall, the TLB already has hot entries
> > for large parts of the kernel so no TLB reloads are needed.  
> 
> _______________________________________________
> Kernelnewbies mailing list
> Kernelnewbies@kernelnewbies.org
> https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies


_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

  reply	other threads:[~2019-03-21 10:46 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-03-20 13:42 What will happen if 2 processes map same physical page Lev Olshvang
2019-03-20 17:07 ` Valdis Klētnieks
2019-03-21  9:56   ` Lev Olshvang
2019-03-21 10:45     ` Okash Khawaja [this message]
2019-03-22  9:15       ` Lev Olshvang
2019-03-22  9:59         ` Okash Khawaja
2019-03-22 23:19         ` Valdis Klētnieks

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190321104528.33471e38@narunkot \
    --to=okash.khawaja@gmail.com \
    --cc=kernelnewbies@kernelnewbies.org \
    --cc=levonshe@yandex.com \
    --cc=linux-il@cs.huji.ac.il \
    --cc=valdis.kletnieks@vt.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).