get_user_pages() cleanup
Subsystems (24)
Jan Kara
reviewercover get_user_pages() cleanup
Hello,
In my quest for changing locking around page faults to make things easier for
filesystems I found out get_user_pages() users could use a cleanup. The knowledge about necessary locking for get_user_pages() is in tons of places in drivers and quite a few of them actually get it wrong (don't have mmap_sem when calling get_user_pages() or hold mmap_sem when calling copy_from_user() in the surrounding code). Rather often this actually doesn't seem necessary. This patch series converts lots of places to use either get_user_pages_fast() or a new simple wrapper get_user_pages_unlocked() to remove the knowledge of mmap_sem from the drivers. I'm still looking into converting a few remaining drivers (most notably v4l2) which are more complex.
As I already wrote, in some cases I actually think drivers were buggy (and I note that in corresponding changelogs). I would really like to ask respective maintainers to have a look at the patches in their area. Also any other comments are welcome. Thanks.
Honza
PS: Sorry for the huge recipient list but I don't really know how to trim it
down...
--
To unsubscribe, send a message with 'unsubscribe linux-aio' in the body to [email protected]. For more info on Linux AIO,
see: http://www.kvack.org/aio/
Don't email: <a href=mailto:"[email protected]">[email protected]</a>
Jan Kara
reviewerPATCH 2/26 ia64: Use get_user_pages_fast() in err_inject.c
Convert get_user_pages() call to get_user_pages_fast(). This actually fixes an apparent bug where get_user_pages() has been called without mmap_sem for an arbitrary user-provided address.
--- arch/ia64/kernel/err_inject.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
arch/ia64/kernel/err_inject.c +1 -2
@@ -142,8 +142,7 @@ store_virtual_to_phys(struct device *dev, struct device_attribute *attr,u64 virt_addr=simple_strtoull (buf,NULL ,16 );int ret; - ret =get_user_pages (current, current->mm, virt_addr, -1 ,VM_READ ,0 ,NULL ,NULL ); + ret =get_user_pages_fast (virt_addr,1 ,VM_READ ,NULL );if (ret<=0 ) {#ifdef ERR_INJ_DEBUG printk ("Virtual address %lx is not existing.\n" ,virt_addr);
Signature · 1 line
1.8.1.4
Jan Kara
reviewerPATCH 4/26 drm: Convert via driver to use get_user_pages_fast()
In reply to Jan Kara--- drivers/gpu/drm/via/via_dmablit.c | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-)
drivers/gpu/drm/via/via_dmablit.c +4 -8
@@ -238,14 +238,10 @@ via_lock_all_dma_pages(drm_via_sg_info_t *vsg, drm_via_dmablit_t *xfer) vsg->pages =vzalloc (sizeof (struct page *) * vsg->num_pages);if (NULL == vsg->pages)return -ENOMEM ; -down_read (¤t->mm->mmap_sem); - ret =get_user_pages (current, current->mm, - (unsigned long )xfer->mem_addr, - vsg->num_pages, - (vsg->direction ==DMA_FROM_DEVICE ), -0 , vsg->pages,NULL ); - -up_read (¤t->mm->mmap_sem); + ret =get_user_pages_fast ((unsigned long )xfer->mem_addr, + vsg->num_pages, + (vsg->direction ==DMA_FROM_DEVICE ), + vsg->pages);if (ret != vsg->num_pages) {if (ret <0 )return ret;
Signature · 7 lines
1.8.1.4 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Jan Kara
reviewerPATCH 7/26 st: Convert sgl_map_user_pages() to use get_user_pages_fast()
In reply to Jan Kara--- drivers/scsi/st.c | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-)
drivers/scsi/st.c +5 -13
@@ -4514,19 +4514,11 @@ static int sgl_map_user_pages(struct st_buffer *STbp,if ((pages =kmalloc (max_pages *sizeof (*pages),GFP_KERNEL )) ==NULL )return -ENOMEM ; -/* Try to fault in all of the necessary pages */ -down_read (¤t->mm->mmap_sem); -/* rw==READ means read from drive, write into memory area */ - res =get_user_pages ( - current, - current->mm, - uaddr, - nr_pages, - rw ==READ , -0 ,/* don't force */ - pages, -NULL ); -up_read (¤t->mm->mmap_sem); +/* +* Try to fault in all of the necessary pages. rw==READ means read +* from drive, write into memory area. +*/ + res =get_user_pages_fast (uaddr, nr_pages, rw ==READ , pages);/* Errors and no page mapped should return here */ if (res < nr_pages)
Signature · 7 lines
1.8.1.4 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Jan Kara
reviewerPATCH 12/26 pvr2fb: Convert pvr2fb_write() to use get_user_pages_fast()
In reply to Jan Kara--- drivers/video/pvr2fb.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-)
drivers/video/pvr2fb.c +1 -5
@@ -686,11 +686,7 @@ static ssize_t pvr2fb_write(struct fb_info *info, const char *buf,if (!pages)return -ENOMEM ; -down_read (¤t->mm->mmap_sem); - ret =get_user_pages (current, current->mm, (unsigned long )buf, - nr_pages,WRITE ,0 , pages,NULL ); -up_read (¤t->mm->mmap_sem); - + ret =get_user_pages_fast ((unsigned long )buf, nr_pages,WRITE , pages);if (ret < nr_pages) { nr_pages = ret; ret = -EINVAL ;
Signature · 1 line
1.8.1.4
Jan Kara
reviewerPATCH 15/26 ceph: Convert ceph_get_direct_page_vector() to get_user_pages_fast()
In reply to Jan Kara--- net/ceph/pagevec.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
net/ceph/pagevec.c +2 -4
@@ -23,17 +23,15 @@ struct page **ceph_get_direct_page_vector(const void __user *data,if (!pages)return ERR_PTR (-ENOMEM ); -down_read (¤t->mm->mmap_sem);while (got < num_pages) { - rc =get_user_pages (current, current->mm, + rc =get_user_pages_fast ( (unsigned long )data + ((unsigned long )got *PAGE_SIZE ), - num_pages - got, write_page,0 , pages + got,NULL ); + num_pages - got, write_page, pages + got);if (rc <0 )break ;BUG_ON (rc ==0 ); got += rc; } -up_read (¤t->mm->mmap_sem);if (rc <0 ) goto fail;return pages;
Signature · 7 lines
1.8.1.4 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Jan Kara
reviewerPATCH 17/26 kvm: Use get_user_pages_unlocked() in async_pf_execute()
In reply to Jan Kara--- virt/kvm/async_pf.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
virt/kvm/async_pf.c +1 -3
@@ -67,9 +67,7 @@ static void async_pf_execute(struct work_struct *work)might_sleep ();use_mm (mm); -down_read (&mm->mmap_sem); -get_user_pages (current, mm, addr,1 ,1 ,0 , &page,NULL ); -up_read (&mm->mmap_sem); +get_user_pages_unlocked (current, mm, addr,1 ,1 ,0 , &page);unuse_mm (mm);spin_lock (&vcpu->async_pf.lock);
Signature · 7 lines
1.8.1.4 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Jan Kara
reviewerPATCH 20/26 ib: Convert ib_umem_get() to get_user_pages_unlocked()
In reply to Jan KaraConvert ib_umem_get() to use get_user_pages_unlocked(). This significantly shortens the section where mmap_sem is held (we only need it for updating of mm->pinned_vm and inside get_user_pages()) and removes the knowledge about locking of get_user_pages().
--- drivers/infiniband/core/umem.c | 41 +++++++++++++++++------------------------ 1 file changed, 17 insertions(+), 24 deletions(-)
drivers/infiniband/core/umem.c +17 -24
@@ -80,7 +80,6 @@ struct ib_umem *ib_umem_get(struct ib_ucontext *context, unsigned long addr, {struct ib_umem *umem;struct page **page_list; -struct vm_area_struct **vma_list;struct ib_umem_chunk *chunk;unsigned long locked;unsigned long lock_limit;
@@ -125,34 +124,31 @@ struct ib_umem *ib_umem_get(struct ib_ucontext *context, unsigned long addr,return ERR_PTR (-ENOMEM ); } -/* -* if we can't alloc the vma_list, it's not so bad; -* just assume the memory is not hugetlb memory -*/ - vma_list = (struct vm_area_struct **)__get_free_page (GFP_KERNEL ); -if (!vma_list) - umem->hugetlb =0 ; - npages =PAGE_ALIGN (size + umem->offset) >>PAGE_SHIFT ;down_write (¤t->mm->mmap_sem); - locked = npages + current->mm->pinned_vm; lock_limit =rlimit (RLIMIT_MEMLOCK ) >>PAGE_SHIFT ; - -if ((locked > lock_limit) && !capable (CAP_IPC_LOCK )) { - ret = -ENOMEM ; - goto out; + locked = npages; +if (npages + current->mm->pinned_vm > lock_limit && + !capable (CAP_IPC_LOCK )) { +up_write (¤t->mm->mmap_sem); +kfree (umem); +free_page ((unsigned long ) page_list); +return ERR_PTR (-ENOMEM ); } + current->mm->pinned_vm += npages; + +up_write (¤t->mm->mmap_sem); cur_base = addr &PAGE_MASK ; ret =0 ;while (npages) { - ret =get_user_pages (current, current->mm, cur_base, + ret =get_user_pages_unlocked (current, current->mm, cur_base, min_t(unsigned long, npages,PAGE_SIZE /sizeof (struct page *)), -1 , !umem->writable, page_list, vma_list); +1 , !umem->writable, page_list);if (ret <0 ) goto out;
@@ -174,8 +170,7 @@ struct ib_umem *ib_umem_get(struct ib_ucontext *context, unsigned long addr, chunk->nents =min_t (int, ret,IB_UMEM_MAX_PAGE_CHUNK );sg_init_table (chunk->page_list, chunk->nents);for (i =0 ; i < chunk->nents; ++i) { -if (vma_list && - !is_vm_hugetlb_page (vma_list[i + off])) +if (!PageHuge (page_list[i + off])) umem->hugetlb =0 ;sg_set_page (&chunk->page_list[i], page_list[i + off],PAGE_SIZE ,0 ); }
@@ -206,12 +201,10 @@ out:if (ret <0 ) {__ib_umem_release (context->device, umem,0 );kfree (umem); - }else - current->mm->pinned_vm = locked; - -up_write (¤t->mm->mmap_sem); -if (vma_list) -free_page ((unsigned long ) vma_list); +down_write (¤t->mm->mmap_sem); + current->mm->pinned_vm -= locked; +up_write (¤t->mm->mmap_sem); + }free_page ((unsigned long ) page_list);return ret <0 ?ERR_PTR (ret) : umem;
Signature · 7 lines
1.8.1.4 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Jan Kara
reviewerPATCH 21/26 ib: Convert ipath_get_user_pages() to get_user_pages_unlocked()
In reply to Jan KaraConvert ipath_get_user_pages() to use get_user_pages_unlocked(). This shortens the section where we hold mmap_sem for writing and also removes the knowledge about get_user_pages() locking from ipath driver. We also fix a bug in testing pinned number of pages when changing the code.
--- drivers/infiniband/hw/ipath/ipath_user_pages.c | 62 +++++++++++--------------- 1 file changed, 27 insertions(+), 35 deletions(-)
drivers/infiniband/hw/ipath/ipath_user_pages.c +27 -35
@@ -52,40 +52,58 @@ static void __ipath_release_user_pages(struct page **p, size_t num_pages, } } -/* call with current->mm->mmap_sem held */ -static int __ipath_get_user_pages (unsigned long start_page,size_t num_pages, -struct page **p,struct vm_area_struct **vma) +/** +* ipath_get_user_pages - lock user pages into memory +* @start_page: the start page +* @num_pages: the number of pages +* @p: the output page structures +* +* This function takes a given start page (page aligned user virtual +* address) and pins it and the following specified number of pages. For +* now, num_pages is always 1, but that will probably change at some point +* (because caller is doing expected sends on a single virtually contiguous +* buffer, so we can do all pages at once). +*/ +int ipath_get_user_pages (unsigned long start_page,size_t num_pages, +struct page **p) {unsigned long lock_limit;size_t got;int ret; +down_write (¤t->mm->mmap_sem); lock_limit =rlimit (RLIMIT_MEMLOCK ) >>PAGE_SHIFT ; -if (num_pages > lock_limit) { +if (current->mm->pinned_vm + num_pages > lock_limit && + !capable (CAP_IPC_LOCK )) { +up_write (¤t->mm->mmap_sem); ret = -ENOMEM ; goto bail; } + current->mm->pinned_vm += num_pages; +up_write (¤t->mm->mmap_sem);ipath_cdbg (VERBOSE ,"pin %lx pages from vaddr %lx\n" , (unsigned long ) num_pages, start_page);for (got =0 ; got < num_pages; got += ret) { - ret =get_user_pages (current, current->mm, - start_page + got *PAGE_SIZE , - num_pages - got,1 ,1 , - p + got, vma); + ret =get_user_pages_unlocked (current, current->mm, + start_page + got *PAGE_SIZE , + num_pages - got,1 ,1 , + p + got);if (ret <0 ) goto bail_release; } - current->mm->pinned_vm += num_pages; ret =0 ; goto bail; bail_release:__ipath_release_user_pages (p, got,0 ); +down_write (¤t->mm->mmap_sem); + current->mm->pinned_vm -= num_pages; +up_write (¤t->mm->mmap_sem); bail:return ret; }
@@ -146,32 +164,6 @@ dma_addr_t ipath_map_single(struct pci_dev *hwdev, void *ptr, size_t size,return phys; } -/** -* ipath_get_user_pages - lock user pages into memory -* @start_page: the start page -* @num_pages: the number of pages -* @p: the output page structures -* -* This function takes a given start page (page aligned user virtual -* address) and pins it and the following specified number of pages. For -* now, num_pages is always 1, but that will probably change at some point -* (because caller is doing expected sends on a single virtually contiguous -* buffer, so we can do all pages at once). -*/ -int ipath_get_user_pages (unsigned long start_page,size_t num_pages, -struct page **p) -{ -int ret; - -down_write (¤t->mm->mmap_sem); - - ret =__ipath_get_user_pages (start_page, num_pages, p,NULL ); - -up_write (¤t->mm->mmap_sem); - -return ret; -} -void ipath_release_user_pages (struct page **p,size_t num_pages) {down_write (¤t->mm->mmap_sem);
Signature · 7 lines
1.8.1.4 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Jan Kara
reviewerPATCH 22/26 ib: Convert ipath_user_sdma_pin_pages() to use get_user_pages_unlocked()
In reply to Jan KaraFunction ipath_user_sdma_queue_pkts() gets called with mmap_sem held for writing. Except for get_user_pages() deep down in ipath_user_sdma_pin_pages() we don't seem to need mmap_sem at all. Even more interestingly the function ipath_user_sdma_queue_pkts() (and also ipath_user_sdma_coalesce() called somewhat later) call copy_from_user() which can hit a page fault and we deadlock on trying to get mmap_sem when handling that fault. So just make ipath_user_sdma_pin_pages() use get_user_pages_unlocked() and leave mmap_sem locking for mm.
--- drivers/infiniband/hw/ipath/ipath_user_sdma.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-)
drivers/infiniband/hw/ipath/ipath_user_sdma.c +2 -5
@@ -280,8 +280,8 @@ static int ipath_user_sdma_pin_pages(const struct ipath_devdata *dd,int j;int ret; - ret =get_user_pages (current, current->mm, addr, - npages,0 ,1 , pages,NULL ); + ret =get_user_pages_unlocked (current, current->mm, addr, + npages,0 ,1 , pages);if (ret != npages) {int i;
@@ -811,10 +811,7 @@ int ipath_user_sdma_writev(struct ipath_devdata *dd,while (dim) {const int mxp =8 ; -down_write (¤t->mm->mmap_sem); ret =ipath_user_sdma_queue_pkts (dd, pq, &list, iov, dim, mxp); -up_write (¤t->mm->mmap_sem); -if (ret <=0 ) goto done_unlock;else {
Signature · 7 lines
1.8.1.4 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Jan Kara
reviewerPATCH 23/26 ib: Convert qib_get_user_pages() to get_user_pages_unlocked()
In reply to Jan KaraConvert qib_get_user_pages() to use get_user_pages_unlocked(). This shortens the section where we hold mmap_sem for writing and also removes the knowledge about get_user_pages() locking from ipath driver. We also fix a bug in testing pinned number of pages when changing the code.
--- drivers/infiniband/hw/qib/qib_user_pages.c | 62 +++++++++++++----------------- 1 file changed, 26 insertions(+), 36 deletions(-)
drivers/infiniband/hw/qib/qib_user_pages.c +26 -36
@@ -48,39 +48,55 @@ static void __qib_release_user_pages(struct page **p, size_t num_pages, } } -/* -* Call with current->mm->mmap_sem held. +/** +* qib_get_user_pages - lock user pages into memory +* @start_page: the start page +* @num_pages: the number of pages +* @p: the output page structures +* +* This function takes a given start page (page aligned user virtual +* address) and pins it and the following specified number of pages. For +* now, num_pages is always 1, but that will probably change at some point +* (because caller is doing expected sends on a single virtually contiguous +* buffer, so we can do all pages at once). */ -static int __qib_get_user_pages (unsigned long start_page,size_t num_pages, -struct page **p,struct vm_area_struct **vma) +int qib_get_user_pages (unsigned long start_page,size_t num_pages, +struct page **p) {unsigned long lock_limit;size_t got;int ret; +struct mm_struct *mm = current->mm; +down_write (&mm->mmap_sem); lock_limit =rlimit (RLIMIT_MEMLOCK ) >>PAGE_SHIFT ; -if (num_pages > lock_limit && !capable (CAP_IPC_LOCK )) { +if (mm->pinned_vm + num_pages > lock_limit && !capable (CAP_IPC_LOCK )) { +up_write (&mm->mmap_sem); ret = -ENOMEM ; goto bail; } + mm->pinned_vm += num_pages; +up_write (&mm->mmap_sem);for (got =0 ; got < num_pages; got += ret) { - ret =get_user_pages (current, current->mm, - start_page + got *PAGE_SIZE , - num_pages - got,1 ,1 , - p + got, vma); + ret =get_user_pages_unlocked (current, mm, + start_page + got *PAGE_SIZE , + num_pages - got,1 ,1 , + p + got);if (ret <0 ) goto bail_release; } - current->mm->pinned_vm += num_pages; ret =0 ; goto bail; bail_release:__qib_release_user_pages (p, got,0 ); +down_write (&mm->mmap_sem); + mm->pinned_vm -= num_pages; +up_write (&mm->mmap_sem); bail:return ret; }
@@ -117,32 +133,6 @@ dma_addr_t qib_map_page(struct pci_dev *hwdev, struct page *page,return phys; } -/** -* qib_get_user_pages - lock user pages into memory -* @start_page: the start page -* @num_pages: the number of pages -* @p: the output page structures -* -* This function takes a given start page (page aligned user virtual -* address) and pins it and the following specified number of pages. For -* now, num_pages is always 1, but that will probably change at some point -* (because caller is doing expected sends on a single virtually contiguous -* buffer, so we can do all pages at once). -*/ -int qib_get_user_pages (unsigned long start_page,size_t num_pages, -struct page **p) -{ -int ret; - -down_write (¤t->mm->mmap_sem); - - ret =__qib_get_user_pages (start_page, num_pages, p,NULL ); - -up_write (¤t->mm->mmap_sem); - -return ret; -} -void qib_release_user_pages (struct page **p,size_t num_pages) {if (current->mm)/* during close after signal, mm can be NULL */
Signature · 7 lines
1.8.1.4 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Jan Kara
reviewerPATCH 24/26 ib: Convert qib_user_sdma_pin_pages() to use get_user_pages_unlocked()
In reply to Jan KaraFunction qib_user_sdma_queue_pkts() gets called with mmap_sem held for writing. Except for get_user_pages() deep down in qib_user_sdma_pin_pages() we don't seem to need mmap_sem at all. Even more interestingly the function qib_user_sdma_queue_pkts() (and also qib_user_sdma_coalesce() called somewhat later) call copy_from_user() which can hit a page fault and we deadlock on trying to get mmap_sem when handling that fault. So just make qib_user_sdma_pin_pages() use get_user_pages_unlocked() and leave mmap_sem locking for mm.
--- drivers/infiniband/hw/qib/qib_user_sdma.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-)
drivers/infiniband/hw/qib/qib_user_sdma.c +2 -5
@@ -594,8 +594,8 @@ static int qib_user_sdma_pin_pages(const struct qib_devdata *dd,else j = npages; - ret =get_user_pages (current, current->mm, addr, - j,0 ,1 , pages,NULL ); + ret =get_user_pages_unlocked (current, current->mm, addr, + j,0 ,1 , pages);if (ret != j) { i =0 ; j = ret;
@@ -1294,11 +1294,8 @@ int qib_user_sdma_writev(struct qib_ctxtdata *rcd,int mxp =8 ;int ndesc =0 ; -down_write (¤t->mm->mmap_sem); ret =qib_user_sdma_queue_pkts (dd, ppd, pq, iov, dim, &list, &mxp, &ndesc); -up_write (¤t->mm->mmap_sem); -if (ret <0 ) goto done_unlock;else {
Signature · 7 lines
1.8.1.4 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Jan Kara
reviewerPATCH 25/26 ib: Convert mthca_map_user_db() to use get_user_pages_fast()
In reply to Jan KaraFunction mthca_map_user_db() appears to call get_user_pages() without holding mmap_sem. Fix the bug by using get_user_pages_fast() instead which also takes care of the locking.
--- drivers/infiniband/hw/mthca/mthca_memfree.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/infiniband/hw/mthca/mthca_memfree.c +1 -2
@@ -472,8 +472,7 @@ int mthca_map_user_db(struct mthca_dev *dev, struct mthca_uar *uar, goto out; } - ret =get_user_pages (current, current->mm, uaddr &PAGE_MASK ,1 ,1 ,0 , - pages,NULL ); + ret =get_user_pages_fast (uaddr &PAGE_MASK ,1 ,1 , pages);if (ret <0 ) goto out;
Signature · 7 lines
1.8.1.4 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Jan Kara
reviewerPATCH 26/26 aio: Remove useless get_user_pages() call
In reply to Jan Karaget_user_pages() call in aio_setup_ring() is useless these days. We create all ringbuffer the pages in page cache using find_or_create_page() anyway so we can just use the pointers we get from this function instead of having to look them up via user address space.
--- fs/aio.c | 48 +++++++++++++++++------------------------------- 1 file changed, 17 insertions(+), 31 deletions(-)
fs/aio.c +17 -31
@@ -269,9 +269,21 @@ static int aio_setup_ring(struct kioctx *ctx) file->f_inode->i_mapping->a_ops = &aio_ctx_aops; file->f_inode->i_mapping->private_data = ctx; file->f_inode->i_size =PAGE_SIZE * (loff_t )nr_pages; + ctx->aio_ring_file = file; + nr_events = (PAGE_SIZE * nr_pages -sizeof (struct aio_ring )) + /sizeof (struct io_event ); + + ctx->ring_pages = ctx->internal_pages; +if (nr_pages >AIO_RING_PAGES ) { + ctx->ring_pages =kcalloc (nr_pages,sizeof (struct page *), +GFP_KERNEL ); +if (!ctx->ring_pages) +return -ENOMEM ; + }for (i =0 ; i < nr_pages; i++) {struct page *page; + page =find_or_create_page (file->f_inode->i_mapping, i,GFP_HIGHUSER | __GFP_ZERO);if (!page)
@@ -281,19 +293,13 @@ static int aio_setup_ring(struct kioctx *ctx)SetPageUptodate (page);SetPageDirty (page);unlock_page (page); + ctx->ring_pages[i] = page; } - ctx->aio_ring_file = file; - nr_events = (PAGE_SIZE * nr_pages -sizeof (struct aio_ring )) - /sizeof (struct io_event ); - - ctx->ring_pages = ctx->internal_pages; -if (nr_pages >AIO_RING_PAGES ) { - ctx->ring_pages =kcalloc (nr_pages,sizeof (struct page *), -GFP_KERNEL ); -if (!ctx->ring_pages) -return -ENOMEM ; + ctx->nr_pages = i; +if (unlikely (ctx->nr_pages != nr_pages)) { +aio_free_ring (ctx); +return -EAGAIN ; } - ctx->mmap_size = nr_pages *PAGE_SIZE ;pr_debug ("attempting mmap of %lu bytes\n" , ctx->mmap_size);
@@ -309,28 +315,8 @@ static int aio_setup_ring(struct kioctx *ctx) }pr_debug ("mmap address: 0x%08lx\n" , ctx->mmap_base); - -/* We must do this while still holding mmap_sem for write, as we -* need to be protected against userspace attempting to mremap() -* or munmap() the ring buffer. -*/ - ctx->nr_pages =get_user_pages (current, mm, ctx->mmap_base, nr_pages, -1 ,0 , ctx->ring_pages,NULL ); - -/* Dropping the reference here is safe as the page cache will hold -* onto the pages for us. It is also required so that page migration -* can unmap the pages and get the right reference count. -*/ -for (i =0 ; i < ctx->nr_pages; i++) -put_page (ctx->ring_pages[i]); -up_write (&mm->mmap_sem); -if (unlikely (ctx->nr_pages != nr_pages)) { -aio_free_ring (ctx); -return -EAGAIN ; - } - ctx->user_id = ctx->mmap_base; ctx->nr_events = nr_events;/* trusted copy */
Signature · 7 lines
1.8.1.4 -- To unsubscribe, send a message with 'unsubscribe linux-aio' in the body to [email protected]. For more info on Linux AIO, see: http://www.kvack.org/aio/ Don't email: <a href=mailto:"[email protected]">[email protected]</a>
Jan Kara
reviewerPATCH 5/26 omap3isp: Make isp_video_buffer_prepare_user() use get_user_pages_fast()
In reply to Jan Kara--- drivers/media/platform/omap3isp/ispqueue.c | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-)
drivers/media/platform/omap3isp/ispqueue.c +3 -7
@@ -331,13 +331,9 @@ static int isp_video_buffer_prepare_user(struct isp_video_buffer *buf)if (buf->pages ==NULL )return -ENOMEM ; -down_read (¤t->mm->mmap_sem); - ret =get_user_pages (current, current->mm, data &PAGE_MASK , - buf->npages, - buf->vbuf.type ==V4L2_BUF_TYPE_VIDEO_CAPTURE ,0 , - buf->pages,NULL ); -up_read (¤t->mm->mmap_sem); - + ret =get_user_pages_fast (data &PAGE_MASK , buf->npages, + buf->vbuf.type ==V4L2_BUF_TYPE_VIDEO_CAPTURE , + buf->pages);if (ret != buf->npages) { buf->npages = ret <0 ?0 : ret;isp_video_buffer_cleanup (buf);
Signature · 1 line
1.8.1.4
Jan Kara
reviewerPATCH 19/26 ivtv: Convert driver to use get_user_pages_unlocked()
In reply to Jan Kara--- drivers/media/pci/ivtv/ivtv-udma.c | 6 ++---- drivers/media/pci/ivtv/ivtv-yuv.c | 12 ++++++------ 2 files changed, 8 insertions(+), 10 deletions(-)
drivers/media/pci/ivtv/ivtv-udma.c +2 -4
@@ -124,10 +124,8 @@ int ivtv_udma_setup(struct ivtv *itv, unsigned long ivtv_dest_addr, }/* Get user pages for DMA Xfer */ -down_read (¤t->mm->mmap_sem); - err =get_user_pages (current, current->mm, - user_dma.uaddr, user_dma.page_count,0 ,1 , dma->map,NULL ); -up_read (¤t->mm->mmap_sem); + err =get_user_pages_unlocked (current, current->mm, user_dma.uaddr, + user_dma.page_count,0 ,1 , dma->map);if (user_dma.page_count != err) { IVTV_DEBUG_WARN("failed to map user pages, returned %d instead of %d\n",
drivers/media/pci/ivtv/ivtv-yuv.c +6 -6
@@ -75,15 +75,15 @@ static int ivtv_yuv_prep_user_dma(struct ivtv *itv, struct ivtv_user_dma *dma,ivtv_udma_get_page_info (&uv_dma, (unsigned long )args->uv_source,360 * uv_decode_height);/* Get user pages for DMA Xfer */ -down_read (¤t->mm->mmap_sem); - y_pages =get_user_pages (current, current->mm, y_dma.uaddr, y_dma.page_count,0 ,1 , &dma->map[0 ],NULL ); + y_pages =get_user_pages_unlocked (current, current->mm, y_dma.uaddr, + y_dma.page_count,0 ,1 , &dma->map[0 ]); uv_pages =0 ;/* silence gcc. value is set and consumed only if: */ if (y_pages == y_dma.page_count) { - uv_pages =get_user_pages (current, current->mm, - uv_dma.uaddr, uv_dma.page_count,0 ,1 , - &dma->map[y_pages],NULL ); + uv_pages =get_user_pages_unlocked (current, current->mm, + uv_dma.uaddr, + uv_dma.page_count,0 ,1 , + &dma->map[y_pages]); } -up_read (¤t->mm->mmap_sem);if (y_pages != y_dma.page_count || uv_pages != uv_dma.page_count) {int rc = -EFAULT ;
Signature · 1 line
1.8.1.4
Jan Kara
reviewerPATCH 3/26 dma: Use get_user_pages_fast() in dma_pin_iovec_pages()
In reply to Jan Kara--- drivers/dma/iovlock.c | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-)
drivers/dma/iovlock.c +4 -11
@@ -95,17 +95,10 @@ struct dma_pinned_list *dma_pin_iovec_pages(struct iovec *iov, size_t len) pages += page_list->nr_pages;/* pin pages down */ -down_read (¤t->mm->mmap_sem); - ret =get_user_pages ( - current, - current->mm, - (unsigned long ) iov[i].iov_base, - page_list->nr_pages, -1 ,/* write */ -0 ,/* force */ - page_list->pages, -NULL ); -up_read (¤t->mm->mmap_sem); + ret =get_user_pages_fast ((unsigned long ) iov[i].iov_base, + page_list->nr_pages, +1 ,/* write */ + page_list->pages);if (ret != page_list->nr_pages) goto unpin;
Signature · 7 lines
1.8.1.4 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Jan Kara
reviewerPATCH 14/26 nfs: Convert direct IO to use get_user_pages_fast()
In reply to Jan Kara--- fs/nfs/direct.c | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-)
fs/nfs/direct.c +4 -8
@@ -337,10 +337,8 @@ static ssize_t nfs_direct_read_schedule_segment(struct nfs_pageio_descriptor *deif (!pagevec)break ;if (uio) { -down_read (¤t->mm->mmap_sem); - result =get_user_pages (current, current->mm, user_addr, - npages,1 ,0 , pagevec,NULL ); -up_read (¤t->mm->mmap_sem); + result =get_user_pages_fast (user_addr, npages,1 , + pagevec);if (result <0 )break ; }else {
@@ -658,10 +656,8 @@ static ssize_t nfs_direct_write_schedule_segment(struct nfs_pageio_descriptor *dbreak ;if (uio) { -down_read (¤t->mm->mmap_sem); - result =get_user_pages (current, current->mm, user_addr, - npages,0 ,0 , pagevec,NULL ); -up_read (¤t->mm->mmap_sem); + result =get_user_pages_fast (user_addr, npages,0 , + pagevec);if (result <0 )break ; }else {
Signature · 1 line
1.8.1.4
Jan Kara
reviewerPATCH 13/26 fsl_hypervisor: Convert ioctl_memcpy() to use get_user_pages_fast()
In reply to Jan Kara--- drivers/virt/fsl_hypervisor.c | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-)
drivers/virt/fsl_hypervisor.c +2 -7
@@ -242,13 +242,8 @@ static long ioctl_memcpy(struct fsl_hv_ioctl_memcpy __user *p) sg_list =PTR_ALIGN (sg_list_unaligned,sizeof (struct fh_sg_list ));/* Get the physical addresses of the source buffer */ -down_read (¤t->mm->mmap_sem); - num_pinned =get_user_pages (current, current->mm, - param.local_vaddr - lb_offset, num_pages, - (param.source ==-1 ) ?READ :WRITE , -0 , pages,NULL ); -up_read (¤t->mm->mmap_sem); - + num_pinned =get_user_pages_fast (param.local_vaddr - lb_offset, + num_pages, (param.source ==-1 ) ?READ :WRITE , pages);if (num_pinned != num_pages) {/* get_user_pages() failed */ pr_debug ("fsl-hv: could not lock source buffer\n" );
Signature · 7 lines
1.8.1.4 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Jan Kara
reviewerPATCH 16/26 mm: Provide get_user_pages_unlocked()
In reply to Jan KaraProvide a wrapper for get_user_pages() which takes care of acquiring and releasing mmap_sem. Using this function reduces amount of places in which we deal with mmap_sem.
--- include/linux/mm.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
include/linux/mm.h +14 -0
@@ -1031,6 +1031,20 @@ long get_user_pages(struct task_struct *tsk, struct mm_struct *mm, struct vm_area_struct **vmas);int get_user_pages_fast (unsigned long start,int nr_pages,int write,struct page **pages); +static inline long +get_user_pages_unlocked (struct task_struct *tsk,struct mm_struct *mm, +unsigned long start,unsigned long nr_pages, +int write,int force,struct page **pages) +{ +long ret; + +down_read (&mm->mmap_sem); + ret =get_user_pages (tsk, mm, start, nr_pages, write, force, pages, +NULL ); +up_read (&mm->mmap_sem); +return ret; +} +struct kvec ;int get_kernel_pages (const struct kvec *iov,int nr_pages,int write,struct page **pages);
Signature · 7 lines
1.8.1.4 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Jan Kara
reviewerPATCH 8/26 ced1401: Convert driver to use get_user_pages_fast()
In reply to Jan Kara--- drivers/staging/ced1401/ced_ioc.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)
drivers/staging/ced1401/ced_ioc.c +1 -4
@@ -692,10 +692,7 @@ static int SetArea(DEVICE_EXTENSION *pdx, int nArea, char __user *puBuf, __func__, puBuf, dwLength, bCircular);/* To pin down user pages we must first acquire the mapping semaphore. */ -down_read (¤t->mm->mmap_sem);/* get memory map semaphore */ - nPages =get_user_pages (current, current->mm, ulStart, len,1 ,0 , - pPages,NULL ); -up_read (¤t->mm->mmap_sem);/* release the semaphore */ + nPages =get_user_pages_fast (ulStart, len,1 , pPages);dev_dbg (&pdx->interface->dev,"%s nPages = %d" , __func__, nPages);if (nPages >0 ) {/* if we succeeded */
Signature · 7 lines
1.8.1.4 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Jan Kara
reviewerPATCH 18/26 mm: Convert process_vm_rw_pages() to use get_user_pages_unlocked()
In reply to Jan Kara--- mm/process_vm_access.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-)
mm/process_vm_access.c +2 -6
@@ -64,12 +64,8 @@ static int process_vm_rw_pages(struct task_struct *task, *bytes_copied =0 ;/* Get the pages we're interested in */ -down_read (&mm->mmap_sem); - pages_pinned =get_user_pages (task, mm, pa, - nr_pages_to_copy, - vm_write,0 , process_pages,NULL ); -up_read (&mm->mmap_sem); - + pages_pinned =get_user_pages_unlocked (task, mm, pa, nr_pages_to_copy, + vm_write,0 , process_pages);if (pages_pinned != nr_pages_to_copy) { rc = -EFAULT ; goto end;
Signature · 7 lines
1.8.1.4 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Jan Kara
reviewerPATCH 6/26 vmw_vmci: Convert driver to use get_user_pages_fast()
In reply to Jan KaraConvert vmci_host_setup_notify() and qp_host_get_user_memory() to use get_user_pages_fast() instead of get_user_pages(). Note that qp_host_get_user_memory() was using mmap_sem for writing without an apparent reason.
--- drivers/misc/vmw_vmci/vmci_host.c | 6 +----- drivers/misc/vmw_vmci/vmci_queue_pair.c | 21 ++++++--------------- 2 files changed, 7 insertions(+), 20 deletions(-)
drivers/misc/vmw_vmci/vmci_host.c +1 -5
@@ -243,11 +243,7 @@ static int vmci_host_setup_notify(struct vmci_ctx *context,/* * Lock physical page backing a given user VA. */ -down_read (¤t->mm->mmap_sem); - retval =get_user_pages (current, current->mm, -PAGE_ALIGN (uva), -1 ,1 ,0 , &page,NULL ); -up_read (¤t->mm->mmap_sem); + retval =get_user_pages_fast (PAGE_ALIGN (uva),1 ,1 , &page);if (retval !=1 )return VMCI_ERROR_GENERIC ;
drivers/misc/vmw_vmci/vmci_queue_pair.c +6 -15
@@ -732,13 +732,9 @@ static int qp_host_get_user_memory(u64 produce_uva,int retval;int err =VMCI_SUCCESS ; -down_write (¤t->mm->mmap_sem); - retval =get_user_pages (current, - current->mm, - (uintptr_t ) produce_uva, - produce_q->kernel_if->num_pages, -1 ,0 , - produce_q->kernel_if->u.h.header_page,NULL ); + retval =get_user_pages_fast ((uintptr_t ) produce_uva, + produce_q->kernel_if->num_pages,1 , + produce_q->kernel_if->u.h.header_page);if (retval < produce_q->kernel_if->num_pages) {pr_warn ("get_user_pages(produce) failed (retval=%d)" , retval); qp_release_pages(produce_q->kernel_if->u.h.header_page,
@@ -747,12 +743,9 @@ static int qp_host_get_user_memory(u64 produce_uva, goto out; } - retval =get_user_pages (current, - current->mm, - (uintptr_t ) consume_uva, - consume_q->kernel_if->num_pages, -1 ,0 , - consume_q->kernel_if->u.h.header_page,NULL ); + retval =get_user_pages_fast ((uintptr_t ) consume_uva, + consume_q->kernel_if->num_pages,1 , + consume_q->kernel_if->u.h.header_page);if (retval < consume_q->kernel_if->num_pages) {pr_warn ("get_user_pages(consume) failed (retval=%d)" , retval); qp_release_pages(consume_q->kernel_if->u.h.header_page,
@@ -763,8 +756,6 @@ static int qp_host_get_user_memory(u64 produce_uva, } out: -up_write (¤t->mm->mmap_sem); -return err; }
Signature · 7 lines
1.8.1.4 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Jan Kara
reviewerPATCH 9/26 crystalhd: Convert crystalhd_map_dio() to use get_user_pages_fast()
In reply to Jan Kara--- drivers/staging/crystalhd/crystalhd_misc.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)
drivers/staging/crystalhd/crystalhd_misc.c +1 -4
@@ -751,10 +751,7 @@ enum BC_STATUS crystalhd_map_dio(struct crystalhd_adp *adp, void *ubuff, } } -down_read (¤t->mm->mmap_sem); - res =get_user_pages (current, current->mm, uaddr, nr_pages, rw ==READ , -0 , dio->pages,NULL ); -up_read (¤t->mm->mmap_sem); + res =get_user_pages_fast (uaddr, nr_pages, rw ==READ , dio->pages);/* Save for release..*/ dio->sig = crystalhd_dio_locked;
Signature · 7 lines
1.8.1.4 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Jan Kara
reviewerPATCH 10/26 lustre: Convert ll_get_user_pages() to use get_user_pages_fast()
In reply to Jan Kara--- drivers/staging/lustre/lustre/llite/rw26.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-)
drivers/staging/lustre/lustre/llite/rw26.c +2 -5
@@ -202,11 +202,8 @@ static inline int ll_get_user_pages(int rw, unsigned long user_addr,OBD_ALLOC_LARGE (*pages, *max_pages *sizeof (**pages));if (*pages) { -down_read (¤t->mm->mmap_sem); - result =get_user_pages (current, current->mm, user_addr, - *max_pages, (rw ==READ ),0 , *pages, -NULL ); -up_read (¤t->mm->mmap_sem); + result =get_user_pages_fast (user_addr, *max_pages, + (rw ==READ ), *pages);if (unlikely (result <=0 ))OBD_FREE_LARGE (*pages, *max_pages *sizeof (**pages)); }
Signature · 7 lines
1.8.1.4 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Jan Kara
reviewerPATCH 1/26 cris: Convert cryptocop to use get_user_pages_fast()
In reply to Jan Kara--- arch/cris/arch-v32/drivers/cryptocop.c | 35 ++++++++++------------------------ 1 file changed, 10 insertions(+), 25 deletions(-)
arch/cris/arch-v32/drivers/cryptocop.c +10 -25
@@ -2716,43 +2716,28 @@ static int cryptocop_ioctl_process(struct inode *inode, struct file *filp, unsig } } -/* Acquire the mm page semaphore. */ -down_read (¤t->mm->mmap_sem); - - err =get_user_pages (current, - current->mm, - (unsigned long int )(oper.indata + prev_ix), - noinpages, -0 ,/* read access only for in data */ -0 ,/* no force */ - inpages, -NULL ); + err =get_user_pages_fast ((unsigned long )(oper.indata + prev_ix), + noinpages, +0 ,/* read access only for in data */ + inpages);if (err <0 ) { -up_read (¤t->mm->mmap_sem); nooutpages = noinpages =0 ; -DEBUG_API (printk ("cryptocop_ioctl_process: get_user_pages indata\n" )); +DEBUG_API (printk ("cryptocop_ioctl_process: get_user_pages_fast indata\n" )); goto error_cleanup; } noinpages = err;if (oper.do_cipher){ - err =get_user_pages (current, - current->mm, - (unsigned long int )oper.cipher_outdata, - nooutpages, -1 ,/* write access for out data */ -0 ,/* no force */ - outpages, -NULL ); -up_read (¤t->mm->mmap_sem); + err =get_user_pages_fast ((unsigned long )oper.cipher_outdata, + nooutpages, +1 ,/* write access for out data */ + outpages);if (err <0 ) { nooutpages =0 ; -DEBUG_API (printk ("cryptocop_ioctl_process: get_user_pages outdata\n" )); +DEBUG_API (printk ("cryptocop_ioctl_process: get_user_pages_fast outdata\n" )); goto error_cleanup; } nooutpages = err; - }else { -up_read (¤t->mm->mmap_sem); }/* Add 6 to nooutpages to make room for possibly inserted buffers for storing digest and
Signature · 7 lines
1.8.1.4 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Jan Kara
reviewerPATCH 11/26 sep: Convert sep_lock_user_pages() to get_user_pages_fast()
In reply to Jan Kara--- drivers/staging/sep/sep_main.c | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-)
drivers/staging/sep/sep_main.c +2 -7
@@ -1263,13 +1263,8 @@ static int sep_lock_user_pages(struct sep_device *sep, }/* Convert the application virtual address into a set of physical */ -down_read (¤t->mm->mmap_sem); - result =get_user_pages (current, current->mm, app_virt_addr, - num_pages, - ((in_out_flag ==SEP_DRIVER_IN_FLAG ) ?0 :1 ), -0 , page_array,NULL ); - -up_read (¤t->mm->mmap_sem); + result =get_user_pages_fast (app_virt_addr, num_pages, + ((in_out_flag ==SEP_DRIVER_IN_FLAG ) ?0 :1 ), page_array);/* Check the number of pages locked - if not all then exit with error */ if (result != num_pages) {
Signature · 7 lines
1.8.1.4 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
RE: [PATCH 23/26] ib: Convert qib_get_user_pages() to get_user_pages_unlocked()
In reply to Jan KaraThanks!!
I would like to test these two patches and also do the stable work for the deadlock as well. Do you have these patches in a repo somewhere to save me a bit of work?
We had been working on an internal version of the deadlock portion of this patch that uses get_user_pages_fast() vs. the new get_user_pages_unlocked().
The risk of GUP fast is the loss of the "force" arg on GUP fast, which I don't see as significant give our use case.
Some nits on the subject and commit message:
1. use IB/qib, IB/ipath vs. ib
2. use the correct ipath vs. qib in the commit message text
Mike
Quoted 135 lines
-----Original Message-----
From: Jan Kara [mailto:[email protected]]
Sent: Wednesday, October 02, 2013 10:28 AM
To: LKML
Cc: [email protected]; Jan Kara; infinipath; Roland Dreier; linux- [email protected]
Subject: [PATCH 23/26] ib: Convertqib_get_user_pages()toget_user_pages_unlocked()Convert
qib_get_user_pages()to useget_user_pages_unlocked(). This shortens the section where we holdmmap_semfor writing and also removes the knowledge aboutget_user_pages()locking from ipath driver. We also fix a bug in testing pinned number of pages when changing the code.CC Mike Marciniszyn <[email protected]>CC Roland Dreier <[email protected]>Signed-off-by Jan Kara <[email protected]>--- drivers/infiniband/hw/qib/qib_user_pages.c | 62 +++++++++++++----------------(quoted diff) +0
-1- 1 file changed, 26 insertions(+), 36 deletions(-)drivers/infiniband/hw/qib/qib_user_pages.c +26
-10@@ -48,39 +48,55 @@ static void __qib_release_user_pages(struct page **p, size_t num_pages, } } -/* -* Call with current->mm->mmap_sem held. +/** +* qib_get_user_pages - lock user pages into memory +* @start_page: the start page +* @num_pages: the number of pages +* @p: the output page structures +* +* This function takes a given start page (page aligned user virtual +* address) and pins it and the following specified number of pages. +For +* now, num_pages is always 1, but that will probably change at some +point +* (because caller is doing expected sends on a single virtually +contiguous +* buffer, so we can do all pages at once). */ -static int __qib_get_user_pages (unsigned long start_page,size_t num_pages, -struct page **p,struct vm_area_struct **vma) +int qib_get_user_pages (unsigned long start_page,size_t num_pages, +struct page **p) {unsigned long lock_limit;size_t got;int ret; +struct mm_struct *mm = current->mm; +down_write (&mm->mmap_sem); lock_limit =rlimit (RLIMIT_MEMLOCK ) >>PAGE_SHIFT ; -if (num_pages > lock_limit && !capable (CAP_IPC_LOCK )) { +if (mm->pinned_vm + num_pages > lock_limit && !capable (CAP_IPC_LOCK )) { +up_write (&mm->mmap_sem); ret = -ENOMEM ; goto bail; } + mm->pinned_vm += num_pages; +up_write (&mm->mmap_sem);for (got =0 ; got < num_pages; got += ret) { - ret =get_user_pages (current, current->mm, - start_page + got *PAGE_SIZE , - num_pages - got,1 ,1 , - p + got, vma); + ret =get_user_pages_unlocked (current, mm, + start_page + got *PAGE_SIZE , + num_pages - got,1 ,1 , + p + got);if (ret <0 ) goto bail_release; } - current->mm->pinned_vm += num_pages; ret =0 ; goto bail; bail_release:drivers/infiniband/hw/qib/qib_user_pages.c +3
-27__qib_release_user_pages (p, got,0 ); +down_write (&mm->mmap_sem); + mm->pinned_vm -= num_pages; +up_write (&mm->mmap_sem); bail:return ret; }@@ -117,32 +133,6 @@ dma_addr_t qib_map_page(struct pci_dev *hwdev,struct page *page, return phys; } -/** -* qib_get_user_pages - lock user pages into memory -* @start_page: the start page -* @num_pages: the number of pages -* @p: the output page structures -* -* This function takes a given start page (page aligned user virtual -* address) and pins it and the following specified number of pages. For -* now, num_pages is always 1, but that will probably change at some point -* (because caller is doing expected sends on a single virtually contiguous -* buffer, so we can do all pages at once). -*/ -int qib_get_user_pages (unsigned long start_page,size_t num_pages, -struct page **p) -{ -int ret; - -down_write (¤t->mm->mmap_sem); - - ret =__qib_get_user_pages (start_page, num_pages, p,NULL ); - -up_write (¤t->mm->mmap_sem); - -return ret; -} -void qib_release_user_pages (struct page **p,size_t num_pages) {if (current->mm)/* during close after signal, mm can be NULL */ --
Quoted 1 line
1.8.1.4
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Re: [PATCH 17/26] kvm: Use get_user_pages_unlocked() in async_pf_execute()
In reply to Jan KaraLooks straightforward.
Quoted 25 lines · Jan Kara
CC Gleb Natapov <[email protected]>CC Paolo Bonzini <[email protected]>Signed-off-by Jan Kara <[email protected]>--- virt/kvm/async_pf.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)virt/kvm/async_pf.c +1
-3@@ -67,9 +67,7 @@ static void async_pf_execute(struct work_struct *work)might_sleep ();use_mm (mm); -down_read (&mm->mmap_sem); -get_user_pages (current, mm, addr,1 ,1 ,0 , &page,NULL ); -up_read (&mm->mmap_sem); +get_user_pages_unlocked (current, mm, addr,1 ,1 ,0 , &page);unuse_mm (mm);spin_lock (&vcpu->async_pf.lock);Signature · 1 line
1.8.1.4
--
Gleb.
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Jan Kara
reviewerRe: [PATCH 23/26] ib: Convert qib_get_user_pages() to get_user_pages_unlocked()
In reply to Marciniszyn, MikeQuoted 5 lines · Mike
Thanks!!
I would like to test these two patches and also do the stable work for the deadlock as well. Do you have these patches in a repo somewhere to save me a bit of work?
I've pushed the patches to:git://git.kernel.org/pub/scm/linux/kernel/git/jack/linux-fs.git
into the branch 'get_user_pages'.
Quoted 6 lines
We had been working on an internal version of the deadlock portion of this patch that uses
get_user_pages_fast()vs. the newget_user_pages_unlocked().The risk of GUP fast is the loss of the "force" arg on GUP fast, which I don't see as significant give our use case.
Yes. I was discussing with Roland some time ago whether the force
argument is needed and he said it is. So I kept the arguments of get_user_pages() intact and just simplified the locking...
BTW: Infiniband still needs mmap_sem for modification of mm->pinned_vm. It might be worthwhile to actually change that to atomic_long_t (only kernel/events/core.c would need update besides infiniband) and avoid taking mmap_sem in infiniband code altogether.
Quoted 3 lines
Some nits on the subject and commit message:
1. use IB/qib, IB/ipath vs. ib
2. use the correct ipath vs. qib in the commit message text
Sure will do. Thanks the quick reply and for comments.
Honza
Quoted 142 lines
Quoted 135 lines
-----Original Message-----
From: Jan Kara [mailto:[email protected]]
Sent: Wednesday, October 02, 2013 10:28 AM
To: LKML
Cc: [email protected]; Jan Kara; infinipath; Roland Dreier; linux- [email protected]
Subject: [PATCH 23/26] ib: Convertqib_get_user_pages()toget_user_pages_unlocked()Convert
qib_get_user_pages()to useget_user_pages_unlocked(). This shortens the section where we holdmmap_semfor writing and also removes the knowledge aboutget_user_pages()locking from ipath driver. We also fix a bug in testing pinned number of pages when changing the code.CC Mike Marciniszyn <[email protected]>CC Roland Dreier <[email protected]>Signed-off-by Jan Kara <[email protected]>--- drivers/infiniband/hw/qib/qib_user_pages.c | 62 +++++++++++++----------------(quoted diff) +0
-1- 1 file changed, 26 insertions(+), 36 deletions(-)drivers/infiniband/hw/qib/qib_user_pages.c +26
-10@@ -48,39 +48,55 @@ static void __qib_release_user_pages(struct page **p, size_t num_pages, } } -/* -* Call with current->mm->mmap_sem held. +/** +* qib_get_user_pages - lock user pages into memory +* @start_page: the start page +* @num_pages: the number of pages +* @p: the output page structures +* +* This function takes a given start page (page aligned user virtual +* address) and pins it and the following specified number of pages. +For +* now, num_pages is always 1, but that will probably change at some +point +* (because caller is doing expected sends on a single virtually +contiguous +* buffer, so we can do all pages at once). */ -static int __qib_get_user_pages (unsigned long start_page,size_t num_pages, -struct page **p,struct vm_area_struct **vma) +int qib_get_user_pages (unsigned long start_page,size_t num_pages, +struct page **p) {unsigned long lock_limit;size_t got;int ret; +struct mm_struct *mm = current->mm; +down_write (&mm->mmap_sem); lock_limit =rlimit (RLIMIT_MEMLOCK ) >>PAGE_SHIFT ; -if (num_pages > lock_limit && !capable (CAP_IPC_LOCK )) { +if (mm->pinned_vm + num_pages > lock_limit && !capable (CAP_IPC_LOCK )) { +up_write (&mm->mmap_sem); ret = -ENOMEM ; goto bail; } + mm->pinned_vm += num_pages; +up_write (&mm->mmap_sem);for (got =0 ; got < num_pages; got += ret) { - ret =get_user_pages (current, current->mm, - start_page + got *PAGE_SIZE , - num_pages - got,1 ,1 , - p + got, vma); + ret =get_user_pages_unlocked (current, mm, + start_page + got *PAGE_SIZE , + num_pages - got,1 ,1 , + p + got);if (ret <0 ) goto bail_release; } - current->mm->pinned_vm += num_pages; ret =0 ; goto bail; bail_release:drivers/infiniband/hw/qib/qib_user_pages.c +3
-27__qib_release_user_pages (p, got,0 ); +down_write (&mm->mmap_sem); + mm->pinned_vm -= num_pages; +up_write (&mm->mmap_sem); bail:return ret; }@@ -117,32 +133,6 @@ dma_addr_t qib_map_page(struct pci_dev *hwdev,struct page *page, return phys; } -/** -* qib_get_user_pages - lock user pages into memory -* @start_page: the start page -* @num_pages: the number of pages -* @p: the output page structures -* -* This function takes a given start page (page aligned user virtual -* address) and pins it and the following specified number of pages. For -* now, num_pages is always 1, but that will probably change at some point -* (because caller is doing expected sends on a single virtually contiguous -* buffer, so we can do all pages at once). -*/ -int qib_get_user_pages (unsigned long start_page,size_t num_pages, -struct page **p) -{ -int ret; - -down_write (¤t->mm->mmap_sem); - - ret =__qib_get_user_pages (start_page, num_pages, p,NULL ); - -up_write (¤t->mm->mmap_sem); - -return ret; -} -void qib_release_user_pages (struct page **p,size_t num_pages) {if (current->mm)/* during close after signal, mm can be NULL */ --Quoted 1 line
1.8.1.4
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
Signature · 6 lines
Jan Kara <[email protected]> SUSE Labs, CR -- To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
RE: [PATCH 23/26] ib: Convert qib_get_user_pages() to get_user_pages_unlocked()
Quoted 5 lines
Quoted 2 lines
The risk of GUP fast is the loss of the "force" arg on GUP fast, which I don't see as significant give our use case.
Yes. I was discussing with Roland some time ago whether the force
argument is needed and he said it is. So I kept the arguments ofget_user_pages()intact and just simplified the locking...
The PSM side of the code is a more traditional use of GUP (like direct I/O), so I think it is a different use case than the locking for IB memory regions.
Mike
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Jan Kara
reviewerRe: [PATCH 23/26] ib: Convert qib_get_user_pages() to get_user_pages_unlocked()
Quoted 9 lines · Mike
Quoted 5 lines
Quoted 2 lines
The risk of GUP fast is the loss of the "force" arg on GUP fast, which I don't see as significant give our use case.
Yes. I was discussing with Roland some time ago whether the force
argument is needed and he said it is. So I kept the arguments ofget_user_pages()intact and just simplified the locking...The PSM side of the code is a more traditional use of GUP (like direct I/O), so I think it is a different use case than the locking for IB memory regions.
Ah, I see. Whatever suits you best. I don't really care as long asget_user_pages() locking doesn't leak into IB drivers :)
Honza
Signature · 6 lines
Jan Kara <[email protected]> SUSE Labs, CR -- To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Quoted 12 lines · Jan Kara
Hello,
In my quest for changing locking around page faults to make things easier for
filesystems I found outget_user_pages()users could use a cleanup. The knowledge about necessary locking forget_user_pages()is in tons of places in drivers and quite a few of them actually get it wrong (don't havemmap_semwhen callingget_user_pages()or holdmmap_semwhen callingcopy_from_user()in the surrounding code). Rather often this actually doesn't seem necessary. This patch series converts lots of places to use eitherget_user_pages_fast()or a new simple wrapperget_user_pages_unlocked()to remove the knowledge ofmmap_semfrom the drivers. I'm still looking into converting a few remaining drivers (most notably v4l2) which are more complex.
Even looking over the kerneldoc comment next to it I still fail to understand when you'd want to use get_user_pages_fast and when not.
This isn't meant as an argument against your series, but maybe a hint that we'd need further work in this direction.
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH 16/26] mm: Provide get_user_pages_unlocked()
In reply to Jan KaraQuoted 5 lines · Jan Kara
Provide a wrapper for
get_user_pages()which takes care of acquiring and releasingmmap_sem. Using this function reduces amount of places in which we deal withmmap_sem.Signed-off-by Jan Kara <[email protected]>
Seem like this should be the default one and the one without the locked should be __unlocked. Maybe a grand rename is in order?
get_user_pages_fast -> get_user_pagesget_user_pages_unlocked -> __get_user_pagesget_user_pages_unlocked -> __get_user_pages_locked
steering people to the most sensible ones by default?
Re: [PATCH 16/26] mm: Provide get_user_pages_unlocked()
In reply to Jan KaraQuoted 20 lines · Jan Kara
Provide a wrapper for
get_user_pages()which takes care of acquiring and releasingmmap_sem. Using this function reduces amount of places in which we deal withmmap_sem.Signed-off-by Jan Kara <[email protected]>--- include/linux/mm.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+)include/linux/mm.h +3
-0@@ -1031,6 +1031,20 @@ long get_user_pages(struct task_struct *tsk, struct mm_struct *mm, struct vm_area_struct **vmas);int get_user_pages_fast (unsigned long start,int nr_pages,int write,struct page **pages); +static inline long +get_user_pages_unlocked(struct task_struct *tsk, struct mm_struct *mm, + unsigned long start, unsigned long nr_pages,
include/linux/mm.h long get_user_pages(struct task_struct *tsk, struct mm_struct *mm, · line 1046
Quoted 10 lines
include/linux/mm.h +10
-0+ int write, int force, struct page **pages) +{ + long ret; + +down_read (&mm->mmap_sem); + ret =get_user_pages (tsk, mm, start, nr_pages, write, force, pages, +NULL ); +up_read (&mm->mmap_sem); +return ret; +}
Hmmm. I like the idea, but I really dislike this name. I don't like xx_unlocked function takes a lock. It is a source of confusing, I believe.
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH 18/26] mm: Convert process_vm_rw_pages() to use get_user_pages_unlocked()
In reply to Jan KaraQuoted 14 lines · Jan Kara
Signed-off-by Jan Kara <[email protected]>--- mm/process_vm_access.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-)mm/process_vm_access.c +0
-1@@ -64,12 +64,8 @@ static int process_vm_rw_pages(struct task_struct *task, *bytes_copied =0 ;/* Get the pages we're interested in */ -down_read (&mm->mmap_sem);
mm/process_vm_access.c static int process_vm_rw_pages(struct task_struct *task, · line 71
Quoted 10 lines
mm/process_vm_access.c +2
-5- pages_pinned =get_user_pages (task, mm, pa, - nr_pages_to_copy, - vm_write,0 , process_pages,NULL ); -up_read (&mm->mmap_sem); - + pages_pinned =get_user_pages_unlocked (task, mm, pa, nr_pages_to_copy, + vm_write,0 , process_pages);if (pages_pinned != nr_pages_to_copy) { rc = -EFAULT ; goto end;
This is wrong because original code is wrong. In this function, page may be pointed to anon pages. Then, you should keep to take mmap_sem until finish to copying. Otherwise concurrent fork() makes nasty COW issue.
Jan Kara
reviewerRe: [PATCH 18/26] mm: Convert process_vm_rw_pages() to use get_user_pages_unlocked()
Quoted 13 lines · KOSAKI Motohiro
Quoted 12 lines · Jan Kara
Signed-off-by Jan Kara <[email protected]>--- mm/process_vm_access.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-)mm/process_vm_access.c +0
-0@@ -64,12 +64,8 @@ static int process_vm_rw_pages(struct task_struct *task, *bytes_copied =0 ;
Quoted 16 lines
mm/process_vm_access.c static int process_vm_rw_pages(struct task_struct *task, · line 71
Quoted 12 lines
mm/process_vm_access.c +2
-6/* Get the pages we're interested in */ -down_read (&mm->mmap_sem); - pages_pinned =get_user_pages (task, mm, pa, - nr_pages_to_copy, - vm_write,0 , process_pages,NULL ); -up_read (&mm->mmap_sem); - + pages_pinned =get_user_pages_unlocked (task, mm, pa, nr_pages_to_copy, + vm_write,0 , process_pages);if (pages_pinned != nr_pages_to_copy) { rc = -EFAULT ; goto end;This is wrong because original code is wrong. In this function, page may be pointed to anon pages. Then, you should keep to take
mmap_semuntil finish to copying. Otherwise concurrentfork()makes nasty COW issue.
Hum, can you be more specific? I suppose you are speaking about situation
when the remote task we are copying data from/to does fork while process_vm_rw_pages() runs. If we are copying data from remote task, I don't see how COW could cause any problem. If we are copying to remote task and fork happens after get_user_pages() but before copy_to_user() then I can see we might be having some trouble. copy_to_user() would then copy data into both original remote process and its child thus essentially bypassing COW. If the child process manages to COW some of the pages before copy_to_user() happens, it can even see only some of the pages. Is that what you mean?
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Jan Kara
reviewerRe: [PATCH 16/26] mm: Provide get_user_pages_unlocked()
In reply to KOSAKI MotohiroQuoted 26 lines · KOSAKI Motohiro
Quoted 25 lines · Jan Kara
Provide a wrapper for
get_user_pages()which takes care of acquiring and releasingmmap_sem. Using this function reduces amount of places in which we deal withmmap_sem.Signed-off-by Jan Kara <[email protected]>--- include/linux/mm.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+)include/linux/mm.h +8
-0@@ -1031,6 +1031,20 @@ long get_user_pages(struct task_struct *tsk, struct mm_struct *mm, struct vm_area_struct **vmas);int get_user_pages_fast (unsigned long start,int nr_pages,int write,struct page **pages); +static inline long +get_user_pages_unlocked (struct task_struct *tsk,struct mm_struct *mm, +unsigned long start,unsigned long nr_pages, +int write,int force,struct page **pages) +{ +long ret; + +down_read (&mm->mmap_sem);
Quoted 8 lines
include/linux/mm.h long get_user_pages(struct task_struct *tsk, struct mm_struct *mm, · line 1046
Quoted 5 lines
include/linux/mm.h +5
-0+ ret =get_user_pages (tsk, mm, start, nr_pages, write, force, pages, +NULL ); +up_read (&mm->mmap_sem); +return ret; +}Hmmm. I like the idea, but I really dislike this name. I don't like
xx_unlockedfunction takes a lock. It is a source of confusing, I believe.
Sure, I'm not very happy about the name either. As Christoph wrote
probably renaming all get_user_pages() variants might be appropriate. I'll think about names some more.
Honza
Signature · 8 lines
Jan Kara <[email protected]> SUSE Labs, CR -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH 05/26] omap3isp: Make isp_video_buffer_prepare_user() use get_user_pages_fast()
In reply to Jan KaraHi Jan,
Thank you for the patch.
Quoted 3 lines · On Wednesday 02 October 2013 16:27:46 Jan Kara wrote:
CC Laurent Pinchart <[email protected]>Signed-off-by Jan Kara <[email protected]>
Could you briefly explain where you're headed with this ? The V4L2 subsystem has suffered for quite a long time from potentiel AB-BA deadlocks, due the drivers taking the mmap_sem semaphore while holding the V4L2 buffers queue lock in the code path below, and taking them in reverse order in the mmap() path (as the mm core takes the mmap_sem semaphore before calling the mmap() operation). We've solved that by releasing the V4L2 buffers queue lock before taking the mmap_sem lock below and taking it back after releasing the mmap_sem lock. Having an explicit indication that the mmap_sem lock was taken helped keeping the problem in mind. I'm not against hiding it in get_user_pages_fast(), but I'd like to know what the next step is. Maybe it would also be worth it adding a /* get_user_pages_fast() takes the mmap_sem */ comment before the call ?
Quoted 26 lines
--- drivers/media/platform/omap3isp/ispqueue.c | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-)(quoted diff) +0
-0
b/drivers/media/platform/omap3isp/ispqueue.cindexe15f01342058..bed380395e6c100644drivers/media/platform/omap3isp/ispqueue.c +3
-7@@ -331,13 +331,9 @@ static int isp_video_buffer_prepare_user(struct isp_video_buffer *buf) if (buf->pages == NULL)return -ENOMEM ; -down_read (¤t->mm->mmap_sem); - ret =get_user_pages (current, current->mm, data &PAGE_MASK , - buf->npages, - buf->vbuf.type ==V4L2_BUF_TYPE_VIDEO_CAPTURE ,0 , - buf->pages,NULL ); -up_read (¤t->mm->mmap_sem); - + ret =get_user_pages_fast (data &PAGE_MASK , buf->npages, + buf->vbuf.type ==V4L2_BUF_TYPE_VIDEO_CAPTURE , + buf->pages);if (ret != buf->npages) { buf->npages = ret <0 ?0 : ret;isp_video_buffer_cleanup (buf);
Signature · 3 lines
Regards, Laurent Pinchart
Jan Kara
reviewerRe: [PATCH 05/26] omap3isp: Make isp_video_buffer_prepare_user() use get_user_pages_fast()
Quoted 10 lines · Laurent Pinchart
Hi Jan,
Thank you for the patch.
Quoted 3 lines · On Wednesday 02 October 2013 16:27:46 Jan Kara wrote:
CC Laurent Pinchart <[email protected]>Signed-off-by Jan Kara <[email protected]>Acked-by Laurent Pinchart <[email protected]>
Thanks!
Quoted 2 lines
Could you briefly explain where you're headed with this ?
My motivation is that currently filesystems have to workaround locking
problems with ->page_mkwrite() (the write page fault handler) being called with mmap_sem held. The plan is to provide ability to ->page_mkwrite() handler to drop mmap_sem. And that needs an audit of all get_user_pages() callers to verify they won't be broken by this locking change. So I've started with making this audit simpler.
Quoted 5 lines
The V4L2 subsystem has suffered for quite a long time from potentiel AB-BA deadlocks, due the drivers taking the
mmap_semsemaphore while holding the V4L2 buffers queue lock in the code path below, and taking them in reverse order in themmap()path (as the mm core takes themmap_semsemaphore before calling themmap()operation).
Yeah, I've read about this in some comment in V4L2. Also there are some
places (drivers/media/platform/omap/omap_vout.c and drivers/media/v4l2-core/) which acquire mmap_sem pretty early to avoid lock inversion with the queue lock. These are actually causing me quite some headache :)
Quoted 7 lines
We've solved that by releasing the V4L2 buffers queue lock before taking the
mmap_semlock below and taking it back after releasing themmap_semlock. Having an explicit indication that themmap_semlock was taken helped keeping the problem in mind. I'm not against hiding it inget_user_pages_fast(), but I'd like to know what the next step is. Maybe it would also be worth it adding a /*get_user_pages_fast()takes themmap_sem*/ comment before the call ?
OK, I can add the comment. Thanks for review.
Honza
Quoted 31 lines
Quoted 26 lines
--- drivers/media/platform/omap3isp/ispqueue.c | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-)(quoted diff) +0
-0
b/drivers/media/platform/omap3isp/ispqueue.cindexe15f01342058..bed380395e6c100644drivers/media/platform/omap3isp/ispqueue.c +3
-7@@ -331,13 +331,9 @@ static int isp_video_buffer_prepare_user(struct isp_video_buffer *buf) if (buf->pages == NULL)return -ENOMEM ; -down_read (¤t->mm->mmap_sem); - ret =get_user_pages (current, current->mm, data &PAGE_MASK , - buf->npages, - buf->vbuf.type ==V4L2_BUF_TYPE_VIDEO_CAPTURE ,0 , - buf->pages,NULL ); -up_read (¤t->mm->mmap_sem); - + ret =get_user_pages_fast (data &PAGE_MASK , buf->npages, + buf->vbuf.type ==V4L2_BUF_TYPE_VIDEO_CAPTURE , + buf->pages);if (ret != buf->npages) { buf->npages = ret <0 ?0 : ret;isp_video_buffer_cleanup (buf);Signature · 3 lines
Regards, Laurent Pinchart
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Jan Kara
reviewerQuoted 14 lines · Christoph Hellwig
Quoted 12 lines · Jan Kara
Hello,
In my quest for changing locking around page faults to make things easier for
filesystems I found outget_user_pages()users could use a cleanup. The knowledge about necessary locking forget_user_pages()is in tons of places in drivers and quite a few of them actually get it wrong (don't havemmap_semwhen callingget_user_pages()or holdmmap_semwhen callingcopy_from_user()in the surrounding code). Rather often this actually doesn't seem necessary. This patch series converts lots of places to use eitherget_user_pages_fast()or a new simple wrapperget_user_pages_unlocked()to remove the knowledge ofmmap_semfrom the drivers. I'm still looking into converting a few remaining drivers (most notably v4l2) which are more complex.
Quoted 2 lines
Even looking over the kerneldoc comment next to it I still fail to understand when you'd want to use
get_user_pages_fastand when not.
AFAIU get_user_pages_fast() should be used
1) if you don't need any special get_user_pages() arguments (like calling it for mm of a different process, forcing COW, or similar).
2) you don't expect pages to be unmapped (then get_user_pages_fast() is actually somewhat slower because it walks page tables twice).
Honza
Signature · 8 lines
Jan Kara <[email protected]> SUSE Labs, CR -- To unsubscribe, send a message with 'unsubscribe linux-aio' in the body to [email protected]. For more info on Linux AIO, see: http://www.kvack.org/aio/ Don't email: <a href=mailto:"[email protected]">[email protected]</a>
Re: [PATCH 18/26] mm: Convert process_vm_rw_pages() to use get_user_pages_unlocked()
In reply to Jan KaraQuoted 30 lines · Jan Kara
Quoted 13 lines · KOSAKI Motohiro
Quoted 12 lines · Jan Kara
Signed-off-by Jan Kara <[email protected]>--- mm/process_vm_access.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-)mm/process_vm_access.c +0
-0@@ -64,12 +64,8 @@ static int process_vm_rw_pages(struct task_struct *task, *bytes_copied =0 ;Quoted 16 lines
mm/process_vm_access.c static int process_vm_rw_pages(struct task_struct *task, · line 71
Quoted 12 lines
mm/process_vm_access.c +2
-6/* Get the pages we're interested in */ -down_read (&mm->mmap_sem); - pages_pinned =get_user_pages (task, mm, pa, - nr_pages_to_copy, - vm_write,0 , process_pages,NULL ); -up_read (&mm->mmap_sem); - + pages_pinned =get_user_pages_unlocked (task, mm, pa, nr_pages_to_copy, + vm_write,0 , process_pages);if (pages_pinned != nr_pages_to_copy) { rc = -EFAULT ; goto end;This is wrong because original code is wrong. In this function, page may be pointed to anon pages. Then, you should keep to take
mmap_semuntil finish to copying. Otherwise concurrentfork()makes nasty COW issue.
Quoted 10 lines
Hum, can you be more specific? I suppose you are speaking about situation
when the remote task we are copying data from/to does fork whileprocess_vm_rw_pages()runs. If we are copying data from remote task, I don't see how COW could cause any problem. If we are copying to remote task and fork happens afterget_user_pages()but beforecopy_to_user()then I can see we might be having some trouble.copy_to_user()would then copy data into both original remote process and its child thus essentially bypassing COW. If the child process manages to COW some of the pages beforecopy_to_user()happens, it can even see only some of the pages. Is that what you mean?
scenario 1: vm_write==0
Process P1 call get_user_pages(pa, process_pages) in process_vm_rw_pages
P1 unlock mmap_sem.
Process P2 call fork(). and make P3.
P2 write memory pa. now the "process_pages" is owned by P3 (the child process)
P3 write memory pa. and then the content of "process_pages" is changed.
P1 read process_pages as P2's page. but actually, it is P3's data. Then, P1 observe garbage, at least unintended, data was read.
scenario 2: vm_write==1
Process P1 call get_user_pages(pa, process_pages) in process_vm_rw_pages. It makes COW break and any anon page sharing broke. P1 unlock mmap_sem.
P2 call fork(). and make P3. And, now COW page sharing is restored.
P2 write memory pa. now the "process_pages" is owned by P3.
P3 write memory pa. it mean P3 changes "process_pages".
P1 write process_pages as P2's page. but actually, it is P3's. It override above P3's write and then P3 observe data corruption.
The solution is to keep holding mmap_sem until finishing process_pages access because mmap_sem prevent fork. and then race never be happen. I mean you cann't use get_user_pages_unlock() if target address point to anon pages.
I'm not sure these story match your explanation.
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH 13/26] fsl_hypervisor: Convert ioctl_memcpy() to use get_user_pages_fast()
In reply to Jan KaraQuoted 3 lines · Jan Kara
CC Timur Tabi <[email protected]>Signed-off-by Jan Kara <[email protected]>---
This seems okay, but I don't have access to hardware at the moment to test it.
RE: [PATCH 23/26] ib: Convert qib_get_user_pages() to get_user_pages_unlocked()
In reply to Jan KaraQuoted 21 lines
-----Original Message-----
From: Jan Kara [mailto:[email protected]]
Sent: Wednesday, October 02, 2013 11:39 AM
To: Marciniszyn, Mike
Cc: Jan Kara; LKML; [email protected]; infinipath; Roland Dreier; linux- [email protected]
Subject: Re: [PATCH 23/26] ib: Convertqib_get_user_pages()toget_user_pages_unlocked()Quoted 9 lines · Mike
Quoted 5 lines
Quoted 2 lines
The risk of GUP fast is the loss of the "force" arg on GUP fast, which I don't see as significant give our use case.
Yes. I was discussing with Roland some time ago whether the force
argument is needed and he said it is. So I kept the arguments ofget_user_pages()intact and just simplified the locking...The PSM side of the code is a more traditional use of GUP (like direct I/O), so I think it is a different use case than the locking for IB memory regions.
Quoted 7 lines
Ah, I see. Whatever suits you best. I don't really care as long as
get_user_pages()locking doesn't leak into IB drivers :)Honza
--
Jan Kara <[email protected]>
SUSE Labs, CR
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
RE: [PATCH 23/26] ib: Convert qib_get_user_pages() to get_user_pages_unlocked()
In reply to Marciniszyn, MikeQuoted 2 lines
The PSM side of the code is a more traditional use of GUP (like direct I/O), so I think it is a different use case than the locking for IB memory regions.
I have resubmitted the two deadlock fixes using get_user_pages_fast() and marked them stable.
See http://marc.info/?l=linux-rdma&m=138089335506355&w=2
Mike
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
RE: [PATCH 23/26] ib: Convert qib_get_user_pages() to get_user_pages_unlocked()
In reply to Marciniszyn, MikeInadvertent send!
Mike
Quoted 38 lines
-----Original Message-----
From: Marciniszyn, Mike
Sent: Friday, October 04, 2013 9:39 AM
To: Jan Kara
Cc: LKML; [email protected]; infinipath; Roland Dreier; linux- [email protected]
Subject: RE: [PATCH 23/26] ib: Convertqib_get_user_pages()toget_user_pages_unlocked()Quoted 27 lines
-----Original Message-----
From: Jan Kara [mailto:[email protected]]
Sent: Wednesday, October 02, 2013 11:39 AM
To: Marciniszyn, Mike
Cc: Jan Kara; LKML; [email protected]; infinipath; Roland Dreier; linux- [email protected]
Subject: Re: [PATCH 23/26] ib: Convertqib_get_user_pages()toget_user_pages_unlocked()Quoted 10 lines · Mike
Quoted 6 lines
Quoted 2 lines
The risk of GUP fast is the loss of the "force" arg on GUP fast, which I don't see as significant give our use case.
Yes. I was discussing with Roland some time ago whether the
force argument is needed and he said it is. So I kept the arguments ofget_user_pages()intact and just simplified the locking...The PSM side of the code is a more traditional use of GUP (like direct I/O), so I think it is a different use case than the locking for IB memory regions.
Ah, I see. Whatever suits you best. I don't really care as long as
get_user_pages()locking doesn't leak into IB drivers :)Honza
--
Jan Kara <[email protected]>
SUSE Labs, CR
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
RE: [PATCH 23/26] ib: Convert qib_get_user_pages() to get_user_pages_unlocked()
In reply to Jan KaraQuoted 5 lines
Convert
qib_get_user_pages()to useget_user_pages_unlocked(). This shortens the section where we holdmmap_semfor writing and also removes the knowledge aboutget_user_pages()locking from ipath driver. We also fix a bug in testing pinned number of pages when changing the code.
This patch and the sibling ipath patch will nominally take the mmap_sem twice where the old routine only took it once. This is a performance issue.
Is the intent here to deprecate get_user_pages()?
I agree, the old code's lock limit test is broke and needs to be fixed. I like the elimination of the silly wrapper routine!
Could the lock limit test be pushed into another version of the wrapper so that there is only one set of mmap_sem transactions?
Mike
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Jan Kara
reviewerRe: [PATCH 23/26] ib: Convert qib_get_user_pages() to get_user_pages_unlocked()
Quoted 9 lines · Mike
Quoted 5 lines
Convert
qib_get_user_pages()to useget_user_pages_unlocked(). This shortens the section where we holdmmap_semfor writing and also removes the knowledge aboutget_user_pages()locking from ipath driver. We also fix a bug in testing pinned number of pages when changing the code.This patch and the sibling ipath patch will nominally take the
mmap_sem
twice where the old routine only took it once. This is a performance
issue.
It will take mmap_sem only once during normal operation. Only ifget_user_pages_unlocked() fail, we have to take mmap_sem again to undo the change of mm->pinned_vm.
Quoted 1 line
Is the intent here to deprecate
get_user_pages()?
Well, as much as I'd like to, there are really places in mm code which
need to call get_user_pages() while holding mmap_sem to be able to inspect corresponding vmas etc. So I want to reduce get_user_pages() use as much as possible but I'm not really hoping in completely removing it.
Quoted 5 lines
I agree, the old code's lock limit test is broke and needs to be fixed. I like the elimination of the silly wrapper routine!
Could the lock limit test be pushed into another version of the wrapper so that there is only one set of
mmap_semtransactions?
I'm sorry, I don't understand what you mean here...
Honza
Signature · 6 lines
Jan Kara <[email protected]> SUSE Labs, CR -- To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Quoted 15 lines · Jan Kara
Quoted 14 lines · Christoph Hellwig
Quoted 12 lines · Jan Kara
Hello,
In my quest for changing locking around page faults to make things easier for
filesystems I found outget_user_pages()users could use a cleanup. The knowledge about necessary locking forget_user_pages()is in tons of places in drivers and quite a few of them actually get it wrong (don't havemmap_semwhen callingget_user_pages()or holdmmap_semwhen callingcopy_from_user()in the surrounding code). Rather often this actually doesn't seem necessary. This patch series converts lots of places to use eitherget_user_pages_fast()or a new simple wrapperget_user_pages_unlocked()to remove the knowledge ofmmap_semfrom the drivers. I'm still looking into converting a few remaining drivers (most notably v4l2) which are more complex.
Quoted 7 lines
Quoted 2 lines
Even looking over the kerneldoc comment next to it I still fail to understand when you'd want to use
get_user_pages_fastand when not.AFAIU
get_user_pages_fast()should be used
1) if you don't need any specialget_user_pages()arguments (like calling it for mm of a different process, forcing COW, or similar).
2) you don't expect pages to be unmapped (thenget_user_pages_fast()is actually somewhat slower because it walks page tables twice).
If target page point to anon or private mapping pages, get_user_pages_fast() is fork unsafe. O_DIRECT man pages describe a bit about this.
see http://man7.org/linux/man-pages/man2/open.2.html
Quoted 5 lines
O_DIRECTI/Os should never be run concurrently with the fork(2) system call, if the memory buffer is a private mapping (i.e., any mapping created with the mmap(2)MAP_PRIVATEflag; this includes memory allocated on the heap and statically allocated buffers). Any such I/Os, whether submitted via an asynchronous I/O interface or
Quoted 8 lines
from another thread in the process, should be completed before fork(2) is called. Failure to do so can result in data corruption and undefined behavior in parent and child processes. This restriction does not apply when the memory buffer for the
O_DIRECTI/Os was created using shmat(2) or mmap(2) with theMAP_SHAREDflag. Nor does this restriction apply when the memory buffer has been advised asMADV_DONTFORKwith madvise(2), ensuring that it will not be available to the child after fork(2).
Thanks.
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 24 lines · KOSAKI Motohiro
Quoted 15 lines · Jan Kara
Quoted 14 lines · Christoph Hellwig
Quoted 12 lines · Jan Kara
Hello,
In my quest for changing locking around page faults to make things easier for
filesystems I found outget_user_pages()users could use a cleanup. The knowledge about necessary locking forget_user_pages()is in tons of places in drivers and quite a few of them actually get it wrong (don't havemmap_semwhen callingget_user_pages()or holdmmap_semwhen callingcopy_from_user()in the surrounding code). Rather often this actually doesn't seem necessary. This patch series converts lots of places to use eitherget_user_pages_fast()or a new simple wrapperget_user_pages_unlocked()to remove the knowledge ofmmap_semfrom the drivers. I'm still looking into converting a few remaining drivers (most notably v4l2) which are more complex.Quoted 7 lines
Quoted 2 lines
Even looking over the kerneldoc comment next to it I still fail to understand when you'd want to use
get_user_pages_fastand when not.AFAIU
get_user_pages_fast()should be used
1) if you don't need any specialget_user_pages()arguments (like calling it for mm of a different process, forcing COW, or similar).
2) you don't expect pages to be unmapped (thenget_user_pages_fast()is actually somewhat slower because it walks page tables twice).
Quoted 19 lines
If target page point to anon or private mapping pages,
get_user_pages_fast()is fork unsafe.O_DIRECTman pages describe a bit about this.see http://man7.org/linux/man-pages/man2/open.2.html
Quoted 13 lines
O_DIRECTI/Os should never be run concurrently with the fork(2) system call, if the memory buffer is a private mapping (i.e., any mapping created with the mmap(2)MAP_PRIVATEflag; this includes memory allocated on the heap and statically allocated buffers). Any such I/Os, whether submitted via an asynchronous I/O interface or from another thread in the process, should be completed before fork(2) is called. Failure to do so can result in data corruption and undefined behavior in parent and child processes. This restriction does not apply when the memory buffer for theO_DIRECTI/Os was created using shmat(2) or mmap(2) with theMAP_SHAREDflag. Nor does this restriction apply when the memory buffer has been advised asMADV_DONTFORKwith madvise(2), ensuring that it will not be available to the child after fork(2).
IMHO, get_user_pages_fast() should be renamed to get_user_pages_quirk(). Its semantics is not equal to get_user_pages(). When someone simply substitute get_user_pages() to get_user_pages_fast(), they might see huge trouble.
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH 10/26] lustre: Convert ll_get_user_pages() to use get_user_pages_fast()
In reply to Jan KaraQuoted 5 lines · Jan Kara
CC Greg Kroah-Hartman <[email protected]>CC Peng Tao <[email protected]>CC Andreas Dilger <[email protected]>Signed-off-by Jan Kara <[email protected]>
Quoted 23 lines
---
drivers/staging/lustre/lustre/llite/rw26.c| 7 ++-----1 file changed, 2 insertions(+), 5 deletions(-)drivers/staging/lustre/lustre/llite/rw26.c +2
-5@@ -202,11 +202,8 @@ static inline int ll_get_user_pages(int rw, unsignedlong user_addr, OBD_ALLOC_LARGE(*pages, *max_pages * sizeof(**pages));if (*pages) { -down_read (¤t->mm->mmap_sem); - result =get_user_pages (current, current->mm, user_addr, - *max_pages, (rw ==READ ),0 , *pages, -NULL ); -up_read (¤t->mm->mmap_sem); + result =get_user_pages_fast (user_addr, *max_pages, + (rw ==READ ), *pages);if (unlikely (result <=0 ))OBD_FREE_LARGE (*pages, *max_pages *sizeof (**pages));
Quoted 1 line
}
Cheers, Andreas
Signature · 4 lines
Andreas Dilger Lustre Software Architect Intel High Performance Data Division
Re: [PATCH 19/26] ivtv: Convert driver to use get_user_pages_unlocked()
In reply to Jan KaraHi Jan:
<rant>
This patch alone does not have suffcient information for me to evaluate it. get_user_pages_unlocked() is added in another patch which I did not receive, and which I cannot find in any list archives.
I wasted quite a bit of time looking for this additional patch:
</rant>
If I found the correct patch for adding get_user_pages_unlocked(), then the patch below looks fine.
Regards,
Andy
Quoted 41 lines · Jan Kara
--- drivers/media/pci/ivtv/ivtv-udma.c | 6 ++---- drivers/media/pci/ivtv/ivtv-yuv.c | 12 ++++++------ 2 files changed, 8 insertions(+), 10 deletions(-)drivers/media/pci/ivtv/ivtv-udma.c +2
-4@@ -124,10 +124,8 @@ int ivtv_udma_setup(struct ivtv *itv, unsigned long ivtv_dest_addr, }/* Get user pages for DMA Xfer */ -down_read (¤t->mm->mmap_sem); - err =get_user_pages (current, current->mm, - user_dma.uaddr, user_dma.page_count,0 ,1 , dma->map,NULL ); -up_read (¤t->mm->mmap_sem); + err =get_user_pages_unlocked (current, current->mm, user_dma.uaddr, + user_dma.page_count,0 ,1 , dma->map);if (user_dma.page_count != err) { IVTV_DEBUG_WARN("failed to map user pages, returned %d instead of %d\n",drivers/media/pci/ivtv/ivtv-yuv.c +2
-4@@ -75,15 +75,15 @@ static int ivtv_yuv_prep_user_dma(struct ivtv *itv, struct ivtv_user_dma *dma,ivtv_udma_get_page_info (&uv_dma, (unsigned long )args->uv_source,360 * uv_decode_height);/* Get user pages for DMA Xfer */ -down_read (¤t->mm->mmap_sem); - y_pages =get_user_pages (current, current->mm, y_dma.uaddr, y_dma.page_count,0 ,1 , &dma->map[0 ],NULL ); + y_pages =get_user_pages_unlocked (current, current->mm, y_dma.uaddr, + y_dma.page_count,0 ,1 , &dma->map[0 ]); uv_pages =0 ;/* silence gcc. value is set and consumed only if: */ if (y_pages == y_dma.page_count) { - uv_pages = get_user_pages(current, current->mm, - uv_dma.uaddr, uv_dma.page_count, 0, 1,
drivers/media/pci/ivtv/ivtv-yuv.c static int ivtv_yuv_prep_user_dma(struct ivtv *itv, struct ivtv_user_dma *dma, · line 89
Quoted 10 lines
drivers/media/pci/ivtv/ivtv-yuv.c +4
-2- &dma->map[y_pages], NULL); + uv_pages =get_user_pages_unlocked (current, current->mm, + uv_dma.uaddr, + uv_dma.page_count,0 ,1 , + &dma->map[y_pages]); } -up_read (¤t->mm->mmap_sem);if (y_pages != y_dma.page_count || uv_pages != uv_dma.page_count) {int rc = -EFAULT ;
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
RE: [PATCH 23/26] ib: Convert qib_get_user_pages() to get_user_pages_unlocked()
In reply to Jan KaraQuoted 35 lines
-----Original Message-----
From: Jan Kara [mailto:[email protected]]
Sent: Friday, October 04, 2013 2:33 PM
To: Marciniszyn, Mike
Cc: Jan Kara; LKML; [email protected]; infinipath; Roland Dreier; linux- [email protected]
Subject: Re: [PATCH 23/26] ib: Convertqib_get_user_pages()toget_user_pages_unlocked()Quoted 4 lines · Mike
Quoted 4 lines
Convert
qib_get_user_pages()to useget_user_pages_unlocked(). This shortens the section where we holdmmap_semfor writing and also removes the knowledge aboutget_user_pages()locking from ipath driver. We also fix a bug in testing pinned number of pages whenchanging the code.
Quoted 5 lines
Quoted 1 line
This patch and the sibling ipath patch will nominally take the
mmap_sem
twice where the old routine only took it once. This is a performance
issue.It will take
mmap_semonly once during normal operation. Only ifget_user_pages_unlocked()fail, we have to takemmap_semagain to undo the change ofmm->pinned_vm.Quoted 1 line
Is the intent here to deprecate
get_user_pages()?Well, as much as I'd like to, there are really places in mm code which need
to callget_user_pages()while holdingmmap_semto be able to inspect corresponding vmas etc. So I want to reduceget_user_pages()use as much as possible but I'm not really hoping in completely removing it.Quoted 3 lines
I agree, the old code's lock limit test is broke and needs to be fixed. I like the elimination of the silly wrapper routine!
Quoted 8 lines
Quoted 2 lines
Could the lock limit test be pushed into another version of the wrapper so that there is only one set of
mmap_semtransactions?I'm sorry, I don't understand what you mean here...
Honza
--
Jan Kara <[email protected]>
SUSE Labs, CR
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
RE: [PATCH 23/26] ib: Convert qib_get_user_pages() to get_user_pages_unlocked()
In reply to Jan KaraQuoted 8 lines
Quoted 3 lines
This patch and the sibling ipath patch will nominally take the
mmap_sem
twice where the old routine only took it once. This is a performance
issue.It will take
mmap_semonly once during normal operation. Only ifget_user_pages_unlocked()fail, we have to takemmap_semagain to undo the change ofmm->pinned_vm.Quoted 1 line
Is the intent here to deprecate
get_user_pages()?
The old code looked like:__qib_get_user_pages()
(broken) ulimit test
for (...)
get_user_pages()qib_get_user_pages()
mmap_sem lock
__qib_get_user_pages()
mmap_sem() unlockThe new code is:
get_user_pages_unlocked()
mmap_sem lock
get_user_pages()
mmap_sem unlock
qib_get_user_pages()
mmap_sem lock
ulimit test and locked pages maintenance
mmap_sem unlock
for (...)
get_user_pages_unlocked()I count an additional pair of mmap_sem transactions in the normal case.
Quoted 5 lines
Quoted 3 lines
Could the lock limit test be pushed into another version of the wrapper so that there is only one set of
mmap_semtransactions?I'm sorry, I don't understand what you mean here...
This is what I had in mind:
get_user_pages_ulimit_unlocked()
mmap_sem lock
ulimit test and locked pages maintenance (from qib/ipath)
for (...)
get_user_pages_unlocked()
mmap_sem unlockqib_get_user_pages()
get_user_pages_ulimit_unlocked()
This really pushes the code into a new wrapper common to ipath/qib and any others that might want to combine locking with ulimit enforcement.
Mike
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Jan Kara
reviewerRe: [PATCH 19/26] ivtv: Convert driver to use get_user_pages_unlocked()
In reply to Andy WallsHello,
Quoted 10 lines · Andy Walls
<rant>
This patch alone does not have suffcient information for me to evaluate it.get_user_pages_unlocked()is added in another patch which I did not receive, and which I cannot find in any list archives.I wasted quite a bit of time looking for this additional patch:
</rant>
Sorry, I should have CCed that patch to driver maintainers as well.
Quoted 5 lines
If I found the correct patch for adding
get_user_pages_unlocked(), then the patch below looks fine.Reviewed-by Andy Walls <[email protected]>Acked-by Andy Walls <[email protected]>
Thanks.
Honza
Quoted 54 lines · Jan Kara
Quoted 51 lines
--- drivers/media/pci/ivtv/ivtv-udma.c | 6 ++---- drivers/media/pci/ivtv/ivtv-yuv.c | 12 ++++++------ 2 files changed, 8 insertions(+), 10 deletions(-)drivers/media/pci/ivtv/ivtv-udma.c +2
-4@@ -124,10 +124,8 @@ int ivtv_udma_setup(struct ivtv *itv, unsigned long ivtv_dest_addr, }/* Get user pages for DMA Xfer */ -down_read (¤t->mm->mmap_sem); - err =get_user_pages (current, current->mm, - user_dma.uaddr, user_dma.page_count,0 ,1 , dma->map,NULL ); -up_read (¤t->mm->mmap_sem); + err =get_user_pages_unlocked (current, current->mm, user_dma.uaddr, + user_dma.page_count,0 ,1 , dma->map);if (user_dma.page_count != err) { IVTV_DEBUG_WARN("failed to map user pages, returned %d instead of %d\n",drivers/media/pci/ivtv/ivtv-yuv.c +6
-6@@ -75,15 +75,15 @@ static int ivtv_yuv_prep_user_dma(struct ivtv *itv, struct ivtv_user_dma *dma,ivtv_udma_get_page_info (&uv_dma, (unsigned long )args->uv_source,360 * uv_decode_height);/* Get user pages for DMA Xfer */ -down_read (¤t->mm->mmap_sem); - y_pages =get_user_pages (current, current->mm, y_dma.uaddr, y_dma.page_count,0 ,1 , &dma->map[0 ],NULL ); + y_pages =get_user_pages_unlocked (current, current->mm, y_dma.uaddr, + y_dma.page_count,0 ,1 , &dma->map[0 ]); uv_pages =0 ;/* silence gcc. value is set and consumed only if: */ if (y_pages == y_dma.page_count) { - uv_pages =get_user_pages (current, current->mm, - uv_dma.uaddr, uv_dma.page_count,0 ,1 , - &dma->map[y_pages],NULL ); + uv_pages =get_user_pages_unlocked (current, current->mm, + uv_dma.uaddr, + uv_dma.page_count,0 ,1 , + &dma->map[y_pages]); } -up_read (¤t->mm->mmap_sem);if (y_pages != y_dma.page_count || uv_pages != uv_dma.page_count) {int rc = -EFAULT ;
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Jan Kara
reviewerRe: [PATCH 23/26] ib: Convert qib_get_user_pages() to get_user_pages_unlocked()
In reply to Marciniszyn, MikeQuoted 27 lines · Mike
Quoted 8 lines
Quoted 3 lines
This patch and the sibling ipath patch will nominally take the
mmap_sem
twice where the old routine only took it once. This is a performance
issue.It will take
mmap_semonly once during normal operation. Only ifget_user_pages_unlocked()fail, we have to takemmap_semagain to undo the change ofmm->pinned_vm.Quoted 1 line
Is the intent here to deprecate
get_user_pages()?The old code looked like:
__qib_get_user_pages()(broken) ulimit test for (...) get_user_pages()
qib_get_user_pages()mmap_sem lock __qib_get_user_pages() mmap_sem() unlockThe new code is:
get_user_pages_unlocked()
mmap_semlock
get_user_pages()
mmap_semunlock
Quoted 8 lines
qib_get_user_pages()mmap_sem lock ulimit test and locked pages maintenance mmap_sem unlock for (...) get_user_pages_unlocked()I count an additional pair of
mmap_semtransactions in the normal case.
Ah, sorry, you are right.
Quoted 14 lines
Quoted 4 lines
Quoted 2 lines
Could the lock limit test be pushed into another version of the wrapper so that there is only one set of
mmap_semtransactions?I'm sorry, I don't understand what you mean here...
This is what I had in mind:
get_user_pages_ulimit_unlocked()mmap_sem lock ulimit test and locked pages maintenance (from qib/ipath) for (...) get_user_pages_unlocked() mmap_sem unlock
Quoted 5 lines
qib_get_user_pages()
get_user_pages_ulimit_unlocked()This really pushes the code into a new wrapper common to ipath/qib and any others that might want to combine locking with ulimit enforcement.
We could do that but frankly, I'd rather change ulimit enforcement to not
require mmap_sem and use atomic counter instead. I'll see what I can do.
Honza
Signature · 8 lines
Jan Kara <[email protected]> SUSE Labs, CR -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Jan Kara
reviewerRe: [PATCH 18/26] mm: Convert process_vm_rw_pages() to use get_user_pages_unlocked()
In reply to KOSAKI MotohiroQuoted 44 lines · KOSAKI Motohiro
Quoted 30 lines · Jan Kara
Quoted 13 lines · KOSAKI Motohiro
Quoted 12 lines · Jan Kara
Signed-off-by Jan Kara <[email protected]>--- mm/process_vm_access.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-)mm/process_vm_access.c +0
-0@@ -64,12 +64,8 @@ static int process_vm_rw_pages(struct task_struct *task, *bytes_copied =0 ;Quoted 16 lines
mm/process_vm_access.c static int process_vm_rw_pages(struct task_struct *task, · line 71
Quoted 12 lines
mm/process_vm_access.c +2
-6/* Get the pages we're interested in */ -down_read (&mm->mmap_sem); - pages_pinned =get_user_pages (task, mm, pa, - nr_pages_to_copy, - vm_write,0 , process_pages,NULL ); -up_read (&mm->mmap_sem); - + pages_pinned =get_user_pages_unlocked (task, mm, pa, nr_pages_to_copy, + vm_write,0 , process_pages);if (pages_pinned != nr_pages_to_copy) { rc = -EFAULT ; goto end;This is wrong because original code is wrong. In this function, page may be pointed to anon pages. Then, you should keep to take
mmap_semuntil finish to copying. Otherwise concurrentfork()makes nasty COW issue.Quoted 10 lines
Hum, can you be more specific? I suppose you are speaking about situation
when the remote task we are copying data from/to does fork whileprocess_vm_rw_pages()runs. If we are copying data from remote task, I don't see how COW could cause any problem. If we are copying to remote task and fork happens afterget_user_pages()but beforecopy_to_user()then I can see we might be having some trouble.copy_to_user()would then copy data into both original remote process and its child thus essentially bypassing COW. If the child process manages to COW some of the pages beforecopy_to_user()happens, it can even see only some of the pages. Is that what you mean?scenario 1: vm_write==0
Quoted 7 lines
Process P1 call
get_user_pages(pa, process_pages)inprocess_vm_rw_pagesP1 unlockmmap_sem.
Process P2 callfork(). and make P3.
P2 write memory pa. now the "process_pages" is owned by P3 (the child process) P3 write memory pa. and then the content of "process_pages" is changed. P1 readprocess_pagesas P2's page. but actually, it is P3's data. Then,
P1 observe garbage, at least unintended, data was read.
Yeah, this really looks buggy because P1 can see data in (supposedly)
P2's address space which P2 never wrote there.
Quoted 10 lines
scenario 2: vm_write==1
Process P1 call
get_user_pages(pa, process_pages)inprocess_vm_rw_pages. It makes COW break and any anon page sharing broke.
P1 unlockmmap_sem.
P2 callfork(). and make P3. And, now COW page sharing is restored. P2 write memory pa. now the "process_pages" is owned by P3. P3 write memory pa. it mean P3 changes "process_pages".
P1 writeprocess_pagesas P2's page. but actually, it is P3's. It override above P3's write and then P3 observe data corruption.
Yep, this is a similar problem as above. Thanks for pointing this out.
Quoted 4 lines
The solution is to keep holding
mmap_semuntil finishingprocess_pagesaccess becausemmap_semprevent fork. and then race never be happen. I mean you cann't useget_user_pages_unlock()if target address point to anon pages.
Yeah, if you are accessing third party mm, you've convinced me you
currently need mmap_sem to avoid problems with COW on anon pages. I'm just thinking that this "hold mmap_sem to prevent fork" is somewhat subtle (definitely would deserve a comment) and if it would be needed in more places we might be better off if we have a more explicit mechanism for that (like a special lock, fork sequence count, or something like that). Anyway I'll have this in mind and if I see other places that need this, I'll try to come up with some solution. Thanks again for explanation.
Honza
Signature · 8 lines
Jan Kara <[email protected]> SUSE Labs, CR -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Jan Kara
reviewerQuoted 45 lines · KOSAKI Motohiro
Quoted 24 lines · KOSAKI Motohiro
Quoted 15 lines · Jan Kara
Quoted 14 lines · Christoph Hellwig
Quoted 12 lines · Jan Kara
Hello,
In my quest for changing locking around page faults to make things easier for
filesystems I found outget_user_pages()users could use a cleanup. The knowledge about necessary locking forget_user_pages()is in tons of places in drivers and quite a few of them actually get it wrong (don't havemmap_semwhen callingget_user_pages()or holdmmap_semwhen callingcopy_from_user()in the surrounding code). Rather often this actually doesn't seem necessary. This patch series converts lots of places to use eitherget_user_pages_fast()or a new simple wrapperget_user_pages_unlocked()to remove the knowledge ofmmap_semfrom the drivers. I'm still looking into converting a few remaining drivers (most notably v4l2) which are more complex.Quoted 7 lines
Quoted 2 lines
Even looking over the kerneldoc comment next to it I still fail to understand when you'd want to use
get_user_pages_fastand when not.AFAIU
get_user_pages_fast()should be used
1) if you don't need any specialget_user_pages()arguments (like calling it for mm of a different process, forcing COW, or similar).
2) you don't expect pages to be unmapped (thenget_user_pages_fast()is actually somewhat slower because it walks page tables twice).Quoted 19 lines
If target page point to anon or private mapping pages,
get_user_pages_fast()is fork unsafe.O_DIRECTman pages describe a bit about this.see http://man7.org/linux/man-pages/man2/open.2.html
Quoted 13 lines
O_DIRECTI/Os should never be run concurrently with the fork(2) system call, if the memory buffer is a private mapping (i.e., any mapping created with the mmap(2)MAP_PRIVATEflag; this includes memory allocated on the heap and statically allocated buffers). Any such I/Os, whether submitted via an asynchronous I/O interface or from another thread in the process, should be completed before fork(2) is called. Failure to do so can result in data corruption and undefined behavior in parent and child processes. This restriction does not apply when the memory buffer for theO_DIRECTI/Os was created using shmat(2) or mmap(2) with theMAP_SHAREDflag. Nor does this restriction apply when the memory buffer has been advised asMADV_DONTFORKwith madvise(2), ensuring that it will not be available to the child after fork(2).
Quoted 3 lines
IMHO,
get_user_pages_fast()should be renamed toget_user_pages_quirk(). Its semantics is not equal toget_user_pages(). When someone simply substituteget_user_pages()toget_user_pages_fast(), they might see huge trouble.
I forgot about this speciality (and actually comments didn't remind me
:(). But thinking about this some more get_user_pages_fast() seems as save as get_user_pages() in presence of threads sharing mm, doesn't it? Because while get_user_pages() are working, other thread can happilly trigger COW on some of the pages and thus get_user_pages() can return pages some of which are invisible in our mm by the time get_user_pages() returns. So although in practice I agree problems of get_user_pages_fast() with fork(2) are more visible, in essence they are still present with clone(2) and get_user_pages().
Honza
Signature · 8 lines
Jan Kara <[email protected]> SUSE Labs, CR -- To unsubscribe, send a message with 'unsubscribe linux-aio' in the body to [email protected]. For more info on Linux AIO, see: http://www.kvack.org/aio/ Don't email: <a href=mailto:"[email protected]">[email protected]</a>
Re: [PATCH 18/26] mm: Convert process_vm_rw_pages() to use get_user_pages_unlocked()
In reply to Jan KaraQuoted 65 lines · Jan Kara
Quoted 44 lines · KOSAKI Motohiro
Quoted 30 lines · Jan Kara
Quoted 13 lines · KOSAKI Motohiro
Quoted 12 lines · Jan Kara
Signed-off-by Jan Kara <[email protected]>--- mm/process_vm_access.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-)mm/process_vm_access.c +0
-0@@ -64,12 +64,8 @@ static int process_vm_rw_pages(struct task_struct *task, *bytes_copied =0 ;Quoted 16 lines
mm/process_vm_access.c static int process_vm_rw_pages(struct task_struct *task, · line 71
Quoted 12 lines
mm/process_vm_access.c +2
-6/* Get the pages we're interested in */ -down_read (&mm->mmap_sem); - pages_pinned =get_user_pages (task, mm, pa, - nr_pages_to_copy, - vm_write,0 , process_pages,NULL ); -up_read (&mm->mmap_sem); - + pages_pinned =get_user_pages_unlocked (task, mm, pa, nr_pages_to_copy, + vm_write,0 , process_pages);if (pages_pinned != nr_pages_to_copy) { rc = -EFAULT ; goto end;This is wrong because original code is wrong. In this function, page may be pointed to anon pages. Then, you should keep to take
mmap_semuntil finish to copying. Otherwise concurrentfork()makes nasty COW issue.Quoted 10 lines
Hum, can you be more specific? I suppose you are speaking about situation
when the remote task we are copying data from/to does fork whileprocess_vm_rw_pages()runs. If we are copying data from remote task, I don't see how COW could cause any problem. If we are copying to remote task and fork happens afterget_user_pages()but beforecopy_to_user()then I can see we might be having some trouble.copy_to_user()would then copy data into both original remote process and its child thus essentially bypassing COW. If the child process manages to COW some of the pages beforecopy_to_user()happens, it can even see only some of the pages. Is that what you mean?scenario 1: vm_write==0
Quoted 7 lines
Process P1 call
get_user_pages(pa, process_pages)inprocess_vm_rw_pagesP1 unlockmmap_sem.
Process P2 callfork(). and make P3.
P2 write memory pa. now the "process_pages" is owned by P3 (the child process) P3 write memory pa. and then the content of "process_pages" is changed. P1 readprocess_pagesas P2's page. but actually, it is P3's data. Then,
P1 observe garbage, at least unintended, data was read.Yeah, this really looks buggy because P1 can see data in (supposedly)
P2's address space which P2 never wrote there.Quoted 10 lines
scenario 2: vm_write==1
Process P1 call
get_user_pages(pa, process_pages)inprocess_vm_rw_pages. It makes COW break and any anon page sharing broke.
P1 unlockmmap_sem.
P2 callfork(). and make P3. And, now COW page sharing is restored. P2 write memory pa. now the "process_pages" is owned by P3. P3 write memory pa. it mean P3 changes "process_pages".
P1 writeprocess_pagesas P2's page. but actually, it is P3's. It override above P3's write and then P3 observe data corruption.
Quoted 7 lines
Yep, this is a similar problem as above. Thanks for pointing this out.
Quoted 4 lines
The solution is to keep holding
mmap_semuntil finishingprocess_pagesaccess becausemmap_semprevent fork. and then race never be happen. I mean you cann't useget_user_pages_unlock()if target address point to anon pages.Yeah, if you are accessing third party mm,
one correction. the condition is,
- third party mm, or
- current process have multiple threads and other threads makes fork() and COW break
Quoted 6 lines
you've convinced me you
currently needmmap_semto avoid problems with COW on anon pages. I'm just thinking that this "holdmmap_semto prevent fork" is somewhat subtle (definitely would deserve a comment) and if it would be needed in more places we might be better off if we have a more explicit mechanism for that (like a special lock, fork sequence count, or something like that).
Hmm..
Actually, I tried this several years ago. But MM people disliked it because they prefer faster kernel than simple code. Any additional lock potentially makes slower the kernel.
However, I fully agree the current mechanism is too complex and misleading, or at least, very hard to understanding. So, any improvement idea is welcome.
Quoted 3 lines
Anyway
I'll have this in mind and if I see other places that need this, I'll try to come up with some solution. Thanks again for explanation.
NP.
I tried to explain this at MM summit. but my English is too poor and I couldn't explain enough to you. Sorry about that.
Anyway, I'd like to fix process_vm_rw_pages() before my memory will be flushed again. Thank you for helping remember this bug.
Quoted 46 lines · Jan Kara
Quoted 45 lines · KOSAKI Motohiro
Quoted 43 lines · KOSAKI Motohiro
Quoted 15 lines · Jan Kara
Quoted 14 lines · Christoph Hellwig
Quoted 12 lines · Jan Kara
Hello,
In my quest for changing locking around page faults to make things easier for
filesystems I found outget_user_pages()users could use a cleanup. The knowledge about necessary locking forget_user_pages()is in tons of places in drivers and quite a few of them actually get it wrong (don't havemmap_semwhen callingget_user_pages()or holdmmap_semwhen callingcopy_from_user()in the surrounding code). Rather often this actually doesn't seem necessary. This patch series converts lots of places to use eitherget_user_pages_fast()or a new simple wrapperget_user_pages_unlocked()to remove the knowledge ofmmap_semfrom the drivers. I'm still looking into converting a few remaining drivers (most notably v4l2) which are more complex.Quoted 7 lines
Quoted 2 lines
Even looking over the kerneldoc comment next to it I still fail to understand when you'd want to use
get_user_pages_fastand when not.AFAIU
get_user_pages_fast()should be used
1) if you don't need any specialget_user_pages()arguments (like calling it for mm of a different process, forcing COW, or similar).
2) you don't expect pages to be unmapped (thenget_user_pages_fast()is actually somewhat slower because it walks page tables twice).If target page point to anon or private mapping pages,
get_user_pages_fast()is fork unsafe.O_DIRECTman pages describe a bit about this.see http://man7.org/linux/man-pages/man2/open.2.html
Quoted 13 lines
O_DIRECTI/Os should never be run concurrently with the fork(2) system call, if the memory buffer is a private mapping (i.e., any mapping created with the mmap(2)MAP_PRIVATEflag; this includes memory allocated on the heap and statically allocated buffers). Any such I/Os, whether submitted via an asynchronous I/O interface or from another thread in the process, should be completed before fork(2) is called. Failure to do so can result in data corruption and undefined behavior in parent and child processes. This restriction does not apply when the memory buffer for theO_DIRECTI/Os was created using shmat(2) or mmap(2) with theMAP_SHAREDflag. Nor does this restriction apply when the memory buffer has been advised asMADV_DONTFORKwith madvise(2), ensuring that it will not be available to the child after fork(2).
Quoted 6 lines
Quoted 3 lines
IMHO,
get_user_pages_fast()should be renamed toget_user_pages_quirk(). Its semantics is not equal toget_user_pages(). When someone simply substituteget_user_pages()toget_user_pages_fast(), they might see huge trouble.I forgot about this speciality (and actually comments didn't remind me
:(). But thinking about this some moreget_user_pages_fast()seems as save asget_user_pages()in presence of threads sharing mm, doesn't it?
It depends.
If there is any guarantee that other threads don't touch the same page which retrieved get_user_pages(), get_user_pages_fast() give us brilliant fast way. Example, as far as I heard form IB guys, the userland library of the infiniband stack uses madvise(MADV_DONTFORK), and then they don't need to care COW issue and can choose fastest way. An another example is a futex. futex doesn't use the contents of the pages, it uses vaddr only for looking up key. Then, it also doesn't have COW issue.
I don't know other cases. But as far as I know, everything is case-by-case.
Quoted 4 lines
Because
whileget_user_pages()are working, other thread can happilly trigger COW on some of the pages and thusget_user_pages()can return pages some of which are invisible in our mm by the timeget_user_pages()returns.
If you are talking about get_user_pages() instead of get_user_pages_fast(), this can't be happen because page-fault takes mmap_sem too.
I would say, mmap_sem has too fat responsibility really.
Quoted 7 lines
So
although in practice I agree problems ofget_user_pages_fast()with fork(2) are more visible, in essence they are still present with clone(2) andget_user_pages().Honza
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Jan Kara
reviewerQuoted 66 lines · KOSAKI Motohiro
Quoted 46 lines · Jan Kara
Quoted 45 lines · KOSAKI Motohiro
Quoted 43 lines · KOSAKI Motohiro
Quoted 15 lines · Jan Kara
Quoted 14 lines · Christoph Hellwig
Quoted 12 lines · Jan Kara
Hello,
In my quest for changing locking around page faults to make things easier for
filesystems I found outget_user_pages()users could use a cleanup. The knowledge about necessary locking forget_user_pages()is in tons of places in drivers and quite a few of them actually get it wrong (don't havemmap_semwhen callingget_user_pages()or holdmmap_semwhen callingcopy_from_user()in the surrounding code). Rather often this actually doesn't seem necessary. This patch series converts lots of places to use eitherget_user_pages_fast()or a new simple wrapperget_user_pages_unlocked()to remove the knowledge ofmmap_semfrom the drivers. I'm still looking into converting a few remaining drivers (most notably v4l2) which are more complex.Quoted 7 lines
Quoted 2 lines
Even looking over the kerneldoc comment next to it I still fail to understand when you'd want to use
get_user_pages_fastand when not.AFAIU
get_user_pages_fast()should be used
1) if you don't need any specialget_user_pages()arguments (like calling it for mm of a different process, forcing COW, or similar).
2) you don't expect pages to be unmapped (thenget_user_pages_fast()is actually somewhat slower because it walks page tables twice).If target page point to anon or private mapping pages,
get_user_pages_fast()is fork unsafe.O_DIRECTman pages describe a bit about this.see http://man7.org/linux/man-pages/man2/open.2.html
Quoted 13 lines
O_DIRECTI/Os should never be run concurrently with the fork(2) system call, if the memory buffer is a private mapping (i.e., any mapping created with the mmap(2)MAP_PRIVATEflag; this includes memory allocated on the heap and statically allocated buffers). Any such I/Os, whether submitted via an asynchronous I/O interface or from another thread in the process, should be completed before fork(2) is called. Failure to do so can result in data corruption and undefined behavior in parent and child processes. This restriction does not apply when the memory buffer for theO_DIRECTI/Os was created using shmat(2) or mmap(2) with theMAP_SHAREDflag. Nor does this restriction apply when the memory buffer has been advised asMADV_DONTFORKwith madvise(2), ensuring that it will not be available to the child after fork(2).Quoted 6 lines
Quoted 3 lines
IMHO,
get_user_pages_fast()should be renamed toget_user_pages_quirk(). Its semantics is not equal toget_user_pages(). When someone simply substituteget_user_pages()toget_user_pages_fast(), they might see huge trouble.I forgot about this speciality (and actually comments didn't remind me
:(). But thinking about this some moreget_user_pages_fast()seems as save asget_user_pages()in presence of threads sharing mm, doesn't it?It depends.
If there is any guarantee that other threads don't touch the same page which retrieved
get_user_pages(),get_user_pages_fast()give us brilliant fast way. Example, as far as I heard form IB guys, the userland library of the infiniband stack usesmadvise(MADV_DONTFORK), and then they don't need to care COW issue and can choose fastest way. An another example is a futex. futex doesn't use the contents of the pages, it uses vaddr only for looking up key. Then, it also doesn't have COW issue.I don't know other cases. But as far as I know, everything is case-by-case.
Quoted 7 lines
Quoted 4 lines
Because
whileget_user_pages()are working, other thread can happilly trigger COW on some of the pages and thusget_user_pages()can return pages some of which are invisible in our mm by the timeget_user_pages()returns.If you are talking about
get_user_pages()instead ofget_user_pages_fast(), this can't be happen because page-fault takesmmap_semtoo.
But both take mmap_sem for reading, so they won't exclude each other...
Quoted 1 line
I would say,
mmap_semhas too fat responsibility really.
I heartily agree with this. And that makes any changes to it really hard.
I've once went through all the places in kernel which acquired mmap_sem. If I remember right, there were close to two hundreds of them! Basic use is the protection of vma tree but then we have less obvious stuff like protection of some other fields in mm_struct, fork exclusion, and god knows what...
Honza
Signature · 8 lines
Jan Kara <[email protected]> SUSE Labs, CR -- To unsubscribe, send a message with 'unsubscribe linux-aio' in the body to [email protected]. For more info on Linux AIO, see: http://www.kvack.org/aio/ Don't email: <a href=mailto:"[email protected]">[email protected]</a>
Jan Kara
reviewerRe: [PATCH 23/26] ib: Convert qib_get_user_pages() to get_user_pages_unlocked()
In reply to Jan KaraQuoted 52 lines · Jan Kara
Quoted 27 lines · Mike
Quoted 8 lines
Quoted 3 lines
This patch and the sibling ipath patch will nominally take the
mmap_sem
twice where the old routine only took it once. This is a performance
issue.It will take
mmap_semonly once during normal operation. Only ifget_user_pages_unlocked()fail, we have to takemmap_semagain to undo the change ofmm->pinned_vm.Quoted 1 line
Is the intent here to deprecate
get_user_pages()?The old code looked like:
__qib_get_user_pages()(broken) ulimit test for (...) get_user_pages()
qib_get_user_pages()mmap_sem lock __qib_get_user_pages() mmap_sem() unlockThe new code is:
get_user_pages_unlocked()
mmap_semlock
get_user_pages()
mmap_semunlockQuoted 8 lines
qib_get_user_pages()mmap_sem lock ulimit test and locked pages maintenance mmap_sem unlock for (...) get_user_pages_unlocked()I count an additional pair of
mmap_semtransactions in the normal case.Ah, sorry, you are right.
Quoted 14 lines
Quoted 4 lines
Quoted 2 lines
Could the lock limit test be pushed into another version of the wrapper so that there is only one set of
mmap_semtransactions?I'm sorry, I don't understand what you mean here...
This is what I had in mind:
get_user_pages_ulimit_unlocked()mmap_sem lock ulimit test and locked pages maintenance (from qib/ipath) for (...) get_user_pages_unlocked() mmap_sem unlock
Quoted 7 lines
Quoted 5 lines
qib_get_user_pages()
get_user_pages_ulimit_unlocked()This really pushes the code into a new wrapper common to ipath/qib and any others that might want to combine locking with ulimit enforcement.
We could do that but frankly, I'd rather change ulimit enforcement to not
requiremmap_semand use atomic counter instead. I'll see what I can do.
OK, so something like the attached patch (compile tested only). What do
you think? I'm just not 100% sure removing mmap_sem surrounding stuff like __ipath_release_user_pages() is safe. I don't see a reason why it shouldn't be - we have references to the pages and we only mark them dirty and put the reference - but maybe I miss something subtle...
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Quoted 1 line
From
44fe90e8303b293370f077ae665d6e43846cf277Mon Sep 17 00:00:00 2001
From: Jan Kara <[email protected]>
Date: Tue, 8 Oct 2013 14:16:24 +0200
Subject: [PATCH] mm: Switch mm->pinned_vm to atomic_long_t
Currently updates to mm->pinned_vm were protected by mmap_sem. kernel/events/core.c actually held it only for reading so that may have been racy. The only other user - Infiniband - used mmap_sem for writing but it caused quite some complications to it.
So switch mm->pinned_vm and convert all the places using it. This allows quite some simplifications to Infiniband code and it now doesn't have to care about mmap_sem at all.
--- drivers/infiniband/core/umem.c | 56 +++------------------ drivers/infiniband/core/uverbs_cmd.c | 1 - drivers/infiniband/core/uverbs_main.c | 2 - drivers/infiniband/hw/ipath/ipath_file_ops.c | 2 +- drivers/infiniband/hw/ipath/ipath_kernel.h | 1 - drivers/infiniband/hw/ipath/ipath_user_pages.c | 70 +++----------------------- drivers/infiniband/hw/qib/qib_user_pages.c | 20 ++------ fs/proc/task_mmu.c | 2 +- include/linux/mm_types.h | 2 +- include/rdma/ib_umem.h | 3 -- include/rdma/ib_verbs.h | 1 - kernel/events/core.c | 13 +++-- 12 files changed, 29 insertions(+), 144 deletions(-)
drivers/infiniband/core/umem.c +7 -49
@@ -80,6 +80,7 @@ struct ib_umem *ib_umem_get(struct ib_ucontext *context, unsigned long addr, {struct ib_umem *umem;struct page **page_list; +struct mm_struct *mm = current->mm;struct ib_umem_chunk *chunk;unsigned long locked;unsigned long lock_limit;
@@ -126,20 +127,13 @@ struct ib_umem *ib_umem_get(struct ib_ucontext *context, unsigned long addr, npages =PAGE_ALIGN (size + umem->offset) >>PAGE_SHIFT ; -down_write (¤t->mm->mmap_sem); - lock_limit =rlimit (RLIMIT_MEMLOCK ) >>PAGE_SHIFT ; locked = npages; -if (npages + current->mm->pinned_vm > lock_limit && +if (atomic_long_add_return (&mm->pinned_vm, npages) > lock_limit && !capable (CAP_IPC_LOCK )) { -up_write (¤t->mm->mmap_sem); -kfree (umem); -free_page ((unsigned long ) page_list); -return ERR_PTR (-ENOMEM ); + ret = -ENOMEM ; + goto out; } - current->mm->pinned_vm += npages; - -up_write (¤t->mm->mmap_sem); cur_base = addr &PAGE_MASK ;
@@ -201,9 +195,7 @@ out:if (ret <0 ) {__ib_umem_release (context->device, umem,0 );kfree (umem); -down_write (¤t->mm->mmap_sem); - current->mm->pinned_vm -= locked; -up_write (¤t->mm->mmap_sem); +atomic_long_sub (&mm->pinned_vm, locked); }free_page ((unsigned long ) page_list);
@@ -211,17 +203,6 @@ out: }EXPORT_SYMBOL (ib_umem_get); -static void ib_umem_account (struct work_struct *work) -{ - struct ib_umem *umem = container_of(work, struct ib_umem, work); - -down_write (&umem->mm->mmap_sem); - umem->mm->pinned_vm -= umem->diff; -up_write (&umem->mm->mmap_sem); -mmput (umem->mm); -kfree (umem); -} -/** * ib_umem_release - release memory pinned with ib_umem_get * @umem: umem struct to release
@@ -234,37 +215,14 @@ void ib_umem_release(struct ib_umem *umem)__ib_umem_release (umem->context->device, umem,1 ); - mm =get_task_mm (current); + mm = current->mm;if (!mm) {kfree (umem);return ; } diff =PAGE_ALIGN (umem->length + umem->offset) >>PAGE_SHIFT ; - -/* -* We may be called with the mm's mmap_sem already held. This -* can happen when a userspace munmap() is the call that drops -* the last reference to our file and calls our release -* method. If there are memory regions to destroy, we'll end -* up here and not be able to take the mmap_sem. In that case -* we defer the vm_locked accounting to the system workqueue. -*/ -if (context->closing) { -if (!down_write_trylock (&mm->mmap_sem)) { -INIT_WORK (&umem->work, ib_umem_account); - umem->mm = mm; - umem->diff = diff; - -queue_work (ib_wq, &umem->work); -return ; - } - }else -down_write (&mm->mmap_sem); - - current->mm->pinned_vm -= diff; -up_write (&mm->mmap_sem); -mmput (mm); +atomic_long_sub (&mm->pinned_vm, diff);kfree (umem); }EXPORT_SYMBOL (ib_umem_release);
drivers/infiniband/core/uverbs_cmd.c +0 -1
@@ -332,7 +332,6 @@ ssize_t ib_uverbs_get_context(struct ib_uverbs_file *file,INIT_LIST_HEAD (&ucontext->ah_list);INIT_LIST_HEAD (&ucontext->xrcd_list);INIT_LIST_HEAD (&ucontext->rule_list); - ucontext->closing =0 ; resp.num_comp_vectors = file->device->num_comp_vectors;
drivers/infiniband/core/uverbs_main.c +0 -2
@@ -196,8 +196,6 @@ static int ib_uverbs_cleanup_ucontext(struct ib_uverbs_file *file,if (!context)return 0 ; - context->closing =1 ; -list_for_each_entry_safe (uobj, tmp, &context->ah_list, list) {struct ib_ah *ah = uobj->object;
drivers/infiniband/hw/ipath/ipath_file_ops.c +1 -1
@@ -2025,7 +2025,7 @@ static void unlock_expected_tids(struct ipath_portdata *pd) dd->ipath_pageshadow[i] =NULL ;pci_unmap_page (dd->pcidev, dd->ipath_physshadow[i],PAGE_SIZE ,PCI_DMA_FROMDEVICE ); -ipath_release_user_pages_on_close (&ps,1 ); +ipath_release_user_pages (&ps,1 ); cnt++; ipath_stats.sps_pageunlocks++; }
drivers/infiniband/hw/ipath/ipath_kernel.h +0 -1
@@ -1083,7 +1083,6 @@ static inline void ipath_sdma_desc_unreserve(struct ipath_devdata *dd, u16 cnt)int ipath_get_user_pages (unsigned long ,size_t ,struct page **);void ipath_release_user_pages (struct page **,size_t ); -void ipath_release_user_pages_on_close (struct page **,size_t );int ipath_eeprom_read (struct ipath_devdata *,u8 ,void *,int );int ipath_eeprom_write (struct ipath_devdata *,u8 ,const void *,int );int ipath_tempsense_read (struct ipath_devdata *,u8 regnum);
drivers/infiniband/hw/ipath/ipath_user_pages.c +8 -62
99 lines not on this page · raw message
drivers/infiniband/hw/qib/qib_user_pages.c +5 -15
@@ -68,16 +68,13 @@ int qib_get_user_pages(unsigned long start_page, size_t num_pages,int ret;struct mm_struct *mm = current->mm; -down_write (&mm->mmap_sem); lock_limit =rlimit (RLIMIT_MEMLOCK ) >>PAGE_SHIFT ; -if (mm->pinned_vm + num_pages > lock_limit && !capable (CAP_IPC_LOCK )) { -up_write (&mm->mmap_sem); +if (atomic_long_add_return (&mm->pinned_vm, num_pages) > lock_limit && + !capable (CAP_IPC_LOCK )) { ret = -ENOMEM ; goto bail; } - mm->pinned_vm += num_pages; -up_write (&mm->mmap_sem);for (got =0 ; got < num_pages; got += ret) { ret = get_user_pages_unlocked(current, mm,
@@ -94,9 +91,7 @@ int qib_get_user_pages(unsigned long start_page, size_t num_pages, bail_release:__qib_release_user_pages (p, got,0 ); -down_write (&mm->mmap_sem); - mm->pinned_vm -= num_pages; -up_write (&mm->mmap_sem); +atomic_long_sub (&mm->pinned_vm, num_pages); bail:return ret; }
@@ -135,13 +130,8 @@ dma_addr_t qib_map_page(struct pci_dev *hwdev, struct page *page,void qib_release_user_pages (struct page **p,size_t num_pages) { -if (current->mm)/* during close after signal, mm can be NULL */ -down_write (¤t->mm->mmap_sem); -__qib_release_user_pages (p, num_pages,1 ); -if (current->mm) { - current->mm->pinned_vm -= num_pages; -up_write (¤t->mm->mmap_sem); - } +if (current->mm) +atomic_long_sub (¤t->mm->pinned_vm, num_pages); }
fs/proc/task_mmu.c +1 -1
@@ -57,7 +57,7 @@ void task_mem(struct seq_file *m, struct mm_struct *mm) hiwater_vm << (PAGE_SHIFT -10 ), total_vm << (PAGE_SHIFT -10 ), mm->locked_vm << (PAGE_SHIFT -10 ), - mm->pinned_vm << (PAGE_SHIFT -10 ), +atomic_long_read (&mm->pinned_vm) << (PAGE_SHIFT -10 ), hiwater_rss << (PAGE_SHIFT -10 ), total_rss << (PAGE_SHIFT -10 ), data << (PAGE_SHIFT-10),
include/linux/mm_types.h +1 -1
@@ -355,7 +355,7 @@ struct mm_struct {unsigned long total_vm;/* Total pages mapped */ unsigned long locked_vm;/* Pages that have PG_mlocked set */ -unsigned long pinned_vm;/* Refcount permanently increased */ +atomic_long_t pinned_vm;/* Refcount permanently increased */ unsigned long shared_vm;/* Shared pages (files) */ unsigned long exec_vm;/* VM_EXEC & ~VM_WRITE */ unsigned long stack_vm;/* VM_GROWSUP/DOWN */
include/rdma/ib_umem.h +0 -3
@@ -47,9 +47,6 @@ struct ib_umem {int writable;int hugetlb;struct list_head chunk_list; -struct work_struct work; -struct mm_struct *mm; -unsigned long diff; };struct ib_umem_chunk {
include/rdma/ib_verbs.h +0 -1
@@ -961,7 +961,6 @@ struct ib_ucontext {struct list_head ah_list;struct list_head xrcd_list;struct list_head rule_list; -int closing; };struct ib_uobject {
kernel/events/core.c +6 -7
@@ -3922,7 +3922,7 @@ again:*/ atomic_long_sub ((size >>PAGE_SHIFT ) +1 , &mmap_user->locked_vm); - vma->vm_mm->pinned_vm -= mmap_locked; +atomic_long_sub (&vma->vm_mm->pinned_vm, mmap_locked);free_uid (mmap_user);ring_buffer_put (rb);/* could be last */
@@ -3944,7 +3944,7 @@ static int perf_mmap(struct file *file, struct vm_area_struct *vma)struct ring_buffer *rb;unsigned long vma_size;unsigned long nr_pages; -long user_extra, extra; +long user_extra, extra =0 ;int ret =0 , flags =0 ;/*
@@ -4006,16 +4006,14 @@ again: user_locked =atomic_long_read (&user->locked_vm) + user_extra; - extra =0 ;if (user_locked > user_lock_limit) extra = user_locked - user_lock_limit; lock_limit =rlimit (RLIMIT_MEMLOCK ); lock_limit >>=PAGE_SHIFT ; - locked = vma->vm_mm->pinned_vm + extra; -if ((locked > lock_limit) &&perf_paranoid_tracepoint_raw () && - !capable (CAP_IPC_LOCK )) { +if (atomic_long_add_return (&vma->vm_mm->pinned_vm, extra) > lock_limit && +perf_paranoid_tracepoint_raw () && !capable (CAP_IPC_LOCK )) { ret = -EPERM ; goto unlock; }
@@ -4039,7 +4037,6 @@ again: rb->mmap_user =get_current_user ();atomic_long_add (user_extra, &user->locked_vm); - vma->vm_mm->pinned_vm += extra;ring_buffer_attach (event, rb);rcu_assign_pointer (event->rb, rb);
@@ -4049,6 +4046,8 @@ again: unlock:if (!ret)atomic_inc (&event->mmap_count); +else if (extra) +atomic_long_sub (&vma->vm_mm->pinned_vm, extra);mutex_unlock (&event->mmap_mutex);/*
Signature · 1 line
1.8.1.4
Jan Kara
reviewerRe: [PATCH 23/26] ib: Convert qib_get_user_pages() to get_user_pages_unlocked()
Quoted 58 lines · Jan Kara
Quoted 57 lines · Jan Kara
Quoted 27 lines · Mike
Quoted 8 lines
Quoted 3 lines
This patch and the sibling ipath patch will nominally take the
mmap_sem
twice where the old routine only took it once. This is a performance
issue.It will take
mmap_semonly once during normal operation. Only ifget_user_pages_unlocked()fail, we have to takemmap_semagain to undo the change ofmm->pinned_vm.Quoted 1 line
Is the intent here to deprecate
get_user_pages()?The old code looked like:
__qib_get_user_pages()(broken) ulimit test for (...) get_user_pages()
qib_get_user_pages()mmap_sem lock __qib_get_user_pages() mmap_sem() unlockThe new code is:
get_user_pages_unlocked()
mmap_semlock
get_user_pages()
mmap_semunlockQuoted 8 lines
qib_get_user_pages()mmap_sem lock ulimit test and locked pages maintenance mmap_sem unlock for (...) get_user_pages_unlocked()I count an additional pair of
mmap_semtransactions in the normal case.Ah, sorry, you are right.
Quoted 19 lines
Quoted 4 lines
Quoted 2 lines
Could the lock limit test be pushed into another version of the wrapper so that there is only one set of
mmap_semtransactions?I'm sorry, I don't understand what you mean here...
This is what I had in mind:
get_user_pages_ulimit_unlocked()mmap_sem lock ulimit test and locked pages maintenance (from qib/ipath) for (...) get_user_pages_unlocked() mmap_sem unlock
qib_get_user_pages()
get_user_pages_ulimit_unlocked()This really pushes the code into a new wrapper common to ipath/qib and any others that might want to combine locking with ulimit enforcement.
Quoted 7 lines
Quoted 2 lines
We could do that but frankly, I'd rather change ulimit enforcement to not
requiremmap_semand use atomic counter instead. I'll see what I can do.OK, so something like the attached patch (compile tested only). What do
you think? I'm just not 100% sure removingmmap_semsurrounding stuff like__ipath_release_user_pages()is safe. I don't see a reason why it shouldn't be - we have references to the pages and we only mark them dirty and put the reference - but maybe I miss something subtle...
Ping? Any opinion on this?
Honza
Quoted 454 lines
From
44fe90e8303b293370f077ae665d6e43846cf277Mon Sep 17 00:00:00 2001
From: Jan Kara <[email protected]>
Date: Tue, 8 Oct 2013 14:16:24 +0200
Subject: [PATCH] mm: Switchmm->pinned_vmtoatomic_long_tCurrently updates to
mm->pinned_vmwere protected bymmap_sem.kernel/events/core.cactually held it only for reading so that may have been racy. The only other user - Infiniband - usedmmap_semfor writing but it caused quite some complications to it.So switch
mm->pinned_vmand convert all the places using it. This allows quite some simplifications to Infiniband code and it now doesn't have to care aboutmmap_semat all.Signed-off-by Jan Kara <[email protected]>--- drivers/infiniband/core/umem.c | 56 +++------------------ drivers/infiniband/core/uverbs_cmd.c | 1 - drivers/infiniband/core/uverbs_main.c | 2 - drivers/infiniband/hw/ipath/ipath_file_ops.c | 2 +- drivers/infiniband/hw/ipath/ipath_kernel.h | 1 - drivers/infiniband/hw/ipath/ipath_user_pages.c | 70 +++----------------------- drivers/infiniband/hw/qib/qib_user_pages.c | 20 ++------ fs/proc/task_mmu.c | 2 +- include/linux/mm_types.h | 2 +- include/rdma/ib_umem.h | 3 -- include/rdma/ib_verbs.h | 1 - kernel/events/core.c | 13 +++-- 12 files changed, 29 insertions(+), 144 deletions(-)drivers/infiniband/core/umem.c +7
-49@@ -80,6 +80,7 @@ struct ib_umem *ib_umem_get(struct ib_ucontext *context, unsigned long addr, {struct ib_umem *umem;struct page **page_list; +struct mm_struct *mm = current->mm;struct ib_umem_chunk *chunk;unsigned long locked;unsigned long lock_limit;@@ -126,20 +127,13 @@ struct ib_umem *ib_umem_get(struct ib_ucontext *context, unsigned long addr, npages =PAGE_ALIGN (size + umem->offset) >>PAGE_SHIFT ; -down_write (¤t->mm->mmap_sem); - lock_limit =rlimit (RLIMIT_MEMLOCK ) >>PAGE_SHIFT ; locked = npages; -if (npages + current->mm->pinned_vm > lock_limit && +if (atomic_long_add_return (&mm->pinned_vm, npages) > lock_limit && !capable (CAP_IPC_LOCK )) { -up_write (¤t->mm->mmap_sem); -kfree (umem); -free_page ((unsigned long ) page_list); -return ERR_PTR (-ENOMEM ); + ret = -ENOMEM ; + goto out; } - current->mm->pinned_vm += npages; - -up_write (¤t->mm->mmap_sem); cur_base = addr &PAGE_MASK ;@@ -201,9 +195,7 @@ out:if (ret <0 ) {__ib_umem_release (context->device, umem,0 );kfree (umem); -down_write (¤t->mm->mmap_sem); - current->mm->pinned_vm -= locked; -up_write (¤t->mm->mmap_sem); +atomic_long_sub (&mm->pinned_vm, locked); }free_page ((unsigned long ) page_list);@@ -211,17 +203,6 @@ out: }EXPORT_SYMBOL (ib_umem_get); -static void ib_umem_account (struct work_struct *work) -{ - struct ib_umem *umem = container_of(work, struct ib_umem, work); - -down_write (&umem->mm->mmap_sem); - umem->mm->pinned_vm -= umem->diff; -up_write (&umem->mm->mmap_sem); -mmput (umem->mm); -kfree (umem); -} -/** * ib_umem_release - release memory pinned with ib_umem_get * @umem: umem struct to release @@ -234,37 +215,14 @@ void ib_umem_release(struct ib_umem *umem)__ib_umem_release (umem->context->device, umem,1 ); - mm =get_task_mm (current); + mm = current->mm;if (!mm) {kfree (umem);return ; } diff =PAGE_ALIGN (umem->length + umem->offset) >>PAGE_SHIFT ; - -/* -* We may be called with the mm's mmap_sem already held. This -* can happen when a userspace munmap() is the call that drops -* the last reference to our file and calls our release -* method. If there are memory regions to destroy, we'll end -* up here and not be able to take the mmap_sem. In that case -* we defer the vm_locked accounting to the system workqueue. -*/ -if (context->closing) { -if (!down_write_trylock (&mm->mmap_sem)) { -INIT_WORK (&umem->work, ib_umem_account); - umem->mm = mm; - umem->diff = diff; - -queue_work (ib_wq, &umem->work); -return ; - } - }else -down_write (&mm->mmap_sem); - - current->mm->pinned_vm -= diff; -up_write (&mm->mmap_sem); -mmput (mm); +atomic_long_sub (&mm->pinned_vm, diff);kfree (umem); }EXPORT_SYMBOL (ib_umem_release);drivers/infiniband/core/uverbs_cmd.c +0
-1@@ -332,7 +332,6 @@ ssize_t ib_uverbs_get_context(struct ib_uverbs_file *file,INIT_LIST_HEAD (&ucontext->ah_list);INIT_LIST_HEAD (&ucontext->xrcd_list);INIT_LIST_HEAD (&ucontext->rule_list); - ucontext->closing =0 ; resp.num_comp_vectors = file->device->num_comp_vectors;drivers/infiniband/core/uverbs_main.c +0
-2@@ -196,8 +196,6 @@ static int ib_uverbs_cleanup_ucontext(struct ib_uverbs_file *file,if (!context)return 0 ; - context->closing =1 ; -list_for_each_entry_safe (uobj, tmp, &context->ah_list, list) {struct ib_ah *ah = uobj->object;drivers/infiniband/hw/ipath/ipath_file_ops.c +1
-1@@ -2025,7 +2025,7 @@ static void unlock_expected_tids(struct ipath_portdata *pd) dd->ipath_pageshadow[i] =NULL ;pci_unmap_page (dd->pcidev, dd->ipath_physshadow[i],PAGE_SIZE ,PCI_DMA_FROMDEVICE ); -ipath_release_user_pages_on_close (&ps,1 ); +ipath_release_user_pages (&ps,1 ); cnt++; ipath_stats.sps_pageunlocks++; }drivers/infiniband/hw/ipath/ipath_kernel.h +0
-1@@ -1083,7 +1083,6 @@ static inline void ipath_sdma_desc_unreserve(struct ipath_devdata *dd, u16 cnt)int ipath_get_user_pages (unsigned long ,size_t ,struct page **);void ipath_release_user_pages (struct page **,size_t ); -void ipath_release_user_pages_on_close (struct page **,size_t );int ipath_eeprom_read (struct ipath_devdata *,u8 ,void *,int );int ipath_eeprom_write (struct ipath_devdata *,u8 ,const void *,int );int ipath_tempsense_read (struct ipath_devdata *,u8 regnum);drivers/infiniband/hw/ipath/ipath_user_pages.c +8
-62@@ -68,26 +68,22 @@ int ipath_get_user_pages(unsigned long start_page, size_t num_pages, struct page **p) {unsigned long lock_limit; +struct mm_struct *mm = current->mm;size_t got;int ret; -down_write (¤t->mm->mmap_sem); lock_limit =rlimit (RLIMIT_MEMLOCK ) >>PAGE_SHIFT ; - -if (current->mm->pinned_vm + num_pages > lock_limit && +if (atomic_long_add_return (&mm->pinned_vm, num_pages) > lock_limit && !capable (CAP_IPC_LOCK )) { -up_write (¤t->mm->mmap_sem); ret = -ENOMEM ; - goto bail; + goto bail_sub; } - current->mm->pinned_vm += num_pages; -up_write (¤t->mm->mmap_sem);ipath_cdbg (VERBOSE ,"pin %lx pages from vaddr %lx\n" , (unsigned long ) num_pages, start_page);for (got =0 ; got < num_pages; got += ret) { - ret =get_user_pages_unlocked (current, current->mm, + ret =get_user_pages_unlocked (current, mm, start_page + got *PAGE_SIZE , num_pages - got,1 ,1 , p + got);@@ -101,9 +97,8 @@ int ipath_get_user_pages(unsigned long start_page, size_t num_pages, bail_release:__ipath_release_user_pages (p, got,0 ); -down_write (¤t->mm->mmap_sem); - current->mm->pinned_vm -= num_pages; -up_write (¤t->mm->mmap_sem); +bail_sub: +atomic_long_sub (&mm->pinned_vm, num_pages); bail:return ret; }@@ -166,56 +161,7 @@ dma_addr_t ipath_map_single(struct pci_dev *hwdev, void *ptr, size_t size,void ipath_release_user_pages (struct page **p,size_t num_pages) { -down_write (¤t->mm->mmap_sem); - -__ipath_release_user_pages (p, num_pages,1 ); - - current->mm->pinned_vm -= num_pages; - -up_write (¤t->mm->mmap_sem); -} - -struct ipath_user_pages_work { -struct work_struct work; -struct mm_struct *mm; -unsigned long num_pages; -}; - -static void user_pages_account (struct work_struct *_work) -{ -struct ipath_user_pages_work *work = - container_of(_work, struct ipath_user_pages_work, work); - -down_write (&work->mm->mmap_sem); - work->mm->pinned_vm -= work->num_pages; -up_write (&work->mm->mmap_sem); -mmput (work->mm); -kfree (work); -} - -void ipath_release_user_pages_on_close (struct page **p,size_t num_pages) -{ -struct ipath_user_pages_work *work; -struct mm_struct *mm; -__ipath_release_user_pages (p, num_pages,1 ); - - mm =get_task_mm (current); -if (!mm) -return ; - - work =kmalloc (sizeof (*work),GFP_KERNEL ); -if (!work) - goto bail_mm; - -INIT_WORK (&work->work, user_pages_account); - work->mm = mm; - work->num_pages = num_pages; - -queue_work (ib_wq, &work->work); -return ; - -bail_mm: -mmput (mm); -return ; +if (current->mm) +atomic_long_sub (¤t->mm->pinned_vm, num_pages); }drivers/infiniband/hw/qib/qib_user_pages.c +5
-15@@ -68,16 +68,13 @@ int qib_get_user_pages(unsigned long start_page, size_t num_pages,int ret;struct mm_struct *mm = current->mm; -down_write (&mm->mmap_sem); lock_limit =rlimit (RLIMIT_MEMLOCK ) >>PAGE_SHIFT ; -if (mm->pinned_vm + num_pages > lock_limit && !capable (CAP_IPC_LOCK )) { -up_write (&mm->mmap_sem); +if (atomic_long_add_return (&mm->pinned_vm, num_pages) > lock_limit && + !capable (CAP_IPC_LOCK )) { ret = -ENOMEM ; goto bail; } - mm->pinned_vm += num_pages; -up_write (&mm->mmap_sem);for (got =0 ; got < num_pages; got += ret) { ret = get_user_pages_unlocked(current, mm,@@ -94,9 +91,7 @@ int qib_get_user_pages(unsigned long start_page, size_t num_pages, bail_release:__qib_release_user_pages (p, got,0 ); -down_write (&mm->mmap_sem); - mm->pinned_vm -= num_pages; -up_write (&mm->mmap_sem); +atomic_long_sub (&mm->pinned_vm, num_pages); bail:return ret; }@@ -135,13 +130,8 @@ dma_addr_t qib_map_page(struct pci_dev *hwdev, struct page *page,void qib_release_user_pages (struct page **p,size_t num_pages) { -if (current->mm)/* during close after signal, mm can be NULL */ -down_write (¤t->mm->mmap_sem); -__qib_release_user_pages (p, num_pages,1 ); -if (current->mm) { - current->mm->pinned_vm -= num_pages; -up_write (¤t->mm->mmap_sem); - } +if (current->mm) +atomic_long_sub (¤t->mm->pinned_vm, num_pages); }fs/proc/task_mmu.c +1
-1@@ -57,7 +57,7 @@ void task_mem(struct seq_file *m, struct mm_struct *mm) hiwater_vm << (PAGE_SHIFT -10 ), total_vm << (PAGE_SHIFT -10 ), mm->locked_vm << (PAGE_SHIFT -10 ), - mm->pinned_vm << (PAGE_SHIFT -10 ), +atomic_long_read (&mm->pinned_vm) << (PAGE_SHIFT -10 ), hiwater_rss << (PAGE_SHIFT -10 ), total_rss << (PAGE_SHIFT -10 ), data << (PAGE_SHIFT-10),include/linux/mm_types.h +1
-1@@ -355,7 +355,7 @@ struct mm_struct {unsigned long total_vm;/* Total pages mapped */ unsigned long locked_vm;/* Pages that have PG_mlocked set */ -unsigned long pinned_vm;/* Refcount permanently increased */ +atomic_long_t pinned_vm;/* Refcount permanently increased */ unsigned long shared_vm;/* Shared pages (files) */ unsigned long exec_vm;/* VM_EXEC & ~VM_WRITE */ unsigned long stack_vm;/* VM_GROWSUP/DOWN */ include/rdma/ib_umem.h +0
-3@@ -47,9 +47,6 @@ struct ib_umem {int writable;int hugetlb;struct list_head chunk_list; -struct work_struct work; -struct mm_struct *mm; -unsigned long diff; };struct ib_umem_chunk {include/rdma/ib_verbs.h +0
-1@@ -961,7 +961,6 @@ struct ib_ucontext {struct list_head ah_list;struct list_head xrcd_list;struct list_head rule_list; -int closing; };struct ib_uobject {kernel/events/core.c +6
-7@@ -3922,7 +3922,7 @@ again:*/ atomic_long_sub ((size >>PAGE_SHIFT ) +1 , &mmap_user->locked_vm); - vma->vm_mm->pinned_vm -= mmap_locked; +atomic_long_sub (&vma->vm_mm->pinned_vm, mmap_locked);free_uid (mmap_user);ring_buffer_put (rb);/* could be last */ @@ -3944,7 +3944,7 @@ static int perf_mmap(struct file *file, struct vm_area_struct *vma)struct ring_buffer *rb;unsigned long vma_size;unsigned long nr_pages; -long user_extra, extra; +long user_extra, extra =0 ;int ret =0 , flags =0 ;/* @@ -4006,16 +4006,14 @@ again: user_locked =atomic_long_read (&user->locked_vm) + user_extra; - extra =0 ;if (user_locked > user_lock_limit) extra = user_locked - user_lock_limit; lock_limit =rlimit (RLIMIT_MEMLOCK ); lock_limit >>=PAGE_SHIFT ; - locked = vma->vm_mm->pinned_vm + extra; -if ((locked > lock_limit) &&perf_paranoid_tracepoint_raw () && - !capable (CAP_IPC_LOCK )) { +if (atomic_long_add_return (&vma->vm_mm->pinned_vm, extra) > lock_limit && +perf_paranoid_tracepoint_raw () && !capable (CAP_IPC_LOCK )) { ret = -EPERM ; goto unlock; }@@ -4039,7 +4037,6 @@ again: rb->mmap_user =get_current_user ();atomic_long_add (user_extra, &user->locked_vm); - vma->vm_mm->pinned_vm += extra;ring_buffer_attach (event, rb);rcu_assign_pointer (event->rb, rb);@@ -4049,6 +4046,8 @@ again: unlock:if (!ret)atomic_inc (&event->mmap_count); +else if (extra) +atomic_long_sub (&vma->vm_mm->pinned_vm, extra);mutex_unlock (&event->mmap_mutex);/* Signature · 1 line
1.8.1.4
Signature · 6 lines
Jan Kara <[email protected]> SUSE Labs, CR -- To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html