Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

authored by

Thomas Weißschuh and committed by
Greg Kroah-Hartman
c1426d39 67237183

+80 -122
+2 -56
drivers/misc/pvpanic/pvpanic-mmio.c
··· 24 24 MODULE_DESCRIPTION("pvpanic-mmio device driver"); 25 25 MODULE_LICENSE("GPL"); 26 26 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 - 69 27 static int pvpanic_mmio_probe(struct platform_device *pdev) 70 28 { 71 29 struct device *dev = &pdev->dev; 72 - struct pvpanic_instance *pi; 73 30 struct resource *res; 74 31 void __iomem *base; 75 32 ··· 49 92 return -EINVAL; 50 93 } 51 94 52 - pi = devm_kmalloc(dev, sizeof(*pi), GFP_KERNEL); 53 - if (!pi) 54 - return -ENOMEM; 55 - 56 - pi->base = base; 57 - pi->capability = PVPANIC_PANICKED | PVPANIC_CRASH_LOADED; 58 - 59 - /* initialize capability by RDPT */ 60 - pi->capability &= ioread8(base); 61 - pi->events = pi->capability; 62 - 63 - return devm_pvpanic_probe(dev, pi); 95 + return devm_pvpanic_probe(dev, base); 64 96 } 65 97 66 98 static const struct of_device_id pvpanic_mmio_match[] = { ··· 69 123 .name = "pvpanic-mmio", 70 124 .of_match_table = pvpanic_mmio_match, 71 125 .acpi_match_table = pvpanic_device_ids, 72 - .dev_groups = pvpanic_mmio_dev_groups, 126 + .dev_groups = pvpanic_dev_groups, 73 127 }, 74 128 .probe = pvpanic_mmio_probe, 75 129 };
+2 -56
drivers/misc/pvpanic/pvpanic-pci.c
··· 22 22 MODULE_DESCRIPTION("pvpanic device driver"); 23 23 MODULE_LICENSE("GPL"); 24 24 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 - 67 25 static int pvpanic_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent) 68 26 { 69 - struct pvpanic_instance *pi; 70 27 void __iomem *base; 71 28 int ret; 72 29 ··· 35 78 if (!base) 36 79 return -ENOMEM; 37 80 38 - pi = devm_kmalloc(&pdev->dev, sizeof(*pi), GFP_KERNEL); 39 - if (!pi) 40 - return -ENOMEM; 41 - 42 - pi->base = base; 43 - pi->capability = PVPANIC_PANICKED | PVPANIC_CRASH_LOADED; 44 - 45 - /* initlize capability by RDPT */ 46 - pi->capability &= ioread8(base); 47 - pi->events = pi->capability; 48 - 49 - return devm_pvpanic_probe(&pdev->dev, pi); 81 + return devm_pvpanic_probe(&pdev->dev, base); 50 82 } 51 83 52 84 static const struct pci_device_id pvpanic_pci_id_tbl[] = { ··· 49 103 .id_table = pvpanic_pci_id_tbl, 50 104 .probe = pvpanic_pci_probe, 51 105 .driver = { 52 - .dev_groups = pvpanic_pci_dev_groups, 106 + .dev_groups = pvpanic_dev_groups, 53 107 }, 54 108 }; 55 109 module_pci_driver(pvpanic_pci_driver);
+74 -2
drivers/misc/pvpanic/pvpanic.c
··· 7 7 * Copyright (C) 2021 Oracle. 8 8 */ 9 9 10 + #include <linux/device.h> 10 11 #include <linux/io.h> 11 12 #include <linux/kernel.h> 12 13 #include <linux/kexec.h> ··· 26 25 MODULE_AUTHOR("Mihai Carabas <mihai.carabas@oracle.com>"); 27 26 MODULE_DESCRIPTION("pvpanic device driver"); 28 27 MODULE_LICENSE("GPL"); 28 + 29 + struct pvpanic_instance { 30 + void __iomem *base; 31 + unsigned int capability; 32 + unsigned int events; 33 + struct list_head list; 34 + }; 29 35 30 36 static struct list_head pvpanic_list; 31 37 static spinlock_t pvpanic_lock; ··· 89 81 spin_unlock(&pvpanic_lock); 90 82 } 91 83 92 - int devm_pvpanic_probe(struct device *dev, struct pvpanic_instance *pi) 84 + static ssize_t capability_show(struct device *dev, struct device_attribute *attr, char *buf) 93 85 { 94 - if (!pi || !pi->base) 86 + struct pvpanic_instance *pi = dev_get_drvdata(dev); 87 + 88 + return sysfs_emit(buf, "%x\n", pi->capability); 89 + } 90 + static DEVICE_ATTR_RO(capability); 91 + 92 + static ssize_t events_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->events); 97 + } 98 + 99 + static ssize_t events_store(struct device *dev, struct device_attribute *attr, 100 + const char *buf, size_t count) 101 + { 102 + struct pvpanic_instance *pi = dev_get_drvdata(dev); 103 + unsigned int tmp; 104 + int err; 105 + 106 + err = kstrtouint(buf, 16, &tmp); 107 + if (err) 108 + return err; 109 + 110 + if ((tmp & pi->capability) != tmp) 95 111 return -EINVAL; 112 + 113 + pi->events = tmp; 114 + 115 + return count; 116 + } 117 + static DEVICE_ATTR_RW(events); 118 + 119 + static struct attribute *pvpanic_dev_attrs[] = { 120 + &dev_attr_capability.attr, 121 + &dev_attr_events.attr, 122 + NULL 123 + }; 124 + 125 + static const struct attribute_group pvpanic_dev_group = { 126 + .attrs = pvpanic_dev_attrs, 127 + }; 128 + 129 + const struct attribute_group *pvpanic_dev_groups[] = { 130 + &pvpanic_dev_group, 131 + NULL 132 + }; 133 + EXPORT_SYMBOL_GPL(pvpanic_dev_groups); 134 + 135 + int devm_pvpanic_probe(struct device *dev, void __iomem *base) 136 + { 137 + struct pvpanic_instance *pi; 138 + 139 + if (!base) 140 + return -EINVAL; 141 + 142 + pi = devm_kmalloc(dev, sizeof(*pi), GFP_KERNEL); 143 + if (!pi) 144 + return -ENOMEM; 145 + 146 + pi->base = base; 147 + pi->capability = PVPANIC_PANICKED | PVPANIC_CRASH_LOADED; 148 + 149 + /* initlize capability by RDPT */ 150 + pi->capability &= ioread8(base); 151 + pi->events = pi->capability; 96 152 97 153 spin_lock(&pvpanic_lock); 98 154 list_add(&pi->list, &pvpanic_list);
+2 -8
drivers/misc/pvpanic/pvpanic.h
··· 8 8 #ifndef PVPANIC_H_ 9 9 #define PVPANIC_H_ 10 10 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[]; 19 13 20 14 #endif /* PVPANIC_H_ */