Cody P Schafer | 0e93a6e | 2014-03-14 16:00:42 +1100 | [diff] [blame] | 1 | /* |
| 2 | * Hypervisor supplied "24x7" performance counter support |
| 3 | * |
| 4 | * Author: Cody P Schafer <cody@linux.vnet.ibm.com> |
| 5 | * Copyright 2014 IBM Corporation. |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License |
| 9 | * as published by the Free Software Foundation; either version |
| 10 | * 2 of the License, or (at your option) any later version. |
| 11 | */ |
| 12 | |
| 13 | #define pr_fmt(fmt) "hv-24x7: " fmt |
| 14 | |
| 15 | #include <linux/perf_event.h> |
Cody P Schafer | 5c5cd7b | 2015-01-30 13:46:00 -0800 | [diff] [blame] | 16 | #include <linux/rbtree.h> |
Cody P Schafer | 0e93a6e | 2014-03-14 16:00:42 +1100 | [diff] [blame] | 17 | #include <linux/module.h> |
| 18 | #include <linux/slab.h> |
Cody P Schafer | 5c5cd7b | 2015-01-30 13:46:00 -0800 | [diff] [blame] | 19 | #include <linux/vmalloc.h> |
| 20 | |
Cody P Schafer | 0e93a6e | 2014-03-14 16:00:42 +1100 | [diff] [blame] | 21 | #include <asm/firmware.h> |
| 22 | #include <asm/hvcall.h> |
| 23 | #include <asm/io.h> |
Cody P Schafer | 5c5cd7b | 2015-01-30 13:46:00 -0800 | [diff] [blame] | 24 | #include <linux/byteorder/generic.h> |
Cody P Schafer | 0e93a6e | 2014-03-14 16:00:42 +1100 | [diff] [blame] | 25 | |
| 26 | #include "hv-24x7.h" |
| 27 | #include "hv-24x7-catalog.h" |
| 28 | #include "hv-common.h" |
| 29 | |
Cody P Schafer | 5c5cd7b | 2015-01-30 13:46:00 -0800 | [diff] [blame] | 30 | static const char *event_domain_suffix(unsigned domain) |
| 31 | { |
| 32 | switch (domain) { |
| 33 | #define DOMAIN(n, v, x, c) \ |
| 34 | case HV_PERF_DOMAIN_##n: \ |
| 35 | return "__" #n; |
| 36 | #include "hv-24x7-domains.h" |
| 37 | #undef DOMAIN |
| 38 | default: |
| 39 | WARN(1, "unknown domain %d\n", domain); |
| 40 | return "__UNKNOWN_DOMAIN_SUFFIX"; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | static bool domain_is_valid(unsigned domain) |
| 45 | { |
| 46 | switch (domain) { |
| 47 | #define DOMAIN(n, v, x, c) \ |
| 48 | case HV_PERF_DOMAIN_##n: \ |
| 49 | /* fall through */ |
| 50 | #include "hv-24x7-domains.h" |
| 51 | #undef DOMAIN |
| 52 | return true; |
| 53 | default: |
| 54 | return false; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | static bool is_physical_domain(unsigned domain) |
| 59 | { |
| 60 | switch (domain) { |
| 61 | #define DOMAIN(n, v, x, c) \ |
| 62 | case HV_PERF_DOMAIN_##n: \ |
| 63 | return c; |
| 64 | #include "hv-24x7-domains.h" |
| 65 | #undef DOMAIN |
| 66 | default: |
| 67 | return false; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | static bool catalog_entry_domain_is_valid(unsigned domain) |
| 72 | { |
| 73 | return is_physical_domain(domain); |
| 74 | } |
| 75 | |
Cody P Schafer | 0e93a6e | 2014-03-14 16:00:42 +1100 | [diff] [blame] | 76 | /* |
| 77 | * TODO: Merging events: |
| 78 | * - Think of the hcall as an interface to a 4d array of counters: |
| 79 | * - x = domains |
| 80 | * - y = indexes in the domain (core, chip, vcpu, node, etc) |
| 81 | * - z = offset into the counter space |
| 82 | * - w = lpars (guest vms, "logical partitions") |
| 83 | * - A single request is: x,y,y_last,z,z_last,w,w_last |
| 84 | * - this means we can retrieve a rectangle of counters in y,z for a single x. |
| 85 | * |
| 86 | * - Things to consider (ignoring w): |
| 87 | * - input cost_per_request = 16 |
| 88 | * - output cost_per_result(ys,zs) = 8 + 8 * ys + ys * zs |
| 89 | * - limited number of requests per hcall (must fit into 4K bytes) |
| 90 | * - 4k = 16 [buffer header] - 16 [request size] * request_count |
| 91 | * - 255 requests per hcall |
| 92 | * - sometimes it will be more efficient to read extra data and discard |
| 93 | */ |
| 94 | |
| 95 | /* |
| 96 | * Example usage: |
Cody P Schafer | 5c5cd7b | 2015-01-30 13:46:00 -0800 | [diff] [blame] | 97 | * perf stat -e 'hv_24x7/domain=2,offset=8,vcpu=0,lpar=0xffffffff/' |
Cody P Schafer | 0e93a6e | 2014-03-14 16:00:42 +1100 | [diff] [blame] | 98 | */ |
| 99 | |
| 100 | /* u3 0-6, one of HV_24X7_PERF_DOMAIN */ |
| 101 | EVENT_DEFINE_RANGE_FORMAT(domain, config, 0, 3); |
| 102 | /* u16 */ |
Cody P Schafer | 5c5cd7b | 2015-01-30 13:46:00 -0800 | [diff] [blame] | 103 | EVENT_DEFINE_RANGE_FORMAT(core, config, 16, 31); |
| 104 | EVENT_DEFINE_RANGE_FORMAT(vcpu, config, 16, 31); |
Cody P Schafer | 0e93a6e | 2014-03-14 16:00:42 +1100 | [diff] [blame] | 105 | /* u32, see "data_offset" */ |
| 106 | EVENT_DEFINE_RANGE_FORMAT(offset, config, 32, 63); |
| 107 | /* u16 */ |
| 108 | EVENT_DEFINE_RANGE_FORMAT(lpar, config1, 0, 15); |
| 109 | |
| 110 | EVENT_DEFINE_RANGE(reserved1, config, 4, 15); |
| 111 | EVENT_DEFINE_RANGE(reserved2, config1, 16, 63); |
| 112 | EVENT_DEFINE_RANGE(reserved3, config2, 0, 63); |
| 113 | |
| 114 | static struct attribute *format_attrs[] = { |
| 115 | &format_attr_domain.attr, |
| 116 | &format_attr_offset.attr, |
Cody P Schafer | 5c5cd7b | 2015-01-30 13:46:00 -0800 | [diff] [blame] | 117 | &format_attr_core.attr, |
| 118 | &format_attr_vcpu.attr, |
Cody P Schafer | 0e93a6e | 2014-03-14 16:00:42 +1100 | [diff] [blame] | 119 | &format_attr_lpar.attr, |
| 120 | NULL, |
| 121 | }; |
| 122 | |
| 123 | static struct attribute_group format_group = { |
| 124 | .name = "format", |
| 125 | .attrs = format_attrs, |
| 126 | }; |
| 127 | |
Cody P Schafer | 5c5cd7b | 2015-01-30 13:46:00 -0800 | [diff] [blame] | 128 | static struct attribute_group event_group = { |
| 129 | .name = "events", |
| 130 | /* .attrs is set in init */ |
| 131 | }; |
| 132 | |
| 133 | static struct attribute_group event_desc_group = { |
| 134 | .name = "event_descs", |
| 135 | /* .attrs is set in init */ |
| 136 | }; |
| 137 | |
| 138 | static struct attribute_group event_long_desc_group = { |
| 139 | .name = "event_long_descs", |
| 140 | /* .attrs is set in init */ |
| 141 | }; |
| 142 | |
Cody P Schafer | 0e93a6e | 2014-03-14 16:00:42 +1100 | [diff] [blame] | 143 | static struct kmem_cache *hv_page_cache; |
| 144 | |
Sukadev Bhattiprolu | 145264e | 2015-03-30 18:53:38 -0700 | [diff] [blame^] | 145 | /* |
| 146 | * request_buffer and result_buffer are not required to be 4k aligned, |
| 147 | * but are not allowed to cross any 4k boundary. Aligning them to 4k is |
| 148 | * the simplest way to ensure that. |
| 149 | */ |
| 150 | #define H24x7_DATA_BUFFER_SIZE 4096 |
| 151 | DEFINE_PER_CPU(char, hv_24x7_reqb[H24x7_DATA_BUFFER_SIZE]) __aligned(4096); |
| 152 | DEFINE_PER_CPU(char, hv_24x7_resb[H24x7_DATA_BUFFER_SIZE]) __aligned(4096); |
| 153 | |
Cody P Schafer | 5c5cd7b | 2015-01-30 13:46:00 -0800 | [diff] [blame] | 154 | static char *event_name(struct hv_24x7_event_data *ev, int *len) |
| 155 | { |
| 156 | *len = be16_to_cpu(ev->event_name_len) - 2; |
| 157 | return (char *)ev->remainder; |
| 158 | } |
| 159 | |
| 160 | static char *event_desc(struct hv_24x7_event_data *ev, int *len) |
| 161 | { |
| 162 | unsigned nl = be16_to_cpu(ev->event_name_len); |
| 163 | __be16 *desc_len = (__be16 *)(ev->remainder + nl - 2); |
| 164 | *len = be16_to_cpu(*desc_len) - 2; |
| 165 | return (char *)ev->remainder + nl; |
| 166 | } |
| 167 | |
| 168 | static char *event_long_desc(struct hv_24x7_event_data *ev, int *len) |
| 169 | { |
| 170 | unsigned nl = be16_to_cpu(ev->event_name_len); |
| 171 | __be16 *desc_len_ = (__be16 *)(ev->remainder + nl - 2); |
| 172 | unsigned desc_len = be16_to_cpu(*desc_len_); |
| 173 | __be16 *long_desc_len = (__be16 *)(ev->remainder + nl + desc_len - 2); |
| 174 | *len = be16_to_cpu(*long_desc_len) - 2; |
| 175 | return (char *)ev->remainder + nl + desc_len; |
| 176 | } |
| 177 | |
| 178 | static bool event_fixed_portion_is_within(struct hv_24x7_event_data *ev, |
| 179 | void *end) |
| 180 | { |
| 181 | void *start = ev; |
| 182 | |
| 183 | return (start + offsetof(struct hv_24x7_event_data, remainder)) < end; |
| 184 | } |
| 185 | |
| 186 | /* |
| 187 | * Things we don't check: |
| 188 | * - padding for desc, name, and long/detailed desc is required to be '\0' |
| 189 | * bytes. |
| 190 | * |
| 191 | * Return NULL if we pass end, |
| 192 | * Otherwise return the address of the byte just following the event. |
| 193 | */ |
| 194 | static void *event_end(struct hv_24x7_event_data *ev, void *end) |
| 195 | { |
| 196 | void *start = ev; |
| 197 | __be16 *dl_, *ldl_; |
| 198 | unsigned dl, ldl; |
| 199 | unsigned nl = be16_to_cpu(ev->event_name_len); |
| 200 | |
| 201 | if (nl < 2) { |
| 202 | pr_debug("%s: name length too short: %d", __func__, nl); |
| 203 | return NULL; |
| 204 | } |
| 205 | |
| 206 | if (start + nl > end) { |
| 207 | pr_debug("%s: start=%p + nl=%u > end=%p", |
| 208 | __func__, start, nl, end); |
| 209 | return NULL; |
| 210 | } |
| 211 | |
| 212 | dl_ = (__be16 *)(ev->remainder + nl - 2); |
| 213 | if (!IS_ALIGNED((uintptr_t)dl_, 2)) |
| 214 | pr_warn("desc len not aligned %p", dl_); |
| 215 | dl = be16_to_cpu(*dl_); |
| 216 | if (dl < 2) { |
| 217 | pr_debug("%s: desc len too short: %d", __func__, dl); |
| 218 | return NULL; |
| 219 | } |
| 220 | |
| 221 | if (start + nl + dl > end) { |
| 222 | pr_debug("%s: (start=%p + nl=%u + dl=%u)=%p > end=%p", |
| 223 | __func__, start, nl, dl, start + nl + dl, end); |
| 224 | return NULL; |
| 225 | } |
| 226 | |
| 227 | ldl_ = (__be16 *)(ev->remainder + nl + dl - 2); |
| 228 | if (!IS_ALIGNED((uintptr_t)ldl_, 2)) |
| 229 | pr_warn("long desc len not aligned %p", ldl_); |
| 230 | ldl = be16_to_cpu(*ldl_); |
| 231 | if (ldl < 2) { |
| 232 | pr_debug("%s: long desc len too short (ldl=%u)", |
| 233 | __func__, ldl); |
| 234 | return NULL; |
| 235 | } |
| 236 | |
| 237 | if (start + nl + dl + ldl > end) { |
| 238 | pr_debug("%s: start=%p + nl=%u + dl=%u + ldl=%u > end=%p", |
| 239 | __func__, start, nl, dl, ldl, end); |
| 240 | return NULL; |
| 241 | } |
| 242 | |
| 243 | return start + nl + dl + ldl; |
| 244 | } |
| 245 | |
Cody P Schafer | 78d1316 | 2014-04-15 10:10:53 -0700 | [diff] [blame] | 246 | static unsigned long h_get_24x7_catalog_page_(unsigned long phys_4096, |
| 247 | unsigned long version, |
| 248 | unsigned long index) |
| 249 | { |
| 250 | pr_devel("h_get_24x7_catalog_page(0x%lx, %lu, %lu)", |
| 251 | phys_4096, |
| 252 | version, |
| 253 | index); |
| 254 | WARN_ON(!IS_ALIGNED(phys_4096, 4096)); |
| 255 | return plpar_hcall_norets(H_GET_24X7_CATALOG_PAGE, |
| 256 | phys_4096, |
| 257 | version, |
| 258 | index); |
| 259 | } |
| 260 | |
Cody P Schafer | 1ee9fcc | 2014-04-15 10:10:54 -0700 | [diff] [blame] | 261 | static unsigned long h_get_24x7_catalog_page(char page[], |
Cody P Schafer | bbad3e5 | 2014-04-15 10:10:55 -0700 | [diff] [blame] | 262 | u64 version, u32 index) |
Cody P Schafer | 0e93a6e | 2014-03-14 16:00:42 +1100 | [diff] [blame] | 263 | { |
Cody P Schafer | 78d1316 | 2014-04-15 10:10:53 -0700 | [diff] [blame] | 264 | return h_get_24x7_catalog_page_(virt_to_phys(page), |
| 265 | version, index); |
Cody P Schafer | 0e93a6e | 2014-03-14 16:00:42 +1100 | [diff] [blame] | 266 | } |
| 267 | |
Cody P Schafer | 5c5cd7b | 2015-01-30 13:46:00 -0800 | [diff] [blame] | 268 | static unsigned core_domains[] = { |
| 269 | HV_PERF_DOMAIN_PHYS_CORE, |
| 270 | HV_PERF_DOMAIN_VCPU_HOME_CORE, |
| 271 | HV_PERF_DOMAIN_VCPU_HOME_CHIP, |
| 272 | HV_PERF_DOMAIN_VCPU_HOME_NODE, |
| 273 | HV_PERF_DOMAIN_VCPU_REMOTE_NODE, |
| 274 | }; |
| 275 | /* chip event data always yeilds a single event, core yeilds multiple */ |
| 276 | #define MAX_EVENTS_PER_EVENT_DATA ARRAY_SIZE(core_domains) |
| 277 | |
| 278 | static char *event_fmt(struct hv_24x7_event_data *event, unsigned domain) |
| 279 | { |
| 280 | const char *sindex; |
| 281 | const char *lpar; |
| 282 | |
| 283 | if (is_physical_domain(domain)) { |
| 284 | lpar = "0x0"; |
| 285 | sindex = "core"; |
| 286 | } else { |
| 287 | lpar = "?"; |
| 288 | sindex = "vcpu"; |
| 289 | } |
| 290 | |
| 291 | return kasprintf(GFP_KERNEL, |
| 292 | "domain=0x%x,offset=0x%x,%s=?,lpar=%s", |
| 293 | domain, |
| 294 | be16_to_cpu(event->event_counter_offs) + |
| 295 | be16_to_cpu(event->event_group_record_offs), |
| 296 | sindex, |
| 297 | lpar); |
| 298 | } |
| 299 | |
| 300 | /* Avoid trusting fw to NUL terminate strings */ |
| 301 | static char *memdup_to_str(char *maybe_str, int max_len, gfp_t gfp) |
| 302 | { |
| 303 | return kasprintf(gfp, "%.*s", max_len, maybe_str); |
| 304 | } |
| 305 | |
| 306 | static ssize_t device_show_string(struct device *dev, |
| 307 | struct device_attribute *attr, char *buf) |
| 308 | { |
| 309 | struct dev_ext_attribute *d; |
| 310 | |
| 311 | d = container_of(attr, struct dev_ext_attribute, attr); |
| 312 | return sprintf(buf, "%s\n", (char *)d->var); |
| 313 | } |
| 314 | |
| 315 | static struct attribute *device_str_attr_create_(char *name, char *str) |
| 316 | { |
| 317 | struct dev_ext_attribute *attr = kzalloc(sizeof(*attr), GFP_KERNEL); |
| 318 | |
| 319 | if (!attr) |
| 320 | return NULL; |
| 321 | |
| 322 | attr->var = str; |
| 323 | attr->attr.attr.name = name; |
| 324 | attr->attr.attr.mode = 0444; |
| 325 | attr->attr.show = device_show_string; |
| 326 | return &attr->attr.attr; |
| 327 | } |
| 328 | |
| 329 | static struct attribute *device_str_attr_create(char *name, int name_max, |
| 330 | int name_nonce, |
| 331 | char *str, size_t str_max) |
| 332 | { |
| 333 | char *n; |
| 334 | char *s = memdup_to_str(str, str_max, GFP_KERNEL); |
| 335 | struct attribute *a; |
| 336 | |
| 337 | if (!s) |
| 338 | return NULL; |
| 339 | |
| 340 | if (!name_nonce) |
| 341 | n = kasprintf(GFP_KERNEL, "%.*s", name_max, name); |
| 342 | else |
| 343 | n = kasprintf(GFP_KERNEL, "%.*s__%d", name_max, name, |
| 344 | name_nonce); |
| 345 | if (!n) |
| 346 | goto out_s; |
| 347 | |
| 348 | a = device_str_attr_create_(n, s); |
| 349 | if (!a) |
| 350 | goto out_n; |
| 351 | |
| 352 | return a; |
| 353 | out_n: |
| 354 | kfree(n); |
| 355 | out_s: |
| 356 | kfree(s); |
| 357 | return NULL; |
| 358 | } |
| 359 | |
| 360 | static void device_str_attr_destroy(struct attribute *attr) |
| 361 | { |
| 362 | struct dev_ext_attribute *d; |
| 363 | |
| 364 | d = container_of(attr, struct dev_ext_attribute, attr.attr); |
| 365 | kfree(d->var); |
| 366 | kfree(d->attr.attr.name); |
| 367 | kfree(d); |
| 368 | } |
| 369 | |
| 370 | static struct attribute *event_to_attr(unsigned ix, |
| 371 | struct hv_24x7_event_data *event, |
| 372 | unsigned domain, |
| 373 | int nonce) |
| 374 | { |
| 375 | int event_name_len; |
| 376 | char *ev_name, *a_ev_name, *val; |
| 377 | const char *ev_suffix; |
| 378 | struct attribute *attr; |
| 379 | |
| 380 | if (!domain_is_valid(domain)) { |
| 381 | pr_warn("catalog event %u has invalid domain %u\n", |
| 382 | ix, domain); |
| 383 | return NULL; |
| 384 | } |
| 385 | |
| 386 | val = event_fmt(event, domain); |
| 387 | if (!val) |
| 388 | return NULL; |
| 389 | |
| 390 | ev_suffix = event_domain_suffix(domain); |
| 391 | ev_name = event_name(event, &event_name_len); |
| 392 | if (!nonce) |
| 393 | a_ev_name = kasprintf(GFP_KERNEL, "%.*s%s", |
| 394 | (int)event_name_len, ev_name, ev_suffix); |
| 395 | else |
| 396 | a_ev_name = kasprintf(GFP_KERNEL, "%.*s%s__%d", |
| 397 | (int)event_name_len, ev_name, ev_suffix, nonce); |
| 398 | |
| 399 | |
| 400 | if (!a_ev_name) |
| 401 | goto out_val; |
| 402 | |
| 403 | attr = device_str_attr_create_(a_ev_name, val); |
| 404 | if (!attr) |
| 405 | goto out_name; |
| 406 | |
| 407 | return attr; |
| 408 | out_name: |
| 409 | kfree(a_ev_name); |
| 410 | out_val: |
| 411 | kfree(val); |
| 412 | return NULL; |
| 413 | } |
| 414 | |
| 415 | static struct attribute *event_to_desc_attr(struct hv_24x7_event_data *event, |
| 416 | int nonce) |
| 417 | { |
| 418 | int nl, dl; |
| 419 | char *name = event_name(event, &nl); |
| 420 | char *desc = event_desc(event, &dl); |
| 421 | |
| 422 | /* If there isn't a description, don't create the sysfs file */ |
| 423 | if (!dl) |
| 424 | return NULL; |
| 425 | |
| 426 | return device_str_attr_create(name, nl, nonce, desc, dl); |
| 427 | } |
| 428 | |
| 429 | static struct attribute * |
| 430 | event_to_long_desc_attr(struct hv_24x7_event_data *event, int nonce) |
| 431 | { |
| 432 | int nl, dl; |
| 433 | char *name = event_name(event, &nl); |
| 434 | char *desc = event_long_desc(event, &dl); |
| 435 | |
| 436 | /* If there isn't a description, don't create the sysfs file */ |
| 437 | if (!dl) |
| 438 | return NULL; |
| 439 | |
| 440 | return device_str_attr_create(name, nl, nonce, desc, dl); |
| 441 | } |
| 442 | |
| 443 | static ssize_t event_data_to_attrs(unsigned ix, struct attribute **attrs, |
| 444 | struct hv_24x7_event_data *event, int nonce) |
| 445 | { |
| 446 | unsigned i; |
| 447 | |
| 448 | switch (event->domain) { |
| 449 | case HV_PERF_DOMAIN_PHYS_CHIP: |
| 450 | *attrs = event_to_attr(ix, event, event->domain, nonce); |
| 451 | return 1; |
| 452 | case HV_PERF_DOMAIN_PHYS_CORE: |
| 453 | for (i = 0; i < ARRAY_SIZE(core_domains); i++) { |
| 454 | attrs[i] = event_to_attr(ix, event, core_domains[i], |
| 455 | nonce); |
| 456 | if (!attrs[i]) { |
| 457 | pr_warn("catalog event %u: individual attr %u " |
| 458 | "creation failure\n", ix, i); |
| 459 | for (; i; i--) |
| 460 | device_str_attr_destroy(attrs[i - 1]); |
| 461 | return -1; |
| 462 | } |
| 463 | } |
| 464 | return i; |
| 465 | default: |
| 466 | pr_warn("catalog event %u: domain %u is not allowed in the " |
| 467 | "catalog\n", ix, event->domain); |
| 468 | return -1; |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | static size_t event_to_attr_ct(struct hv_24x7_event_data *event) |
| 473 | { |
| 474 | switch (event->domain) { |
| 475 | case HV_PERF_DOMAIN_PHYS_CHIP: |
| 476 | return 1; |
| 477 | case HV_PERF_DOMAIN_PHYS_CORE: |
| 478 | return ARRAY_SIZE(core_domains); |
| 479 | default: |
| 480 | return 0; |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | static unsigned long vmalloc_to_phys(void *v) |
| 485 | { |
| 486 | struct page *p = vmalloc_to_page(v); |
| 487 | |
| 488 | BUG_ON(!p); |
| 489 | return page_to_phys(p) + offset_in_page(v); |
| 490 | } |
| 491 | |
| 492 | /* */ |
| 493 | struct event_uniq { |
| 494 | struct rb_node node; |
| 495 | const char *name; |
| 496 | int nl; |
| 497 | unsigned ct; |
| 498 | unsigned domain; |
| 499 | }; |
| 500 | |
| 501 | static int memord(const void *d1, size_t s1, const void *d2, size_t s2) |
| 502 | { |
| 503 | if (s1 < s2) |
| 504 | return 1; |
| 505 | if (s2 > s1) |
| 506 | return -1; |
| 507 | |
| 508 | return memcmp(d1, d2, s1); |
| 509 | } |
| 510 | |
| 511 | static int ev_uniq_ord(const void *v1, size_t s1, unsigned d1, const void *v2, |
| 512 | size_t s2, unsigned d2) |
| 513 | { |
| 514 | int r = memord(v1, s1, v2, s2); |
| 515 | |
| 516 | if (r) |
| 517 | return r; |
| 518 | if (d1 > d2) |
| 519 | return 1; |
| 520 | if (d2 > d1) |
| 521 | return -1; |
| 522 | return 0; |
| 523 | } |
| 524 | |
| 525 | static int event_uniq_add(struct rb_root *root, const char *name, int nl, |
| 526 | unsigned domain) |
| 527 | { |
| 528 | struct rb_node **new = &(root->rb_node), *parent = NULL; |
| 529 | struct event_uniq *data; |
| 530 | |
| 531 | /* Figure out where to put new node */ |
| 532 | while (*new) { |
| 533 | struct event_uniq *it; |
| 534 | int result; |
| 535 | |
| 536 | it = container_of(*new, struct event_uniq, node); |
| 537 | result = ev_uniq_ord(name, nl, domain, it->name, it->nl, |
| 538 | it->domain); |
| 539 | |
| 540 | parent = *new; |
| 541 | if (result < 0) |
| 542 | new = &((*new)->rb_left); |
| 543 | else if (result > 0) |
| 544 | new = &((*new)->rb_right); |
| 545 | else { |
| 546 | it->ct++; |
| 547 | pr_info("found a duplicate event %.*s, ct=%u\n", nl, |
| 548 | name, it->ct); |
| 549 | return it->ct; |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | data = kmalloc(sizeof(*data), GFP_KERNEL); |
| 554 | if (!data) |
| 555 | return -ENOMEM; |
| 556 | |
| 557 | *data = (struct event_uniq) { |
| 558 | .name = name, |
| 559 | .nl = nl, |
| 560 | .ct = 0, |
| 561 | .domain = domain, |
| 562 | }; |
| 563 | |
| 564 | /* Add new node and rebalance tree. */ |
| 565 | rb_link_node(&data->node, parent, new); |
| 566 | rb_insert_color(&data->node, root); |
| 567 | |
| 568 | /* data->ct */ |
| 569 | return 0; |
| 570 | } |
| 571 | |
| 572 | static void event_uniq_destroy(struct rb_root *root) |
| 573 | { |
| 574 | /* |
| 575 | * the strings we point to are in the giant block of memory filled by |
| 576 | * the catalog, and are freed separately. |
| 577 | */ |
| 578 | struct event_uniq *pos, *n; |
| 579 | |
| 580 | rbtree_postorder_for_each_entry_safe(pos, n, root, node) |
| 581 | kfree(pos); |
| 582 | } |
| 583 | |
| 584 | |
| 585 | /* |
| 586 | * ensure the event structure's sizes are self consistent and don't cause us to |
| 587 | * read outside of the event |
| 588 | * |
| 589 | * On success, return the event length in bytes. |
| 590 | * Otherwise, return -1 (and print as appropriate). |
| 591 | */ |
| 592 | static ssize_t catalog_event_len_validate(struct hv_24x7_event_data *event, |
| 593 | size_t event_idx, |
| 594 | size_t event_data_bytes, |
| 595 | size_t event_entry_count, |
| 596 | size_t offset, void *end) |
| 597 | { |
| 598 | ssize_t ev_len; |
| 599 | void *ev_end, *calc_ev_end; |
| 600 | |
| 601 | if (offset >= event_data_bytes) |
| 602 | return -1; |
| 603 | |
| 604 | if (event_idx >= event_entry_count) { |
| 605 | pr_devel("catalog event data has %zu bytes of padding after last event\n", |
| 606 | event_data_bytes - offset); |
| 607 | return -1; |
| 608 | } |
| 609 | |
| 610 | if (!event_fixed_portion_is_within(event, end)) { |
| 611 | pr_warn("event %zu fixed portion is not within range\n", |
| 612 | event_idx); |
| 613 | return -1; |
| 614 | } |
| 615 | |
| 616 | ev_len = be16_to_cpu(event->length); |
| 617 | |
| 618 | if (ev_len % 16) |
| 619 | pr_info("event %zu has length %zu not divisible by 16: event=%pK\n", |
| 620 | event_idx, ev_len, event); |
| 621 | |
| 622 | ev_end = (__u8 *)event + ev_len; |
| 623 | if (ev_end > end) { |
| 624 | pr_warn("event %zu has .length=%zu, ends after buffer end: ev_end=%pK > end=%pK, offset=%zu\n", |
| 625 | event_idx, ev_len, ev_end, end, |
| 626 | offset); |
| 627 | return -1; |
| 628 | } |
| 629 | |
| 630 | calc_ev_end = event_end(event, end); |
| 631 | if (!calc_ev_end) { |
| 632 | pr_warn("event %zu has a calculated length which exceeds buffer length %zu: event=%pK end=%pK, offset=%zu\n", |
| 633 | event_idx, event_data_bytes, event, end, |
| 634 | offset); |
| 635 | return -1; |
| 636 | } |
| 637 | |
| 638 | if (calc_ev_end > ev_end) { |
| 639 | pr_warn("event %zu exceeds it's own length: event=%pK, end=%pK, offset=%zu, calc_ev_end=%pK\n", |
| 640 | event_idx, event, ev_end, offset, calc_ev_end); |
| 641 | return -1; |
| 642 | } |
| 643 | |
| 644 | return ev_len; |
| 645 | } |
| 646 | |
| 647 | #define MAX_4K (SIZE_MAX / 4096) |
| 648 | |
| 649 | static void create_events_from_catalog(struct attribute ***events_, |
| 650 | struct attribute ***event_descs_, |
| 651 | struct attribute ***event_long_descs_) |
| 652 | { |
| 653 | unsigned long hret; |
| 654 | size_t catalog_len, catalog_page_len, event_entry_count, |
| 655 | event_data_len, event_data_offs, |
| 656 | event_data_bytes, junk_events, event_idx, event_attr_ct, i, |
| 657 | attr_max, event_idx_last, desc_ct, long_desc_ct; |
| 658 | ssize_t ct, ev_len; |
| 659 | uint32_t catalog_version_num; |
| 660 | struct attribute **events, **event_descs, **event_long_descs; |
| 661 | struct hv_24x7_catalog_page_0 *page_0 = |
| 662 | kmem_cache_alloc(hv_page_cache, GFP_KERNEL); |
| 663 | void *page = page_0; |
| 664 | void *event_data, *end; |
| 665 | struct hv_24x7_event_data *event; |
| 666 | struct rb_root ev_uniq = RB_ROOT; |
| 667 | |
| 668 | if (!page) |
| 669 | goto e_out; |
| 670 | |
| 671 | hret = h_get_24x7_catalog_page(page, 0, 0); |
| 672 | if (hret) |
| 673 | goto e_free; |
| 674 | |
| 675 | catalog_version_num = be64_to_cpu(page_0->version); |
| 676 | catalog_page_len = be32_to_cpu(page_0->length); |
| 677 | |
| 678 | if (MAX_4K < catalog_page_len) { |
| 679 | pr_err("invalid page count: %zu\n", catalog_page_len); |
| 680 | goto e_free; |
| 681 | } |
| 682 | |
| 683 | catalog_len = catalog_page_len * 4096; |
| 684 | |
| 685 | event_entry_count = be16_to_cpu(page_0->event_entry_count); |
| 686 | event_data_offs = be16_to_cpu(page_0->event_data_offs); |
| 687 | event_data_len = be16_to_cpu(page_0->event_data_len); |
| 688 | |
| 689 | pr_devel("cv %zu cl %zu eec %zu edo %zu edl %zu\n", |
| 690 | (size_t)catalog_version_num, catalog_len, |
| 691 | event_entry_count, event_data_offs, event_data_len); |
| 692 | |
| 693 | if ((MAX_4K < event_data_len) |
| 694 | || (MAX_4K < event_data_offs) |
| 695 | || (MAX_4K - event_data_offs < event_data_len)) { |
| 696 | pr_err("invalid event data offs %zu and/or len %zu\n", |
| 697 | event_data_offs, event_data_len); |
| 698 | goto e_free; |
| 699 | } |
| 700 | |
| 701 | if ((event_data_offs + event_data_len) > catalog_page_len) { |
| 702 | pr_err("event data %zu-%zu does not fit inside catalog 0-%zu\n", |
| 703 | event_data_offs, |
| 704 | event_data_offs + event_data_len, |
| 705 | catalog_page_len); |
| 706 | goto e_free; |
| 707 | } |
| 708 | |
| 709 | if (SIZE_MAX / MAX_EVENTS_PER_EVENT_DATA - 1 < event_entry_count) { |
| 710 | pr_err("event_entry_count %zu is invalid\n", |
| 711 | event_entry_count); |
| 712 | goto e_free; |
| 713 | } |
| 714 | |
| 715 | event_data_bytes = event_data_len * 4096; |
| 716 | |
| 717 | /* |
| 718 | * event data can span several pages, events can cross between these |
| 719 | * pages. Use vmalloc to make this easier. |
| 720 | */ |
| 721 | event_data = vmalloc(event_data_bytes); |
| 722 | if (!event_data) { |
| 723 | pr_err("could not allocate event data\n"); |
| 724 | goto e_free; |
| 725 | } |
| 726 | |
| 727 | end = event_data + event_data_bytes; |
| 728 | |
| 729 | /* |
| 730 | * using vmalloc_to_phys() like this only works if PAGE_SIZE is |
| 731 | * divisible by 4096 |
| 732 | */ |
| 733 | BUILD_BUG_ON(PAGE_SIZE % 4096); |
| 734 | |
| 735 | for (i = 0; i < event_data_len; i++) { |
| 736 | hret = h_get_24x7_catalog_page_( |
| 737 | vmalloc_to_phys(event_data + i * 4096), |
| 738 | catalog_version_num, |
| 739 | i + event_data_offs); |
| 740 | if (hret) { |
| 741 | pr_err("failed to get event data in page %zu\n", |
| 742 | i + event_data_offs); |
| 743 | goto e_event_data; |
| 744 | } |
| 745 | } |
| 746 | |
| 747 | /* |
| 748 | * scan the catalog to determine the number of attributes we need, and |
| 749 | * verify it at the same time. |
| 750 | */ |
| 751 | for (junk_events = 0, event = event_data, event_idx = 0, attr_max = 0; |
| 752 | ; |
| 753 | event_idx++, event = (void *)event + ev_len) { |
| 754 | size_t offset = (void *)event - (void *)event_data; |
| 755 | char *name; |
| 756 | int nl; |
| 757 | |
| 758 | ev_len = catalog_event_len_validate(event, event_idx, |
| 759 | event_data_bytes, |
| 760 | event_entry_count, |
| 761 | offset, end); |
| 762 | if (ev_len < 0) |
| 763 | break; |
| 764 | |
| 765 | name = event_name(event, &nl); |
| 766 | |
| 767 | if (event->event_group_record_len == 0) { |
| 768 | pr_devel("invalid event %zu (%.*s): group_record_len == 0, skipping\n", |
| 769 | event_idx, nl, name); |
| 770 | junk_events++; |
| 771 | continue; |
| 772 | } |
| 773 | |
| 774 | if (!catalog_entry_domain_is_valid(event->domain)) { |
| 775 | pr_info("event %zu (%.*s) has invalid domain %d\n", |
| 776 | event_idx, nl, name, event->domain); |
| 777 | junk_events++; |
| 778 | continue; |
| 779 | } |
| 780 | |
| 781 | attr_max += event_to_attr_ct(event); |
| 782 | } |
| 783 | |
| 784 | event_idx_last = event_idx; |
| 785 | if (event_idx_last != event_entry_count) |
| 786 | pr_warn("event buffer ended before listed # of events were parsed (got %zu, wanted %zu, junk %zu)\n", |
| 787 | event_idx_last, event_entry_count, junk_events); |
| 788 | |
| 789 | events = kmalloc_array(attr_max + 1, sizeof(*events), GFP_KERNEL); |
| 790 | if (!events) |
| 791 | goto e_event_data; |
| 792 | |
| 793 | event_descs = kmalloc_array(event_idx + 1, sizeof(*event_descs), |
| 794 | GFP_KERNEL); |
| 795 | if (!event_descs) |
| 796 | goto e_event_attrs; |
| 797 | |
| 798 | event_long_descs = kmalloc_array(event_idx + 1, |
| 799 | sizeof(*event_long_descs), GFP_KERNEL); |
| 800 | if (!event_long_descs) |
| 801 | goto e_event_descs; |
| 802 | |
| 803 | /* Iterate over the catalog filling in the attribute vector */ |
| 804 | for (junk_events = 0, event_attr_ct = 0, desc_ct = 0, long_desc_ct = 0, |
| 805 | event = event_data, event_idx = 0; |
| 806 | event_idx < event_idx_last; |
| 807 | event_idx++, ev_len = be16_to_cpu(event->length), |
| 808 | event = (void *)event + ev_len) { |
| 809 | char *name; |
| 810 | int nl; |
| 811 | int nonce; |
| 812 | /* |
| 813 | * these are the only "bad" events that are intermixed and that |
| 814 | * we can ignore without issue. make sure to skip them here |
| 815 | */ |
| 816 | if (event->event_group_record_len == 0) |
| 817 | continue; |
| 818 | if (!catalog_entry_domain_is_valid(event->domain)) |
| 819 | continue; |
| 820 | |
| 821 | name = event_name(event, &nl); |
| 822 | nonce = event_uniq_add(&ev_uniq, name, nl, event->domain); |
| 823 | ct = event_data_to_attrs(event_idx, events + event_attr_ct, |
| 824 | event, nonce); |
| 825 | if (ct <= 0) { |
| 826 | pr_warn("event %zu (%.*s) creation failure, skipping\n", |
| 827 | event_idx, nl, name); |
| 828 | junk_events++; |
| 829 | } else { |
| 830 | event_attr_ct += ct; |
| 831 | event_descs[desc_ct] = event_to_desc_attr(event, nonce); |
| 832 | if (event_descs[desc_ct]) |
| 833 | desc_ct++; |
| 834 | event_long_descs[long_desc_ct] = |
| 835 | event_to_long_desc_attr(event, nonce); |
| 836 | if (event_long_descs[long_desc_ct]) |
| 837 | long_desc_ct++; |
| 838 | } |
| 839 | } |
| 840 | |
| 841 | pr_info("read %zu catalog entries, created %zu event attrs (%zu failures), %zu descs\n", |
| 842 | event_idx, event_attr_ct, junk_events, desc_ct); |
| 843 | |
| 844 | events[event_attr_ct] = NULL; |
| 845 | event_descs[desc_ct] = NULL; |
| 846 | event_long_descs[long_desc_ct] = NULL; |
| 847 | |
| 848 | event_uniq_destroy(&ev_uniq); |
| 849 | vfree(event_data); |
| 850 | kmem_cache_free(hv_page_cache, page); |
| 851 | |
| 852 | *events_ = events; |
| 853 | *event_descs_ = event_descs; |
| 854 | *event_long_descs_ = event_long_descs; |
| 855 | return; |
| 856 | |
| 857 | e_event_descs: |
| 858 | kfree(event_descs); |
| 859 | e_event_attrs: |
| 860 | kfree(events); |
| 861 | e_event_data: |
| 862 | vfree(event_data); |
| 863 | e_free: |
| 864 | kmem_cache_free(hv_page_cache, page); |
| 865 | e_out: |
| 866 | *events_ = NULL; |
| 867 | *event_descs_ = NULL; |
| 868 | *event_long_descs_ = NULL; |
| 869 | } |
| 870 | |
Cody P Schafer | 0e93a6e | 2014-03-14 16:00:42 +1100 | [diff] [blame] | 871 | static ssize_t catalog_read(struct file *filp, struct kobject *kobj, |
| 872 | struct bin_attribute *bin_attr, char *buf, |
| 873 | loff_t offset, size_t count) |
| 874 | { |
| 875 | unsigned long hret; |
| 876 | ssize_t ret = 0; |
sukadev@linux.vnet.ibm.com | 56f12be | 2014-09-30 23:03:18 -0700 | [diff] [blame] | 877 | size_t catalog_len = 0, catalog_page_len = 0; |
Cody P Schafer | 0e93a6e | 2014-03-14 16:00:42 +1100 | [diff] [blame] | 878 | loff_t page_offset = 0; |
sukadev@linux.vnet.ibm.com | 56f12be | 2014-09-30 23:03:18 -0700 | [diff] [blame] | 879 | loff_t offset_in_page; |
| 880 | size_t copy_len; |
Cody P Schafer | bbad3e5 | 2014-04-15 10:10:55 -0700 | [diff] [blame] | 881 | uint64_t catalog_version_num = 0; |
Cody P Schafer | 0e93a6e | 2014-03-14 16:00:42 +1100 | [diff] [blame] | 882 | void *page = kmem_cache_alloc(hv_page_cache, GFP_USER); |
| 883 | struct hv_24x7_catalog_page_0 *page_0 = page; |
| 884 | if (!page) |
| 885 | return -ENOMEM; |
| 886 | |
| 887 | hret = h_get_24x7_catalog_page(page, 0, 0); |
| 888 | if (hret) { |
| 889 | ret = -EIO; |
| 890 | goto e_free; |
| 891 | } |
| 892 | |
Cody P Schafer | bbad3e5 | 2014-04-15 10:10:55 -0700 | [diff] [blame] | 893 | catalog_version_num = be64_to_cpu(page_0->version); |
Cody P Schafer | 0e93a6e | 2014-03-14 16:00:42 +1100 | [diff] [blame] | 894 | catalog_page_len = be32_to_cpu(page_0->length); |
| 895 | catalog_len = catalog_page_len * 4096; |
| 896 | |
| 897 | page_offset = offset / 4096; |
sukadev@linux.vnet.ibm.com | 56f12be | 2014-09-30 23:03:18 -0700 | [diff] [blame] | 898 | offset_in_page = offset % 4096; |
Cody P Schafer | 0e93a6e | 2014-03-14 16:00:42 +1100 | [diff] [blame] | 899 | |
| 900 | if (page_offset >= catalog_page_len) |
| 901 | goto e_free; |
| 902 | |
| 903 | if (page_offset != 0) { |
| 904 | hret = h_get_24x7_catalog_page(page, catalog_version_num, |
| 905 | page_offset); |
| 906 | if (hret) { |
| 907 | ret = -EIO; |
| 908 | goto e_free; |
| 909 | } |
| 910 | } |
| 911 | |
sukadev@linux.vnet.ibm.com | 56f12be | 2014-09-30 23:03:18 -0700 | [diff] [blame] | 912 | copy_len = 4096 - offset_in_page; |
| 913 | if (copy_len > count) |
| 914 | copy_len = count; |
| 915 | |
| 916 | memcpy(buf, page+offset_in_page, copy_len); |
| 917 | ret = copy_len; |
| 918 | |
Cody P Schafer | 0e93a6e | 2014-03-14 16:00:42 +1100 | [diff] [blame] | 919 | e_free: |
| 920 | if (hret) |
Cody P Schafer | bbad3e5 | 2014-04-15 10:10:55 -0700 | [diff] [blame] | 921 | pr_err("h_get_24x7_catalog_page(ver=%lld, page=%lld) failed:" |
| 922 | " rc=%ld\n", |
| 923 | catalog_version_num, page_offset, hret); |
Himangi Saraogi | d658972 | 2014-07-22 23:40:19 +0530 | [diff] [blame] | 924 | kmem_cache_free(hv_page_cache, page); |
Cody P Schafer | 0e93a6e | 2014-03-14 16:00:42 +1100 | [diff] [blame] | 925 | |
sukadev@linux.vnet.ibm.com | 56f12be | 2014-09-30 23:03:18 -0700 | [diff] [blame] | 926 | pr_devel("catalog_read: offset=%lld(%lld) count=%zu " |
| 927 | "catalog_len=%zu(%zu) => %zd\n", offset, page_offset, |
| 928 | count, catalog_len, catalog_page_len, ret); |
Cody P Schafer | 0e93a6e | 2014-03-14 16:00:42 +1100 | [diff] [blame] | 929 | |
| 930 | return ret; |
| 931 | } |
| 932 | |
| 933 | #define PAGE_0_ATTR(_name, _fmt, _expr) \ |
| 934 | static ssize_t _name##_show(struct device *dev, \ |
| 935 | struct device_attribute *dev_attr, \ |
| 936 | char *buf) \ |
| 937 | { \ |
| 938 | unsigned long hret; \ |
| 939 | ssize_t ret = 0; \ |
| 940 | void *page = kmem_cache_alloc(hv_page_cache, GFP_USER); \ |
| 941 | struct hv_24x7_catalog_page_0 *page_0 = page; \ |
| 942 | if (!page) \ |
| 943 | return -ENOMEM; \ |
| 944 | hret = h_get_24x7_catalog_page(page, 0, 0); \ |
| 945 | if (hret) { \ |
| 946 | ret = -EIO; \ |
| 947 | goto e_free; \ |
| 948 | } \ |
| 949 | ret = sprintf(buf, _fmt, _expr); \ |
| 950 | e_free: \ |
Sukadev Bhattiprolu | ec2aef5 | 2014-12-10 01:43:34 -0500 | [diff] [blame] | 951 | kmem_cache_free(hv_page_cache, page); \ |
Cody P Schafer | 0e93a6e | 2014-03-14 16:00:42 +1100 | [diff] [blame] | 952 | return ret; \ |
| 953 | } \ |
| 954 | static DEVICE_ATTR_RO(_name) |
| 955 | |
| 956 | PAGE_0_ATTR(catalog_version, "%lld\n", |
Cody P Schafer | bbad3e5 | 2014-04-15 10:10:55 -0700 | [diff] [blame] | 957 | (unsigned long long)be64_to_cpu(page_0->version)); |
Cody P Schafer | 0e93a6e | 2014-03-14 16:00:42 +1100 | [diff] [blame] | 958 | PAGE_0_ATTR(catalog_len, "%lld\n", |
| 959 | (unsigned long long)be32_to_cpu(page_0->length) * 4096); |
| 960 | static BIN_ATTR_RO(catalog, 0/* real length varies */); |
| 961 | |
| 962 | static struct bin_attribute *if_bin_attrs[] = { |
| 963 | &bin_attr_catalog, |
| 964 | NULL, |
| 965 | }; |
| 966 | |
| 967 | static struct attribute *if_attrs[] = { |
| 968 | &dev_attr_catalog_len.attr, |
| 969 | &dev_attr_catalog_version.attr, |
| 970 | NULL, |
| 971 | }; |
| 972 | |
| 973 | static struct attribute_group if_group = { |
| 974 | .name = "interface", |
| 975 | .bin_attrs = if_bin_attrs, |
| 976 | .attrs = if_attrs, |
| 977 | }; |
| 978 | |
| 979 | static const struct attribute_group *attr_groups[] = { |
| 980 | &format_group, |
Cody P Schafer | 5c5cd7b | 2015-01-30 13:46:00 -0800 | [diff] [blame] | 981 | &event_group, |
| 982 | &event_desc_group, |
| 983 | &event_long_desc_group, |
Cody P Schafer | 0e93a6e | 2014-03-14 16:00:42 +1100 | [diff] [blame] | 984 | &if_group, |
| 985 | NULL, |
| 986 | }; |
| 987 | |
Cody P Schafer | 0e93a6e | 2014-03-14 16:00:42 +1100 | [diff] [blame] | 988 | static unsigned long single_24x7_request(u8 domain, u32 offset, u16 ix, |
Sukadev Bhattiprolu | 145264e | 2015-03-30 18:53:38 -0700 | [diff] [blame^] | 989 | u16 lpar, u64 *count, |
Cody P Schafer | 0e93a6e | 2014-03-14 16:00:42 +1100 | [diff] [blame] | 990 | bool success_expected) |
| 991 | { |
sukadev@linux.vnet.ibm.com | f34b6c7 | 2014-12-10 14:29:13 -0800 | [diff] [blame] | 992 | unsigned long ret; |
Cody P Schafer | 0e93a6e | 2014-03-14 16:00:42 +1100 | [diff] [blame] | 993 | |
Sukadev Bhattiprolu | 145264e | 2015-03-30 18:53:38 -0700 | [diff] [blame^] | 994 | struct hv_24x7_request_buffer *request_buffer; |
| 995 | struct hv_24x7_data_result_buffer *result_buffer; |
| 996 | struct hv_24x7_result *resb; |
| 997 | struct hv_24x7_request *req; |
Cody P Schafer | 48bee8a | 2014-09-30 23:03:17 -0700 | [diff] [blame] | 998 | |
| 999 | BUILD_BUG_ON(sizeof(*request_buffer) > 4096); |
| 1000 | BUILD_BUG_ON(sizeof(*result_buffer) > 4096); |
| 1001 | |
sukadev@linux.vnet.ibm.com | f34b6c7 | 2014-12-10 14:29:13 -0800 | [diff] [blame] | 1002 | request_buffer = (void *)get_cpu_var(hv_24x7_reqb); |
| 1003 | result_buffer = (void *)get_cpu_var(hv_24x7_resb); |
Cody P Schafer | 48bee8a | 2014-09-30 23:03:17 -0700 | [diff] [blame] | 1004 | |
sukadev@linux.vnet.ibm.com | f34b6c7 | 2014-12-10 14:29:13 -0800 | [diff] [blame] | 1005 | memset(request_buffer, 0, 4096); |
| 1006 | memset(result_buffer, 0, 4096); |
Cody P Schafer | 48bee8a | 2014-09-30 23:03:17 -0700 | [diff] [blame] | 1007 | |
Sukadev Bhattiprolu | 145264e | 2015-03-30 18:53:38 -0700 | [diff] [blame^] | 1008 | request_buffer->interface_version = HV_24X7_IF_VERSION_CURRENT; |
| 1009 | request_buffer->num_requests = 1; |
Cody P Schafer | 0e93a6e | 2014-03-14 16:00:42 +1100 | [diff] [blame] | 1010 | |
Sukadev Bhattiprolu | 145264e | 2015-03-30 18:53:38 -0700 | [diff] [blame^] | 1011 | req = &request_buffer->requests[0]; |
| 1012 | |
| 1013 | req->performance_domain = domain; |
| 1014 | req->data_size = cpu_to_be16(8); |
| 1015 | req->data_offset = cpu_to_be32(offset); |
| 1016 | req->starting_lpar_ix = cpu_to_be16(lpar), |
| 1017 | req->max_num_lpars = cpu_to_be16(1); |
| 1018 | req->starting_ix = cpu_to_be16(ix); |
| 1019 | req->max_ix = cpu_to_be16(1); |
| 1020 | |
| 1021 | /* |
| 1022 | * NOTE: Due to variable number of array elements in request and |
| 1023 | * result buffer(s), sizeof() is not reliable. Use the actual |
| 1024 | * allocated buffer size, H24x7_DATA_BUFFER_SIZE. |
| 1025 | */ |
Cody P Schafer | 0e93a6e | 2014-03-14 16:00:42 +1100 | [diff] [blame] | 1026 | ret = plpar_hcall_norets(H_GET_24X7_DATA, |
Sukadev Bhattiprolu | 145264e | 2015-03-30 18:53:38 -0700 | [diff] [blame^] | 1027 | virt_to_phys(request_buffer), H24x7_DATA_BUFFER_SIZE, |
| 1028 | virt_to_phys(result_buffer), H24x7_DATA_BUFFER_SIZE); |
Cody P Schafer | 0e93a6e | 2014-03-14 16:00:42 +1100 | [diff] [blame] | 1029 | |
| 1030 | if (ret) { |
| 1031 | if (success_expected) |
Cody P Schafer | 48bee8a | 2014-09-30 23:03:17 -0700 | [diff] [blame] | 1032 | pr_err_ratelimited("hcall failed: %d %#x %#x %d => " |
| 1033 | "0x%lx (%ld) detail=0x%x failing ix=%x\n", |
| 1034 | domain, offset, ix, lpar, ret, ret, |
Sukadev Bhattiprolu | 145264e | 2015-03-30 18:53:38 -0700 | [diff] [blame^] | 1035 | result_buffer->detailed_rc, |
| 1036 | result_buffer->failing_request_ix); |
sukadev@linux.vnet.ibm.com | f34b6c7 | 2014-12-10 14:29:13 -0800 | [diff] [blame] | 1037 | goto out; |
Cody P Schafer | 0e93a6e | 2014-03-14 16:00:42 +1100 | [diff] [blame] | 1038 | } |
| 1039 | |
Sukadev Bhattiprolu | 145264e | 2015-03-30 18:53:38 -0700 | [diff] [blame^] | 1040 | resb = &result_buffer->results[0]; |
Cody P Schafer | 48bee8a | 2014-09-30 23:03:17 -0700 | [diff] [blame] | 1041 | |
Sukadev Bhattiprolu | 145264e | 2015-03-30 18:53:38 -0700 | [diff] [blame^] | 1042 | *count = be64_to_cpu(resb->elements[0].element_data[0]); |
Cody P Schafer | 48bee8a | 2014-09-30 23:03:17 -0700 | [diff] [blame] | 1043 | out: |
Cody P Schafer | 0e93a6e | 2014-03-14 16:00:42 +1100 | [diff] [blame] | 1044 | return ret; |
| 1045 | } |
| 1046 | |
| 1047 | static unsigned long event_24x7_request(struct perf_event *event, u64 *res, |
| 1048 | bool success_expected) |
| 1049 | { |
Cody P Schafer | 5c5cd7b | 2015-01-30 13:46:00 -0800 | [diff] [blame] | 1050 | u16 idx; |
| 1051 | unsigned domain = event_get_domain(event); |
| 1052 | |
| 1053 | if (is_physical_domain(domain)) |
| 1054 | idx = event_get_core(event); |
| 1055 | else |
| 1056 | idx = event_get_vcpu(event); |
| 1057 | |
Cody P Schafer | 0e93a6e | 2014-03-14 16:00:42 +1100 | [diff] [blame] | 1058 | return single_24x7_request(event_get_domain(event), |
| 1059 | event_get_offset(event), |
Cody P Schafer | 5c5cd7b | 2015-01-30 13:46:00 -0800 | [diff] [blame] | 1060 | idx, |
Cody P Schafer | 0e93a6e | 2014-03-14 16:00:42 +1100 | [diff] [blame] | 1061 | event_get_lpar(event), |
| 1062 | res, |
| 1063 | success_expected); |
| 1064 | } |
| 1065 | |
| 1066 | static int h_24x7_event_init(struct perf_event *event) |
| 1067 | { |
| 1068 | struct hv_perf_caps caps; |
| 1069 | unsigned domain; |
| 1070 | unsigned long hret; |
| 1071 | u64 ct; |
| 1072 | |
| 1073 | /* Not our event */ |
| 1074 | if (event->attr.type != event->pmu->type) |
| 1075 | return -ENOENT; |
| 1076 | |
| 1077 | /* Unused areas must be 0 */ |
| 1078 | if (event_get_reserved1(event) || |
| 1079 | event_get_reserved2(event) || |
| 1080 | event_get_reserved3(event)) { |
| 1081 | pr_devel("reserved set when forbidden 0x%llx(0x%llx) 0x%llx(0x%llx) 0x%llx(0x%llx)\n", |
| 1082 | event->attr.config, |
| 1083 | event_get_reserved1(event), |
| 1084 | event->attr.config1, |
| 1085 | event_get_reserved2(event), |
| 1086 | event->attr.config2, |
| 1087 | event_get_reserved3(event)); |
| 1088 | return -EINVAL; |
| 1089 | } |
| 1090 | |
| 1091 | /* unsupported modes and filters */ |
| 1092 | if (event->attr.exclude_user || |
| 1093 | event->attr.exclude_kernel || |
| 1094 | event->attr.exclude_hv || |
| 1095 | event->attr.exclude_idle || |
| 1096 | event->attr.exclude_host || |
Vince Weaver | cc56d67 | 2014-06-19 14:40:09 -0400 | [diff] [blame] | 1097 | event->attr.exclude_guest) |
Cody P Schafer | 0e93a6e | 2014-03-14 16:00:42 +1100 | [diff] [blame] | 1098 | return -EINVAL; |
| 1099 | |
| 1100 | /* no branch sampling */ |
| 1101 | if (has_branch_stack(event)) |
| 1102 | return -EOPNOTSUPP; |
| 1103 | |
| 1104 | /* offset must be 8 byte aligned */ |
| 1105 | if (event_get_offset(event) % 8) { |
| 1106 | pr_devel("bad alignment\n"); |
| 1107 | return -EINVAL; |
| 1108 | } |
| 1109 | |
| 1110 | /* Domains above 6 are invalid */ |
| 1111 | domain = event_get_domain(event); |
| 1112 | if (domain > 6) { |
| 1113 | pr_devel("invalid domain %d\n", domain); |
| 1114 | return -EINVAL; |
| 1115 | } |
| 1116 | |
| 1117 | hret = hv_perf_caps_get(&caps); |
| 1118 | if (hret) { |
| 1119 | pr_devel("could not get capabilities: rc=%ld\n", hret); |
| 1120 | return -EIO; |
| 1121 | } |
| 1122 | |
Cody P Schafer | 5c5cd7b | 2015-01-30 13:46:00 -0800 | [diff] [blame] | 1123 | /* Physical domains & other lpars require extra capabilities */ |
Cody P Schafer | 0e93a6e | 2014-03-14 16:00:42 +1100 | [diff] [blame] | 1124 | if (!caps.collect_privileged && (is_physical_domain(domain) || |
| 1125 | (event_get_lpar(event) != event_get_lpar_max()))) { |
| 1126 | pr_devel("hv permisions disallow: is_physical_domain:%d, lpar=0x%llx\n", |
| 1127 | is_physical_domain(domain), |
| 1128 | event_get_lpar(event)); |
| 1129 | return -EACCES; |
| 1130 | } |
| 1131 | |
| 1132 | /* see if the event complains */ |
| 1133 | if (event_24x7_request(event, &ct, false)) { |
| 1134 | pr_devel("test hcall failed\n"); |
| 1135 | return -EIO; |
| 1136 | } |
| 1137 | |
| 1138 | return 0; |
| 1139 | } |
| 1140 | |
| 1141 | static u64 h_24x7_get_value(struct perf_event *event) |
| 1142 | { |
| 1143 | unsigned long ret; |
| 1144 | u64 ct; |
| 1145 | ret = event_24x7_request(event, &ct, true); |
| 1146 | if (ret) |
| 1147 | /* We checked this in event init, shouldn't fail here... */ |
| 1148 | return 0; |
| 1149 | |
| 1150 | return ct; |
| 1151 | } |
| 1152 | |
| 1153 | static void h_24x7_event_update(struct perf_event *event) |
| 1154 | { |
| 1155 | s64 prev; |
| 1156 | u64 now; |
| 1157 | now = h_24x7_get_value(event); |
| 1158 | prev = local64_xchg(&event->hw.prev_count, now); |
| 1159 | local64_add(now - prev, &event->count); |
| 1160 | } |
| 1161 | |
| 1162 | static void h_24x7_event_start(struct perf_event *event, int flags) |
| 1163 | { |
| 1164 | if (flags & PERF_EF_RELOAD) |
| 1165 | local64_set(&event->hw.prev_count, h_24x7_get_value(event)); |
| 1166 | } |
| 1167 | |
| 1168 | static void h_24x7_event_stop(struct perf_event *event, int flags) |
| 1169 | { |
| 1170 | h_24x7_event_update(event); |
| 1171 | } |
| 1172 | |
| 1173 | static int h_24x7_event_add(struct perf_event *event, int flags) |
| 1174 | { |
| 1175 | if (flags & PERF_EF_START) |
| 1176 | h_24x7_event_start(event, flags); |
| 1177 | |
| 1178 | return 0; |
| 1179 | } |
| 1180 | |
Cody P Schafer | 0e93a6e | 2014-03-14 16:00:42 +1100 | [diff] [blame] | 1181 | static struct pmu h_24x7_pmu = { |
| 1182 | .task_ctx_nr = perf_invalid_context, |
| 1183 | |
| 1184 | .name = "hv_24x7", |
| 1185 | .attr_groups = attr_groups, |
| 1186 | .event_init = h_24x7_event_init, |
| 1187 | .add = h_24x7_event_add, |
| 1188 | .del = h_24x7_event_stop, |
| 1189 | .start = h_24x7_event_start, |
| 1190 | .stop = h_24x7_event_stop, |
| 1191 | .read = h_24x7_event_update, |
Cody P Schafer | 0e93a6e | 2014-03-14 16:00:42 +1100 | [diff] [blame] | 1192 | }; |
| 1193 | |
| 1194 | static int hv_24x7_init(void) |
| 1195 | { |
| 1196 | int r; |
| 1197 | unsigned long hret; |
| 1198 | struct hv_perf_caps caps; |
| 1199 | |
| 1200 | if (!firmware_has_feature(FW_FEATURE_LPAR)) { |
Cody P Schafer | e98bf00 | 2014-04-15 10:10:50 -0700 | [diff] [blame] | 1201 | pr_debug("not a virtualized system, not enabling\n"); |
Cody P Schafer | 0e93a6e | 2014-03-14 16:00:42 +1100 | [diff] [blame] | 1202 | return -ENODEV; |
| 1203 | } |
| 1204 | |
| 1205 | hret = hv_perf_caps_get(&caps); |
| 1206 | if (hret) { |
Cody P Schafer | e98bf00 | 2014-04-15 10:10:50 -0700 | [diff] [blame] | 1207 | pr_debug("could not obtain capabilities, not enabling, rc=%ld\n", |
Cody P Schafer | 0e93a6e | 2014-03-14 16:00:42 +1100 | [diff] [blame] | 1208 | hret); |
| 1209 | return -ENODEV; |
| 1210 | } |
| 1211 | |
| 1212 | hv_page_cache = kmem_cache_create("hv-page-4096", 4096, 4096, 0, NULL); |
| 1213 | if (!hv_page_cache) |
| 1214 | return -ENOMEM; |
| 1215 | |
Vince Weaver | cc56d67 | 2014-06-19 14:40:09 -0400 | [diff] [blame] | 1216 | /* sampling not supported */ |
| 1217 | h_24x7_pmu.capabilities |= PERF_PMU_CAP_NO_INTERRUPT; |
| 1218 | |
Cody P Schafer | 5c5cd7b | 2015-01-30 13:46:00 -0800 | [diff] [blame] | 1219 | create_events_from_catalog(&event_group.attrs, |
| 1220 | &event_desc_group.attrs, |
| 1221 | &event_long_desc_group.attrs); |
| 1222 | |
Cody P Schafer | 0e93a6e | 2014-03-14 16:00:42 +1100 | [diff] [blame] | 1223 | r = perf_pmu_register(&h_24x7_pmu, h_24x7_pmu.name, -1); |
| 1224 | if (r) |
| 1225 | return r; |
| 1226 | |
| 1227 | return 0; |
| 1228 | } |
| 1229 | |
| 1230 | device_initcall(hv_24x7_init); |