From: Andrew Morton <akpm@linux-foundation.org>
To: HATAYAMA Daisuke <d.hatayama@jp.fujitsu.com>
Cc: vgoyal@redhat.com, ebiederm@xmission.com, cpw@sgi.com,
kumagai-atsushi@mxc.nes.nec.co.jp, lisa.mitchell@hp.com,
kexec@lists.infradead.org, linux-kernel@vger.kernel.org,
zhangyanfei@cn.fujitsu.com, jingbai.ma@hp.com,
linux-mm@kvack.org, riel@redhat.com, walken@google.com,
hughd@google.com, kosaki.motohiro@jp.fujitsu.com
Subject: Re: [PATCH v8 6/9] vmcore: allocate ELF note segment in the 2nd kernel vmalloc memory
Date: Thu, 23 May 2013 15:17:14 -0700 [thread overview]
Message-ID: <20130523151714.a4096553280794de47d0fdb6@linux-foundation.org> (raw)
In-Reply-To: <20130523052530.13864.7616.stgit@localhost6.localdomain6>
On Thu, 23 May 2013 14:25:30 +0900 HATAYAMA Daisuke <d.hatayama@jp.fujitsu.com> wrote:
> The reasons why we don't allocate ELF note segment in the 1st kernel
> (old memory) on page boundary is to keep backward compatibility for
> old kernels, and that if doing so, we waste not a little memory due to
> round-up operation to fit the memory to page boundary since most of
> the buffers are in per-cpu area.
>
> ELF notes are per-cpu, so total size of ELF note segments depends on
> number of CPUs. The current maximum number of CPUs on x86_64 is 5192,
> and there's already system with 4192 CPUs in SGI, where total size
> amounts to 1MB. This can be larger in the near future or possibly even
> now on another architecture that has larger size of note per a single
> cpu. Thus, to avoid the case where memory allocation for large block
> fails, we allocate vmcore objects on vmalloc memory.
>
> This patch adds elfnotes_buf and elfnotes_sz variables to keep pointer
> to the ELF note segment buffer and its size. There's no longer the
> vmcore object that corresponds to the ELF note segment in
> vmcore_list. Accordingly, read_vmcore() has new case for ELF note
> segment and set_vmcore_list_offsets_elf{64,32}() and other helper
> functions starts calculating offset from sum of size of ELF headers
> and size of ELF note segment.
>
> ...
>
> @@ -154,6 +157,26 @@ static ssize_t read_vmcore(struct file *file, char __user *buffer,
> return acc;
> }
>
> + /* Read Elf note segment */
> + if (*fpos < elfcorebuf_sz + elfnotes_sz) {
> + void *kaddr;
> +
> + tsz = elfcorebuf_sz + elfnotes_sz - *fpos;
> + if (buflen < tsz)
> + tsz = buflen;
We have min().
>
> ...
>
> +/* Merges all the PT_NOTE headers into one. */
> +static int __init merge_note_headers_elf64(char *elfptr, size_t *elfsz,
> + char **notes_buf, size_t *notes_sz)
> +{
> + int i, nr_ptnote=0, rc=0;
> + char *tmp;
> + Elf64_Ehdr *ehdr_ptr;
> + Elf64_Phdr phdr;
> + u64 phdr_sz = 0, note_off;
> +
> + ehdr_ptr = (Elf64_Ehdr *)elfptr;
> +
> + rc = update_note_header_size_elf64(ehdr_ptr);
> + if (rc < 0)
> + return rc;
> +
> + rc = get_note_number_and_size_elf64(ehdr_ptr, &nr_ptnote, &phdr_sz);
> + if (rc < 0)
> + return rc;
> +
> + *notes_sz = roundup(phdr_sz, PAGE_SIZE);
> + *notes_buf = vzalloc(*notes_sz);
I think this gets leaked in a number of places.
> + if (!*notes_buf)
> + return -ENOMEM;
> +
> + rc = copy_notes_elf64(ehdr_ptr, *notes_buf);
> + if (rc < 0)
> + return rc;
> +
> /* Prepare merged PT_NOTE program header. */
> phdr.p_type = PT_NOTE;
> phdr.p_flags = 0;
> note_off = sizeof(Elf64_Ehdr) +
> (ehdr_ptr->e_phnum - nr_ptnote +1) * sizeof(Elf64_Phdr);
> - phdr.p_offset = note_off;
> + phdr.p_offset = roundup(note_off, PAGE_SIZE);
> phdr.p_vaddr = phdr.p_paddr = 0;
> phdr.p_filesz = phdr.p_memsz = phdr_sz;
> phdr.p_align = 0;
Please review and test:
From: Andrew Morton <akpm@linux-foundation.org>
Subject: vmcore-allocate-elf-note-segment-in-the-2nd-kernel-vmalloc-memory-fix
use min(), fix error-path vzalloc() leaks
Cc: Atsushi Kumagai <kumagai-atsushi@mxc.nes.nec.co.jp>
Cc: HATAYAMA Daisuke <d.hatayama@jp.fujitsu.com>
Cc: KOSAKI Motohiro <kosaki.motohiro@jp.fujitsu.com>
Cc: Lisa Mitchell <lisa.mitchell@hp.com>
Cc: Vivek Goyal <vgoyal@redhat.com>
Cc: Zhang Yanfei <zhangyanfei@cn.fujitsu.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
---
fs/proc/vmcore.c | 16 +++++-----------
1 file changed, 5 insertions(+), 11 deletions(-)
diff -puN fs/proc/vmcore.c~vmcore-allocate-elf-note-segment-in-the-2nd-kernel-vmalloc-memory-fix fs/proc/vmcore.c
--- a/fs/proc/vmcore.c~vmcore-allocate-elf-note-segment-in-the-2nd-kernel-vmalloc-memory-fix
+++ a/fs/proc/vmcore.c
@@ -142,9 +142,7 @@ static ssize_t read_vmcore(struct file *
/* Read ELF core header */
if (*fpos < elfcorebuf_sz) {
- tsz = elfcorebuf_sz - *fpos;
- if (buflen < tsz)
- tsz = buflen;
+ tsz = min(elfcorebuf_sz - (size_t)*fpos, buflen);
if (copy_to_user(buffer, elfcorebuf + *fpos, tsz))
return -EFAULT;
buflen -= tsz;
@@ -161,9 +159,7 @@ static ssize_t read_vmcore(struct file *
if (*fpos < elfcorebuf_sz + elfnotes_sz) {
void *kaddr;
- tsz = elfcorebuf_sz + elfnotes_sz - *fpos;
- if (buflen < tsz)
- tsz = buflen;
+ tsz = min(elfcorebuf_sz + elfnotes_sz - (size_t)*fpos, buflen);
kaddr = elfnotes_buf + *fpos - elfcorebuf_sz;
if (copy_to_user(buffer, kaddr, tsz))
return -EFAULT;
@@ -179,9 +175,7 @@ static ssize_t read_vmcore(struct file *
list_for_each_entry(m, &vmcore_list, list) {
if (*fpos < m->offset + m->size) {
- tsz = m->offset + m->size - *fpos;
- if (buflen < tsz)
- tsz = buflen;
+ tsz = min_t(size_t, m->offset + m->size - *fpos, buflen);
start = m->paddr + *fpos - m->offset;
tmp = read_from_oldmem(buffer, tsz, &start, 1);
if (tmp < 0)
@@ -710,6 +704,8 @@ static void free_elfcorebuf(void)
{
free_pages((unsigned long)elfcorebuf, get_order(elfcorebuf_sz_orig));
elfcorebuf = NULL;
+ vfree(elfnotes_buf);
+ elfnotes_buf = NULL;
}
static int __init parse_crash_elf64_headers(void)
@@ -898,8 +894,6 @@ void vmcore_cleanup(void)
list_del(&m->list);
kfree(m);
}
- vfree(elfnotes_buf);
- elfnotes_buf = NULL;
free_elfcorebuf();
}
EXPORT_SYMBOL_GPL(vmcore_cleanup);
_
next prev parent reply other threads:[~2013-05-23 22:17 UTC|newest]
Thread overview: 32+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-05-23 5:24 [PATCH v8 0/9] kdump, vmcore: support mmap() on /proc/vmcore HATAYAMA Daisuke
2013-05-23 5:25 ` [PATCH v8 1/9] vmcore: clean up read_vmcore() HATAYAMA Daisuke
2013-05-23 5:25 ` [PATCH v8 2/9] vmcore: allocate buffer for ELF headers on page-size alignment HATAYAMA Daisuke
2013-05-23 14:22 ` Vivek Goyal
2013-05-23 21:46 ` Andrew Morton
2013-05-23 5:25 ` [PATCH v8 3/9] vmcore: treat memory chunks referenced by PT_LOAD program header entries in page-size boundary in vmcore_list HATAYAMA Daisuke
2013-05-23 21:49 ` Andrew Morton
2013-05-24 13:12 ` Vivek Goyal
2013-05-27 0:13 ` HATAYAMA Daisuke
2013-05-23 5:25 ` [PATCH v8 4/9] vmalloc: make find_vm_area check in range HATAYAMA Daisuke
2013-05-23 5:25 ` [PATCH v8 5/9] vmalloc: introduce remap_vmalloc_range_partial HATAYAMA Daisuke
2013-05-23 22:00 ` Andrew Morton
2013-05-23 5:25 ` [PATCH v8 6/9] vmcore: allocate ELF note segment in the 2nd kernel vmalloc memory HATAYAMA Daisuke
2013-05-23 14:28 ` Vivek Goyal
2013-05-23 22:17 ` Andrew Morton [this message]
2013-05-23 5:25 ` [PATCH v8 7/9] vmcore: Allow user process to remap ELF note segment buffer HATAYAMA Daisuke
2013-05-23 14:32 ` Vivek Goyal
2013-05-23 5:25 ` [PATCH v8 8/9] vmcore: calculate vmcore file size from buffer size and total size of vmcore objects HATAYAMA Daisuke
2013-05-23 14:34 ` Vivek Goyal
2013-05-23 5:25 ` [PATCH v8 9/9] vmcore: support mmap() on /proc/vmcore HATAYAMA Daisuke
2013-05-23 22:24 ` Andrew Morton
[not found] ` <CAJGZr0LwivLTH+E7WAR1B9_6B4e=jv04KgCUL_PdVpi9JjDpBw@mail.gmail.com>
2013-05-27 1:49 ` HATAYAMA Daisuke
[not found] ` <CAJGZr0LmsFXEgb3UXVb+rqo1aq5KJyNxyNAD+DG+3KnJm_ZncQ@mail.gmail.com>
2013-05-30 9:26 ` Zhang Yanfei
[not found] ` <CAJGZr0Ld6Q4a4f-VObAbvqCp=+fTFNEc6M-Fdnhh28GTcSm1=w@mail.gmail.com>
2013-06-03 8:43 ` Atsushi Kumagai
[not found] ` <CAJGZr0KV9hmdFWQE5Z9kOieHSPhGKLAhsw1Me2RE2ADsbU=b7w@mail.gmail.com>
2013-06-07 1:11 ` Zhang Yanfei
[not found] ` <CAJGZr0+9VUweN1Ssdq6P9Lug1GnTB3+RPv77JLRmnw=rpd9+Dw@mail.gmail.com>
2013-06-30 23:53 ` HATAYAMA Daisuke
[not found] ` <CAJGZr0Jwy6OLADBO9GExWVbwG_LMk41ZsSMZKvWmwcA9StVZQA@mail.gmail.com>
2013-07-01 19:53 ` Andrew Morton
2013-06-06 21:31 ` Arnd Bergmann
2013-06-07 1:01 ` HATAYAMA Daisuke
2013-06-07 18:34 ` Arnd Bergmann
2013-06-08 10:42 ` HATAYAMA Daisuke
2013-05-23 14:35 ` [PATCH v8 0/9] kdump, " Vivek Goyal
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=20130523151714.a4096553280794de47d0fdb6@linux-foundation.org \
--to=akpm@linux-foundation.org \
--cc=cpw@sgi.com \
--cc=d.hatayama@jp.fujitsu.com \
--cc=ebiederm@xmission.com \
--cc=hughd@google.com \
--cc=jingbai.ma@hp.com \
--cc=kexec@lists.infradead.org \
--cc=kosaki.motohiro@jp.fujitsu.com \
--cc=kumagai-atsushi@mxc.nes.nec.co.jp \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=lisa.mitchell@hp.com \
--cc=riel@redhat.com \
--cc=vgoyal@redhat.com \
--cc=walken@google.com \
--cc=zhangyanfei@cn.fujitsu.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
all inboxes | Powered by JetHome®