163fa15dbSBob Pearson // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
28700e3e7SMoni Shoua /*
38700e3e7SMoni Shoua * Copyright (c) 2016 Mellanox Technologies Ltd. All rights reserved.
48700e3e7SMoni Shoua * Copyright (c) 2015 System Fabric Works, Inc. All rights reserved.
58700e3e7SMoni Shoua */
68700e3e7SMoni Shoua
78700e3e7SMoni Shoua #include <linux/vmalloc.h>
88700e3e7SMoni Shoua #include <linux/mm.h>
98700e3e7SMoni Shoua #include <linux/errno.h>
10ff23dfa1SShamir Rabinovitch #include <rdma/uverbs_ioctl.h>
118700e3e7SMoni Shoua
128700e3e7SMoni Shoua #include "rxe.h"
138700e3e7SMoni Shoua #include "rxe_loc.h"
148700e3e7SMoni Shoua #include "rxe_queue.h"
158700e3e7SMoni Shoua
rxe_mmap_release(struct kref * ref)168700e3e7SMoni Shoua void rxe_mmap_release(struct kref *ref)
178700e3e7SMoni Shoua {
188700e3e7SMoni Shoua struct rxe_mmap_info *ip = container_of(ref,
198700e3e7SMoni Shoua struct rxe_mmap_info, ref);
208700e3e7SMoni Shoua struct rxe_dev *rxe = to_rdev(ip->context->device);
218700e3e7SMoni Shoua
228700e3e7SMoni Shoua spin_lock_bh(&rxe->pending_lock);
238700e3e7SMoni Shoua
248700e3e7SMoni Shoua if (!list_empty(&ip->pending_mmaps))
258700e3e7SMoni Shoua list_del(&ip->pending_mmaps);
268700e3e7SMoni Shoua
278700e3e7SMoni Shoua spin_unlock_bh(&rxe->pending_lock);
288700e3e7SMoni Shoua
298700e3e7SMoni Shoua vfree(ip->obj); /* buf */
308700e3e7SMoni Shoua kfree(ip);
318700e3e7SMoni Shoua }
328700e3e7SMoni Shoua
338700e3e7SMoni Shoua /*
348700e3e7SMoni Shoua * open and close keep track of how many times the memory region is mapped,
358700e3e7SMoni Shoua * to avoid releasing it.
368700e3e7SMoni Shoua */
rxe_vma_open(struct vm_area_struct * vma)378700e3e7SMoni Shoua static void rxe_vma_open(struct vm_area_struct *vma)
388700e3e7SMoni Shoua {
398700e3e7SMoni Shoua struct rxe_mmap_info *ip = vma->vm_private_data;
408700e3e7SMoni Shoua
418700e3e7SMoni Shoua kref_get(&ip->ref);
428700e3e7SMoni Shoua }
438700e3e7SMoni Shoua
rxe_vma_close(struct vm_area_struct * vma)448700e3e7SMoni Shoua static void rxe_vma_close(struct vm_area_struct *vma)
458700e3e7SMoni Shoua {
468700e3e7SMoni Shoua struct rxe_mmap_info *ip = vma->vm_private_data;
478700e3e7SMoni Shoua
488700e3e7SMoni Shoua kref_put(&ip->ref, rxe_mmap_release);
498700e3e7SMoni Shoua }
508700e3e7SMoni Shoua
51b4fbec96SKamal Heib static const struct vm_operations_struct rxe_vm_ops = {
528700e3e7SMoni Shoua .open = rxe_vma_open,
538700e3e7SMoni Shoua .close = rxe_vma_close,
548700e3e7SMoni Shoua };
558700e3e7SMoni Shoua
568700e3e7SMoni Shoua /**
578700e3e7SMoni Shoua * rxe_mmap - create a new mmap region
588700e3e7SMoni Shoua * @context: the IB user context of the process making the mmap() call
598700e3e7SMoni Shoua * @vma: the VMA to be initialized
608700e3e7SMoni Shoua * Return zero if the mmap is OK. Otherwise, return an errno.
618700e3e7SMoni Shoua */
rxe_mmap(struct ib_ucontext * context,struct vm_area_struct * vma)628700e3e7SMoni Shoua int rxe_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
638700e3e7SMoni Shoua {
648700e3e7SMoni Shoua struct rxe_dev *rxe = to_rdev(context->device);
658700e3e7SMoni Shoua unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
668700e3e7SMoni Shoua unsigned long size = vma->vm_end - vma->vm_start;
678700e3e7SMoni Shoua struct rxe_mmap_info *ip, *pp;
688700e3e7SMoni Shoua int ret;
698700e3e7SMoni Shoua
708700e3e7SMoni Shoua /*
718700e3e7SMoni Shoua * Search the device's list of objects waiting for a mmap call.
728700e3e7SMoni Shoua * Normally, this list is very short since a call to create a
738700e3e7SMoni Shoua * CQ, QP, or SRQ is soon followed by a call to mmap().
748700e3e7SMoni Shoua */
758700e3e7SMoni Shoua spin_lock_bh(&rxe->pending_lock);
768700e3e7SMoni Shoua list_for_each_entry_safe(ip, pp, &rxe->pending_mmaps, pending_mmaps) {
778700e3e7SMoni Shoua if (context != ip->context || (__u64)offset != ip->info.offset)
788700e3e7SMoni Shoua continue;
798700e3e7SMoni Shoua
808700e3e7SMoni Shoua /* Don't allow a mmap larger than the object. */
818700e3e7SMoni Shoua if (size > ip->info.size) {
82*a9fb3287SBob Pearson rxe_dbg_dev(rxe, "mmap region is larger than the object!\n");
838700e3e7SMoni Shoua spin_unlock_bh(&rxe->pending_lock);
848700e3e7SMoni Shoua ret = -EINVAL;
858700e3e7SMoni Shoua goto done;
868700e3e7SMoni Shoua }
878700e3e7SMoni Shoua
888700e3e7SMoni Shoua goto found_it;
898700e3e7SMoni Shoua }
90*a9fb3287SBob Pearson rxe_dbg_dev(rxe, "unable to find pending mmap info\n");
918700e3e7SMoni Shoua spin_unlock_bh(&rxe->pending_lock);
928700e3e7SMoni Shoua ret = -EINVAL;
938700e3e7SMoni Shoua goto done;
948700e3e7SMoni Shoua
958700e3e7SMoni Shoua found_it:
968700e3e7SMoni Shoua list_del_init(&ip->pending_mmaps);
978700e3e7SMoni Shoua spin_unlock_bh(&rxe->pending_lock);
988700e3e7SMoni Shoua
998700e3e7SMoni Shoua ret = remap_vmalloc_range(vma, ip->obj, 0);
1008700e3e7SMoni Shoua if (ret) {
101*a9fb3287SBob Pearson rxe_dbg_dev(rxe, "err %d from remap_vmalloc_range\n", ret);
1028700e3e7SMoni Shoua goto done;
1038700e3e7SMoni Shoua }
1048700e3e7SMoni Shoua
1058700e3e7SMoni Shoua vma->vm_ops = &rxe_vm_ops;
1068700e3e7SMoni Shoua vma->vm_private_data = ip;
1078700e3e7SMoni Shoua rxe_vma_open(vma);
1088700e3e7SMoni Shoua done:
1098700e3e7SMoni Shoua return ret;
1108700e3e7SMoni Shoua }
1118700e3e7SMoni Shoua
1128700e3e7SMoni Shoua /*
1138700e3e7SMoni Shoua * Allocate information for rxe_mmap
1148700e3e7SMoni Shoua */
rxe_create_mmap_info(struct rxe_dev * rxe,u32 size,struct ib_udata * udata,void * obj)115ff23dfa1SShamir Rabinovitch struct rxe_mmap_info *rxe_create_mmap_info(struct rxe_dev *rxe, u32 size,
116ff23dfa1SShamir Rabinovitch struct ib_udata *udata, void *obj)
1178700e3e7SMoni Shoua {
1188700e3e7SMoni Shoua struct rxe_mmap_info *ip;
1198700e3e7SMoni Shoua
120ff23dfa1SShamir Rabinovitch if (!udata)
121ff23dfa1SShamir Rabinovitch return ERR_PTR(-EINVAL);
122ff23dfa1SShamir Rabinovitch
1238700e3e7SMoni Shoua ip = kmalloc(sizeof(*ip), GFP_KERNEL);
1248700e3e7SMoni Shoua if (!ip)
125bb43c8e3SSudip Mukherjee return ERR_PTR(-ENOMEM);
1268700e3e7SMoni Shoua
1278700e3e7SMoni Shoua size = PAGE_ALIGN(size);
1288700e3e7SMoni Shoua
1298700e3e7SMoni Shoua spin_lock_bh(&rxe->mmap_offset_lock);
1308700e3e7SMoni Shoua
1318700e3e7SMoni Shoua if (rxe->mmap_offset == 0)
132cb886455SJason Gunthorpe rxe->mmap_offset = ALIGN(PAGE_SIZE, SHMLBA);
1338700e3e7SMoni Shoua
1348700e3e7SMoni Shoua ip->info.offset = rxe->mmap_offset;
135cb886455SJason Gunthorpe rxe->mmap_offset += ALIGN(size, SHMLBA);
1368700e3e7SMoni Shoua
1378700e3e7SMoni Shoua spin_unlock_bh(&rxe->mmap_offset_lock);
1388700e3e7SMoni Shoua
1398700e3e7SMoni Shoua INIT_LIST_HEAD(&ip->pending_mmaps);
1408700e3e7SMoni Shoua ip->info.size = size;
141ff23dfa1SShamir Rabinovitch ip->context =
142ff23dfa1SShamir Rabinovitch container_of(udata, struct uverbs_attr_bundle, driver_udata)
143ff23dfa1SShamir Rabinovitch ->context;
1448700e3e7SMoni Shoua ip->obj = obj;
1458700e3e7SMoni Shoua kref_init(&ip->ref);
1468700e3e7SMoni Shoua
1478700e3e7SMoni Shoua return ip;
1488700e3e7SMoni Shoua }
149