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
8492 struct trace_iterator *iter = &info->iter;8492 struct trace_iterator *iter = &info->iter;
8493 int ret = 0;8493 int ret = 0;
8494 8494
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
8495 /* Currently the boot mapped buffer is not supported for mmap */8499 /* Currently the boot mapped buffer is not supported for mmap */
8496 if (iter->tr->flags & TRACE_ARRAY_FL_BOOT)8500 if (iter->tr->flags & TRACE_ARRAY_FL_BOOT)
8497 return -ENODEV;8501 return -ENODEV;
@@ -9600,9 +9604,6 @@ static void free_trace_buffers(struct trace_array *tr) · 1102 unmodified lines
9600#ifdef CONFIG_TRACER_MAX_TRACE9604#ifdef CONFIG_TRACER_MAX_TRACE
9601 free_trace_buffer(&tr->max_buffer);9605 free_trace_buffer(&tr->max_buffer);
9602#endif9606#endif
9603
9604 if (tr->range_addr_start)
9605 vunmap((void *)tr->range_addr_start);
9606}9607}
9607 9608
9608static void init_trace_flags_index(struct trace_array *tr)9609static 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
10696__init static void enable_instances(void)10697__init static void enable_instances(void)
10697{10698{
10698 struct trace_array *tr;10699 struct trace_array *tr;
10700 bool memmap_area = false;
10699 char *curr_str;10701 char *curr_str;
10700 char *name;10702 char *name;
10701 char *str;10703 char *str;
@@ -10764,6 +10766,7 @@ __init static void enable_instances(void) · 62 unmodified lines
10764 name);10766 name);
10765 continue;10767 continue;
10766 }10768 }
10769 memmap_area = true;
10767 } else if (tok) {10770 } else if (tok) {
10768 if (!reserve_mem_find_by_name(tok, &start, &size)) {10771 if (!reserve_mem_find_by_name(tok, &start, &size)) {
10769 start = 0;10772 start = 0;
@@ -10784,7 +10787,10 @@ __init static void enable_instances(void) · 14 unmodified lines
10784 continue;10787 continue;
10785 }10788 }
10786 10789
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);
10788 if (addr) {10794 if (addr) {
10789 pr_info("Tracing: mapped boot instance %s at physical memory %pa of size 0x%lx\n",10795 pr_info("Tracing: mapped boot instance %s at physical memory %pa of size 0x%lx\n",
10790 name, &start, (unsigned long)size);10796 name, &start, (unsigned long)size);
@@ -10811,10 +10817,13 @@ __init static void enable_instances(void) · 20 unmodified lines
10811 update_printk_trace(tr);10817 update_printk_trace(tr);
10812 10818
10813 /*10819 /*
10814 * If start is set, then this is a mapped buffer, and10820 * memmap'd buffers can not be freed.
10815 * cannot be deleted by user space, so keep the reference
10816 * to it.
10817 */10821 */
10822 if (memmap_area) {
10823 tr->flags |= TRACE_ARRAY_FL_MEMMAP;
10824 tr->ref++;
10825 }
10826
10818 if (start) {10827 if (start) {
10819 tr->flags |= TRACE_ARRAY_FL_BOOT | TRACE_ARRAY_FL_LAST_BOOT;10828 tr->flags |= TRACE_ARRAY_FL_BOOT | TRACE_ARRAY_FL_LAST_BOOT;
10820 tr->range_name = no_free_ptr(rname);10829 tr->range_name = no_free_ptr(rname);
kernel/trace/trace.h+1−0
@@ -446,6 +446,7 @@ enum { · 445 unmodified lines
446 TRACE_ARRAY_FL_BOOT = BIT(1),446 TRACE_ARRAY_FL_BOOT = BIT(1),
447 TRACE_ARRAY_FL_LAST_BOOT = BIT(2),447 TRACE_ARRAY_FL_LAST_BOOT = BIT(2),
448 TRACE_ARRAY_FL_MOD_INIT = BIT(3),448 TRACE_ARRAY_FL_MOD_INIT = BIT(3),
449 TRACE_ARRAY_FL_MEMMAP = BIT(4),
449};450};
450 451
451#ifdef CONFIG_MODULES452#ifdef CONFIG_MODULES