linux-trace-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jiri Olsa <olsajiri@gmail.com>
To: Masami Hiramatsu <mhiramat@kernel.org>, Oleg Nesterov <oleg@redhat.com>
Cc: "Peter Zijlstra" <peterz@infradead.org>,
	"Andrii Nakryiko" <andrii@kernel.org>,
	bpf@vger.kernel.org, linux-kernel@vger.kernel.org,
	linux-trace-kernel@vger.kernel.org, x86@kernel.org,
	"Song Liu" <songliubraving@fb.com>, "Yonghong Song" <yhs@fb.com>,
	"John Fastabend" <john.fastabend@gmail.com>,
	"Hao Luo" <haoluo@google.com>,
	"Steven Rostedt" <rostedt@goodmis.org>,
	"Alan Maguire" <alan.maguire@oracle.com>,
	"David Laight" <David.Laight@aculab.com>,
	"Thomas Weißschuh" <thomas@t-8ch.de>,
	"Ingo Molnar" <mingo@kernel.org>
Subject: Re: [PATCHv3 perf/core 08/22] uprobes/x86: Add mapping for optimized uprobe trampolines
Date: Fri, 4 Jul 2025 10:23:08 +0200	[thread overview]
Message-ID: <aGePbMh3_yw2makD@krava> (raw)
In-Reply-To: <aF6Q9NgCJx5p0MNJ@krava>

On Fri, Jun 27, 2025 at 02:39:16PM +0200, Jiri Olsa wrote:
> On Fri, Jun 27, 2025 at 03:01:45PM +0900, Masami Hiramatsu wrote:
> 
> SNIP
> 
> > > > 
> > > > > +			return tramp;
> > > > > +	}
> > > > > +
> > > > > +	tramp = create_uprobe_trampoline(vaddr);
> > > > > +	if (!tramp)
> > > > > +		return NULL;
> > > > > +
> > > > > +	*new = true;
> > > > > +	hlist_add_head(&tramp->node, &state->head_tramps);
> > > > > +	return tramp;
> > > > > +}
> > > > > +
> > > > > +static void destroy_uprobe_trampoline(struct uprobe_trampoline *tramp)
> > > > > +{
> > > > > +	hlist_del(&tramp->node);
> > > > > +	kfree(tramp);
> > > > 
> > > > Don't we need to unmap the tramp->vaddr?
> > > 
> > > that's tricky because we have no way to make sure the application is
> > > no longer executing the trampoline, it's described in the changelog
> > > of following patch:
> > > 
> > >     uprobes/x86: Add support to optimize uprobes
> > > 
> > >     ...
> > > 
> > >     We do not unmap and release uprobe trampoline when it's no longer needed,
> > >     because there's no easy way to make sure none of the threads is still
> > >     inside the trampoline. But we do not waste memory, because there's just
> > >     single page for all the uprobe trampoline mappings.
> > > 
> > 
> > I think we should put this as a code comment.
> 
> ok
> 
> > 
> > >     We do waste frame on page mapping for every 4GB by keeping the uprobe
> > >     trampoline page mapped, but that seems ok.
> > 
> > Hmm, this is not right with the current find_nearest_page(), because
> > it always finds a page from the farthest +2GB range until it is full.
> > Thus, in the worst case, if we hits uprobes with the order of
> > uprobe0 -> 1 -> 2 which is put as below;
> > 
> > 0x0abc0004  [uprobe2]
> > ...
> > 0x0abc2004  [uprobe1]
> > ...
> > 0x0abc4004  [uprobe0]
> > 
> > Then the trampoline pages can be allocated as below.
> > 
> > 0x8abc0000  [uprobe_tramp2]
> > [gap]
> > 0x8abc2000  [uprobe_tramp1]
> > [gap]
> > 0x8abc4000  [uprobe_tramp0]
> > 
> > Using true "find_nearest_page()", this will be mitigated. But not
> > allocated for "every 4GB". So I think we should drop that part
> > from the comment :)
> 
> I think you're right, it's better to start with nearest page,
> will change it in new version

