Skip to content

Commit c1426d3

Browse files
t-8chgregkh
authored andcommitted
misc/pvpanic: deduplicate common code
pvpanic-mmio.c and pvpanic-pci.c share a lot of code. Refactor it into pvpanic.c where it doesn't have to be kept in sync manually and where the core logic can be understood more easily. No functional change. Signed-off-by: Thomas Weißschuh <linux@weissschuh.net> Link: https://lore.kernel.org/r/20231011-pvpanic-cleanup-v2-1-4b21d56f779f@weissschuh.net Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 6723718 commit c1426d3

4 files changed

Lines changed: 80 additions & 122 deletions

File tree

drivers/misc/pvpanic/pvpanic-mmio.c

Lines changed: 2 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -24,52 +24,9 @@ MODULE_AUTHOR("Hu Tao <hutao@cn.fujitsu.com>");
2424
MODULE_DESCRIPTION("pvpanic-mmio device driver");
2525
MODULE_LICENSE("GPL");
2626

27-
static ssize_t capability_show(struct device *dev, struct device_attribute *attr, char *buf)
28-
{
29-
struct pvpanic_instance *pi = dev_get_drvdata(dev);
30-
31-
return sysfs_emit(buf, "%x\n", pi->capability);
32-
}
33-
static DEVICE_ATTR_RO(capability);
34-
35-
static ssize_t events_show(struct device *dev, struct device_attribute *attr, char *buf)
36-
{
37-
struct pvpanic_instance *pi = dev_get_drvdata(dev);
38-
39-
return sysfs_emit(buf, "%x\n", pi->events);
40-
}
41-
42-
static ssize_t events_store(struct device *dev, struct device_attribute *attr,
43-
const char *buf, size_t count)
44-
{
45-
struct pvpanic_instance *pi = dev_get_drvdata(dev);
46-
unsigned int tmp;
47-
int err;
48-
49-
err = kstrtouint(buf, 16, &tmp);
50-
if (err)
51-
return err;
52-
53-
if ((tmp & pi->capability) != tmp)
54-
return -EINVAL;
55-
56-
pi->events = tmp;
57-
58-
return count;
59-
}
60-
static DEVICE_ATTR_RW(events);
61-
62-
static struct attribute *pvpanic_mmio_dev_attrs[] = {
63-
&dev_attr_capability.attr,
64-
&dev_attr_events.attr,
65-
NULL
66-
};
67-
ATTRIBUTE_GROUPS(pvpanic_mmio_dev);
68-
6927
static int pvpanic_mmio_probe(struct platform_device *pdev)
7028
{
7129
struct device *dev = &pdev->dev;
72-
struct pvpanic_instance *pi;
7330
struct resource *res;
7431
void __iomem *base;
7532

@@ -92,18 +49,7 @@ static int pvpanic_mmio_probe(struct platform_device *pdev)
9249
return -EINVAL;
9350
}
9451

95-
pi = devm_kmalloc(dev, sizeof(*pi), GFP_KERNEL);
96-
if (!pi)
97-
return -ENOMEM;
98-
99-
pi->base = base;
100-
pi->capability = PVPANIC_PANICKED | PVPANIC_CRASH_LOADED;
101-
102-
/* initialize capability by RDPT */
103-
pi->capability &= ioread8(base);
104-
pi->events = pi->capability;
105-
106-
return devm_pvpanic_probe(dev, pi);
52+
return devm_pvpanic_probe(dev, base);
10753
}
10854

10955
static const struct of_device_id pvpanic_mmio_match[] = {
@@ -123,7 +69,7 @@ static struct platform_driver pvpanic_mmio_driver = {
12369
.name = "pvpanic-mmio",
12470
.of_match_table = pvpanic_mmio_match,
12571
.acpi_match_table = pvpanic_device_ids,
126-
.dev_groups = pvpanic_mmio_dev_groups,
72+
.dev_groups = pvpanic_dev_groups,
12773
},
12874
.probe = pvpanic_mmio_probe,
12975
};

