History

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/Z-wFszhJ_9o4dc8O@kernel.org/

Steven Rostedt committed 1 year ago

Commit 34ea8fa1 parent c44a14f2 files · +17 −7Browse files

Unified · Split

2 files
  1. kernel/trace/trace.c +16 −7
  2. kernel/trace/trace.h +1 −0
kernel/trace/trace.c+16−7
@@ -8492,6 +8492,10 @@ static int tracing_buffers_mmap(struct file *filp, struct vm_area_struct *vma) · 8491 unmodified lines
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) · 1102 unmodified lines
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) { } · 1087 unmodified lines
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) · 62 unmodified lines
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) · 14 unmodified lines
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) · 20 unmodified lines
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−0
@@ -446,6 +446,7 @@ enum { · 445 unmodified lines
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