Daniel Vetter
2019-Nov-04 17:38 UTC
[Nouveau] [PATCH 2/3] drm/nouveau: slowpath for pushbuf ioctl
We can't copy_*_user while holding reservations, that will (soon even for nouveau) lead to deadlocks. And it breaks the cross-driver contract around dma_resv. Fix this by adding a slowpath for when we need relocations, and by pushing the writeback of the new presumed offsets to the very end. Aside from "it compiles" entirely untested unfortunately. Signed-off-by: Daniel Vetter <daniel.vetter at intel.com> Cc: Ilia Mirkin <imirkin at alum.mit.edu> Cc: Maarten Lankhorst <maarten.lankhorst at linux.intel.com> Cc: Ben Skeggs <bskeggs at redhat.com> Cc: nouveau at lists.freedesktop.org --- drivers/gpu/drm/nouveau/nouveau_gem.c | 57 ++++++++++++++++++--------- 1 file changed, 38 insertions(+), 19 deletions(-) diff --git a/drivers/gpu/drm/nouveau/nouveau_gem.c b/drivers/gpu/drm/nouveau/nouveau_gem.c index 1324c19f4e5c..05ec8edd6a8b 100644 --- a/drivers/gpu/drm/nouveau/nouveau_gem.c +++ b/drivers/gpu/drm/nouveau/nouveau_gem.c @@ -484,12 +484,9 @@ validate_init(struct nouveau_channel *chan, struct drm_file *file_priv, static int validate_list(struct nouveau_channel *chan, struct nouveau_cli *cli, - struct list_head *list, struct drm_nouveau_gem_pushbuf_bo *pbbo, - uint64_t user_pbbo_ptr) + struct list_head *list, struct drm_nouveau_gem_pushbuf_bo *pbbo) { struct nouveau_drm *drm = chan->drm; - struct drm_nouveau_gem_pushbuf_bo __user *upbbo - (void __force __user *)(uintptr_t)user_pbbo_ptr; struct nouveau_bo *nvbo; int ret, relocs = 0; @@ -533,10 +530,6 @@ validate_list(struct nouveau_channel *chan, struct nouveau_cli *cli, b->presumed.offset = nvbo->bo.offset; b->presumed.valid = 0; relocs++; - - if (copy_to_user(&upbbo[nvbo->pbbo_index].presumed, - &b->presumed, sizeof(b->presumed))) - return -EFAULT; } } @@ -547,8 +540,8 @@ static int nouveau_gem_pushbuf_validate(struct nouveau_channel *chan, struct drm_file *file_priv, struct drm_nouveau_gem_pushbuf_bo *pbbo, - uint64_t user_buffers, int nr_buffers, - struct validate_op *op, int *apply_relocs) + int nr_buffers, + struct validate_op *op, bool *apply_relocs) { struct nouveau_cli *cli = nouveau_cli(file_priv); int ret; @@ -565,7 +558,7 @@ nouveau_gem_pushbuf_validate(struct nouveau_channel *chan, return ret; } - ret = validate_list(chan, cli, &op->list, pbbo, user_buffers); + ret = validate_list(chan, cli, &op->list, pbbo); if (unlikely(ret < 0)) { if (ret != -ERESTARTSYS) NV_PRINTK(err, cli, "validating bo list\n"); @@ -605,16 +598,12 @@ u_memcpya(uint64_t user, unsigned nmemb, unsigned size) static int nouveau_gem_pushbuf_reloc_apply(struct nouveau_cli *cli, struct drm_nouveau_gem_pushbuf *req, + struct drm_nouveau_gem_pushbuf_reloc *reloc, struct drm_nouveau_gem_pushbuf_bo *bo) { - struct drm_nouveau_gem_pushbuf_reloc *reloc = NULL; int ret = 0; unsigned i; - reloc = u_memcpya(req->relocs, req->nr_relocs, sizeof(*reloc)); - if (IS_ERR(reloc)) - return PTR_ERR(reloc); - for (i = 0; i < req->nr_relocs; i++) { struct drm_nouveau_gem_pushbuf_reloc *r = &reloc[i]; struct drm_nouveau_gem_pushbuf_bo *b; @@ -693,11 +682,13 @@ nouveau_gem_ioctl_pushbuf(struct drm_device *dev, void *data, struct nouveau_drm *drm = nouveau_drm(dev); struct drm_nouveau_gem_pushbuf *req = data; struct drm_nouveau_gem_pushbuf_push *push; + struct drm_nouveau_gem_pushbuf_reloc *reloc = NULL; struct drm_nouveau_gem_pushbuf_bo *bo; struct nouveau_channel *chan = NULL; struct validate_op op; struct nouveau_fence *fence = NULL; - int i, j, ret = 0, do_reloc = 0; + int i, j, ret = 0; + bool do_reloc = false; if (unlikely(!abi16)) return -ENOMEM; @@ -755,7 +746,8 @@ nouveau_gem_ioctl_pushbuf(struct drm_device *dev, void *data, } /* Validate buffer list */ - ret = nouveau_gem_pushbuf_validate(chan, file_priv, bo, req->buffers, +revalidate: + ret = nouveau_gem_pushbuf_validate(chan, file_priv, bo, req->nr_buffers, &op, &do_reloc); if (ret) { if (ret != -ERESTARTSYS) @@ -765,7 +757,18 @@ nouveau_gem_ioctl_pushbuf(struct drm_device *dev, void *data, /* Apply any relocations that are required */ if (do_reloc) { - ret = nouveau_gem_pushbuf_reloc_apply(cli, req, bo); + if (!reloc) { + validate_fini(&op, chan, NULL, bo); + reloc = u_memcpya(req->relocs, req->nr_relocs, sizeof(*reloc)); + if (IS_ERR(reloc)) { + ret = PTR_ERR(reloc); + goto out_prevalid; + } + + goto revalidate; + } + + ret = nouveau_gem_pushbuf_reloc_apply(cli, req, reloc, bo); if (ret) { NV_PRINTK(err, cli, "reloc apply: %d\n", ret); goto out; @@ -851,6 +854,22 @@ nouveau_gem_ioctl_pushbuf(struct drm_device *dev, void *data, validate_fini(&op, chan, fence, bo); nouveau_fence_unref(&fence); + if (do_reloc) { + struct drm_nouveau_gem_pushbuf_bo __user *upbbo + u64_to_user_ptr(req->buffers); + + for (i = 0; i < req->nr_buffers; i++) { + if (bo[i].presumed.valid) + continue; + + if (copy_to_user(&upbbo[i].presumed, &bo[i].presumed, + sizeof(bo[i].presumed))) { + ret = -EFAULT; + break; + } + } + u_free(reloc); + } out_prevalid: u_free(bo); u_free(push); -- 2.24.0.rc2
Daniel Vetter
2019-Nov-05 11:04 UTC
[Nouveau] [PATCH 2/3] drm/nouveau: slowpath for pushbuf ioctl
On Mon, Nov 04, 2019 at 06:38:00PM +0100, Daniel Vetter wrote:> We can't copy_*_user while holding reservations, that will (soon even > for nouveau) lead to deadlocks. And it breaks the cross-driver > contract around dma_resv. > > Fix this by adding a slowpath for when we need relocations, and by > pushing the writeback of the new presumed offsets to the very end. > > Aside from "it compiles" entirely untested unfortunately. > > Signed-off-by: Daniel Vetter <daniel.vetter at intel.com> > Cc: Ilia Mirkin <imirkin at alum.mit.edu> > Cc: Maarten Lankhorst <maarten.lankhorst at linux.intel.com> > Cc: Ben Skeggs <bskeggs at redhat.com> > Cc: nouveau at lists.freedesktop.orgPing for ack/review so I can land this entire series. intel-gfx-ci is all happy with the rebased version, so nouveau ack is the only hold-up here. -Daniel> --- > drivers/gpu/drm/nouveau/nouveau_gem.c | 57 ++++++++++++++++++--------- > 1 file changed, 38 insertions(+), 19 deletions(-) > > diff --git a/drivers/gpu/drm/nouveau/nouveau_gem.c b/drivers/gpu/drm/nouveau/nouveau_gem.c > index 1324c19f4e5c..05ec8edd6a8b 100644 > --- a/drivers/gpu/drm/nouveau/nouveau_gem.c > +++ b/drivers/gpu/drm/nouveau/nouveau_gem.c > @@ -484,12 +484,9 @@ validate_init(struct nouveau_channel *chan, struct drm_file *file_priv, > > static int > validate_list(struct nouveau_channel *chan, struct nouveau_cli *cli, > - struct list_head *list, struct drm_nouveau_gem_pushbuf_bo *pbbo, > - uint64_t user_pbbo_ptr) > + struct list_head *list, struct drm_nouveau_gem_pushbuf_bo *pbbo) > { > struct nouveau_drm *drm = chan->drm; > - struct drm_nouveau_gem_pushbuf_bo __user *upbbo > - (void __force __user *)(uintptr_t)user_pbbo_ptr; > struct nouveau_bo *nvbo; > int ret, relocs = 0; > > @@ -533,10 +530,6 @@ validate_list(struct nouveau_channel *chan, struct nouveau_cli *cli, > b->presumed.offset = nvbo->bo.offset; > b->presumed.valid = 0; > relocs++; > - > - if (copy_to_user(&upbbo[nvbo->pbbo_index].presumed, > - &b->presumed, sizeof(b->presumed))) > - return -EFAULT; > } > } > > @@ -547,8 +540,8 @@ static int > nouveau_gem_pushbuf_validate(struct nouveau_channel *chan, > struct drm_file *file_priv, > struct drm_nouveau_gem_pushbuf_bo *pbbo, > - uint64_t user_buffers, int nr_buffers, > - struct validate_op *op, int *apply_relocs) > + int nr_buffers, > + struct validate_op *op, bool *apply_relocs) > { > struct nouveau_cli *cli = nouveau_cli(file_priv); > int ret; > @@ -565,7 +558,7 @@ nouveau_gem_pushbuf_validate(struct nouveau_channel *chan, > return ret; > } > > - ret = validate_list(chan, cli, &op->list, pbbo, user_buffers); > + ret = validate_list(chan, cli, &op->list, pbbo); > if (unlikely(ret < 0)) { > if (ret != -ERESTARTSYS) > NV_PRINTK(err, cli, "validating bo list\n"); > @@ -605,16 +598,12 @@ u_memcpya(uint64_t user, unsigned nmemb, unsigned size) > static int > nouveau_gem_pushbuf_reloc_apply(struct nouveau_cli *cli, > struct drm_nouveau_gem_pushbuf *req, > + struct drm_nouveau_gem_pushbuf_reloc *reloc, > struct drm_nouveau_gem_pushbuf_bo *bo) > { > - struct drm_nouveau_gem_pushbuf_reloc *reloc = NULL; > int ret = 0; > unsigned i; > > - reloc = u_memcpya(req->relocs, req->nr_relocs, sizeof(*reloc)); > - if (IS_ERR(reloc)) > - return PTR_ERR(reloc); > - > for (i = 0; i < req->nr_relocs; i++) { > struct drm_nouveau_gem_pushbuf_reloc *r = &reloc[i]; > struct drm_nouveau_gem_pushbuf_bo *b; > @@ -693,11 +682,13 @@ nouveau_gem_ioctl_pushbuf(struct drm_device *dev, void *data, > struct nouveau_drm *drm = nouveau_drm(dev); > struct drm_nouveau_gem_pushbuf *req = data; > struct drm_nouveau_gem_pushbuf_push *push; > + struct drm_nouveau_gem_pushbuf_reloc *reloc = NULL; > struct drm_nouveau_gem_pushbuf_bo *bo; > struct nouveau_channel *chan = NULL; > struct validate_op op; > struct nouveau_fence *fence = NULL; > - int i, j, ret = 0, do_reloc = 0; > + int i, j, ret = 0; > + bool do_reloc = false; > > if (unlikely(!abi16)) > return -ENOMEM; > @@ -755,7 +746,8 @@ nouveau_gem_ioctl_pushbuf(struct drm_device *dev, void *data, > } > > /* Validate buffer list */ > - ret = nouveau_gem_pushbuf_validate(chan, file_priv, bo, req->buffers, > +revalidate: > + ret = nouveau_gem_pushbuf_validate(chan, file_priv, bo, > req->nr_buffers, &op, &do_reloc); > if (ret) { > if (ret != -ERESTARTSYS) > @@ -765,7 +757,18 @@ nouveau_gem_ioctl_pushbuf(struct drm_device *dev, void *data, > > /* Apply any relocations that are required */ > if (do_reloc) { > - ret = nouveau_gem_pushbuf_reloc_apply(cli, req, bo); > + if (!reloc) { > + validate_fini(&op, chan, NULL, bo); > + reloc = u_memcpya(req->relocs, req->nr_relocs, sizeof(*reloc)); > + if (IS_ERR(reloc)) { > + ret = PTR_ERR(reloc); > + goto out_prevalid; > + } > + > + goto revalidate; > + } > + > + ret = nouveau_gem_pushbuf_reloc_apply(cli, req, reloc, bo); > if (ret) { > NV_PRINTK(err, cli, "reloc apply: %d\n", ret); > goto out; > @@ -851,6 +854,22 @@ nouveau_gem_ioctl_pushbuf(struct drm_device *dev, void *data, > validate_fini(&op, chan, fence, bo); > nouveau_fence_unref(&fence); > > + if (do_reloc) { > + struct drm_nouveau_gem_pushbuf_bo __user *upbbo > + u64_to_user_ptr(req->buffers); > + > + for (i = 0; i < req->nr_buffers; i++) { > + if (bo[i].presumed.valid) > + continue; > + > + if (copy_to_user(&upbbo[i].presumed, &bo[i].presumed, > + sizeof(bo[i].presumed))) { > + ret = -EFAULT; > + break; > + } > + } > + u_free(reloc); > + } > out_prevalid: > u_free(bo); > u_free(push); > -- > 2.24.0.rc2 >-- Daniel Vetter Software Engineer, Intel Corporation http://blog.ffwll.ch
Maarten Lankhorst
2019-Nov-05 12:30 UTC
[Nouveau] [PATCH 2/3] drm/nouveau: slowpath for pushbuf ioctl
Op 05-11-2019 om 12:04 schreef Daniel Vetter:> On Mon, Nov 04, 2019 at 06:38:00PM +0100, Daniel Vetter wrote: >> We can't copy_*_user while holding reservations, that will (soon even >> for nouveau) lead to deadlocks. And it breaks the cross-driver >> contract around dma_resv. >> >> Fix this by adding a slowpath for when we need relocations, and by >> pushing the writeback of the new presumed offsets to the very end. >> >> Aside from "it compiles" entirely untested unfortunately. >> >> Signed-off-by: Daniel Vetter <daniel.vetter at intel.com> >> Cc: Ilia Mirkin <imirkin at alum.mit.edu> >> Cc: Maarten Lankhorst <maarten.lankhorst at linux.intel.com> >> Cc: Ben Skeggs <bskeggs at redhat.com> >> Cc: nouveau at lists.freedesktop.org > Ping for ack/review so I can land this entire series. intel-gfx-ci is all > happy with the rebased version, so nouveau ack is the only hold-up here.I don't feel confident reviewing this as I lack the hw, so all review caveats reply. Having said that, this looks sane, and if it blows up we'll found out eventually. :) Reviewed-by: Maarten Lankhorst <maarten.lankhorst at linux.intel.com>