linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Matvejchikov Ilya <matvejchikov@gmail.com>
To: linux-mm@kvack.org
Cc: Ilya Matveychikov <matvejchikov@gmail.com>
Subject: A question aboout virtual mapping of kernel and module pages
Date: Fri, 20 Dec 2013 00:25:13 +0400	[thread overview]
Message-ID: <CAKh5naYHUUUPnSv4skmX=+88AB-L=M4ruQti5cX=1BRxZY2JRg@mail.gmail.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 2278 bytes --]

I'm using VMAP function to create memory writable mapping as it suggested
in ksplice project. Here is the implementation of map_writable function:

/*
 * map_writable creates a shadow page mapping of the range
 * [addr, addr + len) so that we can write to code mapped read-only.
 *
 * It is similar to a generalized version of x86's text_poke.  But
 * because one cannot use vmalloc/vfree() inside stop_machine, we use
 * map_writable to map the pages before stop_machine, then use the
 * mapping inside stop_machine, and unmap the pages afterwards.
 */
static void *map_writable(void *addr, size_t len)
{
        void *vaddr;
        int nr_pages = DIV_ROUND_UP(offset_in_page(addr) + len, PAGE_SIZE);
        struct page **pages = kmalloc(nr_pages * sizeof(*pages),
GFP_KERNEL);
        void *page_addr = (void *)((unsigned long)addr & PAGE_MASK);
        int i;

        if (pages == NULL)
                return NULL;

        for (i = 0; i < nr_pages; i++) {
                if (__module_address((unsigned long)page_addr) == NULL) {
                        pages[i] = virt_to_page(page_addr);
                        WARN_ON(!PageReserved(pages[i]));
                } else {
                        pages[i] = vmalloc_to_page(page_addr);
                }
                if (pages[i] == NULL) {
                        kfree(pages);
                        return NULL;
                }
                page_addr += PAGE_SIZE;
        }
        vaddr = vmap(pages, nr_pages, VM_MAP, PAGE_KERNEL);
        kfree(pages);
        if (vaddr == NULL)
                return NULL;
        return vaddr + offset_in_page(addr);
}

This function works well when I used it to map kernel's text addresses. All
fine and I can rewrite read-only data well via the mapping.

Now, I need to modify kernel module's text. Given the symbol address inside
the module, I use the same method. The mapping I've got seems to be valid.
But all my changes visible only in that mapping and not in the module!

I suppose that in case of module mapping I get something like copy-on-write
but I can't prove it.

Can anyone explain me what's happend and why I can use it for mapping
kernel and can't for modules?

http://stackoverflow.com/questions/20658357/virtual-mapping-of-kernel-and-module-pages

[-- Attachment #2: Type: text/html, Size: 2630 bytes --]

             reply	other threads:[~2013-12-19 20:25 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-12-19 20:25 Matvejchikov Ilya [this message]
2013-12-20  2:41 ` Vladimir Murzin
2013-12-20  8:25   ` Matvejchikov Ilya

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='CAKh5naYHUUUPnSv4skmX=+88AB-L=M4ruQti5cX=1BRxZY2JRg@mail.gmail.com' \
    --to=matvejchikov@gmail.com \
    --cc=linux-mm@kvack.org \
    /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