Merge tag 'iommu-fixes-v4.13-rc6' of git://git.kernel.org/pub/scm/linux/kernel/git/joro/iommu

Pull IOMMU fix from Joerg Roedel:
"Another fix, this time in common IOMMU sysfs code.

In the conversion from the old iommu sysfs-code to the
iommu_device_register interface, I missed to update the release path
for the struct device associated with an IOMMU. It freed the 'struct
device', which was a pointer before, but is now embedded in another
struct.

Freeing from the middle of allocated memory had all kinds of nasty
side effects when an IOMMU was unplugged. Unfortunatly nobody
unplugged and IOMMU until now, so this was not discovered earlier. The
fix is to make the 'struct device' a pointer again"

* tag 'iommu-fixes-v4.13-rc6' of git://git.kernel.org/pub/scm/linux/kernel/git/joro/iommu:
iommu: Fix wrong freeing of iommu_device->dev

Changed files
+37 -15
drivers
include
linux
+3 -1
drivers/iommu/amd_iommu_types.h
··· 574 574 575 575 static inline struct amd_iommu *dev_to_amd_iommu(struct device *dev) 576 576 { 577 - return container_of(dev, struct amd_iommu, iommu.dev); 577 + struct iommu_device *iommu = dev_to_iommu_device(dev); 578 + 579 + return container_of(iommu, struct amd_iommu, iommu); 578 580 } 579 581 580 582 #define ACPIHID_UID_LEN 256
+3 -1
drivers/iommu/intel-iommu.c
··· 4736 4736 4737 4737 static inline struct intel_iommu *dev_to_intel_iommu(struct device *dev) 4738 4738 { 4739 - return container_of(dev, struct intel_iommu, iommu.dev); 4739 + struct iommu_device *iommu_dev = dev_to_iommu_device(dev); 4740 + 4741 + return container_of(iommu_dev, struct intel_iommu, iommu); 4740 4742 } 4741 4743 4742 4744 static ssize_t intel_iommu_show_version(struct device *dev,
+20 -12
drivers/iommu/iommu-sysfs.c
··· 62 62 va_list vargs; 63 63 int ret; 64 64 65 - device_initialize(&iommu->dev); 65 + iommu->dev = kzalloc(sizeof(*iommu->dev), GFP_KERNEL); 66 + if (!iommu->dev) 67 + return -ENOMEM; 66 68 67 - iommu->dev.class = &iommu_class; 68 - iommu->dev.parent = parent; 69 - iommu->dev.groups = groups; 69 + device_initialize(iommu->dev); 70 + 71 + iommu->dev->class = &iommu_class; 72 + iommu->dev->parent = parent; 73 + iommu->dev->groups = groups; 70 74 71 75 va_start(vargs, fmt); 72 - ret = kobject_set_name_vargs(&iommu->dev.kobj, fmt, vargs); 76 + ret = kobject_set_name_vargs(&iommu->dev->kobj, fmt, vargs); 73 77 va_end(vargs); 74 78 if (ret) 75 79 goto error; 76 80 77 - ret = device_add(&iommu->dev); 81 + ret = device_add(iommu->dev); 78 82 if (ret) 79 83 goto error; 84 + 85 + dev_set_drvdata(iommu->dev, iommu); 80 86 81 87 return 0; 82 88 83 89 error: 84 - put_device(&iommu->dev); 90 + put_device(iommu->dev); 85 91 return ret; 86 92 } 87 93 88 94 void iommu_device_sysfs_remove(struct iommu_device *iommu) 89 95 { 90 - device_unregister(&iommu->dev); 96 + dev_set_drvdata(iommu->dev, NULL); 97 + device_unregister(iommu->dev); 98 + iommu->dev = NULL; 91 99 } 92 100 /* 93 101 * IOMMU drivers can indicate a device is managed by a given IOMMU using ··· 110 102 if (!iommu || IS_ERR(iommu)) 111 103 return -ENODEV; 112 104 113 - ret = sysfs_add_link_to_group(&iommu->dev.kobj, "devices", 105 + ret = sysfs_add_link_to_group(&iommu->dev->kobj, "devices", 114 106 &link->kobj, dev_name(link)); 115 107 if (ret) 116 108 return ret; 117 109 118 - ret = sysfs_create_link_nowarn(&link->kobj, &iommu->dev.kobj, "iommu"); 110 + ret = sysfs_create_link_nowarn(&link->kobj, &iommu->dev->kobj, "iommu"); 119 111 if (ret) 120 - sysfs_remove_link_from_group(&iommu->dev.kobj, "devices", 112 + sysfs_remove_link_from_group(&iommu->dev->kobj, "devices", 121 113 dev_name(link)); 122 114 123 115 return ret; ··· 129 121 return; 130 122 131 123 sysfs_remove_link(&link->kobj, "iommu"); 132 - sysfs_remove_link_from_group(&iommu->dev.kobj, "devices", dev_name(link)); 124 + sysfs_remove_link_from_group(&iommu->dev->kobj, "devices", dev_name(link)); 133 125 }
+11 -1
include/linux/iommu.h
··· 240 240 struct list_head list; 241 241 const struct iommu_ops *ops; 242 242 struct fwnode_handle *fwnode; 243 - struct device dev; 243 + struct device *dev; 244 244 }; 245 245 246 246 int iommu_device_register(struct iommu_device *iommu); ··· 263 263 struct fwnode_handle *fwnode) 264 264 { 265 265 iommu->fwnode = fwnode; 266 + } 267 + 268 + static inline struct iommu_device *dev_to_iommu_device(struct device *dev) 269 + { 270 + return (struct iommu_device *)dev_get_drvdata(dev); 266 271 } 267 272 268 273 #define IOMMU_GROUP_NOTIFY_ADD_DEVICE 1 /* Device added */ ··· 592 587 static inline void iommu_device_set_fwnode(struct iommu_device *iommu, 593 588 struct fwnode_handle *fwnode) 594 589 { 590 + } 591 + 592 + static inline struct iommu_device *dev_to_iommu_device(struct device *dev) 593 + { 594 + return NULL; 595 595 } 596 596 597 597 static inline void iommu_device_unregister(struct iommu_device *iommu)