mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Ravi Bangoria <ravi.bangoria@amd.com>
To: Peter Zijlstra <peterz@infradead.org>
Cc: "mingo@kernel.org" <mingo@kernel.org>,
	"lucas.demarchi@intel.com" <lucas.demarchi@intel.com>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	"willy@infradead.org" <willy@infradead.org>,
	"acme@kernel.org" <acme@kernel.org>,
	"namhyung@kernel.org" <namhyung@kernel.org>,
	"mark.rutland@arm.com" <mark.rutland@arm.com>,
	"alexander.shishkin@linux.intel.com"
	<alexander.shishkin@linux.intel.com>,
	"jolsa@kernel.org" <jolsa@kernel.org>,
	"irogers@google.com" <irogers@google.com>,
	"adrian.hunter@intel.com" <adrian.hunter@intel.com>,
	"kan.liang@linux.intel.com" <kan.liang@linux.intel.com>,
	Ravi Bangoria <ravi.bangoria@amd.com>
Subject: Re: [PATCH 19/19] perf: Make perf_pmu_unregister() useable
Date: Thu, 19 Dec 2024 16:26:31 +0530	[thread overview]
Message-ID: <9d105236-a591-4f6e-84db-75e5d1d6fe36@amd.com> (raw)
In-Reply-To: <8c31f7bd-871d-4a38-ad15-a16a116e1f39@amd.com>

