Path: blob/master/drivers/infiniband/hw/qib/qib_user_pages.c
15112 views
/*1* Copyright (c) 2006, 2007, 2008, 2009 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>3536#include "qib.h"3738static void __qib_release_user_pages(struct page **p, size_t num_pages,39int dirty)40{41size_t i;4243for (i = 0; i < num_pages; i++) {44if (dirty)45set_page_dirty_lock(p[i]);46put_page(p[i]);47}48}4950/*51* Call with current->mm->mmap_sem held.52*/53static int __qib_get_user_pages(unsigned long start_page, size_t num_pages,54struct page **p, struct vm_area_struct **vma)55{56unsigned long lock_limit;57size_t got;58int ret;5960lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;6162if (num_pages > lock_limit && !capable(CAP_IPC_LOCK)) {63ret = -ENOMEM;64goto bail;65}6667for (got = 0; got < num_pages; got += ret) {68ret = get_user_pages(current, current->mm,69start_page + got * PAGE_SIZE,70num_pages - got, 1, 1,71p + got, vma);72if (ret < 0)73goto bail_release;74}7576current->mm->locked_vm += num_pages;7778ret = 0;79goto bail;8081bail_release:82__qib_release_user_pages(p, got, 0);83bail:84return ret;85}8687/**88* qib_map_page - a safety wrapper around pci_map_page()89*90* A dma_addr of all 0's is interpreted by the chip as "disabled".91* Unfortunately, it can also be a valid dma_addr returned on some92* architectures.93*94* The powerpc iommu assigns dma_addrs in ascending order, so we don't95* have to bother with retries or mapping a dummy page to insure we96* don't just get the same mapping again.97*98* I'm sure we won't be so lucky with other iommu's, so FIXME.99*/100dma_addr_t qib_map_page(struct pci_dev *hwdev, struct page *page,101unsigned long offset, size_t size, int direction)102{103dma_addr_t phys;104105phys = pci_map_page(hwdev, page, offset, size, direction);106107if (phys == 0) {108pci_unmap_page(hwdev, phys, size, direction);109phys = pci_map_page(hwdev, page, offset, size, direction);110/*111* FIXME: If we get 0 again, we should keep this page,112* map another, then free the 0 page.113*/114}115116return phys;117}118119/**120* qib_get_user_pages - lock user pages into memory121* @start_page: the start page122* @num_pages: the number of pages123* @p: the output page structures124*125* This function takes a given start page (page aligned user virtual126* address) and pins it and the following specified number of pages. For127* now, num_pages is always 1, but that will probably change at some point128* (because caller is doing expected sends on a single virtually contiguous129* buffer, so we can do all pages at once).130*/131int qib_get_user_pages(unsigned long start_page, size_t num_pages,132struct page **p)133{134int ret;135136down_write(¤t->mm->mmap_sem);137138ret = __qib_get_user_pages(start_page, num_pages, p, NULL);139140up_write(¤t->mm->mmap_sem);141142return ret;143}144145void qib_release_user_pages(struct page **p, size_t num_pages)146{147if (current->mm) /* during close after signal, mm can be NULL */148down_write(¤t->mm->mmap_sem);149150__qib_release_user_pages(p, num_pages, 1);151152if (current->mm) {153current->mm->locked_vm -= num_pages;154up_write(¤t->mm->mmap_sem);155}156}157158159