drivers/misc/pvpanic/pvpanic-pci.c

Lines changed: 2 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -22,51 +22,8 @@ MODULE_AUTHOR("Mihai Carabas <mihai.carabas@oracle.com>");
2222
MODULE_DESCRIPTION("pvpanic device driver");
2323
MODULE_LICENSE("GPL");
2424

25-
static ssize_t capability_show(struct device *dev, struct device_attribute *attr, char *buf)
26-
{
27-
struct pvpanic_instance *pi = dev_get_drvdata(dev);
28-
29-
return sysfs_emit(buf, "%x\n", pi->capability);
30-
}
31-
static DEVICE_ATTR_RO(capability);
32-
33-
static ssize_t events_show(struct device *dev, struct device_attribute *attr, char *buf)
34-
{
35-
struct pvpanic_instance *pi = dev_get_drvdata(dev);
36-
37-
return sysfs_emit(buf, "%x\n", pi->events);
38-
}
39-
40-
static ssize_t events_store(struct device *dev, struct device_attribute *attr,
41-
const char *buf, size_t count)
42-
{
43-
struct pvpanic_instance *pi = dev_get_drvdata(dev);
44-
unsigned int tmp;
45-
int err;
46-
47-
err = kstrtouint(buf, 16, &tmp);
48-
if (err)
49-
return err;
50-
51-
if ((tmp & pi->capability) != tmp)
52-
return -EINVAL;
53-
54-
pi->events = tmp;
55-
56-
return count;
57-
}
58-
static DEVICE_ATTR_RW(events);
59-
60-
static struct attribute *pvpanic_pci_dev_attrs[] = {
61-
&dev_attr_capability.attr,
62-
&dev_attr_events.attr,
63-
NULL
64-
};
65-
ATTRIBUTE_GROUPS(pvpanic_pci_dev);
66-
6725
static int pvpanic_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
6826
{
69-
struct pvpanic_instance *pi;
7027
void __iomem *base;
7128
int ret;
7229

@@ -78,18 +35,7 @@ static int pvpanic_pci_probe(struct pci_dev *pdev, const struct pci_device_id *e
7835
if (!base)
7936
return -ENOMEM;
8037

81-
pi = devm_kmalloc(&pdev->dev, sizeof(*pi), GFP_KERNEL);
82-
if (!pi)
83-
return -ENOMEM;
84-
85-
pi->base = base;
86-
pi->capability = PVPANIC_PANICKED | PVPANIC_CRASH_LOADED;
87-
88-
/* initlize capability by RDPT */
89-
pi->capability &= ioread8(base);
90-
pi->events = pi->capability;
91-
92-
return devm_pvpanic_probe(&pdev->dev, pi);
38+
return devm_pvpanic_probe(&pdev->dev, base);
9339
}
9440

9541
static const struct pci_device_id pvpanic_pci_id_tbl[] = {
@@ -103,7 +49,7 @@ static struct pci_driver pvpanic_pci_driver = {
10349
.id_table = pvpanic_pci_id_tbl,
10450
.probe = pvpanic_pci_probe,
10551
.driver = {
106-
.dev_groups = pvpanic_pci_dev_groups,
52+
.dev_groups = pvpanic_dev_groups,
10753
},
10854
};
10955
module_pci_driver(pvpanic_pci_driver);

drivers/misc/pvpanic/pvpanic.c

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* Copyright (C) 2021 Oracle.
88
*/
99

10+
#include <linux/device.h>
1011
#include <linux/io.h>
1112
#include <linux/kernel.h>
1213
#include <linux/kexec.h>
@@ -26,6 +27,13 @@ MODULE_AUTHOR("Mihai Carabas <mihai.carabas@oracle.com>");
2627
MODULE_DESCRIPTION("pvpanic device driver");
2728
MODULE_LICENSE("GPL");
2829

30+
struct pvpanic_instance {
31+
void __iomem *base;
32+
unsigned int capability;
33+
unsigned int events;
34+
struct list_head list;
35+
};
36+
2937
static struct list_head pvpanic_list;
3038
static spinlock_t pvpanic_lock;
3139

@@ -81,11 +89,75 @@ static void pvpanic_remove(void *param)
8189
spin_unlock(&pvpanic_lock);
8290
}
8391

84-
int devm_pvpanic_probe(struct device *dev, struct pvpanic_instance *pi)
92+
static ssize_t capability_show(struct device *dev, struct device_attribute *attr, char *buf)
93+
{
94+
struct pvpanic_instance *pi = dev_get_drvdata(dev);
95+
96+
return sysfs_emit(buf, "%x\n", pi->capability);
97+
}
98+
static DEVICE_ATTR_RO(capability);
99+
100+
static ssize_t events_show(struct device *dev, struct device_attribute *attr, char *buf)
101+
{
102+
struct pvpanic_instance *pi = dev_get_drvdata(dev);
103+
104+
return sysfs_emit(buf, "%x\n", pi->events);
105+
}
106+
107+
static ssize_t events_store(struct device *dev, struct device_attribute *attr,
108+
const char *buf, size_t count)
109+
{
110+
struct pvpanic_instance *pi = dev_get_drvdata(dev);
111+
unsigned int tmp;
112+
int err;
113+
114+
err = kstrtouint(buf, 16, &tmp);
115+
if (err)
116+
return err;
117+
118+
if ((tmp & pi->capability) != tmp)
119+
return -EINVAL;
120+
121+
pi->events = tmp;
122+
123+
return count;
124+
}
125+
static DEVICE_ATTR_RW(events);
126+
127+
static struct attribute *pvpanic_dev_attrs[] = {
128+
&dev_attr_capability.attr,
129+
&dev_attr_events.attr,
130+
NULL
131+
};
132+
133+
static const struct attribute_group pvpanic_dev_group = {
134+
.attrs = pvpanic_dev_attrs,
135+
};
136+
137+
const struct attribute_group *pvpanic_dev_groups[] = {
138+
&pvpanic_dev_group,
139+
NULL
140+
};
141+
EXPORT_SYMBOL_GPL(pvpanic_dev_groups);
142+
143+
int devm_pvpanic_probe(struct device *dev, void __iomem *base)
85144
{
86-
if (!pi || !pi->base)
145+
struct pvpanic_instance *pi;
146+
147+
if (!base)
87148
return -EINVAL;
88149

150+
pi = devm_kmalloc(dev, sizeof(*pi), GFP_KERNEL);
151+
if (!pi)
152+
return -ENOMEM;
153+
154+
pi->base = base;
155+
pi->capability = PVPANIC_PANICKED | PVPANIC_CRASH_LOADED;
156+
157+
/* initlize capability by RDPT */
158+
pi->capability &= ioread8(base);
159+
pi->events = pi->capability;
160+
89161
spin_lock(&pvpanic_lock);
90162
list_add(&pi->list, &pvpanic_list);
91163
spin_unlock(&pvpanic_lock);

drivers/misc/pvpanic/pvpanic.h

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,7 @@
88
#ifndef PVPANIC_H_
99
#define PVPANIC_H_
1010

11-
struct pvpanic_instance {
12-
void __iomem *base;
13-
unsigned int capability;
14-
unsigned int events;
15-
struct list_head list;
16-
};
17-
18-
int devm_pvpanic_probe(struct device *dev, struct pvpanic_instance *pi);
11+
int devm_pvpanic_probe(struct device *dev, void __iomem *base);
12+
extern const struct attribute_group *pvpanic_dev_groups[];
1913

2014
#endif /* PVPANIC_H_ */

0 commit comments

Comments
 (0)