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

cxl/region: Add a 'uuid' attribute

The process of provisioning a region involves triggering the creation of
a new region object, pouring in the configuration, and then binding that
configured object to the region driver to start its operation. For
persistent memory regions the CXL specification mandates that it
identified by a uuid. Add an ABI for userspace to specify a region's
uuid.

Signed-off-by: Ben Widawsky <bwidawsk@kernel.org>
[djbw: simplify locking]
Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Link: https://lore.kernel.org/r/165784334465.1758207.8224025435884752570.stgit@dwillia2-xfh.jf.intel.com
Signed-off-by: Dan Williams <dan.j.williams@intel.com>

authored by

Ben Widawsky and committed by
Dan Williams
dd5ba0eb 779dd20c

+153
+10
Documentation/ABI/testing/sysfs-bus-cxl
··· 293 293 Description: 294 294 (WO) Write a string in the form 'regionZ' to delete that region, 295 295 provided it is currently idle / not bound to a driver. 296 + 297 + 298 + What: /sys/bus/cxl/devices/regionZ/uuid 299 + Date: May, 2022 300 + KernelVersion: v5.20 301 + Contact: linux-cxl@vger.kernel.org 302 + Description: 303 + (RW) Write a unique identifier for the region. This field must 304 + be set for persistent regions and it must not conflict with the 305 + UUID of another region.
+118
drivers/cxl/core/region.c
··· 5 5 #include <linux/device.h> 6 6 #include <linux/module.h> 7 7 #include <linux/slab.h> 8 + #include <linux/uuid.h> 8 9 #include <linux/idr.h> 9 10 #include <cxl.h> 10 11 #include "core.h" ··· 18 17 * Memory ranges, Regions represent the active mapped capacity by the HDM 19 18 * Decoder Capability structures throughout the Host Bridges, Switches, and 20 19 * Endpoints in the topology. 20 + * 21 + * Region configuration has ordering constraints. UUID may be set at any time 22 + * but is only visible for persistent regions. 21 23 */ 22 24 25 + /* 26 + * All changes to the interleave configuration occur with this lock held 27 + * for write. 28 + */ 29 + static DECLARE_RWSEM(cxl_region_rwsem); 30 + 23 31 static struct cxl_region *to_cxl_region(struct device *dev); 32 + 33 + static ssize_t uuid_show(struct device *dev, struct device_attribute *attr, 34 + char *buf) 35 + { 36 + struct cxl_region *cxlr = to_cxl_region(dev); 37 + struct cxl_region_params *p = &cxlr->params; 38 + ssize_t rc; 39 + 40 + rc = down_read_interruptible(&cxl_region_rwsem); 41 + if (rc) 42 + return rc; 43 + rc = sysfs_emit(buf, "%pUb\n", &p->uuid); 44 + up_read(&cxl_region_rwsem); 45 + 46 + return rc; 47 + } 48 + 49 + static int is_dup(struct device *match, void *data) 50 + { 51 + struct cxl_region_params *p; 52 + struct cxl_region *cxlr; 53 + uuid_t *uuid = data; 54 + 55 + if (!is_cxl_region(match)) 56 + return 0; 57 + 58 + lockdep_assert_held(&cxl_region_rwsem); 59 + cxlr = to_cxl_region(match); 60 + p = &cxlr->params; 61 + 62 + if (uuid_equal(&p->uuid, uuid)) { 63 + dev_dbg(match, "already has uuid: %pUb\n", uuid); 64 + return -EBUSY; 65 + } 66 + 67 + return 0; 68 + } 69 + 70 + static ssize_t uuid_store(struct device *dev, struct device_attribute *attr, 71 + const char *buf, size_t len) 72 + { 73 + struct cxl_region *cxlr = to_cxl_region(dev); 74 + struct cxl_region_params *p = &cxlr->params; 75 + uuid_t temp; 76 + ssize_t rc; 77 + 78 + if (len != UUID_STRING_LEN + 1) 79 + return -EINVAL; 80 + 81 + rc = uuid_parse(buf, &temp); 82 + if (rc) 83 + return rc; 84 + 85 + if (uuid_is_null(&temp)) 86 + return -EINVAL; 87 + 88 + rc = down_write_killable(&cxl_region_rwsem); 89 + if (rc) 90 + return rc; 91 + 92 + if (uuid_equal(&p->uuid, &temp)) 93 + goto out; 94 + 95 + rc = -EBUSY; 96 + if (p->state >= CXL_CONFIG_ACTIVE) 97 + goto out; 98 + 99 + rc = bus_for_each_dev(&cxl_bus_type, NULL, &temp, is_dup); 100 + if (rc < 0) 101 + goto out; 102 + 103 + uuid_copy(&p->uuid, &temp); 104 + out: 105 + up_write(&cxl_region_rwsem); 106 + 107 + if (rc) 108 + return rc; 109 + return len; 110 + } 111 + static DEVICE_ATTR_RW(uuid); 112 + 113 + static umode_t cxl_region_visible(struct kobject *kobj, struct attribute *a, 114 + int n) 115 + { 116 + struct device *dev = kobj_to_dev(kobj); 117 + struct cxl_region *cxlr = to_cxl_region(dev); 118 + 119 + if (a == &dev_attr_uuid.attr && cxlr->mode != CXL_DECODER_PMEM) 120 + return 0; 121 + return a->mode; 122 + } 123 + 124 + static struct attribute *cxl_region_attrs[] = { 125 + &dev_attr_uuid.attr, 126 + NULL, 127 + }; 128 + 129 + static const struct attribute_group cxl_region_group = { 130 + .attrs = cxl_region_attrs, 131 + .is_visible = cxl_region_visible, 132 + }; 133 + 134 + static const struct attribute_group *region_groups[] = { 135 + &cxl_base_attribute_group, 136 + &cxl_region_group, 137 + NULL, 138 + }; 24 139 25 140 static void cxl_region_release(struct device *dev) 26 141 { ··· 149 32 static const struct device_type cxl_region_type = { 150 33 .name = "cxl_region", 151 34 .release = cxl_region_release, 35 + .groups = region_groups 152 36 }; 153 37 154 38 bool is_cxl_region(struct device *dev)
+25
drivers/cxl/cxl.h
··· 295 295 struct cxl_switch_decoder cxlsd; 296 296 }; 297 297 298 + /* 299 + * enum cxl_config_state - State machine for region configuration 300 + * @CXL_CONFIG_IDLE: Any sysfs attribute can be written freely 301 + * @CXL_CONFIG_ACTIVE: All targets have been added the region is now 302 + * active 303 + */ 304 + enum cxl_config_state { 305 + CXL_CONFIG_IDLE, 306 + CXL_CONFIG_ACTIVE, 307 + }; 308 + 309 + /** 310 + * struct cxl_region_params - region settings 311 + * @state: allow the driver to lockdown further parameter changes 312 + * @uuid: unique id for persistent regions 313 + * 314 + * State transitions are protected by the cxl_region_rwsem 315 + */ 316 + struct cxl_region_params { 317 + enum cxl_config_state state; 318 + uuid_t uuid; 319 + }; 320 + 298 321 /** 299 322 * struct cxl_region - CXL region 300 323 * @dev: This region's device 301 324 * @id: This region's id. Id is globally unique across all regions 302 325 * @mode: Endpoint decoder allocation / access mode 303 326 * @type: Endpoint decoder target type 327 + * @params: active + config params for the region 304 328 */ 305 329 struct cxl_region { 306 330 struct device dev; 307 331 int id; 308 332 enum cxl_decoder_mode mode; 309 333 enum cxl_decoder_type type; 334 + struct cxl_region_params params; 310 335 }; 311 336 312 337 /**