From nobody Fri Oct 3 11:26:02 2025 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 8868E309DB5; Tue, 2 Sep 2025 14:35:26 +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=1756823726; cv=none; b=mmJJSOuLqBR3mi5t2BAhLKXrFOZEqg6pzsXgeTS+mYXg9EtWyKa55lxq+uAltvFvGJejomYL5K1RZHj/SRFVi8gIDEZsBMagYFvdA13jh+0V3z82Uk0LzsH4qT37DvupoETRvV2VIghpdU3r5iH+uEzoEIBOgA+g9KWTSYP1Q5c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756823726; c=relaxed/simple; bh=tDmYNAmIv1AO38nKYIGGnLwjJ4+wCAxin6PUU3wtmPc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=JSTmGBVlnhku8+sBGaoCTnKkJk9jhl14Exc28A8YwdhSF6iNTmawnPhePd3wKqY4VdKW5iqSkzAJY8zoK1xPyILVSOpYaBl7fxMuKC9D9S7O2oRwAeGddTZVtJHkF4fr7D5Y4WqmOXxBkyC2XUKhahOdJK+TBnxinD308JteDg8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=dFlSDziQ; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="dFlSDziQ" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8E478C4CEED; Tue, 2 Sep 2025 14:35:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1756823726; bh=tDmYNAmIv1AO38nKYIGGnLwjJ4+wCAxin6PUU3wtmPc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=dFlSDziQfQjciFA3+labMmTVTZOcmG2QAcSZNF+zyid4HQIDg4rj2+MLWOS3US1xS VK6PVezRNlO8d6SJfrYY8n5GFmT7KRk5pskHfXrqZRT8YayKqIUXH6I1FUnJ7SQsCm NfEZa6ZJX6F0UIXO4gwTZOHxzQpY+PYUhXWc6QU0gA7M+JmgqAoTjmvwUlgkIo3wiW KdU67JG8Y+uT8YqtgHEryoynGf/Z2TxzJojXN+Zu1neRCBT49iKZk9p7T3AFjxp5wc 8lctWLh4ysbfj3hTJKQVz+8gu4fBNS2KDkdej6334+8cLfvW7Iq9+kCvmMSVI/dRqN 1qCNa2XZjtgsQ== From: Jiri Olsa To: Oleg Nesterov , Masami Hiramatsu , Peter Zijlstra , Andrii Nakryiko Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, x86@kernel.org, Song Liu , Yonghong Song , John Fastabend , Hao Luo , Steven Rostedt , Ingo Molnar Subject: [PATCH perf/core 01/11] uprobes: Add unique flag to uprobe consumer Date: Tue, 2 Sep 2025 16:34:54 +0200 Message-ID: <20250902143504.1224726-2-jolsa@kernel.org> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20250902143504.1224726-1-jolsa@kernel.org> References: <20250902143504.1224726-1-jolsa@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Adding unique flag to uprobe consumer to ensure it's the only consumer attached on the uprobe. This is helpful for use cases when consumer wants to change user space registers, which might confuse other consumers. With this change we can ensure there's only one consumer on specific uprobe. Signed-off-by: Jiri Olsa --- include/linux/uprobes.h | 1 + kernel/events/uprobes.c | 30 ++++++++++++++++++++++++++++-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/include/linux/uprobes.h b/include/linux/uprobes.h index 08ef78439d0d..0df849dee720 100644 --- a/include/linux/uprobes.h +++ b/include/linux/uprobes.h @@ -60,6 +60,7 @@ struct uprobe_consumer { struct list_head cons_node; =20 __u64 id; /* set when uprobe_consumer is registered */ + bool is_unique; /* the only consumer on uprobe */ }; =20 #ifdef CONFIG_UPROBES diff --git a/kernel/events/uprobes.c b/kernel/events/uprobes.c index 996a81080d56..b9b088f7333a 100644 --- a/kernel/events/uprobes.c +++ b/kernel/events/uprobes.c @@ -1024,14 +1024,35 @@ static struct uprobe *alloc_uprobe(struct inode *in= ode, loff_t offset, return uprobe; } =20 -static void consumer_add(struct uprobe *uprobe, struct uprobe_consumer *uc) +static bool consumer_can_add(struct list_head *head, struct uprobe_consume= r *uc) +{ + /* Uprobe has no consumer, we can add any. */ + if (list_empty(head)) + return true; + /* Uprobe has consumer/s, we can't add unique one. */ + if (uc->is_unique) + return false; + /* + * Uprobe has consumer/s, we can add nother consumer only if the + * current consumer is not unique. + **/ + return !list_first_entry(head, struct uprobe_consumer, cons_node)->is_uni= que; +} + +static int consumer_add(struct uprobe *uprobe, struct uprobe_consumer *uc) { static atomic64_t id; + int ret =3D -EBUSY; =20 down_write(&uprobe->consumer_rwsem); + if (!consumer_can_add(&uprobe->consumers, uc)) + goto unlock; list_add_rcu(&uc->cons_node, &uprobe->consumers); uc->id =3D (__u64) atomic64_inc_return(&id); + ret =3D 0; +unlock: up_write(&uprobe->consumer_rwsem); + return ret; } =20 /* @@ -1420,7 +1441,12 @@ struct uprobe *uprobe_register(struct inode *inode, return uprobe; =20 down_write(&uprobe->register_rwsem); - consumer_add(uprobe, uc); + ret =3D consumer_add(uprobe, uc); + if (ret) { + put_uprobe(uprobe); + up_write(&uprobe->register_rwsem); + return ERR_PTR(ret); + } ret =3D register_for_each_vma(uprobe, uc); up_write(&uprobe->register_rwsem); =20 --=20 2.51.0