[-- Attachment #1: Type: text/plain, Size: 419 bytes --]

>> How did you test; perf-fuzzer or something?
> 
> Prepared a simple test that does pmu register(), unregister() and
> "perf record" in parallel. It's quite dirty, I'll clean it up and
> share it here.

Attaching the testsuite here.

It contains:
- Kernel module that allows user to register and unregister a pmu
- Shell script to that runs "perf record" on test pmu
- README explains how to run the test

Thanks,
Ravi

[-- Attachment #2: _tinypmu-u-register.sh --]
[-- Type: text/plain, Size: 262 bytes --]

#!/bin/bash
# SPDX-License-Identifier: GPL-2.0
# Author: Ravi Bangoria <ravi.bangoria@amd.com>

# Must not run directly. Run it via tinypmu-u.sh

if [ "$EUID" -ne 0 ]; then
	echo "Please run as root"
	exit
fi

while true; do
	echo 1 > /dev/tinypmu_register
done

[-- Attachment #3: _tinypmu-u-unregister.sh --]
[-- Type: text/plain, Size: 264 bytes --]

#!/bin/bash
# SPDX-License-Identifier: GPL-2.0
# Author: Ravi Bangoria <ravi.bangoria@amd.com>

# Must not run directly. Run it via tinypmu-u.sh

if [ "$EUID" -ne 0 ]; then
	echo "Please run as root"
	exit
fi

while true; do
	echo 1 > /dev/tinypmu_unregister
done

[-- Attachment #4: Makefile --]
[-- Type: text/plain, Size: 314 bytes --]

# SPDX-License-Identifier: GPL-2.0
# Author: Ravi Bangoria <ravi.bangoria@amd.com>

obj-m := tinypmu-k.o

KDIR  := /lib/modules/$(shell uname -r)/build
PWD   := $(shell pwd)

default:
	$(MAKE) -C $(KDIR) M=$(PWD) modules

clean:
	rm -rf *.o *.mod.c *.mod *.ko *.order *.symvers \.*.o \.*.ko \.*.cmd \.tmp_versions

[-- Attachment #5: README --]
[-- Type: text/plain, Size: 445 bytes --]

A simple testsuite to stress test pmu register / unregister code:
https://lore.kernel.org/r/20241104133909.669111662@infradead.org

Build:

  $ make

Clean:

  $ make clean

Test perf pmu register and unregister:

  term1~$ while true; do sudo bash tinypmu-u.sh; done

Test event creation / mmap / unmap / event deletion etc along with
pmu register / unregister, (run this in parallel to above command):

  term2~$ sudo bash tinypmu-u-events.sh

[-- Attachment #6: tinypmu-k.c --]
[-- Type: text/plain, Size: 4621 bytes --]

// SPDX-License-Identifier: GPL-2.0
/*
 * Provide an interface /dev/tinypmu_register and /dev/tinypmu_unregister to
 * stress test perf_pmu_register() and perf_pmu_unregister() functions.
 *
 * Author: Ravi Bangoria <ravi.bangoria@amd.com>
 */
#define pr_fmt(fmt) "tinypmu: " fmt

#include <linux/module.h>
#include <linux/perf_event.h>
#include <linux/fs.h>
#include <linux/miscdevice.h>

#define NR_TINYPMUS		1

static int tinypmu_event_init(struct perf_event *event)
{
	return 0;
}

static void tinypmu_del(struct perf_event *event, int flags)
{
}

static int tinypmu_add(struct perf_event *event, int flags)
{
	return 0;
}

static void tinypmu_start(struct perf_event *event, int flags)
{
}

static void tinypmu_stop(struct perf_event *event, int flags)
{
}

static void tinypmu_read(struct perf_event *event)
{
}

PMU_FORMAT_ATTR(event, "config:0-20");
static struct attribute *tinypmu_events_attr[] = {
	&format_attr_event.attr,
	NULL,
};

static struct attribute_group tinypmu_events_group = {
	.name = "format",
	.attrs = tinypmu_events_attr,
};

static const struct attribute_group *tinypmu_attr_groups[] = {
	&tinypmu_events_group,
	NULL,
};

static struct pmu *alloc_tinypmu(void)
{
	struct pmu *tinypmu = kzalloc(sizeof(struct pmu), GFP_KERNEL);
	if (!tinypmu)
		return NULL;

	tinypmu->task_ctx_nr = perf_invalid_context;
	tinypmu->event_init  = tinypmu_event_init;
	tinypmu->add         = tinypmu_add;
	tinypmu->del         = tinypmu_del;
	tinypmu->start       = tinypmu_start;
	tinypmu->stop        = tinypmu_stop;
	tinypmu->read        = tinypmu_read;
	tinypmu->attr_groups = tinypmu_attr_groups;

	return tinypmu;
}

static DEFINE_MUTEX(lock);
static struct pmu *tinypmus[NR_TINYPMUS];

static char pmu_name[NR_TINYPMUS][11];

static void register_pmu(unsigned int idx)
{
	struct pmu *temp;
	int ret;

	if (idx >= NR_TINYPMUS)
		return;

	temp = alloc_tinypmu();
	if (!temp) {
		mutex_unlock(&lock);
		return;
	}

	mutex_lock(&lock);
	if (tinypmus[idx]) {
		mutex_unlock(&lock);
		kfree(temp);
		return;
	}

	ret = perf_pmu_register(temp, pmu_name[idx], -1);
	if (!ret) {
		tinypmus[idx] = temp;
		mutex_unlock(&lock);
		return;
	}

	mutex_unlock(&lock);
	kfree(temp);
	return;
}

static void unregister_pmu(unsigned int idx)
{
	struct pmu *temp;

	if (idx >= NR_TINYPMUS)
		return;

	mutex_lock(&lock);
	if (!tinypmus[idx]) {
		mutex_unlock(&lock);
		return;
	}

	/*
	 * Must call perf_pmu_unregister() inside atomic section. If I try
	 * to reduce atomic section by using temp to cache tinypmus[idx]
	 * plus clear tinypmus[idx] and do unregister after mutex_unlock(),
	 * register_pmu() will assume no pmu exists with "tinypmu<idx>" name
	 * since tinypmus[idx] is NULL and try to register new pmu although
	 * this function is yet to unregistered pmu with the same name.
	 */
	perf_pmu_unregister(tinypmus[idx]);

	temp = tinypmus[idx];
	tinypmus[idx] = NULL;
	mutex_unlock(&lock);

	kfree(temp);
}

static ssize_t register_write(struct file *f, const char *data, size_t size,
			      loff_t *ppos)
{
	unsigned int idx;

	get_random_bytes(&idx, sizeof(idx));
	idx %= NR_TINYPMUS;
	register_pmu(idx);
	return 1;
}

static ssize_t unregister_write(struct file *f, const char *data, size_t size,
			        loff_t *ppos)
{
	unsigned int idx;

	get_random_bytes(&idx, sizeof(idx));
	idx %= NR_TINYPMUS;
	unregister_pmu(idx);
	return 1;
}

static const struct file_operations register_fops = {
	.owner = THIS_MODULE,
	.write = register_write,
};

static const struct file_operations unregister_fops = {
	.owner = THIS_MODULE,
	.write = unregister_write,
};

static struct miscdevice register_dev = {
	MISC_DYNAMIC_MINOR,
	"tinypmu_register",
	&register_fops,
};

static struct miscdevice unregister_dev = {
	MISC_DYNAMIC_MINOR,
	"tinypmu_unregister",
	&unregister_fops,
};

static int __init hello_init(void)
{
	int ret;
	int i;

	ret = misc_register(&register_dev);
	if (ret) {
		pr_err("Failed to register register_dev\n");
		return ret;
	}

	ret = misc_register(&unregister_dev);
	if (ret) {
		pr_err("Failed to register unregister_dev\n");
		misc_deregister(&register_dev);
		return ret;
	}

	for (i = 0; i < NR_TINYPMUS; i++)
		sprintf(pmu_name[i], "tinypmu%d", i);

	for (i = 0; i < NR_TINYPMUS; i++)
		register_pmu(i);

	return 0;
}

module_init(hello_init);

static void __exit hello_exit(void)
{
	int i;

	for (i = 0; i < NR_TINYPMUS; i++)
		unregister_pmu(i);
	misc_deregister(&register_dev);
	misc_deregister(&unregister_dev);
}

module_exit(hello_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Ravi Bangoria");
MODULE_DESCRIPTION("PMU register/unregister stress test");
MODULE_VERSION("dev");

[-- Attachment #7: tinypmu-u.sh --]
[-- Type: text/plain, Size: 629 bytes --]

#!/bin/bash
# SPDX-License-Identifier: GPL-2.0
# Author: Ravi Bangoria <ravi.bangoria@amd.com>

if [ "$EUID" -ne 0 ]; then
	echo "Please run as root"
	exit
fi

sudo insmod tinypmu-k.ko

cleanup() {
	for i in "$@"; do
		echo -n "${i} "
		kill ${i}
	done

	wait
	rmmod tinypmu_k

	rm -rf /dev/tinypmu_register
	rm -rf /dev/tinypmu_unregister
	echo ""
	exit
}

bash _tinypmu-u-register.sh &
reg_pid=$!
bash _tinypmu-u-unregister.sh &
unreg_pid=$!

# register Ctrl+C cleanup if aborted inbetween
#trap "cleanup '${reg_pid}' '${unreg_pid}' ${event_pids[@]}" 2

echo ${reg_pid} ${unreg_pid}

sleep 10

cleanup ${reg_pid} ${unreg_pid}


[-- Attachment #8: tinypmu-u-events.sh --]
[-- Type: text/plain, Size: 262 bytes --]

#!/bin/bash
# SPDX-License-Identifier: GPL-2.0
# Author: Ravi Bangoria <ravi.bangoria@amd.com>

pmu_nr=${1}

if [ "$EUID" -ne 0 ]; then
	echo "Please run as root"
	exit
fi

while true; do
	perf record -a -e tinypmu${pmu_nr}// -- sleep 1 >> /dev/null 2>&1 &
done

  reply	other threads:[~2024-12-19 10:56 UTC|newest]

Thread overview: 85+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-04 13:39 [PATCH 00/19] perf: Make perf_pmu_unregister() usable Peter Zijlstra
2024-11-04 13:39 ` [PATCH 01/19] lockdep: Fix might_fault() Peter Zijlstra
2025-03-01 20:06   ` [tip: locking/core] lockdep/mm: Fix might_fault() lockdep check of current->mm->mmap_lock tip-bot2 for Peter Zijlstra
2024-11-04 13:39 ` [PATCH 02/19] perf: Fix pmus_lock vs pmus_srcu ordering Peter Zijlstra
2025-03-01 20:07   ` [tip: perf/core] perf/core: Fix pmus_lock vs. " tip-bot2 for Peter Zijlstra
2024-11-04 13:39 ` [PATCH 03/19] perf: Fix perf_pmu_register() vs perf_init_event() Peter Zijlstra
2024-11-04 15:36   ` Uros Bizjak
2024-11-05 12:01     ` Peter Zijlstra
2025-03-01 20:07   ` [tip: perf/core] perf/core: Fix perf_pmu_register() vs. perf_init_event() tip-bot2 for Peter Zijlstra
2024-11-04 13:39 ` [PATCH 04/19] perf: Simplify perf_event_alloc() error path Peter Zijlstra
2025-03-01 20:07   ` [tip: perf/core] perf/core: Simplify the " tip-bot2 for Peter Zijlstra
2025-03-04  8:57   ` tip-bot2 for Peter Zijlstra
2025-03-06  7:57   ` [PATCH 04/19] perf: Simplify " Lai, Yi
2025-03-06  9:24     ` Ingo Molnar
2025-03-07  3:16       ` Lai, Yi
2025-03-07 11:33         ` Ingo Molnar
2024-11-04 13:39 ` [PATCH 05/19] perf: Simplify perf_pmu_register() " Peter Zijlstra
2025-03-01 20:07   ` [tip: perf/core] perf/core: Simplify the " tip-bot2 for Peter Zijlstra
2025-03-04  8:57   ` tip-bot2 for Peter Zijlstra
2024-11-04 13:39 ` [PATCH 06/19] perf: Simplify perf_pmu_register() Peter Zijlstra
2024-11-20 13:06   ` Ravi Bangoria
2024-11-20 14:46     ` Peter Zijlstra
2024-11-20 15:53       ` Ravi Bangoria
2025-03-01 20:07   ` [tip: perf/core] perf/core: " tip-bot2 for Peter Zijlstra
2025-03-04  8:57   ` tip-bot2 for Peter Zijlstra
2025-03-25 19:47   ` [PATCH 06/19] perf: " Sidhartha Kumar
2024-11-04 13:39 ` [PATCH 07/19] perf: Simplify perf_init_event() Peter Zijlstra
2025-03-01 20:07   ` [tip: perf/core] perf/core: " tip-bot2 for Peter Zijlstra
2025-03-04  8:57   ` tip-bot2 for Peter Zijlstra
2024-11-04 13:39 ` [PATCH 08/19] perf: Simplify perf_event_alloc() Peter Zijlstra
2025-03-01 20:07   ` [tip: perf/core] perf/core: " tip-bot2 for Peter Zijlstra
2025-03-04  8:57   ` tip-bot2 for Peter Zijlstra
2024-11-04 13:39 ` [PATCH 09/19] perf: Merge pmu_disable_count into cpu_pmu_context Peter Zijlstra
2025-03-01 20:07   ` [tip: perf/core] perf/core: Merge struct pmu::pmu_disable_count into struct perf_cpu_pmu_context::pmu_disable_count tip-bot2 for Peter Zijlstra
2025-03-04  8:57   ` tip-bot2 for Peter Zijlstra
2024-11-04 13:39 ` [PATCH 10/19] perf: Add this_cpc() helper Peter Zijlstra
2025-03-01 20:07   ` [tip: perf/core] perf/core: " tip-bot2 for Peter Zijlstra
2025-03-04  8:57   ` tip-bot2 for Peter Zijlstra
2024-11-04 13:39 ` [PATCH 11/19] perf: Detach perf_cpu_pmu_context and pmu lifetimes Peter Zijlstra
2025-03-03 12:29   ` [tip: perf/core] perf/core: Detach 'struct perf_cpu_pmu_context' and 'struct pmu' lifetimes tip-bot2 for Peter Zijlstra
2025-03-04  8:56   ` tip-bot2 for Peter Zijlstra
2024-11-04 13:39 ` [PATCH 12/19] perf: Introduce perf_free_addr_filters() Peter Zijlstra
2025-03-01 20:07   ` [tip: perf/core] perf/core: " tip-bot2 for Peter Zijlstra
2025-03-04  8:57   ` tip-bot2 for Peter Zijlstra
2024-11-04 13:39 ` [PATCH 13/19] perf: Robustify perf_event_free_bpf_prog() Peter Zijlstra
2025-03-01 20:07   ` [tip: perf/core] perf/bpf: " tip-bot2 for Peter Zijlstra
2025-03-04  8:57   ` tip-bot2 for Peter Zijlstra
2024-11-04 13:39 ` [PATCH 14/19] perf: Simplify perf_mmap() control flow Peter Zijlstra
2025-03-01 20:07   ` [tip: perf/core] perf/core: Simplify the " tip-bot2 for Peter Zijlstra
2025-03-04  8:57   ` tip-bot2 for Peter Zijlstra
2024-11-04 13:39 ` [PATCH 15/19] perf: Fix perf_mmap() failure path Peter Zijlstra
2025-03-01 19:17   ` Ingo Molnar
2025-03-03 12:38     ` Lorenzo Stoakes
2025-03-04  8:46   ` [tip: perf/core] perf/core: " tip-bot2 for Peter Zijlstra
2025-03-04  8:56   ` tip-bot2 for Peter Zijlstra
2024-11-04 13:39 ` [PATCH 16/19] perf: Further simplify perf_mmap() Peter Zijlstra
2025-03-01 20:07   ` [tip: perf/core] perf/core: " tip-bot2 for Peter Zijlstra
2025-03-04  8:57   ` tip-bot2 for Peter Zijlstra
2024-11-04 13:39 ` [PATCH 17/19] perf: Remove retry loop from perf_mmap() Peter Zijlstra
2025-03-01 20:07   ` [tip: perf/core] perf/core: " tip-bot2 for Peter Zijlstra
2025-03-04  8:56   ` tip-bot2 for Peter Zijlstra
2024-11-04 13:39 ` [PATCH 18/19] perf: Lift event->mmap_mutex in perf_mmap() Peter Zijlstra
2025-03-01 20:07   ` [tip: perf/core] perf/core: " tip-bot2 for Peter Zijlstra
2025-03-04  8:56   ` tip-bot2 for Peter Zijlstra
2024-11-04 13:39 ` [PATCH 19/19] perf: Make perf_pmu_unregister() useable Peter Zijlstra
2024-11-05 15:08   ` Liang, Kan
2024-11-05 15:16     ` Peter Zijlstra
2024-11-05 15:25       ` Liang, Kan
2024-11-25  4:10   ` Ravi Bangoria
2024-12-17  9:12     ` Peter Zijlstra
2024-12-17 11:52       ` Peter Zijlstra
2024-12-19  9:33         ` Ravi Bangoria
2024-12-19 10:56           ` Ravi Bangoria [this message]
2025-01-03  4:24           ` Ravi Bangoria
2025-01-17  0:03             ` Peter Zijlstra
2025-01-17  5:20               ` Ravi Bangoria
2025-01-17  8:36                 ` Peter Zijlstra
2025-01-17 13:04               ` Peter Zijlstra
2025-01-17 21:04                 ` Peter Zijlstra
2025-01-20 11:15                   ` Ravi Bangoria
2025-01-03  4:29   ` Ravi Bangoria
2024-12-16 18:02 ` [PATCH 00/19] perf: Make perf_pmu_unregister() usable Lucas De Marchi
2025-03-01 20:00 ` Ingo Molnar
2025-03-03  3:25   ` Ravi Bangoria
2025-03-03  9:16     ` Peter Zijlstra

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=9d105236-a591-4f6e-84db-75e5d1d6fe36@amd.com \
    --to=ravi.bangoria@amd.com \
    --cc=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=irogers@google.com \
    --cc=jolsa@kernel.org \
    --cc=kan.liang@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lucas.demarchi@intel.com \
    --cc=mark.rutland@arm.com \
    --cc=mingo@kernel.org \
    --cc=namhyung@kernel.org \
    --cc=peterz@infradead.org \
    --cc=willy@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®