|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
| 2 | +/* Copyright (C) 2024 Intel Corporation |
| 3 | + */ |
| 4 | +#define pr_fmt(fmt) "iommufd: " fmt |
| 5 | + |
| 6 | +#include <linux/file.h> |
| 7 | +#include <linux/fs.h> |
| 8 | +#include <linux/module.h> |
| 9 | +#include <linux/mutex.h> |
| 10 | +#include <linux/iommufd.h> |
| 11 | +#include <linux/poll.h> |
| 12 | +#include <linux/anon_inodes.h> |
| 13 | +#include <uapi/linux/iommufd.h> |
| 14 | + |
| 15 | +#include "../iommu-priv.h" |
| 16 | +#include "iommufd_private.h" |
| 17 | + |
| 18 | +void iommufd_fault_destroy(struct iommufd_object *obj) |
| 19 | +{ |
| 20 | + struct iommufd_fault *fault = container_of(obj, struct iommufd_fault, obj); |
| 21 | + struct iopf_group *group, *next; |
| 22 | + |
| 23 | + /* |
| 24 | + * The iommufd object's reference count is zero at this point. |
| 25 | + * We can be confident that no other threads are currently |
| 26 | + * accessing this pointer. Therefore, acquiring the mutex here |
| 27 | + * is unnecessary. |
| 28 | + */ |
| 29 | + list_for_each_entry_safe(group, next, &fault->deliver, node) { |
| 30 | + list_del(&group->node); |
| 31 | + iopf_group_response(group, IOMMU_PAGE_RESP_INVALID); |
| 32 | + iopf_free_group(group); |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +static void iommufd_compose_fault_message(struct iommu_fault *fault, |
| 37 | + struct iommu_hwpt_pgfault *hwpt_fault, |
| 38 | + struct iommufd_device *idev, |
| 39 | + u32 cookie) |
| 40 | +{ |
| 41 | + hwpt_fault->flags = fault->prm.flags; |
| 42 | + hwpt_fault->dev_id = idev->obj.id; |
| 43 | + hwpt_fault->pasid = fault->prm.pasid; |
| 44 | + hwpt_fault->grpid = fault->prm.grpid; |
| 45 | + hwpt_fault->perm = fault->prm.perm; |
| 46 | + hwpt_fault->addr = fault->prm.addr; |
| 47 | + hwpt_fault->length = 0; |
| 48 | + hwpt_fault->cookie = cookie; |
| 49 | +} |
| 50 | + |
| 51 | +static ssize_t iommufd_fault_fops_read(struct file *filep, char __user *buf, |
| 52 | + size_t count, loff_t *ppos) |
| 53 | +{ |
| 54 | + size_t fault_size = sizeof(struct iommu_hwpt_pgfault); |
| 55 | + struct iommufd_fault *fault = filep->private_data; |
| 56 | + struct iommu_hwpt_pgfault data; |
| 57 | + struct iommufd_device *idev; |
| 58 | + struct iopf_group *group; |
| 59 | + struct iopf_fault *iopf; |
| 60 | + size_t done = 0; |
| 61 | + int rc = 0; |
| 62 | + |
| 63 | + if (*ppos || count % fault_size) |
| 64 | + return -ESPIPE; |
| 65 | + |
| 66 | + mutex_lock(&fault->mutex); |
| 67 | + while (!list_empty(&fault->deliver) && count > done) { |
| 68 | + group = list_first_entry(&fault->deliver, |
| 69 | + struct iopf_group, node); |
| 70 | + |
| 71 | + if (group->fault_count * fault_size > count - done) |
| 72 | + break; |
| 73 | + |
| 74 | + rc = xa_alloc(&fault->response, &group->cookie, group, |
| 75 | + xa_limit_32b, GFP_KERNEL); |
| 76 | + if (rc) |
| 77 | + break; |
| 78 | + |
| 79 | + idev = to_iommufd_handle(group->attach_handle)->idev; |
| 80 | + list_for_each_entry(iopf, &group->faults, list) { |
| 81 | + iommufd_compose_fault_message(&iopf->fault, |
| 82 | + &data, idev, |
| 83 | + group->cookie); |
| 84 | + if (copy_to_user(buf + done, &data, fault_size)) { |
| 85 | + xa_erase(&fault->response, group->cookie); |
| 86 | + rc = -EFAULT; |
| 87 | + break; |
| 88 | + } |
| 89 | + done += fault_size; |
| 90 | + } |
| 91 | + |
| 92 | + list_del(&group->node); |
| 93 | + } |
| 94 | + mutex_unlock(&fault->mutex); |
| 95 | + |
| 96 | + return done == 0 ? rc : done; |
| 97 | +} |
| 98 | + |
| 99 | +static ssize_t iommufd_fault_fops_write(struct file *filep, const char __user *buf, |
| 100 | + size_t count, loff_t *ppos) |
| 101 | +{ |
| 102 | + size_t response_size = sizeof(struct iommu_hwpt_page_response); |
| 103 | + struct iommufd_fault *fault = filep->private_data; |
| 104 | + struct iommu_hwpt_page_response response; |
| 105 | + struct iopf_group *group; |
| 106 | + size_t done = 0; |
| 107 | + int rc = 0; |
| 108 | + |
| 109 | + if (*ppos || count % response_size) |
| 110 | + return -ESPIPE; |
| 111 | + |
| 112 | + mutex_lock(&fault->mutex); |
| 113 | + while (count > done) { |
| 114 | + rc = copy_from_user(&response, buf + done, response_size); |
| 115 | + if (rc) |
| 116 | + break; |
| 117 | + |
| 118 | + group = xa_erase(&fault->response, response.cookie); |
| 119 | + if (!group) { |
| 120 | + rc = -EINVAL; |
| 121 | + break; |
| 122 | + } |
| 123 | + |
| 124 | + iopf_group_response(group, response.code); |
| 125 | + iopf_free_group(group); |
| 126 | + done += response_size; |
| 127 | + } |
| 128 | + mutex_unlock(&fault->mutex); |
| 129 | + |
| 130 | + return done == 0 ? rc : done; |
| 131 | +} |
| 132 | + |
| 133 | +static __poll_t iommufd_fault_fops_poll(struct file *filep, |
| 134 | + struct poll_table_struct *wait) |
| 135 | +{ |
| 136 | + struct iommufd_fault *fault = filep->private_data; |
| 137 | + __poll_t pollflags = EPOLLOUT; |
| 138 | + |
| 139 | + poll_wait(filep, &fault->wait_queue, wait); |
| 140 | + mutex_lock(&fault->mutex); |
| 141 | + if (!list_empty(&fault->deliver)) |
| 142 | + pollflags |= EPOLLIN | EPOLLRDNORM; |
| 143 | + mutex_unlock(&fault->mutex); |
| 144 | + |
| 145 | + return pollflags; |
| 146 | +} |
| 147 | + |
| 148 | +static int iommufd_fault_fops_release(struct inode *inode, struct file *filep) |
| 149 | +{ |
| 150 | + struct iommufd_fault *fault = filep->private_data; |
| 151 | + |
| 152 | + refcount_dec(&fault->obj.users); |
| 153 | + iommufd_ctx_put(fault->ictx); |
| 154 | + return 0; |
| 155 | +} |
| 156 | + |
| 157 | +static const struct file_operations iommufd_fault_fops = { |
| 158 | + .owner = THIS_MODULE, |
| 159 | + .open = nonseekable_open, |
| 160 | + .read = iommufd_fault_fops_read, |
| 161 | + .write = iommufd_fault_fops_write, |
| 162 | + .poll = iommufd_fault_fops_poll, |
| 163 | + .release = iommufd_fault_fops_release, |
| 164 | + .llseek = no_llseek, |
| 165 | +}; |
| 166 | + |
| 167 | +int iommufd_fault_alloc(struct iommufd_ucmd *ucmd) |
| 168 | +{ |
| 169 | + struct iommu_fault_alloc *cmd = ucmd->cmd; |
| 170 | + struct iommufd_fault *fault; |
| 171 | + struct file *filep; |
| 172 | + int fdno; |
| 173 | + int rc; |
| 174 | + |
| 175 | + if (cmd->flags) |
| 176 | + return -EOPNOTSUPP; |
| 177 | + |
| 178 | + fault = iommufd_object_alloc(ucmd->ictx, fault, IOMMUFD_OBJ_FAULT); |
| 179 | + if (IS_ERR(fault)) |
| 180 | + return PTR_ERR(fault); |
| 181 | + |
| 182 | + fault->ictx = ucmd->ictx; |
| 183 | + INIT_LIST_HEAD(&fault->deliver); |
| 184 | + xa_init_flags(&fault->response, XA_FLAGS_ALLOC1); |
| 185 | + mutex_init(&fault->mutex); |
| 186 | + init_waitqueue_head(&fault->wait_queue); |
| 187 | + |
| 188 | + filep = anon_inode_getfile("[iommufd-pgfault]", &iommufd_fault_fops, |
| 189 | + fault, O_RDWR); |
| 190 | + if (IS_ERR(filep)) { |
| 191 | + rc = PTR_ERR(filep); |
| 192 | + goto out_abort; |
| 193 | + } |
| 194 | + |
| 195 | + refcount_inc(&fault->obj.users); |
| 196 | + iommufd_ctx_get(fault->ictx); |
| 197 | + fault->filep = filep; |
| 198 | + |
| 199 | + fdno = get_unused_fd_flags(O_CLOEXEC); |
| 200 | + if (fdno < 0) { |
| 201 | + rc = fdno; |
| 202 | + goto out_fput; |
| 203 | + } |
| 204 | + |
| 205 | + cmd->out_fault_id = fault->obj.id; |
| 206 | + cmd->out_fault_fd = fdno; |
| 207 | + |
| 208 | + rc = iommufd_ucmd_respond(ucmd, sizeof(*cmd)); |
| 209 | + if (rc) |
| 210 | + goto out_put_fdno; |
| 211 | + iommufd_object_finalize(ucmd->ictx, &fault->obj); |
| 212 | + |
| 213 | + fd_install(fdno, fault->filep); |
| 214 | + |
| 215 | + return 0; |
| 216 | +out_put_fdno: |
| 217 | + put_unused_fd(fdno); |
| 218 | +out_fput: |
| 219 | + fput(filep); |
| 220 | + refcount_dec(&fault->obj.users); |
| 221 | + iommufd_ctx_put(fault->ictx); |
| 222 | +out_abort: |
| 223 | + iommufd_object_abort_and_destroy(ucmd->ictx, &fault->obj); |
| 224 | + |
| 225 | + return rc; |
| 226 | +} |
0 commit comments