dma-buf: avoid using IS_ERR_OR_NULL
Subsystems (6)
PATCH 1/1 dma-buf: avoid using IS_ERR_OR_NULL
dma_buf_map_attachment and dma_buf_vmap can return NULL or ERR_PTR on a error. This encourages a common buggy pattern in callers:
sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
if (IS_ERR_OR_NULL(sgt))
return PTR_ERR(sgt);This causes the caller to return 0 on an error. IS_ERR_OR_NULL is almost always a sign of poorly-defined error handling.
This patch converts dma_buf_map_attachment to always return ERR_PTR, and fixes the callers that incorrectly handled NULL. There are a few more callers that were not checking for NULL at all, which would have dereferenced a NULL pointer later. There are also a few more callers that correctly handled NULL and ERR_PTR differently, I left those alone but they could also be modified to delete the NULL check.
This patch also converts dma_buf_vmap to always return NULL. All the callers to dma_buf_vmap only check for NULL, and would have dereferenced an ERR_PTR and panic'd if one was ever returned. This is not consistent with the rest of the dma buf APIs, but matches the expectations of all of the callers.
--- drivers/base/dma-buf.c | 18 +++++++++++------- drivers/gpu/drm/drm_prime.c | 2 +- drivers/gpu/drm/exynos/exynos_drm_dmabuf.c | 2 +- drivers/media/v4l2-core/videobuf2-dma-contig.c | 2 +- 4 files changed, 14 insertions(+), 10 deletions(-)
drivers/base/dma-buf.c +11 -7
@@ -251,9 +251,8 @@ EXPORT_SYMBOL_GPL(dma_buf_put);* @dmabuf: [in] buffer to attach device to. * @dev: [in] device to be attached. * -* Returns struct dma_buf_attachment * for this attachment; may return negative -* error codes. -* +* Returns struct dma_buf_attachment * for this attachment; returns ERR_PTR on +* error. */ struct dma_buf_attachment *dma_buf_attach (struct dma_buf *dmabuf,struct device *dev)
@@ -319,9 +318,8 @@ EXPORT_SYMBOL_GPL(dma_buf_detach);* @attach: [in] attachment whose scatterlist is to be returned * @direction: [in] direction of DMA transfer * -* Returns sg_table containing the scatterlist to be returned; may return NULL -* or ERR_PTR. -* +* Returns sg_table containing the scatterlist to be returned; returns ERR_PTR +* on error. */ struct sg_table *dma_buf_map_attachment (struct dma_buf_attachment *attach,enum dma_data_direction direction)
@@ -334,6 +332,8 @@ struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *attach,return ERR_PTR (-EINVAL ); sg_table = attach->dmabuf->ops->map_dma_buf (attach, direction); +if (!sg_table) + sg_table =ERR_PTR (-ENOMEM );return sg_table; }
@@ -544,6 +544,8 @@ EXPORT_SYMBOL_GPL(dma_buf_mmap);* These calls are optional in drivers. The intended use for them * is for mapping objects linear in kernel space for high use objects. * Please attempt to use kmap/kunmap before thinking about these interfaces. +* +* Returns NULL on error. */ void *dma_buf_vmap (struct dma_buf *dmabuf) {
@@ -566,7 +568,9 @@ void *dma_buf_vmap(struct dma_buf *dmabuf)BUG_ON (dmabuf->vmap_ptr); ptr = dmabuf->ops->vmap (dmabuf); -if (IS_ERR_OR_NULL (ptr)) +if (WARN_ON_ONCE (IS_ERR (ptr))) + ptr =NULL ; +if (!ptr) goto out_unlock; dmabuf->vmap_ptr = ptr;
drivers/gpu/drm/drm_prime.c +1 -1
@@ -471,7 +471,7 @@ struct drm_gem_object *drm_gem_prime_import(struct drm_device *dev,get_dma_buf (dma_buf); sgt =dma_buf_map_attachment (attach,DMA_BIDIRECTIONAL ); -if (IS_ERR_OR_NULL (sgt)) { +if (IS_ERR (sgt)) { ret =PTR_ERR (sgt); goto fail_detach; }
drivers/gpu/drm/exynos/exynos_drm_dmabuf.c +1 -1
@@ -224,7 +224,7 @@ struct drm_gem_object *exynos_dmabuf_prime_import(struct drm_device *drm_dev,get_dma_buf (dma_buf); sgt =dma_buf_map_attachment (attach,DMA_BIDIRECTIONAL ); -if (IS_ERR_OR_NULL (sgt)) { +if (IS_ERR (sgt)) { ret =PTR_ERR (sgt); goto err_buf_detach; }
drivers/media/v4l2-core/videobuf2-dma-contig.c +1 -1
@@ -719,7 +719,7 @@ static int vb2_dc_map_dmabuf(void *mem_priv)/* get the associated scatterlist for this buffer */ sgt =dma_buf_map_attachment (buf->db_attach, buf->dma_dir); -if (IS_ERR_OR_NULL (sgt)) { +if (IS_ERR (sgt)) {pr_err ("Error getting dmabuf scatterlist\n" );return -EINVAL ; }
Signature · 1 line
1.8.5.1
Quoted 79 lines · Colin Cross
dma_buf_map_attachmentanddma_buf_vmapcan return NULL orERR_PTRon a error. This encourages a common buggy pattern in callers:sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL); if (IS_ERR_OR_NULL(sgt)) return PTR_ERR(sgt);This causes the caller to return 0 on an error.
IS_ERR_OR_NULLis almost always a sign of poorly-defined error handling.This patch converts
dma_buf_map_attachmentto always returnERR_PTR, and fixes the callers that incorrectly handled NULL. There are a few more callers that were not checking for NULL at all, which would have dereferenced a NULL pointer later. There are also a few more callers that correctly handled NULL andERR_PTRdifferently, I left those alone but they could also be modified to delete the NULL check.This patch also converts
dma_buf_vmapto always return NULL. All the callers todma_buf_vmaponly check for NULL, and would have dereferenced anERR_PTRand panic'd if one was ever returned. This is not consistent with the rest of the dma buf APIs, but matches the expectations of all of the callers.Signed-off-by Colin Cross <[email protected]>--- drivers/base/dma-buf.c | 18 +++++++++++------- drivers/gpu/drm/drm_prime.c | 2 +- drivers/gpu/drm/exynos/exynos_drm_dmabuf.c | 2 +- drivers/media/v4l2-core/videobuf2-dma-contig.c | 2 +- 4 files changed, 14 insertions(+), 10 deletions(-)drivers/base/dma-buf.c +8
-6@@ -251,9 +251,8 @@ EXPORT_SYMBOL_GPL(dma_buf_put);* @dmabuf: [in] buffer to attach device to. * @dev: [in] device to be attached. * -* Returns struct dma_buf_attachment * for this attachment; may return negative -* error codes. -* +* Returns struct dma_buf_attachment * for this attachment; returns ERR_PTR on +* error. */ struct dma_buf_attachment *dma_buf_attach (struct dma_buf *dmabuf,struct device *dev)@@ -319,9 +318,8 @@ EXPORT_SYMBOL_GPL(dma_buf_detach);* @attach: [in] attachment whose scatterlist is to be returned * @direction: [in] direction of DMA transfer * -* Returns sg_table containing the scatterlist to be returned; may return NULL -* or ERR_PTR. -* +* Returns sg_table containing the scatterlist to be returned; returns ERR_PTR +* on error. */ struct sg_table *dma_buf_map_attachment (struct dma_buf_attachment *attach,enum dma_data_direction direction)@@ -334,6 +332,8 @@ struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *attach,return ERR_PTR (-EINVAL ); sg_table = attach->dmabuf->ops->map_dma_buf (attach, direction); +if (!sg_table) + sg_table =ERR_PTR (-ENOMEM );return sg_table; }@@ -544,6 +544,8 @@ EXPORT_SYMBOL_GPL(dma_buf_mmap);* These calls are optional in drivers. The intended use for them * is for mapping objects linear in kernel space for high use objects. * Please attempt to use kmap/kunmap before thinking about these interfaces. +* +* Returns NULL on error. */ void *dma_buf_vmap (struct dma_buf *dmabuf) {
drivers/base/dma-buf.c void *dma_buf_vmap(struct dma_buf *dmabuf) · line 571
Quoted 5 lines
drivers/base/dma-buf.c +1
-1@@ -566,7 +568,9 @@ void *dma_buf_vmap(struct dma_buf *dmabuf)BUG_ON (dmabuf->vmap_ptr); ptr = dmabuf->ops->vmap (dmabuf); -if (IS_ERR_OR_NULL (ptr)) +if (WARN_ON_ONCE (IS_ERR (ptr)))
since vmap is optional, the WARN_ON might be a bit strong.. although it would be a bit strange for an exporter to supply a vmap fxn which always returned NULL, not sure about that. Just thought I'd mention it in case anyone else had an opinion about that.
But either way:
Quoted 52 lines
drivers/base/dma-buf.c +2
-0+ ptr =NULL ; +if (!ptr) goto out_unlock; dmabuf->vmap_ptr = ptr;drivers/gpu/drm/drm_prime.c +1
-1@@ -471,7 +471,7 @@ struct drm_gem_object *drm_gem_prime_import(struct drm_device *dev,get_dma_buf (dma_buf); sgt =dma_buf_map_attachment (attach,DMA_BIDIRECTIONAL ); -if (IS_ERR_OR_NULL (sgt)) { +if (IS_ERR (sgt)) { ret =PTR_ERR (sgt); goto fail_detach; }drivers/gpu/drm/exynos/exynos_drm_dmabuf.c +1
-1@@ -224,7 +224,7 @@ struct drm_gem_object *exynos_dmabuf_prime_import(struct drm_device *drm_dev,get_dma_buf (dma_buf); sgt =dma_buf_map_attachment (attach,DMA_BIDIRECTIONAL ); -if (IS_ERR_OR_NULL (sgt)) { +if (IS_ERR (sgt)) { ret =PTR_ERR (sgt); goto err_buf_detach; }drivers/media/v4l2-core/videobuf2-dma-contig.c +1
-2@@ -719,7 +719,7 @@ static int vb2_dc_map_dmabuf(void *mem_priv)/* get the associated scatterlist for this buffer */ sgt =dma_buf_map_attachment (buf->db_attach, buf->dma_dir); -if (IS_ERR_OR_NULL (sgt)) { +if (IS_ERR (sgt)) {pr_err ("Error getting dmabuf scatterlist\n" );return -EINVAL ; } --1.8.5.1
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo at vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
Mauro Carvalho Chehab
Reviewed-byEm Sat, 21 Dec 2013 07:42:17 -0500
Rob Clark <[email protected]> escreveu:
Quoted 91 lines · Colin Cross
Quoted 79 lines
dma_buf_map_attachmentanddma_buf_vmapcan return NULL orERR_PTRon a error. This encourages a common buggy pattern in callers:sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL); if (IS_ERR_OR_NULL(sgt)) return PTR_ERR(sgt);This causes the caller to return 0 on an error.
IS_ERR_OR_NULLis almost always a sign of poorly-defined error handling.This patch converts
dma_buf_map_attachmentto always returnERR_PTR, and fixes the callers that incorrectly handled NULL. There are a few more callers that were not checking for NULL at all, which would have dereferenced a NULL pointer later. There are also a few more callers that correctly handled NULL andERR_PTRdifferently, I left those alone but they could also be modified to delete the NULL check.This patch also converts
dma_buf_vmapto always return NULL. All the callers todma_buf_vmaponly check for NULL, and would have dereferenced anERR_PTRand panic'd if one was ever returned. This is not consistent with the rest of the dma buf APIs, but matches the expectations of all of the callers.Signed-off-by Colin Cross <[email protected]>--- drivers/base/dma-buf.c | 18 +++++++++++------- drivers/gpu/drm/drm_prime.c | 2 +- drivers/gpu/drm/exynos/exynos_drm_dmabuf.c | 2 +- drivers/media/v4l2-core/videobuf2-dma-contig.c | 2 +- 4 files changed, 14 insertions(+), 10 deletions(-)drivers/base/dma-buf.c +8
-6@@ -251,9 +251,8 @@ EXPORT_SYMBOL_GPL(dma_buf_put);* @dmabuf: [in] buffer to attach device to. * @dev: [in] device to be attached. * -* Returns struct dma_buf_attachment * for this attachment; may return negative -* error codes. -* +* Returns struct dma_buf_attachment * for this attachment; returns ERR_PTR on +* error. */ struct dma_buf_attachment *dma_buf_attach (struct dma_buf *dmabuf,struct device *dev)@@ -319,9 +318,8 @@ EXPORT_SYMBOL_GPL(dma_buf_detach);* @attach: [in] attachment whose scatterlist is to be returned * @direction: [in] direction of DMA transfer * -* Returns sg_table containing the scatterlist to be returned; may return NULL -* or ERR_PTR. -* +* Returns sg_table containing the scatterlist to be returned; returns ERR_PTR +* on error. */ struct sg_table *dma_buf_map_attachment (struct dma_buf_attachment *attach,enum dma_data_direction direction)@@ -334,6 +332,8 @@ struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *attach,return ERR_PTR (-EINVAL ); sg_table = attach->dmabuf->ops->map_dma_buf (attach, direction); +if (!sg_table) + sg_table =ERR_PTR (-ENOMEM );return sg_table; }@@ -544,6 +544,8 @@ EXPORT_SYMBOL_GPL(dma_buf_mmap);* These calls are optional in drivers. The intended use for them * is for mapping objects linear in kernel space for high use objects. * Please attempt to use kmap/kunmap before thinking about these interfaces. +* +* Returns NULL on error. */ void *dma_buf_vmap (struct dma_buf *dmabuf) {drivers/base/dma-buf.c void *dma_buf_vmap(struct dma_buf *dmabuf) · line 571
Quoted 5 lines
drivers/base/dma-buf.c +1
-1@@ -566,7 +568,9 @@ void *dma_buf_vmap(struct dma_buf *dmabuf)BUG_ON (dmabuf->vmap_ptr); ptr = dmabuf->ops->vmap (dmabuf); -if (IS_ERR_OR_NULL (ptr)) +if (WARN_ON_ONCE (IS_ERR (ptr)))since vmap is optional, the
WARN_ONmight be a bit strong.. although it would be a bit strange for an exporter to supply a vmap fxn which always returned NULL, not sure about that. Just thought I'd mention it in case anyone else had an opinion about that.
Quoted 3 lines
But either way:
Reviewed-by Rob Clark <[email protected]>
IMHO, a WARN_ON_ONCE() here (or some other error report printk) seems ok, as, if this function is called, the caller would be expecting it to not fail.
Either way:
Quoted 58 lines
Quoted 49 lines
drivers/base/dma-buf.c +2
-0+ ptr =NULL ; +if (!ptr) goto out_unlock; dmabuf->vmap_ptr = ptr;drivers/gpu/drm/drm_prime.c +1
-1@@ -471,7 +471,7 @@ struct drm_gem_object *drm_gem_prime_import(struct drm_device *dev,get_dma_buf (dma_buf); sgt =dma_buf_map_attachment (attach,DMA_BIDIRECTIONAL ); -if (IS_ERR_OR_NULL (sgt)) { +if (IS_ERR (sgt)) { ret =PTR_ERR (sgt); goto fail_detach; }drivers/gpu/drm/exynos/exynos_drm_dmabuf.c +1
-1@@ -224,7 +224,7 @@ struct drm_gem_object *exynos_dmabuf_prime_import(struct drm_device *drm_dev,get_dma_buf (dma_buf); sgt =dma_buf_map_attachment (attach,DMA_BIDIRECTIONAL ); -if (IS_ERR_OR_NULL (sgt)) { +if (IS_ERR (sgt)) { ret =PTR_ERR (sgt); goto err_buf_detach; }drivers/media/v4l2-core/videobuf2-dma-contig.c +1
-2@@ -719,7 +719,7 @@ static int vb2_dc_map_dmabuf(void *mem_priv)/* get the associated scatterlist for this buffer */ sgt =dma_buf_map_attachment (buf->db_attach, buf->dma_dir); -if (IS_ERR_OR_NULL (sgt)) { +if (IS_ERR (sgt)) {pr_err ("Error getting dmabuf scatterlist\n" );return -EINVAL ; } --1.8.5.1
Quoted 5 lines
--
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/--
To unsubscribe from this list: send the line "unsubscribe linux-media" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Signature · 2 lines
Cheers, Mauro
Greg Kroah-Hartman
maintainerQuoted 82 lines · Rob Clark
Quoted 81 lines · Colin Cross
dma_buf_map_attachmentanddma_buf_vmapcan return NULL orERR_PTRon a error. This encourages a common buggy pattern in callers:sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL); if (IS_ERR_OR_NULL(sgt)) return PTR_ERR(sgt);This causes the caller to return 0 on an error.
IS_ERR_OR_NULLis almost always a sign of poorly-defined error handling.This patch converts
dma_buf_map_attachmentto always returnERR_PTR, and fixes the callers that incorrectly handled NULL. There are a few more callers that were not checking for NULL at all, which would have dereferenced a NULL pointer later. There are also a few more callers that correctly handled NULL andERR_PTRdifferently, I left those alone but they could also be modified to delete the NULL check.This patch also converts
dma_buf_vmapto always return NULL. All the callers todma_buf_vmaponly check for NULL, and would have dereferenced anERR_PTRand panic'd if one was ever returned. This is not consistent with the rest of the dma buf APIs, but matches the expectations of all of the callers.Signed-off-by Colin Cross <[email protected]>--- drivers/base/dma-buf.c | 18 +++++++++++------- drivers/gpu/drm/drm_prime.c | 2 +- drivers/gpu/drm/exynos/exynos_drm_dmabuf.c | 2 +- drivers/media/v4l2-core/videobuf2-dma-contig.c | 2 +- 4 files changed, 14 insertions(+), 10 deletions(-)drivers/base/dma-buf.c +8
-6@@ -251,9 +251,8 @@ EXPORT_SYMBOL_GPL(dma_buf_put);* @dmabuf: [in] buffer to attach device to. * @dev: [in] device to be attached. * -* Returns struct dma_buf_attachment * for this attachment; may return negative -* error codes. -* +* Returns struct dma_buf_attachment * for this attachment; returns ERR_PTR on +* error. */ struct dma_buf_attachment *dma_buf_attach (struct dma_buf *dmabuf,struct device *dev)@@ -319,9 +318,8 @@ EXPORT_SYMBOL_GPL(dma_buf_detach);* @attach: [in] attachment whose scatterlist is to be returned * @direction: [in] direction of DMA transfer * -* Returns sg_table containing the scatterlist to be returned; may return NULL -* or ERR_PTR. -* +* Returns sg_table containing the scatterlist to be returned; returns ERR_PTR +* on error. */ struct sg_table *dma_buf_map_attachment (struct dma_buf_attachment *attach,enum dma_data_direction direction)@@ -334,6 +332,8 @@ struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *attach,return ERR_PTR (-EINVAL ); sg_table = attach->dmabuf->ops->map_dma_buf (attach, direction); +if (!sg_table) + sg_table =ERR_PTR (-ENOMEM );return sg_table; }@@ -544,6 +544,8 @@ EXPORT_SYMBOL_GPL(dma_buf_mmap);* These calls are optional in drivers. The intended use for them * is for mapping objects linear in kernel space for high use objects. * Please attempt to use kmap/kunmap before thinking about these interfaces. +* +* Returns NULL on error. */ void *dma_buf_vmap (struct dma_buf *dmabuf) {@@ -566,7 +568,9 @@ void *dma_buf_vmap(struct dma_buf *dmabuf)BUG_ON (dmabuf->vmap_ptr);
Quoted 8 lines
drivers/base/dma-buf.c void *dma_buf_vmap(struct dma_buf *dmabuf) · line 571
Quoted 3 lines
drivers/base/dma-buf.c +1
-1ptr = dmabuf->ops->vmap (dmabuf); -if (IS_ERR_OR_NULL (ptr)) +if (WARN_ON_ONCE (IS_ERR (ptr)))since vmap is optional, the
WARN_ONmight be a bit strong.. although it would be a bit strange for an exporter to supply a vmap fxn which always returned NULL, not sure about that. Just thought I'd mention it in case anyone else had an opinion about that.
Yeah, I don't like this, it could cause unnecessary reports of problems.
Care to fix it up?
thanks,
greg k-h
Quoted 92 lines · Greg Kroah-Hartman
Quoted 82 lines · Rob Clark
Quoted 81 lines · Colin Cross
dma_buf_map_attachmentanddma_buf_vmapcan return NULL orERR_PTRon a error. This encourages a common buggy pattern in callers:sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL); if (IS_ERR_OR_NULL(sgt)) return PTR_ERR(sgt);This causes the caller to return 0 on an error.
IS_ERR_OR_NULLis almost always a sign of poorly-defined error handling.This patch converts
dma_buf_map_attachmentto always returnERR_PTR, and fixes the callers that incorrectly handled NULL. There are a few more callers that were not checking for NULL at all, which would have dereferenced a NULL pointer later. There are also a few more callers that correctly handled NULL andERR_PTRdifferently, I left those alone but they could also be modified to delete the NULL check.This patch also converts
dma_buf_vmapto always return NULL. All the callers todma_buf_vmaponly check for NULL, and would have dereferenced anERR_PTRand panic'd if one was ever returned. This is not consistent with the rest of the dma buf APIs, but matches the expectations of all of the callers.Signed-off-by Colin Cross <[email protected]>--- drivers/base/dma-buf.c | 18 +++++++++++------- drivers/gpu/drm/drm_prime.c | 2 +- drivers/gpu/drm/exynos/exynos_drm_dmabuf.c | 2 +- drivers/media/v4l2-core/videobuf2-dma-contig.c | 2 +- 4 files changed, 14 insertions(+), 10 deletions(-)drivers/base/dma-buf.c +8
-6@@ -251,9 +251,8 @@ EXPORT_SYMBOL_GPL(dma_buf_put);* @dmabuf: [in] buffer to attach device to. * @dev: [in] device to be attached. * -* Returns struct dma_buf_attachment * for this attachment; may return negative -* error codes. -* +* Returns struct dma_buf_attachment * for this attachment; returns ERR_PTR on +* error. */ struct dma_buf_attachment *dma_buf_attach (struct dma_buf *dmabuf,struct device *dev)@@ -319,9 +318,8 @@ EXPORT_SYMBOL_GPL(dma_buf_detach);* @attach: [in] attachment whose scatterlist is to be returned * @direction: [in] direction of DMA transfer * -* Returns sg_table containing the scatterlist to be returned; may return NULL -* or ERR_PTR. -* +* Returns sg_table containing the scatterlist to be returned; returns ERR_PTR +* on error. */ struct sg_table *dma_buf_map_attachment (struct dma_buf_attachment *attach,enum dma_data_direction direction)@@ -334,6 +332,8 @@ struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *attach,return ERR_PTR (-EINVAL ); sg_table = attach->dmabuf->ops->map_dma_buf (attach, direction); +if (!sg_table) + sg_table =ERR_PTR (-ENOMEM );return sg_table; }@@ -544,6 +544,8 @@ EXPORT_SYMBOL_GPL(dma_buf_mmap);* These calls are optional in drivers. The intended use for them * is for mapping objects linear in kernel space for high use objects. * Please attempt to use kmap/kunmap before thinking about these interfaces. +* +* Returns NULL on error. */ void *dma_buf_vmap (struct dma_buf *dmabuf) {@@ -566,7 +568,9 @@ void *dma_buf_vmap(struct dma_buf *dmabuf)BUG_ON (dmabuf->vmap_ptr);Quoted 8 lines
drivers/base/dma-buf.c void *dma_buf_vmap(struct dma_buf *dmabuf) · line 571
Quoted 3 lines
drivers/base/dma-buf.c +1
-1ptr = dmabuf->ops->vmap (dmabuf); -if (IS_ERR_OR_NULL (ptr)) +if (WARN_ON_ONCE (IS_ERR (ptr)))since vmap is optional, the
WARN_ONmight be a bit strong.. although it would be a bit strange for an exporter to supply a vmap fxn which always returned NULL, not sure about that. Just thought I'd mention it in case anyone else had an opinion about that.
Quoted 1 line
Yeah, I don't like this, it could cause unnecessary reports of problems.
The WARN_ON_ONCE is only if the vmap op returns ERR_PTR, not if it returns NULL. This is designed to catch vmap ops that don't follow the spec, so I would call them necessary reports, but I can take it out if you still disagree.
Greg Kroah-Hartman
maintainerQuoted 94 lines · Colin Cross
Quoted 92 lines · Greg Kroah-Hartman
Quoted 90 lines · Rob Clark
Quoted 79 lines · Colin Cross
dma_buf_map_attachmentanddma_buf_vmapcan return NULL orERR_PTRon a error. This encourages a common buggy pattern in callers:sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL); if (IS_ERR_OR_NULL(sgt)) return PTR_ERR(sgt);This causes the caller to return 0 on an error.
IS_ERR_OR_NULLis almost always a sign of poorly-defined error handling.This patch converts
dma_buf_map_attachmentto always returnERR_PTR, and fixes the callers that incorrectly handled NULL. There are a few more callers that were not checking for NULL at all, which would have dereferenced a NULL pointer later. There are also a few more callers that correctly handled NULL andERR_PTRdifferently, I left those alone but they could also be modified to delete the NULL check.This patch also converts
dma_buf_vmapto always return NULL. All the callers todma_buf_vmaponly check for NULL, and would have dereferenced anERR_PTRand panic'd if one was ever returned. This is not consistent with the rest of the dma buf APIs, but matches the expectations of all of the callers.Signed-off-by Colin Cross <[email protected]>--- drivers/base/dma-buf.c | 18 +++++++++++------- drivers/gpu/drm/drm_prime.c | 2 +- drivers/gpu/drm/exynos/exynos_drm_dmabuf.c | 2 +- drivers/media/v4l2-core/videobuf2-dma-contig.c | 2 +- 4 files changed, 14 insertions(+), 10 deletions(-)drivers/base/dma-buf.c +8
-6@@ -251,9 +251,8 @@ EXPORT_SYMBOL_GPL(dma_buf_put);* @dmabuf: [in] buffer to attach device to. * @dev: [in] device to be attached. * -* Returns struct dma_buf_attachment * for this attachment; may return negative -* error codes. -* +* Returns struct dma_buf_attachment * for this attachment; returns ERR_PTR on +* error. */ struct dma_buf_attachment *dma_buf_attach (struct dma_buf *dmabuf,struct device *dev)@@ -319,9 +318,8 @@ EXPORT_SYMBOL_GPL(dma_buf_detach);* @attach: [in] attachment whose scatterlist is to be returned * @direction: [in] direction of DMA transfer * -* Returns sg_table containing the scatterlist to be returned; may return NULL -* or ERR_PTR. -* +* Returns sg_table containing the scatterlist to be returned; returns ERR_PTR +* on error. */ struct sg_table *dma_buf_map_attachment (struct dma_buf_attachment *attach,enum dma_data_direction direction)@@ -334,6 +332,8 @@ struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *attach,return ERR_PTR (-EINVAL ); sg_table = attach->dmabuf->ops->map_dma_buf (attach, direction); +if (!sg_table) + sg_table =ERR_PTR (-ENOMEM );return sg_table; }@@ -544,6 +544,8 @@ EXPORT_SYMBOL_GPL(dma_buf_mmap);* These calls are optional in drivers. The intended use for them * is for mapping objects linear in kernel space for high use objects. * Please attempt to use kmap/kunmap before thinking about these interfaces. +* +* Returns NULL on error. */ void *dma_buf_vmap (struct dma_buf *dmabuf) {drivers/base/dma-buf.c void *dma_buf_vmap(struct dma_buf *dmabuf) · line 571
Quoted 5 lines
drivers/base/dma-buf.c +1
-1@@ -566,7 +568,9 @@ void *dma_buf_vmap(struct dma_buf *dmabuf)BUG_ON (dmabuf->vmap_ptr); ptr = dmabuf->ops->vmap (dmabuf); -if (IS_ERR_OR_NULL (ptr)) +if (WARN_ON_ONCE (IS_ERR (ptr)))since vmap is optional, the
WARN_ONmight be a bit strong.. although it would be a bit strange for an exporter to supply a vmap fxn which always returned NULL, not sure about that. Just thought I'd mention it in case anyone else had an opinion about that.
Quoted 6 lines
Quoted 1 line
Yeah, I don't like this, it could cause unnecessary reports of problems.
The
WARN_ON_ONCEis only if the vmap op returnsERR_PTR, not if it returns NULL. This is designed to catch vmap ops that don't follow the spec, so I would call them necessary reports, but I can take it out if you still disagree.
Ah, ok, that makes more sense. I'll queue this up.
thanks,
greg k-h