I wonder we could actualy use page every 4GB (+-) code below seems to work, wdyt?

thanks,
jirka


---

+#define __4GB		 (1UL << 32)
+#define MASK_4GB	~(__4GB - 1)
+#define PAGE_COUNT(addr) ((addr & ~MASK_4GB) >> PAGE_SHIFT)
+
+static unsigned long find_nearest_page(unsigned long vaddr)
+{
+	struct vm_unmapped_area_info info = {
+		.length     = PAGE_SIZE,
+		.align_mask = ~PAGE_MASK,
+	};
+	unsigned long limit, low_limit = PAGE_SIZE, high_limit = TASK_SIZE;
+	unsigned long cross_4GB, low_4GB, high_4GB;
+	unsigned long low_tramp, high_tramp;
+	unsigned long call_end = vaddr + 5;
+
+	/*
+	 * The idea is to create a trampoline every 4GB, so we need to find
+	 * free page closest to the 4GB alignment. We find intersecting 4GB
+	 * alignment address and search up and down to find the closest free
+	 * page.
+	 */
+
+	low_4GB = call_end & MASK_4GB;
+	high_4GB = low_4GB + __4GB;
+
+	/* Restrict limits to be within (PAGE_SIZE,TASK_SIZE) boundaries. */
+	if (!check_add_overflow(call_end, INT_MIN, &limit))
+		low_limit = limit;
+	if (low_limit == PAGE_SIZE)
+		low_4GB = low_limit;
+
+	high_limit = call_end + INT_MAX;
+	if (high_limit > TASK_SIZE)
+		high_limit = high_4GB = TASK_SIZE;
+
+	/* Get 4GB alligned address that's within 2GB distance from call_end */
+	if (low_limit <= low_4GB)
+		cross_4GB = low_4GB;
+	else
+		cross_4GB = high_4GB;
+
+	/* Search up from intersecting 4GB alignment address. */
+	info.low_limit = cross_4GB;
+	info.high_limit = high_limit;
+	high_tramp = vm_unmapped_area(&info);
+
+	/* Search down from intersecting 4GB alignment address. */
+	info.low_limit = low_limit;
+	info.high_limit = cross_4GB;
+	info.flags = VM_UNMAPPED_AREA_TOPDOWN;
+	low_tramp = vm_unmapped_area(&info);
+
+	if (IS_ERR_VALUE(high_tramp) && IS_ERR_VALUE(low_tramp))
+		return -ENOMEM;
+	if (IS_ERR_VALUE(high_tramp))
+		return low_tramp;
+	if (IS_ERR_VALUE(low_tramp))
+		return high_tramp;
+
+	/* Return address that's closest to the 4GB alignment address. */
+	if (cross_4GB - low_tramp < high_tramp - cross_4GB)
+		return low_tramp;
+	return high_tramp;
+}

  reply	other threads:[~2025-07-04  8:23 UTC|newest]

