Small patchset using container_of instead of casting on first structure member address.
Fabian Frederick (9): drm/cirrus: use container_of to resolve cirrus_fbdev from drm_fb_helper drm/mgag200: use container_of to resolve mga_fbdev from drm_fb_helper drm/radeon: use container_of to resolve radeon_fbdev from drm_fb_helper drm/nouveau: use container_of to resolve nouveau_fbdev from drm_fb_helper drm/nouveau: use container_of to resolve nouveau_plane from drm_plane drm/qxl: use container_of to resolve qxl_fbdev from drm_fb_helper drm/gma500: use container_of to resolve psb_fbdev from drm_fb_helper drm/ast: use container_of to resolve ast_fbdev from drm_fb_helper drm/udl: use container_of to resolve udl_fbdev from drm_fb_helper
drivers/gpu/drm/ast/ast_fb.c | 3 ++- drivers/gpu/drm/cirrus/cirrus_fbdev.c | 3 ++- drivers/gpu/drm/gma500/framebuffer.c | 3 ++- drivers/gpu/drm/mgag200/mgag200_fb.c | 3 ++- drivers/gpu/drm/nouveau/dispnv04/overlay.c | 15 ++++++++++----- drivers/gpu/drm/nouveau/nouveau_fbcon.c | 3 ++- drivers/gpu/drm/qxl/qxl_fb.c | 3 ++- drivers/gpu/drm/radeon/radeon_fb.c | 3 ++- drivers/gpu/drm/udl/udl_fb.c | 3 ++- 9 files changed, 26 insertions(+), 13 deletions(-)
Use container_of instead of casting first structure member.
Signed-off-by: Fabian Frederick fabf@skynet.be --- Compiled but untested.
drivers/gpu/drm/cirrus/cirrus_fbdev.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/drivers/gpu/drm/cirrus/cirrus_fbdev.c b/drivers/gpu/drm/cirrus/cirrus_fbdev.c index 2a135f2..d231b1c 100644 --- a/drivers/gpu/drm/cirrus/cirrus_fbdev.c +++ b/drivers/gpu/drm/cirrus/cirrus_fbdev.c @@ -160,7 +160,8 @@ static int cirrusfb_create_object(struct cirrus_fbdev *afbdev, static int cirrusfb_create(struct drm_fb_helper *helper, struct drm_fb_helper_surface_size *sizes) { - struct cirrus_fbdev *gfbdev = (struct cirrus_fbdev *)helper; + struct cirrus_fbdev *gfbdev = + container_of(helper, struct cirrus_fbdev, helper); struct drm_device *dev = gfbdev->helper.dev; struct cirrus_device *cdev = gfbdev->helper.dev->dev_private; struct fb_info *info;
Use container_of instead of casting first structure member.
Signed-off-by: Fabian Frederick fabf@skynet.be --- Compiled but untested.
drivers/gpu/drm/mgag200/mgag200_fb.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/drivers/gpu/drm/mgag200/mgag200_fb.c b/drivers/gpu/drm/mgag200/mgag200_fb.c index 5451dc5..4415af3 100644 --- a/drivers/gpu/drm/mgag200/mgag200_fb.c +++ b/drivers/gpu/drm/mgag200/mgag200_fb.c @@ -158,7 +158,8 @@ static int mgag200fb_create_object(struct mga_fbdev *afbdev, static int mgag200fb_create(struct drm_fb_helper *helper, struct drm_fb_helper_surface_size *sizes) { - struct mga_fbdev *mfbdev = (struct mga_fbdev *)helper; + struct mga_fbdev *mfbdev = + container_of(helper, struct mga_fbdev, helper); struct drm_device *dev = mfbdev->helper.dev; struct drm_mode_fb_cmd2 mode_cmd; struct mga_device *mdev = dev->dev_private;
Use container_of instead of casting first structure member.
Signed-off-by: Fabian Frederick fabf@skynet.be --- Compiled but untested.
drivers/gpu/drm/radeon/radeon_fb.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/drivers/gpu/drm/radeon/radeon_fb.c b/drivers/gpu/drm/radeon/radeon_fb.c index 94b0f2a..0ea1db8 100644 --- a/drivers/gpu/drm/radeon/radeon_fb.c +++ b/drivers/gpu/drm/radeon/radeon_fb.c @@ -189,7 +189,8 @@ out_unref: static int radeonfb_create(struct drm_fb_helper *helper, struct drm_fb_helper_surface_size *sizes) { - struct radeon_fbdev *rfbdev = (struct radeon_fbdev *)helper; + struct radeon_fbdev *rfbdev = + container_of(helper, struct radeon_fbdev, helper); struct radeon_device *rdev = rfbdev->rdev; struct fb_info *info; struct drm_framebuffer *fb = NULL;
Use container_of instead of casting first structure member.
Signed-off-by: Fabian Frederick fabf@skynet.be --- Compiled but untested.
drivers/gpu/drm/nouveau/nouveau_fbcon.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/drivers/gpu/drm/nouveau/nouveau_fbcon.c b/drivers/gpu/drm/nouveau/nouveau_fbcon.c index 8bdd270..673d270 100644 --- a/drivers/gpu/drm/nouveau/nouveau_fbcon.c +++ b/drivers/gpu/drm/nouveau/nouveau_fbcon.c @@ -308,7 +308,8 @@ static int nouveau_fbcon_create(struct drm_fb_helper *helper, struct drm_fb_helper_surface_size *sizes) { - struct nouveau_fbdev *fbcon = (struct nouveau_fbdev *)helper; + struct nouveau_fbdev *fbcon = + container_of(helper, struct nouveau_fbdev, helper); struct drm_device *dev = fbcon->dev; struct nouveau_drm *drm = nouveau_drm(dev); struct nvif_device *device = &drm->device;
Use container_of instead of casting first structure member.
Signed-off-by: Fabian Frederick fabf@skynet.be --- Compiled but untested.
drivers/gpu/drm/nouveau/dispnv04/overlay.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-)
diff --git a/drivers/gpu/drm/nouveau/dispnv04/overlay.c b/drivers/gpu/drm/nouveau/dispnv04/overlay.c index b36afcb..1e9056a 100644 --- a/drivers/gpu/drm/nouveau/dispnv04/overlay.c +++ b/drivers/gpu/drm/nouveau/dispnv04/overlay.c @@ -97,7 +97,8 @@ nv10_update_plane(struct drm_plane *plane, struct drm_crtc *crtc, uint32_t src_w, uint32_t src_h) { struct nvif_device *dev = &nouveau_drm(plane->dev)->device; - struct nouveau_plane *nv_plane = (struct nouveau_plane *)plane; + struct nouveau_plane *nv_plane = + container_of(plane, struct nouveau_plane, base); struct nouveau_framebuffer *nv_fb = nouveau_framebuffer(fb); struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc); struct nouveau_bo *cur = nv_plane->cur; @@ -173,7 +174,8 @@ static int nv10_disable_plane(struct drm_plane *plane) { struct nvif_device *dev = &nouveau_drm(plane->dev)->device; - struct nouveau_plane *nv_plane = (struct nouveau_plane *)plane; + struct nouveau_plane *nv_plane = + container_of(plane, struct nouveau_plane, base);
nvif_wr32(dev, NV_PVIDEO_STOP, 1); if (nv_plane->cur) { @@ -224,7 +226,8 @@ nv_set_property(struct drm_plane *plane, struct drm_property *property, uint64_t value) { - struct nouveau_plane *nv_plane = (struct nouveau_plane *)plane; + struct nouveau_plane *nv_plane = + container_of(plane, struct nouveau_plane, base);
if (property == nv_plane->props.colorkey) nv_plane->colorkey = value; @@ -344,7 +347,8 @@ nv04_update_plane(struct drm_plane *plane, struct drm_crtc *crtc, uint32_t src_w, uint32_t src_h) { struct nvif_device *dev = &nouveau_drm(plane->dev)->device; - struct nouveau_plane *nv_plane = (struct nouveau_plane *)plane; + struct nouveau_plane *nv_plane = + container_of(plane, struct nouveau_plane, base); struct nouveau_framebuffer *nv_fb = nouveau_framebuffer(fb); struct nouveau_bo *cur = nv_plane->cur; uint32_t overlay = 1; @@ -423,7 +427,8 @@ static int nv04_disable_plane(struct drm_plane *plane) { struct nvif_device *dev = &nouveau_drm(plane->dev)->device; - struct nouveau_plane *nv_plane = (struct nouveau_plane *)plane; + struct nouveau_plane *nv_plane = + container_of(plane, struct nouveau_plane, base);
nvif_mask(dev, NV_PVIDEO_OVERLAY, 1, 0); nvif_wr32(dev, NV_PVIDEO_OE_STATE, 0);
Use container_of instead of casting first structure member.
Signed-off-by: Fabian Frederick fabf@skynet.be --- Compiled but untested.
drivers/gpu/drm/qxl/qxl_fb.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/drivers/gpu/drm/qxl/qxl_fb.c b/drivers/gpu/drm/qxl/qxl_fb.c index df56788..3d7c1d0 100644 --- a/drivers/gpu/drm/qxl/qxl_fb.c +++ b/drivers/gpu/drm/qxl/qxl_fb.c @@ -625,7 +625,8 @@ static int qxl_fb_find_or_create_single( struct drm_fb_helper *helper, struct drm_fb_helper_surface_size *sizes) { - struct qxl_fbdev *qfbdev = (struct qxl_fbdev *)helper; + struct qxl_fbdev *qfbdev = + container_of(helper, struct qxl_fbdev, helper); int new_fb = 0; int ret;
Use container_of instead of casting first structure member.
Signed-off-by: Fabian Frederick fabf@skynet.be --- Compiled but untested.
drivers/gpu/drm/gma500/framebuffer.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/drivers/gpu/drm/gma500/framebuffer.c b/drivers/gpu/drm/gma500/framebuffer.c index d0dd3be..ddd90dd 100644 --- a/drivers/gpu/drm/gma500/framebuffer.c +++ b/drivers/gpu/drm/gma500/framebuffer.c @@ -540,7 +540,8 @@ static void psbfb_gamma_get(struct drm_crtc *crtc, u16 *red, static int psbfb_probe(struct drm_fb_helper *helper, struct drm_fb_helper_surface_size *sizes) { - struct psb_fbdev *psb_fbdev = (struct psb_fbdev *)helper; + struct psb_fbdev *psb_fbdev = + container_of(helper, struct psb_fbdev, psb_fb_helper); struct drm_device *dev = psb_fbdev->psb_fb_helper.dev; struct drm_psb_private *dev_priv = dev->dev_private; int bytespp;
Use container_of instead of casting first structure member.
Signed-off-by: Fabian Frederick fabf@skynet.be --- Compiled but untested.
drivers/gpu/drm/ast/ast_fb.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/drivers/gpu/drm/ast/ast_fb.c b/drivers/gpu/drm/ast/ast_fb.c index cba45c7..5c60ae5 100644 --- a/drivers/gpu/drm/ast/ast_fb.c +++ b/drivers/gpu/drm/ast/ast_fb.c @@ -186,7 +186,8 @@ static int astfb_create_object(struct ast_fbdev *afbdev, static int astfb_create(struct drm_fb_helper *helper, struct drm_fb_helper_surface_size *sizes) { - struct ast_fbdev *afbdev = (struct ast_fbdev *)helper; + struct ast_fbdev *afbdev = + container_of(helper, struct ast_fbdev, helper); struct drm_device *dev = afbdev->helper.dev; struct drm_mode_fb_cmd2 mode_cmd; struct drm_framebuffer *fb;
Use container_of instead of casting first structure member.
Signed-off-by: Fabian Frederick fabf@skynet.be --- Compiled but untested.
drivers/gpu/drm/udl/udl_fb.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/drivers/gpu/drm/udl/udl_fb.c b/drivers/gpu/drm/udl/udl_fb.c index d1da339..8cbcb45 100644 --- a/drivers/gpu/drm/udl/udl_fb.c +++ b/drivers/gpu/drm/udl/udl_fb.c @@ -472,7 +472,8 @@ udl_framebuffer_init(struct drm_device *dev, static int udlfb_create(struct drm_fb_helper *helper, struct drm_fb_helper_surface_size *sizes) { - struct udl_fbdev *ufbdev = (struct udl_fbdev *)helper; + struct udl_fbdev *ufbdev = + container_of(helper, struct udl_fbdev, helper); struct drm_device *dev = ufbdev->helper.dev; struct fb_info *info; struct device *device = dev->dev;
On Sun, 14 Sep 2014 18:40:13 +0200 Fabian Frederick fabf@skynet.be wrote:
Small patchset using container_of instead of casting on first structure member address.
Why. Container_of is useful for random offsets but its just convoluting and confusing code which is designed with the fields intentionally at the start.
Alan
On 15 September 2014 at 01:13 One Thousand Gnomes gnomes@lxorguk.ukuu.org.uk wrote:
On Sun, 14 Sep 2014 18:40:13 +0200 Fabian Frederick fabf@skynet.be wrote:
Small patchset using container_of instead of casting on first structure member address.
Why. Container_of is useful for random offsets but its just convoluting and confusing code which is designed with the fields intentionally at the start.
Alan
What if someone doesn't know about that intention one day and inserts some field in the structure at the "wrong place" ? This would need at least some comment in each declaration but once again it's hard to control.One other way is to commonly use container_of and get rid of every casting with some semantic script.
Peter has been asking for container_of in the following:
http://marc.info/?l=linux-arm-kernel&m=140838705729653&w=2
struct uart_amba_port *uap = (struct uart_amba_port *)port
(port/uart_port is the first field in uart_amba_port though)
Regards, Fabian
On Mon, Sep 15, 2014 at 08:15:54PM +0200, Fabian Frederick wrote:
On 15 September 2014 at 01:13 One Thousand Gnomes gnomes@lxorguk.ukuu.org.uk wrote:
On Sun, 14 Sep 2014 18:40:13 +0200 Fabian Frederick fabf@skynet.be wrote:
Small patchset using container_of instead of casting on first structure member address.
Why. Container_of is useful for random offsets but its just convoluting and confusing code which is designed with the fields intentionally at the start.
Alan
What if someone doesn't know about that intention one day and inserts some field in the structure at the "wrong place" ? This would need at least some comment in each declaration but once again it's hard to control.One other way is to commonly use container_of and get rid of every casting with some semantic script.
Peter has been asking for container_of in the following:
http://marc.info/?l=linux-arm-kernel&m=140838705729653&w=2
struct uart_amba_port *uap = (struct uart_amba_port *)port
(port/uart_port is the first field in uart_amba_port though)
fwiw I like this and I've picked it up into my topic/core-stuff branch. So if driver writers don't outright nack this it'll all land in Dave's drm-next (probably still for 3.18). -Daniel
What if someone doesn't know about that intention one day and inserts some field in the structure at the "wrong place" ?
Then we have code review. Also they are much more likely to simply change the code elsewhere and break it. Nobody afaik ever got this wrong, while the litany of other broken patches and bugs introduced into the same code is lagre.
The goal of the kernel is not to be foolproof to developer incompetence - that's a battle you can't win until you replace driver developers with software systems.
Alan
dri-devel@lists.freedesktop.org