Path: blob/master/drivers/infiniband/hw/ipath/ipath_user_pages.c
15112 views
/*1* Copyright (c) 2006, 2007 QLogic Corporation. All rights reserved.2* Copyright (c) 2003, 2004, 2005, 2006 PathScale, Inc. All rights reserved.3*4* This software is available to you under a choice of one of two5* licenses. You may choose to be licensed under the terms of the GNU6* General Public License (GPL) Version 2, available from the file7* COPYING in the main directory of this source tree, or the8* OpenIB.org BSD license below:9*10* Redistribution and use in source and binary forms, with or11* without modification, are permitted provided that the following12* conditions are met:13*14* - Redistributions of source code must retain the above15* copyright notice, this list of conditions and the following16* disclaimer.17*18* - Redistributions in binary form must reproduce the above19* copyright notice, this list of conditions and the following20* disclaimer in the documentation and/or other materials21* provided with the distribution.22*23* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,24* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF25* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND26* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS27* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN28* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN29* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE30* SOFTWARE.31*/3233#include <linux/mm.h>34#include <linux/device.h>35#include <linux/slab.h>36#include <linux/sched.h>3738#include "ipath_kernel.h"3940static void __ipath_release_user_pages(struct page **p, size_t num_pages,41int dirty)42{43size_t i;4445for (i = 0; i < num_pages; i++) {46ipath_cdbg(MM, "%lu/%lu put_page %p\n", (unsigned long) i,47(unsigned long) num_pages, p[i]);48if (dirty)49set_page_dirty_lock(p[i]);50put_page(p[i]);51}52}5354/* call with current->mm->mmap_sem held */55static int __ipath_get_user_pages(unsigned long start_page, size_t num_pages,56struct page **p, struct vm_area_struct **vma)57{58unsigned long lock_limit;59size_t got;60int ret;6162lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;6364if (num_pages > lock_limit) {65ret = -ENOMEM;66goto bail;67}6869ipath_cdbg(VERBOSE, "pin %lx pages from vaddr %lx\n",70(unsigned long) num_pages, start_page);7172for (got = 0; got < num_pages; got += ret) {73ret = get_user_pages(current, current->mm,74start_page + got * PAGE_SIZE,75num_pages - got, 1, 1,76p + got, vma);77if (ret < 0)78goto bail_release;79}8081current->mm->locked_vm += num_pages;8283ret = 0;84goto bail;8586bail_release:87__ipath_release_user_pages(p, got, 0);88bail:89return ret;90}9192/**93* ipath_map_page - a safety wrapper around pci_map_page()94*95* A dma_addr of all 0's is interpreted by the chip as "disabled".96* Unfortunately, it can also be a valid dma_addr returned on some97* architectures.98*99* The powerpc iommu assigns dma_addrs in ascending order, so we don't100* have to bother with retries or mapping a dummy page to insure we101* don't just get the same mapping again.102*103* I'm sure we won't be so lucky with other iommu's, so FIXME.104*/105dma_addr_t ipath_map_page(struct pci_dev *hwdev, struct page *page,106unsigned long offset, size_t size, int direction)107{108dma_addr_t phys;109110phys = pci_map_page(hwdev, page, offset, size, direction);111112if (phys == 0) {113pci_unmap_page(hwdev, phys, size, direction);114phys = pci_map_page(hwdev, page, offset, size, direction);115/*116* FIXME: If we get 0 again, we should keep this page,117* map another, then free the 0 page.118*/119}120121return phys;122}123124/**125* ipath_map_single - a safety wrapper around pci_map_single()126*127* Same idea as ipath_map_page().128*/129dma_addr_t ipath_map_single(struct pci_dev *hwdev, void *ptr, size_t size,130int direction)131{132dma_addr_t phys;133134phys = pci_map_single(hwdev, ptr, size, direction);135136if (phys == 0) {137pci_unmap_single(hwdev, phys, size, direction);138phys = pci_map_single(hwdev, ptr, size, direction);139/*140* FIXME: If we get 0 again, we should keep this page,141* map another, then free the 0 page.142*/143}144145return phys;146}147148/**149* ipath_get_user_pages - lock user pages into memory150* @start_page: the start page151* @num_pages: the number of pages152* @p: the output page structures153*154* This function takes a given start page (page aligned user virtual155* address) and pins it and the following specified number of pages. For156* now, num_pages is always 1, but that will probably change at some point157* (because caller is doing expected sends on a single virtually contiguous158* buffer, so we can do all pages at once).159*/160int ipath_get_user_pages(unsigned long start_page, size_t num_pages,161struct page **p)162{163int ret;164165down_write(¤t->mm->mmap_sem);166167ret = __ipath_get_user_pages(start_page, num_pages, p, NULL);168169up_write(¤t->mm->mmap_sem);170171return ret;172}173174void ipath_release_user_pages(struct page **p, size_t num_pages)175{176down_write(¤t->mm->mmap_sem);177178__ipath_release_user_pages(p, num_pages, 1);179180current->mm->locked_vm -= num_pages;181182up_write(¤t->mm->mmap_sem);183}184185struct ipath_user_pages_work {186struct work_struct work;187struct mm_struct *mm;188unsigned long num_pages;189};190191static void user_pages_account(struct work_struct *_work)192{193struct ipath_user_pages_work *work =194container_of(_work, struct ipath_user_pages_work, work);195196down_write(&work->mm->mmap_sem);197work->mm->locked_vm -= work->num_pages;198up_write(&work->mm->mmap_sem);199mmput(work->mm);200kfree(work);201}202203void ipath_release_user_pages_on_close(struct page **p, size_t num_pages)204{205struct ipath_user_pages_work *work;206struct mm_struct *mm;207208__ipath_release_user_pages(p, num_pages, 1);209210mm = get_task_mm(current);211if (!mm)212return;213214work = kmalloc(sizeof(*work), GFP_KERNEL);215if (!work)216goto bail_mm;217218INIT_WORK(&work->work, user_pages_account);219work->mm = mm;220work->num_pages = num_pages;221222queue_work(ib_wq, &work->work);223return;224225bail_mm:226mmput(mm);227return;228}229230231