Thread overview: 39+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-05 13:23 [PATCHv3 perf/core 00/22] uprobes: Add support to optimize usdt probes on x86_64 Jiri Olsa
2025-06-05 13:23 ` [PATCHv3 perf/core 01/22] uprobes: Remove breakpoint in unapply_uprobe under mmap_write_lock Jiri Olsa
2025-06-25  6:04   ` Masami Hiramatsu
2025-06-05 13:23 ` [PATCHv3 perf/core 02/22] uprobes: Rename arch_uretprobe_trampoline function Jiri Olsa
2025-06-05 13:23 ` [PATCHv3 perf/core 03/22] uprobes: Make copy_from_page global Jiri Olsa
2025-06-05 13:23 ` [PATCHv3 perf/core 04/22] uprobes: Add uprobe_write function Jiri Olsa
2025-06-25  6:12   ` Masami Hiramatsu
2025-06-05 13:23 ` [PATCHv3 perf/core 05/22] uprobes: Add nbytes argument to uprobe_write Jiri Olsa
2025-06-25  6:13   ` Masami Hiramatsu
2025-06-05 13:23 ` [PATCHv3 perf/core 06/22] uprobes: Add is_register argument to uprobe_write and uprobe_write_opcode Jiri Olsa
2025-06-25  6:32   ` Masami Hiramatsu
2025-06-05 13:23 ` [PATCHv3 perf/core 07/22] uprobes: Add do_ref_ctr argument to uprobe_write function Jiri Olsa
2025-06-25  6:42   ` Masami Hiramatsu
2025-06-25 15:11     ` Jiri Olsa
2025-06-27  4:58       ` Masami Hiramatsu
2025-06-05 13:23 ` [PATCHv3 perf/core 08/22] uprobes/x86: Add mapping for optimized uprobe trampolines Jiri Olsa
2025-06-25  8:21   ` Masami Hiramatsu
2025-06-25 15:16     ` Jiri Olsa
2025-06-27  6:01       ` Masami Hiramatsu
2025-06-27 12:39         ` Jiri Olsa
2025-07-04  8:23           ` Jiri Olsa [this message]
2025-06-05 13:23 ` [PATCHv3 perf/core 09/22] uprobes/x86: Add uprobe syscall to speed up uprobe Jiri Olsa
2025-06-05 13:23 ` [PATCHv3 perf/core 10/22] uprobes/x86: Add support to optimize uprobes Jiri Olsa
2025-06-05 13:23 ` [PATCHv3 perf/core 11/22] selftests/bpf: Import usdt.h from libbpf/usdt project Jiri Olsa
2025-06-05 13:23 ` [PATCHv3 perf/core 12/22] selftests/bpf: Reorg the uprobe_syscall test function Jiri Olsa
2025-06-05 13:23 ` [PATCHv3 perf/core 13/22] selftests/bpf: Rename uprobe_syscall_executed prog to test_uretprobe_multi Jiri Olsa
2025-06-05 13:23 ` [PATCHv3 perf/core 14/22] selftests/bpf: Add uprobe/usdt syscall tests Jiri Olsa
2025-06-05 13:23 ` [PATCHv3 perf/core 15/22] selftests/bpf: Add hit/attach/detach race optimized uprobe test Jiri Olsa
2025-06-05 13:23 ` [PATCHv3 perf/core 16/22] selftests/bpf: Add uprobe syscall sigill signal test Jiri Olsa
2025-06-05 13:23 ` [PATCHv3 perf/core 17/22] selftests/bpf: Add optimized usdt variant for basic usdt test Jiri Olsa
2025-06-05 13:23 ` [PATCHv3 perf/core 18/22] selftests/bpf: Add uprobe_regs_equal test Jiri Olsa
2025-06-05 13:23 ` [PATCHv3 perf/core 19/22] selftests/bpf: Change test_uretprobe_regs_change for uprobe and uretprobe Jiri Olsa
2025-06-05 13:23 ` [PATCHv3 perf/core 20/22] seccomp: passthrough uprobe systemcall without filtering Jiri Olsa
2025-06-05 13:23 ` [PATCHv3 perf/core 21/22] selftests/seccomp: validate uprobe syscall passes through seccomp Jiri Olsa
2025-06-05 13:23 ` [PATCHv3 22/22] man2: Add uprobe syscall page Jiri Olsa
2025-06-11  8:30   ` Alejandro Colomar
2025-06-17 13:08 ` [PATCHv3 perf/core 00/22] uprobes: Add support to optimize usdt probes on x86_64 Jiri Olsa
2025-06-24  8:36   ` Jiri Olsa
2025-06-25  6:05     ` Masami Hiramatsu

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=aGePbMh3_yw2makD@krava \
    --to=olsajiri@gmail.com \
    --cc=David.Laight@aculab.com \
    --cc=alan.maguire@oracle.com \
    --cc=andrii@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=haoluo@google.com \
    --cc=john.fastabend@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=mhiramat@kernel.org \
    --cc=mingo@kernel.org \
    --cc=oleg@redhat.com \
    --cc=peterz@infradead.org \
    --cc=rostedt@goodmis.org \
    --cc=songliubraving@fb.com \
    --cc=thomas@t-8ch.de \
    --cc=x86@kernel.org \
    --cc=yhs@fb.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).