* [PATCH v4 07/12] crypto: atmel-i2c - introduce shared teardown helpers and fix queue flush
From: Lothar Rubusch @ 2026-05-22 23:01 UTC (permalink / raw)
To: thorsten.blum, herbert, davem, nicolas.ferre, alexandre.belloni,
claudiu.beznea, tudor.ambarus, ardb, linusw, krzk+dt
Cc: linux-crypto, linux-arm-kernel, linux-kernel, l.rubusch
In-Reply-To: <20260522230134.32414-1-l.rubusch@gmail.com>
Introduce atmel_i2c_deactivate_client() and atmel_i2c_unregister_client()
helpers in the atmel-i2c core library to modularize client teardown. This
encapsulates common client state tracking and list manipulation operations.
Convert the ECC driver's error recovery and device removal paths to utilize
these new helpers, ensuring consistent execution ordering when modifying
device-readiness states and deleting linked-list nodes.
Additionally, migrate the atmel_i2c_flush_queue() call out of the module
exit path. It now runs inside the core unregistration helper. Export both
new tracking symbols via EXPORT_SYMBOL_GPL() to match the existing core
driver licensing standard.
Signed-off-by: Lothar Rubusch <l.rubusch@gmail.com>
---
drivers/crypto/atmel-ecc.c | 25 ++++++-------------------
drivers/crypto/atmel-i2c.c | 20 ++++++++++++++++++++
drivers/crypto/atmel-i2c.h | 3 +++
3 files changed, 29 insertions(+), 19 deletions(-)
diff --git a/drivers/crypto/atmel-ecc.c b/drivers/crypto/atmel-ecc.c
index 1ae9c52812df..e6d3e6574251 100644
--- a/drivers/crypto/atmel-ecc.c
+++ b/drivers/crypto/atmel-ecc.c
@@ -351,12 +351,8 @@ static int atmel_ecc_probe(struct i2c_client *client)
msecs_to_jiffies(2000));
mutex_lock(&atmel_ecc_kpp_lock);
if (timeout == 0) {
- spin_lock(&atmel_i2c_mgmt.i2c_list_lock);
- i2c_priv->ready = false;
- list_del(&i2c_priv->i2c_client_list_node);
- spin_unlock(&atmel_i2c_mgmt.i2c_list_lock);
- mutex_unlock(&atmel_ecc_kpp_lock);
-
+ atmel_i2c_deactivate_client(i2c_priv);
+ atmel_i2c_unregister_client(i2c_priv);
dev_err(&client->dev, "probe timed out, former driver instance not fully deregistered\n");
return -ETIMEDOUT;
}
@@ -365,12 +361,8 @@ static int atmel_ecc_probe(struct i2c_client *client)
if (atmel_ecc_kpp_refcnt == 0) {
ret = crypto_register_kpp(&atmel_ecdh_nist_p256);
if (ret) {
- spin_lock(&atmel_i2c_mgmt.i2c_list_lock);
- i2c_priv->ready = false;
- list_del(&i2c_priv->i2c_client_list_node);
- spin_unlock(&atmel_i2c_mgmt.i2c_list_lock);
- mutex_unlock(&atmel_ecc_kpp_lock);
-
+ atmel_i2c_deactivate_client(i2c_priv);
+ atmel_i2c_unregister_client(i2c_priv);
dev_err(&client->dev, "%s alg registration failed\n",
atmel_ecdh_nist_p256.base.cra_driver_name);
return ret;
@@ -388,9 +380,7 @@ static void atmel_ecc_remove(struct i2c_client *client)
struct atmel_i2c_client_priv *i2c_priv = i2c_get_clientdata(client);
bool trigger_unreg = false;
- spin_lock(&atmel_i2c_mgmt.i2c_list_lock);
- i2c_priv->ready = false;
- spin_unlock(&atmel_i2c_mgmt.i2c_list_lock);
+ atmel_i2c_deactivate_client(i2c_priv);
/*
* The Linux crypto core automatically blocks until all active
@@ -410,9 +400,7 @@ static void atmel_ecc_remove(struct i2c_client *client)
if (atomic_read(&i2c_priv->tfm_count))
wait_for_completion(&i2c_priv->remove_done);
- spin_lock(&atmel_i2c_mgmt.i2c_list_lock);
- list_del(&i2c_priv->i2c_client_list_node);
- spin_unlock(&atmel_i2c_mgmt.i2c_list_lock);
+ atmel_i2c_unregister_client(i2c_priv);
/*
* The driver registers once an algorithm, but maintains a list of
@@ -461,7 +449,6 @@ static int __init atmel_ecc_init(void)
static void __exit atmel_ecc_exit(void)
{
- atmel_i2c_flush_queue();
i2c_del_driver(&atmel_ecc_driver);
}
diff --git a/drivers/crypto/atmel-i2c.c b/drivers/crypto/atmel-i2c.c
index a42b0ea30033..db818ce55033 100644
--- a/drivers/crypto/atmel-i2c.c
+++ b/drivers/crypto/atmel-i2c.c
@@ -354,6 +354,26 @@ static int device_sanity_check(struct i2c_client *client)
return ret;
}
+void atmel_i2c_deactivate_client(struct atmel_i2c_client_priv *i2c_priv)
+{
+ spin_lock(&atmel_i2c_mgmt.i2c_list_lock);
+ i2c_priv->ready = false;
+ spin_unlock(&atmel_i2c_mgmt.i2c_list_lock);
+}
+EXPORT_SYMBOL_GPL(atmel_i2c_deactivate_client);
+
+void atmel_i2c_unregister_client(struct atmel_i2c_client_priv *i2c_priv)
+{
+ spin_lock(&atmel_i2c_mgmt.i2c_list_lock);
+ if (!list_empty(&i2c_priv->i2c_client_list_node))
+ list_del_init(&i2c_priv->i2c_client_list_node);
+ spin_unlock(&atmel_i2c_mgmt.i2c_list_lock);
+
+ /* don't sleep inside spin locks */
+ atmel_i2c_flush_queue();
+}
+EXPORT_SYMBOL_GPL(atmel_i2c_unregister_client);
+
int atmel_i2c_probe(struct i2c_client *client)
{
struct atmel_i2c_client_priv *i2c_priv;
diff --git a/drivers/crypto/atmel-i2c.h b/drivers/crypto/atmel-i2c.h
index 82321c35c21f..07fd2248e20b 100644
--- a/drivers/crypto/atmel-i2c.h
+++ b/drivers/crypto/atmel-i2c.h
@@ -193,4 +193,7 @@ void atmel_i2c_init_genkey_cmd(struct atmel_i2c_cmd *cmd, u16 keyid);
int atmel_i2c_init_ecdh_cmd(struct atmel_i2c_cmd *cmd,
struct scatterlist *pubkey);
+void atmel_i2c_deactivate_client(struct atmel_i2c_client_priv *i2c_priv);
+void atmel_i2c_unregister_client(struct atmel_i2c_client_priv *i2c_priv);
+
#endif /* __ATMEL_I2C_H__ */
--
2.39.5
^ permalink raw reply related
* [PATCH v4 10/12] crypto: atmel-i2c - implement capability-based client selection
From: Lothar Rubusch @ 2026-05-22 23:01 UTC (permalink / raw)
To: thorsten.blum, herbert, davem, nicolas.ferre, alexandre.belloni,
claudiu.beznea, tudor.ambarus, ardb, linusw, krzk+dt
Cc: linux-crypto, linux-arm-kernel, linux-kernel, l.rubusch
In-Reply-To: <20260522230134.32414-1-l.rubusch@gmail.com>
Extend the shared I2C client allocation interface to support feature-aware
hardware selection by introducing capability filtering.
Add a 'caps' mask to 'struct atmel_i2c_client_priv' alongside an
'atmel_i2c_capability' enum. The allocator now explicitly filters hardware
nodes by a requested capability bit while retaining the least-loaded device
load-balancing scheme.
Update the ECC driver to advertise ATMEL_CAP_ECDH configuration capability
during probe, and adapt the tfm context setup execution path to request
this specific capability variant. Initialize the bitmask field to zero
inside the SHA204A driver context for now.
Signed-off-by: Lothar Rubusch <l.rubusch@gmail.com>
---
drivers/crypto/atmel-ecc.c | 4 +++-
drivers/crypto/atmel-i2c.c | 6 +++++-
drivers/crypto/atmel-i2c.h | 8 +++++++-
drivers/crypto/atmel-sha204a.c | 2 ++
4 files changed, 17 insertions(+), 3 deletions(-)
diff --git a/drivers/crypto/atmel-ecc.c b/drivers/crypto/atmel-ecc.c
index 16e607cd06c4..76b8f9e7c2e1 100644
--- a/drivers/crypto/atmel-ecc.c
+++ b/drivers/crypto/atmel-ecc.c
@@ -212,7 +212,7 @@ static int atmel_ecdh_init_tfm(struct crypto_kpp *tfm)
struct atmel_ecdh_ctx *ctx = kpp_tfm_ctx(tfm);
ctx->curve_id = ECC_CURVE_NIST_P256;
- ctx->client = atmel_i2c_client_alloc();
+ ctx->client = atmel_i2c_client_alloc(ATMEL_CAP_ECDH);
if (IS_ERR(ctx->client)) {
pr_err("tfm - i2c_client binding failed\n");
return PTR_ERR(ctx->client);
@@ -287,6 +287,8 @@ static int atmel_ecc_probe(struct i2c_client *client)
i2c_priv = i2c_get_clientdata(client);
i2c_priv->ready = false;
+ i2c_priv->caps = BIT(ATMEL_CAP_ECDH);
+
spin_lock(&atmel_i2c_mgmt.i2c_list_lock);
list_add_tail(&i2c_priv->i2c_client_list_node,
&atmel_i2c_mgmt.i2c_client_list);
diff --git a/drivers/crypto/atmel-i2c.c b/drivers/crypto/atmel-i2c.c
index 92d3e28f9d9a..4953b8fcb02d 100644
--- a/drivers/crypto/atmel-i2c.c
+++ b/drivers/crypto/atmel-i2c.c
@@ -57,7 +57,7 @@ static void atmel_i2c_checksum(struct atmel_i2c_cmd *cmd)
*__crc16 = cpu_to_le16(bitrev16(crc16(0, data, len)));
}
-struct i2c_client *atmel_i2c_client_alloc(void)
+struct i2c_client *atmel_i2c_client_alloc(enum atmel_i2c_capability cap)
{
struct atmel_i2c_client_priv *i2c_priv, *min_i2c_priv = NULL;
struct i2c_client *client = ERR_PTR(-ENODEV);
@@ -75,6 +75,10 @@ struct i2c_client *atmel_i2c_client_alloc(void)
i2c_client_list_node) {
if (!i2c_priv->ready)
continue;
+
+ if (!(i2c_priv->caps & BIT(cap)))
+ continue;
+
tfm_cnt = atomic_read(&i2c_priv->tfm_count);
if (tfm_cnt < min_tfm_cnt) {
min_tfm_cnt = tfm_cnt;
diff --git a/drivers/crypto/atmel-i2c.h b/drivers/crypto/atmel-i2c.h
index ddab80bc1a72..af2e49332ab6 100644
--- a/drivers/crypto/atmel-i2c.h
+++ b/drivers/crypto/atmel-i2c.h
@@ -115,6 +115,10 @@ struct atmel_i2c_cmd {
#define ECDH_PREFIX_MODE 0x00
/* Used for binding tfm objects to i2c clients. */
+enum atmel_i2c_capability {
+ ATMEL_CAP_ECDH = 0,
+};
+
struct atmel_i2c_client_mgmt {
struct list_head i2c_client_list;
spinlock_t i2c_list_lock;
@@ -131,6 +135,7 @@ extern struct atmel_i2c_client_mgmt atmel_i2c_mgmt;
* @tfm_count : number of active crypto transformations on i2c client
* @hwrng : hold the hardware generated rng
* @ready : hw client is ready to use
+ * @caps : feature capability of the particular driver
*
* Reads and writes from/to the i2c client are sequential. The first byte
* transmitted to the device is treated as the byte size. Any attempt to send
@@ -149,6 +154,7 @@ struct atmel_i2c_client_priv {
struct hwrng hwrng;
struct completion remove_done;
bool ready;
+ u32 caps;
};
/**
@@ -193,7 +199,7 @@ void atmel_i2c_init_genkey_cmd(struct atmel_i2c_cmd *cmd, u16 keyid);
int atmel_i2c_init_ecdh_cmd(struct atmel_i2c_cmd *cmd,
struct scatterlist *pubkey);
-struct i2c_client *atmel_i2c_client_alloc(void);
+struct i2c_client *atmel_i2c_client_alloc(enum atmel_i2c_capability cap);
void atmel_i2c_client_free(struct i2c_client *client);
void atmel_i2c_deactivate_client(struct atmel_i2c_client_priv *i2c_priv);
diff --git a/drivers/crypto/atmel-sha204a.c b/drivers/crypto/atmel-sha204a.c
index 33e5a66b843c..0c5b5cdbfcbc 100644
--- a/drivers/crypto/atmel-sha204a.c
+++ b/drivers/crypto/atmel-sha204a.c
@@ -178,6 +178,8 @@ static int atmel_sha204a_probe(struct i2c_client *client)
i2c_priv = i2c_get_clientdata(client);
+ i2c_priv->caps = 0;
+
memset(&i2c_priv->hwrng, 0, sizeof(i2c_priv->hwrng));
i2c_priv->hwrng.name = dev_name(&client->dev);
--
2.39.5
^ permalink raw reply related
* [PATCH v4 11/12] crypto: atmel-sha204a - integrate into core management tracking
From: Lothar Rubusch @ 2026-05-22 23:01 UTC (permalink / raw)
To: thorsten.blum, herbert, davem, nicolas.ferre, alexandre.belloni,
claudiu.beznea, tudor.ambarus, ardb, linusw, krzk+dt
Cc: linux-crypto, linux-arm-kernel, linux-kernel, l.rubusch
In-Reply-To: <20260522230134.32414-1-l.rubusch@gmail.com>
Register the SHA204A I2C device instance into the shared atmel_i2c client
management tracking list during the probe phase. This allows the driver to
participate in the central hardware selection infrastructure.
Rework the error-unwind paths inside atmel_sha204a_probe() to prevent stale
entries from remaining in the global tracking structures if a partial
initialization failure occurs. If sysfs group creation fails, explicitly
trigger devm_hwrng_unregister() to preserve the strict lifecycle ordering
introduced in previous stability fixes.
Convert the removal path to use the core teardown helpers. Ensure the
device readiness state is deactivated using atmel_i2c_deactivate_client()
and the tracking node is removed via atmel_i2c_unregister_client() before
local memory resources are freed. This guarantees that any in-flight work
queue items are unconditionally flushed, eliminating a potential
Use-After-Free (UAF) window during device removal.
No functional change intended beyond improved lifecycle handling.
Signed-off-by: Lothar Rubusch <l.rubusch@gmail.com>
---
drivers/crypto/atmel-sha204a.c | 26 ++++++++++++++++++++++----
1 file changed, 22 insertions(+), 4 deletions(-)
diff --git a/drivers/crypto/atmel-sha204a.c b/drivers/crypto/atmel-sha204a.c
index 0c5b5cdbfcbc..86a68f2a27e0 100644
--- a/drivers/crypto/atmel-sha204a.c
+++ b/drivers/crypto/atmel-sha204a.c
@@ -177,9 +177,15 @@ static int atmel_sha204a_probe(struct i2c_client *client)
return ret;
i2c_priv = i2c_get_clientdata(client);
+ i2c_priv->ready = false;
i2c_priv->caps = 0;
+ spin_lock(&atmel_i2c_mgmt.i2c_list_lock);
+ list_add_tail(&i2c_priv->i2c_client_list_node,
+ &atmel_i2c_mgmt.i2c_client_list);
+ spin_unlock(&atmel_i2c_mgmt.i2c_list_lock);
+
memset(&i2c_priv->hwrng, 0, sizeof(i2c_priv->hwrng));
i2c_priv->hwrng.name = dev_name(&client->dev);
@@ -192,15 +198,26 @@ static int atmel_sha204a_probe(struct i2c_client *client)
ret = devm_hwrng_register(&client->dev, &i2c_priv->hwrng);
if (ret) {
dev_err(&client->dev, "failed to register RNG (%d)\n", ret);
- return ret;
+ goto err_list_del;
}
ret = sysfs_create_group(&client->dev.kobj, &atmel_sha204a_groups);
if (ret) {
dev_err(&client->dev, "failed to create sysfs group (%d)\n", ret);
- return ret;
+ goto err_hwrng_unregister;
}
+ spin_lock(&atmel_i2c_mgmt.i2c_list_lock);
+ i2c_priv->ready = true;
+ spin_unlock(&atmel_i2c_mgmt.i2c_list_lock);
+
+ return 0;
+
+err_hwrng_unregister:
+ devm_hwrng_unregister(&client->dev, &i2c_priv->hwrng);
+err_list_del:
+ atmel_i2c_unregister_client(i2c_priv);
+
return ret;
}
@@ -208,9 +225,11 @@ static void atmel_sha204a_remove(struct i2c_client *client)
{
struct atmel_i2c_client_priv *i2c_priv = i2c_get_clientdata(client);
+ atmel_i2c_deactivate_client(i2c_priv);
+
sysfs_remove_group(&client->dev.kobj, &atmel_sha204a_groups);
devm_hwrng_unregister(&client->dev, &i2c_priv->hwrng);
- atmel_i2c_flush_queue();
+ atmel_i2c_unregister_client(i2c_priv);
kfree((void *)i2c_priv->hwrng.priv);
}
@@ -245,7 +264,6 @@ static int __init atmel_sha204a_init(void)
static void __exit atmel_sha204a_exit(void)
{
- atmel_i2c_flush_queue();
i2c_del_driver(&atmel_sha204a_driver);
}
--
2.39.5
^ permalink raw reply related
* [PATCH v4 08/12] crypto: atmel-ecc - switch to module_i2c_driver
From: Lothar Rubusch @ 2026-05-22 23:01 UTC (permalink / raw)
To: thorsten.blum, herbert, davem, nicolas.ferre, alexandre.belloni,
claudiu.beznea, tudor.ambarus, ardb, linusw, krzk+dt
Cc: linux-crypto, linux-arm-kernel, linux-kernel, l.rubusch
In-Reply-To: <20260522230134.32414-1-l.rubusch@gmail.com>
Remove custom boilerplate module configuration code and convert the module
init/exit paths to use the modern module_i2c_driver() helper macro.
This shortens and simplifies driver initialization. Custom structure setup
is no longer required here since management tracking context initialization
was already safely moved into the atmel-i2c core library module.
Signed-off-by: Lothar Rubusch <l.rubusch@gmail.com>
---
drivers/crypto/atmel-ecc.c | 13 +------------
1 file changed, 1 insertion(+), 12 deletions(-)
diff --git a/drivers/crypto/atmel-ecc.c b/drivers/crypto/atmel-ecc.c
index e6d3e6574251..d2490693a198 100644
--- a/drivers/crypto/atmel-ecc.c
+++ b/drivers/crypto/atmel-ecc.c
@@ -442,18 +442,7 @@ static struct i2c_driver atmel_ecc_driver = {
.id_table = atmel_ecc_id,
};
-static int __init atmel_ecc_init(void)
-{
- return i2c_add_driver(&atmel_ecc_driver);
-}
-
-static void __exit atmel_ecc_exit(void)
-{
- i2c_del_driver(&atmel_ecc_driver);
-}
-
-module_init(atmel_ecc_init);
-module_exit(atmel_ecc_exit);
+module_i2c_driver(atmel_ecc_driver);
MODULE_AUTHOR("Tudor Ambarus");
MODULE_DESCRIPTION("Microchip / Atmel ECC (I2C) driver");
--
2.39.5
^ permalink raw reply related
* [PATCH v4 04/12] crypto: atmel-ecc - rename driver_data before moving it into atmel-i2c
From: Lothar Rubusch @ 2026-05-22 23:01 UTC (permalink / raw)
To: thorsten.blum, herbert, davem, nicolas.ferre, alexandre.belloni,
claudiu.beznea, tudor.ambarus, ardb, linusw, krzk+dt
Cc: linux-crypto, linux-arm-kernel, linux-kernel, l.rubusch
In-Reply-To: <20260522230134.32414-1-l.rubusch@gmail.com>
Rename the local driver_data instance to atmel_i2c_mgmt in
preparation for moving the shared I2C client management
infrastructure into the atmel-i2c core driver in a subsequent
change.
No functional changes intended.
Signed-off-by: Lothar Rubusch <l.rubusch@gmail.com>
---
drivers/crypto/atmel-ecc.c | 39 +++++++++++++++++++-------------------
1 file changed, 19 insertions(+), 20 deletions(-)
diff --git a/drivers/crypto/atmel-ecc.c b/drivers/crypto/atmel-ecc.c
index 005a9a3d919c..d12a9dbe45a7 100644
--- a/drivers/crypto/atmel-ecc.c
+++ b/drivers/crypto/atmel-ecc.c
@@ -28,7 +28,7 @@ static int atmel_ecc_kpp_refcnt;
DECLARE_COMPLETION(atmel_ecc_unreg_done);
static bool atmel_ecc_unreg_active;
-static struct atmel_ecc_driver_data driver_data;
+static struct atmel_ecc_driver_data atmel_i2c_mgmt;
/**
* struct atmel_ecdh_ctx - transformation context
@@ -214,14 +214,14 @@ static struct i2c_client *atmel_ecc_i2c_client_alloc(void)
int min_tfm_cnt = INT_MAX;
int tfm_cnt;
- spin_lock(&driver_data.i2c_list_lock);
+ spin_lock(&atmel_i2c_mgmt.i2c_list_lock);
- if (list_empty(&driver_data.i2c_client_list)) {
- spin_unlock(&driver_data.i2c_list_lock);
+ if (list_empty(&atmel_i2c_mgmt.i2c_client_list)) {
+ spin_unlock(&atmel_i2c_mgmt.i2c_list_lock);
return ERR_PTR(-ENODEV);
}
- list_for_each_entry(i2c_priv, &driver_data.i2c_client_list,
+ list_for_each_entry(i2c_priv, &atmel_i2c_mgmt.i2c_client_list,
i2c_client_list_node) {
if (!i2c_priv->ready)
continue;
@@ -239,7 +239,7 @@ static struct i2c_client *atmel_ecc_i2c_client_alloc(void)
client = min_i2c_priv->client;
}
- spin_unlock(&driver_data.i2c_list_lock);
+ spin_unlock(&atmel_i2c_mgmt.i2c_list_lock);
return client;
}
@@ -334,11 +334,11 @@ static int atmel_ecc_probe(struct i2c_client *client)
i2c_priv = i2c_get_clientdata(client);
i2c_priv->ready = false;
- spin_lock(&driver_data.i2c_list_lock);
+ spin_lock(&atmel_i2c_mgmt.i2c_list_lock);
list_add_tail(&i2c_priv->i2c_client_list_node,
- &driver_data.i2c_client_list);
+ &atmel_i2c_mgmt.i2c_client_list);
i2c_priv->ready = true;
- spin_unlock(&driver_data.i2c_list_lock);
+ spin_unlock(&atmel_i2c_mgmt.i2c_list_lock);
mutex_lock(&atmel_ecc_kpp_lock);
/*
@@ -352,12 +352,11 @@ static int atmel_ecc_probe(struct i2c_client *client)
timeout = wait_for_completion_timeout(&atmel_ecc_unreg_done,
msecs_to_jiffies(2000));
mutex_lock(&atmel_ecc_kpp_lock);
-
if (timeout == 0) {
- spin_lock(&driver_data.i2c_list_lock);
+ spin_lock(&atmel_i2c_mgmt.i2c_list_lock);
i2c_priv->ready = false;
list_del(&i2c_priv->i2c_client_list_node);
- spin_unlock(&driver_data.i2c_list_lock);
+ spin_unlock(&atmel_i2c_mgmt.i2c_list_lock);
mutex_unlock(&atmel_ecc_kpp_lock);
dev_err(&client->dev, "probe timed out, former driver instance not fully deregistered\n");
@@ -368,10 +367,10 @@ static int atmel_ecc_probe(struct i2c_client *client)
if (atmel_ecc_kpp_refcnt == 0) {
ret = crypto_register_kpp(&atmel_ecdh_nist_p256);
if (ret) {
- spin_lock(&driver_data.i2c_list_lock);
+ spin_lock(&atmel_i2c_mgmt.i2c_list_lock);
i2c_priv->ready = false;
list_del(&i2c_priv->i2c_client_list_node);
- spin_unlock(&driver_data.i2c_list_lock);
+ spin_unlock(&atmel_i2c_mgmt.i2c_list_lock);
mutex_unlock(&atmel_ecc_kpp_lock);
dev_err(&client->dev, "%s alg registration failed\n",
@@ -391,9 +390,9 @@ static void atmel_ecc_remove(struct i2c_client *client)
struct atmel_i2c_client_priv *i2c_priv = i2c_get_clientdata(client);
bool trigger_unreg = false;
- spin_lock(&driver_data.i2c_list_lock);
+ spin_lock(&atmel_i2c_mgmt.i2c_list_lock);
i2c_priv->ready = false;
- spin_unlock(&driver_data.i2c_list_lock);
+ spin_unlock(&atmel_i2c_mgmt.i2c_list_lock);
/*
* The Linux crypto core automatically blocks until all active
@@ -413,9 +412,9 @@ static void atmel_ecc_remove(struct i2c_client *client)
if (atomic_read(&i2c_priv->tfm_count))
wait_for_completion(&i2c_priv->remove_done);
- spin_lock(&driver_data.i2c_list_lock);
+ spin_lock(&atmel_i2c_mgmt.i2c_list_lock);
list_del(&i2c_priv->i2c_client_list_node);
- spin_unlock(&driver_data.i2c_list_lock);
+ spin_unlock(&atmel_i2c_mgmt.i2c_list_lock);
/*
* The driver registers once an algorithm, but maintains a list of
@@ -459,8 +458,8 @@ static struct i2c_driver atmel_ecc_driver = {
static int __init atmel_ecc_init(void)
{
- spin_lock_init(&driver_data.i2c_list_lock);
- INIT_LIST_HEAD(&driver_data.i2c_client_list);
+ spin_lock_init(&atmel_i2c_mgmt.i2c_list_lock);
+ INIT_LIST_HEAD(&atmel_i2c_mgmt.i2c_client_list);
return i2c_add_driver(&atmel_ecc_driver);
}
--
2.39.5
^ permalink raw reply related
* [PATCH v4 12/12] crypto: atmel-sha204a - switch to module_i2c_driver
From: Lothar Rubusch @ 2026-05-22 23:01 UTC (permalink / raw)
To: thorsten.blum, herbert, davem, nicolas.ferre, alexandre.belloni,
claudiu.beznea, tudor.ambarus, ardb, linusw, krzk+dt
Cc: linux-crypto, linux-arm-kernel, linux-kernel, l.rubusch
In-Reply-To: <20260522230134.32414-1-l.rubusch@gmail.com>
Replace explicit module init and exit boilerplate functions with the
module_i2c_driver() macro helper to simplify the driver registration
path.
Signed-off-by: Lothar Rubusch <l.rubusch@gmail.com>
---
drivers/crypto/atmel-sha204a.c | 13 +------------
1 file changed, 1 insertion(+), 12 deletions(-)
diff --git a/drivers/crypto/atmel-sha204a.c b/drivers/crypto/atmel-sha204a.c
index 86a68f2a27e0..74f91e176713 100644
--- a/drivers/crypto/atmel-sha204a.c
+++ b/drivers/crypto/atmel-sha204a.c
@@ -257,18 +257,7 @@ static struct i2c_driver atmel_sha204a_driver = {
.driver.of_match_table = atmel_sha204a_dt_ids,
};
-static int __init atmel_sha204a_init(void)
-{
- return i2c_add_driver(&atmel_sha204a_driver);
-}
-
-static void __exit atmel_sha204a_exit(void)
-{
- i2c_del_driver(&atmel_sha204a_driver);
-}
-
-module_init(atmel_sha204a_init);
-module_exit(atmel_sha204a_exit);
+module_i2c_driver(atmel_sha204a_driver);
MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
MODULE_DESCRIPTION("Microchip / Atmel SHA204A (I2C) driver");
--
2.39.5
^ permalink raw reply related
* [PATCH v4 02/12] crypto: atmel-ecc - fix multi-device kpp registration
From: Lothar Rubusch @ 2026-05-22 23:01 UTC (permalink / raw)
To: thorsten.blum, herbert, davem, nicolas.ferre, alexandre.belloni,
claudiu.beznea, tudor.ambarus, ardb, linusw, krzk+dt
Cc: linux-crypto, linux-arm-kernel, linux-kernel, l.rubusch
In-Reply-To: <20260522230134.32414-1-l.rubusch@gmail.com>
When multiple atmel-ecc hardware accelerator chips are attached to the
same host, registering the same static kpp_alg structure multiple times
corrupts internal fields used by the crypto core's algorithm list. This
leads to immediate list corruption or kernel panics.
Additionally, removing an individual device via sysfs while active crypto
transformations (TFMs) are running triggers a use-after-free (UAF) bug.
Because the device driver core lacks unbind error-handling paths, the
underlying memory allocated via devm for the i2c_priv structure is freed
unconditionally, leaving active transformation context pointers dangling.
Fix these problems by implementing a centralized subsystem tracking matrix:
1. Introduce a global subsystem mutex and reference counter to ensure
that the static 'atmel_ecdh_nist_p256' structure is only registered by
the first probing device, and unregistered exclusively when the last
supporting device unbinds.
2. Maintain per-device allocation tracking with 'tfm_count'. On remove,
mark the device unready to halt load-balancing assignments, and block
via a completion barrier until all pending transformation contexts bound
to that specific physical hardware are freed.
3. Fix a critical re-registration race where a high-velocity unbind and
subsequent re-probe cycles occur while crypto core asynchronous users
are still purging. Establish a global 'atmel_ecc_unreg_active' state
fence to force concurrent probing threads to execute a 2-second timeout
bounded wait_for_completion_timeout() rather than unsafely mutating
static lists.
Fixes: 11105693fa05 ("crypto: atmel-ecc - introduce Microchip / Atmel ECC driver")
Signed-off-by: Lothar Rubusch <l.rubusch@gmail.com>
---
drivers/crypto/atmel-ecc.c | 107 ++++++++++++++++++++++++++++---------
drivers/crypto/atmel-i2c.h | 1 +
2 files changed, 82 insertions(+), 26 deletions(-)
diff --git a/drivers/crypto/atmel-ecc.c b/drivers/crypto/atmel-ecc.c
index 94360d29f9f9..005a9a3d919c 100644
--- a/drivers/crypto/atmel-ecc.c
+++ b/drivers/crypto/atmel-ecc.c
@@ -23,6 +23,11 @@
#include <crypto/kpp.h>
#include "atmel-i2c.h"
+static DEFINE_MUTEX(atmel_ecc_kpp_lock);
+static int atmel_ecc_kpp_refcnt;
+DECLARE_COMPLETION(atmel_ecc_unreg_done);
+static bool atmel_ecc_unreg_active;
+
static struct atmel_ecc_driver_data driver_data;
/**
@@ -243,7 +248,8 @@ static void atmel_ecc_i2c_client_free(struct i2c_client *client)
{
struct atmel_i2c_client_priv *i2c_priv = i2c_get_clientdata(client);
- atomic_dec(&i2c_priv->tfm_count);
+ if (atomic_dec_and_test(&i2c_priv->tfm_count))
+ complete(&i2c_priv->remove_done);
}
static int atmel_ecdh_init_tfm(struct crypto_kpp *tfm)
@@ -278,7 +284,8 @@ static void atmel_ecdh_exit_tfm(struct crypto_kpp *tfm)
struct atmel_ecdh_ctx *ctx = kpp_tfm_ctx(tfm);
kfree(ctx->public_key);
- crypto_free_kpp(ctx->fallback);
+ if (ctx->fallback)
+ crypto_free_kpp(ctx->fallback);
atmel_ecc_i2c_client_free(ctx->client);
}
@@ -317,6 +324,7 @@ static struct kpp_alg atmel_ecdh_nist_p256 = {
static int atmel_ecc_probe(struct i2c_client *client)
{
struct atmel_i2c_client_priv *i2c_priv;
+ unsigned long timeout;
int ret;
ret = atmel_i2c_probe(client);
@@ -332,50 +340,97 @@ static int atmel_ecc_probe(struct i2c_client *client)
i2c_priv->ready = true;
spin_unlock(&driver_data.i2c_list_lock);
- ret = crypto_register_kpp(&atmel_ecdh_nist_p256);
- if (ret) {
- spin_lock(&driver_data.i2c_list_lock);
- i2c_priv->ready = false;
- list_del(&i2c_priv->i2c_client_list_node);
- spin_unlock(&driver_data.i2c_list_lock);
+ mutex_lock(&atmel_ecc_kpp_lock);
+ /*
+ * For cases where the same/last such device is still in unregistering,
+ * and now re-registering (refcnt is 0, but completion still exists).
+ * Safely capture the pointer, drop the lock and sleep until it
+ * terminates upon completion or retry limit reached.
+ */
+ while (atmel_ecc_unreg_active) {
+ mutex_unlock(&atmel_ecc_kpp_lock);
+ timeout = wait_for_completion_timeout(&atmel_ecc_unreg_done,
+ msecs_to_jiffies(2000));
+ mutex_lock(&atmel_ecc_kpp_lock);
+
+ if (timeout == 0) {
+ spin_lock(&driver_data.i2c_list_lock);
+ i2c_priv->ready = false;
+ list_del(&i2c_priv->i2c_client_list_node);
+ spin_unlock(&driver_data.i2c_list_lock);
+ mutex_unlock(&atmel_ecc_kpp_lock);
+
+ dev_err(&client->dev, "probe timed out, former driver instance not fully deregistered\n");
+ return -ETIMEDOUT;
+ }
+ }
- dev_err(&client->dev, "%s alg registration failed\n",
- atmel_ecdh_nist_p256.base.cra_driver_name);
- return ret;
- } else {
- dev_info(&client->dev, "atmel ecc algorithms registered in /proc/crypto\n");
+ if (atmel_ecc_kpp_refcnt == 0) {
+ ret = crypto_register_kpp(&atmel_ecdh_nist_p256);
+ if (ret) {
+ spin_lock(&driver_data.i2c_list_lock);
+ i2c_priv->ready = false;
+ list_del(&i2c_priv->i2c_client_list_node);
+ spin_unlock(&driver_data.i2c_list_lock);
+ mutex_unlock(&atmel_ecc_kpp_lock);
+
+ dev_err(&client->dev, "%s alg registration failed\n",
+ atmel_ecdh_nist_p256.base.cra_driver_name);
+ return ret;
+ }
}
+ atmel_ecc_kpp_refcnt++;
+ mutex_unlock(&atmel_ecc_kpp_lock);
+ dev_info(&client->dev, "atmel ecc algorithms registered in /proc/crypto\n");
return ret;
}
static void atmel_ecc_remove(struct i2c_client *client)
{
struct atmel_i2c_client_priv *i2c_priv = i2c_get_clientdata(client);
+ bool trigger_unreg = false;
spin_lock(&driver_data.i2c_list_lock);
i2c_priv->ready = false;
spin_unlock(&driver_data.i2c_list_lock);
- /* Return EBUSY if i2c client already allocated. */
- if (atomic_read(&i2c_priv->tfm_count)) {
- /*
- * After we return here, the memory backing the device is freed.
- * That happens no matter what the return value of this function
- * is because in the Linux device model there is no error
- * handling for unbinding a driver.
- * If there is still some action pending, it probably involves
- * accessing the freed memory.
- */
- dev_emerg(&client->dev, "Device is busy, expect memory corruption.\n");
- return;
+ /*
+ * The Linux crypto core automatically blocks until all active
+ * transformations utilizing that specific algorithm structure
+ * are fully freed and closed.
+ */
+ mutex_lock(&atmel_ecc_kpp_lock);
+ atmel_ecc_kpp_refcnt--;
+
+ if (atmel_ecc_kpp_refcnt == 0) {
+ trigger_unreg = true;
+ atmel_ecc_unreg_active = true;
+ reinit_completion(&atmel_ecc_unreg_done);
}
+ mutex_unlock(&atmel_ecc_kpp_lock);
- crypto_unregister_kpp(&atmel_ecdh_nist_p256);
+ if (atomic_read(&i2c_priv->tfm_count))
+ wait_for_completion(&i2c_priv->remove_done);
spin_lock(&driver_data.i2c_list_lock);
list_del(&i2c_priv->i2c_client_list_node);
spin_unlock(&driver_data.i2c_list_lock);
+
+ /*
+ * The driver registers once an algorithm, but maintains a list of
+ * supporting i2c devices. Unregister the algorithm only, when the last
+ * supporting device deregisters. Use completions to assure no inflight
+ * TFMs and/or re-registering driver probe will then loose memory
+ * by over initializing the global statics.
+ */
+ if (trigger_unreg) {
+ crypto_unregister_kpp(&atmel_ecdh_nist_p256);
+ mutex_lock(&atmel_ecc_kpp_lock);
+ atmel_ecc_unreg_active = false;
+ complete_all(&atmel_ecc_unreg_done);
+ mutex_unlock(&atmel_ecc_kpp_lock);
+ }
}
static const struct of_device_id atmel_ecc_dt_ids[] = {
diff --git a/drivers/crypto/atmel-i2c.h b/drivers/crypto/atmel-i2c.h
index e3b12030f9c4..b320559e50eb 100644
--- a/drivers/crypto/atmel-i2c.h
+++ b/drivers/crypto/atmel-i2c.h
@@ -146,6 +146,7 @@ struct atmel_i2c_client_priv {
size_t wake_token_sz;
atomic_t tfm_count ____cacheline_aligned;
struct hwrng hwrng;
+ struct completion remove_done;
bool ready;
};
--
2.39.5
^ permalink raw reply related
* [PATCH v4 03/12] crypto: atmel-sha204a - fix heap info leak on I2C transfer failure
From: Lothar Rubusch @ 2026-05-22 23:01 UTC (permalink / raw)
To: thorsten.blum, herbert, davem, nicolas.ferre, alexandre.belloni,
claudiu.beznea, tudor.ambarus, ardb, linusw, krzk+dt
Cc: linux-crypto, linux-arm-kernel, linux-kernel, l.rubusch
In-Reply-To: <20260522230134.32414-1-l.rubusch@gmail.com>
When a non-blocking read operation is requested, the driver dynamically
allocates memory to track asynchronous transfer status. If the underlying
I2C transmission fails, atmel_sha204a_rng_done() logs a rate-limited
warning but incorrectly proceeds to cache the pointer to this uninitialized
buffer inside the rng->priv data field anyway.
On subsequent execution passes, atmel_sha204a_rng_read_nonblocking()
detects the stale rng->priv value, skips executing a hardware data read,
and copies up to 32 bytes of uninitialized kernel heap data from this
garbage memory pool straight back into the system's hwrng data stream.
Fix this information disclosure vector by immediately releasing the
allocated asynchronous work data buffer and explicitly clearing the
tracking pointer context whenever an I2C transaction returns a non-zero
error status.
Additionally, duplicate the tfm counter decrement within the new error
path to ensure the reference counter is properly released before executing
the early return, maintaining the driver's availability for subsequent
requests.
Fixes: da001fb651b0 ("crypto: atmel-i2c - add support for SHA204A random number generator")
Signed-off-by: Lothar Rubusch <l.rubusch@gmail.com>
---
drivers/crypto/atmel-sha204a.c | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)
diff --git a/drivers/crypto/atmel-sha204a.c b/drivers/crypto/atmel-sha204a.c
index 12eb85b57380..33e5a66b843c 100644
--- a/drivers/crypto/atmel-sha204a.c
+++ b/drivers/crypto/atmel-sha204a.c
@@ -31,10 +31,15 @@ static void atmel_sha204a_rng_done(struct atmel_i2c_work_data *work_data,
struct atmel_i2c_client_priv *i2c_priv = work_data->ctx;
struct hwrng *rng = areq;
- if (status)
+ if (status) {
dev_warn_ratelimited(&i2c_priv->client->dev,
"i2c transaction failed (%d)\n",
status);
+ kfree(work_data);
+ rng->priv = 0;
+ atomic_dec(&i2c_priv->tfm_count);
+ return;
+ }
rng->priv = (unsigned long)work_data;
atomic_dec(&i2c_priv->tfm_count);
--
2.39.5
^ permalink raw reply related
* [PATCH v4 01/12] crypto: atmel-ecc - fix use after free situation
From: Lothar Rubusch @ 2026-05-22 23:01 UTC (permalink / raw)
To: thorsten.blum, herbert, davem, nicolas.ferre, alexandre.belloni,
claudiu.beznea, tudor.ambarus, ardb, linusw, krzk+dt
Cc: linux-crypto, linux-arm-kernel, linux-kernel, l.rubusch
In-Reply-To: <20260522230134.32414-1-l.rubusch@gmail.com>
Fixes a possible race condition, when having multiple of such devices
attached (identified by sashiko feedback).
The Scenario:
Thread A (Device 1 Probe): Successfully adds i2c_priv to the global
list (Line 324). The lock is released.
Thread B (An active crypto request): Concurrently calls
atmel_ecc_i2c_client_alloc(). It scans the global list, sees
Device 1, and assigns a crypto job to it.
Thread A: Moves to line 332. crypto_register_kpp() fails (e.g., out of
memory or name clash).
Thread A: Enters the error path. It removes Device 1 from the list and
frees the i2c_priv memory.
Thread B: Is still actively trying to talk to the I2C hardware using
the i2c_priv pointer it grabbed in Step 2. The memory is now
gone. Result: Kernel crash (Use-After-Free).
Fixes: 11105693fa05 ("crypto: atmel-ecc - introduce Microchip / Atmel ECC driver")
Signed-off-by: Lothar Rubusch <l.rubusch@gmail.com>
---
drivers/crypto/atmel-ecc.c | 10 ++++++++++
drivers/crypto/atmel-i2c.h | 2 ++
2 files changed, 12 insertions(+)
diff --git a/drivers/crypto/atmel-ecc.c b/drivers/crypto/atmel-ecc.c
index 9660f6426a84..94360d29f9f9 100644
--- a/drivers/crypto/atmel-ecc.c
+++ b/drivers/crypto/atmel-ecc.c
@@ -218,6 +218,8 @@ static struct i2c_client *atmel_ecc_i2c_client_alloc(void)
list_for_each_entry(i2c_priv, &driver_data.i2c_client_list,
i2c_client_list_node) {
+ if (!i2c_priv->ready)
+ continue;
tfm_cnt = atomic_read(&i2c_priv->tfm_count);
if (tfm_cnt < min_tfm_cnt) {
min_tfm_cnt = tfm_cnt;
@@ -322,20 +324,24 @@ static int atmel_ecc_probe(struct i2c_client *client)
return ret;
i2c_priv = i2c_get_clientdata(client);
+ i2c_priv->ready = false;
spin_lock(&driver_data.i2c_list_lock);
list_add_tail(&i2c_priv->i2c_client_list_node,
&driver_data.i2c_client_list);
+ i2c_priv->ready = true;
spin_unlock(&driver_data.i2c_list_lock);
ret = crypto_register_kpp(&atmel_ecdh_nist_p256);
if (ret) {
spin_lock(&driver_data.i2c_list_lock);
+ i2c_priv->ready = false;
list_del(&i2c_priv->i2c_client_list_node);
spin_unlock(&driver_data.i2c_list_lock);
dev_err(&client->dev, "%s alg registration failed\n",
atmel_ecdh_nist_p256.base.cra_driver_name);
+ return ret;
} else {
dev_info(&client->dev, "atmel ecc algorithms registered in /proc/crypto\n");
}
@@ -347,6 +353,10 @@ static void atmel_ecc_remove(struct i2c_client *client)
{
struct atmel_i2c_client_priv *i2c_priv = i2c_get_clientdata(client);
+ spin_lock(&driver_data.i2c_list_lock);
+ i2c_priv->ready = false;
+ spin_unlock(&driver_data.i2c_list_lock);
+
/* Return EBUSY if i2c client already allocated. */
if (atomic_read(&i2c_priv->tfm_count)) {
/*
diff --git a/drivers/crypto/atmel-i2c.h b/drivers/crypto/atmel-i2c.h
index 72f04c15682f..e3b12030f9c4 100644
--- a/drivers/crypto/atmel-i2c.h
+++ b/drivers/crypto/atmel-i2c.h
@@ -129,6 +129,7 @@ struct atmel_ecc_driver_data {
* @wake_token_sz : size in bytes of the wake_token
* @tfm_count : number of active crypto transformations on i2c client
* @hwrng : hold the hardware generated rng
+ * @ready : hw client is ready to use
*
* Reads and writes from/to the i2c client are sequential. The first byte
* transmitted to the device is treated as the byte size. Any attempt to send
@@ -145,6 +146,7 @@ struct atmel_i2c_client_priv {
size_t wake_token_sz;
atomic_t tfm_count ____cacheline_aligned;
struct hwrng hwrng;
+ bool ready;
};
/**
--
2.39.5
^ permalink raw reply related
* [PATCH v4 00/12] crypto: atmel - introduce shared i2c core client management and capability-based selection framework
From: Lothar Rubusch @ 2026-05-22 23:01 UTC (permalink / raw)
To: thorsten.blum, herbert, davem, nicolas.ferre, alexandre.belloni,
claudiu.beznea, tudor.ambarus, ardb, linusw, krzk+dt
Cc: linux-crypto, linux-arm-kernel, linux-kernel, l.rubusch
This patch series introduces a staged refactoring of the Atmel crypto I2C
drivers in preparation for a shared core-based architecture. The goal is to
consolidate I2C client management and selection logic into a common
atmel-i2c core driver while keeping ECC (ECDH) and SHA204A client drivers
functionally separate but interoperating through shared infrastructure.
The series moves existing ECC-specific client tracking into a shared
management structure, relocates allocation and selection logic, and
introduces capability-based filtering for hardware selection. This allows
individual crypto drivers to request hardware clients based on supported
features while still benefiting from a unified least-loaded selection
strategy.
Subsequent patches extend this base by:
- migrating client management fully into the core driver,
- introducing explicit capability advertisement by each hardware client,
- updating ECC and SHA204A drivers to participate in capability-aware allocation,
- and cleaning up probe/remove paths to ensure consistent lifecycle handling.
No functional behavioral changes are intended at this stage beyond internal
refactoring and preparation for future feature expansion. The series is
designed to preserve existing crypto functionality while gradually
centralizing shared logic in the atmel-i2c core layer, reducing duplication
and improving maintainability across all Atmel crypto drivers.
Note, this series is a more elaborated part of a bigger refactoring,
sketched out here:
https://sashiko.dev/#/patchset/20260512224349.64621-1-l.rubusch%40gmail.com
Signed-off-by: Lothar Rubusch <l.rubusch@gmail.com>
---
v3 -> v4:
- sashiko feedback[3]
- fixes and patches reordered
- fix: cover a possible memory leak at deregistration and subsequent reregistration
[3] https://sashiko.dev/#/patchset/20260520155703.23018-1-l.rubusch%40gmail.com
v2 -> v3:
- sashiko feedback[2]
- ecc: reorder setting ready flag in probe()
- i2c: unconditionally call flush in unregister client function, to avoid UAF for multiple devices
- i2c: fixed a sleep-in-atomic bug by moving atmel_i2c_flush_queue() outside the spin_lock section
- sha204a: reorder calls in remove() avoid UAF flagged risk
- sha204a: rephrase commit messages
[2] https://sashiko.dev/#/patchset/20260519204803.17034-1-l.rubusch%40gmail.com
v1 -> v2:
- going over Sashiko feedback[1]
- rephrasing commit messages and titles
- fix: introduce a ready/state flag to address the UAF risk
- fix: add kpp lock and refcnt to impede overwriting global driver struct
- fix: explicitely clearing rng cached buffer in return branch
- unregistering ready state by dedicated function
- reorder Atmel ECC related things and atmel I2C at beginning
- reorder Atmel SHA204a related things behind introduction of cap
- patches dropped: NULL checks in remove functions
- changed to EXPORT_SYMBOL_GPL
- additionally to alloc hw client also migrate freeing it to core driver
[1] https://sashiko.dev/#/patchset/20260517180639.9657-1-l.rubusch%40gmail.com
---
Lothar Rubusch (12):
crypto: atmel-ecc - fix use after free situation
crypto: atmel-ecc - fix multi-device kpp registration
crypto: atmel-sha204a - fix heap info leak on I2C transfer failure
crypto: atmel-ecc - rename driver_data before moving it into atmel-i2c
crypto: atmel - rename atmel_ecc_driver_data to atmel_i2c_client_mgmt
crypto: atmel-i2c - move client management instance into core
crypto: atmel-i2c - introduce shared teardown helpers and fix queue
flush
crypto: atmel-ecc - switch to module_i2c_driver
crypto: atmel-i2c - move shared client allocation logic to core
crypto: atmel-i2c - implement capability-based client selection
crypto: atmel-sha204a - integrate into core management tracking
crypto: atmel-sha204a - switch to module_i2c_driver
drivers/crypto/atmel-ecc.c | 177 ++++++++++++++++-----------------
drivers/crypto/atmel-i2c.c | 77 ++++++++++++++
drivers/crypto/atmel-i2c.h | 18 +++-
drivers/crypto/atmel-sha204a.c | 48 +++++----
4 files changed, 210 insertions(+), 110 deletions(-)
base-commit: 49e05bb00f2e8168695f7af4d694c39e1423e8a2
--
2.39.5
^ permalink raw reply
* [PATCH v9 17/23] perf python: Add callchain support
From: Ian Rogers @ 2026-05-22 22:04 UTC (permalink / raw)
To: irogers, acme, namhyung
Cc: adrian.hunter, alice.mei.rogers, dapeng1.mi, james.clark, leo.yan,
linux-arm-kernel, linux-kernel, linux-perf-users, mingo, peterz,
tmricht
In-Reply-To: <20260522220435.2378363-1-irogers@google.com>
Implement pyrf_callchain_node and pyrf_callchain types for lazy
iteration over callchain frames. Add callchain property to
sample_event.
Assisted-by: Gemini:gemini-3.1-pro-preview
Signed-off-by: Ian Rogers <irogers@google.com>
---
v2:
1. Eager Callchain Resolution: Moved the callchain resolution from
deferred iteration to eager processing in
pyrf_session_tool__sample() . This avoids risks of reading from
unmapped memory or following dangling pointers to closed sessions.
2. Cached Callchain: Added a callchain field to struct pyrf_event to
store the resolved object.
3. Simplified Access: pyrf_sample_event__get_callchain() now just
returns the cached object if available.
4. Avoided Double Free: Handled lazy cleanups properly.
v6:
- Moved callchain resolution from `session_tool__sample` to
`pyrf_event__new`.
---
tools/perf/util/python.c | 241 ++++++++++++++++++++++++++++++++++++++-
1 file changed, 240 insertions(+), 1 deletion(-)
diff --git a/tools/perf/util/python.c b/tools/perf/util/python.c
index daedd67f12d5..de7a51389572 100644
--- a/tools/perf/util/python.c
+++ b/tools/perf/util/python.c
@@ -66,6 +66,8 @@ struct pyrf_event {
struct addr_location al;
/** @al_resolved: True when machine__resolve been called. */
bool al_resolved;
+ /** @callchain: Resolved callchain, eagerly computed if requested. */
+ PyObject *callchain;
/** @event: The underlying perf_event that may be in a file or ring buffer. */
union perf_event event;
};
@@ -103,6 +105,7 @@ static void pyrf_event__delete(struct pyrf_event *pevent)
{
if (pevent->al_resolved)
addr_location__exit(&pevent->al);
+ Py_XDECREF(pevent->callchain);
perf_sample__exit(&pevent->sample);
Py_TYPE(pevent)->tp_free((PyObject *)pevent);
}
@@ -669,6 +672,181 @@ static PyObject *pyrf_sample_event__insn(PyObject *self, PyObject *args __maybe_
pevent->sample.insn_len);
}
+struct pyrf_callchain_node {
+ PyObject_HEAD
+ u64 ip;
+ struct map *map;
+ struct symbol *sym;
+};
+
+static void pyrf_callchain_node__delete(struct pyrf_callchain_node *pnode)
+{
+ map__put(pnode->map);
+ Py_TYPE(pnode)->tp_free((PyObject*)pnode);
+}
+
+static PyObject *pyrf_callchain_node__get_ip(struct pyrf_callchain_node *pnode,
+ void *closure __maybe_unused)
+{
+ return PyLong_FromUnsignedLongLong(pnode->ip);
+}
+
+static PyObject *pyrf_callchain_node__get_symbol(struct pyrf_callchain_node *pnode,
+ void *closure __maybe_unused)
+{
+ if (pnode->sym)
+ return PyUnicode_FromString(pnode->sym->name);
+ return PyUnicode_FromString("[unknown]");
+}
+
+static PyObject *pyrf_callchain_node__get_dso(struct pyrf_callchain_node *pnode,
+ void *closure __maybe_unused)
+{
+ const char *dsoname = "[unknown]";
+
+ if (pnode->map) {
+ struct dso *dso = map__dso(pnode->map);
+ if (dso) {
+ if (symbol_conf.show_kernel_path && dso__long_name(dso))
+ dsoname = dso__long_name(dso);
+ else
+ dsoname = dso__name(dso);
+ }
+ }
+ return PyUnicode_FromString(dsoname);
+}
+
+static PyGetSetDef pyrf_callchain_node__getset[] = {
+ { .name = "ip", .get = (getter)pyrf_callchain_node__get_ip, },
+ { .name = "symbol", .get = (getter)pyrf_callchain_node__get_symbol, },
+ { .name = "dso", .get = (getter)pyrf_callchain_node__get_dso, },
+ { .name = NULL, },
+};
+
+static PyTypeObject pyrf_callchain_node__type = {
+ PyVarObject_HEAD_INIT(NULL, 0)
+ .tp_name = "perf.callchain_node",
+ .tp_basicsize = sizeof(struct pyrf_callchain_node),
+ .tp_dealloc = (destructor)pyrf_callchain_node__delete,
+ .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
+ .tp_doc = "perf callchain node object.",
+ .tp_getset = pyrf_callchain_node__getset,
+};
+
+struct pyrf_callchain_frame {
+ u64 ip;
+ struct map *map;
+ struct symbol *sym;
+};
+
+struct pyrf_callchain {
+ PyObject_HEAD
+ struct pyrf_event *pevent;
+ struct pyrf_callchain_frame *frames;
+ u64 nr_frames;
+ u64 pos;
+ bool resolved;
+};
+
+static void pyrf_callchain__delete(struct pyrf_callchain *pchain)
+{
+ Py_XDECREF(pchain->pevent);
+ if (pchain->frames) {
+ for (u64 i = 0; i < pchain->nr_frames; i++)
+ map__put(pchain->frames[i].map);
+ free(pchain->frames);
+ }
+ Py_TYPE(pchain)->tp_free((PyObject*)pchain);
+}
+
+static PyObject *pyrf_callchain__next(struct pyrf_callchain *pchain)
+{
+ struct pyrf_callchain_node *pnode;
+
+ if (!pchain->resolved) {
+ struct evsel *evsel = pchain->pevent->sample.evsel;
+ struct evlist *evlist = evsel->evlist;
+ struct perf_session *session = evlist ? evlist__session(evlist) : NULL;
+ struct addr_location al;
+ struct callchain_cursor *cursor;
+ struct callchain_cursor_node *node;
+ u64 i;
+
+ if (!session || !pchain->pevent->sample.callchain)
+ return NULL;
+
+ addr_location__init(&al);
+ if (machine__resolve(&session->machines.host, &al, &pchain->pevent->sample) < 0) {
+ addr_location__exit(&al);
+ return NULL;
+ }
+
+ cursor = get_tls_callchain_cursor();
+ if (thread__resolve_callchain(al.thread, cursor,
+ &pchain->pevent->sample, NULL, NULL,
+ PERF_MAX_STACK_DEPTH) != 0) {
+ addr_location__exit(&al);
+ return NULL;
+ }
+ callchain_cursor_commit(cursor);
+
+ pchain->nr_frames = cursor->nr;
+ if (pchain->nr_frames > 0) {
+ pchain->frames = calloc(pchain->nr_frames, sizeof(*pchain->frames));
+ if (!pchain->frames) {
+ addr_location__exit(&al);
+ return PyErr_NoMemory();
+ }
+
+ for (i = 0; i < pchain->nr_frames; i++) {
+ node = callchain_cursor_current(cursor);
+ pchain->frames[i].ip = node->ip;
+ pchain->frames[i].map = map__get(node->ms.map);
+ pchain->frames[i].sym = node->ms.sym;
+ callchain_cursor_advance(cursor);
+ }
+ }
+ pchain->resolved = true;
+ addr_location__exit(&al);
+ }
+
+ if (pchain->pos >= pchain->nr_frames)
+ return NULL;
+
+ pnode = PyObject_New(struct pyrf_callchain_node, &pyrf_callchain_node__type);
+ if (!pnode)
+ return NULL;
+
+ pnode->ip = pchain->frames[pchain->pos].ip;
+ pnode->map = map__get(pchain->frames[pchain->pos].map);
+ pnode->sym = pchain->frames[pchain->pos].sym;
+
+ pchain->pos++;
+ return (PyObject *)pnode;
+}
+
+static PyTypeObject pyrf_callchain__type = {
+ PyVarObject_HEAD_INIT(NULL, 0)
+ .tp_name = "perf.callchain",
+ .tp_basicsize = sizeof(struct pyrf_callchain),
+ .tp_dealloc = (destructor)pyrf_callchain__delete,
+ .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
+ .tp_doc = "perf callchain object.",
+ .tp_iter = PyObject_SelfIter,
+ .tp_iternext = (iternextfunc)pyrf_callchain__next,
+};
+
+static PyObject *pyrf_sample_event__get_callchain(PyObject *self, void *closure __maybe_unused)
+{
+ struct pyrf_event *pevent = (void *)self;
+
+ if (!pevent->callchain)
+ Py_RETURN_NONE;
+
+ Py_INCREF(pevent->callchain);
+ return pevent->callchain;
+}
+
static PyObject*
pyrf_sample_event__getattro(struct pyrf_event *pevent, PyObject *attr_name)
{
@@ -683,6 +861,12 @@ pyrf_sample_event__getattro(struct pyrf_event *pevent, PyObject *attr_name)
}
static PyGetSetDef pyrf_sample_event__getset[] = {
+ {
+ .name = "callchain",
+ .get = pyrf_sample_event__get_callchain,
+ .set = NULL,
+ .doc = "event callchain.",
+ },
{
.name = "raw_buf",
.get = (getter)pyrf_sample_event__get_raw_buf,
@@ -852,6 +1036,12 @@ static int pyrf_event__setup_types(void)
err = PyType_Ready(&pyrf_context_switch_event__type);
if (err < 0)
goto out;
+ err = PyType_Ready(&pyrf_callchain_node__type);
+ if (err < 0)
+ goto out;
+ err = PyType_Ready(&pyrf_callchain__type);
+ if (err < 0)
+ goto out;
out:
return err;
}
@@ -872,9 +1062,11 @@ static PyTypeObject *pyrf_event__type[] = {
};
static PyObject *pyrf_event__new(const union perf_event *event, struct evsel *evsel,
- struct perf_session *session __maybe_unused)
+ struct perf_session *session)
{
struct pyrf_event *pevent;
+ struct perf_sample *sample;
+ struct machine *machine = session ? &session->machines.host : NULL;
size_t size;
int err;
size_t min_size = sizeof(struct perf_event_header);
@@ -1033,6 +1225,7 @@ static PyObject *pyrf_event__new(const union perf_event *event, struct evsel *ev
}
perf_sample__init(&pevent->sample, /*all=*/true);
+ pevent->callchain = NULL;
pevent->al_resolved = false;
addr_location__init(&pevent->al);
@@ -1046,6 +1239,49 @@ static PyObject *pyrf_event__new(const union perf_event *event, struct evsel *ev
return PyErr_Format(PyExc_OSError,
"perf: can't parse sample, err=%d", err);
}
+ sample = &pevent->sample;
+ if (machine && sample->callchain) {
+ struct addr_location al;
+ struct callchain_cursor *cursor;
+ u64 i;
+ struct pyrf_callchain *pchain;
+
+ addr_location__init(&al);
+ if (machine__resolve(machine, &al, sample) >= 0) {
+ cursor = get_tls_callchain_cursor();
+ if (thread__resolve_callchain(al.thread, cursor, sample,
+ NULL, NULL, PERF_MAX_STACK_DEPTH) == 0) {
+ callchain_cursor_commit(cursor);
+
+ pchain = PyObject_New(struct pyrf_callchain, &pyrf_callchain__type);
+ if (pchain) {
+ pchain->pevent = pevent;
+ Py_INCREF(pevent);
+ pchain->nr_frames = cursor->nr;
+ pchain->pos = 0;
+ pchain->resolved = true;
+ pchain->frames = calloc(pchain->nr_frames,
+ sizeof(*pchain->frames));
+ if (pchain->frames) {
+ struct callchain_cursor_node *node;
+
+ for (i = 0; i < pchain->nr_frames; i++) {
+ node = callchain_cursor_current(cursor);
+ pchain->frames[i].ip = node->ip;
+ pchain->frames[i].map =
+ map__get(node->ms.map);
+ pchain->frames[i].sym = node->ms.sym;
+ callchain_cursor_advance(cursor);
+ }
+ pevent->callchain = (PyObject *)pchain;
+ } else {
+ Py_DECREF(pchain);
+ }
+ }
+ }
+ addr_location__exit(&al);
+ }
+ }
return (PyObject *)pevent;
}
@@ -3145,6 +3381,9 @@ static PyObject *pyrf_session__new(PyTypeObject *type, PyObject *args, PyObject
}
psession->session = session;
+ symbol_conf.use_callchain = true;
+ symbol_conf.show_kernel_path = true;
+ symbol_conf.inline_name = false;
if (symbol__init(perf_session__env(session)) < 0) {
PyErr_SetString(PyExc_OSError, "perf: symbol__init failed");
goto err_out;
--
2.54.0.794.g4f17f83d09-goog
^ permalink raw reply related
* Re: [PATCH v5 4/5] PCI: dwc: Use common D3cold eligibility helper in suspend path
From: Bjorn Helgaas @ 2026-05-22 22:48 UTC (permalink / raw)
To: Krishna Chaitanya Chundru
Cc: Jingoo Han, Manivannan Sadhasivam, Lorenzo Pieralisi,
Krzysztof Wilczyński, Rob Herring, Bjorn Helgaas,
Will Deacon, linux-pci, linux-kernel, linux-arm-msm,
linux-arm-kernel, jonathanh, bjorn.andersson, Frank Li, linux-pm
In-Reply-To: <20260520000153.GA14400@bhelgaas>
On Tue, May 19, 2026 at 07:01:53PM -0500, Bjorn Helgaas wrote:
> [+cc Frank, linux-pm]
>
> On Wed, Apr 29, 2026 at 12:12:26PM +0530, Krishna Chaitanya Chundru wrote:
> > Previously, the driver skipped putting the link into L2/device state in
> > D3cold whenever L1 ASPM was enabled, since some devices (e.g. NVMe) expect
> > low resume latency and may not tolerate deeper power states.
>
> I think "some devices expect low resume latency and may not tolerate
> deeper power states" conveys the wrong message. It's not that NVMe
> has a mysterious acceptable resume latency number that we have to meet
> or that NVMe has some inherent aversion to D3cold or L1SS or whatever
> "deeper power states" refers to.
>
> It could be that ASPM L1 was configured incorrectly (e.g., an L1->L0
> transition didn't happen within the advertised exit latency, leading
> to some device access failure) or a device lost internal context when
> the driver didn't expect it (e.g., the Qcom problem where L1SS exit
> takes too long and results in a link-down and device reset [1]).
>
> It sounds to me like the ASPM L1 check was a way to avoid problems
> like that, but I don't think we ever really had a root cause.
Possible updated commit log text:
Previously, the driver skipped putting the link into L2 and device
state in D3cold when L1 ASPM was enabled since some devices (e.g.
NVMe) failed to resume correctly if they were suspended with L1
enabled. The root cause of the failure is unknown.
> [1] https://lore.kernel.org/linux-pci/20260519-l1ss-fix-v2-0-b2c3a4bdeb15@oss.qualcomm.com/
>
> > However, such devices typically remain in D0 and are already covered
> > by the new helper's requirement that all endpoints be in D3hot
> > before the devices under host bridge may enter D3cold.
It makes me nervous when we assume "typical" things, but I don't have
any ideas about wording this.
This is all merged and in pci/next, so we can leave it as-is or amend
the commit log if anybody has better ideas.
> If we put the host bridge in D3cold, I assume the hierarchy below is
> either put in D3cold as well, or at least every device in the
> hierarchy will be reset as a consequence of the Root Port link going
> down.
>
> If the driver doesn't manage the device power state itself, I assume
> we have the freedom to put the hierarchy in D3cold or reset it.
>
> Do we have the same freedom if the driver *does* manage the power
> state itself? What if the driver put the device in D3hot, expecting
> it to *stay* in D3hot?
>
> I think pci_host_common_d3cold_possible() will see the device in D3hot
> and decide that D3cold is possible.
>
> (I'm looking at https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/power/pci.rst?id=v7.0#n746)
>
> > So, replace the local L1/L1SS-based check in dw_pcie_suspend_noirq() with
> > the shared pci_host_common_d3cold_possible() helper to decide whether the
> > devices under host bridge can safely transition to D3cold.
> >
> > In addition, propagate PME-from-D3cold capability information from the
> > helper and record it in skip_pwrctrl_off. Some devices (e.g. M.2 cards
> > without auxiliary power) may lose PME detection when main power is
> > removed, even if they advertise PME-from-D3cold support. This allows
> > controller power-off to be skipped when required to preserve wakeup
> > functionality.
> >
> > Update the suspended flag in dw_pcie_resume_noirq() only after the PCIe
> > link resumes successfully, to avoid marking the controller active when
> > link resume fails.
> >
> > Signed-off-by: Krishna Chaitanya Chundru <krishna.chundru@oss.qualcomm.com>
> > ---
> > drivers/pci/controller/dwc/pcie-designware-host.c | 15 +++++++--------
> > drivers/pci/controller/dwc/pcie-designware.h | 1 +
> > 2 files changed, 8 insertions(+), 8 deletions(-)
> >
> > diff --git a/drivers/pci/controller/dwc/pcie-designware-host.c b/drivers/pci/controller/dwc/pcie-designware-host.c
> > index c9517a348836..9e409a1909e6 100644
> > --- a/drivers/pci/controller/dwc/pcie-designware-host.c
> > +++ b/drivers/pci/controller/dwc/pcie-designware-host.c
> > @@ -16,9 +16,11 @@
> > #include <linux/msi.h>
> > #include <linux/of_address.h>
> > #include <linux/of_pci.h>
> > +#include <linux/pci.h>
> > #include <linux/pci_regs.h>
> > #include <linux/platform_device.h>
> >
> > +#include "../pci-host-common.h"
> > #include "../../pci.h"
> > #include "pcie-designware.h"
> >
> > @@ -1218,18 +1220,14 @@ static int dw_pcie_pme_turn_off(struct dw_pcie *pci)
> >
> > int dw_pcie_suspend_noirq(struct dw_pcie *pci)
> > {
> > - u8 offset = dw_pcie_find_capability(pci, PCI_CAP_ID_EXP);
> > + bool pme_capable = false;
> > int ret = 0;
> > u32 val;
> >
> > if (!dw_pcie_link_up(pci))
> > goto stop_link;
> >
> > - /*
> > - * If L1SS is supported, then do not put the link into L2 as some
> > - * devices such as NVMe expect low resume latency.
> > - */
> > - if (dw_pcie_readw_dbi(pci, offset + PCI_EXP_LNKCTL) & PCI_EXP_LNKCTL_ASPM_L1)
> > + if (!pci_host_common_d3cold_possible(pci->pp.bridge, &pme_capable))
> > return 0;
> >
> > if (pci->pp.ops->pme_turn_off) {
> > @@ -1273,6 +1271,7 @@ int dw_pcie_suspend_noirq(struct dw_pcie *pci)
> > udelay(1);
> >
> > stop_link:
> > + pci->pp.skip_pwrctrl_off = pme_capable;
> > dw_pcie_stop_link(pci);
> > if (pci->pp.ops->deinit)
> > pci->pp.ops->deinit(&pci->pp);
> > @@ -1290,8 +1289,6 @@ int dw_pcie_resume_noirq(struct dw_pcie *pci)
> > if (!pci->suspended)
> > return 0;
> >
> > - pci->suspended = false;
> > -
> > if (pci->pp.ops->init) {
> > ret = pci->pp.ops->init(&pci->pp);
> > if (ret) {
> > @@ -1313,6 +1310,8 @@ int dw_pcie_resume_noirq(struct dw_pcie *pci)
> > if (pci->pp.ops->post_init)
> > pci->pp.ops->post_init(&pci->pp);
> >
> > + pci->suspended = false;
> > +
> > return 0;
> >
> > err_stop_link:
> > diff --git a/drivers/pci/controller/dwc/pcie-designware.h b/drivers/pci/controller/dwc/pcie-designware.h
> > index 3e69ef60165b..e759c5c7257e 100644
> > --- a/drivers/pci/controller/dwc/pcie-designware.h
> > +++ b/drivers/pci/controller/dwc/pcie-designware.h
> > @@ -450,6 +450,7 @@ struct dw_pcie_rp {
> > bool ecam_enabled;
> > bool native_ecam;
> > bool skip_l23_ready;
> > + bool skip_pwrctrl_off;
> > };
> >
> > struct dw_pcie_ep_ops {
> >
> > --
> > 2.34.1
> >
^ permalink raw reply
* [PATCH v9 03/23] perf tests: Sort includes and add missed explicit dependencies
From: Ian Rogers @ 2026-05-22 22:04 UTC (permalink / raw)
To: irogers, acme, namhyung
Cc: adrian.hunter, alice.mei.rogers, dapeng1.mi, james.clark, leo.yan,
linux-arm-kernel, linux-kernel, linux-perf-users, mingo, peterz,
tmricht
In-Reply-To: <20260522220435.2378363-1-irogers@google.com>
Fix missing #includes found while cleaning the evsel/evlist header
files. Sort the remaining header files for consistency with the rest
of the code.
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/tests/hwmon_pmu.c | 14 +++++++++-----
tools/perf/tests/mmap-basic.c | 18 +++++++++++-------
2 files changed, 20 insertions(+), 12 deletions(-)
diff --git a/tools/perf/tests/hwmon_pmu.c b/tools/perf/tests/hwmon_pmu.c
index 4aa4aac94f09..ada6e445c4c4 100644
--- a/tools/perf/tests/hwmon_pmu.c
+++ b/tools/perf/tests/hwmon_pmu.c
@@ -1,15 +1,19 @@
// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
-#include "debug.h"
-#include "evlist.h"
#include "hwmon_pmu.h"
-#include "parse-events.h"
-#include "tests.h"
+
#include <errno.h>
+
#include <fcntl.h>
-#include <sys/stat.h>
#include <linux/compiler.h>
#include <linux/kernel.h>
#include <linux/string.h>
+#include <sys/stat.h>
+
+#include "debug.h"
+#include "evlist.h"
+#include "parse-events.h"
+#include "pmus.h"
+#include "tests.h"
static const struct test_event {
const char *name;
diff --git a/tools/perf/tests/mmap-basic.c b/tools/perf/tests/mmap-basic.c
index a18d84d858aa..a69cd1046e9a 100644
--- a/tools/perf/tests/mmap-basic.c
+++ b/tools/perf/tests/mmap-basic.c
@@ -1,25 +1,29 @@
// SPDX-License-Identifier: GPL-2.0
#include <errno.h>
-#include <fcntl.h>
#include <inttypes.h>
#include <stdlib.h>
+
+#include <fcntl.h>
+#include <linux/err.h>
+#include <linux/kernel.h>
+#include <linux/string.h>
+
#include <perf/cpumap.h>
+#include <perf/evlist.h>
+#include <perf/mmap.h>
#include "cpumap.h"
#include "debug.h"
#include "event.h"
#include "evlist.h"
#include "evsel.h"
-#include "thread_map.h"
+#include "pmu.h"
+#include "pmus.h"
#include "tests.h"
+#include "thread_map.h"
#include "util/affinity.h"
#include "util/mmap.h"
#include "util/sample.h"
-#include <linux/err.h>
-#include <linux/kernel.h>
-#include <linux/string.h>
-#include <perf/evlist.h>
-#include <perf/mmap.h>
/*
* This test will generate random numbers of calls to some getpid syscalls,
--
2.54.0.794.g4f17f83d09-goog
^ permalink raw reply related
* [PATCH v9 14/23] perf python: Add python session abstraction wrapping perf's session
From: Ian Rogers @ 2026-05-22 22:04 UTC (permalink / raw)
To: irogers, acme, namhyung
Cc: adrian.hunter, alice.mei.rogers, dapeng1.mi, james.clark, leo.yan,
linux-arm-kernel, linux-kernel, linux-perf-users, mingo, peterz,
tmricht
In-Reply-To: <20260522220435.2378363-1-irogers@google.com>
Sessions are necessary to be able to use perf.data files within a
tool. Add a wrapper python type that incorporates the tool. Allow a
sample callback to be passed when creating the session. When
process_events is run this callback will be called, if supplied, for
sample events.
An example use looks like:
```
$ perf record -e cycles,instructions -a sleep 3
$ PYTHONPATH=..../perf/python python3
Python 3.13.7 (main, Aug 20 2025, 22:17:40) [GCC 14.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import perf
>>> count=0
... def handle_sample(x):
... global count
... if count < 3:
... print(dir(x))
... count = count + 1
... perf.session(perf.data("perf.data"),sample=handle_sample).process_events()
...
['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'sample_addr', 'sample_cpu', 'sample_id', 'sample_ip', 'sample_period', 'sample_pid', 'sample_stream_id', 'sample_tid', 'sample_time', 'type']
['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'sample_addr', 'sample_cpu', 'sample_id', 'sample_ip', 'sample_period', 'sample_pid', 'sample_stream_id', 'sample_tid', 'sample_time', 'type']
['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'sample_addr', 'sample_cpu', 'sample_id', 'sample_ip', 'sample_period', 'sample_pid', 'sample_stream_id', 'sample_tid', 'sample_time', 'type']
```
Also, add the ability to get the thread associated with a session. For
threads, allow the comm string to be retrieved. This can be useful for
filtering threads. Connect up some of the standard event handling in
psession->tool to better support queries of the machine. Also connect
up the symbols.
Assisted-by: Gemini:gemini-3.1-pro-preview
Signed-off-by: Ian Rogers <irogers@google.com>
---
v2:
1. Fixed Potential Crash in pyrf_thread__comm : Used
thread__comm_str() to safely retrieve the command name, avoiding a
crash if thread__comm() returns NULL.
2. Fixed Double Free Risk: Zeroed out user_regs , intr_regs , and
callchain in the shallow copy of perf_sample to prevent Python from
attempting to free pointers it doesn't own.
3. Fixed Memory Leak & Exception Handling in Callback: Handled the
return value of PyObject_CallFunction() to avoid leaks, and checked
for failure to abort the loop and propagate Python exceptions
cleanly.
4. Enforced Type Safety: Used O! with &pyrf_data__type in
PyArg_ParseTupleAndKeywords to prevent bad casts from passing
arbitrary objects as perf.data.
5. Added Missing Build ID Handler: Registered
perf_event__process_build_id to allow correct symbol resolution.
6. Fixed Double Free Crash on Init Failure: Set session and pdata to
NULL on failure to prevent tp_dealloc from double-freeing them.
7. Preserved C-level Errors: Made pyrf_session__process_events return
the error code integer rather than always returning None .
v7:
- Fixed NULL comm handling.
- Avoided swallowing exceptions in module init.
- Fixed checkpatch warning for missing blank line.
v8:
- Switch from pyrf_session__init to pyrf_session__new to avoid dealing with a
potentially NULL session variable.
- Added pid, tid, ppid, and cpu attributes to perf.thread.
---
tools/perf/util/python.c | 310 ++++++++++++++++++++++++++++++++++++++-
1 file changed, 304 insertions(+), 6 deletions(-)
diff --git a/tools/perf/util/python.c b/tools/perf/util/python.c
index 82adc59ecd94..18a48779c591 100644
--- a/tools/perf/util/python.c
+++ b/tools/perf/util/python.c
@@ -11,8 +11,10 @@
#include <structmember.h>
#include "callchain.h"
+#include "comm.h"
#include "counts.h"
#include "data.h"
+#include "debug.h"
#include "event.h"
#include "evlist.h"
#include "evsel.h"
@@ -22,8 +24,12 @@
#include "pmus.h"
#include "print_binary.h"
#include "record.h"
+#include "session.h"
#include "strbuf.h"
+#include "symbol.h"
+#include "thread.h"
#include "thread_map.h"
+#include "tool.h"
#include "tp_pmu.h"
#include "trace-event.h"
#include "util/sample.h"
@@ -2417,6 +2423,287 @@ static int pyrf_data__setup_types(void)
return PyType_Ready(&pyrf_data__type);
}
+struct pyrf_thread {
+ PyObject_HEAD
+
+ struct thread *thread;
+};
+
+static void pyrf_thread__delete(struct pyrf_thread *pthread)
+{
+ thread__put(pthread->thread);
+ Py_TYPE(pthread)->tp_free((PyObject *)pthread);
+}
+
+static PyObject *pyrf_thread__comm(PyObject *obj)
+{
+ struct pyrf_thread *pthread = (void *)obj;
+ const char *str = thread__comm_str(pthread->thread);
+
+ if (!str)
+ Py_RETURN_NONE;
+
+ return PyUnicode_FromString(str);
+}
+
+static PyMethodDef pyrf_thread__methods[] = {
+ {
+ .ml_name = "comm",
+ .ml_meth = (PyCFunction)pyrf_thread__comm,
+ .ml_flags = METH_NOARGS,
+ .ml_doc = PyDoc_STR("Comm(and) associated with this thread.")
+ },
+ { .ml_name = NULL, }
+};
+
+static PyObject *pyrf_thread__get_pid(struct pyrf_thread *pthread, void *closure __maybe_unused)
+{
+ return PyLong_FromLong(thread__pid(pthread->thread));
+}
+
+static PyObject *pyrf_thread__get_tid(struct pyrf_thread *pthread, void *closure __maybe_unused)
+{
+ return PyLong_FromLong(thread__tid(pthread->thread));
+}
+
+static PyObject *pyrf_thread__get_ppid(struct pyrf_thread *pthread, void *closure __maybe_unused)
+{
+ return PyLong_FromLong(thread__ppid(pthread->thread));
+}
+
+static PyObject *pyrf_thread__get_cpu(struct pyrf_thread *pthread, void *closure __maybe_unused)
+{
+ return PyLong_FromLong(thread__cpu(pthread->thread));
+}
+
+static PyGetSetDef pyrf_thread__getset[] = {
+ { .name = "pid", .get = (getter)pyrf_thread__get_pid, .doc = "process ID" },
+ { .name = "tid", .get = (getter)pyrf_thread__get_tid, .doc = "thread ID" },
+ { .name = "ppid", .get = (getter)pyrf_thread__get_ppid, .doc = "parent process ID" },
+ { .name = "cpu", .get = (getter)pyrf_thread__get_cpu, .doc = "cpu number" },
+ { .name = NULL }
+};
+
+static const char pyrf_thread__doc[] = PyDoc_STR("perf thread object.");
+
+static PyTypeObject pyrf_thread__type = {
+ PyVarObject_HEAD_INIT(NULL, 0)
+ .tp_name = "perf.thread",
+ .tp_basicsize = sizeof(struct pyrf_thread),
+ .tp_dealloc = (destructor)pyrf_thread__delete,
+ .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
+ .tp_methods = pyrf_thread__methods,
+ .tp_getset = pyrf_thread__getset,
+ .tp_doc = pyrf_thread__doc,
+};
+
+static int pyrf_thread__setup_types(void)
+{
+ return PyType_Ready(&pyrf_thread__type);
+}
+
+static PyObject *pyrf_thread__from_thread(struct thread *thread)
+{
+ struct pyrf_thread *pthread = PyObject_New(struct pyrf_thread, &pyrf_thread__type);
+
+ if (!pthread)
+ return NULL;
+
+ pthread->thread = thread__get(thread);
+ return (PyObject *)pthread;
+}
+
+struct pyrf_session {
+ PyObject_HEAD
+
+ struct perf_session *session;
+ struct perf_tool tool;
+ struct pyrf_data *pdata;
+ PyObject *sample;
+};
+
+static int pyrf_session_tool__sample(const struct perf_tool *tool,
+ union perf_event *event,
+ struct perf_sample *sample,
+ struct machine *machine __maybe_unused)
+{
+ struct pyrf_session *psession = container_of(tool, struct pyrf_session, tool);
+ PyObject *pyevent = pyrf_event__new(event);
+ struct pyrf_event *pevent = (struct pyrf_event *)pyevent;
+ PyObject *ret;
+
+ if (pyevent == NULL)
+ return -ENOMEM;
+
+ memcpy(&pevent->event, event, event->header.size);
+ if (evsel__parse_sample(sample->evsel, &pevent->event, &pevent->sample) < 0) {
+ Py_DECREF(pyevent);
+ return -1;
+ }
+
+ ret = PyObject_CallFunction(psession->sample, "O", pyevent);
+ if (!ret) {
+ Py_DECREF(pyevent);
+ return -1;
+ }
+ Py_DECREF(ret);
+ Py_DECREF(pyevent);
+ return 0;
+}
+
+static PyObject *pyrf_session__find_thread(struct pyrf_session *psession, PyObject *args)
+{
+ struct machine *machine;
+ struct thread *thread = NULL;
+ PyObject *result;
+ int pid;
+
+ if (!PyArg_ParseTuple(args, "i", &pid))
+ return NULL;
+
+ machine = &psession->session->machines.host;
+ thread = machine__find_thread(machine, pid, pid);
+
+ if (!thread) {
+ machine = perf_session__find_machine(psession->session, pid);
+ if (machine)
+ thread = machine__find_thread(machine, pid, pid);
+ }
+
+ if (!thread) {
+ PyErr_Format(PyExc_TypeError, "Failed to find thread %d", pid);
+ return NULL;
+ }
+ result = pyrf_thread__from_thread(thread);
+ thread__put(thread);
+ return result;
+}
+
+static PyObject *pyrf_session__new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
+{
+ struct pyrf_data *pdata;
+ PyObject *sample = NULL;
+ static char *kwlist[] = { "data", "sample", NULL };
+ struct pyrf_session *psession;
+ struct perf_session *session;
+
+ if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!|O", kwlist, &pyrf_data__type, &pdata,
+ &sample))
+ return NULL;
+
+ psession = PyObject_New(struct pyrf_session, type);
+ if (!psession)
+ return NULL;
+
+ psession->session = NULL;
+ psession->sample = NULL;
+ psession->pdata = NULL;
+
+ Py_INCREF(pdata);
+ psession->pdata = pdata;
+
+ perf_tool__init(&psession->tool, /*ordered_events=*/true);
+ psession->tool.ordering_requires_timestamps = true;
+
+ #define ADD_TOOL(name) \
+ do { \
+ if (name) { \
+ if (!PyCallable_Check(name)) { \
+ PyErr_SetString(PyExc_TypeError, #name " must be callable"); \
+ goto err_out; \
+ } \
+ psession->tool.name = pyrf_session_tool__##name; \
+ Py_INCREF(name); \
+ psession->name = name; \
+ } \
+ } while (0)
+
+ ADD_TOOL(sample);
+ #undef ADD_TOOL
+
+ psession->tool.comm = perf_event__process_comm;
+ psession->tool.mmap = perf_event__process_mmap;
+ psession->tool.mmap2 = perf_event__process_mmap2;
+ psession->tool.namespaces = perf_event__process_namespaces;
+ psession->tool.cgroup = perf_event__process_cgroup;
+ psession->tool.exit = perf_event__process_exit;
+ psession->tool.fork = perf_event__process_fork;
+ psession->tool.ksymbol = perf_event__process_ksymbol;
+ psession->tool.text_poke = perf_event__process_text_poke;
+ psession->tool.build_id = perf_event__process_build_id;
+ session = perf_session__new(&pdata->data, &psession->tool);
+ if (IS_ERR(session)) {
+ PyErr_Format(PyExc_IOError, "failed to create session: %ld", PTR_ERR(session));
+ goto err_out;
+ }
+ psession->session = session;
+
+ if (symbol__init(perf_session__env(session)) < 0) {
+ PyErr_SetString(PyExc_OSError, "perf: symbol__init failed");
+ goto err_out;
+ }
+
+ if (perf_session__create_kernel_maps(session) < 0)
+ pr_warning("Cannot read kernel map\n");
+
+ return (PyObject *)psession;
+err_out:
+ Py_DECREF(psession);
+ return NULL;
+}
+
+static void pyrf_session__delete(struct pyrf_session *psession)
+{
+ perf_session__delete(psession->session);
+ Py_XDECREF(psession->pdata);
+ Py_XDECREF(psession->sample);
+ Py_TYPE(psession)->tp_free((PyObject *)psession);
+}
+
+static PyObject *pyrf_session__find_thread_events(struct pyrf_session *psession)
+{
+ int err = perf_session__process_events(psession->session);
+
+ if (PyErr_Occurred())
+ return NULL;
+
+ return PyLong_FromLong(err);
+}
+
+static PyMethodDef pyrf_session__methods[] = {
+ {
+ .ml_name = "process_events",
+ .ml_meth = (PyCFunction)pyrf_session__find_thread_events,
+ .ml_flags = METH_NOARGS,
+ .ml_doc = PyDoc_STR("Iterate and process events.")
+ },
+ {
+ .ml_name = "find_thread",
+ .ml_meth = (PyCFunction)pyrf_session__find_thread,
+ .ml_flags = METH_VARARGS,
+ .ml_doc = PyDoc_STR("Returns the thread associated with a pid.")
+ },
+ { .ml_name = NULL, }
+};
+
+static const char pyrf_session__doc[] = PyDoc_STR("perf session object.");
+
+static PyTypeObject pyrf_session__type = {
+ PyVarObject_HEAD_INIT(NULL, 0)
+ .tp_name = "perf.session",
+ .tp_basicsize = sizeof(struct pyrf_session),
+ .tp_dealloc = (destructor)pyrf_session__delete,
+ .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
+ .tp_methods = pyrf_session__methods,
+ .tp_doc = pyrf_session__doc,
+ .tp_new = pyrf_session__new,
+};
+
+static int pyrf_session__setup_types(void)
+{
+ return PyType_Ready(&pyrf_session__type);
+}
+
static PyMethodDef perf__methods[] = {
{
.ml_name = "metrics",
@@ -2471,8 +2758,10 @@ PyMODINIT_FUNC PyInit_perf(void)
};
PyObject *module = PyModule_Create(&moduledef);
- if (module == NULL ||
- pyrf_event__setup_types() < 0 ||
+ if (module == NULL)
+ return NULL;
+
+ if (pyrf_event__setup_types() < 0 ||
pyrf_evlist__setup_types() < 0 ||
pyrf_evsel__setup_types() < 0 ||
pyrf_thread_map__setup_types() < 0 ||
@@ -2480,8 +2769,12 @@ PyMODINIT_FUNC PyInit_perf(void)
pyrf_pmu_iterator__setup_types() < 0 ||
pyrf_pmu__setup_types() < 0 ||
pyrf_counts_values__setup_types() < 0 ||
- pyrf_data__setup_types() < 0)
- return module;
+ pyrf_data__setup_types() < 0 ||
+ pyrf_session__setup_types() < 0 ||
+ pyrf_thread__setup_types() < 0) {
+ Py_DECREF(module);
+ return NULL;
+ }
/* The page_size is placed in util object. */
page_size = sysconf(_SC_PAGE_SIZE);
@@ -2531,6 +2824,9 @@ PyMODINIT_FUNC PyInit_perf(void)
Py_INCREF(&pyrf_data__type);
PyModule_AddObject(module, "data", (PyObject *)&pyrf_data__type);
+ Py_INCREF(&pyrf_session__type);
+ PyModule_AddObject(module, "session", (PyObject *)&pyrf_session__type);
+
dict = PyModule_GetDict(module);
if (dict == NULL)
goto error;
@@ -2544,7 +2840,9 @@ PyMODINIT_FUNC PyInit_perf(void)
}
error:
- if (PyErr_Occurred())
- PyErr_SetString(PyExc_ImportError, "perf: Init failed!");
+ if (PyErr_Occurred()) {
+ Py_XDECREF(module);
+ return NULL;
+ }
return module;
}
--
2.54.0.794.g4f17f83d09-goog
^ permalink raw reply related
* [PATCH v3 00/11] add mcf54415 DAC driver
From: Angelo Dureghello @ 2026-05-22 21:20 UTC (permalink / raw)
To: Greg Ungerer, Geert Uytterhoeven, Steven King, Arnd Bergmann,
Maxime Coquelin, Alexandre Torgue, Jonathan Cameron,
David Lechner, Nuno Sá, Andy Shevchenko
Cc: Greg Ungerer, linux-m68k, linux-kernel, linux-stm32,
linux-arm-kernel, linux-iio, Angelo Dureghello
This patchset adds a minimalistic DAC driver for the NXP mcf54415/6/7/8
ibuiltin DACs.
Currently the driver enables the raw write only. Feature as dma, sync, or
format are not supoprted for this version.
Additional options suppoerted by the DAC module will be added to the driver
later on, as needed.
The same patchset prepares the m68k/coldfire architecture to support
the driver.
Below soem basic tests done on stmark2 mcf54415-based board, voltage check
on DAC0 and DAC1:
~ # cd /sys/bus/iio/devices/iio:device0/
/sys/bus/iio/devices/iio:device0 # ls
name out_voltage_scale uevent
out_voltage_raw subsystem
/sys/bus/iio/devices/iio:device0 # cat name
mcf54415
/sys/bus/iio/devices/iio:device0 # echo 4095 > out_voltage_raw
/sys/bus/iio/devices/iio:device0 # echo 2048 > out_voltage_raw
/sys/bus/iio/devices/iio:device0 # echo 4096 > out_voltage_raw
sh: write error: Invalid argument
/sys/bus/iio/devices/iio:device0 # cat out_voltage_raw
2048
/sys/bus/iio/devices/iio:device0 #
Same behavior for /sys/bus/iio/devices/iio:device1.
Generated a sine wave by shell script, sine shape is good.
is actually in progress:
Note: this patchset depends on mew mcf_read/mcf_write implementation that
Link: https://lore.kernel.org/linux-m68k/209d0653-6386-4b64-9e15-e358f84453ab@app.fastmail.com/T/#t
Link: https://lore.kernel.org/linux-m68k/20260506142644.3234270-2-gerg@kernel.org/
---
Changes in v3:
- keeping changelog in each single patch, where any
- Link to v2: https://patch.msgid.link/20260513-wip-stmark2-dac-v2-0-fcdae50cf51a@baylibre.com
Changes in v2:
- keeping changelog in each single patch, where any
- Link to v1: https://patch.msgid.link/20260504-wip-stmark2-dac-v1-0-874c36a4910d@baylibre.com
To: Greg Ungerer <gerg@linux-m68k.org>
To: Geert Uytterhoeven <geert@linux-m68k.org>
To: Steven King <sfking@fdwdc.com>
To: Arnd Bergmann <arnd@arndb.de>
To: Maxime Coquelin <mcoquelin.stm32@gmail.com>
To: Alexandre Torgue <alexandre.torgue@foss.st.com>
To: Jonathan Cameron <jic23@kernel.org>
To: David Lechner <dlechner@baylibre.com>
To: Nuno Sá <nuno.sa@analog.com>
To: Andy Shevchenko <andy@kernel.org>
Cc: Greg Ungerer <gerg@uclinux.org>
Cc: linux-m68k@lists.linux-m68k.org
Cc: linux-kernel@vger.kernel.org
Cc: linux-stm32@st-md-mailman.stormreply.com
Cc: linux-arm-kernel@lists.infradead.org
Cc: linux-iio@vger.kernel.org
---
Angelo Dureghello (11):
m68k: mcf5441x: fix clocks numbering
m68k: mcf5441x: add clock for DAC channel 1
m68k: mcf5441x: initialize DAC clocks by iio DAC driver name
m68k: defconfig: update stmark2 defconfig
m68k: add DAC modules base addresses
m68k: mcf5441x: add CCM registers
m68k: mcf5441x: add CCR MISCCR2 bitfields
m68k: stmark2: use ioport.h macros for resources
m68k: stmark2: add mcf5441x DAC platform devices
m68k: stmark2: enable DACs outputs
iio: dac: add mcf54415 DAC
arch/m68k/coldfire/m5441x.c | 21 ++--
arch/m68k/coldfire/stmark2.c | 47 +++++---
arch/m68k/configs/stmark2_defconfig | 2 +
arch/m68k/include/asm/m5441xsim.h | 42 ++++++++
drivers/iio/dac/Kconfig | 11 ++
drivers/iio/dac/Makefile | 1 +
drivers/iio/dac/mcf54415_dac.c | 207 ++++++++++++++++++++++++++++++++++++
7 files changed, 307 insertions(+), 24 deletions(-)
---
base-commit: 9edf4183f0d32146b708220d56ee2d35905ba516
change-id: 20260430-wip-stmark2-dac-7060f49dd94f
Best regards,
--
Angelo Dureghello <adureghello@baylibre.com>
^ permalink raw reply
* [PATCH v3 02/11] m68k: mcf5441x: add clock for DAC channel 1
From: Angelo Dureghello @ 2026-05-22 21:20 UTC (permalink / raw)
To: Greg Ungerer, Geert Uytterhoeven, Steven King, Arnd Bergmann,
Maxime Coquelin, Alexandre Torgue, Jonathan Cameron,
David Lechner, Nuno Sá, Andy Shevchenko
Cc: Greg Ungerer, linux-m68k, linux-kernel, linux-stm32,
linux-arm-kernel, linux-iio, Angelo Dureghello
In-Reply-To: <20260522-wip-stmark2-dac-v3-0-16be0ad35a67@baylibre.com>
From: Angelo Dureghello <adureghello@baylibre.com>
Add missing clock for mcf5441x DAC channel 1.
Signed-off-by: Angelo Dureghello <adureghello@baylibre.com>
---
arch/m68k/coldfire/m5441x.c | 3 +++
1 file changed, 3 insertions(+)
diff --git a/arch/m68k/coldfire/m5441x.c b/arch/m68k/coldfire/m5441x.c
index 613b0275d9d8..5b5e09ecf487 100644
--- a/arch/m68k/coldfire/m5441x.c
+++ b/arch/m68k/coldfire/m5441x.c
@@ -44,6 +44,7 @@ DEFINE_CLK(0, "mcfpit.3", 35, MCF_BUSCLK);
DEFINE_CLK(0, "mcfeport.0", 36, MCF_CLK);
DEFINE_CLK(0, "mcfadc.0", 37, MCF_CLK);
DEFINE_CLK(0, "mcfdac.0", 38, MCF_CLK);
+DEFINE_CLK(0, "mcfdac.1", 39, MCF_CLK);
DEFINE_CLK(0, "mcfrtc.0", 42, MCF_CLK);
DEFINE_CLK(0, "mcfsim.0", 43, MCF_CLK);
DEFINE_CLK(0, "mcfusb-otg.0", 44, MCF_CLK);
@@ -106,6 +107,7 @@ static struct clk_lookup m5411x_clk_lookup[] = {
CLKDEV_INIT("mcfeport.0", NULL, &__clk_0_36),
CLKDEV_INIT("mcfadc.0", NULL, &__clk_0_37),
CLKDEV_INIT("mcfdac.0", NULL, &__clk_0_38),
+ CLKDEV_INIT("mcfdac.1", NULL, &__clk_0_39),
CLKDEV_INIT("mcfrtc.0", NULL, &__clk_0_42),
CLKDEV_INIT("mcfsim.0", NULL, &__clk_0_43),
CLKDEV_INIT("mcfusb-otg.0", NULL, &__clk_0_44),
@@ -176,6 +178,7 @@ static struct clk * const disable_clks[] __initconst = {
&__clk_0_35, /* pit.3 */
&__clk_0_37, /* adc */
&__clk_0_38, /* dac.0 */
+ &__clk_0_39, /* dac.1 */
&__clk_0_44, /* usb otg */
&__clk_0_45, /* usb host */
&__clk_0_47, /* ssi.0 */
--
2.54.0
^ permalink raw reply related
* [PATCH v9 16/23] perf python: Add mmap2 event
From: Ian Rogers @ 2026-05-22 22:04 UTC (permalink / raw)
To: irogers, acme, namhyung
Cc: adrian.hunter, alice.mei.rogers, dapeng1.mi, james.clark, leo.yan,
linux-arm-kernel, linux-kernel, linux-perf-users, mingo, peterz,
tmricht
In-Reply-To: <20260522220435.2378363-1-irogers@google.com>
If mmap is handled so should mmap2 events. Add support as a distinct
python event type.
Assisted-by: Gemini:gemini-3.1-pro-preview
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/util/python.c | 60 ++++++++++++++++++++++++++++++++++++++++
1 file changed, 60 insertions(+)
diff --git a/tools/perf/util/python.c b/tools/perf/util/python.c
index 029915b7a1fe..daedd67f12d5 100644
--- a/tools/perf/util/python.c
+++ b/tools/perf/util/python.c
@@ -153,6 +153,54 @@ static PyTypeObject pyrf_mmap_event__type = {
.tp_repr = (reprfunc)pyrf_mmap_event__repr,
};
+static const char pyrf_mmap2_event__doc[] = PyDoc_STR("perf mmap2 event object.");
+
+static PyMemberDef pyrf_mmap2_event__members[] = {
+ sample_members
+ member_def(perf_event_header, type, T_UINT, "event type"),
+ member_def(perf_event_header, misc, T_UINT, "event misc"),
+ member_def(perf_record_mmap2, pid, T_UINT, "event pid"),
+ member_def(perf_record_mmap2, tid, T_UINT, "event tid"),
+ member_def(perf_record_mmap2, start, T_ULONGLONG, "start of the map"),
+ member_def(perf_record_mmap2, len, T_ULONGLONG, "map length"),
+ member_def(perf_record_mmap2, pgoff, T_ULONGLONG, "page offset"),
+ member_def(perf_record_mmap2, prot, T_UINT, "protection"),
+ member_def(perf_record_mmap2, flags, T_UINT, "flags"),
+ member_def(perf_record_mmap2, filename, T_STRING_INPLACE, "backing store"),
+ { .name = NULL, },
+};
+
+static PyObject *pyrf_mmap2_event__repr(const struct pyrf_event *pevent)
+{
+ PyObject *ret;
+ char *s;
+
+ if (asprintf(&s, "{ type: mmap2, pid: %u, tid: %u, start: %#" PRI_lx64 ", "
+ "length: %#" PRI_lx64 ", offset: %#" PRI_lx64 ", "
+ "flags: %#x, prot: %#x, filename: %s }",
+ pevent->event.mmap2.pid, pevent->event.mmap2.tid,
+ pevent->event.mmap2.start, pevent->event.mmap2.len,
+ pevent->event.mmap2.pgoff, pevent->event.mmap2.flags,
+ pevent->event.mmap2.prot, pevent->event.mmap2.filename) < 0)
+ return PyErr_NoMemory();
+
+ ret = PyUnicode_FromString(s);
+ free(s);
+ return ret;
+}
+
+static PyTypeObject pyrf_mmap2_event__type = {
+ PyVarObject_HEAD_INIT(NULL, 0)
+ .tp_name = "perf.mmap2_event",
+ .tp_basicsize = sizeof(struct pyrf_event),
+ .tp_dealloc = (destructor)pyrf_event__delete,
+ .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
+ .tp_doc = pyrf_mmap2_event__doc,
+ .tp_members = pyrf_mmap2_event__members,
+ .tp_getset = pyrf_event__getset,
+ .tp_repr = (reprfunc)pyrf_mmap2_event__repr,
+};
+
static const char pyrf_task_event__doc[] = PyDoc_STR("perf task (fork/exit) event object.");
static PyMemberDef pyrf_task_event__members[] = {
@@ -778,6 +826,9 @@ static int pyrf_event__setup_types(void)
int err;
err = PyType_Ready(&pyrf_mmap_event__type);
+ if (err < 0)
+ goto out;
+ err = PyType_Ready(&pyrf_mmap2_event__type);
if (err < 0)
goto out;
err = PyType_Ready(&pyrf_lost_event__type);
@@ -807,6 +858,7 @@ static int pyrf_event__setup_types(void)
static PyTypeObject *pyrf_event__type[] = {
[PERF_RECORD_MMAP] = &pyrf_mmap_event__type,
+ [PERF_RECORD_MMAP2] = &pyrf_mmap2_event__type,
[PERF_RECORD_LOST] = &pyrf_lost_event__type,
[PERF_RECORD_COMM] = &pyrf_comm_event__type,
[PERF_RECORD_EXIT] = &pyrf_task_event__type,
@@ -973,6 +1025,11 @@ static PyObject *pyrf_event__new(const union perf_event *event, struct evsel *ev
size_t max_len = pevent->event.header.size - offsetof(struct perf_record_mmap, filename);
pevent->event.mmap.filename[max_len - 1] = '\0';
+ } else if (event->header.type == PERF_RECORD_MMAP2) {
+ /* Ensure '\0' string termination. */
+ size_t max_len = pevent->event.header.size - offsetof(struct perf_record_mmap2, filename);
+
+ pevent->event.mmap2.filename[max_len - 1] = '\0';
}
perf_sample__init(&pevent->sample, /*all=*/true);
@@ -3238,6 +3295,9 @@ PyMODINIT_FUNC PyInit_perf(void)
Py_INCREF(&pyrf_mmap_event__type);
PyModule_AddObject(module, "mmap_event", (PyObject *)&pyrf_mmap_event__type);
+ Py_INCREF(&pyrf_mmap2_event__type);
+ PyModule_AddObject(module, "mmap2_event", (PyObject *)&pyrf_mmap2_event__type);
+
Py_INCREF(&pyrf_lost_event__type);
PyModule_AddObject(module, "lost_event", (PyObject *)&pyrf_lost_event__type);
--
2.54.0.794.g4f17f83d09-goog
^ permalink raw reply related
* [PATCH v9 02/23] perf arch x86: Sort includes and add missed explicit dependencies
From: Ian Rogers @ 2026-05-22 22:04 UTC (permalink / raw)
To: irogers, acme, namhyung
Cc: adrian.hunter, alice.mei.rogers, dapeng1.mi, james.clark, leo.yan,
linux-arm-kernel, linux-kernel, linux-perf-users, mingo, peterz,
tmricht
In-Reply-To: <20260522220435.2378363-1-irogers@google.com>
Fix missing #includes found while cleaning the evsel/evlist header
files. Sort the remaining header files for consistency with the rest
of the code.
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/arch/x86/util/intel-bts.c | 20 +++++++++++--------
tools/perf/arch/x86/util/intel-pt.c | 29 +++++++++++++++-------------
2 files changed, 28 insertions(+), 21 deletions(-)
diff --git a/tools/perf/arch/x86/util/intel-bts.c b/tools/perf/arch/x86/util/intel-bts.c
index 85c8186300c8..100a23d27998 100644
--- a/tools/perf/arch/x86/util/intel-bts.c
+++ b/tools/perf/arch/x86/util/intel-bts.c
@@ -4,26 +4,30 @@
* Copyright (c) 2013-2015, Intel Corporation.
*/
+#include "../../../util/intel-bts.h"
+
#include <errno.h>
-#include <linux/kernel.h>
-#include <linux/types.h>
+
#include <linux/bitops.h>
+#include <linux/kernel.h>
#include <linux/log2.h>
+#include <linux/types.h>
#include <linux/zalloc.h>
+#include <internal/lib.h> // page_size
+
+#include "../../../util/auxtrace.h"
#include "../../../util/cpumap.h"
+#include "../../../util/debug.h"
#include "../../../util/event.h"
-#include "../../../util/evsel.h"
#include "../../../util/evlist.h"
+#include "../../../util/evsel.h"
#include "../../../util/mmap.h"
-#include "../../../util/session.h"
+#include "../../../util/pmu.h"
#include "../../../util/pmus.h"
-#include "../../../util/debug.h"
#include "../../../util/record.h"
+#include "../../../util/session.h"
#include "../../../util/tsc.h"
-#include "../../../util/auxtrace.h"
-#include "../../../util/intel-bts.h"
-#include <internal/lib.h> // page_size
#define KiB(x) ((x) * 1024)
#define MiB(x) ((x) * 1024 * 1024)
diff --git a/tools/perf/arch/x86/util/intel-pt.c b/tools/perf/arch/x86/util/intel-pt.c
index c131a727774f..0307ff15d9fc 100644
--- a/tools/perf/arch/x86/util/intel-pt.c
+++ b/tools/perf/arch/x86/util/intel-pt.c
@@ -3,36 +3,39 @@
* intel_pt.c: Intel Processor Trace support
* Copyright (c) 2013-2015, Intel Corporation.
*/
+#include "../../../util/intel-pt.h"
#include <errno.h>
#include <stdbool.h>
-#include <linux/kernel.h>
-#include <linux/types.h>
+
#include <linux/bitops.h>
+#include <linux/err.h>
+#include <linux/kernel.h>
#include <linux/log2.h>
+#include <linux/types.h>
#include <linux/zalloc.h>
-#include <linux/err.h>
-#include "../../../util/session.h"
+#include <api/fs/fs.h>
+#include <internal/lib.h> // page_size
+#include <subcmd/parse-options.h>
+
+#include "../../../util/auxtrace.h"
+#include "../../../util/config.h"
+#include "../../../util/cpumap.h"
+#include "../../../util/debug.h"
#include "../../../util/event.h"
#include "../../../util/evlist.h"
#include "../../../util/evsel.h"
#include "../../../util/evsel_config.h"
-#include "../../../util/config.h"
-#include "../../../util/cpumap.h"
#include "../../../util/mmap.h"
-#include <subcmd/parse-options.h>
#include "../../../util/parse-events.h"
-#include "../../../util/pmus.h"
-#include "../../../util/debug.h"
-#include "../../../util/auxtrace.h"
#include "../../../util/perf_api_probe.h"
+#include "../../../util/pmu.h"
+#include "../../../util/pmus.h"
#include "../../../util/record.h"
+#include "../../../util/session.h"
#include "../../../util/target.h"
#include "../../../util/tsc.h"
-#include <internal/lib.h> // page_size
-#include "../../../util/intel-pt.h"
-#include <api/fs/fs.h>
#include "cpuid.h"
#define KiB(x) ((x) * 1024)
--
2.54.0.794.g4f17f83d09-goog
^ permalink raw reply related
* Re: [PATCH RFC v2 0/2] arm64: vdso: Implement __vdso_futex_robust_try_unlock()
From: André Almeida @ 2026-05-22 22:11 UTC (permalink / raw)
To: Sebastian Andrzej Siewior
Cc: Catalin Marinas, Will Deacon, Thomas Gleixner, Mark Rutland,
Mathieu Desnoyers, Carlos O'Donell, Peter Zijlstra,
Florian Weimer, Rich Felker, Torvald Riegel, Darren Hart,
Ingo Molnar, Davidlohr Bueso, Arnd Bergmann, Liam R . Howlett,
Uros Bizjak, Thomas Weißschuh, linux-arm-kernel,
linux-kernel, linux-arch, kernel-dev
In-Reply-To: <20260428110054.oQNT-kMn@linutronix.de>
Hi Sebastian,
Em 28/04/2026 08:00, Sebastian Andrzej Siewior escreveu:
> On 2026-04-24 15:55:59 [-0300], André Almeida wrote:
>> Hi folks,
> Hi,
>
>> This is my take on implementing the new vDSO for unlocking a robust futex in
>> arm64. If you don't know what's that, Thomas wrote a good summary,
>> including the motivation for this work and the x86 implementation:
>>
>> https://lore.kernel.org/lkml/878qb89g7b.ffs@tglx/
>>
>> There are some loose ends in my patchset so I'm sending as a RFC to ask
>> some questions:
>>
>> - If the process is interrupted between the labels, we need to check the
>> conditional flags and clear the op_pending address from the register. Using
>> objdump I see that op_pending addr is being stored at x2, but I suspect that
>> this isn't stable, so I need to figure out how to make sure that the address
>> will always be stored in the same register.
>> - So far I have implemented only the LL/SC version to make review easier, but I
>> can do the LSE version as well.
>
> I am a bit behind here. In the meantime, were you able to check it with
> https://lore.kernel.org/all/20260404093939.7XgeW_54@linutronix.de/
> ?
Sorry for my delay. Yes, I had to do some modifications for the test
(will reply to your patch) and spot one error in my patch, and now the
test passes, thanks! Will send a v3 for this soon.
^ permalink raw reply
* Re: [PATCH 1/8] mm: Add ptep_try_set() for lockless empty-slot installs
From: David Hildenbrand (Arm) @ 2026-05-22 22:07 UTC (permalink / raw)
To: Tejun Heo, David Vernet, Andrea Righi, Changwoo Min,
Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
Martin KaFai Lau, Kumar Kartikeya Dwivedi
Cc: Peter Zijlstra, Catalin Marinas, Will Deacon, Thomas Gleixner,
Ingo Molnar, Borislav Petkov, Dave Hansen, Andrew Morton,
Mike Rapoport, Emil Tsalapatis, sched-ext, bpf, x86,
linux-arm-kernel, linux-mm, linux-kernel
In-Reply-To: <20260522172219.1423324-2-tj@kernel.org>
>
> +#ifndef ptep_try_set
> +/**
> + * ptep_try_set - atomically set an empty kernel PTE
> + * @ptep: page table entry
> + * @new_pte: value to install
> + *
> + * Atomically set *@ptep to @new_pte iff *@ptep is pte_none(). Return true on
> + * success, false if the slot was already populated or the arch has no
> + * implementation.
> + *
> + * For special kernel page tables only - never user page tables. The caller must
> + * prevent concurrent teardown of @ptep and must accept that other writers may
> + * race. Concurrent clearers must use ptep_get_and_clear() so racing accesses
> + * agree on the outcome.
> + *
> + * Architectures opt in by providing a cmpxchg-based override and defining
> + * ptep_try_set as an identity macro. The generic stub returns false, which is
> + * correct for callers that fall through to oops on failure.
> + */
> +static inline bool ptep_try_set(pte_t *ptep, pte_t new_pte)
There are only a handful of valid use cases for this (and the "fall through to
oops on failure") makes this very clear.
So I'm fine with this, assuming there won't really be a lot of other users
besides ebpf.
Acked-by: David Hildenbrand (arm) <david@kernel.org>
--
Cheers,
David
^ permalink raw reply
* [PATCH v9 23/23] perf python: Add LiveSession helper
From: Ian Rogers @ 2026-05-22 22:04 UTC (permalink / raw)
To: irogers, acme, namhyung
Cc: adrian.hunter, alice.mei.rogers, dapeng1.mi, james.clark, leo.yan,
linux-arm-kernel, linux-kernel, linux-perf-users, mingo, peterz,
tmricht
In-Reply-To: <20260522220435.2378363-1-irogers@google.com>
Add LiveSession class in tools/perf/python/perf_live.py to support
live event collection using perf.evlist and perf.parse_events,
avoiding the need to fork a separate perf record process.
Signed-off-by: Ian Rogers <irogers@google.com>
Assisted-by: Gemini:gemini-3.1-pro-preview
---
v2:
1. Fixed File Descriptor Leak: I moved self.evlist.mmap() inside the
try block so that if it raises an exception, the finally block will
still be executed and call self.evlist.close() , preventing file
descriptor leaks.
2. Handled InterruptedError in poll() : I wrapped the poll() call in a
try-except block to catch InterruptedError and continue the
loop. This prevents the live session from crashing on non-fatal
signals like SIGWINCH .
3. Added evlist.config() : I added a call to self.evlist.config() in
the constructor after parse_events() . This applies the default
record options to the events, enabling sampling and setting up
PERF_SAMPLE_* fields so that the kernel will actually generate
PERF_RECORD_SAMPLE events.
4. Enable the evlist and be robust to exceptions from reading unsupported
events like mmap2.
v8:
- Drain all events from a CPU before moving to the next.
---
tools/perf/python/perf_live.py | 53 ++++++++++++++++++++++++++++++++++
1 file changed, 53 insertions(+)
create mode 100755 tools/perf/python/perf_live.py
diff --git a/tools/perf/python/perf_live.py b/tools/perf/python/perf_live.py
new file mode 100755
index 000000000000..2e5347349760
--- /dev/null
+++ b/tools/perf/python/perf_live.py
@@ -0,0 +1,53 @@
+# SPDX-License-Identifier: GPL-2.0
+"""
+Live event session helper using perf.evlist.
+
+This module provides a LiveSession class that allows running a callback
+for each event collected live from the system, similar to perf.session
+but without requiring a perf.data file.
+"""
+
+import perf
+
+
+class LiveSession:
+ """Represents a live event collection session."""
+
+ def __init__(self, event_string: str, sample_callback):
+ self.event_string = event_string
+ self.sample_callback = sample_callback
+ # Create a cpu map for all online CPUs
+ self.cpus = perf.cpu_map()
+ # Parse events and set maps
+ self.evlist = perf.parse_events(self.event_string, self.cpus)
+ self.evlist.config()
+
+ def run(self):
+ """Run the live session."""
+ self.evlist.open()
+ try:
+ self.evlist.mmap()
+ self.evlist.enable()
+
+ while True:
+ # Poll for events with 100ms timeout
+ try:
+ self.evlist.poll(100)
+ except InterruptedError:
+ continue
+ for cpu in self.cpus:
+ while True:
+ try:
+ event = self.evlist.read_on_cpu(cpu)
+ if event is None:
+ break
+ if event.type == perf.RECORD_SAMPLE:
+ self.sample_callback(event)
+ except Exception as e:
+ import sys
+ print(f"Error processing event on CPU {cpu}: {e}", file=sys.stderr)
+ break
+ except KeyboardInterrupt:
+ pass
+ finally:
+ self.evlist.close()
--
2.54.0.794.g4f17f83d09-goog
^ permalink raw reply related
* [PATCH v9 18/23] perf python: Extend API for stat events in python.c
From: Ian Rogers @ 2026-05-22 22:04 UTC (permalink / raw)
To: irogers, acme, namhyung
Cc: adrian.hunter, alice.mei.rogers, dapeng1.mi, james.clark, leo.yan,
linux-arm-kernel, linux-kernel, linux-perf-users, mingo, peterz,
tmricht
In-Reply-To: <20260522220435.2378363-1-irogers@google.com>
Add stat information to the session. Add call backs for stat events.
Assisted-by: Gemini:gemini-3.1-pro-preview
Signed-off-by: Ian Rogers <irogers@google.com>
---
v5:
1. Fix Memory Corruption: Corrected the memory offset for `stat_round_type`
in `pyrf_stat_round_event__members` by adding the base offset of
`struct pyrf_event`.
2. Fix Memory Leak: Added `Py_XDECREF()` to free the unused return value
of the Python callback in `pyrf_session_tool__stat_round()`.
---
v7:
- Added comprehensive size checks in pyrf_event__new for all event
types.
- Fixed line length warning.
---
tools/perf/util/python.c | 180 +++++++++++++++++++++++++++++++++++++--
1 file changed, 173 insertions(+), 7 deletions(-)
diff --git a/tools/perf/util/python.c b/tools/perf/util/python.c
index de7a51389572..4ee3fdbf35de 100644
--- a/tools/perf/util/python.c
+++ b/tools/perf/util/python.c
@@ -343,6 +343,77 @@ static PyTypeObject pyrf_lost_event__type = {
.tp_repr = (reprfunc)pyrf_lost_event__repr,
};
+static const char pyrf_stat_event__doc[] = PyDoc_STR("perf stat event object.");
+
+static PyMemberDef pyrf_stat_event__members[] = {
+ sample_members
+ member_def(perf_event_header, type, T_UINT, "event type"),
+ member_def(perf_record_stat, id, T_ULONGLONG, "event id"),
+ member_def(perf_record_stat, cpu, T_UINT, "event cpu"),
+ member_def(perf_record_stat, thread, T_UINT, "event thread"),
+ member_def(perf_record_stat, val, T_ULONGLONG, "counter value"),
+ member_def(perf_record_stat, ena, T_ULONGLONG, "enabled time"),
+ member_def(perf_record_stat, run, T_ULONGLONG, "running time"),
+ { .name = NULL, },
+};
+
+static PyObject *pyrf_stat_event__repr(const struct pyrf_event *pevent)
+{
+ return PyUnicode_FromFormat(
+ "{ type: stat, id: %llu, cpu: %u, thread: %u, val: %llu, ena: %llu, run: %llu }",
+ pevent->event.stat.id,
+ pevent->event.stat.cpu,
+ pevent->event.stat.thread,
+ pevent->event.stat.val,
+ pevent->event.stat.ena,
+ pevent->event.stat.run);
+}
+
+static PyTypeObject pyrf_stat_event__type = {
+ PyVarObject_HEAD_INIT(NULL, 0)
+ .tp_name = "perf.stat_event",
+ .tp_basicsize = sizeof(struct pyrf_event),
+ .tp_new = PyType_GenericNew,
+ .tp_dealloc = (destructor)pyrf_event__delete,
+ .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
+ .tp_doc = pyrf_stat_event__doc,
+ .tp_members = pyrf_stat_event__members,
+ .tp_getset = pyrf_event__getset,
+ .tp_repr = (reprfunc)pyrf_stat_event__repr,
+};
+
+static const char pyrf_stat_round_event__doc[] = PyDoc_STR("perf stat round event object.");
+
+static PyMemberDef pyrf_stat_round_event__members[] = {
+ sample_members
+ member_def(perf_event_header, type, T_UINT, "event type"),
+ { .name = "stat_round_type", .type = T_ULONGLONG,
+ .offset = offsetof(struct pyrf_event, event) + offsetof(struct perf_record_stat_round, type),
+ .doc = "round type" },
+ member_def(perf_record_stat_round, time, T_ULONGLONG, "round time"),
+ { .name = NULL, },
+};
+
+static PyObject *pyrf_stat_round_event__repr(const struct pyrf_event *pevent)
+{
+ return PyUnicode_FromFormat("{ type: stat_round, type: %llu, time: %llu }",
+ pevent->event.stat_round.type,
+ pevent->event.stat_round.time);
+}
+
+static PyTypeObject pyrf_stat_round_event__type = {
+ PyVarObject_HEAD_INIT(NULL, 0)
+ .tp_name = "perf.stat_round_event",
+ .tp_basicsize = sizeof(struct pyrf_event),
+ .tp_new = PyType_GenericNew,
+ .tp_dealloc = (destructor)pyrf_event__delete,
+ .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
+ .tp_doc = pyrf_stat_round_event__doc,
+ .tp_members = pyrf_stat_round_event__members,
+ .tp_getset = pyrf_event__getset,
+ .tp_repr = (reprfunc)pyrf_stat_round_event__repr,
+};
+
static const char pyrf_read_event__doc[] = PyDoc_STR("perf read event object.");
static PyMemberDef pyrf_read_event__members[] = {
@@ -1034,6 +1105,12 @@ static int pyrf_event__setup_types(void)
if (err < 0)
goto out;
err = PyType_Ready(&pyrf_context_switch_event__type);
+ if (err < 0)
+ goto out;
+ err = PyType_Ready(&pyrf_stat_event__type);
+ if (err < 0)
+ goto out;
+ err = PyType_Ready(&pyrf_stat_round_event__type);
if (err < 0)
goto out;
err = PyType_Ready(&pyrf_callchain_node__type);
@@ -1059,6 +1136,8 @@ static PyTypeObject *pyrf_event__type[] = {
[PERF_RECORD_SAMPLE] = &pyrf_sample_event__type,
[PERF_RECORD_SWITCH] = &pyrf_context_switch_event__type,
[PERF_RECORD_SWITCH_CPU_WIDE] = &pyrf_context_switch_event__type,
+ [PERF_RECORD_STAT] = &pyrf_stat_event__type,
+ [PERF_RECORD_STAT_ROUND] = &pyrf_stat_round_event__type,
};
static PyObject *pyrf_event__new(const union perf_event *event, struct evsel *evsel,
@@ -1091,13 +1170,13 @@ static PyObject *pyrf_event__new(const union perf_event *event, struct evsel *ev
case PERF_RECORD_EXIT:
min_size = sizeof(struct perf_record_fork);
break;
+ case PERF_RECORD_LOST:
+ min_size = sizeof(struct perf_record_lost);
+ break;
case PERF_RECORD_THROTTLE:
case PERF_RECORD_UNTHROTTLE:
min_size = sizeof(struct perf_record_throttle);
break;
- case PERF_RECORD_LOST:
- min_size = sizeof(struct perf_record_lost);
- break;
case PERF_RECORD_READ:
min_size = sizeof(struct perf_record_read);
break;
@@ -2119,7 +2198,40 @@ static PyObject *pyrf_evsel__get_attr_wakeup_events(PyObject *self, void */*clos
return PyLong_FromUnsignedLong(pevsel->evsel->core.attr.wakeup_events);
}
+static PyObject *pyrf_evsel__get_ids(struct pyrf_evsel *pevsel, void *closure __maybe_unused)
+{
+ struct evsel *evsel = pevsel->evsel;
+ PyObject *list = PyList_New(0);
+
+ if (!list)
+ return NULL;
+
+ for (u32 i = 0; i < evsel->core.ids; i++) {
+ PyObject *id = PyLong_FromUnsignedLongLong(evsel->core.id[i]);
+ int ret;
+
+ if (!id) {
+ Py_DECREF(list);
+ return NULL;
+ }
+ ret = PyList_Append(list, id);
+ Py_DECREF(id);
+ if (ret < 0) {
+ Py_DECREF(list);
+ return NULL;
+ }
+ }
+
+ return list;
+}
+
static PyGetSetDef pyrf_evsel__getset[] = {
+ {
+ .name = "ids",
+ .get = (getter)pyrf_evsel__get_ids,
+ .set = NULL,
+ .doc = "event IDs.",
+ },
{
.name = "tracking",
.get = pyrf_evsel__get_tracking,
@@ -2894,6 +3006,8 @@ static const struct perf_constant perf__constants[] = {
PERF_CONST(RECORD_LOST_SAMPLES),
PERF_CONST(RECORD_SWITCH),
PERF_CONST(RECORD_SWITCH_CPU_WIDE),
+ PERF_CONST(RECORD_STAT),
+ PERF_CONST(RECORD_STAT_ROUND),
PERF_CONST(RECORD_MISC_SWITCH_OUT),
{ .name = NULL, },
@@ -3270,6 +3384,7 @@ struct pyrf_session {
struct perf_tool tool;
struct pyrf_data *pdata;
PyObject *sample;
+ PyObject *stat;
};
static int pyrf_session_tool__sample(const struct perf_tool *tool,
@@ -3294,6 +3409,50 @@ static int pyrf_session_tool__sample(const struct perf_tool *tool,
return 0;
}
+static int pyrf_session_tool__stat(const struct perf_tool *tool,
+ struct perf_session *session,
+ union perf_event *event)
+{
+ struct pyrf_session *psession = container_of(tool, struct pyrf_session, tool);
+ struct evsel *evsel = evlist__id2evsel(session->evlist, event->stat.id);
+ PyObject *pyevent = pyrf_event__new(event, evsel, psession->session);
+ const char *name = evsel ? evsel__name(evsel) : "unknown";
+ PyObject *ret;
+
+ if (pyevent == NULL)
+ return -ENOMEM;
+
+ ret = PyObject_CallFunction(psession->stat, "Os", pyevent, name);
+ if (!ret) {
+ Py_DECREF(pyevent);
+ return -1;
+ }
+ Py_DECREF(ret);
+ Py_DECREF(pyevent);
+ return 0;
+}
+
+static int pyrf_session_tool__stat_round(const struct perf_tool *tool,
+ struct perf_session *session __maybe_unused,
+ union perf_event *event)
+{
+ struct pyrf_session *psession = container_of(tool, struct pyrf_session, tool);
+ PyObject *pyevent = pyrf_event__new(event, /*evsel=*/NULL, psession->session);
+ PyObject *ret;
+
+ if (pyevent == NULL)
+ return -ENOMEM;
+
+ ret = PyObject_CallFunction(psession->stat, "O", pyevent);
+ if (!ret) {
+ Py_DECREF(pyevent);
+ return -1;
+ }
+ Py_DECREF(ret);
+ Py_DECREF(pyevent);
+ return 0;
+}
+
static PyObject *pyrf_session__find_thread(struct pyrf_session *psession, PyObject *args)
{
struct machine *machine;
@@ -3325,13 +3484,13 @@ static PyObject *pyrf_session__find_thread(struct pyrf_session *psession, PyObje
static PyObject *pyrf_session__new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
{
struct pyrf_data *pdata;
- PyObject *sample = NULL;
- static char *kwlist[] = { "data", "sample", NULL };
+ PyObject *sample = NULL, *stat = NULL;
+ static char *kwlist[] = { "data", "sample", "stat", NULL };
struct pyrf_session *psession;
struct perf_session *session;
- if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!|O", kwlist, &pyrf_data__type, &pdata,
- &sample))
+ if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!|OO", kwlist, &pyrf_data__type, &pdata,
+ &sample, &stat))
return NULL;
psession = PyObject_New(struct pyrf_session, type);
@@ -3340,6 +3499,7 @@ static PyObject *pyrf_session__new(PyTypeObject *type, PyObject *args, PyObject
psession->session = NULL;
psession->sample = NULL;
+ psession->stat = NULL;
psession->pdata = NULL;
Py_INCREF(pdata);
@@ -3362,8 +3522,13 @@ static PyObject *pyrf_session__new(PyTypeObject *type, PyObject *args, PyObject
} while (0)
ADD_TOOL(sample);
+ ADD_TOOL(stat);
#undef ADD_TOOL
+ if (stat)
+ psession->tool.stat_round = pyrf_session_tool__stat_round;
+
+
psession->tool.comm = perf_event__process_comm;
psession->tool.mmap = perf_event__process_mmap;
psession->tool.mmap2 = perf_event__process_mmap2;
@@ -3403,6 +3568,7 @@ static void pyrf_session__delete(struct pyrf_session *psession)
perf_session__delete(psession->session);
Py_XDECREF(psession->pdata);
Py_XDECREF(psession->sample);
+ Py_XDECREF(psession->stat);
Py_TYPE(psession)->tp_free((PyObject *)psession);
}
--
2.54.0.794.g4f17f83d09-goog
^ permalink raw reply related
* [PATCH v9 22/23] perf python: Add perf.pyi stubs file
From: Ian Rogers @ 2026-05-22 22:04 UTC (permalink / raw)
To: irogers, acme, namhyung
Cc: adrian.hunter, alice.mei.rogers, dapeng1.mi, james.clark, leo.yan,
linux-arm-kernel, linux-kernel, linux-perf-users, mingo, peterz,
tmricht
In-Reply-To: <20260522220435.2378363-1-irogers@google.com>
Add Python type stubs for the perf module to improve IDE support and
static analysis. Includes docstrings for classes, methods, and
constants derived from C source and JSON definitions.
Assisted-by: Gemini:gemini-3.1-pro-preview
Signed-off-by: Ian Rogers <irogers@google.com>
---
v2:
1. Added Missing Module Functions: Added parse_metrics and
pmus. Renamed metrics to parse_metrics to match python.c .
2. Added Constructors: Added __init__ methods for data , evsel , and
evlist with their appropriate arguments.
3. Removed sample_comm : Removed it from sample_event since it is not
exported in python.c .
4. Keyword Handling in branch_entry : I used from_ip and to_ip in the
stubs to match the rename I did in python.c (in turn 145) to avoid
the Python from keyword conflict.
5. Added Missing Event Classes: Added mmap_event , lost_event ,
comm_event , task_event , throttle_event , read_event , and
switch_event .
6. Added Missing evlist Methods: Added get_pollfd and add .
7. Updated Return Types: Changed process_events to return int .
v6:
- Updated `perf.pyi` to use `find_thread` and `elf_machine`.
v8:
- Added mmap2_event class and new evsel attributes to perf.pyi.
- Added pid, tid, ppid, cpu attributes to class thread in perf.pyi.
---
tools/perf/python/perf.pyi | 605 +++++++++++++++++++++++++++++++++++++
1 file changed, 605 insertions(+)
create mode 100644 tools/perf/python/perf.pyi
diff --git a/tools/perf/python/perf.pyi b/tools/perf/python/perf.pyi
new file mode 100644
index 000000000000..1d1d2a833f10
--- /dev/null
+++ b/tools/perf/python/perf.pyi
@@ -0,0 +1,605 @@
+"""Type stubs for the perf Python module."""
+from typing import Callable, Dict, List, Optional, Any, Iterator
+
+def config_get(name: str) -> Optional[str]:
+ """Get a configuration value from perf config.
+
+ Args:
+ name: The configuration variable name (e.g., 'colors.top').
+
+ Returns:
+ The configuration value as a string, or None if not set.
+ """
+ ...
+
+def metrics() -> List[Dict[str, str]]:
+ """Get a list of available metrics.
+
+ Returns:
+ A list of dictionaries, each describing a metric.
+ """
+ ...
+
+def syscall_name(sc_id: int, *, elf_machine: Optional[int] = None) -> str:
+ """Convert a syscall number to its name.
+
+ Args:
+ sc_id: The syscall number.
+ elf_machine: Optional ELF machine type.
+
+ Returns:
+ The name of the syscall.
+ """
+ ...
+
+def syscall_id(name: str, *, elf_machine: Optional[int] = None) -> int:
+ """Convert a syscall name to its number.
+
+ Args:
+ name: The syscall name.
+ elf_machine: Optional ELF machine type.
+
+ Returns:
+ The number of the syscall.
+ """
+ ...
+
+def parse_events(
+ event_string: str,
+ cpus: Optional[cpu_map] = None,
+ threads: Optional[Any] = None
+) -> 'evlist':
+ """Parse an event string and return an evlist.
+
+ Args:
+ event_string: The event string (e.g., 'cycles,instructions').
+ cpus: Optional CPU map to bind events to.
+ threads: Optional thread map to bind events to.
+
+ Returns:
+ An evlist containing the parsed events.
+ """
+ ...
+
+def parse_metrics(metrics_string: str) -> 'evlist':
+ """Parse a string of metrics or metric groups and return an evlist."""
+ ...
+
+def pmus() -> Iterator[Any]:
+ """Returns a sequence of pmus."""
+ ...
+
+class data:
+ """Represents a perf data file."""
+ def __init__(self, path: str = ..., fd: int = ...) -> None: ...
+
+class thread:
+ """Represents a thread in the system."""
+ def comm(self) -> str:
+ """Get the command name of the thread."""
+ ...
+ pid: int
+ tid: int
+ ppid: int
+ cpu: int
+
+class counts_values:
+ """Raw counter values."""
+ val: int
+ ena: int
+ run: int
+
+class thread_map:
+ """Map of threads being monitored."""
+ def __init__(self, pid: int = -1, tid: int = -1) -> None:
+ """Initialize a thread map.
+
+ Args:
+ pid: Process ID to monitor (-1 for all).
+ tid: Thread ID to monitor (-1 for all).
+ """
+ ...
+ def __len__(self) -> int: ...
+ def __getitem__(self, index: int) -> int: ...
+ def __iter__(self) -> Iterator[int]: ...
+
+class evsel:
+ """Event selector, represents a single event being monitored."""
+ def __init__(
+ self,
+ type: int = ...,
+ config: int = ...,
+ sample_freq: int = ...,
+ sample_period: int = ...,
+ sample_type: int = ...,
+ read_format: int = ...,
+ disabled: bool = ...,
+ inherit: bool = ...,
+ pinned: bool = ...,
+ exclusive: bool = ...,
+ exclude_user: bool = ...,
+ exclude_kernel: bool = ...,
+ exclude_hv: bool = ...,
+ exclude_idle: bool = ...,
+ mmap: bool = ...,
+ context_switch: bool = ...,
+ comm: bool = ...,
+ freq: bool = ...,
+ idx: int = ...,
+ ) -> None: ...
+ def __str__(self) -> str:
+ """Return string representation of the event."""
+ ...
+ def open(self) -> None:
+ """Open the event selector file descriptor table."""
+ ...
+ def read(self, cpu: int, thread: int) -> counts_values:
+ """Read counter values for a specific CPU and thread."""
+ ...
+ ids: List[int]
+ def cpus(self) -> cpu_map:
+ """Get CPU map for this event."""
+ ...
+ def threads(self) -> thread_map:
+ """Get thread map for this event."""
+ ...
+ tracking: bool
+ config: int
+ read_format: int
+ sample_period: int
+ sample_type: int
+ size: int
+ type: int
+ wakeup_events: int
+
+
+class sample_event:
+ """Represents a sample event from perf."""
+ evsel: evsel
+ sample_cpu: int
+ sample_time: int
+ sample_pid: int
+ type: int
+ brstack: Optional['branch_stack']
+ callchain: Optional['callchain']
+ def __getattr__(self, name: str) -> Any: ...
+
+class mmap_event:
+ """Represents a mmap event from perf."""
+ type: int
+ pid: int
+ tid: int
+ addr: int
+ len: int
+ pgoff: int
+ filename: str
+
+class mmap2_event:
+ """Represents a mmap2 event from perf."""
+ type: int
+ pid: int
+ tid: int
+ addr: int
+ len: int
+ pgoff: int
+ prot: int
+ flags: int
+ filename: str
+
+class lost_event:
+ """Represents a lost events record."""
+ type: int
+ id: int
+ lost: int
+
+class comm_event:
+ """Represents a COMM record."""
+ type: int
+ pid: int
+ tid: int
+ comm: str
+
+class task_event:
+ """Represents an EXIT or FORK record."""
+ type: int
+ pid: int
+ ppid: int
+ tid: int
+ ptid: int
+ time: int
+
+class throttle_event:
+ """Represents a THROTTLE or UNTHROTTLE record."""
+ type: int
+ time: int
+ id: int
+ stream_id: int
+
+class read_event:
+ """Represents a READ record."""
+ type: int
+ pid: int
+ tid: int
+ value: int
+
+class switch_event:
+ """Represents a SWITCH or SWITCH_CPU_WIDE record."""
+ type: int
+
+class branch_entry:
+ """Represents a branch entry in the branch stack.
+
+ Attributes:
+ from_ip: Source address of the branch (corresponds to 'from' keyword in C).
+ to_ip: Destination address of the branch.
+ mispred: True if the branch was mispredicted.
+ predicted: True if the branch was predicted.
+ in_tx: True if the branch was in a transaction.
+ abort: True if the branch was an abort.
+ cycles: Number of cycles since the last branch.
+ type: Type of branch.
+ """
+ from_ip: int
+ to_ip: int
+ mispred: bool
+ predicted: bool
+ in_tx: bool
+ abort: bool
+ cycles: int
+ type: int
+
+class branch_stack:
+ """Iterator over branch entries in the branch stack."""
+ def __iter__(self) -> Iterator[branch_entry]: ...
+ def __next__(self) -> branch_entry: ...
+
+class callchain_node:
+ """Represents a frame in the callchain."""
+ ip: int
+ sym: Optional[Any]
+ map: Optional[Any]
+
+class callchain:
+ """Iterator over callchain frames."""
+ def __iter__(self) -> Iterator[callchain_node]: ...
+ def __next__(self) -> callchain_node: ...
+
+class stat_event:
+ """Represents a stat event from perf."""
+ type: int
+ id: int
+ cpu: int
+ thread: int
+ val: int
+ ena: int
+ run: int
+
+class stat_round_event:
+ """Represents a stat round event from perf."""
+ type: int
+ time: int
+
+class cpu_map:
+ """Map of CPUs being monitored."""
+ def __init__(self, cpustr: Optional[str] = None) -> None: ...
+ def __len__(self) -> int: ...
+ def __getitem__(self, index: int) -> int: ...
+ def __iter__(self) -> Iterator[int]: ...
+
+
+class evlist:
+ def __init__(self, cpus: cpu_map, threads: thread_map) -> None: ...
+ def open(self) -> None:
+ """Open the events in the list."""
+ ...
+ def close(self) -> None:
+ """Close the events in the list."""
+ ...
+ def mmap(self) -> None:
+ """Memory map the event buffers."""
+ ...
+ def poll(self, timeout: int) -> int:
+ """Poll for events.
+
+ Args:
+ timeout: Timeout in milliseconds.
+
+ Returns:
+ Number of events ready.
+ """
+ ...
+ def read_on_cpu(self, cpu: int) -> Optional[sample_event]:
+ """Read a sample event from a specific CPU.
+
+ Args:
+ cpu: The CPU number.
+
+ Returns:
+ A sample_event if available, or None.
+ """
+ ...
+ def all_cpus(self) -> cpu_map:
+ """Get a cpu_map of all CPUs in the system."""
+ ...
+ def metrics(self) -> List[str]:
+ """Get a list of metric names within the evlist."""
+ ...
+ def compute_metric(self, metric: str, cpu: int, thread: int) -> float:
+ """Compute metric for given name, cpu and thread.
+
+ Args:
+ metric: The metric name.
+ cpu: The CPU number.
+ thread: The thread ID.
+
+ Returns:
+ The computed metric value.
+ """
+ ...
+ def config(self) -> None:
+ """Configure the events in the list."""
+ ...
+ def disable(self) -> None:
+ """Disable all events in the list."""
+ ...
+ def enable(self) -> None:
+ """Enable all events in the list."""
+ ...
+ def get_pollfd(self) -> List[int]:
+ """Get a list of file descriptors for polling."""
+ ...
+ def add(self, evsel: evsel) -> int:
+ """Add an event to the list."""
+ ...
+ def __iter__(self) -> Iterator[evsel]:
+ """Iterate over the events (evsel) in the list."""
+ ...
+
+
+class session:
+ def __init__(
+ self,
+ data: data,
+ sample: Optional[Callable[[sample_event], None]] = None,
+ stat: Optional[Callable[[Any, Optional[str]], None]] = None
+ ) -> None:
+ """Initialize a perf session.
+
+ Args:
+ data: The perf data file to read.
+ sample: Callback for sample events.
+ stat: Callback for stat events.
+ """
+ ...
+ def process_events(self) -> int:
+ """Process all events in the session."""
+ ...
+ def find_thread(self, pid: int) -> thread:
+ """Returns the thread associated with a pid."""
+ ...
+
+# Event Types
+TYPE_HARDWARE: int
+"""Hardware event."""
+
+TYPE_SOFTWARE: int
+"""Software event."""
+
+TYPE_TRACEPOINT: int
+"""Tracepoint event."""
+
+TYPE_HW_CACHE: int
+"""Hardware cache event."""
+
+TYPE_RAW: int
+"""Raw hardware event."""
+
+TYPE_BREAKPOINT: int
+"""Breakpoint event."""
+
+
+# Hardware Counters
+COUNT_HW_CPU_CYCLES: int
+"""Total cycles. Be wary of what happens during CPU frequency scaling."""
+
+COUNT_HW_INSTRUCTIONS: int
+"""Retired instructions. Be careful, these can be affected by various issues,
+most notably hardware interrupt counts."""
+
+COUNT_HW_CACHE_REFERENCES: int
+"""Cache accesses. Usually this indicates Last Level Cache accesses but this
+may vary depending on your CPU."""
+
+COUNT_HW_CACHE_MISSES: int
+"""Cache misses. Usually this indicates Last Level Cache misses."""
+
+COUNT_HW_BRANCH_INSTRUCTIONS: int
+"""Retired branch instructions."""
+
+COUNT_HW_BRANCH_MISSES: int
+"""Mispredicted branch instructions."""
+
+COUNT_HW_BUS_CYCLES: int
+"""Bus cycles, which can be different from total cycles."""
+
+COUNT_HW_STALLED_CYCLES_FRONTEND: int
+"""Stalled cycles during issue [This event is an alias of idle-cycles-frontend]."""
+
+COUNT_HW_STALLED_CYCLES_BACKEND: int
+"""Stalled cycles during retirement [This event is an alias of idle-cycles-backend]."""
+
+COUNT_HW_REF_CPU_CYCLES: int
+"""Total cycles; not affected by CPU frequency scaling."""
+
+
+# Cache Counters
+COUNT_HW_CACHE_L1D: int
+"""Level 1 data cache."""
+
+COUNT_HW_CACHE_L1I: int
+"""Level 1 instruction cache."""
+
+COUNT_HW_CACHE_LL: int
+"""Last Level Cache."""
+
+COUNT_HW_CACHE_DTLB: int
+"""Data TLB."""
+
+COUNT_HW_CACHE_ITLB: int
+"""Instruction TLB."""
+
+COUNT_HW_CACHE_BPU: int
+"""Branch Processing Unit."""
+
+COUNT_HW_CACHE_OP_READ: int
+"""Read accesses."""
+
+COUNT_HW_CACHE_OP_WRITE: int
+"""Write accesses."""
+
+COUNT_HW_CACHE_OP_PREFETCH: int
+"""Prefetch accesses."""
+
+COUNT_HW_CACHE_RESULT_ACCESS: int
+"""Accesses."""
+
+COUNT_HW_CACHE_RESULT_MISS: int
+"""Misses."""
+
+
+# Software Counters
+COUNT_SW_CPU_CLOCK: int
+"""CPU clock event."""
+
+COUNT_SW_TASK_CLOCK: int
+"""Task clock event."""
+
+COUNT_SW_PAGE_FAULTS: int
+"""Page faults."""
+
+COUNT_SW_CONTEXT_SWITCHES: int
+"""Context switches."""
+
+COUNT_SW_CPU_MIGRATIONS: int
+"""CPU migrations."""
+
+COUNT_SW_PAGE_FAULTS_MIN: int
+"""Minor page faults."""
+
+COUNT_SW_PAGE_FAULTS_MAJ: int
+"""Major page faults."""
+
+COUNT_SW_ALIGNMENT_FAULTS: int
+"""Alignment faults."""
+
+COUNT_SW_EMULATION_FAULTS: int
+"""Emulation faults."""
+
+COUNT_SW_DUMMY: int
+"""Dummy event."""
+
+
+# Sample Fields
+SAMPLE_IP: int
+"""Instruction pointer."""
+
+SAMPLE_TID: int
+"""Process and thread ID."""
+
+SAMPLE_TIME: int
+"""Timestamp."""
+
+SAMPLE_ADDR: int
+"""Sampled address."""
+
+SAMPLE_READ: int
+"""Read barcode."""
+
+SAMPLE_CALLCHAIN: int
+"""Call chain."""
+
+SAMPLE_ID: int
+"""Unique ID."""
+
+SAMPLE_CPU: int
+"""CPU number."""
+
+SAMPLE_PERIOD: int
+"""Sample period."""
+
+SAMPLE_STREAM_ID: int
+"""Stream ID."""
+
+SAMPLE_RAW: int
+"""Raw sample."""
+
+
+# Format Fields
+FORMAT_TOTAL_TIME_ENABLED: int
+"""Total time enabled."""
+
+FORMAT_TOTAL_TIME_RUNNING: int
+"""Total time running."""
+
+FORMAT_ID: int
+"""Event ID."""
+
+FORMAT_GROUP: int
+"""Event group."""
+
+
+# Record Types
+RECORD_MMAP: int
+"""MMAP record. Contains header, pid, tid, addr, len, pgoff, filename, and sample_id."""
+
+RECORD_LOST: int
+"""Lost events record. Contains header, id, lost count, and sample_id."""
+
+RECORD_COMM: int
+"""COMM record. Contains header, pid, tid, comm, and sample_id."""
+
+RECORD_EXIT: int
+"""EXIT record. Contains header, pid, ppid, tid, ptid, time, and sample_id."""
+
+RECORD_THROTTLE: int
+"""THROTTLE record. Contains header, time, id, stream_id, and sample_id."""
+
+RECORD_UNTHROTTLE: int
+"""UNTHROTTLE record. Contains header, time, id, stream_id, and sample_id."""
+
+RECORD_FORK: int
+"""FORK record. Contains header, pid, ppid, tid, ptid, time, and sample_id."""
+
+RECORD_READ: int
+"""READ record. Contains header, and read values."""
+
+RECORD_SAMPLE: int
+"""SAMPLE record. Contains header, and sample data requested by sample_type."""
+
+RECORD_MMAP2: int
+"""MMAP2 record. Contains header, pid, tid, addr, len, pgoff, maj, min, ino,
+ino_generation, prot, flags, filename, and sample_id."""
+
+RECORD_AUX: int
+"""AUX record. Contains header, aux_offset, aux_size, flags, and sample_id."""
+
+RECORD_ITRACE_START: int
+"""ITRACE_START record. Contains header, pid, tid, and sample_id."""
+
+RECORD_LOST_SAMPLES: int
+"""LOST_SAMPLES record. Contains header, lost count, and sample_id."""
+
+RECORD_SWITCH: int
+"""SWITCH record. Contains header, and sample_id."""
+
+RECORD_SWITCH_CPU_WIDE: int
+"""SWITCH_CPU_WIDE record. Contains header, and sample_id."""
+
+RECORD_STAT: int
+"""STAT record."""
+
+RECORD_STAT_ROUND: int
+"""STAT_ROUND record."""
+
+RECORD_MISC_SWITCH_OUT: int
+"""MISC_SWITCH_OUT record."""
--
2.54.0.794.g4f17f83d09-goog
^ permalink raw reply related
* [PATCH v9 21/23] perf python: Add config file access
From: Ian Rogers @ 2026-05-22 22:04 UTC (permalink / raw)
To: irogers, acme, namhyung
Cc: adrian.hunter, alice.mei.rogers, dapeng1.mi, james.clark, leo.yan,
linux-arm-kernel, linux-kernel, linux-perf-users, mingo, peterz,
tmricht
In-Reply-To: <20260522220435.2378363-1-irogers@google.com>
Add perf.config_get(name) to expose the perf configuration system.
Assisted-by: Gemini:gemini-3.1-pro-preview
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/util/python.c | 20 ++++++++++++++++++++
1 file changed, 20 insertions(+)
diff --git a/tools/perf/util/python.c b/tools/perf/util/python.c
index 23eb65adb392..458aca9db091 100644
--- a/tools/perf/util/python.c
+++ b/tools/perf/util/python.c
@@ -14,6 +14,7 @@
#include "build-id.h"
#include "callchain.h"
#include "comm.h"
+#include "config.h"
#include "counts.h"
#include "data.h"
#include "debug.h"
@@ -3828,7 +3829,26 @@ static PyObject *pyrf__syscall_id(PyObject *self, PyObject *args, PyObject *kwar
return PyLong_FromLong(id);
}
+static PyObject *pyrf__config_get(PyObject *self, PyObject *args)
+{
+ const char *config_name, *val;
+
+ if (!PyArg_ParseTuple(args, "s", &config_name))
+ return NULL;
+
+ val = perf_config_get(config_name);
+ if (!val)
+ Py_RETURN_NONE;
+ return PyUnicode_FromString(val);
+}
+
static PyMethodDef perf__methods[] = {
+ {
+ .ml_name = "config_get",
+ .ml_meth = (PyCFunction) pyrf__config_get,
+ .ml_flags = METH_VARARGS,
+ .ml_doc = PyDoc_STR("Get a perf config value.")
+ },
{
.ml_name = "metrics",
.ml_meth = (PyCFunction) pyrf__metrics,
--
2.54.0.794.g4f17f83d09-goog
^ permalink raw reply related
* [PATCH v9 15/23] perf python: Refactor and add accessors to sample event
From: Ian Rogers @ 2026-05-22 22:04 UTC (permalink / raw)
To: irogers, acme, namhyung
Cc: adrian.hunter, alice.mei.rogers, dapeng1.mi, james.clark, leo.yan,
linux-arm-kernel, linux-kernel, linux-perf-users, mingo, peterz,
tmricht
In-Reply-To: <20260522220435.2378363-1-irogers@google.com>
Add common evsel field for events and move sample specific fields to
only be present in sample events. Add accessors for sample
events. Ensure offsets are within the bounds of the event. Allocate
just enough memory for the copied event, don't make the maximum event
size each time.
Assisted-by: Gemini:gemini-3.1-pro-preview
Signed-off-by: Ian Rogers <irogers@google.com>
---
v5:
1. Fix Uninitialized Memory: Restore zero-initialization of `pevent->sample`
in `pyrf_event__new()` to prevent wild free crashes on error paths.
v6:
- Refactored `pyrf_event__new` to take `evsel` and `session`, and use
dynamic allocation based on event size. Updated callers.
v8:
- Ensure events are properly deallocated.
---
tools/perf/util/python.c | 618 +++++++++++++++++++++++++++++++++------
1 file changed, 534 insertions(+), 84 deletions(-)
diff --git a/tools/perf/util/python.c b/tools/perf/util/python.c
index 18a48779c591..029915b7a1fe 100644
--- a/tools/perf/util/python.c
+++ b/tools/perf/util/python.c
@@ -10,21 +10,28 @@
#include <perf/mmap.h>
#include <structmember.h>
+#include "addr_location.h"
+#include "build-id.h"
#include "callchain.h"
#include "comm.h"
#include "counts.h"
#include "data.h"
#include "debug.h"
+#include "dso.h"
#include "event.h"
#include "evlist.h"
#include "evsel.h"
#include "expr.h"
+#include "map.h"
#include "metricgroup.h"
#include "mmap.h"
#include "pmus.h"
#include "print_binary.h"
#include "record.h"
+#include "sample.h"
#include "session.h"
+#include "srccode.h"
+#include "srcline.h"
#include "strbuf.h"
#include "symbol.h"
#include "thread.h"
@@ -32,7 +39,6 @@
#include "tool.h"
#include "tp_pmu.h"
#include "trace-event.h"
-#include "util/sample.h"
#ifdef HAVE_LIBTRACEEVENT
#include <event-parse.h>
@@ -40,6 +46,8 @@
PyMODINIT_FUNC PyInit_perf(void);
+static PyObject *pyrf_evsel__from_evsel(struct evsel *evsel);
+
#define member_def(type, member, ptype, help) \
{ #member, ptype, \
offsetof(struct pyrf_event, event) + offsetof(struct type, member), \
@@ -52,21 +60,53 @@ PyMODINIT_FUNC PyInit_perf(void);
struct pyrf_event {
PyObject_HEAD
+ /** @sample: The parsed sample from the event. */
struct perf_sample sample;
- union perf_event event;
+ /** @al: The address location from machine__resolve, lazily computed. */
+ struct addr_location al;
+ /** @al_resolved: True when machine__resolve been called. */
+ bool al_resolved;
+ /** @event: The underlying perf_event that may be in a file or ring buffer. */
+ union perf_event event;
};
#define sample_members \
- sample_member_def(sample_ip, ip, T_ULONGLONG, "event ip"), \
sample_member_def(sample_pid, pid, T_INT, "event pid"), \
sample_member_def(sample_tid, tid, T_INT, "event tid"), \
sample_member_def(sample_time, time, T_ULONGLONG, "event timestamp"), \
- sample_member_def(sample_addr, addr, T_ULONGLONG, "event addr"), \
sample_member_def(sample_id, id, T_ULONGLONG, "event id"), \
sample_member_def(sample_stream_id, stream_id, T_ULONGLONG, "event stream id"), \
sample_member_def(sample_period, period, T_ULONGLONG, "event period"), \
sample_member_def(sample_cpu, cpu, T_UINT, "event cpu"),
+static PyObject *pyrf_event__get_evsel(PyObject *self, void *closure __maybe_unused)
+{
+ struct pyrf_event *pevent = (void *)self;
+
+ if (!pevent->sample.evsel)
+ Py_RETURN_NONE;
+
+ return pyrf_evsel__from_evsel(pevent->sample.evsel);
+}
+
+static PyGetSetDef pyrf_event__getset[] = {
+ {
+ .name = "evsel",
+ .get = pyrf_event__get_evsel,
+ .set = NULL,
+ .doc = "tracking event.",
+ },
+ { .name = NULL, },
+};
+
+static void pyrf_event__delete(struct pyrf_event *pevent)
+{
+ if (pevent->al_resolved)
+ addr_location__exit(&pevent->al);
+ perf_sample__exit(&pevent->sample);
+ Py_TYPE(pevent)->tp_free((PyObject *)pevent);
+}
+
static const char pyrf_mmap_event__doc[] = PyDoc_STR("perf mmap event object.");
static PyMemberDef pyrf_mmap_event__members[] = {
@@ -105,9 +145,11 @@ static PyTypeObject pyrf_mmap_event__type = {
PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "perf.mmap_event",
.tp_basicsize = sizeof(struct pyrf_event),
+ .tp_dealloc = (destructor)pyrf_event__delete,
.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
.tp_doc = pyrf_mmap_event__doc,
.tp_members = pyrf_mmap_event__members,
+ .tp_getset = pyrf_event__getset,
.tp_repr = (reprfunc)pyrf_mmap_event__repr,
};
@@ -140,9 +182,11 @@ static PyTypeObject pyrf_task_event__type = {
PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "perf.task_event",
.tp_basicsize = sizeof(struct pyrf_event),
+ .tp_dealloc = (destructor)pyrf_event__delete,
.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
.tp_doc = pyrf_task_event__doc,
.tp_members = pyrf_task_event__members,
+ .tp_getset = pyrf_event__getset,
.tp_repr = (reprfunc)pyrf_task_event__repr,
};
@@ -169,9 +213,11 @@ static PyTypeObject pyrf_comm_event__type = {
PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "perf.comm_event",
.tp_basicsize = sizeof(struct pyrf_event),
+ .tp_dealloc = (destructor)pyrf_event__delete,
.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
.tp_doc = pyrf_comm_event__doc,
.tp_members = pyrf_comm_event__members,
+ .tp_getset = pyrf_event__getset,
.tp_repr = (reprfunc)pyrf_comm_event__repr,
};
@@ -201,9 +247,11 @@ static PyTypeObject pyrf_throttle_event__type = {
PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "perf.throttle_event",
.tp_basicsize = sizeof(struct pyrf_event),
+ .tp_dealloc = (destructor)pyrf_event__delete,
.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
.tp_doc = pyrf_throttle_event__doc,
.tp_members = pyrf_throttle_event__members,
+ .tp_getset = pyrf_event__getset,
.tp_repr = (reprfunc)pyrf_throttle_event__repr,
};
@@ -236,9 +284,11 @@ static PyTypeObject pyrf_lost_event__type = {
PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "perf.lost_event",
.tp_basicsize = sizeof(struct pyrf_event),
+ .tp_dealloc = (destructor)pyrf_event__delete,
.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
.tp_doc = pyrf_lost_event__doc,
.tp_members = pyrf_lost_event__members,
+ .tp_getset = pyrf_event__getset,
.tp_repr = (reprfunc)pyrf_lost_event__repr,
};
@@ -266,9 +316,11 @@ static PyTypeObject pyrf_read_event__type = {
PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "perf.read_event",
.tp_basicsize = sizeof(struct pyrf_event),
+ .tp_dealloc = (destructor)pyrf_event__delete,
.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
.tp_doc = pyrf_read_event__doc,
.tp_members = pyrf_read_event__members,
+ .tp_getset = pyrf_event__getset,
.tp_repr = (reprfunc)pyrf_read_event__repr,
};
@@ -276,16 +328,17 @@ static const char pyrf_sample_event__doc[] = PyDoc_STR("perf sample event object
static PyMemberDef pyrf_sample_event__members[] = {
sample_members
+ sample_member_def(sample_ip, ip, T_ULONGLONG, "event ip"),
+ sample_member_def(sample_addr, addr, T_ULONGLONG, "event addr"),
+ sample_member_def(sample_phys_addr, phys_addr, T_ULONGLONG, "event physical addr"),
+ sample_member_def(sample_weight, weight, T_ULONGLONG, "event weight"),
+ sample_member_def(sample_data_src, data_src, T_ULONGLONG, "event data source"),
+ sample_member_def(sample_insn_count, insn_cnt, T_ULONGLONG, "event instruction count"),
+ sample_member_def(sample_cyc_count, cyc_cnt, T_ULONGLONG, "event cycle count"),
member_def(perf_event_header, type, T_UINT, "event type"),
{ .name = NULL, },
};
-static void pyrf_sample_event__delete(struct pyrf_event *pevent)
-{
- perf_sample__exit(&pevent->sample);
- Py_TYPE(pevent)->tp_free((PyObject*)pevent);
-}
-
static PyObject *pyrf_sample_event__repr(const struct pyrf_event *pevent)
{
PyObject *ret;
@@ -303,6 +356,8 @@ static PyObject *pyrf_sample_event__repr(const struct pyrf_event *pevent)
#ifdef HAVE_LIBTRACEEVENT
static bool is_tracepoint(const struct pyrf_event *pevent)
{
+ if (!pevent->sample.evsel)
+ return false;
return pevent->sample.evsel->core.attr.type == PERF_TYPE_TRACEPOINT;
}
@@ -373,6 +428,199 @@ get_tracepoint_field(struct pyrf_event *pevent, PyObject *attr_name)
}
#endif /* HAVE_LIBTRACEEVENT */
+static int pyrf_sample_event__resolve_al(struct pyrf_event *pevent)
+{
+ struct evsel *evsel = pevent->sample.evsel;
+ struct evlist *evlist = evsel ? evsel->evlist : NULL;
+ struct perf_session *session = evlist ? evlist__session(evlist) : NULL;
+
+ if (pevent->al_resolved)
+ return 0;
+
+ if (!session)
+ return -1;
+
+ addr_location__init(&pevent->al);
+ if (machine__resolve(&session->machines.host, &pevent->al, &pevent->sample) < 0) {
+ addr_location__exit(&pevent->al);
+ return -1;
+ }
+
+ pevent->al_resolved = true;
+ return 0;
+}
+
+static PyObject *pyrf_sample_event__get_dso(struct pyrf_event *pevent,
+ void *closure __maybe_unused)
+{
+ if (pyrf_sample_event__resolve_al(pevent) < 0 || !pevent->al.map)
+ Py_RETURN_NONE;
+
+ return PyUnicode_FromString(dso__name(map__dso(pevent->al.map)));
+}
+
+static PyObject *pyrf_sample_event__get_dso_long_name(struct pyrf_event *pevent,
+ void *closure __maybe_unused)
+{
+ if (pyrf_sample_event__resolve_al(pevent) < 0 || !pevent->al.map)
+ Py_RETURN_NONE;
+
+ return PyUnicode_FromString(dso__long_name(map__dso(pevent->al.map)));
+}
+
+static PyObject *pyrf_sample_event__get_dso_bid(struct pyrf_event *pevent,
+ void *closure __maybe_unused)
+{
+ char sbuild_id[SBUILD_ID_SIZE];
+
+ if (pyrf_sample_event__resolve_al(pevent) < 0 || !pevent->al.map)
+ Py_RETURN_NONE;
+
+ build_id__snprintf(dso__bid(map__dso(pevent->al.map)), sbuild_id, sizeof(sbuild_id));
+ return PyUnicode_FromString(sbuild_id);
+}
+
+static PyObject *pyrf_sample_event__get_map_start(struct pyrf_event *pevent,
+ void *closure __maybe_unused)
+{
+ if (pyrf_sample_event__resolve_al(pevent) < 0 || !pevent->al.map)
+ Py_RETURN_NONE;
+
+ return PyLong_FromUnsignedLong(map__start(pevent->al.map));
+}
+
+static PyObject *pyrf_sample_event__get_map_end(struct pyrf_event *pevent,
+ void *closure __maybe_unused)
+{
+ if (pyrf_sample_event__resolve_al(pevent) < 0 || !pevent->al.map)
+ Py_RETURN_NONE;
+
+ return PyLong_FromUnsignedLong(map__end(pevent->al.map));
+}
+
+static PyObject *pyrf_sample_event__get_map_pgoff(struct pyrf_event *pevent,
+ void *closure __maybe_unused)
+{
+ if (pyrf_sample_event__resolve_al(pevent) < 0 || !pevent->al.map)
+ Py_RETURN_NONE;
+
+ return PyLong_FromUnsignedLongLong(map__pgoff(pevent->al.map));
+}
+
+static PyObject *pyrf_sample_event__get_symbol(struct pyrf_event *pevent,
+ void *closure __maybe_unused)
+{
+ if (pyrf_sample_event__resolve_al(pevent) < 0 || !pevent->al.sym)
+ Py_RETURN_NONE;
+
+ return PyUnicode_FromString(pevent->al.sym->name);
+}
+
+static PyObject *pyrf_sample_event__get_sym_start(struct pyrf_event *pevent,
+ void *closure __maybe_unused)
+{
+ if (pyrf_sample_event__resolve_al(pevent) < 0 || !pevent->al.sym)
+ Py_RETURN_NONE;
+
+ return PyLong_FromUnsignedLongLong(pevent->al.sym->start);
+}
+
+static PyObject *pyrf_sample_event__get_sym_end(struct pyrf_event *pevent,
+ void *closure __maybe_unused)
+{
+ if (pyrf_sample_event__resolve_al(pevent) < 0 || !pevent->al.sym)
+ Py_RETURN_NONE;
+
+ return PyLong_FromUnsignedLongLong(pevent->al.sym->end);
+}
+
+static PyObject *pyrf_sample_event__get_raw_buf(struct pyrf_event *pevent,
+ void *closure __maybe_unused)
+{
+ if (pevent->event.header.type != PERF_RECORD_SAMPLE)
+ Py_RETURN_NONE;
+
+ return PyBytes_FromStringAndSize((const char *)pevent->sample.raw_data,
+ pevent->sample.raw_size);
+}
+
+static PyObject *pyrf_sample_event__srccode(PyObject *self, PyObject *args)
+{
+ struct pyrf_event *pevent = (void *)self;
+ u64 addr = pevent->sample.ip;
+ char *srcfile = NULL;
+ char *srccode = NULL;
+ unsigned int line = 0;
+ int len = 0;
+ PyObject *result;
+ struct addr_location al;
+
+ if (!PyArg_ParseTuple(args, "|K", &addr))
+ return NULL;
+
+ if (pyrf_sample_event__resolve_al(pevent) < 0)
+ Py_RETURN_NONE;
+
+ if (addr != pevent->sample.ip) {
+ addr_location__init(&al);
+ thread__find_symbol_fb(pevent->al.thread, pevent->sample.cpumode, addr, &al);
+ } else {
+ addr_location__init(&al);
+ al.thread = thread__get(pevent->al.thread);
+ al.map = map__get(pevent->al.map);
+ al.sym = pevent->al.sym;
+ al.addr = pevent->al.addr;
+ }
+
+ if (al.map) {
+ struct dso *dso = map__dso(al.map);
+
+ if (dso) {
+ srcfile = get_srcline_split(dso, map__rip_2objdump(al.map, addr),
+ &line);
+ }
+ }
+ addr_location__exit(&al);
+
+ if (srcfile) {
+ srccode = find_sourceline(srcfile, line, &len);
+ result = Py_BuildValue("(sIs#)", srcfile, line, srccode, (Py_ssize_t)len);
+ free(srcfile);
+ } else {
+ result = Py_BuildValue("(sIs#)", NULL, 0, NULL, (Py_ssize_t)0);
+ }
+
+ return result;
+}
+
+static PyObject *pyrf_sample_event__insn(PyObject *self, PyObject *args __maybe_unused)
+{
+ struct pyrf_event *pevent = (void *)self;
+ struct thread *thread;
+ struct machine *machine;
+
+ if (pyrf_sample_event__resolve_al(pevent) < 0)
+ Py_RETURN_NONE;
+
+ thread = pevent->al.thread;
+
+ if (!thread || !thread__maps(thread))
+ Py_RETURN_NONE;
+
+ machine = maps__machine(thread__maps(thread));
+ if (!machine)
+ Py_RETURN_NONE;
+
+ if (pevent->sample.ip && !pevent->sample.insn_len)
+ perf_sample__fetch_insn(&pevent->sample, thread, machine);
+
+ if (!pevent->sample.insn_len)
+ Py_RETURN_NONE;
+
+ return PyBytes_FromStringAndSize((const char *)pevent->sample.insn,
+ pevent->sample.insn_len);
+}
+
static PyObject*
pyrf_sample_event__getattro(struct pyrf_event *pevent, PyObject *attr_name)
{
@@ -386,13 +634,102 @@ pyrf_sample_event__getattro(struct pyrf_event *pevent, PyObject *attr_name)
return obj ?: PyObject_GenericGetAttr((PyObject *) pevent, attr_name);
}
+static PyGetSetDef pyrf_sample_event__getset[] = {
+ {
+ .name = "raw_buf",
+ .get = (getter)pyrf_sample_event__get_raw_buf,
+ .set = NULL,
+ .doc = "event raw buffer.",
+ },
+ {
+ .name = "evsel",
+ .get = pyrf_event__get_evsel,
+ .set = NULL,
+ .doc = "tracking event.",
+ },
+ {
+ .name = "dso",
+ .get = (getter)pyrf_sample_event__get_dso,
+ .set = NULL,
+ .doc = "event dso short name.",
+ },
+ {
+ .name = "dso_long_name",
+ .get = (getter)pyrf_sample_event__get_dso_long_name,
+ .set = NULL,
+ .doc = "event dso long name.",
+ },
+ {
+ .name = "dso_bid",
+ .get = (getter)pyrf_sample_event__get_dso_bid,
+ .set = NULL,
+ .doc = "event dso build id.",
+ },
+ {
+ .name = "map_start",
+ .get = (getter)pyrf_sample_event__get_map_start,
+ .set = NULL,
+ .doc = "event map start address.",
+ },
+ {
+ .name = "map_end",
+ .get = (getter)pyrf_sample_event__get_map_end,
+ .set = NULL,
+ .doc = "event map end address.",
+ },
+ {
+ .name = "map_pgoff",
+ .get = (getter)pyrf_sample_event__get_map_pgoff,
+ .set = NULL,
+ .doc = "event map page offset.",
+ },
+ {
+ .name = "symbol",
+ .get = (getter)pyrf_sample_event__get_symbol,
+ .set = NULL,
+ .doc = "event symbol name.",
+ },
+ {
+ .name = "sym_start",
+ .get = (getter)pyrf_sample_event__get_sym_start,
+ .set = NULL,
+ .doc = "event symbol start address.",
+ },
+ {
+ .name = "sym_end",
+ .get = (getter)pyrf_sample_event__get_sym_end,
+ .set = NULL,
+ .doc = "event symbol end address.",
+ },
+ { .name = NULL, },
+};
+
+static PyMethodDef pyrf_sample_event__methods[] = {
+ {
+ .ml_name = "srccode",
+ .ml_meth = (PyCFunction)pyrf_sample_event__srccode,
+ .ml_flags = METH_VARARGS,
+ .ml_doc = PyDoc_STR("Get source code for an address.")
+ },
+ {
+ .ml_name = "insn",
+ .ml_meth = (PyCFunction)pyrf_sample_event__insn,
+ .ml_flags = METH_NOARGS,
+ .ml_doc = PyDoc_STR("Get instruction bytes for a sample.")
+ },
+ { .ml_name = NULL, }
+};
+
static PyTypeObject pyrf_sample_event__type = {
PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "perf.sample_event",
.tp_basicsize = sizeof(struct pyrf_event),
+ .tp_dealloc = (destructor)pyrf_event__delete,
.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
.tp_doc = pyrf_sample_event__doc,
.tp_members = pyrf_sample_event__members,
+ .tp_getset = pyrf_sample_event__getset,
+ .tp_methods = pyrf_sample_event__methods,
.tp_repr = (reprfunc)pyrf_sample_event__repr,
.tp_getattro = (getattrofunc) pyrf_sample_event__getattro,
};
@@ -428,25 +765,17 @@ static PyTypeObject pyrf_context_switch_event__type = {
PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "perf.context_switch_event",
.tp_basicsize = sizeof(struct pyrf_event),
+ .tp_dealloc = (destructor)pyrf_event__delete,
.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
.tp_doc = pyrf_context_switch_event__doc,
.tp_members = pyrf_context_switch_event__members,
+ .tp_getset = pyrf_event__getset,
.tp_repr = (reprfunc)pyrf_context_switch_event__repr,
};
static int pyrf_event__setup_types(void)
{
int err;
- pyrf_mmap_event__type.tp_new =
- pyrf_task_event__type.tp_new =
- pyrf_comm_event__type.tp_new =
- pyrf_lost_event__type.tp_new =
- pyrf_read_event__type.tp_new =
- pyrf_sample_event__type.tp_new =
- pyrf_context_switch_event__type.tp_new =
- pyrf_throttle_event__type.tp_new = PyType_GenericNew;
-
- pyrf_sample_event__type.tp_dealloc = (destructor)pyrf_sample_event__delete,
err = PyType_Ready(&pyrf_mmap_event__type);
if (err < 0)
@@ -490,33 +819,175 @@ static PyTypeObject *pyrf_event__type[] = {
[PERF_RECORD_SWITCH_CPU_WIDE] = &pyrf_context_switch_event__type,
};
-static PyObject *pyrf_event__new(const union perf_event *event)
+static PyObject *pyrf_event__new(const union perf_event *event, struct evsel *evsel,
+ struct perf_session *session __maybe_unused)
{
struct pyrf_event *pevent;
- PyTypeObject *ptype;
+ size_t size;
+ int err;
+ size_t min_size = sizeof(struct perf_event_header);
- if ((event->header.type < PERF_RECORD_MMAP ||
- event->header.type > PERF_RECORD_SAMPLE) &&
- !(event->header.type == PERF_RECORD_SWITCH ||
- event->header.type == PERF_RECORD_SWITCH_CPU_WIDE)) {
- PyErr_Format(PyExc_TypeError, "Unexpected header type %u",
+ if (event->header.type >= ARRAY_SIZE(pyrf_event__type) ||
+ pyrf_event__type[event->header.type] == NULL) {
+ return PyErr_Format(PyExc_TypeError, "Unexpected header type %u",
event->header.type);
- return NULL;
}
- // FIXME this better be dynamic or we need to parse everything
- // before calling perf_mmap__consume(), including tracepoint fields.
- if (sizeof(pevent->event) < event->header.size) {
- PyErr_Format(PyExc_TypeError, "Unexpected event size: %zd < %u",
- sizeof(pevent->event), event->header.size);
- return NULL;
+ switch (event->header.type) {
+ case PERF_RECORD_MMAP:
+ min_size = offsetof(struct perf_record_mmap, filename) + 1;
+ break;
+ case PERF_RECORD_MMAP2:
+ min_size = offsetof(struct perf_record_mmap2, filename) + 1;
+ break;
+ case PERF_RECORD_COMM:
+ min_size = sizeof(struct perf_record_comm);
+ break;
+ case PERF_RECORD_FORK:
+ case PERF_RECORD_EXIT:
+ min_size = sizeof(struct perf_record_fork);
+ break;
+ case PERF_RECORD_THROTTLE:
+ case PERF_RECORD_UNTHROTTLE:
+ min_size = sizeof(struct perf_record_throttle);
+ break;
+ case PERF_RECORD_LOST:
+ min_size = sizeof(struct perf_record_lost);
+ break;
+ case PERF_RECORD_READ:
+ min_size = sizeof(struct perf_record_read);
+ break;
+ case PERF_RECORD_SWITCH:
+ case PERF_RECORD_SWITCH_CPU_WIDE:
+ min_size = sizeof(struct perf_record_switch);
+ break;
+ case PERF_RECORD_AUX:
+ min_size = sizeof(struct perf_record_aux);
+ break;
+ case PERF_RECORD_ITRACE_START:
+ min_size = sizeof(struct perf_record_itrace_start);
+ break;
+ case PERF_RECORD_LOST_SAMPLES:
+ min_size = sizeof(struct perf_record_lost_samples);
+ break;
+ case PERF_RECORD_NAMESPACES:
+ min_size = offsetof(struct perf_record_namespaces, link_info);
+ break;
+ case PERF_RECORD_KSYMBOL:
+ min_size = sizeof(struct perf_record_ksymbol);
+ break;
+ case PERF_RECORD_BPF_EVENT:
+ min_size = sizeof(struct perf_record_bpf_event);
+ break;
+ case PERF_RECORD_CGROUP:
+ min_size = sizeof(struct perf_record_cgroup);
+ break;
+ case PERF_RECORD_TEXT_POKE:
+ min_size = offsetof(struct perf_record_text_poke_event, bytes);
+ break;
+ case PERF_RECORD_AUX_OUTPUT_HW_ID:
+ min_size = sizeof(struct perf_record_aux_output_hw_id);
+ break;
+ case PERF_RECORD_CALLCHAIN_DEFERRED:
+ min_size = sizeof(struct perf_record_callchain_deferred);
+ break;
+ case PERF_RECORD_HEADER_ATTR:
+ min_size = sizeof(struct perf_record_header_attr);
+ break;
+ case PERF_RECORD_HEADER_TRACING_DATA:
+ min_size = sizeof(struct perf_record_header_tracing_data);
+ break;
+ case PERF_RECORD_HEADER_BUILD_ID:
+ min_size = offsetof(struct perf_record_header_build_id, filename) + 1;
+ break;
+ case PERF_RECORD_ID_INDEX:
+ min_size = offsetof(struct perf_record_id_index, entries);
+ break;
+ case PERF_RECORD_AUXTRACE_INFO:
+ min_size = offsetof(struct perf_record_auxtrace_info, priv);
+ break;
+ case PERF_RECORD_AUXTRACE:
+ min_size = sizeof(struct perf_record_auxtrace);
+ break;
+ case PERF_RECORD_AUXTRACE_ERROR:
+ min_size = sizeof(struct perf_record_auxtrace_error);
+ break;
+ case PERF_RECORD_THREAD_MAP:
+ min_size = offsetof(struct perf_record_thread_map, entries);
+ break;
+ case PERF_RECORD_CPU_MAP:
+ min_size = sizeof(struct perf_record_cpu_map);
+ break;
+ case PERF_RECORD_STAT_CONFIG:
+ min_size = offsetof(struct perf_record_stat_config, data);
+ break;
+ case PERF_RECORD_STAT:
+ min_size = sizeof(struct perf_record_stat);
+ break;
+ case PERF_RECORD_STAT_ROUND:
+ min_size = sizeof(struct perf_record_stat_round);
+ break;
+ case PERF_RECORD_EVENT_UPDATE:
+ min_size = sizeof(struct perf_record_event_update);
+ break;
+ case PERF_RECORD_TIME_CONV:
+ min_size = sizeof(struct perf_record_time_conv);
+ break;
+ case PERF_RECORD_HEADER_FEATURE:
+ min_size = offsetof(struct perf_record_header_feature, data);
+ break;
+ case PERF_RECORD_COMPRESSED:
+ min_size = offsetof(struct perf_record_compressed, data);
+ break;
+ case PERF_RECORD_COMPRESSED2:
+ min_size = offsetof(struct perf_record_compressed2, data);
+ break;
+ case PERF_RECORD_BPF_METADATA:
+ min_size = offsetof(struct perf_record_bpf_metadata, entries);
+ break;
+ case PERF_RECORD_SCHEDSTAT_CPU:
+ min_size = sizeof(struct perf_record_schedstat_cpu);
+ break;
+ case PERF_RECORD_SCHEDSTAT_DOMAIN:
+ min_size = sizeof(struct perf_record_schedstat_domain);
+ break;
+ default:
+ break;
}
+ if (event->header.size < min_size)
+ return PyErr_Format(PyExc_ValueError, "Event size %u too small for type %u",
+ event->header.size, event->header.type);
+
+ /* Allocate just enough memory for the size of event. */
+ size = offsetof(struct pyrf_event, event) + event->header.size;
+ pevent = (struct pyrf_event *)PyObject_Malloc(size);
+ if (pevent == NULL)
+ return PyErr_NoMemory();
+
+ /* Copy the event for memory safety and initilaize variables. */
+ PyObject_Init((PyObject *)pevent, pyrf_event__type[event->header.type]);
+ memcpy(&pevent->event, event, event->header.size);
+
+ if (event->header.type == PERF_RECORD_MMAP) {
+ /* Ensure '\0' string termination. */
+ size_t max_len = pevent->event.header.size - offsetof(struct perf_record_mmap, filename);
- ptype = pyrf_event__type[event->header.type];
- pevent = PyObject_New(struct pyrf_event, ptype);
- if (pevent != NULL) {
- memcpy(&pevent->event, event, event->header.size);
- perf_sample__init(&pevent->sample, /*all=*/false);
+ pevent->event.mmap.filename[max_len - 1] = '\0';
+ }
+
+ perf_sample__init(&pevent->sample, /*all=*/true);
+ pevent->al_resolved = false;
+ addr_location__init(&pevent->al);
+
+ if (!evsel)
+ return (PyObject *)pevent;
+
+ /* Parse the sample again so that pointers are within the copied event. */
+ err = evsel__parse_sample(evsel, &pevent->event, &pevent->sample);
+ if (err < 0) {
+ Py_DECREF(pevent);
+ return PyErr_Format(PyExc_OSError,
+ "perf: can't parse sample, err=%d", err);
}
return (PyObject *)pevent;
}
@@ -1209,7 +1680,7 @@ static PyObject *pyrf_evsel__str(PyObject *self)
struct pyrf_evsel *pevsel = (void *)self;
struct evsel *evsel = pevsel->evsel;
- return PyUnicode_FromFormat("evsel(%s/%s/)", evsel__pmu_name(evsel), evsel__name(evsel));
+ return PyUnicode_FromFormat("evsel(%s)", evsel__name(evsel));
}
static PyMethodDef pyrf_evsel__methods[] = {
@@ -1773,9 +2244,11 @@ static PyObject *pyrf_evlist__read_on_cpu(struct pyrf_evlist *pevlist,
{
struct evlist *evlist = pevlist->evlist;
union perf_event *event;
+ struct evsel *evsel;
int sample_id_all = 1, cpu;
static char *kwlist[] = { "cpu", "sample_id_all", NULL };
struct mmap *md;
+ PyObject *pyevent;
int err;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|i", kwlist,
@@ -1783,44 +2256,31 @@ static PyObject *pyrf_evlist__read_on_cpu(struct pyrf_evlist *pevlist,
return NULL;
md = get_md(evlist, cpu);
- if (!md) {
- PyErr_Format(PyExc_TypeError, "Unknown CPU '%d'", cpu);
- return NULL;
- }
+ if (!md)
+ return PyErr_Format(PyExc_TypeError, "Unknown CPU '%d'", cpu);
- if (perf_mmap__read_init(&md->core) < 0)
- goto end;
+ err = perf_mmap__read_init(&md->core);
+ if (err < 0) {
+ return PyErr_Format(PyExc_OSError,
+ "perf: error mmap read init, err=%d", err);
+ }
event = perf_mmap__read_event(&md->core);
- if (event != NULL) {
- PyObject *pyevent = pyrf_event__new(event);
- struct pyrf_event *pevent = (struct pyrf_event *)pyevent;
- struct evsel *evsel;
-
- if (pyevent == NULL)
- return PyErr_NoMemory();
-
- evsel = evlist__event2evsel(evlist, event);
- if (!evsel) {
- Py_DECREF(pyevent);
- Py_INCREF(Py_None);
- return Py_None;
- }
+ if (event == NULL)
+ Py_RETURN_NONE;
+ evsel = evlist__event2evsel(evlist, event);
+ if (!evsel) {
+ /* Unknown evsel. */
perf_mmap__consume(&md->core);
-
- err = evsel__parse_sample(evsel, &pevent->event, &pevent->sample);
- if (err) {
- Py_DECREF(pyevent);
- return PyErr_Format(PyExc_OSError,
- "perf: can't parse sample, err=%d", err);
- }
-
- return pyevent;
+ Py_RETURN_NONE;
}
-end:
- Py_INCREF(Py_None);
- return Py_None;
+ pyevent = pyrf_event__new(event, evsel, evlist__session(evlist));
+ perf_mmap__consume(&md->core);
+ if (pyevent == NULL)
+ return PyErr_Occurred() ? NULL : PyErr_NoMemory();
+
+ return pyevent;
}
static PyObject *pyrf_evlist__open(struct pyrf_evlist *pevlist,
@@ -2015,10 +2475,7 @@ static PyObject *pyrf_evlist__str(PyObject *self)
evlist__for_each_entry(pevlist->evlist, pos) {
if (!first)
strbuf_addch(&sb, ',');
- if (!pos->pmu)
- strbuf_addstr(&sb, evsel__name(pos));
- else
- strbuf_addf(&sb, "%s/%s/", pos->pmu->name, evsel__name(pos));
+ strbuf_addstr(&sb, evsel__name(pos));
first = false;
}
strbuf_addstr(&sb, "])");
@@ -2528,19 +2985,12 @@ static int pyrf_session_tool__sample(const struct perf_tool *tool,
struct machine *machine __maybe_unused)
{
struct pyrf_session *psession = container_of(tool, struct pyrf_session, tool);
- PyObject *pyevent = pyrf_event__new(event);
- struct pyrf_event *pevent = (struct pyrf_event *)pyevent;
+ PyObject *pyevent = pyrf_event__new(event, sample->evsel, psession->session);
PyObject *ret;
if (pyevent == NULL)
return -ENOMEM;
- memcpy(&pevent->event, event, event->header.size);
- if (evsel__parse_sample(sample->evsel, &pevent->event, &pevent->sample) < 0) {
- Py_DECREF(pyevent);
- return -1;
- }
-
ret = PyObject_CallFunction(psession->sample, "O", pyevent);
if (!ret) {
Py_DECREF(pyevent);
--
2.54.0.794.g4f17f83d09-goog
^ permalink raw reply related
page: next (older) | prev (newer) | latest
- recent:[subjects (threaded)|topics (new)|topics (active)]
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox