Skip to content

Commit 401b356

Browse files
committed
Merge branch 'pci/trace'
- Add generic RAS tracepoint for hotplug events (Shuai Xue) - Add RAS tracepoint for link speed changes (Shuai Xue) * pci/trace: Documentation: tracing: Add PCI tracepoint documentation PCI: trace: Add RAS tracepoint to monitor link speed changes PCI: trace: Add generic RAS tracepoint for hotplug event
2 parents 73b4779 + 8236fc6 commit 401b356

12 files changed

Lines changed: 280 additions & 15 deletions

File tree

Documentation/trace/events-pci.rst

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
.. SPDX-License-Identifier: GPL-2.0
2+
3+
===========================
4+
Subsystem Trace Points: PCI
5+
===========================
6+
7+
Overview
8+
========
9+
The PCI tracing system provides tracepoints to monitor critical hardware events
10+
that can impact system performance and reliability. These events normally show
11+
up here:
12+
13+
/sys/kernel/tracing/events/pci
14+
15+
Cf. include/trace/events/pci.h for the events definitions.
16+
17+
Available Tracepoints
18+
=====================
19+
20+
pci_hp_event
21+
------------
22+
23+
Monitors PCI hotplug events including card insertion/removal and link
24+
state changes.
25+
::
26+
27+
pci_hp_event "%s slot:%s, event:%s\n"
28+
29+
**Event Types**:
30+
31+
* ``LINK_UP`` - PCIe link established
32+
* ``LINK_DOWN`` - PCIe link lost
33+
* ``CARD_PRESENT`` - Card detected in slot
34+
* ``CARD_NOT_PRESENT`` - Card removed from slot
35+
36+
**Example Usage**::
37+
38+
# Enable the tracepoint
39+
echo 1 > /sys/kernel/debug/tracing/events/pci/pci_hp_event/enable
40+
41+
# Monitor events (the following output is generated when a device is hotplugged)
42+
cat /sys/kernel/debug/tracing/trace_pipe
43+
irq/51-pciehp-88 [001] ..... 1311.177459: pci_hp_event: 0000:00:02.0 slot:10, event:CARD_PRESENT
44+
45+
irq/51-pciehp-88 [001] ..... 1311.177566: pci_hp_event: 0000:00:02.0 slot:10, event:LINK_UP
46+
47+
pcie_link_event
48+
---------------
49+
50+
Monitors PCIe link speed changes and provides detailed link status information.
51+
::
52+
53+
pcie_link_event "%s type:%d, reason:%d, cur_bus_speed:%d, max_bus_speed:%d, width:%u, flit_mode:%u, status:%s\n"
54+
55+
**Parameters**:
56+
57+
* ``type`` - PCIe device type (4=Root Port, etc.)
58+
* ``reason`` - Reason for link change:
59+
60+
- ``0`` - Link retrain
61+
- ``1`` - Bus enumeration
62+
- ``2`` - Bandwidth notification enable
63+
- ``3`` - Bandwidth notification IRQ
64+
- ``4`` - Hotplug event
65+
66+
67+
**Example Usage**::
68+
69+
# Enable the tracepoint
70+
echo 1 > /sys/kernel/debug/tracing/events/pci/pcie_link_event/enable
71+
72+
# Monitor events (the following output is generated when a device is hotplugged)
73+
cat /sys/kernel/debug/tracing/trace_pipe
74+
irq/51-pciehp-88 [001] ..... 381.545386: pcie_link_event: 0000:00:02.0 type:4, reason:4, cur_bus_speed:20, max_bus_speed:23, width:1, flit_mode:0, status:DLLLA

Documentation/trace/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ applications.
5454
events-power
5555
events-nmi
5656
events-msr
57+
events-pci
5758
boottime-trace
5859
histogram
5960
histogram-design

drivers/pci/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,6 @@ obj-y += controller/
4848
obj-y += switch/
4949

5050
subdir-ccflags-$(CONFIG_PCI_DEBUG) := -DDEBUG
51+
52+
CFLAGS_trace.o := -I$(src)
53+
obj-$(CONFIG_TRACING) += trace.o

drivers/pci/hotplug/pciehp_ctrl.c

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <linux/types.h>
2020
#include <linux/pm_runtime.h>
2121
#include <linux/pci.h>
22+
#include <trace/events/pci.h>
2223

2324
#include "../pci.h"
2425
#include "pciehp.h"
@@ -244,12 +245,20 @@ void pciehp_handle_presence_or_link_change(struct controller *ctrl, u32 events)
244245
case ON_STATE:
245246
ctrl->state = POWEROFF_STATE;
246247
mutex_unlock(&ctrl->state_lock);
247-
if (events & PCI_EXP_SLTSTA_DLLSC)
248+
if (events & PCI_EXP_SLTSTA_DLLSC) {
248249
ctrl_info(ctrl, "Slot(%s): Link Down\n",
249250
slot_name(ctrl));
250-
if (events & PCI_EXP_SLTSTA_PDC)
251+
trace_pci_hp_event(pci_name(ctrl->pcie->port),
252+
slot_name(ctrl),
253+
PCI_HOTPLUG_LINK_DOWN);
254+
}
255+
if (events & PCI_EXP_SLTSTA_PDC) {
251256
ctrl_info(ctrl, "Slot(%s): Card not present\n",
252257
slot_name(ctrl));
258+
trace_pci_hp_event(pci_name(ctrl->pcie->port),
259+
slot_name(ctrl),
260+
PCI_HOTPLUG_CARD_NOT_PRESENT);
261+
}
253262
pciehp_disable_slot(ctrl, SURPRISE_REMOVAL);
254263
break;
255264
default:
@@ -269,6 +278,9 @@ void pciehp_handle_presence_or_link_change(struct controller *ctrl, u32 events)
269278
INDICATOR_NOOP);
270279
ctrl_info(ctrl, "Slot(%s): Card not present\n",
271280
slot_name(ctrl));
281+
trace_pci_hp_event(pci_name(ctrl->pcie->port),
282+
slot_name(ctrl),
283+
PCI_HOTPLUG_CARD_NOT_PRESENT);
272284
}
273285
mutex_unlock(&ctrl->state_lock);
274286
return;
@@ -281,12 +293,19 @@ void pciehp_handle_presence_or_link_change(struct controller *ctrl, u32 events)
281293
case OFF_STATE:
282294
ctrl->state = POWERON_STATE;
283295
mutex_unlock(&ctrl->state_lock);
284-
if (present)
296+
if (present) {
285297
ctrl_info(ctrl, "Slot(%s): Card present\n",
286298
slot_name(ctrl));
287-
if (link_active)
288-
ctrl_info(ctrl, "Slot(%s): Link Up\n",
289-
slot_name(ctrl));
299+
trace_pci_hp_event(pci_name(ctrl->pcie->port),
300+
slot_name(ctrl),
301+
PCI_HOTPLUG_CARD_PRESENT);
302+
}
303+
if (link_active) {
304+
ctrl_info(ctrl, "Slot(%s): Link Up\n", slot_name(ctrl));
305+
trace_pci_hp_event(pci_name(ctrl->pcie->port),
306+
slot_name(ctrl),
307+
PCI_HOTPLUG_LINK_UP);
308+
}
290309
ctrl->request_result = pciehp_enable_slot(ctrl);
291310
break;
292311
default:

drivers/pci/hotplug/pciehp_hpc.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,8 @@ int pciehp_check_link_status(struct controller *ctrl)
320320
}
321321

322322
pcie_capability_read_word(pdev, PCI_EXP_LNKSTA2, &linksta2);
323-
__pcie_update_link_speed(ctrl->pcie->port->subordinate, lnk_status, linksta2);
323+
__pcie_update_link_speed(ctrl->pcie->port->subordinate, PCIE_HOTPLUG,
324+
lnk_status, linksta2);
324325

325326
if (!found) {
326327
ctrl_info(ctrl, "Slot(%s): No device found\n",

drivers/pci/pci.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4553,7 +4553,7 @@ int pcie_retrain_link(struct pci_dev *pdev, bool use_lt)
45534553
* Link Speed.
45544554
*/
45554555
if (pdev->subordinate)
4556-
pcie_update_link_speed(pdev->subordinate);
4556+
pcie_update_link_speed(pdev->subordinate, PCIE_LINK_RETRAIN);
45574557

45584558
return rc;
45594559
}

drivers/pci/pci.h

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <linux/align.h>
66
#include <linux/bitfield.h>
77
#include <linux/pci.h>
8+
#include <trace/events/pci.h>
89

910
struct pcie_tlp_log;
1011

@@ -601,12 +602,28 @@ const char *pci_speed_string(enum pci_bus_speed speed);
601602
void __pcie_print_link_status(struct pci_dev *dev, bool verbose);
602603
void pcie_report_downtraining(struct pci_dev *dev);
603604

604-
static inline void __pcie_update_link_speed(struct pci_bus *bus, u16 linksta, u16 linksta2)
605+
enum pcie_link_change_reason {
606+
PCIE_LINK_RETRAIN,
607+
PCIE_ADD_BUS,
608+
PCIE_BWCTRL_ENABLE,
609+
PCIE_BWCTRL_IRQ,
610+
PCIE_HOTPLUG,
611+
};
612+
613+
static inline void __pcie_update_link_speed(struct pci_bus *bus,
614+
enum pcie_link_change_reason reason,
615+
u16 linksta, u16 linksta2)
605616
{
606617
bus->cur_bus_speed = pcie_link_speed[linksta & PCI_EXP_LNKSTA_CLS];
607618
bus->flit_mode = (linksta2 & PCI_EXP_LNKSTA2_FLIT) ? 1 : 0;
619+
620+
trace_pcie_link_event(bus,
621+
reason,
622+
FIELD_GET(PCI_EXP_LNKSTA_NLW, linksta),
623+
linksta & PCI_EXP_LNKSTA_LINK_STATUS_MASK);
608624
}
609-
void pcie_update_link_speed(struct pci_bus *bus);
625+
626+
void pcie_update_link_speed(struct pci_bus *bus, enum pcie_link_change_reason reason);
610627

611628
/* Single Root I/O Virtualization */
612629
struct pci_sriov {

drivers/pci/pcie/bwctrl.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ static void pcie_bwnotif_enable(struct pcie_device *srv)
199199
* Update after enabling notifications & clearing status bits ensures
200200
* link speed is up to date.
201201
*/
202-
pcie_update_link_speed(port->subordinate);
202+
pcie_update_link_speed(port->subordinate, PCIE_BWCTRL_ENABLE);
203203
}
204204

205205
static void pcie_bwnotif_disable(struct pci_dev *port)
@@ -234,7 +234,7 @@ static irqreturn_t pcie_bwnotif_irq(int irq, void *context)
234234
* speed (inside pcie_update_link_speed()) after LBMS has been
235235
* cleared to avoid missing link speed changes.
236236
*/
237-
pcie_update_link_speed(port->subordinate);
237+
pcie_update_link_speed(port->subordinate, PCIE_BWCTRL_IRQ);
238238

239239
return IRQ_HANDLED;
240240
}

drivers/pci/probe.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <linux/irqdomain.h>
2424
#include <linux/pm_runtime.h>
2525
#include <linux/bitfield.h>
26+
#include <trace/events/pci.h>
2627
#include "pci.h"
2728

2829
static struct resource busn_resource = {
@@ -820,14 +821,16 @@ const char *pci_speed_string(enum pci_bus_speed speed)
820821
}
821822
EXPORT_SYMBOL_GPL(pci_speed_string);
822823

823-
void pcie_update_link_speed(struct pci_bus *bus)
824+
void pcie_update_link_speed(struct pci_bus *bus,
825+
enum pcie_link_change_reason reason)
824826
{
825827
struct pci_dev *bridge = bus->self;
826828
u16 linksta, linksta2;
827829

828830
pcie_capability_read_word(bridge, PCI_EXP_LNKSTA, &linksta);
829831
pcie_capability_read_word(bridge, PCI_EXP_LNKSTA2, &linksta2);
830-
__pcie_update_link_speed(bus, linksta, linksta2);
832+
833+
__pcie_update_link_speed(bus, reason, linksta, linksta2);
831834
}
832835
EXPORT_SYMBOL_GPL(pcie_update_link_speed);
833836

@@ -914,7 +917,7 @@ static void pci_set_bus_speed(struct pci_bus *bus)
914917
pcie_capability_read_dword(bridge, PCI_EXP_LNKCAP, &linkcap);
915918
bus->max_bus_speed = pcie_link_speed[linkcap & PCI_EXP_LNKCAP_SLS];
916919

917-
pcie_update_link_speed(bus);
920+
pcie_update_link_speed(bus, PCIE_ADD_BUS);
918921
}
919922
}
920923

drivers/pci/trace.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
/*
3+
* Tracepoints for PCI system
4+
*
5+
* Copyright (C) 2025 Alibaba Corporation
6+
*/
7+
8+
#include <linux/pci.h>
9+
10+
#define CREATE_TRACE_POINTS
11+
#include <trace/events/pci.h>

0 commit comments

Comments
 (0)