cregit-Linux how code gets into the kernel

Release 4.14 arch/powerpc/perf/hv-24x7.c

/*
 * Hypervisor supplied "24x7" performance counter support
 *
 * Author: Cody P Schafer <cody@linux.vnet.ibm.com>
 * Copyright 2014 IBM Corporation.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version
 * 2 of the License, or (at your option) any later version.
 */


#define pr_fmt(fmt) "hv-24x7: " fmt

#include <linux/perf_event.h>
#include <linux/rbtree.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/vmalloc.h>

#include <asm/cputhreads.h>
#include <asm/firmware.h>
#include <asm/hvcall.h>
#include <asm/io.h>
#include <linux/byteorder/generic.h>

#include "hv-24x7.h"
#include "hv-24x7-catalog.h"
#include "hv-common.h"

/* Version of the 24x7 hypervisor API that we should use in this machine. */

static int interface_version;

/* Whether we have to aggregate result data for some domains. */

static bool aggregate_result_elements;


static bool domain_is_valid(unsigned domain) { switch (domain) { #define DOMAIN(n, v, x, c) \ case HV_PERF_DOMAIN_##n: \ /* fall through */ #include "hv-24x7-domains.h" #undef DOMAIN return true; default: return false; } }

Contributors

PersonTokensPropCommitsCommitProp
Cody P Schafer44100.00%1100.00%
Total44100.00%1100.00%


static bool is_physical_domain(unsigned domain) { switch (domain) { #define DOMAIN(n, v, x, c) \ case HV_PERF_DOMAIN_##n: \ return c; #include "hv-24x7-domains.h" #undef DOMAIN default: return false; } }

Contributors

PersonTokensPropCommitsCommitProp
Cody P Schafer39100.00%1100.00%
Total39100.00%1100.00%

/* Domains for which more than one result element are returned for each event. */
static bool domain_needs_aggregation(unsigned int domain) { return aggregate_result_elements && (domain == HV_PERF_DOMAIN_PHYS_CORE || (domain >= HV_PERF_DOMAIN_VCPU_HOME_CORE && domain <= HV_PERF_DOMAIN_VCPU_REMOTE_NODE)); }

Contributors

PersonTokensPropCommitsCommitProp
Thiago Jung Bauermann29100.00%1100.00%
Total29100.00%1100.00%


static const char *domain_name(unsigned domain) { if (!domain_is_valid(domain)) return NULL; switch (domain) { case HV_PERF_DOMAIN_PHYS_CHIP: return "Physical Chip"; case HV_PERF_DOMAIN_PHYS_CORE: return "Physical Core"; case HV_PERF_DOMAIN_VCPU_HOME_CORE: return "VCPU Home Core"; case HV_PERF_DOMAIN_VCPU_HOME_CHIP: return "VCPU Home Chip"; case HV_PERF_DOMAIN_VCPU_HOME_NODE: return "VCPU Home Node"; case HV_PERF_DOMAIN_VCPU_REMOTE_NODE: return "VCPU Remote Node"; } WARN_ON_ONCE(domain); return NULL; }

Contributors

PersonTokensPropCommitsCommitProp
Sukadev Bhattiprolu5779.17%150.00%
Cody P Schafer1520.83%150.00%
Total72100.00%2100.00%


static bool catalog_entry_domain_is_valid(unsigned domain) { /* POWER8 doesn't support virtual domains. */ if (interface_version == 1) return is_physical_domain(domain); else return domain_is_valid(domain); }

Contributors

PersonTokensPropCommitsCommitProp
Cody P Schafer1551.72%150.00%
Thiago Jung Bauermann1448.28%150.00%
Total29100.00%2100.00%

/* * TODO: Merging events: * - Think of the hcall as an interface to a 4d array of counters: * - x = domains * - y = indexes in the domain (core, chip, vcpu, node, etc) * - z = offset into the counter space * - w = lpars (guest vms, "logical partitions") * - A single request is: x,y,y_last,z,z_last,w,w_last * - this means we can retrieve a rectangle of counters in y,z for a single x. * * - Things to consider (ignoring w): * - input cost_per_request = 16 * - output cost_per_result(ys,zs) = 8 + 8 * ys + ys * zs * - limited number of requests per hcall (must fit into 4K bytes) * - 4k = 16 [buffer header] - 16 [request size] * request_count * - 255 requests per hcall * - sometimes it will be more efficient to read extra data and discard */ /* * Example usage: * perf stat -e 'hv_24x7/domain=2,offset=8,vcpu=0,lpar=0xffffffff/' */ /* u3 0-6, one of HV_24X7_PERF_DOMAIN */ EVENT_DEFINE_RANGE_FORMAT(domain, config, 0, 3); /* u16 */ EVENT_DEFINE_RANGE_FORMAT(core, config, 16, 31); EVENT_DEFINE_RANGE_FORMAT(chip, config, 16, 31); EVENT_DEFINE_RANGE_FORMAT(vcpu, config, 16, 31); /* u32, see "data_offset" */ EVENT_DEFINE_RANGE_FORMAT(offset, config, 32, 63); /* u16 */ EVENT_DEFINE_RANGE_FORMAT(lpar, config1, 0, 15); EVENT_DEFINE_RANGE(reserved1, config, 4, 15); EVENT_DEFINE_RANGE(reserved2, config1, 16, 63); EVENT_DEFINE_RANGE(reserved3, config2, 0, 63); static struct attribute *format_attrs[] = { &format_attr_domain.attr, &format_attr_offset.attr, &format_attr_core.attr, &format_attr_chip.attr, &format_attr_vcpu.attr, &format_attr_lpar.attr, NULL, }; static struct attribute_group format_group = { .name = "format", .attrs = format_attrs, }; static struct attribute_group event_group = { .name = "events", /* .attrs is set in init */ }; static struct attribute_group event_desc_group = { .name = "event_descs", /* .attrs is set in init */ }; static struct attribute_group event_long_desc_group = { .name = "event_long_descs", /* .attrs is set in init */ }; static struct kmem_cache *hv_page_cache; DEFINE_PER_CPU(int, hv_24x7_txn_flags); DEFINE_PER_CPU(int, hv_24x7_txn_err); struct hv_24x7_hw { struct perf_event *events[255]; }; DEFINE_PER_CPU(struct hv_24x7_hw, hv_24x7_hw); /* * request_buffer and result_buffer are not required to be 4k aligned, * but are not allowed to cross any 4k boundary. Aligning them to 4k is * the simplest way to ensure that. */ #define H24x7_DATA_BUFFER_SIZE 4096 DEFINE_PER_CPU(char, hv_24x7_reqb[H24x7_DATA_BUFFER_SIZE]) __aligned(4096); DEFINE_PER_CPU(char, hv_24x7_resb[H24x7_DATA_BUFFER_SIZE]) __aligned(4096);
static unsigned int max_num_requests(int interface_version) { return (H24x7_DATA_BUFFER_SIZE - sizeof(struct hv_24x7_request_buffer)) / H24x7_REQUEST_SIZE(interface_version); }

Contributors

PersonTokensPropCommitsCommitProp
Thiago Jung Bauermann26100.00%1100.00%
Total26100.00%1100.00%


static char *event_name(struct hv_24x7_event_data *ev, int *len) { *len = be16_to_cpu(ev->event_name_len) - 2; return (char *)ev->remainder; }

Contributors

PersonTokensPropCommitsCommitProp
Cody P Schafer37100.00%1100.00%
Total37100.00%1100.00%


static char *event_desc(struct hv_24x7_event_data *ev, int *len) { unsigned nl = be16_to_cpu(ev->event_name_len); __be16 *desc_len = (__be16 *)(ev->remainder + nl - 2); *len = be16_to_cpu(*desc_len) - 2; return (char *)ev->remainder + nl; }

Contributors

PersonTokensPropCommitsCommitProp
Cody P Schafer66100.00%1100.00%
Total66100.00%1100.00%


static char *event_long_desc(struct hv_24x7_event_data *ev, int *len) { unsigned nl = be16_to_cpu(ev->event_name_len); __be16 *desc_len_ = (__be16 *)(ev->remainder + nl - 2); unsigned desc_len = be16_to_cpu(*desc_len_); __be16 *long_desc_len = (__be16 *)(ev->remainder + nl + desc_len - 2); *len = be16_to_cpu(*long_desc_len) - 2; return (char *)ev->remainder + nl + desc_len; }

Contributors

PersonTokensPropCommitsCommitProp
Cody P Schafer97100.00%1100.00%
Total97100.00%1100.00%


static bool event_fixed_portion_is_within(struct hv_24x7_event_data *ev, void *end) { void *start = ev; return (start + offsetof(struct hv_24x7_event_data, remainder)) < end; }

Contributors

PersonTokensPropCommitsCommitProp
Cody P Schafer36100.00%1100.00%
Total36100.00%1100.00%

/* * Things we don't check: * - padding for desc, name, and long/detailed desc is required to be '\0' * bytes. * * Return NULL if we pass end, * Otherwise return the address of the byte just following the event. */
static void *event_end(struct hv_24x7_event_data *ev, void *end) { void *start = ev; __be16 *dl_, *ldl_; unsigned dl, ldl; unsigned nl = be16_to_cpu(ev->event_name_len); if (nl < 2) { pr_debug("%s: name length too short: %d", __func__, nl); return NULL; } if (start + nl > end) { pr_debug("%s: start=%p + nl=%u > end=%p", __func__, start, nl, end); return NULL; } dl_ = (__be16 *)(ev->remainder + nl - 2); if (!IS_ALIGNED((uintptr_t)dl_, 2)) pr_warn("desc len not aligned %p", dl_); dl = be16_to_cpu(*dl_); if (dl < 2) { pr_debug("%s: desc len too short: %d", __func__, dl); return NULL; } if (start + nl + dl > end) { pr_debug("%s: (start=%p + nl=%u + dl=%u)=%p > end=%p", __func__, start, nl, dl, start + nl + dl, end); return NULL; } ldl_ = (__be16 *)(ev->remainder + nl + dl - 2); if (!IS_ALIGNED((uintptr_t)ldl_, 2)) pr_warn("long desc len not aligned %p", ldl_); ldl = be16_to_cpu(*ldl_); if (ldl < 2) { pr_debug("%s: long desc len too short (ldl=%u)", __func__, ldl); return NULL; } if (start + nl + dl + ldl > end) { pr_debug("%s: start=%p + nl=%u + dl=%u + ldl=%u > end=%p", __func__, start, nl, dl, ldl, end); return NULL; } return start + nl + dl + ldl; }

Contributors

PersonTokensPropCommitsCommitProp
Cody P Schafer299100.00%1100.00%
Total299100.00%1100.00%


static long h_get_24x7_catalog_page_(unsigned long phys_4096, unsigned long version, unsigned long index) { pr_devel("h_get_24x7_catalog_page(0x%lx, %lu, %lu)", phys_4096, version, index); WARN_ON(!IS_ALIGNED(phys_4096, 4096)); return plpar_hcall_norets(H_GET_24X7_CATALOG_PAGE, phys_4096, version, index); }

Contributors

PersonTokensPropCommitsCommitProp
Cody P Schafer52100.00%1100.00%
Total52100.00%1100.00%


static long h_get_24x7_catalog_page(char page[], u64 version, u32 index) { return h_get_24x7_catalog_page_(virt_to_phys(page), version, index); }

Contributors

PersonTokensPropCommitsCommitProp
Cody P Schafer29100.00%1100.00%
Total29100.00%1100.00%

/* * Each event we find in the catalog, will have a sysfs entry. Format the * data for this sysfs entry based on the event's domain. * * Events belonging to the Chip domain can only be monitored in that domain. * i.e the domain for these events is a fixed/knwon value. * * Events belonging to the Core domain can be monitored either in the physical * core or in one of the virtual CPU domains. So the domain value for these * events must be specified by the user (i.e is a required parameter). Format * the Core events with 'domain=?' so the perf-tool can error check required * parameters. * * NOTE: For the Core domain events, rather than making domain a required * parameter we could default it to PHYS_CORE and allowe users to * override the domain to one of the VCPU domains. * * However, this can make the interface a little inconsistent. * * If we set domain=2 (PHYS_CHIP) and allow user to override this field * the user may be tempted to also modify the "offset=x" field in which * can lead to confusing usage. Consider the HPM_PCYC (offset=0x18) and * HPM_INST (offset=0x20) events. With: * * perf stat -e hv_24x7/HPM_PCYC,offset=0x20/ * * we end up monitoring HPM_INST, while the command line has HPM_PCYC. * * By not assigning a default value to the domain for the Core events, * we can have simple guidelines: * * - Specifying values for parameters with "=?" is required. * * - Specifying (i.e overriding) values for other parameters * is undefined. */
static char *event_fmt(struct hv_24x7_event_data *event, unsigned domain) { const char *sindex; const char *lpar; const char *domain_str; char buf[8]; switch (domain) { case HV_PERF_DOMAIN_PHYS_CHIP: snprintf(buf, sizeof(buf), "%d", domain); domain_str = buf; lpar = "0x0"; sindex = "chip"; break; case HV_PERF_DOMAIN_PHYS_CORE: domain_str = "?"; lpar = "0x0"; sindex = "core"; break; default: domain_str = "?"; lpar = "?"; sindex = "vcpu"; } return kasprintf(GFP_KERNEL, "domain=%s,offset=0x%x,%s=?,lpar=%s", domain_str, be16_to_cpu(event->event_counter_offs) + be16_to_cpu(event->event_group_record_offs), sindex, lpar); }

Contributors

PersonTokensPropCommitsCommitProp
Cody P Schafer7255.81%133.33%
Sukadev Bhattiprolu5744.19%266.67%
Total129100.00%3100.00%

/* Avoid trusting fw to NUL terminate strings */
static char *memdup_to_str(char *maybe_str, int max_len, gfp_t gfp) { return kasprintf(gfp, "%.*s", max_len, maybe_str); }

Contributors

PersonTokensPropCommitsCommitProp
Cody P Schafer29100.00%1100.00%
Total29100.00%1100.00%


static ssize_t device_show_string(struct device *dev, struct device_attribute *attr, char *buf) { struct dev_ext_attribute *d; d = container_of(attr, struct dev_ext_attribute, attr); return sprintf(buf, "%s\n", (char *)d->var); }

Contributors

PersonTokensPropCommitsCommitProp
Cody P Schafer53100.00%1100.00%
Total53100.00%1100.00%


static struct attribute *device_str_attr_create_(char *name, char *str) { struct dev_ext_attribute *attr = kzalloc(sizeof(*attr), GFP_KERNEL); if (!attr) return NULL; sysfs_attr_init(&attr->attr.attr); attr->var = str; attr->attr.attr.name = name; attr->attr.attr.mode = 0444; attr->attr.show = device_show_string; return &attr->attr.attr; }

Contributors

PersonTokensPropCommitsCommitProp
Cody P Schafer8289.13%150.00%
Sukadev Bhattiprolu1010.87%150.00%
Total92100.00%2100.00%

/* * Allocate and initialize strings representing event attributes. * * NOTE: The strings allocated here are never destroyed and continue to * exist till shutdown. This is to allow us to create as many events * from the catalog as possible, even if we encounter errors with some. * In case of changes to error paths in future, these may need to be * freed by the caller. */
static struct attribute *device_str_attr_create(char *name, int name_max, int name_nonce, char *str, size_t str_max) { char *n; char *s = memdup_to_str(str, str_max, GFP_KERNEL); struct attribute *a; if (!s) return NULL; if (!name_nonce) n = kasprintf(GFP_KERNEL, "%.*s", name_max, name); else n = kasprintf(GFP_KERNEL, "%.*s__%d", name_max, name, name_nonce); if (!n) goto out_s; a = device_str_attr_create_(n, s); if (!a) goto out_n; return a; out_n: kfree(n); out_s: kfree(s); return NULL; }

Contributors

PersonTokensPropCommitsCommitProp
Cody P Schafer134100.00%1100.00%
Total134100.00%1100.00%


static struct attribute *event_to_attr(unsigned ix, struct hv_24x7_event_data *event, unsigned domain, int nonce) { int event_name_len; char *ev_name, *a_ev_name, *val; struct attribute *attr; if (!domain_is_valid(domain)) { pr_warn("catalog event %u has invalid domain %u\n", ix, domain); return NULL; } val = event_fmt(event, domain); if (!val) return NULL; ev_name = event_name(event, &event_name_len); if (!nonce) a_ev_name = kasprintf(GFP_KERNEL, "%.*s", (int)event_name_len, ev_name); else a_ev_name = kasprintf(GFP_KERNEL, "%.*s__%d", (int)event_name_len, ev_name, nonce); if (!a_ev_name) goto out_val; attr = device_str_attr_create_(a_ev_name, val); if (!attr) goto out_name; return attr; out_name: kfree(a_ev_name); out_val: kfree(val); return NULL; }

Contributors

PersonTokensPropCommitsCommitProp
Cody P Schafer17298.85%150.00%
Sukadev Bhattiprolu21.15%150.00%
Total174100.00%2100.00%


static struct attribute *event_to_desc_attr(struct hv_24x7_event_data *event, int nonce) { int nl, dl; char *name = event_name(event, &nl); char *desc = event_desc(event, &dl); /* If there isn't a description, don't create the sysfs file */ if (!dl) return NULL; return device_str_attr_create(name, nl, nonce, desc, dl); }

Contributors

PersonTokensPropCommitsCommitProp
Cody P Schafer68100.00%1100.00%
Total68100.00%1100.00%


static struct attribute * event_to_long_desc_attr(struct hv_24x7_event_data *event, int nonce) { int nl, dl; char *name = event_name(event, &nl); char *desc = event_long_desc(event, &dl); /* If there isn't a description, don't create the sysfs file */ if (!dl) return NULL; return device_str_attr_create(name, nl, nonce, desc, dl); }

Contributors

PersonTokensPropCommitsCommitProp
Cody P Schafer68100.00%1100.00%
Total68100.00%1100.00%


static int event_data_to_attrs(unsigned ix, struct attribute **attrs, struct hv_24x7_event_data *event, int nonce) { *attrs = event_to_attr(ix, event, event->domain, nonce); if (!*attrs) return -1; return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Cody P Schafer5096.15%150.00%
Sukadev Bhattiprolu23.85%150.00%
Total52100.00%2100.00%

/* */ struct event_uniq { struct rb_node node; const char *name; int nl; unsigned ct; unsigned domain; };
static int memord(const void *d1, size_t s1, const void *d2, size_t s2) { if (s1 < s2) return 1; if (s2 > s1) return -1; return memcmp(d1, d2, s1); }

Contributors

PersonTokensPropCommitsCommitProp
Cody P Schafer51100.00%1100.00%
Total51100.00%1100.00%


static int ev_uniq_ord(const void *v1, size_t s1, unsigned d1, const void *v2, size_t s2, unsigned d2) { int r = memord(v1, s1, v2, s2); if (r) return r; if (d1 > d2) return 1; if (d2 > d1) return -1; return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Cody P Schafer71100.00%1100.00%
Total71100.00%1100.00%


static int event_uniq_add(struct rb_root *root, const char *name, int nl, unsigned domain) { struct rb_node **new = &(root->rb_node), *parent = NULL; struct event_uniq *data; /* Figure out where to put new node */ while (*new) { struct event_uniq *it; int result; it = container_of(*new, struct event_uniq, node); result = ev_uniq_ord(name, nl, domain, it->name, it->nl, it->domain); parent = *new; if (result < 0) new = &((*new)->rb_left); else if (result > 0) new = &((*new)->rb_right); else { it->ct++; pr_info("found a duplicate event %.*s, ct=%u\n", nl, name, it->ct); return it->ct; } } data = kmalloc(sizeof(*data), GFP_KERNEL); if (!data) return -ENOMEM; *data = (struct event_uniq) { .name = name, .nl = nl, .ct = 0, .domain = domain, }; /* Add new node and rebalance tree. */ rb_link_node(&data->node, parent, new); rb_insert_color(&data->node, root); /* data->ct */ return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Cody P Schafer243100.00%1100.00%
Total243100.00%1100.00%


static void event_uniq_destroy(struct rb_root *root) { /* * the strings we point to are in the giant block of memory filled by * the catalog, and are freed separately. */ struct event_uniq *pos, *n; rbtree_postorder_for_each_entry_safe(pos, n, root, node) kfree(pos); }

Contributors

PersonTokensPropCommitsCommitProp
Cody P Schafer35100.00%1100.00%
Total35100.00%1100.00%

/* * ensure the event structure's sizes are self consistent and don't cause us to * read outside of the event * * On success, return the event length in bytes. * Otherwise, return -1 (and print as appropriate). */
static ssize_t catalog_event_len_validate(struct hv_24x7_event_data *event, size_t event_idx, size_t event_data_bytes, size_t event_entry_count, size_t offset, void *end) { ssize_t ev_len; void *ev_end, *calc_ev_end; if (offset >= event_data_bytes) return -1; if (event_idx >= event_entry_count) { pr_devel("catalog event data has %zu bytes of padding after last event\n", event_data_bytes - offset); return -1; } if (!event_fixed_portion_is_within(event, end)) { pr_warn("event %zu fixed portion is not within range\n", event_idx); return -1; } ev_len = be16_to_cpu(event->length); if (ev_len % 16) pr_info("event %zu has length %zu not divisible by 16: event=%pK\n", event_idx, ev_len, event); ev_end = (__u8 *)event + ev_len; if (ev_end > end) { pr_warn("event %zu has .length=%zu, ends after buffer end: ev_end=%pK > end=%pK, offset=%zu\n", event_idx, ev_len, ev_end, end, offset); return -1; } calc_ev_end = event_end(event, end); if (!calc_ev_end) { pr_warn("event %zu has a calculated length which exceeds buffer length %zu: event=%pK end=%pK, offset=%zu\n", event_idx, event_data_bytes, event, end, offset); return -1; } if (calc_ev_end > ev_end) { pr_warn("event %zu exceeds it's own length: event=%pK, end=%pK, offset=%zu, calc_ev_end=%pK\n", event_idx, event, ev_end, offset, calc_ev_end); return -1; } return ev_len; }

Contributors

PersonTokensPropCommitsCommitProp
Cody P Schafer219100.00%1100.00%
Total219100.00%1100.00%

#define MAX_4K (SIZE_MAX / 4096)
static int create_events_from_catalog(struct attribute ***events_, struct attribute ***event_descs_, struct attribute ***event_long_descs_) { long hret; size_t catalog_len, catalog_page_len, event_entry_count, event_data_len, event_data_offs, event_data_bytes, junk_events, event_idx, event_attr_ct, i, attr_max, event_idx_last, desc_ct, long_desc_ct; ssize_t ct, ev_len; uint64_t catalog_version_num; struct attribute **events, **event_descs, **event_long_descs; struct hv_24x7_catalog_page_0 *page_0 = kmem_cache_alloc(hv_page_cache, GFP_KERNEL); void *page = page_0; void *event_data, *end; struct hv_24x7_event_data *event; struct rb_root ev_uniq = RB_ROOT; int ret = 0; if (!page) { ret = -ENOMEM; goto e_out; } hret = h_get_24x7_catalog_page(page, 0, 0); if (hret) { ret = -EIO; goto e_free; } catalog_version_num = be64_to_cpu(page_0->version); catalog_page_len = be32_to_cpu(page_0->length); if (MAX_4K < catalog_page_len) { pr_err("invalid page count: %zu\n", catalog_page_len); ret