live-patching.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Josh Poimboeuf <jpoimboe@kernel.org>
To: Luis Chamberlain <mcgrof@kernel.org>
Cc: Petr Mladek <pmladek@suse.com>, Song Liu <song@kernel.org>,
	patches@lists.linux.dev, linux-modules@vger.kernel.org,
	live-patching@vger.kernel.org
Subject: Re: mod->klp set on copy ok ?
Date: Fri, 17 Mar 2023 09:16:39 -0700	[thread overview]
Message-ID: <20230317161639.3de7yeek6ia4y7ul@treble> (raw)
In-Reply-To: <ZBOPP4YWWhJRk2yn@bombadil.infradead.org>

On Thu, Mar 16, 2023 at 02:50:55PM -0700, Luis Chamberlain wrote:
> The comment for "Update sh_addr to point to copy in image." seems pretty
> misleading to me, what we are doing there is actually ensuring that we update
> the copy's ELF section address to point to our newly allocated memory.
> Do folks agree?
> 
> And how about the size on the memcpy()? That's a shd->sh_size. No matter
> how much I increase my struct module in include/linux/module.h I see
> thes same sh_size. Do folks see same?
> 
> nm --print-size --size-sort fs/xfs/xfs.ko | grep __this_module
> 0000000000000000 0000000000000500 D __this_module
> 
> This is what is supposed to make the final part of layout_and_allocate() work:
> 
> 	mod = (void *)info->sechdrs[info->index.mod].sh_addr;
> 
> This works off of the copy of the module. Let's recall that
> setup_load_info() sets the copy mod to:
> 
> 	info->mod = (void *)info->hdr + info->sechdrs[info->index.mod].sh_offset;
> 
> The memcpy() in move_module() is what *should* be copying over the entire
> mod stuff properly over, that includes the mod->klp for live patching
> but also any new data we muck with in-kernel as the new mod->mem stuff
> in layout_sections(). In short, anything in struct module should be
> shoved into an ELF section. But I'm not quite sure this is all right.

I dug into that code years ago, and the above sounds right.

The .ko file has a .gnu.linkonce.this_module section whose data is just
the original "struct module __this_module" which is created by the
module build (from foo.mod.c).

At the beginning of the finit_module() syscall, the .ko file's ELF
sections get copied (and optionally decompressed) into kernel memory.
Then 'mod' just points to the copied __this_module struct.

Then mod->klp (and possibly mod->taint) get set.

Then in layout_and_allocate(), that 'mod' gets memcpy'd into the second
(and final) in-kernel copy of 'struct module':

 		if (shdr->sh_type != SHT_NOBITS)
 			memcpy(dest, (void *)shdr->sh_addr, shdr->sh_size);
 		/* Update sh_addr to point to copy in image. */
 		shdr->sh_addr = (unsigned long)dest;

I suspect you don't see the size changing when you add to 'struct
module' because it's ____cacheline_aligned.

It's all rather obtuse, but working as designed as far as I can tell.

-- 
Josh

  reply	other threads:[~2023-03-17 16:16 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <CAB=NE6Vo4AXVrn1GPEoZWVF3NkXRoPwWOuUEJqJ35S9VMGTM2Q@mail.gmail.com>
     [not found] ` <ZA8NBuXbVP+PRPp0@alley>
2023-03-16 21:50   ` mod->klp set on copy ok ? Luis Chamberlain
2023-03-17 16:16     ` Josh Poimboeuf [this message]
2023-03-17 19:00       ` Luis Chamberlain
2023-03-17 23:31         ` Josh Poimboeuf

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=20230317161639.3de7yeek6ia4y7ul@treble \
    --to=jpoimboe@kernel.org \
    --cc=linux-modules@vger.kernel.org \
    --cc=live-patching@vger.kernel.org \
    --cc=mcgrof@kernel.org \
    --cc=patches@lists.linux.dev \
    --cc=pmladek@suse.com \
    --cc=song@kernel.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;
as well as URLs for NNTP newsgroup(s).