+static
+int lttng_abi_validate_event_param(struct lttng_kernel_event *event_param)
+{
+ /* Limit ABI to implemented features. */
+ switch (event_param->instrumentation) {
+ case LTTNG_KERNEL_SYSCALL:
+ switch (event_param->u.syscall.entryexit) {
+ case LTTNG_KERNEL_SYSCALL_ENTRY:
+ case LTTNG_KERNEL_SYSCALL_EXIT:
+ case LTTNG_KERNEL_SYSCALL_ENTRYEXIT:
+ break;
+ default:
+ return -EINVAL;
+ }
+ switch (event_param->u.syscall.abi) {
+ case LTTNG_KERNEL_SYSCALL_ABI_ALL:
+ break;
+ default:
+ return -EINVAL;
+ }
+ switch (event_param->u.syscall.match) {
+ case LTTNG_KERNEL_SYSCALL_MATCH_NAME:
+ break;
+ default:
+ return -EINVAL;
+ }
+ break;
+
+ case LTTNG_KERNEL_TRACEPOINT: /* Fallthrough */
+ case LTTNG_KERNEL_KPROBE: /* Fallthrough */
+ case LTTNG_KERNEL_KRETPROBE: /* Fallthrough */
+ case LTTNG_KERNEL_NOOP: /* Fallthrough */
+ case LTTNG_KERNEL_UPROBE:
+ break;
+
+ case LTTNG_KERNEL_FUNCTION: /* Fallthrough */
+ default:
+ return -EINVAL;
+ }
+ return 0;
+}
+
+static
+int lttng_abi_create_event(struct file *event_container_file,
+ struct lttng_event_container *container,
+ struct lttng_kernel_event *event_param,
+ const struct lttng_counter_key *key)
+{
+ int event_fd, ret;
+ struct file *event_file;
+ void *priv;
+
+ event_param->name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
+ switch (event_param->instrumentation) {
+ case LTTNG_KERNEL_KRETPROBE:
+ event_param->u.kretprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
+ break;
+ case LTTNG_KERNEL_KPROBE:
+ event_param->u.kprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
+ break;
+ case LTTNG_KERNEL_FUNCTION:
+ WARN_ON_ONCE(1);
+ /* Not implemented. */
+ break;
+ default:
+ break;
+ }
+ event_fd = lttng_get_unused_fd();
+ if (event_fd < 0) {
+ ret = event_fd;
+ goto fd_error;
+ }
+ event_file = anon_inode_getfile("[lttng_event]",
+ <tng_event_fops,
+ NULL, O_RDWR);
+ if (IS_ERR(event_file)) {
+ ret = PTR_ERR(event_file);
+ goto file_error;
+ }
+ /* The event holds a reference on the container */
+ if (!atomic_long_add_unless(&event_container_file->f_count, 1, LONG_MAX)) {
+ ret = -EOVERFLOW;
+ goto refcount_error;
+ }
+ ret = lttng_abi_validate_event_param(event_param);
+ if (ret)
+ goto event_error;
+ if (event_param->instrumentation == LTTNG_KERNEL_TRACEPOINT
+ || event_param->instrumentation == LTTNG_KERNEL_SYSCALL) {
+ struct lttng_event_enabler *event_enabler;
+
+ if (strutils_is_star_glob_pattern(event_param->name)) {
+ /*
+ * If the event name is a star globbing pattern,
+ * we create the special star globbing enabler.
+ */
+ event_enabler = lttng_event_enabler_create(LTTNG_ENABLER_FORMAT_STAR_GLOB,
+ event_param, key, container);
+ } else {
+ event_enabler = lttng_event_enabler_create(LTTNG_ENABLER_FORMAT_NAME,
+ event_param, key, container);
+ }
+ priv = event_enabler;
+ } else {
+ struct lttng_event *event;
+
+ /*
+ * We tolerate no failure path after event creation. It
+ * will stay invariant for the rest of the session.
+ */
+ event = lttng_event_create(container, event_param, key,
+ NULL, NULL,
+ event_param->instrumentation, event_param->token);
+ if (IS_ERR(event)) {
+ ret = PTR_ERR(event);
+ goto event_error;
+ }
+ priv = event;
+ }
+ event_file->private_data = priv;
+ fd_install(event_fd, event_file);
+ return event_fd;
+
+event_error:
+ atomic_long_dec(&event_container_file->f_count);
+refcount_error:
+ fput(event_file);
+file_error:
+ put_unused_fd(event_fd);
+fd_error:
+ return ret;
+}
+
+static
+int lttng_counter_release(struct inode *inode, struct file *file)
+{
+ struct lttng_counter *counter = file->private_data;
+
+ if (counter) {
+ /*
+ * Do not destroy the counter itself. Wait of the owner
+ * (event_notifier group) to be destroyed.
+ */
+ fput(counter->owner);
+ }
+
+ return 0;
+}
+
+static
+int copy_counter_key(struct lttng_counter_key *key,
+ const struct lttng_kernel_counter_key *ukey)
+{
+ size_t i, j, nr_dimensions;
+
+ nr_dimensions = ukey->nr_dimensions;
+ if (nr_dimensions > LTTNG_COUNTER_DIMENSION_MAX)
+ return -EINVAL;
+ key->nr_dimensions = nr_dimensions;
+ for (i = 0; i < nr_dimensions; i++) {
+ const struct lttng_kernel_counter_key_dimension *udim =
+ &ukey->key_dimensions[i];
+ struct lttng_counter_key_dimension *dim =
+ &key->key_dimensions[i];
+ size_t nr_key_tokens;
+
+ nr_key_tokens = udim->nr_key_tokens;
+ if (!nr_key_tokens || nr_key_tokens > LTTNG_NR_KEY_TOKEN)
+ return -EINVAL;
+ dim->nr_key_tokens = nr_key_tokens;
+ for (j = 0; j < nr_key_tokens; j++) {
+ const struct lttng_kernel_key_token *utoken =
+ &udim->key_tokens[j];
+ struct lttng_key_token *token =
+ &dim->key_tokens[j];
+
+ switch (utoken->type) {
+ case LTTNG_KERNEL_KEY_TOKEN_STRING:
+ {
+ long ret;
+
+ token->type = LTTNG_KEY_TOKEN_STRING;
+ ret = strncpy_from_user(token->arg.string,
+ (char __user *)(unsigned long)utoken->arg.string_ptr,
+ LTTNG_KEY_TOKEN_STRING_LEN_MAX);
+ if (ret < 0)
+ return -EFAULT;
+ if (!ret || ret == LTTNG_KEY_TOKEN_STRING_LEN_MAX)
+ return -EINVAL;
+ break;
+ }
+ case LTTNG_KERNEL_KEY_TOKEN_EVENT_NAME:
+ token->type = LTTNG_KEY_TOKEN_EVENT_NAME;
+ break;
+ case LTTNG_KERNEL_KEY_TOKEN_PROVIDER_NAME:
+ printk(KERN_ERR "LTTng: Provider name token not supported.\n");
+ /* Fallthrough */
+ default:
+ return -EINVAL;
+ }
+ }
+ }
+ return 0;
+}
+
+static
+long lttng_counter_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
+{
+ struct lttng_counter *counter = file->private_data;
+ struct lttng_event_container *container = lttng_counter_get_event_container(counter);
+ size_t indexes[LTTNG_KERNEL_COUNTER_DIMENSION_MAX] = { 0 };
+ int i;
+
+ switch (cmd) {
+ case LTTNG_KERNEL_COUNTER_READ:
+ {
+ struct lttng_kernel_counter_read local_counter_read;
+ struct lttng_kernel_counter_read __user *ucounter_read =
+ (struct lttng_kernel_counter_read __user *) arg;
+ bool overflow, underflow;
+ int64_t value;
+ int32_t cpu;
+ int ret;
+
+ if (copy_from_user(&local_counter_read, ucounter_read,
+ sizeof(local_counter_read)))
+ return -EFAULT;
+ if (validate_zeroed_padding(local_counter_read.padding,
+ sizeof(local_counter_read.padding)))
+ return -EINVAL;
+
+ /* Cast all indexes into size_t. */
+ for (i = 0; i < local_counter_read.index.number_dimensions; i++)
+ indexes[i] = (size_t) local_counter_read.index.dimension_indexes[i];
+ cpu = local_counter_read.cpu;
+
+ ret = lttng_kernel_counter_read(counter, indexes, cpu, &value,
+ &overflow, &underflow);
+ if (ret)
+ return ret;
+ local_counter_read.value.value = value;
+ local_counter_read.value.overflow = overflow;
+ local_counter_read.value.underflow = underflow;
+
+ if (copy_to_user(&ucounter_read->value, &local_counter_read.value,
+ sizeof(local_counter_read.value)))
+ return -EFAULT;
+
+ return 0;
+ }
+ case LTTNG_KERNEL_COUNTER_AGGREGATE:
+ {
+ struct lttng_kernel_counter_aggregate local_counter_aggregate;
+ struct lttng_kernel_counter_aggregate __user *ucounter_aggregate =
+ (struct lttng_kernel_counter_aggregate __user *) arg;
+ bool overflow, underflow;
+ int64_t value;
+ int ret;
+
+ if (copy_from_user(&local_counter_aggregate, ucounter_aggregate,
+ sizeof(local_counter_aggregate)))
+ return -EFAULT;
+ if (validate_zeroed_padding(local_counter_aggregate.padding,
+ sizeof(local_counter_aggregate.padding)))
+ return -EINVAL;
+
+ /* Cast all indexes into size_t. */
+ for (i = 0; i < local_counter_aggregate.index.number_dimensions; i++)
+ indexes[i] = (size_t) local_counter_aggregate.index.dimension_indexes[i];
+
+ ret = lttng_kernel_counter_aggregate(counter, indexes, &value,
+ &overflow, &underflow);
+ if (ret)
+ return ret;
+ local_counter_aggregate.value.value = value;
+ local_counter_aggregate.value.overflow = overflow;
+ local_counter_aggregate.value.underflow = underflow;
+
+ if (copy_to_user(&ucounter_aggregate->value, &local_counter_aggregate.value,
+ sizeof(local_counter_aggregate.value)))
+ return -EFAULT;
+
+ return 0;
+ }
+ case LTTNG_KERNEL_COUNTER_CLEAR:
+ {
+ struct lttng_kernel_counter_clear local_counter_clear;
+ struct lttng_kernel_counter_clear __user *ucounter_clear =
+ (struct lttng_kernel_counter_clear __user *) arg;
+
+ if (copy_from_user(&local_counter_clear, ucounter_clear,
+ sizeof(local_counter_clear)))
+ return -EFAULT;
+ if (validate_zeroed_padding(local_counter_clear.padding,
+ sizeof(local_counter_clear.padding)))
+ return -EINVAL;
+
+ /* Cast all indexes into size_t. */
+ for (i = 0; i < local_counter_clear.index.number_dimensions; i++)
+ indexes[i] = (size_t) local_counter_clear.index.dimension_indexes[i];
+
+ return lttng_kernel_counter_clear(counter, indexes);
+ }
+ case LTTNG_KERNEL_COUNTER_EVENT:
+ {
+ struct lttng_kernel_counter_event *ucounter_event_param;
+ struct lttng_counter_key *key;
+ int ret;
+
+ key = kzalloc(sizeof(*key), GFP_KERNEL);
+ if (!key)
+ return -ENOMEM;
+ ucounter_event_param = kzalloc(sizeof(*ucounter_event_param), GFP_KERNEL);
+ if (!ucounter_event_param) {
+ ret = -ENOMEM;
+ goto free_key;
+ }
+ if (copy_from_user(ucounter_event_param,
+ (struct lttng_kernel_counter_event __user *) arg,
+ sizeof(*ucounter_event_param))) {
+ ret = -EFAULT;
+ goto free_param;
+ }
+ ret = copy_counter_key(key, &ucounter_event_param->key);
+ if (ret)
+ goto free_param;
+ ret = lttng_abi_create_event(file, container, &ucounter_event_param->event, key);
+ free_param:
+ kfree(ucounter_event_param);
+ free_key:
+ kfree(key);
+ return ret;
+ }
+ case LTTNG_KERNEL_ENABLE:
+ return lttng_event_container_enable(container);
+ case LTTNG_KERNEL_DISABLE:
+ return lttng_event_container_disable(container);
+ case LTTNG_KERNEL_SYSCALL_MASK:
+ return lttng_event_container_syscall_mask(container,
+ (struct lttng_kernel_syscall_mask __user *) arg);
+ case LTTNG_KERNEL_COUNTER_MAP_NR_DESCRIPTORS:
+ {
+ uint64_t __user *user_nr_descriptors = (uint64_t __user *) arg;
+ uint64_t nr_descriptors;
+
+ mutex_lock(&counter->map.lock);
+ nr_descriptors = counter->map.nr_descriptors;
+ mutex_unlock(&counter->map.lock);
+ return put_user(nr_descriptors, user_nr_descriptors);
+ }
+ case LTTNG_KERNEL_COUNTER_MAP_DESCRIPTOR:
+ {
+ struct lttng_kernel_counter_map_descriptor __user *user_descriptor =
+ (struct lttng_kernel_counter_map_descriptor __user *) arg;
+ struct lttng_kernel_counter_map_descriptor local_descriptor;
+ struct lttng_counter_map_descriptor *kernel_descriptor;
+ int ret;
+
+ if (copy_from_user(&local_descriptor, user_descriptor,
+ sizeof(local_descriptor)))
+ return -EFAULT;
+ if (validate_zeroed_padding(local_descriptor.padding,
+ sizeof(local_descriptor.padding)))
+ return -EINVAL;
+
+ mutex_lock(&counter->map.lock);
+ if (local_descriptor.descriptor_index >= counter->map.nr_descriptors) {
+ ret = -EOVERFLOW;
+ goto map_descriptor_error_unlock;
+ }
+ kernel_descriptor = &counter->map.descriptors[local_descriptor.descriptor_index];
+ local_descriptor.user_token = kernel_descriptor->user_token;
+ local_descriptor.array_index = kernel_descriptor->array_index;
+ memcpy(local_descriptor.key, kernel_descriptor->key, LTTNG_KERNEL_COUNTER_KEY_LEN);
+ mutex_unlock(&counter->map.lock);
+
+ if (copy_to_user(user_descriptor, &local_descriptor,
+ sizeof(local_descriptor)))
+ return -EFAULT;
+
+ return 0;
+
+ map_descriptor_error_unlock:
+ mutex_unlock(&counter->map.lock);
+ return ret;
+ }
+ default:
+ WARN_ON_ONCE(1);
+ return -ENOSYS;
+ }
+}
+
+static const struct file_operations lttng_counter_fops = {
+ .owner = THIS_MODULE,
+ .release = lttng_counter_release,
+ .unlocked_ioctl = lttng_counter_ioctl,
+#ifdef CONFIG_COMPAT
+ .compat_ioctl = lttng_counter_ioctl,
+#endif
+};
+
+static
+long lttng_abi_session_create_counter(
+ struct lttng_session *session,
+ const struct lttng_kernel_counter_conf *counter_conf)
+{
+ int counter_fd, ret, i;
+ char *counter_transport_name;
+ struct lttng_event_container *container;
+ struct lttng_counter *counter;
+ struct file *counter_file;
+ size_t dimension_sizes[LTTNG_KERNEL_COUNTER_DIMENSION_MAX] = { 0 };
+ size_t number_dimensions;
+
+ counter_fd = lttng_get_unused_fd();
+ if (counter_fd < 0) {
+ ret = counter_fd;
+ goto fd_error;
+ }
+
+ counter_file = anon_inode_getfile("[lttng_counter]",
+ <tng_counter_fops,
+ NULL, O_RDONLY);
+ if (IS_ERR(counter_file)) {
+ ret = PTR_ERR(counter_file);
+ goto file_error;
+ }
+
+ if (counter_conf->arithmetic != LTTNG_KERNEL_COUNTER_ARITHMETIC_MODULAR) {
+ printk(KERN_ERR "LTTng: Map: Error counter of the wrong type.\n");
+ return -EINVAL;
+ }
+
+ switch (counter_conf->bitness) {
+ case LTTNG_KERNEL_COUNTER_BITNESS_64:
+ counter_transport_name = "counter-per-cpu-64-modular";
+ break;
+ case LTTNG_KERNEL_COUNTER_BITNESS_32:
+ counter_transport_name = "counter-per-cpu-32-modular";
+ break;
+ default:
+ return -EINVAL;
+ }
+
+ number_dimensions = (size_t) counter_conf->number_dimensions;
+
+ for (i = 0; i < counter_conf->number_dimensions; i++) {
+ if (counter_conf->dimensions[i].has_underflow)
+ return -EINVAL;
+ if (counter_conf->dimensions[i].has_overflow)
+ return -EINVAL;
+ dimension_sizes[i] = counter_conf->dimensions[i].size;
+ }
+
+ if (!atomic_long_add_unless(&session->file->f_count, 1, LONG_MAX)) {
+ ret = -EOVERFLOW;
+ goto refcount_error;
+ }
+
+ counter = lttng_session_create_counter(session,
+ counter_transport_name,
+ number_dimensions, dimension_sizes,
+ counter_conf->coalesce_hits);
+ if (!counter) {
+ ret = -EINVAL;
+ goto counter_error;
+ }
+
+ counter->owner = session->file;
+ container = lttng_counter_get_event_container(counter);
+ container->file = counter_file;
+ counter_file->private_data = counter;
+
+ fd_install(counter_fd, counter_file);
+
+ return counter_fd;
+
+counter_error:
+ atomic_long_dec(&session->file->f_count);
+refcount_error:
+ fput(counter_file);
+file_error:
+ put_unused_fd(counter_fd);
+fd_error:
+ return ret;
+}
+