public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Johannes Weiner <hannes@cmpxchg.org>
To: sidc7 <siddhartha.chhabra@gmail.com>
Cc: linux-kernel@vger.kernel.org
Subject: Re: COW optimization on exec
Date: Sun, 15 Mar 2009 04:41:34 +0100	[thread overview]
Message-ID: <20090315034134.GA9829@cmpxchg.org> (raw)
In-Reply-To: <22519639.post@talk.nabble.com>

On Sat, Mar 14, 2009 at 07:57:54PM -0700, sidc7 wrote:
> 
> The Linux kernel uses the COW optimization for fork, so the processes share
> the same pages, till on of the processes writes to the page. I was
> wondering, if I do a fork and do an exec immediately following the fork,
> will the COW optimization still be applied as it is most likely that the new
> process is going to write to the shared pages? So doing a COW will not give
> much benefit here, if it is done at all. Can anyone clarify if COW will be
> applied in such a case, for e.g. a command shell.

COWing the pages is not much extrawork, it's handled with this code:

        /*
         * If it's a COW mapping, write protect it both
         * in the parent and the child
         */
        if (is_cow_mapping(vm_flags)) {
                ptep_set_wrprotect(src_mm, addr, src_pte);
                pte = pte_wrprotect(pte);
        }

you can find it in mm/memory.c::copy_one_pte().  The fault handler
will then take care of it (it will notice that the pte is
write-protected while the mapping itself allows writes) and then
replace the page with a copy in the faulting process.

The real overhead is copying the page tables in the first place.  But
the kernel can not know whether exec() is soon to be called, so fork()
always must provide the copy-whole-address-space semantics.

If the forking process knows in advance the child will exec
immediately, it can use vfork() which doesn't copy the address space.

	Hannes

  reply	other threads:[~2009-03-15  3:42 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-03-15  2:57 COW optimization on exec sidc7
2009-03-15  3:41 ` Johannes Weiner [this message]
2009-03-15  4:20   ` sidc7
2009-03-15  3:58 ` Nick Piggin
2009-03-15  6:20   ` sidc7

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=20090315034134.GA9829@cmpxchg.org \
    --to=hannes@cmpxchg.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=siddhartha.chhabra@gmail.com \
    /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