All of lore.kernel.org
 help / color / mirror / Atom feed
From: Mike Rapoport <rppt@kernel.org>
To: Steven Rostedt <rostedt@goodmis.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>,
	linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org,
	Masami Hiramatsu <mhiramat@kernel.org>,
	Mark Rutland <mark.rutland@arm.com>,
	Mathieu Desnoyers <mathieu.desnoyers@efficios.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	Vincent Donnefort <vdonnefort@google.com>,
	Vlastimil Babka <vbabka@suse.cz>, Kees Cook <kees@kernel.org>,
	Tony Luck <tony.luck@intel.com>,
	"Guilherme G. Piccoli" <gpiccoli@igalia.com>,
	linux-hardening@vger.kernel.org
Subject: Re: [PATCH v2 1/2] tracing: ring-buffer: Have the ring buffer code do the vmap of physical memory
Date: Tue, 1 Apr 2025 18:26:43 +0300	[thread overview]
Message-ID: <Z-wFszhJ_9o4dc8O@kernel.org> (raw)
In-Reply-To: <20250401111159.7632a0fa@gandalf.local.home>

On Tue, Apr 01, 2025 at 11:11:59AM -0400, Steven Rostedt wrote:
> On Tue, 1 Apr 2025 12:56:31 +0300
> Mike Rapoport <rppt@kernel.org> wrote:
> 
> > > For example, using "mem=" on the kernel command line will literally
> > > limit the amount of RAM the kernel will use, and in doing so will
> > > limit the page allocations too.  
> > 
> > And using memmap=m$n on x86 creates a hole in System RAM that does not have
> > neither struct page nor kernel mappings and it is never considered RAM
> > anywhere in mm.
> 
> Hmm, when that is used, then we had better not "free" the buffer.
> 
> >  
> > > IOW, all of these kernel command line things are *subtle*.
> > > 
> > > Don't mis-use them by then making assumptions about how they work
> > > today (or how they will work tomorrow).  
> > 
> > I'd say it's better not to use them at all. They cause weirdness in memory
> > layout and also they are inconsistent in how architectures implement them.
> >  
> > > > Mike can correct me if I'm wrong, but the memory that was stolen was actual
> > > > memory returned by the system (E820 in x86). It reserves the memory before
> > > > the memory allocation reserves this memory. So what reserve_mem returns is
> > > > valid memory that can be used by memory allocator, but is currently just
> > > > "reserved" which means it wants to prevent the allocator from using it.  
> > > 
> > > That may indeed be true of reserve_mem.  
> > 
> > The reserve_mem behaves like any other early allocation, it has proper
> > struct pages (PG_Reserved) and it is mapped in the direct map so
> > phys_to_virt() will work on it.
> > 
> > As for mapping it to userspace, vm_iomap_memory() seems the best API to
> > use. It has all the alignment checks and will refuse to map ranges that are
> > not properly aligned and it will use vma information to create the right
> > mappings.
> >  
> 
> When using vmap() to get the virtual addresses (via the kmalloc_array() of
> struct pages), the vunmap() gives the memory back to the memory allocator:
> 
> ~# free
>                total        used        free      shared  buff/cache   available
> Mem:         8185928      296676     7840576         920      148280     7889252
> Swap:        7812092           0     7812092
> ~# rmdir /sys/kernel/tracing/instances/boot_mapped/
> ~# free
>                total        used        free      shared  buff/cache   available
> Mem:         8206404      290868     7866772         920      148384     7915536
> Swap:        7812092           0     7812092
> 
> With no issues.
> 
> But if I use vmap_page_range(), how do I give that back to the memory allocator?

But you don't need neither vmap() nor vmap_page_range() to have kernel page
tables for memory that you get from reserve_mem. It's already mapped and
plain phys_to_virt() gives you the virtual address you can use.
 
> Calling vunmap() on that memory gives me:
> 
>  1779.832484] ------------[ cut here ]------------
> [ 1779.834076] Trying to vunmap() nonexistent vm area (000000027c000000)
> [ 1779.835941] WARNING: CPU: 6 PID: 956 at mm/vmalloc.c:3413 vunmap+0x5a/0x60
> 
> What's the proper way to say: "I no longer need this physical memory I
> reserved, the kernel can now use it"?
 
free_reserved_area()

> -- Steve

-- 
Sincerely yours,
Mike.

  reply	other threads:[~2025-04-01 15:26 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-31 14:34 [PATCH v2 0/2] ring-buffer: Allow persistent memory to be user space mmapped Steven Rostedt
2025-03-31 14:34 ` [PATCH v2 1/2] tracing: ring-buffer: Have the ring buffer code do the vmap of physical memory Steven Rostedt
2025-03-31 16:55   ` Linus Torvalds
2025-03-31 17:39     ` Steven Rostedt
2025-03-31 19:12       ` Linus Torvalds
2025-03-31 20:58         ` Steven Rostedt
2025-03-31 21:42           ` Linus Torvalds
2025-03-31 23:42             ` Steven Rostedt
2025-04-01  0:09               ` Jann Horn
2025-04-01  1:02                 ` Steven Rostedt
2025-04-01  1:28                   ` Jann Horn
2025-04-01  1:50                     ` Steven Rostedt
2025-04-01  2:23                       ` Mathieu Desnoyers
2025-04-01  1:30                   ` Linus Torvalds
2025-04-01  1:41                     ` Steven Rostedt
2025-04-01  1:55                       ` Linus Torvalds
2025-04-01  9:53                         ` Ingo Molnar
2025-04-01  0:11               ` Linus Torvalds
2025-04-01  0:27                 ` Linus Torvalds
2025-04-01  0:30                 ` Steven Rostedt
2025-04-01  0:38                   ` Linus Torvalds
2025-04-01  0:49                     ` Linus Torvalds
2025-04-01  1:36                       ` Steven Rostedt
2025-04-01  1:44                         ` Linus Torvalds
2025-04-03  5:59                     ` Herbert Xu
2025-04-03 16:47                       ` Linus Torvalds
2025-04-01  9:56             ` Mike Rapoport
2025-04-01 15:11               ` Steven Rostedt
2025-04-01 15:26                 ` Mike Rapoport [this message]
2025-04-01 15:54                   ` Steven Rostedt
2025-04-01 17:58                     ` Mike Rapoport
2025-04-03 16:45       ` Kees Cook
2025-04-03 16:51         ` Linus Torvalds
2025-04-03 17:15           ` Steven Rostedt
2025-03-31 14:34 ` [PATCH v2 2/2] ring-buffer: Allow persistent ring buffers to be mmapped Steven Rostedt

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=Z-wFszhJ_9o4dc8O@kernel.org \
    --to=rppt@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=gpiccoli@igalia.com \
    --cc=kees@kernel.org \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=mathieu.desnoyers@efficios.com \
    --cc=mhiramat@kernel.org \
    --cc=rostedt@goodmis.org \
    --cc=tony.luck@intel.com \
    --cc=torvalds@linux-foundation.org \
    --cc=vbabka@suse.cz \
    --cc=vdonnefort@google.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.