vfio: ccw: error handling fixes and improvements
Subsystems (4)
v4 cover vfio: ccw: error handling fixes and improvements
Dear Reviewers,
Here is a new version for this patch series.
We didn't get agreement on patch #4 (#5 in v2) in the former cycle though, I made it based on my understanding. We can continue discussing on it.
Changelog:v3->v4:
- Change mail address from @linux.vnet.ibm.com to @linux.ibm.com
v2->v3:
- #2. Reworded comment message.
- #4. Trace failure only, trace all error path, and a common knob for all failure paths.
v1->v2:
- #1. Reworded commit message and comment, plus some typo fixes.
- #2. New patch.
- #3. Added the missing suggested-by Pierre.
Fixed typos.
Added sanity check on pa->pa_iova_pfn and updated comments accordingly. - #4. Removed unused idaw_nr.
- #5. Replaced leading white spaces with TABs.
Traced the function in anycase.
Dong Jia Shi (3):
vfio: ccw: shorten kernel doc description for pfn_array_pin()
vfio: ccw: refactor and improve pfn_array_alloc_pin()
vfio: ccw: set ccw->cda to NULL defensively
Halil Pasic (1):
vfio: ccw: add traceponits for interesting error paths
drivers/s390/cio/Makefile | 1 + drivers/s390/cio/vfio_ccw_cp.c | 121 +++++++++++++++++++------------------- drivers/s390/cio/vfio_ccw_fsm.c | 17 +++++- drivers/s390/cio/vfio_ccw_trace.h | 54 +++++++++++++++++ 4 files changed, 130 insertions(+), 63 deletions(-) create mode 100644 drivers/s390/cio/vfio_ccw_trace.h
Signature · 1 line
2.16.3
PATCH v4 1/4 vfio: ccw: shorten kernel doc description for pfn_array_pin()
The kernel doc description for usage of the struct pfn_array in pfn_array_pin() is unnecessary long. Let's shorten it by describing the contents of the struct pfn_array fields at the struct's definition instead.
--- drivers/s390/cio/vfio_ccw_cp.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-)
drivers/s390/cio/vfio_ccw_cp.c +6 -8
@@ -23,9 +23,13 @@#define CCWCHAIN_LEN_MAX 256struct pfn_array { +/* Starting guest physical I/O address. */ unsigned long pa_iova; +/* Array that stores PFNs of the pages need to pin. */ unsigned long *pa_iova_pfn; +/* Array that receives PFNs of the pages pinned. */ unsigned long *pa_pfn; +/* Number of pages to pin/pinned from @pa_iova. */ int pa_nr; };
@@ -53,14 +57,8 @@ struct ccwchain {* Attempt to pin user pages in memory. * * Usage of pfn_array: -* @pa->pa_iova starting guest physical I/O address. Assigned by caller. -* @pa->pa_iova_pfn array that stores PFNs of the pages need to pin. Allocated -* by caller. -* @pa->pa_pfn array that receives PFNs of the pages pinned. Allocated by -* caller. -* @pa->pa_nr number of pages from @pa->pa_iova to pin. Assigned by -* caller. -* number of pages pinned. Assigned by callee. +* Any field in this structure should be initialized by caller. +* We expect @pa->pa_nr > 0, and its value will be assigned by callee. * * Returns: * Number of pages pinned on success.
Signature · 1 line
2.16.3
PATCH v4 2/4 vfio: ccw: refactor and improve pfn_array_alloc_pin()
In reply to Dong Jia ShiThis refactors pfn_array_alloc_pin() and also improves it by adding defensive code in error handling so that calling pfn_array_unpin_free() after error return won't lead to problem. This mainly does:
1. Merge pfn_array_pin() into pfn_array_alloc_pin(), since there is no other user of pfn_array_pin(). As a result, also remove kernel-doc for pfn_array_pin() and add/update kernel-doc for pfn_array_alloc_pin() and struct pfn_array.
2. For a vfio_pin_pages() failure, set pa->pa_nr to zero to indicate zero pages were pinned.
3. Set pa->pa_iova_pfn to NULL right after it was freed.
--- drivers/s390/cio/vfio_ccw_cp.c | 82 +++++++++++++++++++----------------------- 1 file changed, 36 insertions(+), 46 deletions(-)
drivers/s390/cio/vfio_ccw_cp.c +36 -46
@@ -29,7 +29,7 @@ struct pfn_array {unsigned long *pa_iova_pfn;/* Array that receives PFNs of the pages pinned. */ unsigned long *pa_pfn; -/* Number of pages to pin/pinned from @pa_iova. */ +/* Number of pages pinned from @pa_iova. */ int pa_nr; };
@@ -50,64 +50,33 @@ struct ccwchain { };/* -* pfn_array_pin() - pin user pages in memory +* pfn_array_alloc_pin() - alloc memory for PFNs, then pin user pages in memory * @pa: pfn_array on which to perform the operation * @mdev: the mediated device to perform pin/unpin operations +* @iova: target guest physical address +* @len: number of bytes that should be pinned from @iova * -* Attempt to pin user pages in memory. +* Attempt to allocate memory for PFNs, and pin user pages in memory. * * Usage of pfn_array: -* Any field in this structure should be initialized by caller. -* We expect @pa->pa_nr > 0, and its value will be assigned by callee. +* We expect (pa_nr == 0) and (pa_iova_pfn == NULL), any field in +* this structure will be filled in by this function. * * Returns: * Number of pages pinned on success. -* If @pa->pa_nr is 0 or negative, returns 0. +* If @pa->pa_nr is not 0, or @pa->pa_iova_pfn is not NULL initially, +* returns -EINVAL. * If no pages were pinned, returns -errno. */ -static int pfn_array_pin (struct pfn_array *pa,struct device *mdev) -{ -int i, ret; - -if (pa->pa_nr <=0 ) { - pa->pa_nr =0 ; -return 0 ; - } - - pa->pa_iova_pfn[0 ] = pa->pa_iova >>PAGE_SHIFT ; -for (i =1 ; i < pa->pa_nr; i++) - pa->pa_iova_pfn[i] = pa->pa_iova_pfn[i -1 ] +1 ; - - ret =vfio_pin_pages (mdev, pa->pa_iova_pfn, pa->pa_nr, -IOMMU_READ |IOMMU_WRITE , pa->pa_pfn); - -if (ret >0 && ret != pa->pa_nr) { -vfio_unpin_pages (mdev, pa->pa_iova_pfn, ret); - pa->pa_nr =0 ; -return 0 ; - } - -return ret; -} - -/* Unpin the pages before releasing the memory. */ -static void pfn_array_unpin_free (struct pfn_array *pa,struct device *mdev) -{ -vfio_unpin_pages (mdev, pa->pa_iova_pfn, pa->pa_nr); - pa->pa_nr =0 ; -kfree (pa->pa_iova_pfn); -} - -/* Alloc memory for PFNs, then pin pages with them. */ static int pfn_array_alloc_pin (struct pfn_array *pa,struct device *mdev,u64 iova,unsigned int len) { -int ret =0 ; +int i, ret =0 ;if (!len)return 0 ; -if (pa->pa_nr) +if (pa->pa_nr || pa->pa_iova_pfn)return -EINVAL ; pa->pa_iova = iova;
@@ -124,18 +93,39 @@ static int pfn_array_alloc_pin(struct pfn_array *pa, struct device *mdev,return -ENOMEM ; pa->pa_pfn = pa->pa_iova_pfn + pa->pa_nr; - ret =pfn_array_pin (pa, mdev); + pa->pa_iova_pfn[0 ] = pa->pa_iova >>PAGE_SHIFT ; +for (i =1 ; i < pa->pa_nr; i++) + pa->pa_iova_pfn[i] = pa->pa_iova_pfn[i -1 ] +1 ; + + ret =vfio_pin_pages (mdev, pa->pa_iova_pfn, pa->pa_nr, +IOMMU_READ |IOMMU_WRITE , pa->pa_pfn); -if (ret >0 ) -return ret; -else if (!ret) +if (ret <0 ) { + goto err_out; + }else if (ret >0 && ret != pa->pa_nr) { +vfio_unpin_pages (mdev, pa->pa_iova_pfn, ret); ret = -EINVAL ; + goto err_out; + } + +return ret; +err_out: + pa->pa_nr =0 ;kfree (pa->pa_iova_pfn); + pa->pa_iova_pfn =NULL ;return ret; } +/* Unpin the pages before releasing the memory. */ +static void pfn_array_unpin_free (struct pfn_array *pa,struct device *mdev) +{ +vfio_unpin_pages (mdev, pa->pa_iova_pfn, pa->pa_nr); + pa->pa_nr =0 ; +kfree (pa->pa_iova_pfn); +} +static int pfn_array_table_init (struct pfn_array_table *pat,int nr) { pat->pat_pa =kcalloc (nr,sizeof (*pat->pat_pa),GFP_KERNEL );
Signature · 1 line
2.16.3
PATCH v4 3/4 vfio: ccw: set ccw->cda to NULL defensively
In reply to Dong Jia ShiLet's avoid free on ccw->cda that points to a guest address or an already freed memory area by setting it to NULL if memory allocation didn't happen or failed.
--- drivers/s390/cio/vfio_ccw_cp.c | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-)
drivers/s390/cio/vfio_ccw_cp.c +20 -11
@@ -491,7 +491,7 @@ static int ccwchain_fetch_direct(struct ccwchain *chain,struct ccw1 *ccw;struct pfn_array_table *pat;unsigned long *idaws; -int idaw_nr; +int ret; ccw = chain->ch_ccw + idx;
@@ -511,18 +511,19 @@ static int ccwchain_fetch_direct(struct ccwchain *chain,* needed when translating a direct ccw to a idal ccw. */ pat = chain->ch_pat + idx; -if (pfn_array_table_init (pat,1 )) -return -ENOMEM ; - idaw_nr =pfn_array_alloc_pin (pat->pat_pa, cp->mdev, - ccw->cda, ccw->count); -if (idaw_nr <0 ) -return idaw_nr; + ret =pfn_array_table_init (pat,1 ); +if (ret) + goto out_init; + + ret =pfn_array_alloc_pin (pat->pat_pa, cp->mdev, ccw->cda, ccw->count); +if (ret <0 ) + goto out_init;/* Translate this direct ccw to a idal ccw. */ - idaws =kcalloc (idaw_nr,sizeof (*idaws),GFP_DMA |GFP_KERNEL ); + idaws =kcalloc (ret,sizeof (*idaws),GFP_DMA |GFP_KERNEL );if (!idaws) { -pfn_array_table_unpin_free (pat, cp->mdev); -return -ENOMEM ; + ret = -ENOMEM ; + goto out_unpin; } ccw->cda = (__u32 )virt_to_phys (idaws); ccw->flags |=CCW_FLAG_IDA ;
@@ -530,6 +531,12 @@ static int ccwchain_fetch_direct(struct ccwchain *chain,pfn_array_table_idal_create_words (pat, idaws);return 0 ; + +out_unpin: +pfn_array_table_unpin_free (pat, cp->mdev); +out_init: + ccw->cda =0 ; +return ret; } static int ccwchain_fetch_idal(struct ccwchain *chain,
@@ -559,7 +566,7 @@ static int ccwchain_fetch_idal(struct ccwchain *chain, pat = chain->ch_pat + idx; ret =pfn_array_table_init (pat, idaw_nr);if (ret) -return ret; + goto out_init;/* Translate idal ccw to use new allocated idaws. */ idaws =kzalloc (idaw_len,GFP_DMA |GFP_KERNEL );
@@ -591,6 +598,8 @@ static int ccwchain_fetch_idal(struct ccwchain *chain,kfree (idaws); out_unpin:pfn_array_table_unpin_free (pat, cp->mdev); +out_init: + ccw->cda =0 ;return ret; }
Signature · 1 line
2.16.3
PATCH v4 4/4 vfio: ccw: add traceponits for interesting error paths
In reply to Dong Jia ShiFrom: Halil Pasic <[email protected]>
Add some tracepoints so we can inspect what is not working as is should.
--- drivers/s390/cio/Makefile | 1 + drivers/s390/cio/vfio_ccw_fsm.c | 17 +++++++++++- drivers/s390/cio/vfio_ccw_trace.h | 54 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 drivers/s390/cio/vfio_ccw_trace.h
drivers/s390/cio/Makefile +1 -0
@@ -5,6 +5,7 @@ # The following is required for define_trace.h to find ./trace.h CFLAGS_trace.o := -I$(src) +CFLAGS_vfio_ccw_fsm.o := -I$(src) obj-y += airq.o blacklist.o chsc.o cio.o css.o chp.o idset.o isc.o \ fcx.o itcw.o crw.o ccwreq.o trace.o ioasm.o
drivers/s390/cio/vfio_ccw_fsm.c +16 -1
@@ -13,6 +13,9 @@#include "ioasm.h" #include "vfio_ccw_private.h" +#define CREATE_TRACE_POINTS +#include "vfio_ccw_trace.h" +static int fsm_io_helper (struct vfio_ccw_private *private) {struct subchannel *sch;
@@ -110,6 +113,10 @@ static void fsm_disabled_irq(struct vfio_ccw_private *private,*/ cio_disable_subchannel (sch); } +inline struct subchannel_id get_schid (struct vfio_ccw_private *p) +{ +return p->sch->schid; +}/* * Deal with the ccw command request from the userspace.
@@ -121,6 +128,7 @@ static void fsm_io_request(struct vfio_ccw_private *private,union scsw *scsw = &private->scsw;struct ccw_io_region *io_region = &private->io_region;struct mdev_device *mdev = private->mdev; +char *errstr ="request" ; private->state =VFIO_CCW_STATE_BOXED ;
@@ -132,15 +140,19 @@ static void fsm_io_request(struct vfio_ccw_private *private,/* Don't try to build a cp if transport mode is specified. */ if (orb->tm.b) { io_region->ret_code = -EOPNOTSUPP ; + errstr ="transport mode" ; goto err_out; } io_region->ret_code =cp_init (&private->cp,mdev_dev (mdev), orb); -if (io_region->ret_code) +if (io_region->ret_code) { + errstr ="cp init" ; goto err_out; + } io_region->ret_code =cp_prefetch (&private->cp);if (io_region->ret_code) { + errstr ="cp prefetch" ;cp_free (&private->cp); goto err_out; }
@@ -148,6 +160,7 @@ static void fsm_io_request(struct vfio_ccw_private *private,/* Start channel program and wait for I/O interrupt. */ io_region->ret_code =fsm_io_helper (private);if (io_region->ret_code) { + errstr ="cp fsm_io_helper" ;cp_free (&private->cp); goto err_out; }
@@ -164,6 +177,8 @@ static void fsm_io_request(struct vfio_ccw_private *private, err_out: private->state =VFIO_CCW_STATE_IDLE ; +trace_vfio_ccw_io_fctl (scsw->cmd.fctl,get_schid (private), + io_region->ret_code, errstr); }/*
drivers/s390/cio/vfio_ccw_trace.h new file +54 -0
54 lines not on this page · raw message
Signature · 1 line
2.16.3
Quoted 6 lines · On Wed, 23 May 2018 04:56:41 +0200 Dong Jia Shi <[email protected]> wrote:
Dear Reviewers,
Here is a new version for this patch series.
We didn't get agreement on patch #4 (#5 in v2) in the former cycle though, I made it based on my understanding. We can continue discussing on it.
I'll probably go ahead and queue 1-3 (would not mind r-bs, though) in any case.
I hope we can queue the tracing patch as well, as any tracing beats no tracing :)
Quoted 35 lines
Changelog:
v3->v4:
- Change mail address from @linux.vnet.ibm.com to @linux.ibm.com
v2->v3:
- #2. Reworded comment message.
- #4. Trace failure only, trace all error path, and a common knob for all failure paths.
v1->v2:
- #1. Reworded commit message and comment, plus some typo fixes.
- #2. New patch.
- #3. Added the missing suggested-by Pierre.Fixed typos. Added sanity check on pa->pa_iova_pfn and updated comments accordingly.- #4. Removed unused
idaw_nr.
- #5. Replaced leading white spaces with TABs.
Traced the function in anycase.Dong Jia Shi (3):
vfio: ccw: shorten kernel doc description forpfn_array_pin()
vfio: ccw: refactor and improvepfn_array_alloc_pin()
vfio: ccw: setccw->cdato NULL defensivelyHalil Pasic (1):
vfio: ccw: add traceponits for interesting error pathsdrivers/s390/cio/Makefile | 1 + drivers/s390/cio/vfio_ccw_cp.c | 121 +++++++++++++++++++------------------- drivers/s390/cio/vfio_ccw_fsm.c | 17 +++++- drivers/s390/cio/vfio_ccw_trace.h | 54 +++++++++++++++++ 4 files changed, 130 insertions(+), 63 deletions(-) create mode 100644 drivers/s390/cio/vfio_ccw_trace.h
Re: [PATCH v4 4/4] vfio: ccw: add traceponits for interesting error paths
In reply to Dong Jia ShiOn Wed, 23 May 2018 04:56:45 +0200
Dong Jia Shi <[email protected]> wrote:
Still s/traceponits/tracepoints/ (can fix up when applying).
Quoted 88 lines
From: Halil Pasic <[email protected]>
Add some tracepoints so we can inspect what is not working as is should.
Signed-off-by Halil Pasic <[email protected]>Signed-off-by Dong Jia Shi <[email protected]>--- drivers/s390/cio/Makefile | 1 + drivers/s390/cio/vfio_ccw_fsm.c | 17 +++++++++++- drivers/s390/cio/vfio_ccw_trace.h | 54 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 drivers/s390/cio/vfio_ccw_trace.hdrivers/s390/cio/Makefile +1
-0@@ -5,6 +5,7 @@ # The following is required for define_trace.h to find ./trace.h CFLAGS_trace.o := -I$(src) +CFLAGS_vfio_ccw_fsm.o := -I$(src) obj-y += airq.o blacklist.o chsc.o cio.o css.o chp.o idset.o isc.o \ fcx.o itcw.o crw.o ccwreq.o trace.o ioasm.odrivers/s390/cio/vfio_ccw_fsm.c +14
-1@@ -13,6 +13,9 @@#include "ioasm.h" #include "vfio_ccw_private.h" +#define CREATE_TRACE_POINTS +#include "vfio_ccw_trace.h" +static int fsm_io_helper (struct vfio_ccw_private *private) {struct subchannel *sch;@@ -110,6 +113,10 @@ static void fsm_disabled_irq(struct vfio_ccw_private *private,*/ cio_disable_subchannel (sch); } +inline struct subchannel_id get_schid (struct vfio_ccw_private *p) +{ +return p->sch->schid; +}/* * Deal with the ccw command request from the userspace. @@ -121,6 +128,7 @@ static void fsm_io_request(struct vfio_ccw_private *private,union scsw *scsw = &private->scsw;struct ccw_io_region *io_region = &private->io_region;struct mdev_device *mdev = private->mdev; +char *errstr ="request" ; private->state =VFIO_CCW_STATE_BOXED ;@@ -132,15 +140,19 @@ static void fsm_io_request(struct vfio_ccw_private *private,/* Don't try to build a cp if transport mode is specified. */ if (orb->tm.b) { io_region->ret_code = -EOPNOTSUPP ; + errstr ="transport mode" ; goto err_out; } io_region->ret_code =cp_init (&private->cp,mdev_dev (mdev), orb); -if (io_region->ret_code) +if (io_region->ret_code) { + errstr ="cp init" ; goto err_out; + } io_region->ret_code =cp_prefetch (&private->cp);if (io_region->ret_code) { + errstr ="cp prefetch" ;cp_free (&private->cp); goto err_out; }@@ -148,6 +160,7 @@ static void fsm_io_request(struct vfio_ccw_private *private,/* Start channel program and wait for I/O interrupt. */ io_region->ret_code =fsm_io_helper (private);if (io_region->ret_code) { + errstr ="cp fsm_io_helper" ;cp_free (&private->cp); goto err_out; }
drivers/s390/cio/vfio_ccw_fsm.c static void fsm_io_request(struct vfio_ccw_private *private, · line 184
Quoted 8 lines
drivers/s390/cio/vfio_ccw_fsm.c +2
-0@@ -164,6 +177,8 @@ static void fsm_io_request(struct vfio_ccw_private *private, err_out: private->state =VFIO_CCW_STATE_IDLE ; +trace_vfio_ccw_io_fctl (scsw->cmd.fctl,get_schid (private), + io_region->ret_code, errstr); }/*
This approach looks reasonable to me. We can still improve this later, should we want to.
Quoted 7 lines · On Wed, 23 May 2018 12:21:29 +0200 Cornelia Huck <[email protected]> wrote:
Quoted 4 lines · On Wed, 23 May 2018 04:56:41 +0200 Dong Jia Shi <[email protected]> wrote:
Dear Reviewers,
Here is a new version for this patch series.
Quoted 8 lines
Quoted 2 lines
We didn't get agreement on patch #4 (#5 in v2) in the former cycle though, I made it based on my understanding. We can continue discussing on it.
I'll probably go ahead and queue 1-3 (would not mind r-bs, though) in any case.
I hope we can queue the tracing patch as well, as any tracing beats no tracing :)
OK, I've pushed this out to the vfio-ccw branch.
Would still be great to get some R-bs.
What about sending a pull req next week, so that we get this ready for the 4.18 merge window?