genwqe: Take R/W permissions into account when dealing with memory pages
Subsystems (2)
PATCH 1/1 genwqe: Take R/W permissions into account when dealing with memory pages
Currently we assume userspace pages are always writable when doing memory pinning. This is not true, specially since userspace applications may allocate their memory the way they want, we have no control over it. If a read-only page is set for pinning, currently the driver fails due to get_user_pages_fast() refusing to map read-only pages as writable.
This patch changes this behavior, by taking the permission flags of the pages into account in both pinning/unpinning process, as well as in the DMA data copy-back to userpace (which we shouldn't try to do blindly, since it will fail in case of read-only-pages).
---
Arnd/Greg, we found this bug recently, although not critical, it's really a boring issue affecting driver functionality. If it's possible to take this patch still on v4.14, we'd be really thankful!
But we know it's late, so if not possible, v4.15 is cool.
Thanks in advance!
drivers/misc/genwqe/card_base.h | 7 ++++++- drivers/misc/genwqe/card_dev.c | 6 +++++- drivers/misc/genwqe/card_utils.c | 43 +++++++++++++++++++++++++--------------- 3 files changed, 38 insertions(+), 18 deletions(-)
drivers/misc/genwqe/card_base.h +6 -1
@@ -182,6 +182,7 @@ struct dma_mapping {struct list_head card_list;/* list of usr_maps for card */ struct list_head pin_list;/* list of pinned memory for dev */ +int write;/* writable map? useful in unmapping */ }; static inline void genwqe_mapping_init(struct dma_mapping *m,
@@ -189,6 +190,7 @@ static inline void genwqe_mapping_init(struct dma_mapping *m, {memset (m,0 ,sizeof (*m)); m->type = type; + m->write =1 ;/* Assume the maps we create are R/W */ }/**
@@ -347,6 +349,7 @@ enum genwqe_requ_state {* @user_size: size of user-space memory area * @page: buffer for partial pages if needed * @page_dma_addr: dma address partial pages +* @write: should we write it back to userspace? */ struct genwqe_sgl {dma_addr_t sgl_dma_addr;
@@ -356,6 +359,8 @@ struct genwqe_sgl {void __user *user_addr;/* user-space base-address */ size_t user_size;/* size of memory area */ +int write; +unsigned long nr_pages;unsigned long fpage_offs;size_t fpage_size;
@@ -369,7 +374,7 @@ struct genwqe_sgl { };int genwqe_alloc_sync_sgl (struct genwqe_dev *cd,struct genwqe_sgl *sgl, -void __user *user_addr,size_t user_size); +void __user *user_addr,size_t user_size,int write);int genwqe_setup_sgl (struct genwqe_dev *cd,struct genwqe_sgl *sgl,dma_addr_t *dma_list);
drivers/misc/genwqe/card_dev.c +5 -1
@@ -942,6 +942,10 @@ static int ddcb_cmd_fixups(struct genwqe_file *cfile, struct ddcb_requ *req)genwqe_mapping_init (m,GENWQE_MAPPING_SGL_TEMP ); + +if (ats_flags ==ATS_TYPE_SGL_RD ) + m->write =0 ; + rc =genwqe_user_vmap (cd, m, (void *)u_addr, u_size, req);if (rc !=0 )
@@ -954,7 +958,7 @@ static int ddcb_cmd_fixups(struct genwqe_file *cfile, struct ddcb_requ *req)/* create genwqe style scatter gather list */ rc =genwqe_alloc_sync_sgl (cd, &req->sgls[i], (void __user *)u_addr, - u_size); + u_size, m->write);if (rc !=0 ) goto err_out;
drivers/misc/genwqe/card_utils.c +27 -16
94 lines not on this page · raw message
Signature · 1 line
2.15.0.rc0
Greg KH
maintainerQuoted 13 lines · Guilherme G. Piccoli
Currently we assume userspace pages are always writable when doing memory pinning. This is not true, specially since userspace applications may allocate their memory the way they want, we have no control over it. If a read-only page is set for pinning, currently the driver fails due to
get_user_pages_fast()refusing to map read-only pages as writable.This patch changes this behavior, by taking the permission flags of the pages into account in both pinning/unpinning process, as well as in the DMA data copy-back to userpace (which we shouldn't try to do blindly, since it will fail in case of read-only-pages).
Signed-off-by Frank Haverkamp <[email protected]>Signed-off-by Guilherme G. Piccoli <[email protected]>
Quoted 7 lines
---
Arnd/Greg, we found this bug recently, although not critical, it's really a boring issue affecting driver functionality. If it's possible to take this patch still on v4.14, we'd be really thankful!
But we know it's late, so if not possible, v4.15 is cool.
Is this a regression? It seems like it's just a "fix something that has always been broken but no one has noticed yet" type of thing, right?
thanks,
greg k-h
Quoted 3 lines · Greg KH
Is this a regression? It seems like it's just a "fix something that has always been broken but no one has noticed yet" type of thing, right?
Yes, this a fix for something that always has been broken, not a regression!
Thanks,
Guilherme
Quoted 4 lines
thanks,
greg k-h