Dan Williams | 51cf784 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* Copyright(c) 2017-2018 Intel Corporation. All rights reserved. */ |
Dan Williams | 89ec9f2 | 2018-10-29 15:52:42 -0700 | [diff] [blame] | 3 | #include <linux/memremap.h> |
Dan Williams | 51cf784 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 4 | #include <linux/device.h> |
Dan Williams | d200781 | 2018-11-07 15:31:23 -0800 | [diff] [blame] | 5 | #include <linux/mutex.h> |
| 6 | #include <linux/list.h> |
Dan Williams | 51cf784 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 7 | #include <linux/slab.h> |
| 8 | #include <linux/dax.h> |
Dan Williams | fcffb6a | 2020-10-13 16:50:24 -0700 | [diff] [blame] | 9 | #include <linux/io.h> |
Dan Williams | 51cf784 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 10 | #include "dax-private.h" |
| 11 | #include "bus.h" |
| 12 | |
Dan Williams | 730926c | 2017-07-16 13:51:53 -0700 | [diff] [blame] | 13 | static struct class *dax_class; |
| 14 | |
Dan Williams | d200781 | 2018-11-07 15:31:23 -0800 | [diff] [blame] | 15 | static DEFINE_MUTEX(dax_bus_lock); |
| 16 | |
| 17 | #define DAX_NAME_LEN 30 |
| 18 | struct dax_id { |
| 19 | struct list_head list; |
| 20 | char dev_name[DAX_NAME_LEN]; |
| 21 | }; |
| 22 | |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 23 | static int dax_bus_uevent(struct device *dev, struct kobj_uevent_env *env) |
| 24 | { |
| 25 | /* |
| 26 | * We only ever expect to handle device-dax instances, i.e. the |
| 27 | * @type argument to MODULE_ALIAS_DAX_DEVICE() is always zero |
| 28 | */ |
| 29 | return add_uevent_var(env, "MODALIAS=" DAX_DEVICE_MODALIAS_FMT, 0); |
| 30 | } |
| 31 | |
Dan Williams | d200781 | 2018-11-07 15:31:23 -0800 | [diff] [blame] | 32 | static struct dax_device_driver *to_dax_drv(struct device_driver *drv) |
| 33 | { |
| 34 | return container_of(drv, struct dax_device_driver, drv); |
| 35 | } |
| 36 | |
| 37 | static struct dax_id *__dax_match_id(struct dax_device_driver *dax_drv, |
| 38 | const char *dev_name) |
| 39 | { |
| 40 | struct dax_id *dax_id; |
| 41 | |
| 42 | lockdep_assert_held(&dax_bus_lock); |
| 43 | |
| 44 | list_for_each_entry(dax_id, &dax_drv->ids, list) |
| 45 | if (sysfs_streq(dax_id->dev_name, dev_name)) |
| 46 | return dax_id; |
| 47 | return NULL; |
| 48 | } |
| 49 | |
| 50 | static int dax_match_id(struct dax_device_driver *dax_drv, struct device *dev) |
| 51 | { |
| 52 | int match; |
| 53 | |
| 54 | mutex_lock(&dax_bus_lock); |
| 55 | match = !!__dax_match_id(dax_drv, dev_name(dev)); |
| 56 | mutex_unlock(&dax_bus_lock); |
| 57 | |
| 58 | return match; |
| 59 | } |
| 60 | |
Dan Williams | 664525b | 2019-01-24 13:12:04 -0800 | [diff] [blame] | 61 | enum id_action { |
| 62 | ID_REMOVE, |
| 63 | ID_ADD, |
| 64 | }; |
| 65 | |
Dan Williams | d200781 | 2018-11-07 15:31:23 -0800 | [diff] [blame] | 66 | static ssize_t do_id_store(struct device_driver *drv, const char *buf, |
Dan Williams | 664525b | 2019-01-24 13:12:04 -0800 | [diff] [blame] | 67 | size_t count, enum id_action action) |
Dan Williams | d200781 | 2018-11-07 15:31:23 -0800 | [diff] [blame] | 68 | { |
| 69 | struct dax_device_driver *dax_drv = to_dax_drv(drv); |
| 70 | unsigned int region_id, id; |
| 71 | char devname[DAX_NAME_LEN]; |
| 72 | struct dax_id *dax_id; |
| 73 | ssize_t rc = count; |
| 74 | int fields; |
| 75 | |
| 76 | fields = sscanf(buf, "dax%d.%d", ®ion_id, &id); |
| 77 | if (fields != 2) |
| 78 | return -EINVAL; |
| 79 | sprintf(devname, "dax%d.%d", region_id, id); |
| 80 | if (!sysfs_streq(buf, devname)) |
| 81 | return -EINVAL; |
| 82 | |
| 83 | mutex_lock(&dax_bus_lock); |
| 84 | dax_id = __dax_match_id(dax_drv, buf); |
| 85 | if (!dax_id) { |
Dan Williams | 664525b | 2019-01-24 13:12:04 -0800 | [diff] [blame] | 86 | if (action == ID_ADD) { |
Dan Williams | d200781 | 2018-11-07 15:31:23 -0800 | [diff] [blame] | 87 | dax_id = kzalloc(sizeof(*dax_id), GFP_KERNEL); |
| 88 | if (dax_id) { |
| 89 | strncpy(dax_id->dev_name, buf, DAX_NAME_LEN); |
| 90 | list_add(&dax_id->list, &dax_drv->ids); |
| 91 | } else |
| 92 | rc = -ENOMEM; |
Arnd Bergmann | 9de82ca | 2021-03-22 12:44:58 +0100 | [diff] [blame] | 93 | } |
Dan Williams | 664525b | 2019-01-24 13:12:04 -0800 | [diff] [blame] | 94 | } else if (action == ID_REMOVE) { |
Dan Williams | d200781 | 2018-11-07 15:31:23 -0800 | [diff] [blame] | 95 | list_del(&dax_id->list); |
| 96 | kfree(dax_id); |
Arnd Bergmann | 9de82ca | 2021-03-22 12:44:58 +0100 | [diff] [blame] | 97 | } |
Dan Williams | d200781 | 2018-11-07 15:31:23 -0800 | [diff] [blame] | 98 | mutex_unlock(&dax_bus_lock); |
Dan Williams | 664525b | 2019-01-24 13:12:04 -0800 | [diff] [blame] | 99 | |
| 100 | if (rc < 0) |
| 101 | return rc; |
| 102 | if (action == ID_ADD) |
| 103 | rc = driver_attach(drv); |
| 104 | if (rc) |
| 105 | return rc; |
| 106 | return count; |
Dan Williams | d200781 | 2018-11-07 15:31:23 -0800 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | static ssize_t new_id_store(struct device_driver *drv, const char *buf, |
| 110 | size_t count) |
| 111 | { |
Dan Williams | 664525b | 2019-01-24 13:12:04 -0800 | [diff] [blame] | 112 | return do_id_store(drv, buf, count, ID_ADD); |
Dan Williams | d200781 | 2018-11-07 15:31:23 -0800 | [diff] [blame] | 113 | } |
| 114 | static DRIVER_ATTR_WO(new_id); |
| 115 | |
| 116 | static ssize_t remove_id_store(struct device_driver *drv, const char *buf, |
| 117 | size_t count) |
| 118 | { |
Dan Williams | 664525b | 2019-01-24 13:12:04 -0800 | [diff] [blame] | 119 | return do_id_store(drv, buf, count, ID_REMOVE); |
Dan Williams | d200781 | 2018-11-07 15:31:23 -0800 | [diff] [blame] | 120 | } |
| 121 | static DRIVER_ATTR_WO(remove_id); |
| 122 | |
| 123 | static struct attribute *dax_drv_attrs[] = { |
| 124 | &driver_attr_new_id.attr, |
| 125 | &driver_attr_remove_id.attr, |
| 126 | NULL, |
| 127 | }; |
| 128 | ATTRIBUTE_GROUPS(dax_drv); |
| 129 | |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 130 | static int dax_bus_match(struct device *dev, struct device_driver *drv); |
| 131 | |
Dan Williams | c2f3011 | 2020-10-13 16:50:03 -0700 | [diff] [blame] | 132 | static bool is_static(struct dax_region *dax_region) |
| 133 | { |
| 134 | return (dax_region->res.flags & IORESOURCE_DAX_STATIC) != 0; |
| 135 | } |
| 136 | |
Dan Williams | 60e93dc | 2020-10-13 16:50:39 -0700 | [diff] [blame] | 137 | static u64 dev_dax_size(struct dev_dax *dev_dax) |
| 138 | { |
| 139 | u64 size = 0; |
| 140 | int i; |
| 141 | |
| 142 | device_lock_assert(&dev_dax->dev); |
| 143 | |
| 144 | for (i = 0; i < dev_dax->nr_range; i++) |
| 145 | size += range_len(&dev_dax->ranges[i].range); |
| 146 | |
| 147 | return size; |
| 148 | } |
| 149 | |
Dan Williams | f11cf81 | 2020-10-13 16:50:08 -0700 | [diff] [blame] | 150 | static int dax_bus_probe(struct device *dev) |
| 151 | { |
| 152 | struct dax_device_driver *dax_drv = to_dax_drv(dev->driver); |
| 153 | struct dev_dax *dev_dax = to_dev_dax(dev); |
Dan Williams | 0f3da14 | 2020-10-13 16:50:13 -0700 | [diff] [blame] | 154 | struct dax_region *dax_region = dev_dax->region; |
Dan Williams | 0f3da14 | 2020-10-13 16:50:13 -0700 | [diff] [blame] | 155 | int rc; |
Dan Williams | f11cf81 | 2020-10-13 16:50:08 -0700 | [diff] [blame] | 156 | |
Dan Williams | 60e93dc | 2020-10-13 16:50:39 -0700 | [diff] [blame] | 157 | if (dev_dax_size(dev_dax) == 0 || dev_dax->id < 0) |
Dan Williams | 0f3da14 | 2020-10-13 16:50:13 -0700 | [diff] [blame] | 158 | return -ENXIO; |
| 159 | |
| 160 | rc = dax_drv->probe(dev_dax); |
| 161 | |
| 162 | if (rc || is_static(dax_region)) |
| 163 | return rc; |
| 164 | |
| 165 | /* |
| 166 | * Track new seed creation only after successful probe of the |
| 167 | * previous seed. |
| 168 | */ |
| 169 | if (dax_region->seed == dev) |
| 170 | dax_region->seed = NULL; |
| 171 | |
| 172 | return 0; |
Dan Williams | f11cf81 | 2020-10-13 16:50:08 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | static int dax_bus_remove(struct device *dev) |
| 176 | { |
| 177 | struct dax_device_driver *dax_drv = to_dax_drv(dev->driver); |
| 178 | struct dev_dax *dev_dax = to_dev_dax(dev); |
| 179 | |
Uwe Kleine-König | 8029968 | 2021-02-05 23:28:39 +0100 | [diff] [blame] | 180 | if (dax_drv->remove) |
Uwe Kleine-König | 0d519e0 | 2021-02-05 23:28:42 +0100 | [diff] [blame] | 181 | dax_drv->remove(dev_dax); |
Uwe Kleine-König | 8029968 | 2021-02-05 23:28:39 +0100 | [diff] [blame] | 182 | |
Uwe Kleine-König | 0d519e0 | 2021-02-05 23:28:42 +0100 | [diff] [blame] | 183 | return 0; |
Dan Williams | f11cf81 | 2020-10-13 16:50:08 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 186 | static struct bus_type dax_bus_type = { |
| 187 | .name = "dax", |
| 188 | .uevent = dax_bus_uevent, |
| 189 | .match = dax_bus_match, |
Dan Williams | f11cf81 | 2020-10-13 16:50:08 -0700 | [diff] [blame] | 190 | .probe = dax_bus_probe, |
| 191 | .remove = dax_bus_remove, |
Dan Williams | d200781 | 2018-11-07 15:31:23 -0800 | [diff] [blame] | 192 | .drv_groups = dax_drv_groups, |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 193 | }; |
| 194 | |
| 195 | static int dax_bus_match(struct device *dev, struct device_driver *drv) |
| 196 | { |
Dan Williams | d200781 | 2018-11-07 15:31:23 -0800 | [diff] [blame] | 197 | struct dax_device_driver *dax_drv = to_dax_drv(drv); |
| 198 | |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 199 | /* |
Dan Williams | d200781 | 2018-11-07 15:31:23 -0800 | [diff] [blame] | 200 | * All but the 'device-dax' driver, which has 'match_always' |
| 201 | * set, requires an exact id match. |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 202 | */ |
Dan Williams | d200781 | 2018-11-07 15:31:23 -0800 | [diff] [blame] | 203 | if (dax_drv->match_always) |
| 204 | return 1; |
| 205 | |
| 206 | return dax_match_id(dax_drv, dev); |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Dan Williams | 51cf784 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 209 | /* |
| 210 | * Rely on the fact that drvdata is set before the attributes are |
| 211 | * registered, and that the attributes are unregistered before drvdata |
| 212 | * is cleared to assume that drvdata is always valid. |
| 213 | */ |
| 214 | static ssize_t id_show(struct device *dev, |
| 215 | struct device_attribute *attr, char *buf) |
| 216 | { |
| 217 | struct dax_region *dax_region = dev_get_drvdata(dev); |
| 218 | |
| 219 | return sprintf(buf, "%d\n", dax_region->id); |
| 220 | } |
| 221 | static DEVICE_ATTR_RO(id); |
| 222 | |
| 223 | static ssize_t region_size_show(struct device *dev, |
| 224 | struct device_attribute *attr, char *buf) |
| 225 | { |
| 226 | struct dax_region *dax_region = dev_get_drvdata(dev); |
| 227 | |
| 228 | return sprintf(buf, "%llu\n", (unsigned long long) |
| 229 | resource_size(&dax_region->res)); |
| 230 | } |
| 231 | static struct device_attribute dev_attr_region_size = __ATTR(size, 0444, |
| 232 | region_size_show, NULL); |
| 233 | |
Dan Williams | 6d82120 | 2020-10-13 16:50:55 -0700 | [diff] [blame] | 234 | static ssize_t region_align_show(struct device *dev, |
Dan Williams | 51cf784 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 235 | struct device_attribute *attr, char *buf) |
| 236 | { |
| 237 | struct dax_region *dax_region = dev_get_drvdata(dev); |
| 238 | |
| 239 | return sprintf(buf, "%u\n", dax_region->align); |
| 240 | } |
Dan Williams | 6d82120 | 2020-10-13 16:50:55 -0700 | [diff] [blame] | 241 | static struct device_attribute dev_attr_region_align = |
| 242 | __ATTR(align, 0400, region_align_show, NULL); |
Dan Williams | 51cf784 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 243 | |
Dan Williams | c2f3011 | 2020-10-13 16:50:03 -0700 | [diff] [blame] | 244 | #define for_each_dax_region_resource(dax_region, res) \ |
| 245 | for (res = (dax_region)->res.child; res; res = res->sibling) |
| 246 | |
| 247 | static unsigned long long dax_region_avail_size(struct dax_region *dax_region) |
| 248 | { |
| 249 | resource_size_t size = resource_size(&dax_region->res); |
| 250 | struct resource *res; |
| 251 | |
| 252 | device_lock_assert(dax_region->dev); |
| 253 | |
| 254 | for_each_dax_region_resource(dax_region, res) |
| 255 | size -= resource_size(res); |
| 256 | return size; |
| 257 | } |
| 258 | |
| 259 | static ssize_t available_size_show(struct device *dev, |
| 260 | struct device_attribute *attr, char *buf) |
| 261 | { |
| 262 | struct dax_region *dax_region = dev_get_drvdata(dev); |
| 263 | unsigned long long size; |
| 264 | |
| 265 | device_lock(dev); |
| 266 | size = dax_region_avail_size(dax_region); |
| 267 | device_unlock(dev); |
| 268 | |
| 269 | return sprintf(buf, "%llu\n", size); |
| 270 | } |
| 271 | static DEVICE_ATTR_RO(available_size); |
| 272 | |
Dan Williams | 0f3da14 | 2020-10-13 16:50:13 -0700 | [diff] [blame] | 273 | static ssize_t seed_show(struct device *dev, |
| 274 | struct device_attribute *attr, char *buf) |
| 275 | { |
| 276 | struct dax_region *dax_region = dev_get_drvdata(dev); |
| 277 | struct device *seed; |
| 278 | ssize_t rc; |
| 279 | |
| 280 | if (is_static(dax_region)) |
| 281 | return -EINVAL; |
| 282 | |
| 283 | device_lock(dev); |
| 284 | seed = dax_region->seed; |
| 285 | rc = sprintf(buf, "%s\n", seed ? dev_name(seed) : ""); |
| 286 | device_unlock(dev); |
| 287 | |
| 288 | return rc; |
| 289 | } |
| 290 | static DEVICE_ATTR_RO(seed); |
| 291 | |
| 292 | static ssize_t create_show(struct device *dev, |
| 293 | struct device_attribute *attr, char *buf) |
| 294 | { |
| 295 | struct dax_region *dax_region = dev_get_drvdata(dev); |
| 296 | struct device *youngest; |
| 297 | ssize_t rc; |
| 298 | |
| 299 | if (is_static(dax_region)) |
| 300 | return -EINVAL; |
| 301 | |
| 302 | device_lock(dev); |
| 303 | youngest = dax_region->youngest; |
| 304 | rc = sprintf(buf, "%s\n", youngest ? dev_name(youngest) : ""); |
| 305 | device_unlock(dev); |
| 306 | |
| 307 | return rc; |
| 308 | } |
| 309 | |
| 310 | static ssize_t create_store(struct device *dev, struct device_attribute *attr, |
| 311 | const char *buf, size_t len) |
| 312 | { |
| 313 | struct dax_region *dax_region = dev_get_drvdata(dev); |
| 314 | unsigned long long avail; |
| 315 | ssize_t rc; |
| 316 | int val; |
| 317 | |
| 318 | if (is_static(dax_region)) |
| 319 | return -EINVAL; |
| 320 | |
| 321 | rc = kstrtoint(buf, 0, &val); |
| 322 | if (rc) |
| 323 | return rc; |
| 324 | if (val != 1) |
| 325 | return -EINVAL; |
| 326 | |
| 327 | device_lock(dev); |
| 328 | avail = dax_region_avail_size(dax_region); |
| 329 | if (avail == 0) |
| 330 | rc = -ENOSPC; |
| 331 | else { |
| 332 | struct dev_dax_data data = { |
| 333 | .dax_region = dax_region, |
| 334 | .size = 0, |
| 335 | .id = -1, |
| 336 | }; |
| 337 | struct dev_dax *dev_dax = devm_create_dev_dax(&data); |
| 338 | |
| 339 | if (IS_ERR(dev_dax)) |
| 340 | rc = PTR_ERR(dev_dax); |
| 341 | else { |
| 342 | /* |
| 343 | * In support of crafting multiple new devices |
| 344 | * simultaneously multiple seeds can be created, |
| 345 | * but only the first one that has not been |
| 346 | * successfully bound is tracked as the region |
| 347 | * seed. |
| 348 | */ |
| 349 | if (!dax_region->seed) |
| 350 | dax_region->seed = &dev_dax->dev; |
| 351 | dax_region->youngest = &dev_dax->dev; |
| 352 | rc = len; |
| 353 | } |
| 354 | } |
| 355 | device_unlock(dev); |
| 356 | |
| 357 | return rc; |
| 358 | } |
| 359 | static DEVICE_ATTR_RW(create); |
| 360 | |
| 361 | void kill_dev_dax(struct dev_dax *dev_dax) |
| 362 | { |
| 363 | struct dax_device *dax_dev = dev_dax->dax_dev; |
| 364 | struct inode *inode = dax_inode(dax_dev); |
| 365 | |
| 366 | kill_dax(dax_dev); |
| 367 | unmap_mapping_range(inode->i_mapping, 0, 0, 1); |
| 368 | } |
| 369 | EXPORT_SYMBOL_GPL(kill_dev_dax); |
| 370 | |
Dan Williams | 6268d7d | 2020-12-18 18:41:41 -0800 | [diff] [blame] | 371 | static void trim_dev_dax_range(struct dev_dax *dev_dax) |
Dan Williams | 0f3da14 | 2020-10-13 16:50:13 -0700 | [diff] [blame] | 372 | { |
Dan Williams | 6268d7d | 2020-12-18 18:41:41 -0800 | [diff] [blame] | 373 | int i = dev_dax->nr_range - 1; |
| 374 | struct range *range = &dev_dax->ranges[i].range; |
Dan Williams | 0f3da14 | 2020-10-13 16:50:13 -0700 | [diff] [blame] | 375 | struct dax_region *dax_region = dev_dax->region; |
Dan Williams | 0f3da14 | 2020-10-13 16:50:13 -0700 | [diff] [blame] | 376 | |
| 377 | device_lock_assert(dax_region->dev); |
Dan Williams | 6268d7d | 2020-12-18 18:41:41 -0800 | [diff] [blame] | 378 | dev_dbg(&dev_dax->dev, "delete range[%d]: %#llx:%#llx\n", i, |
| 379 | (unsigned long long)range->start, |
| 380 | (unsigned long long)range->end); |
Dan Williams | 60e93dc | 2020-10-13 16:50:39 -0700 | [diff] [blame] | 381 | |
Dan Williams | 6268d7d | 2020-12-18 18:41:41 -0800 | [diff] [blame] | 382 | __release_region(&dax_region->res, range->start, range_len(range)); |
| 383 | if (--dev_dax->nr_range == 0) { |
| 384 | kfree(dev_dax->ranges); |
| 385 | dev_dax->ranges = NULL; |
Dan Williams | 60e93dc | 2020-10-13 16:50:39 -0700 | [diff] [blame] | 386 | } |
Dan Williams | 6268d7d | 2020-12-18 18:41:41 -0800 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | static void free_dev_dax_ranges(struct dev_dax *dev_dax) |
| 390 | { |
| 391 | while (dev_dax->nr_range) |
| 392 | trim_dev_dax_range(dev_dax); |
Dan Williams | 0f3da14 | 2020-10-13 16:50:13 -0700 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | static void unregister_dev_dax(void *dev) |
| 396 | { |
| 397 | struct dev_dax *dev_dax = to_dev_dax(dev); |
| 398 | |
| 399 | dev_dbg(dev, "%s\n", __func__); |
| 400 | |
| 401 | kill_dev_dax(dev_dax); |
Dan Williams | 60e93dc | 2020-10-13 16:50:39 -0700 | [diff] [blame] | 402 | free_dev_dax_ranges(dev_dax); |
Dan Williams | 0f3da14 | 2020-10-13 16:50:13 -0700 | [diff] [blame] | 403 | device_del(dev); |
| 404 | put_device(dev); |
| 405 | } |
| 406 | |
| 407 | /* a return value >= 0 indicates this invocation invalidated the id */ |
| 408 | static int __free_dev_dax_id(struct dev_dax *dev_dax) |
| 409 | { |
| 410 | struct dax_region *dax_region = dev_dax->region; |
| 411 | struct device *dev = &dev_dax->dev; |
| 412 | int rc = dev_dax->id; |
| 413 | |
| 414 | device_lock_assert(dev); |
| 415 | |
| 416 | if (is_static(dax_region) || dev_dax->id < 0) |
| 417 | return -1; |
| 418 | ida_free(&dax_region->ida, dev_dax->id); |
| 419 | dev_dax->id = -1; |
| 420 | return rc; |
| 421 | } |
| 422 | |
| 423 | static int free_dev_dax_id(struct dev_dax *dev_dax) |
| 424 | { |
| 425 | struct device *dev = &dev_dax->dev; |
| 426 | int rc; |
| 427 | |
| 428 | device_lock(dev); |
| 429 | rc = __free_dev_dax_id(dev_dax); |
| 430 | device_unlock(dev); |
| 431 | return rc; |
| 432 | } |
| 433 | |
| 434 | static ssize_t delete_store(struct device *dev, struct device_attribute *attr, |
| 435 | const char *buf, size_t len) |
| 436 | { |
| 437 | struct dax_region *dax_region = dev_get_drvdata(dev); |
| 438 | struct dev_dax *dev_dax; |
| 439 | struct device *victim; |
| 440 | bool do_del = false; |
| 441 | int rc; |
| 442 | |
| 443 | if (is_static(dax_region)) |
| 444 | return -EINVAL; |
| 445 | |
| 446 | victim = device_find_child_by_name(dax_region->dev, buf); |
| 447 | if (!victim) |
| 448 | return -ENXIO; |
| 449 | |
| 450 | device_lock(dev); |
| 451 | device_lock(victim); |
| 452 | dev_dax = to_dev_dax(victim); |
Dan Williams | 60e93dc | 2020-10-13 16:50:39 -0700 | [diff] [blame] | 453 | if (victim->driver || dev_dax_size(dev_dax)) |
Dan Williams | 0f3da14 | 2020-10-13 16:50:13 -0700 | [diff] [blame] | 454 | rc = -EBUSY; |
| 455 | else { |
| 456 | /* |
| 457 | * Invalidate the device so it does not become active |
| 458 | * again, but always preserve device-id-0 so that |
| 459 | * /sys/bus/dax/ is guaranteed to be populated while any |
| 460 | * dax_region is registered. |
| 461 | */ |
| 462 | if (dev_dax->id > 0) { |
| 463 | do_del = __free_dev_dax_id(dev_dax) >= 0; |
| 464 | rc = len; |
| 465 | if (dax_region->seed == victim) |
| 466 | dax_region->seed = NULL; |
| 467 | if (dax_region->youngest == victim) |
| 468 | dax_region->youngest = NULL; |
| 469 | } else |
| 470 | rc = -EBUSY; |
| 471 | } |
| 472 | device_unlock(victim); |
| 473 | |
| 474 | /* won the race to invalidate the device, clean it up */ |
| 475 | if (do_del) |
| 476 | devm_release_action(dev, unregister_dev_dax, victim); |
| 477 | device_unlock(dev); |
| 478 | put_device(victim); |
| 479 | |
| 480 | return rc; |
| 481 | } |
| 482 | static DEVICE_ATTR_WO(delete); |
| 483 | |
Dan Williams | c2f3011 | 2020-10-13 16:50:03 -0700 | [diff] [blame] | 484 | static umode_t dax_region_visible(struct kobject *kobj, struct attribute *a, |
| 485 | int n) |
| 486 | { |
| 487 | struct device *dev = container_of(kobj, struct device, kobj); |
| 488 | struct dax_region *dax_region = dev_get_drvdata(dev); |
| 489 | |
Dan Williams | 0f3da14 | 2020-10-13 16:50:13 -0700 | [diff] [blame] | 490 | if (is_static(dax_region)) |
| 491 | if (a == &dev_attr_available_size.attr |
| 492 | || a == &dev_attr_create.attr |
| 493 | || a == &dev_attr_seed.attr |
| 494 | || a == &dev_attr_delete.attr) |
| 495 | return 0; |
Dan Williams | c2f3011 | 2020-10-13 16:50:03 -0700 | [diff] [blame] | 496 | return a->mode; |
| 497 | } |
| 498 | |
Dan Williams | 51cf784 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 499 | static struct attribute *dax_region_attributes[] = { |
Dan Williams | c2f3011 | 2020-10-13 16:50:03 -0700 | [diff] [blame] | 500 | &dev_attr_available_size.attr, |
Dan Williams | 51cf784 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 501 | &dev_attr_region_size.attr, |
Dan Williams | 6d82120 | 2020-10-13 16:50:55 -0700 | [diff] [blame] | 502 | &dev_attr_region_align.attr, |
Dan Williams | 0f3da14 | 2020-10-13 16:50:13 -0700 | [diff] [blame] | 503 | &dev_attr_create.attr, |
| 504 | &dev_attr_seed.attr, |
| 505 | &dev_attr_delete.attr, |
Dan Williams | 51cf784 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 506 | &dev_attr_id.attr, |
| 507 | NULL, |
| 508 | }; |
| 509 | |
| 510 | static const struct attribute_group dax_region_attribute_group = { |
| 511 | .name = "dax_region", |
| 512 | .attrs = dax_region_attributes, |
Dan Williams | c2f3011 | 2020-10-13 16:50:03 -0700 | [diff] [blame] | 513 | .is_visible = dax_region_visible, |
Dan Williams | 51cf784 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 514 | }; |
| 515 | |
| 516 | static const struct attribute_group *dax_region_attribute_groups[] = { |
| 517 | &dax_region_attribute_group, |
| 518 | NULL, |
| 519 | }; |
| 520 | |
| 521 | static void dax_region_free(struct kref *kref) |
| 522 | { |
| 523 | struct dax_region *dax_region; |
| 524 | |
| 525 | dax_region = container_of(kref, struct dax_region, kref); |
| 526 | kfree(dax_region); |
| 527 | } |
| 528 | |
| 529 | void dax_region_put(struct dax_region *dax_region) |
| 530 | { |
| 531 | kref_put(&dax_region->kref, dax_region_free); |
| 532 | } |
| 533 | EXPORT_SYMBOL_GPL(dax_region_put); |
| 534 | |
| 535 | static void dax_region_unregister(void *region) |
| 536 | { |
| 537 | struct dax_region *dax_region = region; |
| 538 | |
| 539 | sysfs_remove_groups(&dax_region->dev->kobj, |
| 540 | dax_region_attribute_groups); |
| 541 | dax_region_put(dax_region); |
| 542 | } |
| 543 | |
| 544 | struct dax_region *alloc_dax_region(struct device *parent, int region_id, |
Dan Williams | a4574f6 | 2020-10-13 16:50:29 -0700 | [diff] [blame] | 545 | struct range *range, int target_node, unsigned int align, |
Dan Williams | c2f3011 | 2020-10-13 16:50:03 -0700 | [diff] [blame] | 546 | unsigned long flags) |
Dan Williams | 51cf784 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 547 | { |
| 548 | struct dax_region *dax_region; |
| 549 | |
| 550 | /* |
| 551 | * The DAX core assumes that it can store its private data in |
| 552 | * parent->driver_data. This WARN is a reminder / safeguard for |
| 553 | * developers of device-dax drivers. |
| 554 | */ |
| 555 | if (dev_get_drvdata(parent)) { |
| 556 | dev_WARN(parent, "dax core failed to setup private data\n"); |
| 557 | return NULL; |
| 558 | } |
| 559 | |
Dan Williams | a4574f6 | 2020-10-13 16:50:29 -0700 | [diff] [blame] | 560 | if (!IS_ALIGNED(range->start, align) |
| 561 | || !IS_ALIGNED(range_len(range), align)) |
Dan Williams | 51cf784 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 562 | return NULL; |
| 563 | |
| 564 | dax_region = kzalloc(sizeof(*dax_region), GFP_KERNEL); |
| 565 | if (!dax_region) |
| 566 | return NULL; |
| 567 | |
| 568 | dev_set_drvdata(parent, dax_region); |
Dan Williams | 51cf784 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 569 | kref_init(&dax_region->kref); |
| 570 | dax_region->id = region_id; |
| 571 | dax_region->align = align; |
| 572 | dax_region->dev = parent; |
Dan Williams | 8fc5c73 | 2018-11-09 12:43:07 -0800 | [diff] [blame] | 573 | dax_region->target_node = target_node; |
Dan Williams | 0f3da14 | 2020-10-13 16:50:13 -0700 | [diff] [blame] | 574 | ida_init(&dax_region->ida); |
Dan Williams | c2f3011 | 2020-10-13 16:50:03 -0700 | [diff] [blame] | 575 | dax_region->res = (struct resource) { |
Dan Williams | a4574f6 | 2020-10-13 16:50:29 -0700 | [diff] [blame] | 576 | .start = range->start, |
| 577 | .end = range->end, |
Dan Williams | c2f3011 | 2020-10-13 16:50:03 -0700 | [diff] [blame] | 578 | .flags = IORESOURCE_MEM | flags, |
| 579 | }; |
| 580 | |
Dan Williams | 51cf784 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 581 | if (sysfs_create_groups(&parent->kobj, dax_region_attribute_groups)) { |
| 582 | kfree(dax_region); |
| 583 | return NULL; |
| 584 | } |
| 585 | |
| 586 | kref_get(&dax_region->kref); |
| 587 | if (devm_add_action_or_reset(parent, dax_region_unregister, dax_region)) |
| 588 | return NULL; |
| 589 | return dax_region; |
| 590 | } |
| 591 | EXPORT_SYMBOL_GPL(alloc_dax_region); |
| 592 | |
Dan Williams | 0b07ce8 | 2020-10-13 16:50:45 -0700 | [diff] [blame] | 593 | static void dax_mapping_release(struct device *dev) |
| 594 | { |
| 595 | struct dax_mapping *mapping = to_dax_mapping(dev); |
| 596 | struct dev_dax *dev_dax = to_dev_dax(dev->parent); |
| 597 | |
| 598 | ida_free(&dev_dax->ida, mapping->id); |
| 599 | kfree(mapping); |
| 600 | } |
| 601 | |
| 602 | static void unregister_dax_mapping(void *data) |
| 603 | { |
| 604 | struct device *dev = data; |
| 605 | struct dax_mapping *mapping = to_dax_mapping(dev); |
| 606 | struct dev_dax *dev_dax = to_dev_dax(dev->parent); |
| 607 | struct dax_region *dax_region = dev_dax->region; |
| 608 | |
| 609 | dev_dbg(dev, "%s\n", __func__); |
| 610 | |
| 611 | device_lock_assert(dax_region->dev); |
| 612 | |
| 613 | dev_dax->ranges[mapping->range_id].mapping = NULL; |
| 614 | mapping->range_id = -1; |
| 615 | |
| 616 | device_del(dev); |
| 617 | put_device(dev); |
| 618 | } |
| 619 | |
| 620 | static struct dev_dax_range *get_dax_range(struct device *dev) |
| 621 | { |
| 622 | struct dax_mapping *mapping = to_dax_mapping(dev); |
| 623 | struct dev_dax *dev_dax = to_dev_dax(dev->parent); |
| 624 | struct dax_region *dax_region = dev_dax->region; |
| 625 | |
| 626 | device_lock(dax_region->dev); |
| 627 | if (mapping->range_id < 0) { |
| 628 | device_unlock(dax_region->dev); |
| 629 | return NULL; |
| 630 | } |
| 631 | |
| 632 | return &dev_dax->ranges[mapping->range_id]; |
| 633 | } |
| 634 | |
| 635 | static void put_dax_range(struct dev_dax_range *dax_range) |
| 636 | { |
| 637 | struct dax_mapping *mapping = dax_range->mapping; |
| 638 | struct dev_dax *dev_dax = to_dev_dax(mapping->dev.parent); |
| 639 | struct dax_region *dax_region = dev_dax->region; |
| 640 | |
| 641 | device_unlock(dax_region->dev); |
| 642 | } |
| 643 | |
| 644 | static ssize_t start_show(struct device *dev, |
| 645 | struct device_attribute *attr, char *buf) |
| 646 | { |
| 647 | struct dev_dax_range *dax_range; |
| 648 | ssize_t rc; |
| 649 | |
| 650 | dax_range = get_dax_range(dev); |
| 651 | if (!dax_range) |
| 652 | return -ENXIO; |
| 653 | rc = sprintf(buf, "%#llx\n", dax_range->range.start); |
| 654 | put_dax_range(dax_range); |
| 655 | |
| 656 | return rc; |
| 657 | } |
| 658 | static DEVICE_ATTR(start, 0400, start_show, NULL); |
| 659 | |
| 660 | static ssize_t end_show(struct device *dev, |
| 661 | struct device_attribute *attr, char *buf) |
| 662 | { |
| 663 | struct dev_dax_range *dax_range; |
| 664 | ssize_t rc; |
| 665 | |
| 666 | dax_range = get_dax_range(dev); |
| 667 | if (!dax_range) |
| 668 | return -ENXIO; |
| 669 | rc = sprintf(buf, "%#llx\n", dax_range->range.end); |
| 670 | put_dax_range(dax_range); |
| 671 | |
| 672 | return rc; |
| 673 | } |
| 674 | static DEVICE_ATTR(end, 0400, end_show, NULL); |
| 675 | |
| 676 | static ssize_t pgoff_show(struct device *dev, |
| 677 | struct device_attribute *attr, char *buf) |
| 678 | { |
| 679 | struct dev_dax_range *dax_range; |
| 680 | ssize_t rc; |
| 681 | |
| 682 | dax_range = get_dax_range(dev); |
| 683 | if (!dax_range) |
| 684 | return -ENXIO; |
| 685 | rc = sprintf(buf, "%#lx\n", dax_range->pgoff); |
| 686 | put_dax_range(dax_range); |
| 687 | |
| 688 | return rc; |
| 689 | } |
| 690 | static DEVICE_ATTR(page_offset, 0400, pgoff_show, NULL); |
| 691 | |
| 692 | static struct attribute *dax_mapping_attributes[] = { |
| 693 | &dev_attr_start.attr, |
| 694 | &dev_attr_end.attr, |
| 695 | &dev_attr_page_offset.attr, |
| 696 | NULL, |
| 697 | }; |
| 698 | |
| 699 | static const struct attribute_group dax_mapping_attribute_group = { |
| 700 | .attrs = dax_mapping_attributes, |
| 701 | }; |
| 702 | |
| 703 | static const struct attribute_group *dax_mapping_attribute_groups[] = { |
| 704 | &dax_mapping_attribute_group, |
| 705 | NULL, |
| 706 | }; |
| 707 | |
| 708 | static struct device_type dax_mapping_type = { |
| 709 | .release = dax_mapping_release, |
| 710 | .groups = dax_mapping_attribute_groups, |
| 711 | }; |
| 712 | |
| 713 | static int devm_register_dax_mapping(struct dev_dax *dev_dax, int range_id) |
| 714 | { |
| 715 | struct dax_region *dax_region = dev_dax->region; |
| 716 | struct dax_mapping *mapping; |
| 717 | struct device *dev; |
| 718 | int rc; |
| 719 | |
| 720 | device_lock_assert(dax_region->dev); |
| 721 | |
| 722 | if (dev_WARN_ONCE(&dev_dax->dev, !dax_region->dev->driver, |
| 723 | "region disabled\n")) |
| 724 | return -ENXIO; |
| 725 | |
| 726 | mapping = kzalloc(sizeof(*mapping), GFP_KERNEL); |
| 727 | if (!mapping) |
| 728 | return -ENOMEM; |
| 729 | mapping->range_id = range_id; |
| 730 | mapping->id = ida_alloc(&dev_dax->ida, GFP_KERNEL); |
| 731 | if (mapping->id < 0) { |
| 732 | kfree(mapping); |
| 733 | return -ENOMEM; |
| 734 | } |
| 735 | dev_dax->ranges[range_id].mapping = mapping; |
| 736 | dev = &mapping->dev; |
| 737 | device_initialize(dev); |
| 738 | dev->parent = &dev_dax->dev; |
| 739 | dev->type = &dax_mapping_type; |
| 740 | dev_set_name(dev, "mapping%d", mapping->id); |
| 741 | rc = device_add(dev); |
| 742 | if (rc) { |
| 743 | put_device(dev); |
| 744 | return rc; |
| 745 | } |
| 746 | |
| 747 | rc = devm_add_action_or_reset(dax_region->dev, unregister_dax_mapping, |
| 748 | dev); |
| 749 | if (rc) |
| 750 | return rc; |
| 751 | return 0; |
| 752 | } |
| 753 | |
Dan Williams | fcffb6a | 2020-10-13 16:50:24 -0700 | [diff] [blame] | 754 | static int alloc_dev_dax_range(struct dev_dax *dev_dax, u64 start, |
| 755 | resource_size_t size) |
Dan Williams | c2f3011 | 2020-10-13 16:50:03 -0700 | [diff] [blame] | 756 | { |
| 757 | struct dax_region *dax_region = dev_dax->region; |
| 758 | struct resource *res = &dax_region->res; |
| 759 | struct device *dev = &dev_dax->dev; |
Dan Williams | 60e93dc | 2020-10-13 16:50:39 -0700 | [diff] [blame] | 760 | struct dev_dax_range *ranges; |
| 761 | unsigned long pgoff = 0; |
Dan Williams | c2f3011 | 2020-10-13 16:50:03 -0700 | [diff] [blame] | 762 | struct resource *alloc; |
Dan Williams | 0b07ce8 | 2020-10-13 16:50:45 -0700 | [diff] [blame] | 763 | int i, rc; |
Dan Williams | c2f3011 | 2020-10-13 16:50:03 -0700 | [diff] [blame] | 764 | |
| 765 | device_lock_assert(dax_region->dev); |
| 766 | |
Dan Williams | 0f3da14 | 2020-10-13 16:50:13 -0700 | [diff] [blame] | 767 | /* handle the seed alloc special case */ |
| 768 | if (!size) { |
Dan Williams | 60e93dc | 2020-10-13 16:50:39 -0700 | [diff] [blame] | 769 | if (dev_WARN_ONCE(dev, dev_dax->nr_range, |
| 770 | "0-size allocation must be first\n")) |
| 771 | return -EBUSY; |
| 772 | /* nr_range == 0 is elsewhere special cased as 0-size device */ |
Dan Williams | 0f3da14 | 2020-10-13 16:50:13 -0700 | [diff] [blame] | 773 | return 0; |
| 774 | } |
| 775 | |
Zhen Lei | ff8da37 | 2020-12-19 16:18:40 +0800 | [diff] [blame] | 776 | alloc = __request_region(res, start, size, dev_name(dev), 0); |
| 777 | if (!alloc) |
Dan Williams | c2f3011 | 2020-10-13 16:50:03 -0700 | [diff] [blame] | 778 | return -ENOMEM; |
| 779 | |
Zhen Lei | ff8da37 | 2020-12-19 16:18:40 +0800 | [diff] [blame] | 780 | ranges = krealloc(dev_dax->ranges, sizeof(*ranges) |
| 781 | * (dev_dax->nr_range + 1), GFP_KERNEL); |
| 782 | if (!ranges) { |
| 783 | __release_region(res, alloc->start, resource_size(alloc)); |
Dan Williams | 60e93dc | 2020-10-13 16:50:39 -0700 | [diff] [blame] | 784 | return -ENOMEM; |
| 785 | } |
| 786 | |
| 787 | for (i = 0; i < dev_dax->nr_range; i++) |
| 788 | pgoff += PHYS_PFN(range_len(&ranges[i].range)); |
| 789 | dev_dax->ranges = ranges; |
| 790 | ranges[dev_dax->nr_range++] = (struct dev_dax_range) { |
| 791 | .pgoff = pgoff, |
| 792 | .range = { |
| 793 | .start = alloc->start, |
| 794 | .end = alloc->end, |
| 795 | }, |
Dan Williams | c2f3011 | 2020-10-13 16:50:03 -0700 | [diff] [blame] | 796 | }; |
| 797 | |
Dan Williams | 60e93dc | 2020-10-13 16:50:39 -0700 | [diff] [blame] | 798 | dev_dbg(dev, "alloc range[%d]: %pa:%pa\n", dev_dax->nr_range - 1, |
| 799 | &alloc->start, &alloc->end); |
Dan Williams | 0b07ce8 | 2020-10-13 16:50:45 -0700 | [diff] [blame] | 800 | /* |
| 801 | * A dev_dax instance must be registered before mapping device |
| 802 | * children can be added. Defer to devm_create_dev_dax() to add |
| 803 | * the initial mapping device. |
| 804 | */ |
| 805 | if (!device_is_registered(&dev_dax->dev)) |
| 806 | return 0; |
| 807 | |
| 808 | rc = devm_register_dax_mapping(dev_dax, dev_dax->nr_range - 1); |
Dan Williams | 6268d7d | 2020-12-18 18:41:41 -0800 | [diff] [blame] | 809 | if (rc) |
| 810 | trim_dev_dax_range(dev_dax); |
Dan Williams | 60e93dc | 2020-10-13 16:50:39 -0700 | [diff] [blame] | 811 | |
Dan Williams | 6268d7d | 2020-12-18 18:41:41 -0800 | [diff] [blame] | 812 | return rc; |
Dan Williams | c2f3011 | 2020-10-13 16:50:03 -0700 | [diff] [blame] | 813 | } |
| 814 | |
Dan Williams | fcffb6a | 2020-10-13 16:50:24 -0700 | [diff] [blame] | 815 | static int adjust_dev_dax_range(struct dev_dax *dev_dax, struct resource *res, resource_size_t size) |
| 816 | { |
Dan Williams | 60e93dc | 2020-10-13 16:50:39 -0700 | [diff] [blame] | 817 | int last_range = dev_dax->nr_range - 1; |
| 818 | struct dev_dax_range *dax_range = &dev_dax->ranges[last_range]; |
Dan Williams | fcffb6a | 2020-10-13 16:50:24 -0700 | [diff] [blame] | 819 | struct dax_region *dax_region = dev_dax->region; |
Dan Williams | 60e93dc | 2020-10-13 16:50:39 -0700 | [diff] [blame] | 820 | bool is_shrink = resource_size(res) > size; |
| 821 | struct range *range = &dax_range->range; |
| 822 | struct device *dev = &dev_dax->dev; |
| 823 | int rc; |
Dan Williams | fcffb6a | 2020-10-13 16:50:24 -0700 | [diff] [blame] | 824 | |
| 825 | device_lock_assert(dax_region->dev); |
| 826 | |
Dan Williams | 60e93dc | 2020-10-13 16:50:39 -0700 | [diff] [blame] | 827 | if (dev_WARN_ONCE(dev, !size, "deletion is handled by dev_dax_shrink\n")) |
| 828 | return -EINVAL; |
| 829 | |
| 830 | rc = adjust_resource(res, range->start, size); |
Dan Williams | fcffb6a | 2020-10-13 16:50:24 -0700 | [diff] [blame] | 831 | if (rc) |
| 832 | return rc; |
| 833 | |
Dan Williams | 60e93dc | 2020-10-13 16:50:39 -0700 | [diff] [blame] | 834 | *range = (struct range) { |
Dan Williams | fcffb6a | 2020-10-13 16:50:24 -0700 | [diff] [blame] | 835 | .start = range->start, |
| 836 | .end = range->start + size - 1, |
| 837 | }; |
| 838 | |
Dan Williams | 60e93dc | 2020-10-13 16:50:39 -0700 | [diff] [blame] | 839 | dev_dbg(dev, "%s range[%d]: %#llx:%#llx\n", is_shrink ? "shrink" : "extend", |
| 840 | last_range, (unsigned long long) range->start, |
| 841 | (unsigned long long) range->end); |
| 842 | |
Dan Williams | fcffb6a | 2020-10-13 16:50:24 -0700 | [diff] [blame] | 843 | return 0; |
| 844 | } |
| 845 | |
Dan Williams | 51cf784 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 846 | static ssize_t size_show(struct device *dev, |
| 847 | struct device_attribute *attr, char *buf) |
| 848 | { |
| 849 | struct dev_dax *dev_dax = to_dev_dax(dev); |
Dan Williams | 60e93dc | 2020-10-13 16:50:39 -0700 | [diff] [blame] | 850 | unsigned long long size; |
| 851 | |
| 852 | device_lock(dev); |
| 853 | size = dev_dax_size(dev_dax); |
| 854 | device_unlock(dev); |
Dan Williams | 51cf784 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 855 | |
| 856 | return sprintf(buf, "%llu\n", size); |
| 857 | } |
Dan Williams | fcffb6a | 2020-10-13 16:50:24 -0700 | [diff] [blame] | 858 | |
Dan Williams | 6d82120 | 2020-10-13 16:50:55 -0700 | [diff] [blame] | 859 | static bool alloc_is_aligned(struct dev_dax *dev_dax, resource_size_t size) |
Dan Williams | fcffb6a | 2020-10-13 16:50:24 -0700 | [diff] [blame] | 860 | { |
| 861 | /* |
| 862 | * The minimum mapping granularity for a device instance is a |
| 863 | * single subsection, unless the arch says otherwise. |
| 864 | */ |
Dan Williams | 6d82120 | 2020-10-13 16:50:55 -0700 | [diff] [blame] | 865 | return IS_ALIGNED(size, max_t(unsigned long, dev_dax->align, memremap_compat_align())); |
Dan Williams | fcffb6a | 2020-10-13 16:50:24 -0700 | [diff] [blame] | 866 | } |
| 867 | |
| 868 | static int dev_dax_shrink(struct dev_dax *dev_dax, resource_size_t size) |
| 869 | { |
Dan Williams | 60e93dc | 2020-10-13 16:50:39 -0700 | [diff] [blame] | 870 | resource_size_t to_shrink = dev_dax_size(dev_dax) - size; |
Dan Williams | fcffb6a | 2020-10-13 16:50:24 -0700 | [diff] [blame] | 871 | struct dax_region *dax_region = dev_dax->region; |
Dan Williams | fcffb6a | 2020-10-13 16:50:24 -0700 | [diff] [blame] | 872 | struct device *dev = &dev_dax->dev; |
Dan Williams | 60e93dc | 2020-10-13 16:50:39 -0700 | [diff] [blame] | 873 | int i; |
Dan Williams | fcffb6a | 2020-10-13 16:50:24 -0700 | [diff] [blame] | 874 | |
Dan Williams | 60e93dc | 2020-10-13 16:50:39 -0700 | [diff] [blame] | 875 | for (i = dev_dax->nr_range - 1; i >= 0; i--) { |
| 876 | struct range *range = &dev_dax->ranges[i].range; |
Dan Williams | 0b07ce8 | 2020-10-13 16:50:45 -0700 | [diff] [blame] | 877 | struct dax_mapping *mapping = dev_dax->ranges[i].mapping; |
Dan Williams | 60e93dc | 2020-10-13 16:50:39 -0700 | [diff] [blame] | 878 | struct resource *adjust = NULL, *res; |
| 879 | resource_size_t shrink; |
| 880 | |
| 881 | shrink = min_t(u64, to_shrink, range_len(range)); |
| 882 | if (shrink >= range_len(range)) { |
Dan Williams | 0b07ce8 | 2020-10-13 16:50:45 -0700 | [diff] [blame] | 883 | devm_release_action(dax_region->dev, |
| 884 | unregister_dax_mapping, &mapping->dev); |
Dan Williams | 6268d7d | 2020-12-18 18:41:41 -0800 | [diff] [blame] | 885 | trim_dev_dax_range(dev_dax); |
Dan Williams | 60e93dc | 2020-10-13 16:50:39 -0700 | [diff] [blame] | 886 | to_shrink -= shrink; |
| 887 | if (!to_shrink) |
| 888 | break; |
| 889 | continue; |
Dan Williams | fcffb6a | 2020-10-13 16:50:24 -0700 | [diff] [blame] | 890 | } |
| 891 | |
Dan Williams | 60e93dc | 2020-10-13 16:50:39 -0700 | [diff] [blame] | 892 | for_each_dax_region_resource(dax_region, res) |
| 893 | if (strcmp(res->name, dev_name(dev)) == 0 |
| 894 | && res->start == range->start) { |
| 895 | adjust = res; |
| 896 | break; |
| 897 | } |
| 898 | |
| 899 | if (dev_WARN_ONCE(dev, !adjust || i != dev_dax->nr_range - 1, |
| 900 | "failed to find matching resource\n")) |
| 901 | return -ENXIO; |
| 902 | return adjust_dev_dax_range(dev_dax, adjust, range_len(range) |
| 903 | - shrink); |
| 904 | } |
| 905 | return 0; |
| 906 | } |
| 907 | |
| 908 | /* |
| 909 | * Only allow adjustments that preserve the relative pgoff of existing |
| 910 | * allocations. I.e. the dev_dax->ranges array is ordered by increasing pgoff. |
| 911 | */ |
| 912 | static bool adjust_ok(struct dev_dax *dev_dax, struct resource *res) |
| 913 | { |
| 914 | struct dev_dax_range *last; |
| 915 | int i; |
| 916 | |
| 917 | if (dev_dax->nr_range == 0) |
| 918 | return false; |
| 919 | if (strcmp(res->name, dev_name(&dev_dax->dev)) != 0) |
| 920 | return false; |
| 921 | last = &dev_dax->ranges[dev_dax->nr_range - 1]; |
| 922 | if (last->range.start != res->start || last->range.end != res->end) |
| 923 | return false; |
| 924 | for (i = 0; i < dev_dax->nr_range - 1; i++) { |
| 925 | struct dev_dax_range *dax_range = &dev_dax->ranges[i]; |
| 926 | |
| 927 | if (dax_range->pgoff > last->pgoff) |
| 928 | return false; |
| 929 | } |
| 930 | |
| 931 | return true; |
Dan Williams | fcffb6a | 2020-10-13 16:50:24 -0700 | [diff] [blame] | 932 | } |
| 933 | |
| 934 | static ssize_t dev_dax_resize(struct dax_region *dax_region, |
| 935 | struct dev_dax *dev_dax, resource_size_t size) |
| 936 | { |
| 937 | resource_size_t avail = dax_region_avail_size(dax_region), to_alloc; |
Dan Williams | 60e93dc | 2020-10-13 16:50:39 -0700 | [diff] [blame] | 938 | resource_size_t dev_size = dev_dax_size(dev_dax); |
Dan Williams | fcffb6a | 2020-10-13 16:50:24 -0700 | [diff] [blame] | 939 | struct resource *region_res = &dax_region->res; |
| 940 | struct device *dev = &dev_dax->dev; |
Dan Williams | fcffb6a | 2020-10-13 16:50:24 -0700 | [diff] [blame] | 941 | struct resource *res, *first; |
Dan Williams | 60e93dc | 2020-10-13 16:50:39 -0700 | [diff] [blame] | 942 | resource_size_t alloc = 0; |
| 943 | int rc; |
Dan Williams | fcffb6a | 2020-10-13 16:50:24 -0700 | [diff] [blame] | 944 | |
| 945 | if (dev->driver) |
| 946 | return -EBUSY; |
| 947 | if (size == dev_size) |
| 948 | return 0; |
| 949 | if (size > dev_size && size - dev_size > avail) |
| 950 | return -ENOSPC; |
| 951 | if (size < dev_size) |
| 952 | return dev_dax_shrink(dev_dax, size); |
| 953 | |
| 954 | to_alloc = size - dev_size; |
Dan Williams | 6d82120 | 2020-10-13 16:50:55 -0700 | [diff] [blame] | 955 | if (dev_WARN_ONCE(dev, !alloc_is_aligned(dev_dax, to_alloc), |
Dan Williams | fcffb6a | 2020-10-13 16:50:24 -0700 | [diff] [blame] | 956 | "resize of %pa misaligned\n", &to_alloc)) |
| 957 | return -ENXIO; |
| 958 | |
| 959 | /* |
| 960 | * Expand the device into the unused portion of the region. This |
| 961 | * may involve adjusting the end of an existing resource, or |
| 962 | * allocating a new resource. |
| 963 | */ |
Dan Williams | 60e93dc | 2020-10-13 16:50:39 -0700 | [diff] [blame] | 964 | retry: |
Dan Williams | fcffb6a | 2020-10-13 16:50:24 -0700 | [diff] [blame] | 965 | first = region_res->child; |
| 966 | if (!first) |
| 967 | return alloc_dev_dax_range(dev_dax, dax_region->res.start, to_alloc); |
Dan Williams | 60e93dc | 2020-10-13 16:50:39 -0700 | [diff] [blame] | 968 | |
| 969 | rc = -ENOSPC; |
| 970 | for (res = first; res; res = res->sibling) { |
Dan Williams | fcffb6a | 2020-10-13 16:50:24 -0700 | [diff] [blame] | 971 | struct resource *next = res->sibling; |
Dan Williams | fcffb6a | 2020-10-13 16:50:24 -0700 | [diff] [blame] | 972 | |
| 973 | /* space at the beginning of the region */ |
Dan Williams | 60e93dc | 2020-10-13 16:50:39 -0700 | [diff] [blame] | 974 | if (res == first && res->start > dax_region->res.start) { |
| 975 | alloc = min(res->start - dax_region->res.start, to_alloc); |
| 976 | rc = alloc_dev_dax_range(dev_dax, dax_region->res.start, alloc); |
| 977 | break; |
| 978 | } |
Dan Williams | fcffb6a | 2020-10-13 16:50:24 -0700 | [diff] [blame] | 979 | |
Dan Williams | 60e93dc | 2020-10-13 16:50:39 -0700 | [diff] [blame] | 980 | alloc = 0; |
Dan Williams | fcffb6a | 2020-10-13 16:50:24 -0700 | [diff] [blame] | 981 | /* space between allocations */ |
| 982 | if (next && next->start > res->end + 1) |
Dan Williams | 60e93dc | 2020-10-13 16:50:39 -0700 | [diff] [blame] | 983 | alloc = min(next->start - (res->end + 1), to_alloc); |
Dan Williams | fcffb6a | 2020-10-13 16:50:24 -0700 | [diff] [blame] | 984 | |
| 985 | /* space at the end of the region */ |
Dan Williams | 60e93dc | 2020-10-13 16:50:39 -0700 | [diff] [blame] | 986 | if (!alloc && !next && res->end < region_res->end) |
| 987 | alloc = min(region_res->end - res->end, to_alloc); |
Dan Williams | fcffb6a | 2020-10-13 16:50:24 -0700 | [diff] [blame] | 988 | |
Dan Williams | 60e93dc | 2020-10-13 16:50:39 -0700 | [diff] [blame] | 989 | if (!alloc) |
| 990 | continue; |
| 991 | |
| 992 | if (adjust_ok(dev_dax, res)) { |
| 993 | rc = adjust_dev_dax_range(dev_dax, res, resource_size(res) + alloc); |
| 994 | break; |
| 995 | } |
| 996 | rc = alloc_dev_dax_range(dev_dax, res->end + 1, alloc); |
| 997 | break; |
Dan Williams | fcffb6a | 2020-10-13 16:50:24 -0700 | [diff] [blame] | 998 | } |
Dan Williams | 60e93dc | 2020-10-13 16:50:39 -0700 | [diff] [blame] | 999 | if (rc) |
| 1000 | return rc; |
| 1001 | to_alloc -= alloc; |
| 1002 | if (to_alloc) |
| 1003 | goto retry; |
| 1004 | return 0; |
Dan Williams | fcffb6a | 2020-10-13 16:50:24 -0700 | [diff] [blame] | 1005 | } |
| 1006 | |
| 1007 | static ssize_t size_store(struct device *dev, struct device_attribute *attr, |
| 1008 | const char *buf, size_t len) |
| 1009 | { |
| 1010 | ssize_t rc; |
| 1011 | unsigned long long val; |
| 1012 | struct dev_dax *dev_dax = to_dev_dax(dev); |
| 1013 | struct dax_region *dax_region = dev_dax->region; |
| 1014 | |
| 1015 | rc = kstrtoull(buf, 0, &val); |
| 1016 | if (rc) |
| 1017 | return rc; |
| 1018 | |
Dan Williams | 6d82120 | 2020-10-13 16:50:55 -0700 | [diff] [blame] | 1019 | if (!alloc_is_aligned(dev_dax, val)) { |
Dan Williams | fcffb6a | 2020-10-13 16:50:24 -0700 | [diff] [blame] | 1020 | dev_dbg(dev, "%s: size: %lld misaligned\n", __func__, val); |
| 1021 | return -EINVAL; |
| 1022 | } |
| 1023 | |
| 1024 | device_lock(dax_region->dev); |
| 1025 | if (!dax_region->dev->driver) { |
| 1026 | device_unlock(dax_region->dev); |
| 1027 | return -ENXIO; |
| 1028 | } |
| 1029 | device_lock(dev); |
| 1030 | rc = dev_dax_resize(dax_region, dev_dax, val); |
| 1031 | device_unlock(dev); |
| 1032 | device_unlock(dax_region->dev); |
| 1033 | |
| 1034 | return rc == 0 ? len : rc; |
| 1035 | } |
| 1036 | static DEVICE_ATTR_RW(size); |
Dan Williams | 51cf784 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 1037 | |
Joao Martins | 8490e2e | 2020-10-13 16:51:06 -0700 | [diff] [blame] | 1038 | static ssize_t range_parse(const char *opt, size_t len, struct range *range) |
| 1039 | { |
| 1040 | unsigned long long addr = 0; |
| 1041 | char *start, *end, *str; |
Shiyang Ruan | 7323fb2 | 2021-01-26 10:13:31 +0800 | [diff] [blame] | 1042 | ssize_t rc = -EINVAL; |
Joao Martins | 8490e2e | 2020-10-13 16:51:06 -0700 | [diff] [blame] | 1043 | |
| 1044 | str = kstrdup(opt, GFP_KERNEL); |
| 1045 | if (!str) |
| 1046 | return rc; |
| 1047 | |
| 1048 | end = str; |
| 1049 | start = strsep(&end, "-"); |
| 1050 | if (!start || !end) |
| 1051 | goto err; |
| 1052 | |
| 1053 | rc = kstrtoull(start, 16, &addr); |
| 1054 | if (rc) |
| 1055 | goto err; |
| 1056 | range->start = addr; |
| 1057 | |
| 1058 | rc = kstrtoull(end, 16, &addr); |
| 1059 | if (rc) |
| 1060 | goto err; |
| 1061 | range->end = addr; |
| 1062 | |
| 1063 | err: |
| 1064 | kfree(str); |
| 1065 | return rc; |
| 1066 | } |
| 1067 | |
| 1068 | static ssize_t mapping_store(struct device *dev, struct device_attribute *attr, |
| 1069 | const char *buf, size_t len) |
| 1070 | { |
| 1071 | struct dev_dax *dev_dax = to_dev_dax(dev); |
| 1072 | struct dax_region *dax_region = dev_dax->region; |
| 1073 | size_t to_alloc; |
| 1074 | struct range r; |
| 1075 | ssize_t rc; |
| 1076 | |
| 1077 | rc = range_parse(buf, len, &r); |
| 1078 | if (rc) |
| 1079 | return rc; |
| 1080 | |
| 1081 | rc = -ENXIO; |
| 1082 | device_lock(dax_region->dev); |
| 1083 | if (!dax_region->dev->driver) { |
| 1084 | device_unlock(dax_region->dev); |
| 1085 | return rc; |
| 1086 | } |
| 1087 | device_lock(dev); |
| 1088 | |
| 1089 | to_alloc = range_len(&r); |
| 1090 | if (alloc_is_aligned(dev_dax, to_alloc)) |
| 1091 | rc = alloc_dev_dax_range(dev_dax, r.start, to_alloc); |
| 1092 | device_unlock(dev); |
| 1093 | device_unlock(dax_region->dev); |
| 1094 | |
| 1095 | return rc == 0 ? len : rc; |
| 1096 | } |
| 1097 | static DEVICE_ATTR_WO(mapping); |
| 1098 | |
Dan Williams | 6d82120 | 2020-10-13 16:50:55 -0700 | [diff] [blame] | 1099 | static ssize_t align_show(struct device *dev, |
| 1100 | struct device_attribute *attr, char *buf) |
| 1101 | { |
| 1102 | struct dev_dax *dev_dax = to_dev_dax(dev); |
| 1103 | |
| 1104 | return sprintf(buf, "%d\n", dev_dax->align); |
| 1105 | } |
| 1106 | |
| 1107 | static ssize_t dev_dax_validate_align(struct dev_dax *dev_dax) |
| 1108 | { |
Dan Williams | 6d82120 | 2020-10-13 16:50:55 -0700 | [diff] [blame] | 1109 | struct device *dev = &dev_dax->dev; |
| 1110 | int i; |
| 1111 | |
Dan Williams | 6d82120 | 2020-10-13 16:50:55 -0700 | [diff] [blame] | 1112 | for (i = 0; i < dev_dax->nr_range; i++) { |
| 1113 | size_t len = range_len(&dev_dax->ranges[i].range); |
| 1114 | |
| 1115 | if (!alloc_is_aligned(dev_dax, len)) { |
| 1116 | dev_dbg(dev, "%s: align %u invalid for range %d\n", |
| 1117 | __func__, dev_dax->align, i); |
| 1118 | return -EINVAL; |
| 1119 | } |
| 1120 | } |
| 1121 | |
| 1122 | return 0; |
| 1123 | } |
| 1124 | |
| 1125 | static ssize_t align_store(struct device *dev, struct device_attribute *attr, |
| 1126 | const char *buf, size_t len) |
| 1127 | { |
| 1128 | struct dev_dax *dev_dax = to_dev_dax(dev); |
| 1129 | struct dax_region *dax_region = dev_dax->region; |
| 1130 | unsigned long val, align_save; |
| 1131 | ssize_t rc; |
| 1132 | |
| 1133 | rc = kstrtoul(buf, 0, &val); |
| 1134 | if (rc) |
| 1135 | return -ENXIO; |
| 1136 | |
| 1137 | if (!dax_align_valid(val)) |
| 1138 | return -EINVAL; |
| 1139 | |
| 1140 | device_lock(dax_region->dev); |
| 1141 | if (!dax_region->dev->driver) { |
| 1142 | device_unlock(dax_region->dev); |
| 1143 | return -ENXIO; |
| 1144 | } |
| 1145 | |
| 1146 | device_lock(dev); |
| 1147 | if (dev->driver) { |
| 1148 | rc = -EBUSY; |
| 1149 | goto out_unlock; |
| 1150 | } |
| 1151 | |
| 1152 | align_save = dev_dax->align; |
| 1153 | dev_dax->align = val; |
| 1154 | rc = dev_dax_validate_align(dev_dax); |
| 1155 | if (rc) |
| 1156 | dev_dax->align = align_save; |
| 1157 | out_unlock: |
| 1158 | device_unlock(dev); |
| 1159 | device_unlock(dax_region->dev); |
| 1160 | return rc == 0 ? len : rc; |
| 1161 | } |
| 1162 | static DEVICE_ATTR_RW(align); |
| 1163 | |
Dan Williams | 21c7576 | 2019-02-20 11:39:36 -0800 | [diff] [blame] | 1164 | static int dev_dax_target_node(struct dev_dax *dev_dax) |
| 1165 | { |
| 1166 | struct dax_region *dax_region = dev_dax->region; |
| 1167 | |
| 1168 | return dax_region->target_node; |
| 1169 | } |
| 1170 | |
| 1171 | static ssize_t target_node_show(struct device *dev, |
| 1172 | struct device_attribute *attr, char *buf) |
| 1173 | { |
| 1174 | struct dev_dax *dev_dax = to_dev_dax(dev); |
| 1175 | |
| 1176 | return sprintf(buf, "%d\n", dev_dax_target_node(dev_dax)); |
| 1177 | } |
| 1178 | static DEVICE_ATTR_RO(target_node); |
| 1179 | |
Vishal Verma | 40cdc60 | 2019-06-20 18:40:38 -0600 | [diff] [blame] | 1180 | static ssize_t resource_show(struct device *dev, |
| 1181 | struct device_attribute *attr, char *buf) |
| 1182 | { |
| 1183 | struct dev_dax *dev_dax = to_dev_dax(dev); |
Dan Williams | 60e93dc | 2020-10-13 16:50:39 -0700 | [diff] [blame] | 1184 | struct dax_region *dax_region = dev_dax->region; |
| 1185 | unsigned long long start; |
Vishal Verma | 40cdc60 | 2019-06-20 18:40:38 -0600 | [diff] [blame] | 1186 | |
Dan Williams | 60e93dc | 2020-10-13 16:50:39 -0700 | [diff] [blame] | 1187 | if (dev_dax->nr_range < 1) |
| 1188 | start = dax_region->res.start; |
| 1189 | else |
| 1190 | start = dev_dax->ranges[0].range.start; |
| 1191 | |
| 1192 | return sprintf(buf, "%#llx\n", start); |
Vishal Verma | 40cdc60 | 2019-06-20 18:40:38 -0600 | [diff] [blame] | 1193 | } |
Dan Williams | 153dd28 | 2019-11-12 17:12:38 -0800 | [diff] [blame] | 1194 | static DEVICE_ATTR(resource, 0400, resource_show, NULL); |
Vishal Verma | 40cdc60 | 2019-06-20 18:40:38 -0600 | [diff] [blame] | 1195 | |
Vishal Verma | c347bd7 | 2019-02-22 16:58:54 -0700 | [diff] [blame] | 1196 | static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, |
| 1197 | char *buf) |
| 1198 | { |
| 1199 | /* |
| 1200 | * We only ever expect to handle device-dax instances, i.e. the |
| 1201 | * @type argument to MODULE_ALIAS_DAX_DEVICE() is always zero |
| 1202 | */ |
| 1203 | return sprintf(buf, DAX_DEVICE_MODALIAS_FMT "\n", 0); |
| 1204 | } |
| 1205 | static DEVICE_ATTR_RO(modalias); |
| 1206 | |
Dan Williams | cb4dd72 | 2019-11-12 17:13:31 -0800 | [diff] [blame] | 1207 | static ssize_t numa_node_show(struct device *dev, |
| 1208 | struct device_attribute *attr, char *buf) |
| 1209 | { |
| 1210 | return sprintf(buf, "%d\n", dev_to_node(dev)); |
| 1211 | } |
| 1212 | static DEVICE_ATTR_RO(numa_node); |
| 1213 | |
Dan Williams | 21c7576 | 2019-02-20 11:39:36 -0800 | [diff] [blame] | 1214 | static umode_t dev_dax_visible(struct kobject *kobj, struct attribute *a, int n) |
| 1215 | { |
| 1216 | struct device *dev = container_of(kobj, struct device, kobj); |
| 1217 | struct dev_dax *dev_dax = to_dev_dax(dev); |
Dan Williams | fcffb6a | 2020-10-13 16:50:24 -0700 | [diff] [blame] | 1218 | struct dax_region *dax_region = dev_dax->region; |
Dan Williams | 21c7576 | 2019-02-20 11:39:36 -0800 | [diff] [blame] | 1219 | |
| 1220 | if (a == &dev_attr_target_node.attr && dev_dax_target_node(dev_dax) < 0) |
| 1221 | return 0; |
Dan Williams | cb4dd72 | 2019-11-12 17:13:31 -0800 | [diff] [blame] | 1222 | if (a == &dev_attr_numa_node.attr && !IS_ENABLED(CONFIG_NUMA)) |
| 1223 | return 0; |
Joao Martins | 8490e2e | 2020-10-13 16:51:06 -0700 | [diff] [blame] | 1224 | if (a == &dev_attr_mapping.attr && is_static(dax_region)) |
| 1225 | return 0; |
Dan Williams | 6d82120 | 2020-10-13 16:50:55 -0700 | [diff] [blame] | 1226 | if ((a == &dev_attr_align.attr || |
| 1227 | a == &dev_attr_size.attr) && is_static(dax_region)) |
Dan Williams | fcffb6a | 2020-10-13 16:50:24 -0700 | [diff] [blame] | 1228 | return 0444; |
Dan Williams | 21c7576 | 2019-02-20 11:39:36 -0800 | [diff] [blame] | 1229 | return a->mode; |
| 1230 | } |
| 1231 | |
Dan Williams | 51cf784 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 1232 | static struct attribute *dev_dax_attributes[] = { |
Vishal Verma | c347bd7 | 2019-02-22 16:58:54 -0700 | [diff] [blame] | 1233 | &dev_attr_modalias.attr, |
Dan Williams | 51cf784 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 1234 | &dev_attr_size.attr, |
Joao Martins | 8490e2e | 2020-10-13 16:51:06 -0700 | [diff] [blame] | 1235 | &dev_attr_mapping.attr, |
Dan Williams | 21c7576 | 2019-02-20 11:39:36 -0800 | [diff] [blame] | 1236 | &dev_attr_target_node.attr, |
Dan Williams | 6d82120 | 2020-10-13 16:50:55 -0700 | [diff] [blame] | 1237 | &dev_attr_align.attr, |
Vishal Verma | 40cdc60 | 2019-06-20 18:40:38 -0600 | [diff] [blame] | 1238 | &dev_attr_resource.attr, |
Dan Williams | cb4dd72 | 2019-11-12 17:13:31 -0800 | [diff] [blame] | 1239 | &dev_attr_numa_node.attr, |
Dan Williams | 51cf784 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 1240 | NULL, |
| 1241 | }; |
| 1242 | |
| 1243 | static const struct attribute_group dev_dax_attribute_group = { |
| 1244 | .attrs = dev_dax_attributes, |
Dan Williams | 21c7576 | 2019-02-20 11:39:36 -0800 | [diff] [blame] | 1245 | .is_visible = dev_dax_visible, |
Dan Williams | 51cf784 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 1246 | }; |
| 1247 | |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 1248 | static const struct attribute_group *dax_attribute_groups[] = { |
Dan Williams | 51cf784 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 1249 | &dev_dax_attribute_group, |
| 1250 | NULL, |
| 1251 | }; |
Dan Williams | 51cf784 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 1252 | |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 1253 | static void dev_dax_release(struct device *dev) |
Dan Williams | 51cf784 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 1254 | { |
| 1255 | struct dev_dax *dev_dax = to_dev_dax(dev); |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 1256 | struct dax_region *dax_region = dev_dax->region; |
Dan Williams | 51cf784 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 1257 | struct dax_device *dax_dev = dev_dax->dax_dev; |
Dan Williams | 51cf784 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 1258 | |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 1259 | put_dax(dax_dev); |
Dan Williams | 0f3da14 | 2020-10-13 16:50:13 -0700 | [diff] [blame] | 1260 | free_dev_dax_id(dev_dax); |
| 1261 | dax_region_put(dax_region); |
Dan Williams | f5516ec | 2020-10-13 16:49:43 -0700 | [diff] [blame] | 1262 | kfree(dev_dax->pgmap); |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 1263 | kfree(dev_dax); |
| 1264 | } |
| 1265 | |
Dan Williams | 770619a | 2019-11-12 17:12:23 -0800 | [diff] [blame] | 1266 | static const struct device_type dev_dax_type = { |
| 1267 | .release = dev_dax_release, |
| 1268 | .groups = dax_attribute_groups, |
| 1269 | }; |
| 1270 | |
Dan Williams | 174ebec | 2020-10-13 16:49:38 -0700 | [diff] [blame] | 1271 | struct dev_dax *devm_create_dev_dax(struct dev_dax_data *data) |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 1272 | { |
Dan Williams | 174ebec | 2020-10-13 16:49:38 -0700 | [diff] [blame] | 1273 | struct dax_region *dax_region = data->dax_region; |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 1274 | struct device *parent = dax_region->dev; |
| 1275 | struct dax_device *dax_dev; |
| 1276 | struct dev_dax *dev_dax; |
| 1277 | struct inode *inode; |
| 1278 | struct device *dev; |
Dan Williams | c2f3011 | 2020-10-13 16:50:03 -0700 | [diff] [blame] | 1279 | int rc; |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 1280 | |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 1281 | dev_dax = kzalloc(sizeof(*dev_dax), GFP_KERNEL); |
| 1282 | if (!dev_dax) |
| 1283 | return ERR_PTR(-ENOMEM); |
| 1284 | |
Dan Williams | 0f3da14 | 2020-10-13 16:50:13 -0700 | [diff] [blame] | 1285 | if (is_static(dax_region)) { |
| 1286 | if (dev_WARN_ONCE(parent, data->id < 0, |
| 1287 | "dynamic id specified to static region\n")) { |
| 1288 | rc = -EINVAL; |
| 1289 | goto err_id; |
| 1290 | } |
| 1291 | |
| 1292 | dev_dax->id = data->id; |
| 1293 | } else { |
| 1294 | if (dev_WARN_ONCE(parent, data->id >= 0, |
| 1295 | "static id specified to dynamic region\n")) { |
| 1296 | rc = -EINVAL; |
| 1297 | goto err_id; |
| 1298 | } |
| 1299 | |
| 1300 | rc = ida_alloc(&dax_region->ida, GFP_KERNEL); |
| 1301 | if (rc < 0) |
| 1302 | goto err_id; |
| 1303 | dev_dax->id = rc; |
| 1304 | } |
| 1305 | |
Dan Williams | c2f3011 | 2020-10-13 16:50:03 -0700 | [diff] [blame] | 1306 | dev_dax->region = dax_region; |
| 1307 | dev = &dev_dax->dev; |
| 1308 | device_initialize(dev); |
Dan Williams | 0f3da14 | 2020-10-13 16:50:13 -0700 | [diff] [blame] | 1309 | dev_set_name(dev, "dax%d.%d", dax_region->id, dev_dax->id); |
Dan Williams | c2f3011 | 2020-10-13 16:50:03 -0700 | [diff] [blame] | 1310 | |
Dan Williams | fcffb6a | 2020-10-13 16:50:24 -0700 | [diff] [blame] | 1311 | rc = alloc_dev_dax_range(dev_dax, dax_region->res.start, data->size); |
Dan Williams | c2f3011 | 2020-10-13 16:50:03 -0700 | [diff] [blame] | 1312 | if (rc) |
| 1313 | goto err_range; |
| 1314 | |
Dan Williams | f5516ec | 2020-10-13 16:49:43 -0700 | [diff] [blame] | 1315 | if (data->pgmap) { |
Dan Williams | c2f3011 | 2020-10-13 16:50:03 -0700 | [diff] [blame] | 1316 | dev_WARN_ONCE(parent, !is_static(dax_region), |
| 1317 | "custom dev_pagemap requires a static dax_region\n"); |
| 1318 | |
Dan Williams | f5516ec | 2020-10-13 16:49:43 -0700 | [diff] [blame] | 1319 | dev_dax->pgmap = kmemdup(data->pgmap, |
| 1320 | sizeof(struct dev_pagemap), GFP_KERNEL); |
Dan Williams | c2f3011 | 2020-10-13 16:50:03 -0700 | [diff] [blame] | 1321 | if (!dev_dax->pgmap) { |
| 1322 | rc = -ENOMEM; |
Dan Williams | f5516ec | 2020-10-13 16:49:43 -0700 | [diff] [blame] | 1323 | goto err_pgmap; |
Dan Williams | c2f3011 | 2020-10-13 16:50:03 -0700 | [diff] [blame] | 1324 | } |
Dan Williams | f5516ec | 2020-10-13 16:49:43 -0700 | [diff] [blame] | 1325 | } |
Dan Williams | 89ec9f2 | 2018-10-29 15:52:42 -0700 | [diff] [blame] | 1326 | |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 1327 | /* |
| 1328 | * No 'host' or dax_operations since there is no access to this |
| 1329 | * device outside of mmap of the resulting character device. |
| 1330 | */ |
Pankaj Gupta | fefc1d9 | 2019-07-05 19:33:24 +0530 | [diff] [blame] | 1331 | dax_dev = alloc_dax(dev_dax, NULL, NULL, DAXDEV_F_SYNC); |
Vivek Goyal | 4e4ced9 | 2020-04-01 12:11:25 -0400 | [diff] [blame] | 1332 | if (IS_ERR(dax_dev)) { |
| 1333 | rc = PTR_ERR(dax_dev); |
Dan Williams | f5516ec | 2020-10-13 16:49:43 -0700 | [diff] [blame] | 1334 | goto err_alloc_dax; |
Vivek Goyal | 4e4ced9 | 2020-04-01 12:11:25 -0400 | [diff] [blame] | 1335 | } |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 1336 | |
| 1337 | /* a device_dax instance is dead while the driver is not attached */ |
| 1338 | kill_dax(dax_dev); |
| 1339 | |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 1340 | dev_dax->dax_dev = dax_dev; |
Dan Williams | 8fc5c73 | 2018-11-09 12:43:07 -0800 | [diff] [blame] | 1341 | dev_dax->target_node = dax_region->target_node; |
Joao Martins | 33cf94d | 2020-10-13 16:50:50 -0700 | [diff] [blame] | 1342 | dev_dax->align = dax_region->align; |
Dan Williams | 0b07ce8 | 2020-10-13 16:50:45 -0700 | [diff] [blame] | 1343 | ida_init(&dev_dax->ida); |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 1344 | kref_get(&dax_region->kref); |
| 1345 | |
| 1346 | inode = dax_inode(dax_dev); |
| 1347 | dev->devt = inode->i_rdev; |
Dan Williams | 174ebec | 2020-10-13 16:49:38 -0700 | [diff] [blame] | 1348 | if (data->subsys == DEV_DAX_BUS) |
Dan Williams | 730926c | 2017-07-16 13:51:53 -0700 | [diff] [blame] | 1349 | dev->bus = &dax_bus_type; |
| 1350 | else |
| 1351 | dev->class = dax_class; |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 1352 | dev->parent = parent; |
Dan Williams | 770619a | 2019-11-12 17:12:23 -0800 | [diff] [blame] | 1353 | dev->type = &dev_dax_type; |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 1354 | |
| 1355 | rc = device_add(dev); |
| 1356 | if (rc) { |
| 1357 | kill_dev_dax(dev_dax); |
| 1358 | put_device(dev); |
| 1359 | return ERR_PTR(rc); |
| 1360 | } |
| 1361 | |
| 1362 | rc = devm_add_action_or_reset(dax_region->dev, unregister_dev_dax, dev); |
| 1363 | if (rc) |
| 1364 | return ERR_PTR(rc); |
| 1365 | |
Dan Williams | 0b07ce8 | 2020-10-13 16:50:45 -0700 | [diff] [blame] | 1366 | /* register mapping device for the initial allocation range */ |
| 1367 | if (dev_dax->nr_range && range_len(&dev_dax->ranges[0].range)) { |
| 1368 | rc = devm_register_dax_mapping(dev_dax, 0); |
| 1369 | if (rc) |
| 1370 | return ERR_PTR(rc); |
| 1371 | } |
| 1372 | |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 1373 | return dev_dax; |
Dan Williams | c2f3011 | 2020-10-13 16:50:03 -0700 | [diff] [blame] | 1374 | |
Dan Williams | f5516ec | 2020-10-13 16:49:43 -0700 | [diff] [blame] | 1375 | err_alloc_dax: |
| 1376 | kfree(dev_dax->pgmap); |
| 1377 | err_pgmap: |
Dan Williams | 60e93dc | 2020-10-13 16:50:39 -0700 | [diff] [blame] | 1378 | free_dev_dax_ranges(dev_dax); |
Dan Williams | c2f3011 | 2020-10-13 16:50:03 -0700 | [diff] [blame] | 1379 | err_range: |
Dan Williams | 0f3da14 | 2020-10-13 16:50:13 -0700 | [diff] [blame] | 1380 | free_dev_dax_id(dev_dax); |
| 1381 | err_id: |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 1382 | kfree(dev_dax); |
| 1383 | |
| 1384 | return ERR_PTR(rc); |
| 1385 | } |
Dan Williams | 174ebec | 2020-10-13 16:49:38 -0700 | [diff] [blame] | 1386 | EXPORT_SYMBOL_GPL(devm_create_dev_dax); |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 1387 | |
Dan Williams | d200781 | 2018-11-07 15:31:23 -0800 | [diff] [blame] | 1388 | static int match_always_count; |
| 1389 | |
| 1390 | int __dax_driver_register(struct dax_device_driver *dax_drv, |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 1391 | struct module *module, const char *mod_name) |
| 1392 | { |
Dan Williams | d200781 | 2018-11-07 15:31:23 -0800 | [diff] [blame] | 1393 | struct device_driver *drv = &dax_drv->drv; |
| 1394 | int rc = 0; |
| 1395 | |
Uwe Kleine-König | 5b8e64f | 2021-02-05 23:28:38 +0100 | [diff] [blame] | 1396 | /* |
| 1397 | * dax_bus_probe() calls dax_drv->probe() unconditionally. |
| 1398 | * So better be safe than sorry and ensure it is provided. |
| 1399 | */ |
| 1400 | if (!dax_drv->probe) |
| 1401 | return -EINVAL; |
| 1402 | |
Dan Williams | d200781 | 2018-11-07 15:31:23 -0800 | [diff] [blame] | 1403 | INIT_LIST_HEAD(&dax_drv->ids); |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 1404 | drv->owner = module; |
| 1405 | drv->name = mod_name; |
| 1406 | drv->mod_name = mod_name; |
| 1407 | drv->bus = &dax_bus_type; |
Dan Williams | d200781 | 2018-11-07 15:31:23 -0800 | [diff] [blame] | 1408 | |
| 1409 | /* there can only be one default driver */ |
| 1410 | mutex_lock(&dax_bus_lock); |
| 1411 | match_always_count += dax_drv->match_always; |
| 1412 | if (match_always_count > 1) { |
| 1413 | match_always_count--; |
| 1414 | WARN_ON(1); |
| 1415 | rc = -EINVAL; |
| 1416 | } |
| 1417 | mutex_unlock(&dax_bus_lock); |
| 1418 | if (rc) |
| 1419 | return rc; |
Uwe Kleine-König | e307bf1 | 2021-02-05 23:28:40 +0100 | [diff] [blame] | 1420 | |
| 1421 | rc = driver_register(drv); |
| 1422 | if (rc && dax_drv->match_always) { |
| 1423 | mutex_lock(&dax_bus_lock); |
| 1424 | match_always_count -= dax_drv->match_always; |
| 1425 | mutex_unlock(&dax_bus_lock); |
| 1426 | } |
| 1427 | |
| 1428 | return rc; |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 1429 | } |
| 1430 | EXPORT_SYMBOL_GPL(__dax_driver_register); |
| 1431 | |
Dan Williams | d200781 | 2018-11-07 15:31:23 -0800 | [diff] [blame] | 1432 | void dax_driver_unregister(struct dax_device_driver *dax_drv) |
| 1433 | { |
Dan Williams | 730926c | 2017-07-16 13:51:53 -0700 | [diff] [blame] | 1434 | struct device_driver *drv = &dax_drv->drv; |
Dan Williams | d200781 | 2018-11-07 15:31:23 -0800 | [diff] [blame] | 1435 | struct dax_id *dax_id, *_id; |
| 1436 | |
| 1437 | mutex_lock(&dax_bus_lock); |
| 1438 | match_always_count -= dax_drv->match_always; |
| 1439 | list_for_each_entry_safe(dax_id, _id, &dax_drv->ids, list) { |
| 1440 | list_del(&dax_id->list); |
| 1441 | kfree(dax_id); |
| 1442 | } |
| 1443 | mutex_unlock(&dax_bus_lock); |
Dan Williams | 730926c | 2017-07-16 13:51:53 -0700 | [diff] [blame] | 1444 | driver_unregister(drv); |
Dan Williams | d200781 | 2018-11-07 15:31:23 -0800 | [diff] [blame] | 1445 | } |
| 1446 | EXPORT_SYMBOL_GPL(dax_driver_unregister); |
| 1447 | |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 1448 | int __init dax_bus_init(void) |
| 1449 | { |
Dan Williams | 730926c | 2017-07-16 13:51:53 -0700 | [diff] [blame] | 1450 | int rc; |
| 1451 | |
| 1452 | if (IS_ENABLED(CONFIG_DEV_DAX_PMEM_COMPAT)) { |
| 1453 | dax_class = class_create(THIS_MODULE, "dax"); |
| 1454 | if (IS_ERR(dax_class)) |
| 1455 | return PTR_ERR(dax_class); |
| 1456 | } |
| 1457 | |
| 1458 | rc = bus_register(&dax_bus_type); |
| 1459 | if (rc) |
| 1460 | class_destroy(dax_class); |
| 1461 | return rc; |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 1462 | } |
| 1463 | |
| 1464 | void __exit dax_bus_exit(void) |
| 1465 | { |
| 1466 | bus_unregister(&dax_bus_type); |
Dan Williams | 730926c | 2017-07-16 13:51:53 -0700 | [diff] [blame] | 1467 | class_destroy(dax_class); |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 1468 | } |