1 // SPDX-License-Identifier: MIT 2 /* 3 * Copyright © 2023-2024 Intel Corporation 4 */ 5 6 #include <linux/errno.h> 7 #include <linux/perf_event.h> 8 #include <linux/sysctl.h> 9 10 #include <uapi/drm/xe_drm.h> 11 12 #include "xe_eu_stall.h" 13 #include "xe_oa.h" 14 #include "xe_observation.h" 15 16 static u32 xe_observation_paranoid = true; 17 static struct ctl_table_header *sysctl_header; 18 19 /** 20 * xe_observation_paranoid_check - Gate access to xe observation streams. 21 * 22 * When the xe-specific observation_paranoid sysctl is enabled (the 23 * default), defer to perf_allow_cpu() so that access is governed by the 24 * same policy as system-wide perf CPU events: kernel.perf_event_paranoid 25 * plus the security_perf_event_open() LSM hook. When the sysctl has been 26 * cleared by a privileged user, observation is open to all callers. 27 * 28 * Return: 0 if access is permitted, a negative errno otherwise. 29 */ 30 int xe_observation_paranoid_check(void) 31 { 32 if (!xe_observation_paranoid) 33 return 0; 34 35 return perf_allow_cpu(); 36 } 37 38 static int xe_oa_ioctl(struct drm_device *dev, struct drm_xe_observation_param *arg, 39 struct drm_file *file) 40 { 41 switch (arg->observation_op) { 42 case DRM_XE_OBSERVATION_OP_STREAM_OPEN: 43 return xe_oa_stream_open_ioctl(dev, arg->param, file); 44 case DRM_XE_OBSERVATION_OP_ADD_CONFIG: 45 return xe_oa_add_config_ioctl(dev, arg->param, file); 46 case DRM_XE_OBSERVATION_OP_REMOVE_CONFIG: 47 return xe_oa_remove_config_ioctl(dev, arg->param, file); 48 default: 49 return -EINVAL; 50 } 51 } 52 53 static int xe_eu_stall_ioctl(struct drm_device *dev, struct drm_xe_observation_param *arg, 54 struct drm_file *file) 55 { 56 switch (arg->observation_op) { 57 case DRM_XE_OBSERVATION_OP_STREAM_OPEN: 58 return xe_eu_stall_stream_open(dev, arg->param, file); 59 default: 60 return -EINVAL; 61 } 62 } 63 64 /** 65 * xe_observation_ioctl - The top level observation layer ioctl 66 * @dev: @drm_device 67 * @data: pointer to struct @drm_xe_observation_param 68 * @file: @drm_file 69 * 70 * The function is called for different observation streams types and 71 * allows execution of different operations supported by those stream 72 * types. 73 * 74 * Return: 0 on success or a negative error code on failure. 75 */ 76 int xe_observation_ioctl(struct drm_device *dev, void *data, struct drm_file *file) 77 { 78 struct drm_xe_observation_param *arg = data; 79 80 if (arg->extensions) 81 return -EINVAL; 82 83 switch (arg->observation_type) { 84 case DRM_XE_OBSERVATION_TYPE_OA: 85 return xe_oa_ioctl(dev, arg, file); 86 case DRM_XE_OBSERVATION_TYPE_EU_STALL: 87 return xe_eu_stall_ioctl(dev, arg, file); 88 default: 89 return -EINVAL; 90 } 91 } 92 93 static const struct ctl_table observation_ctl_table[] = { 94 { 95 .procname = "observation_paranoid", 96 .data = &xe_observation_paranoid, 97 .maxlen = sizeof(xe_observation_paranoid), 98 .mode = 0644, 99 .proc_handler = proc_dointvec_minmax, 100 .extra1 = SYSCTL_ZERO, 101 .extra2 = SYSCTL_ONE, 102 }, 103 }; 104 105 /** 106 * xe_observation_sysctl_register - Register the observation_paranoid sysctl 107 * 108 * When dev.xe.observation_paranoid is set (the default), access to 109 * observation streams follows the system-wide perf_allow_cpu() policy: 110 * kernel.perf_event_paranoid plus the security_perf_event_open() LSM 111 * hook. A privileged user can clear the sysctl to bypass that gate and 112 * allow unprivileged access to observation data. 113 * 114 * Return: always returns 0 115 */ 116 int xe_observation_sysctl_register(void) 117 { 118 sysctl_header = register_sysctl("dev/xe", observation_ctl_table); 119 return 0; 120 } 121 122 /** 123 * xe_observation_sysctl_unregister - Unregister xe_observation_paranoid sysctl 124 */ 125 void xe_observation_sysctl_unregister(void) 126 { 127 unregister_sysctl_table(sysctl_header); 128 } 129