1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * VAS user space API for its accelerators (Only NX-GZIP is supported now)
4 * Copyright (C) 2019 Haren Myneni, IBM Corp
7 #include <linux/kernel.h>
8 #include <linux/device.h>
9 #include <linux/cdev.h>
11 #include <linux/slab.h>
12 #include <linux/uaccess.h>
13 #include <linux/kthread.h>
14 #include <linux/sched/signal.h>
15 #include <linux/mmu_context.h>
18 #include <uapi/asm/vas-api.h>
21 * The driver creates the device node that can be used as follows:
24 * fd = open("/dev/crypto/nx-gzip", O_RDWR);
25 * rc = ioctl(fd, VAS_TX_WIN_OPEN, &attr);
26 * paste_addr = mmap(NULL, PAGE_SIZE, prot, MAP_SHARED, fd, 0ULL).
27 * vas_copy(&crb, 0, 1);
28 * vas_paste(paste_addr, 0, 1);
29 * close(fd) or exit process to close window.
31 * where "vas_copy" and "vas_paste" are defined in copy-paste.h.
32 * copy/paste returns to the user space directly. So refer NX hardware
33 * documentation for exact copy/paste usage and completion / error
38 * Wrapper object for the nx-gzip device - there is just one instance of
39 * this node for the whole system.
41 static struct coproc_dev {
43 struct device *device;
47 enum vas_cop_type cop_type;
48 const struct vas_user_win_ops *vops;
51 struct coproc_instance {
52 struct coproc_dev *coproc;
53 struct vas_window *txwin;
56 static char *coproc_devnode(struct device *dev, umode_t *mode)
58 return kasprintf(GFP_KERNEL, "crypto/%s", dev_name(dev));
62 * Take reference to pid and mm
64 int get_vas_user_win_ref(struct vas_user_win_ref *task_ref)
67 * Window opened by a child thread may not be closed when
68 * it exits. So take reference to its pid and release it
69 * when the window is free by parent thread.
70 * Acquire a reference to the task's pid to make sure
71 * pid will not be re-used - needed only for multithread
74 task_ref->pid = get_task_pid(current, PIDTYPE_PID);
76 * Acquire a reference to the task's mm.
78 task_ref->mm = get_task_mm(current);
80 put_pid(task_ref->pid);
81 pr_err("VAS: pid(%d): mm_struct is not found\n",
89 * Process closes window during exit. In the case of
90 * multithread application, the child thread can open
91 * window and can exit without closing it. So takes tgid
92 * reference until window closed to make sure tgid is not
95 task_ref->tgid = find_get_pid(task_tgid_vnr(current));
101 * Successful return must release the task reference with
104 static bool ref_get_pid_and_task(struct vas_user_win_ref *task_ref,
105 struct task_struct **tskp, struct pid **pidp)
107 struct task_struct *tsk;
111 tsk = get_pid_task(pid, PIDTYPE_PID);
113 pid = task_ref->tgid;
114 tsk = get_pid_task(pid, PIDTYPE_PID);
116 * Parent thread (tgid) will be closing window when it
117 * exits. So should not get here.
119 if (WARN_ON_ONCE(!tsk))
123 /* Return if the task is exiting. */
124 if (tsk->flags & PF_EXITING) {
125 put_task_struct(tsk);
136 * Update the CSB to indicate a translation error.
138 * User space will be polling on CSB after the request is issued.
139 * If NX can handle the request without any issues, it updates CSB.
140 * Whereas if NX encounters page fault, the kernel will handle the
141 * fault and update CSB with translation error.
143 * If we are unable to update the CSB means copy_to_user failed due to
144 * invalid csb_addr, send a signal to the process.
146 void vas_update_csb(struct coprocessor_request_block *crb,
147 struct vas_user_win_ref *task_ref)
149 struct coprocessor_status_block csb;
150 struct kernel_siginfo info;
151 struct task_struct *tsk;
152 void __user *csb_addr;
157 * NX user space windows can not be opened for task->mm=NULL
158 * and faults will not be generated for kernel requests.
160 if (WARN_ON_ONCE(!task_ref->mm))
163 csb_addr = (void __user *)be64_to_cpu(crb->csb_addr);
165 memset(&csb, 0, sizeof(csb));
166 csb.cc = CSB_CC_FAULT_ADDRESS;
167 csb.ce = CSB_CE_TERMINATION;
172 * NX operates and returns in BE format as defined CRB struct.
173 * So saves fault_storage_addr in BE as NX pastes in FIFO and
174 * expects user space to convert to CPU format.
176 csb.address = crb->stamp.nx.fault_storage_addr;
180 * Process closes send window after all pending NX requests are
181 * completed. In multi-thread applications, a child thread can
182 * open a window and can exit without closing it. May be some
183 * requests are pending or this window can be used by other
184 * threads later. We should handle faults if NX encounters
185 * pages faults on these requests. Update CSB with translation
186 * error and fault address. If csb_addr passed by user space is
187 * invalid, send SEGV signal to pid saved in window. If the
188 * child thread is not running, send the signal to tgid.
189 * Parent thread (tgid) will close this window upon its exit.
191 * pid and mm references are taken when window is opened by
192 * process (pid). So tgid is used only when child thread opens
193 * a window and exits without closing it.
196 if (!ref_get_pid_and_task(task_ref, &tsk, &pid))
199 kthread_use_mm(task_ref->mm);
200 rc = copy_to_user(csb_addr, &csb, sizeof(csb));
202 * User space polls on csb.flags (first byte). So add barrier
203 * then copy first byte with csb flags update.
207 /* Make sure update to csb.flags is visible now */
209 rc = copy_to_user(csb_addr, &csb, sizeof(u8));
211 kthread_unuse_mm(task_ref->mm);
212 put_task_struct(tsk);
219 pr_debug("Invalid CSB address 0x%p signalling pid(%d)\n",
220 csb_addr, pid_vnr(pid));
222 clear_siginfo(&info);
223 info.si_signo = SIGSEGV;
224 info.si_errno = EFAULT;
225 info.si_code = SEGV_MAPERR;
226 info.si_addr = csb_addr;
228 * process will be polling on csb.flags after request is sent to
229 * NX. So generally CSB update should not fail except when an
230 * application passes invalid csb_addr. So an error message will
231 * be displayed and leave it to user space whether to ignore or
232 * handle this signal.
235 rc = kill_pid_info(SIGSEGV, &info, pid);
238 pr_devel("%s(): pid %d kill_proc_info() rc %d\n", __func__,
242 void vas_dump_crb(struct coprocessor_request_block *crb)
244 struct data_descriptor_entry *dde;
245 struct nx_fault_stamp *nx;
248 pr_devel("SrcDDE: addr 0x%llx, len %d, count %d, idx %d, flags %d\n",
249 be64_to_cpu(dde->address), be32_to_cpu(dde->length),
250 dde->count, dde->index, dde->flags);
253 pr_devel("TgtDDE: addr 0x%llx, len %d, count %d, idx %d, flags %d\n",
254 be64_to_cpu(dde->address), be32_to_cpu(dde->length),
255 dde->count, dde->index, dde->flags);
258 pr_devel("NX Stamp: PSWID 0x%x, FSA 0x%llx, flags 0x%x, FS 0x%x\n",
259 be32_to_cpu(nx->pswid),
260 be64_to_cpu(crb->stamp.nx.fault_storage_addr),
261 nx->flags, nx->fault_status);
264 static int coproc_open(struct inode *inode, struct file *fp)
266 struct coproc_instance *cp_inst;
268 cp_inst = kzalloc(sizeof(*cp_inst), GFP_KERNEL);
272 cp_inst->coproc = container_of(inode->i_cdev, struct coproc_dev,
274 fp->private_data = cp_inst;
279 static int coproc_ioc_tx_win_open(struct file *fp, unsigned long arg)
281 void __user *uptr = (void __user *)arg;
282 struct vas_tx_win_open_attr uattr;
283 struct coproc_instance *cp_inst;
284 struct vas_window *txwin;
287 cp_inst = fp->private_data;
290 * One window for file descriptor
295 rc = copy_from_user(&uattr, uptr, sizeof(uattr));
297 pr_err("%s(): copy_from_user() returns %d\n", __func__, rc);
301 if (uattr.version != 1) {
302 pr_err("Invalid window open API version\n");
306 if (!cp_inst->coproc->vops || !cp_inst->coproc->vops->open_win) {
307 pr_err("VAS API is not registered\n");
311 txwin = cp_inst->coproc->vops->open_win(uattr.vas_id, uattr.flags,
312 cp_inst->coproc->cop_type);
314 pr_err("%s() VAS window open failed, %ld\n", __func__,
316 return PTR_ERR(txwin);
319 mutex_init(&txwin->task_ref.mmap_mutex);
320 cp_inst->txwin = txwin;
325 static int coproc_release(struct inode *inode, struct file *fp)
327 struct coproc_instance *cp_inst = fp->private_data;
330 if (cp_inst->txwin) {
331 if (cp_inst->coproc->vops &&
332 cp_inst->coproc->vops->close_win) {
333 rc = cp_inst->coproc->vops->close_win(cp_inst->txwin);
337 cp_inst->txwin = NULL;
341 fp->private_data = NULL;
344 * We don't know here if user has other receive windows
345 * open, so we can't really call clear_thread_tidr().
346 * So, once the process calls set_thread_tidr(), the
347 * TIDR value sticks around until process exits, resulting
348 * in an extra copy in restore_sprs().
355 * If the executed instruction that caused the fault was a paste, then
356 * clear regs CR0[EQ], advance NIP, and return 0. Else return error code.
358 static int do_fail_paste(void)
360 struct pt_regs *regs = current->thread.regs;
363 if (WARN_ON_ONCE(!regs))
366 if (WARN_ON_ONCE(!user_mode(regs)))
370 * If we couldn't translate the instruction, the driver should
371 * return success without handling the fault, it will be retried
372 * or the instruction fetch will fault.
374 if (get_user(instword, (u32 __user *)(regs->nip)))
378 * Not a paste instruction, driver may fail the fault.
380 if ((instword & PPC_INST_PASTE_MASK) != PPC_INST_PASTE)
383 regs->ccr &= ~0xe0000000; /* Clear CR0[0-2] to fail paste */
384 regs_add_return_ip(regs, 4); /* Emulate the paste */
390 * This fault handler is invoked when the core generates page fault on
391 * the paste address. Happens if the kernel closes window in hypervisor
392 * (on pseries) due to lost credit or the paste address is not mapped.
394 static vm_fault_t vas_mmap_fault(struct vm_fault *vmf)
396 struct vm_area_struct *vma = vmf->vma;
397 struct file *fp = vma->vm_file;
398 struct coproc_instance *cp_inst = fp->private_data;
399 struct vas_window *txwin;
405 * window is not opened. Shouldn't expect this error.
407 if (!cp_inst || !cp_inst->txwin) {
408 pr_err("%s(): Unexpected fault on paste address with TX window closed\n",
410 return VM_FAULT_SIGBUS;
413 txwin = cp_inst->txwin;
415 * When the LPAR lost credits due to core removal or during
416 * migration, invalidate the existing mapping for the current
417 * paste addresses and set windows in-active (zap_page_range in
418 * reconfig_close_windows()).
419 * New mapping will be done later after migration or new credits
420 * available. So continue to receive faults if the user space
423 if (txwin->task_ref.vma != vmf->vma) {
424 pr_err("%s(): No previous mapping with paste address\n",
426 return VM_FAULT_SIGBUS;
429 mutex_lock(&txwin->task_ref.mmap_mutex);
431 * The window may be inactive due to lost credit (Ex: core
432 * removal with DLPAR). If the window is active again when
433 * the credit is available, map the new paste address at the
434 * the window virtual address.
436 if (txwin->status == VAS_WIN_ACTIVE) {
437 paste_addr = cp_inst->coproc->vops->paste_addr(txwin);
439 fault = vmf_insert_pfn(vma, vma->vm_start,
440 (paste_addr >> PAGE_SHIFT));
441 mutex_unlock(&txwin->task_ref.mmap_mutex);
445 mutex_unlock(&txwin->task_ref.mmap_mutex);
448 * Received this fault due to closing the actual window.
449 * It can happen during migration or lost credits.
450 * Since no mapping, return the paste instruction failure
453 ret = do_fail_paste();
455 * The user space can retry several times until success (needed
456 * for migration) or should fallback to SW compression or
457 * manage with the existing open windows if available.
458 * Looking at sysfs interface, it can determine whether these
459 * failures are coming during migration or core removal:
460 * nr_used_credits > nr_total_credits when lost credits
462 if (!ret || (ret == -EAGAIN))
463 return VM_FAULT_NOPAGE;
465 return VM_FAULT_SIGBUS;
468 static const struct vm_operations_struct vas_vm_ops = {
469 .fault = vas_mmap_fault,
472 static int coproc_mmap(struct file *fp, struct vm_area_struct *vma)
474 struct coproc_instance *cp_inst = fp->private_data;
475 struct vas_window *txwin;
481 txwin = cp_inst->txwin;
483 if ((vma->vm_end - vma->vm_start) > PAGE_SIZE) {
484 pr_debug("%s(): size 0x%zx, PAGE_SIZE 0x%zx\n", __func__,
485 (vma->vm_end - vma->vm_start), PAGE_SIZE);
489 /* Ensure instance has an open send window */
491 pr_err("%s(): No send window open?\n", __func__);
495 if (!cp_inst->coproc->vops || !cp_inst->coproc->vops->paste_addr) {
496 pr_err("%s(): VAS API is not registered\n", __func__);
501 * The initial mmap is done after the window is opened
502 * with ioctl. But before mmap(), this window can be closed in
503 * the hypervisor due to lost credit (core removal on pseries).
504 * So if the window is not active, return mmap() failure with
505 * -EACCES and expects the user space reissue mmap() when it
506 * is active again or open new window when the credit is available.
507 * mmap_mutex protects the paste address mmap() with DLPAR
508 * close/open event and allows mmap() only when the window is
511 mutex_lock(&txwin->task_ref.mmap_mutex);
512 if (txwin->status != VAS_WIN_ACTIVE) {
513 pr_err("%s(): Window is not active\n", __func__);
518 paste_addr = cp_inst->coproc->vops->paste_addr(txwin);
520 pr_err("%s(): Window paste address failed\n", __func__);
525 pfn = paste_addr >> PAGE_SHIFT;
527 /* flags, page_prot from cxl_mmap(), except we want cachable */
528 vma->vm_flags |= VM_IO | VM_PFNMAP;
529 vma->vm_page_prot = pgprot_cached(vma->vm_page_prot);
531 prot = __pgprot(pgprot_val(vma->vm_page_prot) | _PAGE_DIRTY);
533 rc = remap_pfn_range(vma, vma->vm_start, pfn + vma->vm_pgoff,
534 vma->vm_end - vma->vm_start, prot);
536 pr_devel("%s(): paste addr %llx at %lx, rc %d\n", __func__,
537 paste_addr, vma->vm_start, rc);
539 txwin->task_ref.vma = vma;
540 vma->vm_ops = &vas_vm_ops;
543 mutex_unlock(&txwin->task_ref.mmap_mutex);
547 static long coproc_ioctl(struct file *fp, unsigned int cmd, unsigned long arg)
550 case VAS_TX_WIN_OPEN:
551 return coproc_ioc_tx_win_open(fp, arg);
557 static struct file_operations coproc_fops = {
559 .release = coproc_release,
561 .unlocked_ioctl = coproc_ioctl,
565 * Supporting only nx-gzip coprocessor type now, but this API code
566 * extended to other coprocessor types later.
568 int vas_register_coproc_api(struct module *mod, enum vas_cop_type cop_type,
570 const struct vas_user_win_ops *vops)
575 rc = alloc_chrdev_region(&coproc_device.devt, 1, 1, name);
577 pr_err("Unable to allocate coproc major number: %i\n", rc);
581 pr_devel("%s device allocated, dev [%i,%i]\n", name,
582 MAJOR(coproc_device.devt), MINOR(coproc_device.devt));
584 coproc_device.class = class_create(mod, name);
585 if (IS_ERR(coproc_device.class)) {
586 rc = PTR_ERR(coproc_device.class);
587 pr_err("Unable to create %s class %d\n", name, rc);
590 coproc_device.class->devnode = coproc_devnode;
591 coproc_device.cop_type = cop_type;
592 coproc_device.vops = vops;
594 coproc_fops.owner = mod;
595 cdev_init(&coproc_device.cdev, &coproc_fops);
597 devno = MKDEV(MAJOR(coproc_device.devt), 0);
598 rc = cdev_add(&coproc_device.cdev, devno, 1);
600 pr_err("cdev_add() failed %d\n", rc);
604 coproc_device.device = device_create(coproc_device.class, NULL,
605 devno, NULL, name, MINOR(devno));
606 if (IS_ERR(coproc_device.device)) {
607 rc = PTR_ERR(coproc_device.device);
608 pr_err("Unable to create coproc-%d %d\n", MINOR(devno), rc);
612 pr_devel("%s: Added dev [%d,%d]\n", __func__, MAJOR(devno),
618 cdev_del(&coproc_device.cdev);
620 class_destroy(coproc_device.class);
622 unregister_chrdev_region(coproc_device.devt, 1);
626 void vas_unregister_coproc_api(void)
630 cdev_del(&coproc_device.cdev);
631 devno = MKDEV(MAJOR(coproc_device.devt), 0);
632 device_destroy(coproc_device.class, devno);
634 class_destroy(coproc_device.class);
635 unregister_chrdev_region(coproc_device.devt, 1);