+static
+long lttng_event_notifier_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
+{
+ struct lttng_event_notifier *event_notifier;
+ struct lttng_event_notifier_enabler *event_notifier_enabler;
+ enum lttng_event_type *evtype = file->private_data;
+
+ switch (cmd) {
+ case LTTNG_KERNEL_ENABLE:
+ switch (*evtype) {
+ case LTTNG_TYPE_EVENT:
+ event_notifier = file->private_data;
+ return lttng_event_notifier_enable(event_notifier);
+ case LTTNG_TYPE_ENABLER:
+ event_notifier_enabler = file->private_data;
+ return lttng_event_notifier_enabler_enable(event_notifier_enabler);
+ default:
+ WARN_ON_ONCE(1);
+ return -ENOSYS;
+ }
+ case LTTNG_KERNEL_DISABLE:
+ switch (*evtype) {
+ case LTTNG_TYPE_EVENT:
+ event_notifier = file->private_data;
+ return lttng_event_notifier_disable(event_notifier);
+ case LTTNG_TYPE_ENABLER:
+ event_notifier_enabler = file->private_data;
+ return lttng_event_notifier_enabler_disable(event_notifier_enabler);
+ default:
+ WARN_ON_ONCE(1);
+ return -ENOSYS;
+ }
+ case LTTNG_KERNEL_FILTER:
+ switch (*evtype) {
+ case LTTNG_TYPE_EVENT:
+ return -EINVAL;
+ case LTTNG_TYPE_ENABLER:
+ event_notifier_enabler = file->private_data;
+ return lttng_event_notifier_enabler_attach_filter_bytecode(
+ event_notifier_enabler,
+ (struct lttng_kernel_filter_bytecode __user *) arg);
+ default:
+ WARN_ON_ONCE(1);
+ return -ENOSYS;
+ }
+
+ case LTTNG_KERNEL_CAPTURE:
+ switch (*evtype) {
+ case LTTNG_TYPE_EVENT:
+ return -EINVAL;
+ case LTTNG_TYPE_ENABLER:
+ event_notifier_enabler = file->private_data;
+ return lttng_event_notifier_enabler_attach_capture_bytecode(
+ event_notifier_enabler,
+ (struct lttng_kernel_capture_bytecode __user *) arg);
+ default:
+ WARN_ON_ONCE(1);
+ return -ENOSYS;
+ }
+ case LTTNG_KERNEL_ADD_CALLSITE:
+ switch (*evtype) {
+ case LTTNG_TYPE_EVENT:
+ event_notifier = file->private_data;
+ return lttng_event_notifier_add_callsite(event_notifier,
+ (struct lttng_kernel_event_callsite __user *) arg);
+ case LTTNG_TYPE_ENABLER:
+ return -EINVAL;
+ default:
+ WARN_ON_ONCE(1);
+ return -ENOSYS;
+ }
+ default:
+ return -ENOIOCTLCMD;
+ }
+}
+
+static
+int lttng_event_notifier_release(struct inode *inode, struct file *file)
+{
+ struct lttng_event_notifier *event_notifier;
+ struct lttng_event_notifier_enabler *event_notifier_enabler;
+ enum lttng_event_type *evtype = file->private_data;
+
+ if (!evtype)
+ return 0;
+
+ switch (*evtype) {
+ case LTTNG_TYPE_EVENT:
+ event_notifier = file->private_data;
+ if (event_notifier)
+ fput(event_notifier->group->file);
+ break;
+ case LTTNG_TYPE_ENABLER:
+ event_notifier_enabler = file->private_data;
+ if (event_notifier_enabler)
+ fput(event_notifier_enabler->group->file);
+ break;
+ default:
+ WARN_ON_ONCE(1);
+ break;
+ }
+
+ return 0;
+}
+
+static const struct file_operations lttng_event_notifier_fops = {
+ .owner = THIS_MODULE,
+ .release = lttng_event_notifier_release,
+ .unlocked_ioctl = lttng_event_notifier_ioctl,
+#ifdef CONFIG_COMPAT
+ .compat_ioctl = lttng_event_notifier_ioctl,
+#endif
+};
+
+static
+int lttng_abi_create_event_notifier(struct file *event_notifier_group_file,
+ struct lttng_kernel_event_notifier *event_notifier_param)
+{
+ struct lttng_event_notifier_group *event_notifier_group =
+ event_notifier_group_file->private_data;
+ int event_notifier_fd, ret;
+ struct file *event_notifier_file;
+ void *priv;
+
+ switch (event_notifier_param->event.instrumentation) {
+ case LTTNG_KERNEL_TRACEPOINT:
+ case LTTNG_KERNEL_UPROBE:
+ break;
+ case LTTNG_KERNEL_KPROBE:
+ event_notifier_param->event.u.kprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
+ break;
+ case LTTNG_KERNEL_SYSCALL:
+ break;
+ case LTTNG_KERNEL_KRETPROBE:
+ /* Placing an event notifier on kretprobe is not supported. */
+ case LTTNG_KERNEL_FUNCTION:
+ case LTTNG_KERNEL_NOOP:
+ default:
+ ret = -EINVAL;
+ goto inval_instr;
+ }
+
+ event_notifier_param->event.name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
+
+ event_notifier_fd = lttng_get_unused_fd();
+ if (event_notifier_fd < 0) {
+ ret = event_notifier_fd;
+ goto fd_error;
+ }
+
+ event_notifier_file = anon_inode_getfile("[lttng_event_notifier]",
+ <tng_event_notifier_fops,
+ NULL, O_RDWR);
+ if (IS_ERR(event_notifier_file)) {
+ ret = PTR_ERR(event_notifier_file);
+ goto file_error;
+ }
+
+ /* The event notifier holds a reference on the event notifier group. */
+ if (!atomic_long_add_unless(&event_notifier_group_file->f_count, 1, LONG_MAX)) {
+ ret = -EOVERFLOW;
+ goto refcount_error;
+ }
+
+ ret = lttng_abi_validate_event_param(&event_notifier_param->event);
+ if (ret)
+ goto event_notifier_error;
+
+ switch (event_notifier_param->event.instrumentation) {
+ case LTTNG_KERNEL_TRACEPOINT: /* Fall-through */
+ case LTTNG_KERNEL_SYSCALL:
+ {
+ struct lttng_event_notifier_enabler *enabler;
+
+ if (strutils_is_star_glob_pattern(event_notifier_param->event.name)) {
+ /*
+ * If the event name is a star globbing pattern,
+ * we create the special star globbing enabler.
+ */
+ enabler = lttng_event_notifier_enabler_create(
+ event_notifier_group,
+ LTTNG_ENABLER_FORMAT_STAR_GLOB,
+ event_notifier_param);
+ } else {
+ enabler = lttng_event_notifier_enabler_create(
+ event_notifier_group,
+ LTTNG_ENABLER_FORMAT_NAME,
+ event_notifier_param);
+ }
+ priv = enabler;
+ break;
+ }
+
+ case LTTNG_KERNEL_KPROBE: /* Fall-through */
+ case LTTNG_KERNEL_KRETPROBE: /* Fall-through */
+ case LTTNG_KERNEL_UPROBE:
+ {
+ struct lttng_event_notifier *event_notifier;
+
+ /*
+ * We tolerate no failure path after event notifier creation.
+ * It will stay invariant for the rest of the session.
+ */
+ event_notifier = lttng_event_notifier_create(NULL,
+ event_notifier_param->event.token,
+ event_notifier_param->error_counter_index,
+ event_notifier_group,
+ event_notifier_param, NULL,
+ event_notifier_param->event.instrumentation);
+ WARN_ON_ONCE(!event_notifier);
+ if (IS_ERR(event_notifier)) {
+ ret = PTR_ERR(event_notifier);
+ goto event_notifier_error;
+ }
+ priv = event_notifier;
+ break;
+ }
+
+ case LTTNG_KERNEL_FUNCTION: /* Fall-through */
+ case LTTNG_KERNEL_NOOP: /* Fall-through */
+ default:
+ ret = -EINVAL;
+ goto event_notifier_error;
+ }
+ event_notifier_file->private_data = priv;
+ fd_install(event_notifier_fd, event_notifier_file);
+ return event_notifier_fd;
+
+event_notifier_error:
+ atomic_long_dec(&event_notifier_group_file->f_count);
+refcount_error:
+ fput(event_notifier_file);
+file_error:
+ put_unused_fd(event_notifier_fd);
+fd_error:
+inval_instr:
+ return ret;
+}
+
+static
+long lttng_abi_event_notifier_group_create_error_counter(
+ struct file *event_notifier_group_file,
+ const struct lttng_kernel_counter_conf *error_counter_conf)
+{
+ int counter_fd, ret;
+ char *counter_transport_name;
+ size_t counter_len;
+ struct lttng_counter *counter = NULL;
+ struct file *counter_file;
+ struct lttng_event_notifier_group *event_notifier_group =
+ (struct lttng_event_notifier_group *) event_notifier_group_file->private_data;
+
+ if (error_counter_conf->arithmetic != LTTNG_KERNEL_COUNTER_ARITHMETIC_MODULAR) {
+ printk(KERN_ERR "LTTng: event_notifier: Error counter of the wrong arithmetic type.\n");
+ return -EINVAL;
+ }
+
+ if (error_counter_conf->number_dimensions != 1) {
+ printk(KERN_ERR "LTTng: event_notifier: Error counter has more than one dimension.\n");
+ return -EINVAL;
+ }
+
+ switch (error_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;
+ }
+
+ /*
+ * Lock sessions to provide mutual exclusion against concurrent
+ * modification of event_notifier group, which would result in
+ * overwriting the error counter if set concurrently.
+ */
+ lttng_lock_sessions();
+
+ if (event_notifier_group->error_counter) {
+ printk(KERN_ERR "Error counter already created in event_notifier group\n");
+ ret = -EBUSY;
+ goto fd_error;
+ }
+
+ 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;
+ }
+
+ counter_len = error_counter_conf->dimensions[0].size;
+
+ if (!atomic_long_add_unless(&event_notifier_group_file->f_count, 1, LONG_MAX)) {
+ ret = -EOVERFLOW;
+ goto refcount_error;
+ }
+
+ counter = lttng_kernel_counter_create(counter_transport_name,
+ 1, &counter_len);
+ if (!counter) {
+ ret = -EINVAL;
+ goto counter_error;
+ }
+
+ event_notifier_group->error_counter_len = counter_len;
+ /*
+ * store-release to publish error counter matches load-acquire
+ * in record_error. Ensures the counter is created and the
+ * error_counter_len is set before they are used.
+ */
+ lttng_smp_store_release(&event_notifier_group->error_counter, counter);
+
+ counter->file = counter_file;
+ counter->owner = event_notifier_group->file;
+ counter_file->private_data = counter;
+ /* Ownership transferred. */
+ counter = NULL;
+
+ fd_install(counter_fd, counter_file);
+ lttng_unlock_sessions();
+
+ return counter_fd;
+
+counter_error:
+ atomic_long_dec(&event_notifier_group_file->f_count);
+refcount_error:
+ fput(counter_file);
+file_error:
+ put_unused_fd(counter_fd);
+fd_error:
+ lttng_unlock_sessions();
+ return ret;
+}
+
+static
+long lttng_event_notifier_group_ioctl(struct file *file, unsigned int cmd,
+ unsigned long arg)
+{
+ switch (cmd) {
+ case LTTNG_KERNEL_EVENT_NOTIFIER_GROUP_NOTIFICATION_FD:
+ {
+ return lttng_abi_open_event_notifier_group_stream(file);
+ }
+ case LTTNG_KERNEL_EVENT_NOTIFIER_CREATE:
+ {
+ struct lttng_kernel_event_notifier uevent_notifier_param;
+
+ if (copy_from_user(&uevent_notifier_param,
+ (struct lttng_kernel_event_notifier __user *) arg,
+ sizeof(uevent_notifier_param)))
+ return -EFAULT;
+ return lttng_abi_create_event_notifier(file, &uevent_notifier_param);
+ }
+ case LTTNG_KERNEL_COUNTER:
+ {
+ struct lttng_kernel_counter_conf uerror_counter_conf;
+
+ if (copy_from_user(&uerror_counter_conf,
+ (struct lttng_kernel_counter_conf __user *) arg,
+ sizeof(uerror_counter_conf)))
+ return -EFAULT;
+ return lttng_abi_event_notifier_group_create_error_counter(file,
+ &uerror_counter_conf);
+ }
+ default:
+ return -ENOIOCTLCMD;
+ }
+ return 0;
+}
+
+static
+int lttng_event_notifier_group_release(struct inode *inode, struct file *file)
+{
+ struct lttng_event_notifier_group *event_notifier_group =
+ file->private_data;
+
+ if (event_notifier_group)
+ lttng_event_notifier_group_destroy(event_notifier_group);
+ return 0;
+}
+
+static const struct file_operations lttng_event_notifier_group_fops = {
+ .owner = THIS_MODULE,
+ .release = lttng_event_notifier_group_release,
+ .unlocked_ioctl = lttng_event_notifier_group_ioctl,
+#ifdef CONFIG_COMPAT
+ .compat_ioctl = lttng_event_notifier_group_ioctl,
+#endif
+};
+