From nobody Wed Nov 27 18:43:54 2024 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.9]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F1E972141D0 for ; Tue, 8 Oct 2024 18:35:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.9 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728412543; cv=none; b=W+cjHUHNthoqlaYS+zW4uKZD70yOkhftzBDdzTm+J2mMoiNvmfumQaXev9TdAJRnhnyda3ZX3poz3iU0B5f3zqc6B8/W8f+ki8I004eFAZh5MztPSOi16p9xTEFGbJ3AbIOTHGDZ8X9h4lzNxEhLMTWqpr6NurmI4sDiNAFs9FM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728412543; c=relaxed/simple; bh=9NC942Tnu9eotfuiQ8yTEZBW0BsoN3zsxEknTk/dLpA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=g3E7RrGEMvPhhIxMWIPtH0wuHmn3O3UUDai37zbIYLgvzCwaIiRlxi38ebfGF102oZD3tM6v54Ayxfb0RaSWCiKCXMcOu3fNRRF7BKHYgbziLYtCUmp/1KD6Au8c+CtBgoGQWRT3M2GgxFc7RBEtuyRiPpXcoYk4YTKumOqA2HY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=BOWb547T; arc=none smtp.client-ip=192.198.163.9 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="BOWb547T" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1728412542; x=1759948542; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=9NC942Tnu9eotfuiQ8yTEZBW0BsoN3zsxEknTk/dLpA=; b=BOWb547TP/V0TvNa2disxv7ByD0NIVvQaI6tQj8RFmlcMnlQhpM5UTA+ 1T1eL7/22i03PwlbvecRFfVoG2xel/6govgohqjPNKjk/6d9tn7TPzJiD RDxxVwpq8VO7NYnLHHJADJqG0Vh774jsK1FBJeQQMRbfqlWXlv7B2kHbV Z37AtwVlitx1YkuGqx3ndPj3k9+cB/FgA9gcb6RzuRDy5RpfYjVfg7lGG 1l/7ykaSVmoR8ySThBlEtoYRklC+hsm4t/OhwQBwoeWr4hwXFXSFvvjJj iD4baZojPwlhOe0FTR+XlLJL7msiu9+a7qA0SeT99T6ZRQPALCQDlMEmh Q==; X-CSE-ConnectionGUID: qU8ep5+4Tn+tsk2k/oMbLA== X-CSE-MsgGUID: q5dZcY8VTXyJbvZ2+bdEdQ== X-IronPort-AV: E=McAfee;i="6700,10204,11219"; a="38295365" X-IronPort-AV: E=Sophos;i="6.11,187,1725346800"; d="scan'208";a="38295365" Received: from fmviesa004.fm.intel.com ([10.60.135.144]) by fmvoesa103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Oct 2024 11:35:42 -0700 X-CSE-ConnectionGUID: 1sJW7vDLTn64kfNX0ZMYPg== X-CSE-MsgGUID: k5K8oGCjTxiFbh0EcnRngQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,187,1725346800"; d="scan'208";a="80530929" Received: from ldmartin-desk2.corp.intel.com (HELO ldmartin-desk2.intel.com) ([10.125.110.138]) by fmviesa004-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Oct 2024 11:35:40 -0700 From: Lucas De Marchi To: linux-kernel@vger.kernel.org Cc: dri-devel@lists.freedesktop.org, Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Umesh Nerlige Ramappa , Ian Rogers , Tvrtko Ursulin , Lucas De Marchi Subject: [PATCH 4/5] perf/dummy_pmu: Tie pmu to device lifecycle Date: Tue, 8 Oct 2024 13:35:00 -0500 Message-ID: <20241008183501.1354695-5-lucas.demarchi@intel.com> X-Mailer: git-send-email 2.46.2 In-Reply-To: <20241008183501.1354695-1-lucas.demarchi@intel.com> References: <20241008183501.1354695-1-lucas.demarchi@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Allow to unregister the PMU from perf with active events. When driver is being accessed perf keeps a reference that when released triggers the device cleanup. Signed-off-by: Lucas De Marchi --- kernel/events/dummy_pmu.c | 56 ++++++++++++++++++++++++++++----------- 1 file changed, 41 insertions(+), 15 deletions(-) diff --git a/kernel/events/dummy_pmu.c b/kernel/events/dummy_pmu.c index cdba3a831e4a..c07e111bff01 100644 --- a/kernel/events/dummy_pmu.c +++ b/kernel/events/dummy_pmu.c @@ -49,6 +49,11 @@ static struct dummy_device *pmu_to_device(struct dummy_p= mu *pmu) return container_of(pmu, struct dummy_device, pmu); } =20 +static struct dummy_pmu *pmu_to_dummy(struct pmu *pmu) +{ + return container_of(pmu, struct dummy_pmu, base); +} + static ssize_t dummy_pmu_events_sysfs_show(struct device *dev, struct device_attribute *attr, char *page) @@ -92,18 +97,9 @@ static const struct attribute_group *attr_groups[] =3D { NULL, }; =20 -static void dummy_pmu_event_destroy(struct perf_event *event) -{ - struct dummy_pmu *pmu =3D event_to_pmu(event); - struct dummy_device *d =3D pmu_to_device(pmu); - - kref_put(&d->refcount, device_release); -} - static int dummy_pmu_event_init(struct perf_event *event) { struct dummy_pmu *pmu =3D event_to_pmu(event); - struct dummy_device *d =3D pmu_to_device(pmu); =20 if (!pmu->registered) return -ENODEV; @@ -121,10 +117,6 @@ static int dummy_pmu_event_init(struct perf_event *eve= nt) if (event->cpu < 0) return -EINVAL; =20 - /* Event keeps a ref to maintain PMU allocated, even if it's unregistered= */ - kref_get(&d->refcount); - event->destroy =3D dummy_pmu_event_destroy; - return 0; } =20 @@ -195,10 +187,29 @@ static void dummy_pmu_event_del(struct perf_event *ev= ent, int flags) dummy_pmu_event_stop(event, PERF_EF_UPDATE); } =20 +static struct pmu *dummy_pmu_get(struct pmu *pmu) +{ + struct dummy_device *d =3D pmu_to_device(pmu_to_dummy(pmu)); + + kref_get(&d->refcount); + + return pmu; +} + +static void dummy_pmu_put(struct pmu *pmu) +{ + struct dummy_device *d =3D pmu_to_device(pmu_to_dummy(pmu)); + + kref_put(&d->refcount, device_release); +} + static int device_init(struct dummy_device *d) { int ret; =20 + if (WARN_ONCE(d->pmu.name, "Cannot re-register pmu.\n")) + return -EINVAL; + d->pmu.base =3D (struct pmu){ .attr_groups =3D attr_groups, .module =3D THIS_MODULE, @@ -209,6 +220,8 @@ static int device_init(struct dummy_device *d) .start =3D dummy_pmu_event_start, .stop =3D dummy_pmu_event_stop, .read =3D dummy_pmu_event_read, + .get =3D dummy_pmu_get, + .put =3D dummy_pmu_put, }; =20 d->pmu.name =3D kasprintf(GFP_KERNEL, "dummy_pmu_%u", d->instance); @@ -217,12 +230,22 @@ static int device_init(struct dummy_device *d) =20 ret =3D perf_pmu_register(&d->pmu.base, d->pmu.name, -1); if (ret) - return ret; + goto fail; =20 d->pmu.registered =3D true; pr_info("Device registered: %s\n", d->pmu.name); =20 return 0; + +fail: + /* + * See device_release: if name is non-NULL, dummy_pmu was registered + * with perf and needs cleanup + */ + kfree(d->pmu.name); + d->pmu.name =3D NULL; + + return ret; } =20 static void device_exit(struct dummy_device *d) @@ -237,7 +260,10 @@ static void device_release(struct kref *ref) { struct dummy_device *d =3D container_of(ref, struct dummy_device, refcoun= t); =20 - kfree(d->pmu.name); + if (d->pmu.name) { + perf_pmu_free(&d->pmu.base); + kfree(d->pmu.name); + } kfree(d); } =20 --=20 2.46.2