Skip to content

Commit 34ea8fa

Browse files
committed
tracing: Have reserve_mem use phys_to_virt() and separate from memmap buffer
The reserve_mem kernel command line option may pass back a physical address, but the memory is still part of the normal memory just like using memblock_alloc() would be. This means that the physical memory returned by the reserve_mem command line option can be converted directly to virtual memory by simply using phys_to_virt(). When freeing the buffer there's no need to call vunmap() anymore as the memory allocated by reserve_mem is freed by the call to reserve_mem_release_by_name(). Because the persistent ring buffer can also be allocated via the memmap option, which *is* different than normal memory as it cannot be added back to the buddy system, it must be treated differently. It still needs to be virtually mapped to have access to it. It also can not be freed nor can it ever be memory mapped to user space. Create a new trace_array flag called TRACE_ARRAY_FL_MEMMAP which gets set if the buffer is created by the memmap option, and this will prevent the buffer from being memory mapped by user space. Also increment the ref count for memmap'ed buffers so that they can never be freed. Link: https://lore.kernel.org/all/[email protected]/ Cc: Linus Torvalds <[email protected]> Cc: Masami Hiramatsu <[email protected]> Cc: Mark Rutland <[email protected]> Cc: Mathieu Desnoyers <[email protected]> Cc: Andrew Morton <[email protected]> Cc: Vincent Donnefort <[email protected]> Cc: Vlastimil Babka <[email protected]> Cc: Jann Horn <[email protected]> Link: https://lore.kernel.org/[email protected] Suggested-by: Mike Rapoport <[email protected]> Signed-off-by: Steven Rostedt (Google) <[email protected]>
1 parent c44a14f commit 34ea8fa

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

kernel/trace/trace.c

+16-7
Original file line numberDiff line numberDiff line change
@@ -8492,6 +8492,10 @@ static int tracing_buffers_mmap(struct file *filp, struct vm_area_struct *vma)
84928492
struct trace_iterator *iter = &info->iter;
84938493
int ret = 0;
84948494

8495+
/* A memmap'ed buffer is not supported for user space mmap */
8496+
if (iter->tr->flags & TRACE_ARRAY_FL_MEMMAP)
8497+
return -ENODEV;
8498+
84958499
/* Currently the boot mapped buffer is not supported for mmap */
84968500
if (iter->tr->flags & TRACE_ARRAY_FL_BOOT)
84978501
return -ENODEV;
@@ -9600,9 +9604,6 @@ static void free_trace_buffers(struct trace_array *tr)
96009604
#ifdef CONFIG_TRACER_MAX_TRACE
96019605
free_trace_buffer(&tr->max_buffer);
96029606
#endif
9603-
9604-
if (tr->range_addr_start)
9605-
vunmap((void *)tr->range_addr_start);
96069607
}
96079608

96089609
static void init_trace_flags_index(struct trace_array *tr)
@@ -10696,6 +10697,7 @@ static inline void do_allocate_snapshot(const char *name) { }
1069610697
__init static void enable_instances(void)
1069710698
{
1069810699
struct trace_array *tr;
10700+
bool memmap_area = false;
1069910701
char *curr_str;
1070010702
char *name;
1070110703
char *str;
@@ -10764,6 +10766,7 @@ __init static void enable_instances(void)
1076410766
name);
1076510767
continue;
1076610768
}
10769+
memmap_area = true;
1076710770
} else if (tok) {
1076810771
if (!reserve_mem_find_by_name(tok, &start, &size)) {
1076910772
start = 0;
@@ -10784,7 +10787,10 @@ __init static void enable_instances(void)
1078410787
continue;
1078510788
}
1078610789

10787-
addr = map_pages(start, size);
10790+
if (memmap_area)
10791+
addr = map_pages(start, size);
10792+
else
10793+
addr = (unsigned long)phys_to_virt(start);
1078810794
if (addr) {
1078910795
pr_info("Tracing: mapped boot instance %s at physical memory %pa of size 0x%lx\n",
1079010796
name, &start, (unsigned long)size);
@@ -10811,10 +10817,13 @@ __init static void enable_instances(void)
1081110817
update_printk_trace(tr);
1081210818

1081310819
/*
10814-
* If start is set, then this is a mapped buffer, and
10815-
* cannot be deleted by user space, so keep the reference
10816-
* to it.
10820+
* memmap'd buffers can not be freed.
1081710821
*/
10822+
if (memmap_area) {
10823+
tr->flags |= TRACE_ARRAY_FL_MEMMAP;
10824+
tr->ref++;
10825+
}
10826+
1081810827
if (start) {
1081910828
tr->flags |= TRACE_ARRAY_FL_BOOT | TRACE_ARRAY_FL_LAST_BOOT;
1082010829
tr->range_name = no_free_ptr(rname);

kernel/trace/trace.h

+1
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,7 @@ enum {
446446
TRACE_ARRAY_FL_BOOT = BIT(1),
447447
TRACE_ARRAY_FL_LAST_BOOT = BIT(2),
448448
TRACE_ARRAY_FL_MOD_INIT = BIT(3),
449+
TRACE_ARRAY_FL_MEMMAP = BIT(4),
449450
};
450451

451452
#ifdef CONFIG_MODULES

0 commit comments

Comments
 (0)