Ian Campbell | df66025 | 2009-02-09 12:05:51 -0800 | [diff] [blame] | 1 | /****************************************************************************** |
| 2 | * Talks to Xen Store to figure out what devices we have (backend half). |
| 3 | * |
| 4 | * Copyright (C) 2005 Rusty Russell, IBM Corporation |
| 5 | * Copyright (C) 2005 Mike Wray, Hewlett-Packard |
| 6 | * Copyright (C) 2005, 2006 XenSource Ltd |
| 7 | * Copyright (C) 2007 Solarflare Communications, Inc. |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or |
| 10 | * modify it under the terms of the GNU General Public License version 2 |
| 11 | * as published by the Free Software Foundation; or, when distributed |
| 12 | * separately from the Linux kernel or incorporated into other |
| 13 | * software packages, subject to the following license: |
| 14 | * |
| 15 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 16 | * of this source file (the "Software"), to deal in the Software without |
| 17 | * restriction, including without limitation the rights to use, copy, modify, |
| 18 | * merge, publish, distribute, sublicense, and/or sell copies of the Software, |
| 19 | * and to permit persons to whom the Software is furnished to do so, subject to |
| 20 | * the following conditions: |
| 21 | * |
| 22 | * The above copyright notice and this permission notice shall be included in |
| 23 | * all copies or substantial portions of the Software. |
| 24 | * |
| 25 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 26 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 27 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 28 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 29 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 30 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 31 | * IN THE SOFTWARE. |
| 32 | */ |
| 33 | |
Joe Perches | 283c097 | 2013-06-28 03:21:41 -0700 | [diff] [blame] | 34 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 35 | |
| 36 | #define DPRINTK(fmt, ...) \ |
| 37 | pr_debug("(%s:%d) " fmt "\n", \ |
| 38 | __func__, __LINE__, ##__VA_ARGS__) |
Ian Campbell | df66025 | 2009-02-09 12:05:51 -0800 | [diff] [blame] | 39 | |
| 40 | #include <linux/kernel.h> |
| 41 | #include <linux/err.h> |
| 42 | #include <linux/string.h> |
| 43 | #include <linux/ctype.h> |
| 44 | #include <linux/fcntl.h> |
| 45 | #include <linux/mm.h> |
| 46 | #include <linux/notifier.h> |
Paul Gortmaker | 63c9744 | 2011-07-10 13:22:07 -0400 | [diff] [blame] | 47 | #include <linux/export.h> |
Ian Campbell | df66025 | 2009-02-09 12:05:51 -0800 | [diff] [blame] | 48 | |
| 49 | #include <asm/page.h> |
| 50 | #include <asm/pgtable.h> |
| 51 | #include <asm/xen/hypervisor.h> |
| 52 | #include <asm/hypervisor.h> |
| 53 | #include <xen/xenbus.h> |
Ian Campbell | df66025 | 2009-02-09 12:05:51 -0800 | [diff] [blame] | 54 | #include <xen/features.h> |
| 55 | |
Juergen Gross | 332f791 | 2017-02-09 14:39:56 +0100 | [diff] [blame] | 56 | #include "xenbus.h" |
Ian Campbell | df66025 | 2009-02-09 12:05:51 -0800 | [diff] [blame] | 57 | |
| 58 | /* backend/<type>/<fe-uuid>/<id> => <type>-<fe-domid>-<id> */ |
| 59 | static int backend_bus_id(char bus_id[XEN_BUS_ID_SIZE], const char *nodename) |
| 60 | { |
| 61 | int domid, err; |
| 62 | const char *devid, *type, *frontend; |
| 63 | unsigned int typelen; |
| 64 | |
| 65 | type = strchr(nodename, '/'); |
| 66 | if (!type) |
| 67 | return -EINVAL; |
| 68 | type++; |
| 69 | typelen = strcspn(type, "/"); |
| 70 | if (!typelen || type[typelen] != '/') |
| 71 | return -EINVAL; |
| 72 | |
| 73 | devid = strrchr(nodename, '/') + 1; |
| 74 | |
| 75 | err = xenbus_gather(XBT_NIL, nodename, "frontend-id", "%i", &domid, |
| 76 | "frontend", NULL, &frontend, |
| 77 | NULL); |
| 78 | if (err) |
| 79 | return err; |
| 80 | if (strlen(frontend) == 0) |
| 81 | err = -ERANGE; |
| 82 | if (!err && !xenbus_exists(XBT_NIL, frontend, "")) |
| 83 | err = -ENOENT; |
| 84 | kfree(frontend); |
| 85 | |
| 86 | if (err) |
| 87 | return err; |
| 88 | |
Ian Campbell | 6bac7f9 | 2010-12-10 14:39:15 +0000 | [diff] [blame] | 89 | if (snprintf(bus_id, XEN_BUS_ID_SIZE, "%.*s-%i-%s", |
| 90 | typelen, type, domid, devid) >= XEN_BUS_ID_SIZE) |
Ian Campbell | df66025 | 2009-02-09 12:05:51 -0800 | [diff] [blame] | 91 | return -ENOSPC; |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | static int xenbus_uevent_backend(struct device *dev, |
| 96 | struct kobj_uevent_env *env) |
| 97 | { |
| 98 | struct xenbus_device *xdev; |
| 99 | struct xenbus_driver *drv; |
| 100 | struct xen_bus_type *bus; |
| 101 | |
| 102 | DPRINTK(""); |
| 103 | |
| 104 | if (dev == NULL) |
| 105 | return -ENODEV; |
| 106 | |
| 107 | xdev = to_xenbus_device(dev); |
| 108 | bus = container_of(xdev->dev.bus, struct xen_bus_type, bus); |
Ian Campbell | df66025 | 2009-02-09 12:05:51 -0800 | [diff] [blame] | 109 | |
Bastian Blank | 149bb2f | 2011-06-29 14:40:08 +0200 | [diff] [blame] | 110 | if (add_uevent_var(env, "MODALIAS=xen-backend:%s", xdev->devicetype)) |
| 111 | return -ENOMEM; |
| 112 | |
Ian Campbell | df66025 | 2009-02-09 12:05:51 -0800 | [diff] [blame] | 113 | /* stuff we want to pass to /sbin/hotplug */ |
| 114 | if (add_uevent_var(env, "XENBUS_TYPE=%s", xdev->devicetype)) |
| 115 | return -ENOMEM; |
| 116 | |
| 117 | if (add_uevent_var(env, "XENBUS_PATH=%s", xdev->nodename)) |
| 118 | return -ENOMEM; |
| 119 | |
| 120 | if (add_uevent_var(env, "XENBUS_BASE_PATH=%s", bus->root)) |
| 121 | return -ENOMEM; |
| 122 | |
| 123 | if (dev->driver) { |
| 124 | drv = to_xenbus_driver(dev->driver); |
| 125 | if (drv && drv->uevent) |
| 126 | return drv->uevent(xdev, env); |
| 127 | } |
| 128 | |
| 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | /* backend/<typename>/<frontend-uuid>/<name> */ |
| 133 | static int xenbus_probe_backend_unit(struct xen_bus_type *bus, |
| 134 | const char *dir, |
| 135 | const char *type, |
| 136 | const char *name) |
| 137 | { |
| 138 | char *nodename; |
| 139 | int err; |
| 140 | |
| 141 | nodename = kasprintf(GFP_KERNEL, "%s/%s", dir, name); |
| 142 | if (!nodename) |
| 143 | return -ENOMEM; |
| 144 | |
| 145 | DPRINTK("%s\n", nodename); |
| 146 | |
| 147 | err = xenbus_probe_node(bus, type, nodename); |
| 148 | kfree(nodename); |
| 149 | return err; |
| 150 | } |
| 151 | |
| 152 | /* backend/<typename>/<frontend-domid> */ |
Ian Campbell | 6bac7f9 | 2010-12-10 14:39:15 +0000 | [diff] [blame] | 153 | static int xenbus_probe_backend(struct xen_bus_type *bus, const char *type, |
| 154 | const char *domid) |
Ian Campbell | df66025 | 2009-02-09 12:05:51 -0800 | [diff] [blame] | 155 | { |
| 156 | char *nodename; |
| 157 | int err = 0; |
| 158 | char **dir; |
| 159 | unsigned int i, dir_n = 0; |
| 160 | |
| 161 | DPRINTK(""); |
| 162 | |
| 163 | nodename = kasprintf(GFP_KERNEL, "%s/%s/%s", bus->root, type, domid); |
| 164 | if (!nodename) |
| 165 | return -ENOMEM; |
| 166 | |
| 167 | dir = xenbus_directory(XBT_NIL, nodename, "", &dir_n); |
| 168 | if (IS_ERR(dir)) { |
| 169 | kfree(nodename); |
| 170 | return PTR_ERR(dir); |
| 171 | } |
| 172 | |
| 173 | for (i = 0; i < dir_n; i++) { |
| 174 | err = xenbus_probe_backend_unit(bus, nodename, type, dir[i]); |
| 175 | if (err) |
| 176 | break; |
| 177 | } |
| 178 | kfree(dir); |
| 179 | kfree(nodename); |
| 180 | return err; |
| 181 | } |
| 182 | |
| 183 | static void frontend_changed(struct xenbus_watch *watch, |
Juergen Gross | 5584ea2 | 2017-02-09 14:39:57 +0100 | [diff] [blame] | 184 | const char *path, const char *token) |
Ian Campbell | df66025 | 2009-02-09 12:05:51 -0800 | [diff] [blame] | 185 | { |
Juergen Gross | 5584ea2 | 2017-02-09 14:39:57 +0100 | [diff] [blame] | 186 | xenbus_otherend_changed(watch, path, token, 0); |
Ian Campbell | df66025 | 2009-02-09 12:05:51 -0800 | [diff] [blame] | 187 | } |
| 188 | |
Ian Campbell | df66025 | 2009-02-09 12:05:51 -0800 | [diff] [blame] | 189 | static struct xen_bus_type xenbus_backend = { |
| 190 | .root = "backend", |
Ian Campbell | 6bac7f9 | 2010-12-10 14:39:15 +0000 | [diff] [blame] | 191 | .levels = 3, /* backend/type/<frontend>/<id> */ |
Ian Campbell | df66025 | 2009-02-09 12:05:51 -0800 | [diff] [blame] | 192 | .get_bus_id = backend_bus_id, |
| 193 | .probe = xenbus_probe_backend, |
| 194 | .otherend_changed = frontend_changed, |
| 195 | .bus = { |
Ian Campbell | 6bac7f9 | 2010-12-10 14:39:15 +0000 | [diff] [blame] | 196 | .name = "xen-backend", |
| 197 | .match = xenbus_match, |
| 198 | .uevent = xenbus_uevent_backend, |
| 199 | .probe = xenbus_dev_probe, |
| 200 | .remove = xenbus_dev_remove, |
Greg Kroah-Hartman | 85dd926 | 2013-10-06 23:55:49 -0700 | [diff] [blame] | 201 | .dev_groups = xenbus_dev_groups, |
Ian Campbell | df66025 | 2009-02-09 12:05:51 -0800 | [diff] [blame] | 202 | }, |
| 203 | }; |
| 204 | |
| 205 | static void backend_changed(struct xenbus_watch *watch, |
Juergen Gross | 5584ea2 | 2017-02-09 14:39:57 +0100 | [diff] [blame] | 206 | const char *path, const char *token) |
Ian Campbell | df66025 | 2009-02-09 12:05:51 -0800 | [diff] [blame] | 207 | { |
| 208 | DPRINTK(""); |
| 209 | |
Juergen Gross | 5584ea2 | 2017-02-09 14:39:57 +0100 | [diff] [blame] | 210 | xenbus_dev_changed(path, &xenbus_backend); |
Ian Campbell | df66025 | 2009-02-09 12:05:51 -0800 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | static struct xenbus_watch be_watch = { |
| 214 | .node = "backend", |
| 215 | .callback = backend_changed, |
| 216 | }; |
| 217 | |
| 218 | static int read_frontend_details(struct xenbus_device *xendev) |
| 219 | { |
| 220 | return xenbus_read_otherend_details(xendev, "frontend-id", "frontend"); |
| 221 | } |
| 222 | |
Ian Campbell | df66025 | 2009-02-09 12:05:51 -0800 | [diff] [blame] | 223 | int xenbus_dev_is_online(struct xenbus_device *dev) |
| 224 | { |
Juergen Gross | 999c9af | 2016-10-31 14:58:42 +0100 | [diff] [blame] | 225 | return !!xenbus_read_unsigned(dev->nodename, "online", 0); |
Ian Campbell | df66025 | 2009-02-09 12:05:51 -0800 | [diff] [blame] | 226 | } |
| 227 | EXPORT_SYMBOL_GPL(xenbus_dev_is_online); |
| 228 | |
David Vrabel | 95afae4 | 2014-09-08 17:30:41 +0100 | [diff] [blame] | 229 | int __xenbus_register_backend(struct xenbus_driver *drv, struct module *owner, |
| 230 | const char *mod_name) |
Ian Campbell | df66025 | 2009-02-09 12:05:51 -0800 | [diff] [blame] | 231 | { |
| 232 | drv->read_otherend_details = read_frontend_details; |
| 233 | |
David Vrabel | 95afae4 | 2014-09-08 17:30:41 +0100 | [diff] [blame] | 234 | return xenbus_register_driver_common(drv, &xenbus_backend, |
| 235 | owner, mod_name); |
Ian Campbell | df66025 | 2009-02-09 12:05:51 -0800 | [diff] [blame] | 236 | } |
David Vrabel | 95afae4 | 2014-09-08 17:30:41 +0100 | [diff] [blame] | 237 | EXPORT_SYMBOL_GPL(__xenbus_register_backend); |
Ian Campbell | df66025 | 2009-02-09 12:05:51 -0800 | [diff] [blame] | 238 | |
| 239 | static int backend_probe_and_watch(struct notifier_block *notifier, |
| 240 | unsigned long event, |
| 241 | void *data) |
| 242 | { |
| 243 | /* Enumerate devices in xenstore and watch for changes. */ |
| 244 | xenbus_probe_devices(&xenbus_backend); |
Ian Campbell | df66025 | 2009-02-09 12:05:51 -0800 | [diff] [blame] | 245 | register_xenbus_watch(&be_watch); |
Jeremy Fitzhardinge | b5d33d0 | 2010-03-29 14:38:35 -0700 | [diff] [blame] | 246 | |
Ian Campbell | df66025 | 2009-02-09 12:05:51 -0800 | [diff] [blame] | 247 | return NOTIFY_DONE; |
| 248 | } |
| 249 | |
SeongJae Park | 8a10567 | 2020-01-27 09:18:08 +0100 | [diff] [blame] | 250 | static int backend_reclaim_memory(struct device *dev, void *data) |
| 251 | { |
| 252 | const struct xenbus_driver *drv; |
SeongJae Park | 060eabe | 2020-01-27 09:18:09 +0100 | [diff] [blame^] | 253 | struct xenbus_device *xdev; |
SeongJae Park | 8a10567 | 2020-01-27 09:18:08 +0100 | [diff] [blame] | 254 | |
| 255 | if (!dev->driver) |
| 256 | return 0; |
| 257 | drv = to_xenbus_driver(dev->driver); |
SeongJae Park | 060eabe | 2020-01-27 09:18:09 +0100 | [diff] [blame^] | 258 | if (drv && drv->reclaim_memory) { |
| 259 | xdev = to_xenbus_device(dev); |
| 260 | if (!spin_trylock(&xdev->reclaim_lock)) |
| 261 | return 0; |
| 262 | drv->reclaim_memory(xdev); |
| 263 | spin_unlock(&xdev->reclaim_lock); |
| 264 | } |
SeongJae Park | 8a10567 | 2020-01-27 09:18:08 +0100 | [diff] [blame] | 265 | return 0; |
| 266 | } |
| 267 | |
| 268 | /* |
| 269 | * Returns 0 always because we are using shrinker to only detect memory |
| 270 | * pressure. |
| 271 | */ |
| 272 | static unsigned long backend_shrink_memory_count(struct shrinker *shrinker, |
| 273 | struct shrink_control *sc) |
| 274 | { |
| 275 | bus_for_each_dev(&xenbus_backend.bus, NULL, NULL, |
| 276 | backend_reclaim_memory); |
| 277 | return 0; |
| 278 | } |
| 279 | |
| 280 | static struct shrinker backend_memory_shrinker = { |
| 281 | .count_objects = backend_shrink_memory_count, |
| 282 | .seeks = DEFAULT_SEEKS, |
| 283 | }; |
| 284 | |
Ian Campbell | df66025 | 2009-02-09 12:05:51 -0800 | [diff] [blame] | 285 | static int __init xenbus_probe_backend_init(void) |
| 286 | { |
| 287 | static struct notifier_block xenstore_notifier = { |
| 288 | .notifier_call = backend_probe_and_watch |
| 289 | }; |
| 290 | int err; |
| 291 | |
| 292 | DPRINTK(""); |
| 293 | |
| 294 | /* Register ourselves with the kernel bus subsystem */ |
| 295 | err = bus_register(&xenbus_backend.bus); |
Jeremy Fitzhardinge | b5d33d0 | 2010-03-29 14:38:35 -0700 | [diff] [blame] | 296 | if (err) |
Ian Campbell | df66025 | 2009-02-09 12:05:51 -0800 | [diff] [blame] | 297 | return err; |
Ian Campbell | df66025 | 2009-02-09 12:05:51 -0800 | [diff] [blame] | 298 | |
| 299 | register_xenstore_notifier(&xenstore_notifier); |
| 300 | |
SeongJae Park | 8a10567 | 2020-01-27 09:18:08 +0100 | [diff] [blame] | 301 | if (register_shrinker(&backend_memory_shrinker)) |
| 302 | pr_warn("shrinker registration failed\n"); |
| 303 | |
Ian Campbell | df66025 | 2009-02-09 12:05:51 -0800 | [diff] [blame] | 304 | return 0; |
| 305 | } |
Jeremy Fitzhardinge | c3676e8 | 2009-03-04 22:32:16 -0800 | [diff] [blame] | 306 | subsys_initcall(xenbus_probe_backend_init); |