Pull libnvdimm fixes from Dan Williams: "A collection of locking and async operations fixes for v5.3-rc2. These had been soaking in a branch targeting the merge window, but missed due to a regression hunt. This fixed up version has otherwise been in -next this past week with no reported issues. In order to gain confidence in the locking changes the pull also includes a debug / instrumentation patch to enable lockdep coverage for libnvdimm subsystem operations that depend on the device_lock for exclusion. As mentioned in the changelog it is a hack, but it works and documents the locking expectations of the sub-system in a way that others can use lockdep to verify. The driver core touches got an ack from Greg. Summary: - Fix duplicate device_unregister() calls (multiple threads competing to do unregister work when scheduling device removal from a sysfs attribute of the self-same device). - Fix badblocks registration order bug. Ensure region badblocks are initialized in advance of namespace registration. - Fix a deadlock between the bus lock and probe operations. - Export device-core infrastructure to coordinate async operations via the device ->dead state. - Add device-core infrastructure to validate device_lock() usage with lockdep" * tag 'libnvdimm-fixes-5.3-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/nvdimm/nvdimm: driver-core, libnvdimm: Let device subsystems add local lockdep coverage libnvdimm/bus: Fix wait_nvdimm_bus_probe_idle() ABBA deadlock libnvdimm/bus: Stop holding nvdimm_bus_list_mutex over __nd_ioctl() libnvdimm/bus: Prepare the nd_ioctl() path to be re-entrant libnvdimm/region: Register badblocks before namespaces libnvdimm/bus: Prevent duplicate device_unregister() calls drivers/base: Introduce kill_device()
Unified · Split
| @@ -1282,7 +1282,7 @@ static ssize_t hw_error_scrub_store(struct device *dev, · 1281 unmodified lines | ||
| 1282 | 1282 | if (rc) |
| 1283 | 1283 | return rc; |
| 1284 | 1284 | |
| 1285 | − device_lock(dev); | |
| 1285 | + nfit_device_lock(dev); | |
| 1286 | 1286 | nd_desc = dev_get_drvdata(dev); |
| 1287 | 1287 | if (nd_desc) { |
| 1288 | 1288 | struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc); |
| @@ -1299,7 +1299,7 @@ static ssize_t hw_error_scrub_store(struct device *dev, · 10 unmodified lines | ||
| 1299 | 1299 | break; |
| 1300 | 1300 | } |
| 1301 | 1301 | } |
| 1302 | − device_unlock(dev); | |
| 1302 | + nfit_device_unlock(dev); | |
| 1303 | 1303 | if (rc) |
| 1304 | 1304 | return rc; |
| 1305 | 1305 | return size; |
| @@ -1319,7 +1319,7 @@ static ssize_t scrub_show(struct device *dev, · 13 unmodified lines | ||
| 1319 | 1319 | ssize_t rc = -ENXIO; |
| 1320 | 1320 | bool busy; |
| 1321 | 1321 | |
| 1322 | − device_lock(dev); | |
| 1322 | + nfit_device_lock(dev); | |
| 1323 | 1323 | nd_desc = dev_get_drvdata(dev); |
| 1324 | 1324 | if (!nd_desc) { |
| 1325 | 1325 | device_unlock(dev); |
| @@ -1339,7 +1339,7 @@ static ssize_t scrub_show(struct device *dev, · 13 unmodified lines | ||
| 1339 | 1339 | } |
| 1340 | 1340 | |
| 1341 | 1341 | mutex_unlock(&acpi_desc->init_mutex); |
| 1342 | − device_unlock(dev); | |
| 1342 | + nfit_device_unlock(dev); | |
| 1343 | 1343 | return rc; |
| 1344 | 1344 | } |
| 1345 | 1345 | |
| @@ -1356,14 +1356,14 @@ static ssize_t scrub_store(struct device *dev, · 10 unmodified lines | ||
| 1356 | 1356 | if (val != 1) |
| 1357 | 1357 | return -EINVAL; |
| 1358 | 1358 | |
| 1359 | − device_lock(dev); | |
| 1359 | + nfit_device_lock(dev); | |
| 1360 | 1360 | nd_desc = dev_get_drvdata(dev); |
| 1361 | 1361 | if (nd_desc) { |
| 1362 | 1362 | struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc); |
| 1363 | 1363 | |
| 1364 | 1364 | rc = acpi_nfit_ars_rescan(acpi_desc, ARS_REQ_LONG); |
| 1365 | 1365 | } |
| 1366 | − device_unlock(dev); | |
| 1366 | + nfit_device_unlock(dev); | |
| 1367 | 1367 | if (rc) |
| 1368 | 1368 | return rc; |
| 1369 | 1369 | return size; |
| @@ -1749,9 +1749,9 @@ static void acpi_nvdimm_notify(acpi_handle handle, u32 event, void *data) · 379 unmodified lines | ||
| 1749 | 1749 | struct acpi_device *adev = data; |
| 1750 | 1750 | struct device *dev = &adev->dev; |
| 1751 | 1751 | |
| 1752 | − device_lock(dev->parent); | |
| 1752 | + nfit_device_lock(dev->parent); | |
| 1753 | 1753 | __acpi_nvdimm_notify(dev, event); |
| 1754 | − device_unlock(dev->parent); | |
| 1754 | + nfit_device_unlock(dev->parent); | |
| 1755 | 1755 | } |
| 1756 | 1756 | |
| 1757 | 1757 | static bool acpi_nvdimm_has_method(struct acpi_device *adev, char *method) |
| @@ -3457,8 +3457,8 @@ static int acpi_nfit_flush_probe(struct nvdimm_bus_descriptor *nd_desc) · 1699 unmodified lines | ||
| 3457 | 3457 | struct device *dev = acpi_desc->dev; |
| 3458 | 3458 | |
| 3459 | 3459 | /* Bounce the device lock to flush acpi_nfit_add / acpi_nfit_notify */ |
| 3460 | − device_lock(dev); | |
| 3461 | − device_unlock(dev); | |
| 3460 | + nfit_device_lock(dev); | |
| 3461 | + nfit_device_unlock(dev); | |
| 3462 | 3462 | |
| 3463 | 3463 | /* Bounce the init_mutex to complete initial registration */ |
| 3464 | 3464 | mutex_lock(&acpi_desc->init_mutex); |
| @@ -3602,8 +3602,8 @@ void acpi_nfit_shutdown(void *data) · 137 unmodified lines | ||
| 3602 | 3602 | * acpi_nfit_ars_rescan() submissions have had a chance to |
| 3603 | 3603 | * either submit or see ->cancel set. |
| 3604 | 3604 | */ |
| 3605 | − device_lock(bus_dev); | |
| 3606 | − device_unlock(bus_dev); | |
| 3605 | + nfit_device_lock(bus_dev); | |
| 3606 | + nfit_device_unlock(bus_dev); | |
| 3607 | 3607 | |
| 3608 | 3608 | flush_workqueue(nfit_wq); |
| 3609 | 3609 | } |
| @@ -3746,9 +3746,9 @@ EXPORT_SYMBOL_GPL(__acpi_nfit_notify); · 136 unmodified lines | ||
| 3746 | 3746 | |
| 3747 | 3747 | static void acpi_nfit_notify(struct acpi_device *adev, u32 event) |
| 3748 | 3748 | { |
| 3749 | − device_lock(&adev->dev); | |
| 3749 | + nfit_device_lock(&adev->dev); | |
| 3750 | 3750 | __acpi_nfit_notify(&adev->dev, adev->handle, event); |
| 3751 | − device_unlock(&adev->dev); | |
| 3751 | + nfit_device_unlock(&adev->dev); | |
| 3752 | 3752 | } |
| 3753 | 3753 | |
| 3754 | 3754 | static const struct acpi_device_id acpi_nfit_ids[] = { |
| @@ -312,6 +312,30 @@ static inline struct acpi_nfit_desc *to_acpi_desc( · 311 unmodified lines | ||
| 312 | 312 | return container_of(nd_desc, struct acpi_nfit_desc, nd_desc); |
| 313 | 313 | } |
| 314 | 314 | |
| 315 | +#ifdef CONFIG_PROVE_LOCKING | |
| 316 | +static inline void nfit_device_lock(struct device *dev) | |
| 317 | +{ | |
| 318 | + device_lock(dev); | |
| 319 | + mutex_lock(&dev->lockdep_mutex); | |
| 320 | +} | |
| 321 | + | |
| 322 | +static inline void nfit_device_unlock(struct device *dev) | |
| 323 | +{ | |
| 324 | + mutex_unlock(&dev->lockdep_mutex); | |
| 325 | + device_unlock(dev); | |
| 326 | +} | |
| 327 | +#else | |
| 328 | +static inline void nfit_device_lock(struct device *dev) | |
| 329 | +{ | |
| 330 | + device_lock(dev); | |
| 331 | +} | |
| 332 | + | |
| 333 | +static inline void nfit_device_unlock(struct device *dev) | |
| 334 | +{ | |
| 335 | + device_unlock(dev); | |
| 336 | +} | |
| 337 | +#endif | |
| 338 | + | |
| 315 | 339 | const guid_t *to_nfit_uuid(enum nfit_uuids id); |
| 316 | 340 | int acpi_nfit_init(struct acpi_nfit_desc *acpi_desc, void *nfit, acpi_size sz); |
| 317 | 341 | void acpi_nfit_shutdown(void *data); |
| @@ -1663,6 +1663,9 @@ void device_initialize(struct device *dev) · 1662 unmodified lines | ||
| 1663 | 1663 | kobject_init(&dev->kobj, &device_ktype); |
| 1664 | 1664 | INIT_LIST_HEAD(&dev->dma_pools); |
| 1665 | 1665 | mutex_init(&dev->mutex); |
| 1666 | +#ifdef CONFIG_PROVE_LOCKING | |
| 1667 | + mutex_init(&dev->lockdep_mutex); | |
| 1668 | +#endif | |
| 1666 | 1669 | lockdep_set_novalidate_class(&dev->mutex); |
| 1667 | 1670 | spin_lock_init(&dev->devres_lock); |
| 1668 | 1671 | INIT_LIST_HEAD(&dev->devres_head); |
| @@ -2211,6 +2214,24 @@ void put_device(struct device *dev) · 542 unmodified lines | ||
| 2211 | 2214 | } |
| 2212 | 2215 | EXPORT_SYMBOL_GPL(put_device); |
| 2213 | 2216 | |
| 2217 | +bool kill_device(struct device *dev) | |
| 2218 | +{ | |
| 2219 | + /* | |
| 2220 | + * Require the device lock and set the "dead" flag to guarantee that | |
| 2221 | + * the update behavior is consistent with the other bitfields near | |
| 2222 | + * it and that we cannot have an asynchronous probe routine trying | |
| 2223 | + * to run while we are tearing out the bus/class/sysfs from | |
| 2224 | + * underneath the device. | |
| 2225 | + */ | |
| 2226 | + lockdep_assert_held(&dev->mutex); | |
| 2227 | + | |
| 2228 | + if (dev->p->dead) | |
| 2229 | + return false; | |
| 2230 | + dev->p->dead = true; | |
| 2231 | + return true; | |
| 2232 | +} | |
| 2233 | +EXPORT_SYMBOL_GPL(kill_device); | |
| 2234 | + | |
| 2214 | 2235 | /** |
| 2215 | 2236 | * device_del - delete device from system. |
| 2216 | 2237 | * @dev: device. |
| @@ -2230,15 +2251,8 @@ void device_del(struct device *dev) · 13 unmodified lines | ||
| 2230 | 2251 | struct kobject *glue_dir = NULL; |
| 2231 | 2252 | struct class_interface *class_intf; |
| 2232 | 2253 | |
| 2233 | − /* | |
| 2234 | − * Hold the device lock and set the "dead" flag to guarantee that | |
| 2235 | − * the update behavior is consistent with the other bitfields near | |
| 2236 | − * it and that we cannot have an asynchronous probe routine trying | |
| 2237 | − * to run while we are tearing out the bus/class/sysfs from | |
| 2238 | − * underneath the device. | |
| 2239 | − */ | |
| 2240 | 2254 | device_lock(dev); |
| 2241 | − dev->p->dead = true; | |
| 2255 | + kill_device(dev); | |
| 2242 | 2256 | device_unlock(dev); |
| 2243 | 2257 | |
| 2244 | 2258 | /* Notify clients of device removal. This call must come |
| @@ -62,14 +62,14 @@ static ssize_t sector_size_store(struct device *dev, · 61 unmodified lines | ||
| 62 | 62 | struct nd_btt *nd_btt = to_nd_btt(dev); |
| 63 | 63 | ssize_t rc; |
| 64 | 64 | |
| 65 | − device_lock(dev); | |
| 65 | + nd_device_lock(dev); | |
| 66 | 66 | nvdimm_bus_lock(dev); |
| 67 | 67 | rc = nd_size_select_store(dev, buf, &nd_btt->lbasize, |
| 68 | 68 | btt_lbasize_supported); |
| 69 | 69 | dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf, |
| 70 | 70 | buf[len - 1] == '\n' ? "" : "\n"); |
| 71 | 71 | nvdimm_bus_unlock(dev); |
| 72 | − device_unlock(dev); | |
| 72 | + nd_device_unlock(dev); | |
| 73 | 73 | |
| 74 | 74 | return rc ? rc : len; |
| 75 | 75 | } |
| @@ -91,11 +91,11 @@ static ssize_t uuid_store(struct device *dev, · 15 unmodified lines | ||
| 91 | 91 | struct nd_btt *nd_btt = to_nd_btt(dev); |
| 92 | 92 | ssize_t rc; |
| 93 | 93 | |
| 94 | − device_lock(dev); | |
| 94 | + nd_device_lock(dev); | |
| 95 | 95 | rc = nd_uuid_store(dev, &nd_btt->uuid, buf, len); |
| 96 | 96 | dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf, |
| 97 | 97 | buf[len - 1] == '\n' ? "" : "\n"); |
| 98 | − device_unlock(dev); | |
| 98 | + nd_device_unlock(dev); | |
| 99 | 99 | |
| 100 | 100 | return rc ? rc : len; |
| 101 | 101 | } |
| @@ -120,13 +120,13 @@ static ssize_t namespace_store(struct device *dev, · 18 unmodified lines | ||
| 120 | 120 | struct nd_btt *nd_btt = to_nd_btt(dev); |
| 121 | 121 | ssize_t rc; |
| 122 | 122 | |
| 123 | − device_lock(dev); | |
| 123 | + nd_device_lock(dev); | |
| 124 | 124 | nvdimm_bus_lock(dev); |
| 125 | 125 | rc = nd_namespace_store(dev, &nd_btt->ndns, buf, len); |
| 126 | 126 | dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf, |
| 127 | 127 | buf[len - 1] == '\n' ? "" : "\n"); |
| 128 | 128 | nvdimm_bus_unlock(dev); |
| 129 | − device_unlock(dev); | |
| 129 | + nd_device_unlock(dev); | |
| 130 | 130 | |
| 131 | 131 | return rc; |
| 132 | 132 | } |
| @@ -138,14 +138,14 @@ static ssize_t size_show(struct device *dev, · 5 unmodified lines | ||
| 138 | 138 | struct nd_btt *nd_btt = to_nd_btt(dev); |
| 139 | 139 | ssize_t rc; |
| 140 | 140 | |
| 141 | − device_lock(dev); | |
| 141 | + nd_device_lock(dev); | |
| 142 | 142 | if (dev->driver) |
| 143 | 143 | rc = sprintf(buf, "%llu\n", nd_btt->size); |
| 144 | 144 | else { |
| 145 | 145 | /* no size to convey if the btt instance is disabled */ |
| 146 | 146 | rc = -ENXIO; |
| 147 | 147 | } |
| 148 | − device_unlock(dev); | |
| 148 | + nd_device_unlock(dev); | |
| 149 | 149 | |
| 150 | 150 | return rc; |
| 151 | 151 | } |
| @@ -26,7 +26,7 @@ · 25 unmodified lines | ||
| 26 | 26 | |
| 27 | 27 | int nvdimm_major; |
| 28 | 28 | static int nvdimm_bus_major; |
| 29 | −static struct class *nd_class; | |
| 29 | +struct class *nd_class; | |
| 30 | 30 | static DEFINE_IDA(nd_ida); |
| 31 | 31 | |
| 32 | 32 | static int to_nd_device_type(struct device *dev) |
| @@ -73,7 +73,7 @@ static void nvdimm_bus_probe_end(struct nvdimm_bus *nvdimm_bus) · 40 unmodified lines | ||
| 73 | 73 | { |
| 74 | 74 | nvdimm_bus_lock(&nvdimm_bus->dev); |
| 75 | 75 | if (--nvdimm_bus->probe_active == 0) |
| 76 | − wake_up(&nvdimm_bus->probe_wait); | |
| 76 | + wake_up(&nvdimm_bus->wait); | |
| 77 | 77 | nvdimm_bus_unlock(&nvdimm_bus->dev); |
| 78 | 78 | } |
| 79 | 79 | |
| @@ -91,7 +91,10 @@ static int nvdimm_bus_probe(struct device *dev) · 11 unmodified lines | ||
| 91 | 91 | dev->driver->name, dev_name(dev)); |
| 92 | 92 | |
| 93 | 93 | nvdimm_bus_probe_start(nvdimm_bus); |
| 94 | + debug_nvdimm_lock(dev); | |
| 94 | 95 | rc = nd_drv->probe(dev); |
| 96 | + debug_nvdimm_unlock(dev); | |
| 97 | + | |
| 95 | 98 | if (rc == 0) |
| 96 | 99 | nd_region_probe_success(nvdimm_bus, dev); |
| 97 | 100 | else |
| @@ -113,8 +116,11 @@ static int nvdimm_bus_remove(struct device *dev) · 15 unmodified lines | ||
| 113 | 116 | struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev); |
| 114 | 117 | int rc = 0; |
| 115 | 118 | |
| 116 | − if (nd_drv->remove) | |
| 119 | + if (nd_drv->remove) { | |
| 120 | + debug_nvdimm_lock(dev); | |
| 117 | 121 | rc = nd_drv->remove(dev); |
| 122 | + debug_nvdimm_unlock(dev); | |
| 123 | + } | |
| 118 | 124 | nd_region_disable(nvdimm_bus, dev); |
| 119 | 125 | |
| 120 | 126 | dev_dbg(&nvdimm_bus->dev, "%s.remove(%s) = %d\n", dev->driver->name, |
| @@ -140,7 +146,7 @@ static void nvdimm_bus_shutdown(struct device *dev) · 19 unmodified lines | ||
| 140 | 146 | |
| 141 | 147 | void nd_device_notify(struct device *dev, enum nvdimm_event event) |
| 142 | 148 | { |
| 143 | − device_lock(dev); | |
| 149 | + nd_device_lock(dev); | |
| 144 | 150 | if (dev->driver) { |
| 145 | 151 | struct nd_device_driver *nd_drv; |
| 146 | 152 | |
| @@ -148,7 +154,7 @@ void nd_device_notify(struct device *dev, enum nvdimm_event event) · 1 unmodified line | ||
| 148 | 154 | if (nd_drv->notify) |
| 149 | 155 | nd_drv->notify(dev, event); |
| 150 | 156 | } |
| 151 | − device_unlock(dev); | |
| 157 | + nd_device_unlock(dev); | |
| 152 | 158 | } |
| 153 | 159 | EXPORT_SYMBOL(nd_device_notify); |
| 154 | 160 | |
| @@ -296,7 +302,7 @@ static void nvdimm_bus_release(struct device *dev) · 141 unmodified lines | ||
| 296 | 302 | kfree(nvdimm_bus); |
| 297 | 303 | } |
| 298 | 304 | |
| 299 | −static bool is_nvdimm_bus(struct device *dev) | |
| 305 | +bool is_nvdimm_bus(struct device *dev) | |
| 300 | 306 | { |
| 301 | 307 | return dev->release == nvdimm_bus_release; |
| 302 | 308 | } |
| @@ -341,7 +347,7 @@ struct nvdimm_bus *nvdimm_bus_register(struct device *parent, · 38 unmodified lines | ||
| 341 | 347 | return NULL; |
| 342 | 348 | INIT_LIST_HEAD(&nvdimm_bus->list); |
| 343 | 349 | INIT_LIST_HEAD(&nvdimm_bus->mapping_list); |
| 344 | − init_waitqueue_head(&nvdimm_bus->probe_wait); | |
| 350 | + init_waitqueue_head(&nvdimm_bus->wait); | |
| 345 | 351 | nvdimm_bus->id = ida_simple_get(&nd_ida, 0, 0, GFP_KERNEL); |
| 346 | 352 | if (nvdimm_bus->id < 0) { |
| 347 | 353 | kfree(nvdimm_bus); |
| @@ -426,6 +432,9 @@ static int nd_bus_remove(struct device *dev) · 78 unmodified lines | ||
| 426 | 432 | list_del_init(&nvdimm_bus->list); |
| 427 | 433 | mutex_unlock(&nvdimm_bus_list_mutex); |
| 428 | 434 | |
| 435 | + wait_event(nvdimm_bus->wait, | |
| 436 | + atomic_read(&nvdimm_bus->ioctl_active) == 0); | |
| 437 | + | |
| 429 | 438 | nd_synchronize(); |
| 430 | 439 | device_for_each_child(&nvdimm_bus->dev, NULL, child_unregister); |
| 431 | 440 | |
| @@ -547,13 +556,38 @@ EXPORT_SYMBOL(nd_device_register); · 115 unmodified lines | ||
| 547 | 556 | |
| 548 | 557 | void nd_device_unregister(struct device *dev, enum nd_async_mode mode) |
| 549 | 558 | { |
| 559 | + bool killed; | |
| 560 | + | |
| 550 | 561 | switch (mode) { |
| 551 | 562 | case ND_ASYNC: |
| 563 | + /* | |
| 564 | + * In the async case this is being triggered with the | |
| 565 | + * device lock held and the unregistration work needs to | |
| 566 | + * be moved out of line iff this is thread has won the | |
| 567 | + * race to schedule the deletion. | |
| 568 | + */ | |
| 569 | + if (!kill_device(dev)) | |
| 570 | + return; | |
| 571 | + | |
| 552 | 572 | get_device(dev); |
| 553 | 573 | async_schedule_domain(nd_async_device_unregister, dev, |
| 554 | 574 | &nd_async_domain); |
| 555 | 575 | break; |
| 556 | 576 | case ND_SYNC: |
| 577 | + /* | |
| 578 | + * In the sync case the device is being unregistered due | |
| 579 | + * to a state change of the parent. Claim the kill state | |
| 580 | + * to synchronize against other unregistration requests, | |
| 581 | + * or otherwise let the async path handle it if the | |
| 582 | + * unregistration was already queued. | |
| 583 | + */ | |
| 584 | + nd_device_lock(dev); | |
| 585 | + killed = kill_device(dev); | |
| 586 | + nd_device_unlock(dev); | |
| 587 | + | |
| 588 | + if (!killed) | |
| 589 | + return; | |
| 590 | + | |
| 557 | 591 | nd_synchronize(); |
| 558 | 592 | device_unregister(dev); |
| 559 | 593 | break; |
| @@ -859,10 +893,12 @@ void wait_nvdimm_bus_probe_idle(struct device *dev) · 299 unmodified lines | ||
| 859 | 893 | do { |
| 860 | 894 | if (nvdimm_bus->probe_active == 0) |
| 861 | 895 | break; |
| 862 | − nvdimm_bus_unlock(&nvdimm_bus->dev); | |
| 863 | − wait_event(nvdimm_bus->probe_wait, | |
| 896 | + nvdimm_bus_unlock(dev); | |
| 897 | + nd_device_unlock(dev); | |
| 898 | + wait_event(nvdimm_bus->wait, | |
| 864 | 899 | nvdimm_bus->probe_active == 0); |
| 865 | − nvdimm_bus_lock(&nvdimm_bus->dev); | |
| 900 | + nd_device_lock(dev); | |
| 901 | + nvdimm_bus_lock(dev); | |
| 866 | 902 | } while (true); |
| 867 | 903 | } |
| 868 | 904 | |
| @@ -945,20 +981,19 @@ static int __nd_ioctl(struct nvdimm_bus *nvdimm_bus, struct nvdimm *nvdimm, · 76 unmodified lines | ||
| 945 | 981 | int read_only, unsigned int ioctl_cmd, unsigned long arg) |
| 946 | 982 | { |
| 947 | 983 | struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc; |
| 948 | − static char out_env[ND_CMD_MAX_ENVELOPE]; | |
| 949 | − static char in_env[ND_CMD_MAX_ENVELOPE]; | |
| 950 | 984 | const struct nd_cmd_desc *desc = NULL; |
| 951 | 985 | unsigned int cmd = _IOC_NR(ioctl_cmd); |
| 952 | 986 | struct device *dev = &nvdimm_bus->dev; |
| 953 | 987 | void __user *p = (void __user *) arg; |
| 988 | + char *out_env = NULL, *in_env = NULL; | |
| 954 | 989 | const char *cmd_name, *dimm_name; |
| 955 | 990 | u32 in_len = 0, out_len = 0; |
| 956 | 991 | unsigned int func = cmd; |
| 957 | 992 | unsigned long cmd_mask; |
| 958 | 993 | struct nd_cmd_pkg pkg; |
| 959 | 994 | int rc, i, cmd_rc; |
| 995 | + void *buf = NULL; | |
| 960 | 996 | u64 buf_len = 0; |
| 961 | − void *buf; | |
| 962 | 997 | |
| 963 | 998 | if (nvdimm) { |
| 964 | 999 | desc = nd_cmd_dimm_desc(cmd); |
| @@ -989,7 +1024,7 @@ static int __nd_ioctl(struct nvdimm_bus *nvdimm_bus, struct nvdimm *nvdimm, · 24 unmodified lines | ||
| 989 | 1024 | case ND_CMD_ARS_START: |
| 990 | 1025 | case ND_CMD_CLEAR_ERROR: |
| 991 | 1026 | case ND_CMD_CALL: |
| 992 | − dev_dbg(&nvdimm_bus->dev, "'%s' command while read-only.\n", | |
| 1027 | + dev_dbg(dev, "'%s' command while read-only.\n", | |
| 993 | 1028 | nvdimm ? nvdimm_cmd_name(cmd) |
| 994 | 1029 | : nvdimm_bus_cmd_name(cmd)); |
| 995 | 1030 | return -EPERM; |
| @@ -998,6 +1033,9 @@ static int __nd_ioctl(struct nvdimm_bus *nvdimm_bus, struct nvdimm *nvdimm, · 2 unmodified lines | ||
| 998 | 1033 | } |
| 999 | 1034 | |
| 1000 | 1035 | /* process an input envelope */ |
| 1036 | + in_env = kzalloc(ND_CMD_MAX_ENVELOPE, GFP_KERNEL); | |
| 1037 | + if (!in_env) | |
| 1038 | + return -ENOMEM; | |
| 1001 | 1039 | for (i = 0; i < desc->in_num; i++) { |
| 1002 | 1040 | u32 in_size, copy; |
| 1003 | 1041 | |
| @@ -1005,14 +1043,17 @@ static int __nd_ioctl(struct nvdimm_bus *nvdimm_bus, struct nvdimm *nvdimm, · 1 unmodified line | ||
| 1005 | 1043 | if (in_size == UINT_MAX) { |
| 1006 | 1044 | dev_err(dev, "%s:%s unknown input size cmd: %s field: %d\n", |
| 1007 | 1045 | __func__, dimm_name, cmd_name, i); |
| 1008 | − return -ENXIO; | |
| 1046 | + rc = -ENXIO; | |
| 1047 | + goto out; | |
| 1009 | 1048 | } |
| 1010 | − if (in_len < sizeof(in_env)) | |
| 1011 | − copy = min_t(u32, sizeof(in_env) - in_len, in_size); | |
| 1049 | + if (in_len < ND_CMD_MAX_ENVELOPE) | |
| 1050 | + copy = min_t(u32, ND_CMD_MAX_ENVELOPE - in_len, in_size); | |
| 1012 | 1051 | else |
| 1013 | 1052 | copy = 0; |
| 1014 | − if (copy && copy_from_user(&in_env[in_len], p + in_len, copy)) | |
| 1015 | − return -EFAULT; | |
| 1053 | + if (copy && copy_from_user(&in_env[in_len], p + in_len, copy)) { | |
| 1054 | + rc = -EFAULT; | |
| 1055 | + goto out; | |
| 1056 | + } | |
| 1016 | 1057 | in_len += in_size; |
| 1017 | 1058 | } |
| 1018 | 1059 | |
| @@ -1024,6 +1065,12 @@ static int __nd_ioctl(struct nvdimm_bus *nvdimm_bus, struct nvdimm *nvdimm, · 5 unmodified lines | ||
| 1024 | 1065 | } |
| 1025 | 1066 | |
| 1026 | 1067 | /* process an output envelope */ |
| 1068 | + out_env = kzalloc(ND_CMD_MAX_ENVELOPE, GFP_KERNEL); | |
| 1069 | + if (!out_env) { | |
| 1070 | + rc = -ENOMEM; | |
| 1071 | + goto out; | |
| 1072 | + } | |
| 1073 | + | |
| 1027 | 1074 | for (i = 0; i < desc->out_num; i++) { |
| 1028 | 1075 | u32 out_size = nd_cmd_out_size(nvdimm, cmd, desc, i, |
| 1029 | 1076 | (u32 *) in_env, (u32 *) out_env, 0); |
| @@ -1032,15 +1079,18 @@ static int __nd_ioctl(struct nvdimm_bus *nvdimm_bus, struct nvdimm *nvdimm, · 2 unmodified lines | ||
| 1032 | 1079 | if (out_size == UINT_MAX) { |
| 1033 | 1080 | dev_dbg(dev, "%s unknown output size cmd: %s field: %d\n", |
| 1034 | 1081 | dimm_name, cmd_name, i); |
| 1035 | − return -EFAULT; | |
| 1082 | + rc = -EFAULT; | |
| 1083 | + goto out; | |
| 1036 | 1084 | } |
| 1037 | − if (out_len < sizeof(out_env)) | |
| 1038 | − copy = min_t(u32, sizeof(out_env) - out_len, out_size); | |
| 1085 | + if (out_len < ND_CMD_MAX_ENVELOPE) | |
| 1086 | + copy = min_t(u32, ND_CMD_MAX_ENVELOPE - out_len, out_size); | |
| 1039 | 1087 | else |
| 1040 | 1088 | copy = 0; |
| 1041 | 1089 | if (copy && copy_from_user(&out_env[out_len], |
| 1042 | − p + in_len + out_len, copy)) | |
| 1043 | − return -EFAULT; | |
| 1090 | + p + in_len + out_len, copy)) { | |
| 1091 | + rc = -EFAULT; | |
| 1092 | + goto out; | |
| 1093 | + } | |
| 1044 | 1094 | out_len += out_size; |
| 1045 | 1095 | } |
| 1046 | 1096 | |
| @@ -1048,19 +1098,23 @@ static int __nd_ioctl(struct nvdimm_bus *nvdimm_bus, struct nvdimm *nvdimm, · 1 unmodified line | ||
| 1048 | 1098 | if (buf_len > ND_IOCTL_MAX_BUFLEN) { |
| 1049 | 1099 | dev_dbg(dev, "%s cmd: %s buf_len: %llu > %d\n", dimm_name, |
| 1050 | 1100 | cmd_name, buf_len, ND_IOCTL_MAX_BUFLEN); |
| 1051 | − return -EINVAL; | |
| 1101 | + rc = -EINVAL; | |
| 1102 | + goto out; | |
| 1052 | 1103 | } |
| 1053 | 1104 | |
| 1054 | 1105 | buf = vmalloc(buf_len); |
| 1055 | − if (!buf) | |
| 1056 | − return -ENOMEM; | |
| 1106 | + if (!buf) { | |
| 1107 | + rc = -ENOMEM; | |
| 1108 | + goto out; | |
| 1109 | + } | |
| 1057 | 1110 | |
| 1058 | 1111 | if (copy_from_user(buf, p, buf_len)) { |
| 1059 | 1112 | rc = -EFAULT; |
| 1060 | 1113 | goto out; |
| 1061 | 1114 | } |
| 1062 | 1115 | |
| 1063 | − nvdimm_bus_lock(&nvdimm_bus->dev); | |
| 1116 | + nd_device_lock(dev); | |
| 1117 | + nvdimm_bus_lock(dev); | |
| 1064 | 1118 | rc = nd_cmd_clear_to_send(nvdimm_bus, nvdimm, func, buf); |
| 1065 | 1119 | if (rc) |
| 1066 | 1120 | goto out_unlock; |
| @@ -1075,39 +1129,24 @@ static int __nd_ioctl(struct nvdimm_bus *nvdimm_bus, struct nvdimm *nvdimm, · 8 unmodified lines | ||
| 1075 | 1129 | nvdimm_account_cleared_poison(nvdimm_bus, clear_err->address, |
| 1076 | 1130 | clear_err->cleared); |
| 1077 | 1131 | } |
| 1078 | − nvdimm_bus_unlock(&nvdimm_bus->dev); | |
| 1079 | 1132 | |
| 1080 | 1133 | if (copy_to_user(p, buf, buf_len)) |
| 1081 | 1134 | rc = -EFAULT; |
| 1082 | 1135 | |
| 1083 | − vfree(buf); | |
| 1084 | − return rc; | |
| 1085 | − | |
| 1086 | − out_unlock: | |
| 1087 | − nvdimm_bus_unlock(&nvdimm_bus->dev); | |
| 1088 | − out: | |
| 1136 | +out_unlock: | |
| 1137 | + nvdimm_bus_unlock(dev); | |
| 1138 | + nd_device_unlock(dev); | |
| 1139 | +out: | |
| 1140 | + kfree(in_env); | |
| 1141 | + kfree(out_env); | |
| 1089 | 1142 | vfree(buf); |
| 1090 | 1143 | return rc; |
| 1091 | 1144 | } |
| 1092 | 1145 | |
| 1093 | −static long nd_ioctl(struct file *file, unsigned int cmd, unsigned long arg) | |
| 1094 | −{ | |
| 1095 | − long id = (long) file->private_data; | |
| 1096 | − int rc = -ENXIO, ro; | |
| 1097 | − struct nvdimm_bus *nvdimm_bus; | |
| 1098 | − | |
| 1099 | − ro = ((file->f_flags & O_ACCMODE) == O_RDONLY); | |
| 1100 | − mutex_lock(&nvdimm_bus_list_mutex); | |
| 1101 | − list_for_each_entry(nvdimm_bus, &nvdimm_bus_list, list) { | |
| 1102 | − if (nvdimm_bus->id == id) { | |
| 1103 | − rc = __nd_ioctl(nvdimm_bus, NULL, ro, cmd, arg); | |
| 1104 | − break; | |
| 1105 | − } | |
| 1106 | − } | |
| 1107 | − mutex_unlock(&nvdimm_bus_list_mutex); | |
| 1108 | − | |
| 1109 | − return rc; | |
| 1110 | −} | |
| 1146 | +enum nd_ioctl_mode { | |
| 1147 | + BUS_IOCTL, | |
| 1148 | + DIMM_IOCTL, | |
| 1149 | +}; | |
| 1111 | 1150 | |
| 1112 | 1151 | static int match_dimm(struct device *dev, void *data) |
| 1113 | 1152 | { |
| @@ -1122,31 +1161,62 @@ static int match_dimm(struct device *dev, void *data) · 8 unmodified lines | ||
| 1122 | 1161 | return 0; |
| 1123 | 1162 | } |
| 1124 | 1163 | |
| 1125 | −static long nvdimm_ioctl(struct file *file, unsigned int cmd, unsigned long arg) | |
| 1164 | +static long nd_ioctl(struct file *file, unsigned int cmd, unsigned long arg, | |
| 1165 | + enum nd_ioctl_mode mode) | |
| 1166 | + | |
| 1126 | 1167 | { |
| 1127 | − int rc = -ENXIO, ro; | |
| 1128 | − struct nvdimm_bus *nvdimm_bus; | |
| 1168 | + struct nvdimm_bus *nvdimm_bus, *found = NULL; | |
| 1169 | + long id = (long) file->private_data; | |
| 1170 | + struct nvdimm *nvdimm = NULL; | |
| 1171 | + int rc, ro; | |
| 1129 | 1172 | |
| 1130 | 1173 | ro = ((file->f_flags & O_ACCMODE) == O_RDONLY); |
| 1131 | 1174 | mutex_lock(&nvdimm_bus_list_mutex); |
| 1132 | 1175 | list_for_each_entry(nvdimm_bus, &nvdimm_bus_list, list) { |
| 1133 | − struct device *dev = device_find_child(&nvdimm_bus->dev, | |
| 1134 | − file->private_data, match_dimm); | |
| 1135 | − struct nvdimm *nvdimm; | |
| 1136 | − | |
| 1137 | − if (!dev) | |
| 1138 | − continue; | |
| 1176 | + if (mode == DIMM_IOCTL) { | |
| 1177 | + struct device *dev; | |
| 1178 | + | |
| 1179 | + dev = device_find_child(&nvdimm_bus->dev, | |
| 1180 | + file->private_data, match_dimm); | |
| 1181 | + if (!dev) | |
| 1182 | + continue; | |
| 1183 | + nvdimm = to_nvdimm(dev); | |
| 1184 | + found = nvdimm_bus; | |
| 1185 | + } else if (nvdimm_bus->id == id) { | |
| 1186 | + found = nvdimm_bus; | |
| 1187 | + } | |
| 1139 | 1188 | |
| 1140 | − nvdimm = to_nvdimm(dev); | |
| 1141 | − rc = __nd_ioctl(nvdimm_bus, nvdimm, ro, cmd, arg); | |
| 1142 | − put_device(dev); | |
| 1143 | − break; | |
| 1189 | + if (found) { | |
| 1190 | + atomic_inc(&nvdimm_bus->ioctl_active); | |
| 1191 | + break; | |
| 1192 | + } | |
| 1144 | 1193 | } |
| 1145 | 1194 | mutex_unlock(&nvdimm_bus_list_mutex); |
| 1146 | 1195 | |
| 1196 | + if (!found) | |
| 1197 | + return -ENXIO; | |
| 1198 | + | |
| 1199 | + nvdimm_bus = found; | |
| 1200 | + rc = __nd_ioctl(nvdimm_bus, nvdimm, ro, cmd, arg); | |
| 1201 | + | |
| 1202 | + if (nvdimm) | |
| 1203 | + put_device(&nvdimm->dev); | |
| 1204 | + if (atomic_dec_and_test(&nvdimm_bus->ioctl_active)) | |
| 1205 | + wake_up(&nvdimm_bus->wait); | |
| 1206 | + | |
| 1147 | 1207 | return rc; |
| 1148 | 1208 | } |
| 1149 | 1209 | |
| 1210 | +static long bus_ioctl(struct file *file, unsigned int cmd, unsigned long arg) | |
| 1211 | +{ | |
| 1212 | + return nd_ioctl(file, cmd, arg, BUS_IOCTL); | |
| 1213 | +} | |
| 1214 | + | |
| 1215 | +static long dimm_ioctl(struct file *file, unsigned int cmd, unsigned long arg) | |
| 1216 | +{ | |
| 1217 | + return nd_ioctl(file, cmd, arg, DIMM_IOCTL); | |
| 1218 | +} | |
| 1219 | + | |
| 1150 | 1220 | static int nd_open(struct inode *inode, struct file *file) |
| 1151 | 1221 | { |
| 1152 | 1222 | long minor = iminor(inode); |
| @@ -1158,16 +1228,16 @@ static int nd_open(struct inode *inode, struct file *file) · 5 unmodified lines | ||
| 1158 | 1228 | static const struct file_operations nvdimm_bus_fops = { |
| 1159 | 1229 | .owner = THIS_MODULE, |
| 1160 | 1230 | .open = nd_open, |
| 1161 | − .unlocked_ioctl = nd_ioctl, | |
| 1162 | − .compat_ioctl = nd_ioctl, | |
| 1231 | + .unlocked_ioctl = bus_ioctl, | |
| 1232 | + .compat_ioctl = bus_ioctl, | |
| 1163 | 1233 | .llseek = noop_llseek, |
| 1164 | 1234 | }; |
| 1165 | 1235 | |
| 1166 | 1236 | static const struct file_operations nvdimm_fops = { |
| 1167 | 1237 | .owner = THIS_MODULE, |
| 1168 | 1238 | .open = nd_open, |
| 1169 | − .unlocked_ioctl = nvdimm_ioctl, | |
| 1170 | − .compat_ioctl = nvdimm_ioctl, | |
| 1239 | + .unlocked_ioctl = dimm_ioctl, | |
| 1240 | + .compat_ioctl = dimm_ioctl, | |
| 1171 | 1241 | .llseek = noop_llseek, |
| 1172 | 1242 | }; |
| 1173 | 1243 | |
| @@ -246,7 +246,7 @@ static int nd_uuid_parse(struct device *dev, u8 *uuid_out, const char *buf, · 245 unmodified lines | ||
| 246 | 246 | * |
| 247 | 247 | * Enforce that uuids can only be changed while the device is disabled |
| 248 | 248 | * (driver detached) |
| 249 | − * LOCKING: expects device_lock() is held on entry | |
| 249 | + * LOCKING: expects nd_device_lock() is held on entry | |
| 250 | 250 | */ |
| 251 | 251 | int nd_uuid_store(struct device *dev, u8 **uuid_out, const char *buf, |
| 252 | 252 | size_t len) |
| @@ -347,15 +347,15 @@ static DEVICE_ATTR_RO(provider); · 94 unmodified lines | ||
| 347 | 347 | |
| 348 | 348 | static int flush_namespaces(struct device *dev, void *data) |
| 349 | 349 | { |
| 350 | − device_lock(dev); | |
| 351 | − device_unlock(dev); | |
| 350 | + nd_device_lock(dev); | |
| 351 | + nd_device_unlock(dev); | |
| 352 | 352 | return 0; |
| 353 | 353 | } |
| 354 | 354 | |
| 355 | 355 | static int flush_regions_dimms(struct device *dev, void *data) |
| 356 | 356 | { |
| 357 | − device_lock(dev); | |
| 358 | − device_unlock(dev); | |
| 357 | + nd_device_lock(dev); | |
| 358 | + nd_device_unlock(dev); | |
| 359 | 359 | device_for_each_child(dev, NULL, flush_namespaces); |
| 360 | 360 | return 0; |
| 361 | 361 | } |
| @@ -484,12 +484,12 @@ static ssize_t security_store(struct device *dev, · 483 unmodified lines | ||
| 484 | 484 | * done while probing is idle and the DIMM is not in active use |
| 485 | 485 | * in any region. |
| 486 | 486 | */ |
| 487 | − device_lock(dev); | |
| 487 | + nd_device_lock(dev); | |
| 488 | 488 | nvdimm_bus_lock(dev); |
| 489 | 489 | wait_nvdimm_bus_probe_idle(dev); |
| 490 | 490 | rc = __security_store(dev, buf, len); |
| 491 | 491 | nvdimm_bus_unlock(dev); |
| 492 | − device_unlock(dev); | |
| 492 | + nd_device_unlock(dev); | |
| 493 | 493 | |
| 494 | 494 | return rc; |
| 495 | 495 | } |
| @@ -410,7 +410,7 @@ static ssize_t alt_name_store(struct device *dev, · 409 unmodified lines | ||
| 410 | 410 | struct nd_region *nd_region = to_nd_region(dev->parent); |
| 411 | 411 | ssize_t rc; |
| 412 | 412 | |
| 413 | − device_lock(dev); | |
| 413 | + nd_device_lock(dev); | |
| 414 | 414 | nvdimm_bus_lock(dev); |
| 415 | 415 | wait_nvdimm_bus_probe_idle(dev); |
| 416 | 416 | rc = __alt_name_store(dev, buf, len); |
| @@ -418,7 +418,7 @@ static ssize_t alt_name_store(struct device *dev, · 1 unmodified line | ||
| 418 | 418 | rc = nd_namespace_label_update(nd_region, dev); |
| 419 | 419 | dev_dbg(dev, "%s(%zd)\n", rc < 0 ? "fail " : "", rc); |
| 420 | 420 | nvdimm_bus_unlock(dev); |
| 421 | − device_unlock(dev); | |
| 421 | + nd_device_unlock(dev); | |
| 422 | 422 | |
| 423 | 423 | return rc < 0 ? rc : len; |
| 424 | 424 | } |
| @@ -1077,7 +1077,7 @@ static ssize_t size_store(struct device *dev, · 652 unmodified lines | ||
| 1077 | 1077 | if (rc) |
| 1078 | 1078 | return rc; |
| 1079 | 1079 | |
| 1080 | − device_lock(dev); | |
| 1080 | + nd_device_lock(dev); | |
| 1081 | 1081 | nvdimm_bus_lock(dev); |
| 1082 | 1082 | wait_nvdimm_bus_probe_idle(dev); |
| 1083 | 1083 | rc = __size_store(dev, val); |
| @@ -1103,7 +1103,7 @@ static ssize_t size_store(struct device *dev, · 19 unmodified lines | ||
| 1103 | 1103 | dev_dbg(dev, "%llx %s (%d)\n", val, rc < 0 ? "fail" : "success", rc); |
| 1104 | 1104 | |
| 1105 | 1105 | nvdimm_bus_unlock(dev); |
| 1106 | − device_unlock(dev); | |
| 1106 | + nd_device_unlock(dev); | |
| 1107 | 1107 | |
| 1108 | 1108 | return rc < 0 ? rc : len; |
| 1109 | 1109 | } |
| @@ -1286,7 +1286,7 @@ static ssize_t uuid_store(struct device *dev, · 176 unmodified lines | ||
| 1286 | 1286 | } else |
| 1287 | 1287 | return -ENXIO; |
| 1288 | 1288 | |
| 1289 | − device_lock(dev); | |
| 1289 | + nd_device_lock(dev); | |
| 1290 | 1290 | nvdimm_bus_lock(dev); |
| 1291 | 1291 | wait_nvdimm_bus_probe_idle(dev); |
| 1292 | 1292 | if (to_ndns(dev)->claim) |
| @@ -1302,7 +1302,7 @@ static ssize_t uuid_store(struct device *dev, · 9 unmodified lines | ||
| 1302 | 1302 | dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf, |
| 1303 | 1303 | buf[len - 1] == '\n' ? "" : "\n"); |
| 1304 | 1304 | nvdimm_bus_unlock(dev); |
| 1305 | − device_unlock(dev); | |
| 1305 | + nd_device_unlock(dev); | |
| 1306 | 1306 | |
| 1307 | 1307 | return rc < 0 ? rc : len; |
| 1308 | 1308 | } |
| @@ -1376,7 +1376,7 @@ static ssize_t sector_size_store(struct device *dev, · 67 unmodified lines | ||
| 1376 | 1376 | } else |
| 1377 | 1377 | return -ENXIO; |
| 1378 | 1378 | |
| 1379 | − device_lock(dev); | |
| 1379 | + nd_device_lock(dev); | |
| 1380 | 1380 | nvdimm_bus_lock(dev); |
| 1381 | 1381 | if (to_ndns(dev)->claim) |
| 1382 | 1382 | rc = -EBUSY; |
| @@ -1387,7 +1387,7 @@ static ssize_t sector_size_store(struct device *dev, · 4 unmodified lines | ||
| 1387 | 1387 | dev_dbg(dev, "result: %zd %s: %s%s", rc, rc < 0 ? "tried" : "wrote", |
| 1388 | 1388 | buf, buf[len - 1] == '\n' ? "" : "\n"); |
| 1389 | 1389 | nvdimm_bus_unlock(dev); |
| 1390 | − device_unlock(dev); | |
| 1390 | + nd_device_unlock(dev); | |
| 1391 | 1391 | |
| 1392 | 1392 | return rc ? rc : len; |
| 1393 | 1393 | } |
| @@ -1502,9 +1502,9 @@ static ssize_t holder_show(struct device *dev, · 108 unmodified lines | ||
| 1502 | 1502 | struct nd_namespace_common *ndns = to_ndns(dev); |
| 1503 | 1503 | ssize_t rc; |
| 1504 | 1504 | |
| 1505 | − device_lock(dev); | |
| 1505 | + nd_device_lock(dev); | |
| 1506 | 1506 | rc = sprintf(buf, "%s\n", ndns->claim ? dev_name(ndns->claim) : ""); |
| 1507 | − device_unlock(dev); | |
| 1507 | + nd_device_unlock(dev); | |
| 1508 | 1508 | |
| 1509 | 1509 | return rc; |
| 1510 | 1510 | } |
| @@ -1541,7 +1541,7 @@ static ssize_t holder_class_store(struct device *dev, · 30 unmodified lines | ||
| 1541 | 1541 | struct nd_region *nd_region = to_nd_region(dev->parent); |
| 1542 | 1542 | ssize_t rc; |
| 1543 | 1543 | |
| 1544 | − device_lock(dev); | |
| 1544 | + nd_device_lock(dev); | |
| 1545 | 1545 | nvdimm_bus_lock(dev); |
| 1546 | 1546 | wait_nvdimm_bus_probe_idle(dev); |
| 1547 | 1547 | rc = __holder_class_store(dev, buf); |
| @@ -1549,7 +1549,7 @@ static ssize_t holder_class_store(struct device *dev, · 1 unmodified line | ||
| 1549 | 1549 | rc = nd_namespace_label_update(nd_region, dev); |
| 1550 | 1550 | dev_dbg(dev, "%s(%zd)\n", rc < 0 ? "fail " : "", rc); |
| 1551 | 1551 | nvdimm_bus_unlock(dev); |
| 1552 | − device_unlock(dev); | |
| 1552 | + nd_device_unlock(dev); | |
| 1553 | 1553 | |
| 1554 | 1554 | return rc < 0 ? rc : len; |
| 1555 | 1555 | } |
| @@ -1560,7 +1560,7 @@ static ssize_t holder_class_show(struct device *dev, · 4 unmodified lines | ||
| 1560 | 1560 | struct nd_namespace_common *ndns = to_ndns(dev); |
| 1561 | 1561 | ssize_t rc; |
| 1562 | 1562 | |
| 1563 | − device_lock(dev); | |
| 1563 | + nd_device_lock(dev); | |
| 1564 | 1564 | if (ndns->claim_class == NVDIMM_CCLASS_NONE) |
| 1565 | 1565 | rc = sprintf(buf, "\n"); |
| 1566 | 1566 | else if ((ndns->claim_class == NVDIMM_CCLASS_BTT) || |
| @@ -1572,7 +1572,7 @@ static ssize_t holder_class_show(struct device *dev, · 5 unmodified lines | ||
| 1572 | 1572 | rc = sprintf(buf, "dax\n"); |
| 1573 | 1573 | else |
| 1574 | 1574 | rc = sprintf(buf, "<unknown>\n"); |
| 1575 | − device_unlock(dev); | |
| 1575 | + nd_device_unlock(dev); | |
| 1576 | 1576 | |
| 1577 | 1577 | return rc; |
| 1578 | 1578 | } |
| @@ -1586,7 +1586,7 @@ static ssize_t mode_show(struct device *dev, · 7 unmodified lines | ||
| 1586 | 1586 | char *mode; |
| 1587 | 1587 | ssize_t rc; |
| 1588 | 1588 | |
| 1589 | − device_lock(dev); | |
| 1589 | + nd_device_lock(dev); | |
| 1590 | 1590 | claim = ndns->claim; |
| 1591 | 1591 | if (claim && is_nd_btt(claim)) |
| 1592 | 1592 | mode = "safe"; |
| @@ -1599,7 +1599,7 @@ static ssize_t mode_show(struct device *dev, · 6 unmodified lines | ||
| 1599 | 1599 | else |
| 1600 | 1600 | mode = "raw"; |
| 1601 | 1601 | rc = sprintf(buf, "%s\n", mode); |
| 1602 | − device_unlock(dev); | |
| 1602 | + nd_device_unlock(dev); | |
| 1603 | 1603 | |
| 1604 | 1604 | return rc; |
| 1605 | 1605 | } |
| @@ -1703,8 +1703,8 @@ struct nd_namespace_common *nvdimm_namespace_common_probe(struct device *dev) · 97 unmodified lines | ||
| 1703 | 1703 | * Flush any in-progess probes / removals in the driver |
| 1704 | 1704 | * for the raw personality of this namespace. |
| 1705 | 1705 | */ |
| 1706 | − device_lock(&ndns->dev); | |
| 1707 | − device_unlock(&ndns->dev); | |
| 1706 | + nd_device_lock(&ndns->dev); | |
| 1707 | + nd_device_unlock(&ndns->dev); | |
| 1708 | 1708 | if (ndns->dev.driver) { |
| 1709 | 1709 | dev_dbg(&ndns->dev, "is active, can't bind %s\n", |
| 1710 | 1710 | dev_name(dev)); |
| @@ -9,6 +9,7 @@ · 8 unmodified lines | ||
| 9 | 9 | #include <linux/sizes.h> |
| 10 | 10 | #include <linux/mutex.h> |
| 11 | 11 | #include <linux/nd.h> |
| 12 | +#include "nd.h" | |
| 12 | 13 | |
| 13 | 14 | extern struct list_head nvdimm_bus_list; |
| 14 | 15 | extern struct mutex nvdimm_bus_list_mutex; |
| @@ -17,10 +18,11 @@ extern struct workqueue_struct *nvdimm_wq; · 2 unmodified lines | ||
| 17 | 18 | |
| 18 | 19 | struct nvdimm_bus { |
| 19 | 20 | struct nvdimm_bus_descriptor *nd_desc; |
| 20 | − wait_queue_head_t probe_wait; | |
| 21 | + wait_queue_head_t wait; | |
| 21 | 22 | struct list_head list; |
| 22 | 23 | struct device dev; |
| 23 | 24 | int id, probe_active; |
| 25 | + atomic_t ioctl_active; | |
| 24 | 26 | struct list_head mapping_list; |
| 25 | 27 | struct mutex reconfig_mutex; |
| 26 | 28 | struct badrange badrange; |
| @@ -181,4 +183,71 @@ ssize_t nd_namespace_store(struct device *dev, · 154 unmodified lines | ||
| 181 | 183 | struct nd_namespace_common **_ndns, const char *buf, |
| 182 | 184 | size_t len); |
| 183 | 185 | struct nd_pfn *to_nd_pfn_safe(struct device *dev); |
| 186 | +bool is_nvdimm_bus(struct device *dev); | |
| 187 | + | |
| 188 | +#ifdef CONFIG_PROVE_LOCKING | |
| 189 | +extern struct class *nd_class; | |
| 190 | + | |
| 191 | +enum { | |
| 192 | + LOCK_BUS, | |
| 193 | + LOCK_NDCTL, | |
| 194 | + LOCK_REGION, | |
| 195 | + LOCK_DIMM = LOCK_REGION, | |
| 196 | + LOCK_NAMESPACE, | |
| 197 | + LOCK_CLAIM, | |
| 198 | +}; | |
| 199 | + | |
| 200 | +static inline void debug_nvdimm_lock(struct device *dev) | |
| 201 | +{ | |
| 202 | + if (is_nd_region(dev)) | |
| 203 | + mutex_lock_nested(&dev->lockdep_mutex, LOCK_REGION); | |
| 204 | + else if (is_nvdimm(dev)) | |
| 205 | + mutex_lock_nested(&dev->lockdep_mutex, LOCK_DIMM); | |
| 206 | + else if (is_nd_btt(dev) || is_nd_pfn(dev) || is_nd_dax(dev)) | |
| 207 | + mutex_lock_nested(&dev->lockdep_mutex, LOCK_CLAIM); | |
| 208 | + else if (dev->parent && (is_nd_region(dev->parent))) | |
| 209 | + mutex_lock_nested(&dev->lockdep_mutex, LOCK_NAMESPACE); | |
| 210 | + else if (is_nvdimm_bus(dev)) | |
| 211 | + mutex_lock_nested(&dev->lockdep_mutex, LOCK_BUS); | |
| 212 | + else if (dev->class && dev->class == nd_class) | |
| 213 | + mutex_lock_nested(&dev->lockdep_mutex, LOCK_NDCTL); | |
| 214 | + else | |
| 215 | + dev_WARN(dev, "unknown lock level\n"); | |
| 216 | +} | |
| 217 | + | |
| 218 | +static inline void debug_nvdimm_unlock(struct device *dev) | |
| 219 | +{ | |
| 220 | + mutex_unlock(&dev->lockdep_mutex); | |
| 221 | +} | |
| 222 | + | |
| 223 | +static inline void nd_device_lock(struct device *dev) | |
| 224 | +{ | |
| 225 | + device_lock(dev); | |
| 226 | + debug_nvdimm_lock(dev); | |
| 227 | +} | |
| 228 | + | |
| 229 | +static inline void nd_device_unlock(struct device *dev) | |
| 230 | +{ | |
| 231 | + debug_nvdimm_unlock(dev); | |
| 232 | + device_unlock(dev); | |
| 233 | +} | |
| 234 | +#else | |
| 235 | +static inline void nd_device_lock(struct device *dev) | |
| 236 | +{ | |
| 237 | + device_lock(dev); | |
| 238 | +} | |
| 239 | + | |
| 240 | +static inline void nd_device_unlock(struct device *dev) | |
| 241 | +{ | |
| 242 | + device_unlock(dev); | |
| 243 | +} | |
| 244 | + | |
| 245 | +static inline void debug_nvdimm_lock(struct device *dev) | |
| 246 | +{ | |
| 247 | +} | |
| 248 | + | |
| 249 | +static inline void debug_nvdimm_unlock(struct device *dev) | |
| 250 | +{ | |
| 251 | +} | |
| 252 | +#endif | |
| 184 | 253 | #endif /* __ND_CORE_H__ */ |
| @@ -67,7 +67,7 @@ static ssize_t mode_store(struct device *dev, · 66 unmodified lines | ||
| 67 | 67 | struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev); |
| 68 | 68 | ssize_t rc = 0; |
| 69 | 69 | |
| 70 | − device_lock(dev); | |
| 70 | + nd_device_lock(dev); | |
| 71 | 71 | nvdimm_bus_lock(dev); |
| 72 | 72 | if (dev->driver) |
| 73 | 73 | rc = -EBUSY; |
| @@ -89,7 +89,7 @@ static ssize_t mode_store(struct device *dev, · 15 unmodified lines | ||
| 89 | 89 | dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf, |
| 90 | 90 | buf[len - 1] == '\n' ? "" : "\n"); |
| 91 | 91 | nvdimm_bus_unlock(dev); |
| 92 | − device_unlock(dev); | |
| 92 | + nd_device_unlock(dev); | |
| 93 | 93 | |
| 94 | 94 | return rc ? rc : len; |
| 95 | 95 | } |
| @@ -132,14 +132,14 @@ static ssize_t align_store(struct device *dev, · 36 unmodified lines | ||
| 132 | 132 | struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev); |
| 133 | 133 | ssize_t rc; |
| 134 | 134 | |
| 135 | − device_lock(dev); | |
| 135 | + nd_device_lock(dev); | |
| 136 | 136 | nvdimm_bus_lock(dev); |
| 137 | 137 | rc = nd_size_select_store(dev, buf, &nd_pfn->align, |
| 138 | 138 | nd_pfn_supported_alignments()); |
| 139 | 139 | dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf, |
| 140 | 140 | buf[len - 1] == '\n' ? "" : "\n"); |
| 141 | 141 | nvdimm_bus_unlock(dev); |
| 142 | − device_unlock(dev); | |
| 142 | + nd_device_unlock(dev); | |
| 143 | 143 | |
| 144 | 144 | return rc ? rc : len; |
| 145 | 145 | } |
| @@ -161,11 +161,11 @@ static ssize_t uuid_store(struct device *dev, · 15 unmodified lines | ||
| 161 | 161 | struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev); |
| 162 | 162 | ssize_t rc; |
| 163 | 163 | |
| 164 | − device_lock(dev); | |
| 164 | + nd_device_lock(dev); | |
| 165 | 165 | rc = nd_uuid_store(dev, &nd_pfn->uuid, buf, len); |
| 166 | 166 | dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf, |
| 167 | 167 | buf[len - 1] == '\n' ? "" : "\n"); |
| 168 | − device_unlock(dev); | |
| 168 | + nd_device_unlock(dev); | |
| 169 | 169 | |
| 170 | 170 | return rc ? rc : len; |
| 171 | 171 | } |
| @@ -190,13 +190,13 @@ static ssize_t namespace_store(struct device *dev, · 18 unmodified lines | ||
| 190 | 190 | struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev); |
| 191 | 191 | ssize_t rc; |
| 192 | 192 | |
| 193 | − device_lock(dev); | |
| 193 | + nd_device_lock(dev); | |
| 194 | 194 | nvdimm_bus_lock(dev); |
| 195 | 195 | rc = nd_namespace_store(dev, &nd_pfn->ndns, buf, len); |
| 196 | 196 | dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf, |
| 197 | 197 | buf[len - 1] == '\n' ? "" : "\n"); |
| 198 | 198 | nvdimm_bus_unlock(dev); |
| 199 | − device_unlock(dev); | |
| 199 | + nd_device_unlock(dev); | |
| 200 | 200 | |
| 201 | 201 | return rc; |
| 202 | 202 | } |
| @@ -208,7 +208,7 @@ static ssize_t resource_show(struct device *dev, · 5 unmodified lines | ||
| 208 | 208 | struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev); |
| 209 | 209 | ssize_t rc; |
| 210 | 210 | |
| 211 | − device_lock(dev); | |
| 211 | + nd_device_lock(dev); | |
| 212 | 212 | if (dev->driver) { |
| 213 | 213 | struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb; |
| 214 | 214 | u64 offset = __le64_to_cpu(pfn_sb->dataoff); |
| @@ -222,7 +222,7 @@ static ssize_t resource_show(struct device *dev, · 7 unmodified lines | ||
| 222 | 222 | /* no address to convey if the pfn instance is disabled */ |
| 223 | 223 | rc = -ENXIO; |
| 224 | 224 | } |
| 225 | − device_unlock(dev); | |
| 225 | + nd_device_unlock(dev); | |
| 226 | 226 | |
| 227 | 227 | return rc; |
| 228 | 228 | } |
| @@ -234,7 +234,7 @@ static ssize_t size_show(struct device *dev, · 5 unmodified lines | ||
| 234 | 234 | struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev); |
| 235 | 235 | ssize_t rc; |
| 236 | 236 | |
| 237 | − device_lock(dev); | |
| 237 | + nd_device_lock(dev); | |
| 238 | 238 | if (dev->driver) { |
| 239 | 239 | struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb; |
| 240 | 240 | u64 offset = __le64_to_cpu(pfn_sb->dataoff); |
| @@ -250,7 +250,7 @@ static ssize_t size_show(struct device *dev, · 9 unmodified lines | ||
| 250 | 250 | /* no size to convey if the pfn instance is disabled */ |
| 251 | 251 | rc = -ENXIO; |
| 252 | 252 | } |
| 253 | − device_unlock(dev); | |
| 253 | + nd_device_unlock(dev); | |
| 254 | 254 | |
| 255 | 255 | return rc; |
| 256 | 256 | } |
| @@ -522,8 +522,8 @@ static int nd_pmem_remove(struct device *dev) · 521 unmodified lines | ||
| 522 | 522 | nvdimm_namespace_detach_btt(to_nd_btt(dev)); |
| 523 | 523 | else { |
| 524 | 524 | /* |
| 525 | − * Note, this assumes device_lock() context to not race | |
| 526 | − * nd_pmem_notify() | |
| 525 | + * Note, this assumes nd_device_lock() context to not | |
| 526 | + * race nd_pmem_notify() | |
| 527 | 527 | */ |
| 528 | 528 | sysfs_put(pmem->bb_state); |
| 529 | 529 | pmem->bb_state = NULL; |
| @@ -34,17 +34,6 @@ static int nd_region_probe(struct device *dev) · 33 unmodified lines | ||
| 34 | 34 | if (rc) |
| 35 | 35 | return rc; |
| 36 | 36 | |
| 37 | − rc = nd_region_register_namespaces(nd_region, &err); | |
| 38 | − if (rc < 0) | |
| 39 | − return rc; | |
| 40 | − | |
| 41 | − ndrd = dev_get_drvdata(dev); | |
| 42 | − ndrd->ns_active = rc; | |
| 43 | − ndrd->ns_count = rc + err; | |
| 44 | − | |
| 45 | − if (rc && err && rc == err) | |
| 46 | − return -ENODEV; | |
| 47 | − | |
| 48 | 37 | if (is_nd_pmem(&nd_region->dev)) { |
| 49 | 38 | struct resource ndr_res; |
| 50 | 39 | |
| @@ -60,6 +49,17 @@ static int nd_region_probe(struct device *dev) · 9 unmodified lines | ||
| 60 | 49 | nvdimm_badblocks_populate(nd_region, &nd_region->bb, &ndr_res); |
| 61 | 50 | } |
| 62 | 51 | |
| 52 | + rc = nd_region_register_namespaces(nd_region, &err); | |
| 53 | + if (rc < 0) | |
| 54 | + return rc; | |
| 55 | + | |
| 56 | + ndrd = dev_get_drvdata(dev); | |
| 57 | + ndrd->ns_active = rc; | |
| 58 | + ndrd->ns_count = rc + err; | |
| 59 | + | |
| 60 | + if (rc && err && rc == err) | |
| 61 | + return -ENODEV; | |
| 62 | + | |
| 63 | 63 | nd_region->btt_seed = nd_btt_create(nd_region); |
| 64 | 64 | nd_region->pfn_seed = nd_pfn_create(nd_region); |
| 65 | 65 | nd_region->dax_seed = nd_dax_create(nd_region); |
| @@ -102,7 +102,7 @@ static int nd_region_remove(struct device *dev) · 36 unmodified lines | ||
| 102 | 102 | nvdimm_bus_unlock(dev); |
| 103 | 103 | |
| 104 | 104 | /* |
| 105 | − * Note, this assumes device_lock() context to not race | |
| 105 | + * Note, this assumes nd_device_lock() context to not race | |
| 106 | 106 | * nd_region_notify() |
| 107 | 107 | */ |
| 108 | 108 | sysfs_put(nd_region->bb_state); |
| @@ -331,7 +331,7 @@ static ssize_t set_cookie_show(struct device *dev, · 330 unmodified lines | ||
| 331 | 331 | * the v1.1 namespace label cookie definition. To read all this |
| 332 | 332 | * data we need to wait for probing to settle. |
| 333 | 333 | */ |
| 334 | − device_lock(dev); | |
| 334 | + nd_device_lock(dev); | |
| 335 | 335 | nvdimm_bus_lock(dev); |
| 336 | 336 | wait_nvdimm_bus_probe_idle(dev); |
| 337 | 337 | if (nd_region->ndr_mappings) { |
| @@ -348,7 +348,7 @@ static ssize_t set_cookie_show(struct device *dev, · 10 unmodified lines | ||
| 348 | 348 | } |
| 349 | 349 | } |
| 350 | 350 | nvdimm_bus_unlock(dev); |
| 351 | − device_unlock(dev); | |
| 351 | + nd_device_unlock(dev); | |
| 352 | 352 | |
| 353 | 353 | if (rc) |
| 354 | 354 | return rc; |
| @@ -424,10 +424,12 @@ static ssize_t available_size_show(struct device *dev, · 69 unmodified lines | ||
| 424 | 424 | * memory nvdimm_bus_lock() is dropped, but that's userspace's |
| 425 | 425 | * problem to not race itself. |
| 426 | 426 | */ |
| 427 | + nd_device_lock(dev); | |
| 427 | 428 | nvdimm_bus_lock(dev); |
| 428 | 429 | wait_nvdimm_bus_probe_idle(dev); |
| 429 | 430 | available = nd_region_available_dpa(nd_region); |
| 430 | 431 | nvdimm_bus_unlock(dev); |
| 432 | + nd_device_unlock(dev); | |
| 431 | 433 | |
| 432 | 434 | return sprintf(buf, "%llu\n", available); |
| 433 | 435 | } |
| @@ -439,10 +441,12 @@ static ssize_t max_available_extent_show(struct device *dev, · 5 unmodified lines | ||
| 439 | 441 | struct nd_region *nd_region = to_nd_region(dev); |
| 440 | 442 | unsigned long long available = 0; |
| 441 | 443 | |
| 444 | + nd_device_lock(dev); | |
| 442 | 445 | nvdimm_bus_lock(dev); |
| 443 | 446 | wait_nvdimm_bus_probe_idle(dev); |
| 444 | 447 | available = nd_region_allocatable_dpa(nd_region); |
| 445 | 448 | nvdimm_bus_unlock(dev); |
| 449 | + nd_device_unlock(dev); | |
| 446 | 450 | |
| 447 | 451 | return sprintf(buf, "%llu\n", available); |
| 448 | 452 | } |
| @@ -561,12 +565,12 @@ static ssize_t region_badblocks_show(struct device *dev, · 112 unmodified lines | ||
| 561 | 565 | struct nd_region *nd_region = to_nd_region(dev); |
| 562 | 566 | ssize_t rc; |
| 563 | 567 | |
| 564 | − device_lock(dev); | |
| 568 | + nd_device_lock(dev); | |
| 565 | 569 | if (dev->driver) |
| 566 | 570 | rc = badblocks_show(&nd_region->bb, buf, 0); |
| 567 | 571 | else |
| 568 | 572 | rc = -ENXIO; |
| 569 | − device_unlock(dev); | |
| 573 | + nd_device_unlock(dev); | |
| 570 | 574 | |
| 571 | 575 | return rc; |
| 572 | 576 | } |
| @@ -915,6 +915,8 @@ struct dev_links_info { · 914 unmodified lines | ||
| 915 | 915 | * This identifies the device type and carries type-specific |
| 916 | 916 | * information. |
| 917 | 917 | * @mutex: Mutex to synchronize calls to its driver. |
| 918 | + * @lockdep_mutex: An optional debug lock that a subsystem can use as a | |
| 919 | + * peer lock to gain localized lockdep coverage of the device_lock. | |
| 918 | 920 | * @bus: Type of bus device is on. |
| 919 | 921 | * @driver: Which driver has allocated this |
| 920 | 922 | * @platform_data: Platform data specific to the device. |
| @@ -998,6 +1000,9 @@ struct device { · 77 unmodified lines | ||
| 998 | 1000 | core doesn't touch it */ |
| 999 | 1001 | void *driver_data; /* Driver data, set and get with |
| 1000 | 1002 | dev_set_drvdata/dev_get_drvdata */ |
| 1003 | +#ifdef CONFIG_PROVE_LOCKING | |
| 1004 | + struct mutex lockdep_mutex; | |
| 1005 | +#endif | |
| 1001 | 1006 | struct mutex mutex; /* mutex to synchronize calls to |
| 1002 | 1007 | * its driver. |
| 1003 | 1008 | */ |
| @@ -1383,6 +1388,7 @@ extern int (*platform_notify_remove)(struct device *dev); · 379 unmodified lines | ||
| 1383 | 1388 | */ |
| 1384 | 1389 | extern struct device *get_device(struct device *dev); |
| 1385 | 1390 | extern void put_device(struct device *dev); |
| 1391 | +extern bool kill_device(struct device *dev); | |
| 1386 | 1392 | |
| 1387 | 1393 | #ifdef CONFIG_DEVTMPFS |
| 1388 | 1394 | extern int devtmpfs_create_node(struct device *dev); |