Received: by 2002:ab2:6309:0:b0:1fb:d597:ff75 with SMTP id s9csp656033lqt; Thu, 6 Jun 2024 14:30:56 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUpFOgvSx1IPWsOLucMq44wgvaahVQru3YsmM49qx3YuOUtzr0iUY2kzipA5ZVXH65xSrmQKqi9XfxbkkdlfRJZ/4WIhRcyOgUGgasobw== X-Google-Smtp-Source: AGHT+IFpZPIccQ0WVDfurDgfB/dpXIz+K0Xswo4aQg/ZntN5gt2FNGG8sVwC5/WMUkgftqRDdsWk X-Received: by 2002:a05:6870:649f:b0:250:4bcb:9a0a with SMTP id 586e51a60fabf-254648d19cbmr730722fac.52.1717709456611; Thu, 06 Jun 2024 14:30:56 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717709456; cv=pass; d=google.com; s=arc-20160816; b=OkDZpT8htjHwoxuJ1/sEvp36oPN7OkRrHFlKspcwn7TPnPPcVktKHFVNnX0YXeY1zF j0aFBqq0hR7tPSbCrpSuv/+LR1GCJHLKHOrMd6tHCcGyRyRZ4+oTqcZGnH50WT9smiXY kg1q1Pp+WjeHlW7i6+Tt1xZ+Px80e0XJLjBECoN4CdoYkpbMbK8ygYCCzJEWrDJS1CG5 oQtwhrSFBs2Yn5GkjXH4xgX2iJKdfIhsh2xqjVaEQODdVbUoB2wgeEMayzePEmyHSHsR ODYLneALQ0sCOlaiDMAAoKjN6wSsQQGFfdsz3TcW+tI2bPNe+pq+DR6OY955P3CSI6jn sVdA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :subject:cc:to:from:date; bh=s3kxjfwKBIlKyh9ydlf6kQ7HUhPeiMEvU3E57WA+mAc=; fh=APLEwzDCuZjAyYd8ZCw4YQ4qXWbcLRWeojtUCuRmRf0=; b=0+RtMJzHqxbHaWC7Tg2m7sMNYAt0K5+7rr5yhL+Nr/oPzx4WVnzzGhidrqJ7oJr1fK p8lljFW3QmNYbXC/U24Unh5XPpQWzLcArk8ZofBwoQMZOG0keQuAMFjmP4gf52mwfzNo g6d8ZxpLZJvI/+lgkqSeH6ssMHm/AcEeJFlZtlT4wWuPGc7Jtrp05j8WY5reRa8EGaRk gQ4KEycAwWHh2zG0GXLkdZIHcat9f0lmhxUo9tR4PmjStHss6La3eHqxhv65ga7Sfv2n iGgA//UvSnYTH6W3nW2ByHJpuE64uUPPZ00vtNgvRNRUb/chYNbL4jwb2QoPBCTXvgmt fE9A==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel+bounces-205081-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-205081-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id af79cd13be357-79533339dadsi184811085a.581.2024.06.06.14.30.56 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 06 Jun 2024 14:30:56 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-205081-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel+bounces-205081-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-205081-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 401A71C2523D for ; Thu, 6 Jun 2024 21:30:56 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 97260199E85; Thu, 6 Jun 2024 21:26:24 +0000 (UTC) Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E937F199E81; Thu, 6 Jun 2024 21:26:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717709184; cv=none; b=Lto2ImOcJGahh5We74/tkRmXN63rpDj1UvJIPXbfd41igBih0U3x6XOHmb3aUoGeAFKCn3oGvnRM599/isYYphQ/SUExheP0F/ptXJbg7dHJE0iiQFY1qXVzzi9MGZZqInh4Zx9bG7LMOrlBD16R7Jpm3TxnpJ/EFdcKjoZrIVY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717709184; c=relaxed/simple; bh=u3BMVhJFQV+4/9LSqKjoNcdopb32tJzJ4B4aOzQyuKE=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=f1R8iPlDPUP2N7cFCHcmtqP3F9p3Qdw5KF4eNCxOutrLZkusKh3YpHZCJfSweIeIeaBY27bdo6IQEExATI8dZlFYzSt/QWNPRrUPa/PqHsxop3w6G7FXO7oxQdCcJ+fqA1D/GLuOUhDcUq17ehZaLvIRzulfGTcWjoy28632vY8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 711ABC2BD10; Thu, 6 Jun 2024 21:26:19 +0000 (UTC) Date: Thu, 6 Jun 2024 17:26:31 -0400 From: Steven Rostedt To: linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org Cc: Masami Hiramatsu , Mark Rutland , Mathieu Desnoyers , Andrew Morton , Vincent Donnefort , Joel Fernandes , Daniel Bristot de Oliveira , Ingo Molnar , Peter Zijlstra , suleiman@google.com, Thomas Gleixner , Vineeth Pillai , Youssef Esmat , Beau Belgrave , Alexander Graf , Baoquan He , Borislav Petkov , "Paul E. McKenney" , David Howells , Mike Rapoport , Dave Hansen , Tony Luck , Guenter Roeck , Ross Zwisler , Kees Cook , linux-mm@kvack.org Subject: Re: [PATCH v3 08/13] tracing: Add option to use memmapped memory for trace boot instance Message-ID: <20240606172631.4a4b1cf7@rorschach.local.home> In-Reply-To: <20240606212137.333436708@goodmis.org> References: <20240606211735.684785459@goodmis.org> <20240606212137.333436708@goodmis.org> X-Mailer: Claws Mail 3.17.8 (GTK+ 2.24.33; x86_64-pc-linux-gnu) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Memory management folks. Please review this patch. Specifically the "map_pages()" function below. On Thu, 06 Jun 2024 17:17:43 -0400 Steven Rostedt wrote: > From: "Steven Rostedt (Google)" > > Add an option to the trace_instance kernel command line parameter that > allows it to use the reserved memory from memmap boot parameter. > > memmap=12M$0x284500000 trace_instance=boot_mapped@0x284500000:12M > > The above will reserves 12 megs at the physical address 0x284500000. > The second parameter will create a "boot_mapped" instance and use the > memory reserved as the memory for the ring buffer. > > That will create an instance called "boot_mapped": > > /sys/kernel/tracing/instances/boot_mapped > > Note, because the ring buffer is using a defined memory ranged, it will > act just like a memory mapped ring buffer. It will not have a snapshot > buffer, as it can't swap out the buffer. The snapshot files as well as any > tracers that uses a snapshot will not be present in the boot_mapped > instance. > > Cc: linux-mm@kvack.org > Signed-off-by: Steven Rostedt (Google) > --- > .../admin-guide/kernel-parameters.txt | 9 +++ > kernel/trace/trace.c | 75 +++++++++++++++++-- > 2 files changed, 78 insertions(+), 6 deletions(-) > > diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt > index b600df82669d..ff26b6094e79 100644 > --- a/Documentation/admin-guide/kernel-parameters.txt > +++ b/Documentation/admin-guide/kernel-parameters.txt > @@ -6754,6 +6754,15 @@ > the same thing would happen if it was left off). The irq_handler_entry > event, and all events under the "initcall" system. > > + If memory has been reserved (see memmap for x86), the instance > + can use that memory: > + > + memmap=12M$0x284500000 trace_instance=boot_map@0x284500000:12M > + > + The above will create a "boot_map" instance that uses the physical > + memory at 0x284500000 that is 12Megs. The per CPU buffers of that > + instance will be split up accordingly. > + > trace_options=[option-list] > [FTRACE] Enable or disable tracer options at boot. > The option-list is a comma delimited list of options > diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c > index 622fe670949d..13e89023f33b 100644 > --- a/kernel/trace/trace.c > +++ b/kernel/trace/trace.c > @@ -9504,6 +9504,31 @@ static int instance_mkdir(const char *name) > return ret; > } > > +static u64 map_pages(u64 start, u64 size) > +{ > + struct page **pages; > + phys_addr_t page_start; > + unsigned int page_count; > + unsigned int i; > + void *vaddr; > + > + page_count = DIV_ROUND_UP(size, PAGE_SIZE); > + > + page_start = start; > + pages = kmalloc_array(page_count, sizeof(struct page *), GFP_KERNEL); > + if (!pages) > + return 0; > + > + for (i = 0; i < page_count; i++) { > + phys_addr_t addr = page_start + i * PAGE_SIZE; > + pages[i] = pfn_to_page(addr >> PAGE_SHIFT); > + } > + vaddr = vmap(pages, page_count, VM_MAP, PAGE_KERNEL); > + kfree(pages); > + > + return (u64)(unsigned long)vaddr; > +} If for some reason the memmap=nn$ss fails, but this still gets called, will the above just map over any memory. That is, is it possible that the kernel could have used this memory? Is there a way to detect this? That is, I don't want this to succeed if the memory location it's about to map to is used by the kernel, or will be used by user space. -- Steve > + > /** > * trace_array_get_by_name - Create/Lookup a trace array, given its name. > * @name: The name of the trace array to be looked up/created. > @@ -10350,6 +10375,7 @@ __init static void enable_instances(void) > { > struct trace_array *tr; > char *curr_str; > + char *name; > char *str; > char *tok; > > @@ -10358,19 +10384,56 @@ __init static void enable_instances(void) > str = boot_instance_info; > > while ((curr_str = strsep(&str, "\t"))) { > + unsigned long start = 0; > + unsigned long size = 0; > + unsigned long addr = 0; > > tok = strsep(&curr_str, ","); > + name = strsep(&tok, "@"); > + if (tok) { > + start = memparse(tok, &tok); > + if (!start) { > + pr_warn("Tracing: Invalid boot instance address for %s\n", > + name); > + continue; > + } > + } > > - if (IS_ENABLED(CONFIG_TRACER_MAX_TRACE)) > - do_allocate_snapshot(tok); > + if (start) { > + if (*tok != ':') { > + pr_warn("Tracing: No size specified for instance %s\n", name); > + continue; > + } > + tok++; > + size = memparse(tok, &tok); > + if (!size) { > + pr_warn("Tracing: Invalid boot instance size for %s\n", > + name); > + continue; > + } > + addr = map_pages(start, size); > + if (addr) { > + pr_info("Tracing: mapped boot instance %s at physical memory 0x%lx of size 0x%lx\n", > + name, start, size); > + } else { > + pr_warn("Tracing: Failed to map boot instance %s\n", name); > + continue; > + } > + } else { > + /* Only non mapped buffers have snapshot buffers */ > + if (IS_ENABLED(CONFIG_TRACER_MAX_TRACE)) > + do_allocate_snapshot(tok); > + } > > - tr = trace_array_get_by_name(tok, NULL); > + tr = trace_array_create_systems(name, NULL, addr, size); > if (!tr) { > - pr_warn("Failed to create instance buffer %s\n", curr_str); > + pr_warn("Tracing: Failed to create instance buffer %s\n", curr_str); > continue; > } > - /* Allow user space to delete it */ > - trace_array_put(tr); > + > + /* Only allow non mapped buffers to be deleted */ > + if (!start) > + trace_array_put(tr); > > while ((tok = strsep(&curr_str, ","))) { > early_enable_events(tr, tok, true);