1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
3 * Copyright (c) 2016 Mellanox Technologies Ltd. All rights reserved.
4 * Copyright (c) 2015 System Fabric Works, Inc. All rights reserved.
7 #include <linux/vmalloc.h>
9 #include <linux/errno.h>
10 #include <rdma/uverbs_ioctl.h>
14 #include "rxe_queue.h"
16 void rxe_mmap_release(struct kref *ref)
18 struct rxe_mmap_info *ip = container_of(ref,
19 struct rxe_mmap_info, ref);
20 struct rxe_dev *rxe = to_rdev(ip->context->device);
22 spin_lock_bh(&rxe->pending_lock);
24 if (!list_empty(&ip->pending_mmaps))
25 list_del(&ip->pending_mmaps);
27 spin_unlock_bh(&rxe->pending_lock);
29 vfree(ip->obj); /* buf */
34 * open and close keep track of how many times the memory region is mapped,
35 * to avoid releasing it.
37 static void rxe_vma_open(struct vm_area_struct *vma)
39 struct rxe_mmap_info *ip = vma->vm_private_data;
44 static void rxe_vma_close(struct vm_area_struct *vma)
46 struct rxe_mmap_info *ip = vma->vm_private_data;
48 kref_put(&ip->ref, rxe_mmap_release);
51 static const struct vm_operations_struct rxe_vm_ops = {
53 .close = rxe_vma_close,
57 * rxe_mmap - create a new mmap region
58 * @context: the IB user context of the process making the mmap() call
59 * @vma: the VMA to be initialized
60 * Return zero if the mmap is OK. Otherwise, return an errno.
62 int rxe_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
64 struct rxe_dev *rxe = to_rdev(context->device);
65 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
66 unsigned long size = vma->vm_end - vma->vm_start;
67 struct rxe_mmap_info *ip, *pp;
71 * Search the device's list of objects waiting for a mmap call.
72 * Normally, this list is very short since a call to create a
73 * CQ, QP, or SRQ is soon followed by a call to mmap().
75 spin_lock_bh(&rxe->pending_lock);
76 list_for_each_entry_safe(ip, pp, &rxe->pending_mmaps, pending_mmaps) {
77 if (context != ip->context || (__u64)offset != ip->info.offset)
80 /* Don't allow a mmap larger than the object. */
81 if (size > ip->info.size) {
82 rxe_dbg_dev(rxe, "mmap region is larger than the object!\n");
83 spin_unlock_bh(&rxe->pending_lock);
90 rxe_dbg_dev(rxe, "unable to find pending mmap info\n");
91 spin_unlock_bh(&rxe->pending_lock);
96 list_del_init(&ip->pending_mmaps);
97 spin_unlock_bh(&rxe->pending_lock);
99 ret = remap_vmalloc_range(vma, ip->obj, 0);
101 rxe_dbg_dev(rxe, "err %d from remap_vmalloc_range\n", ret);
105 vma->vm_ops = &rxe_vm_ops;
106 vma->vm_private_data = ip;
113 * Allocate information for rxe_mmap
115 struct rxe_mmap_info *rxe_create_mmap_info(struct rxe_dev *rxe, u32 size,
116 struct ib_udata *udata, void *obj)
118 struct rxe_mmap_info *ip;
121 return ERR_PTR(-EINVAL);
123 ip = kmalloc(sizeof(*ip), GFP_KERNEL);
125 return ERR_PTR(-ENOMEM);
127 size = PAGE_ALIGN(size);
129 spin_lock_bh(&rxe->mmap_offset_lock);
131 if (rxe->mmap_offset == 0)
132 rxe->mmap_offset = ALIGN(PAGE_SIZE, SHMLBA);
134 ip->info.offset = rxe->mmap_offset;
135 rxe->mmap_offset += ALIGN(size, SHMLBA);
137 spin_unlock_bh(&rxe->mmap_offset_lock);
139 INIT_LIST_HEAD(&ip->pending_mmaps);
140 ip->info.size = size;
142 container_of(udata, struct uverbs_attr_bundle, driver_udata)