Skip to content

Commit df2052c

Browse files
Lekensteynkraxel
authored andcommitted
bochs: convert to drm_fb_helper_fbdev_setup/teardown
Currently unloading bochs_drm (after unbinding the vtconsole) results in a warning about a leaked connector: [drm:drm_mode_config_cleanup] *ERROR* connector Virtual-3 leaked! While investigating a potential fix I noticed that a lot of open-coded functionality is already implemented elsewhere, so start converting it: bochs_fbdev_init -> drm_fb_helper_fbdev_setup: trivial (similar impl). bochs_fbdev_fini -> drm_fb_helper_fbdev_teardown: requires unembedding "struct drm_framebuffer" from "struct bochs_framebuffer". Unembedding drm_framebuffer is made easy using drm_gem_fbdev_fb_create which can replace bochs_fbdev_destroy and custom routines in bochs_mm.c. For this to work, the GEM object is moved into "drm_framebuffer". After that, "bochs_framebuffer" is no longer needed and therefore removed. Remove the unused "size" and "initialized" fields from fb, the latter is not necessary as drm_fb_helper_fbdev_teardown can be called even if bochsfb_create fails. This theory was tested by returning early and late (just before drm_gem_fbdev_fb_create). Both scenarios fail gracefully although the latter seems to leak the object from bochsfb_create_object (not a regression). Guess on the reason for the encoder leak: drm_framebuffer_cleanup was previously used, but did not destroy much. drm_fb_helper_fbdev_teardown is now used and calls drm_framebuffer_remove which does a bit more work. Tested with 'echo 0 > /sys/class/vtconsole/vtcon1/bind; rmmod bochs_drm' and also with Xorg + fbdev (startx -> xterm). The latter triggered a warning in ttm_bo_vm_open that existed before, see https://lkml.kernel.org/r/1464000533-13140-4-git-send-email-mstaudt@suse.de Acked-by: Daniel Vetter <daniel.vetter@ffwll.ch> Signed-off-by: Peter Wu <peter@lekensteyn.nl> Link: http://patchwork.freedesktop.org/patch/msgid/20180906221810.20170-3-peter@lekensteyn.nl Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
1 parent 70c0ef7 commit df2052c

File tree

4 files changed

+22
-157
lines changed

4 files changed

+22
-157
lines changed

drivers/gpu/drm/bochs/bochs.h

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,6 @@ enum bochs_types {
5151
BOCHS_UNKNOWN,
5252
};
5353

54-
struct bochs_framebuffer {
55-
struct drm_framebuffer base;
56-
struct drm_gem_object *obj;
57-
};
58-
5954
struct bochs_device {
6055
/* hw */
6156
void __iomem *mmio;
@@ -88,15 +83,11 @@ struct bochs_device {
8883

8984
/* fbdev */
9085
struct {
91-
struct bochs_framebuffer gfb;
86+
struct drm_framebuffer *fb;
9287
struct drm_fb_helper helper;
93-
int size;
94-
bool initialized;
9588
} fb;
9689
};
9790

98-
#define to_bochs_framebuffer(x) container_of(x, struct bochs_framebuffer, base)
99-
10091
struct bochs_bo {
10192
struct ttm_buffer_object bo;
10293
struct ttm_placement placement;
@@ -148,19 +139,15 @@ int bochs_dumb_create(struct drm_file *file, struct drm_device *dev,
148139
int bochs_dumb_mmap_offset(struct drm_file *file, struct drm_device *dev,
149140
uint32_t handle, uint64_t *offset);
150141

151-
int bochs_framebuffer_init(struct drm_device *dev,
152-
struct bochs_framebuffer *gfb,
153-
const struct drm_mode_fb_cmd2 *mode_cmd,
154-
struct drm_gem_object *obj);
155142
int bochs_bo_pin(struct bochs_bo *bo, u32 pl_flag, u64 *gpu_addr);
156143
int bochs_bo_unpin(struct bochs_bo *bo);
157144

158-
extern const struct drm_mode_config_funcs bochs_mode_funcs;
159-
160145
/* bochs_kms.c */
161146
int bochs_kms_init(struct bochs_device *bochs);
162147
void bochs_kms_fini(struct bochs_device *bochs);
163148

164149
/* bochs_fbdev.c */
165150
int bochs_fbdev_init(struct bochs_device *bochs);
166151
void bochs_fbdev_fini(struct bochs_device *bochs);
152+
153+
extern const struct drm_mode_config_funcs bochs_mode_funcs;

drivers/gpu/drm/bochs/bochs_fbdev.c

Lines changed: 17 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,15 @@
66
*/
77

88
#include "bochs.h"
9+
#include <drm/drm_gem_framebuffer_helper.h>
910

1011
/* ---------------------------------------------------------------------- */
1112

1213
static int bochsfb_mmap(struct fb_info *info,
1314
struct vm_area_struct *vma)
1415
{
1516
struct drm_fb_helper *fb_helper = info->par;
16-
struct bochs_device *bochs =
17-
container_of(fb_helper, struct bochs_device, fb.helper);
18-
struct bochs_bo *bo = gem_to_bochs_bo(bochs->fb.gfb.obj);
17+
struct bochs_bo *bo = gem_to_bochs_bo(fb_helper->fb->obj[0]);
1918

2019
return ttm_fbdev_mmap(vma, &bo->bo);
2120
}
@@ -101,19 +100,20 @@ static int bochsfb_create(struct drm_fb_helper *helper,
101100

102101
/* init fb device */
103102
info = drm_fb_helper_alloc_fbi(helper);
104-
if (IS_ERR(info))
103+
if (IS_ERR(info)) {
104+
DRM_ERROR("Failed to allocate fbi: %ld\n", PTR_ERR(info));
105105
return PTR_ERR(info);
106+
}
106107

107108
info->par = &bochs->fb.helper;
108109

109-
ret = bochs_framebuffer_init(bochs->dev, &bochs->fb.gfb, &mode_cmd, gobj);
110-
if (ret)
111-
return ret;
112-
113-
bochs->fb.size = size;
110+
fb = drm_gem_fbdev_fb_create(bochs->dev, sizes, 0, gobj, NULL);
111+
if (IS_ERR(fb)) {
112+
DRM_ERROR("Failed to create framebuffer: %ld\n", PTR_ERR(fb));
113+
return PTR_ERR(fb);
114+
}
114115

115116
/* setup helper */
116-
fb = &bochs->fb.gfb.base;
117117
bochs->fb.helper.fb = fb;
118118

119119
strcpy(info->fix.id, "bochsdrmfb");
@@ -130,69 +130,24 @@ static int bochsfb_create(struct drm_fb_helper *helper,
130130
drm_vma_offset_remove(&bo->bo.bdev->vma_manager, &bo->bo.vma_node);
131131
info->fix.smem_start = 0;
132132
info->fix.smem_len = size;
133-
134-
bochs->fb.initialized = true;
135-
return 0;
136-
}
137-
138-
static int bochs_fbdev_destroy(struct bochs_device *bochs)
139-
{
140-
struct bochs_framebuffer *gfb = &bochs->fb.gfb;
141-
142-
DRM_DEBUG_DRIVER("\n");
143-
144-
drm_fb_helper_unregister_fbi(&bochs->fb.helper);
145-
146-
if (gfb->obj) {
147-
drm_gem_object_unreference_unlocked(gfb->obj);
148-
gfb->obj = NULL;
149-
}
150-
151-
drm_framebuffer_unregister_private(&gfb->base);
152-
drm_framebuffer_cleanup(&gfb->base);
153-
154133
return 0;
155134
}
156135

157136
static const struct drm_fb_helper_funcs bochs_fb_helper_funcs = {
158137
.fb_probe = bochsfb_create,
159138
};
160139

140+
const struct drm_mode_config_funcs bochs_mode_funcs = {
141+
.fb_create = drm_gem_fb_create,
142+
};
143+
161144
int bochs_fbdev_init(struct bochs_device *bochs)
162145
{
163-
int ret;
164-
165-
drm_fb_helper_prepare(bochs->dev, &bochs->fb.helper,
166-
&bochs_fb_helper_funcs);
167-
168-
ret = drm_fb_helper_init(bochs->dev, &bochs->fb.helper, 1);
169-
if (ret)
170-
return ret;
171-
172-
ret = drm_fb_helper_single_add_all_connectors(&bochs->fb.helper);
173-
if (ret)
174-
goto fini;
175-
176-
drm_helper_disable_unused_functions(bochs->dev);
177-
178-
ret = drm_fb_helper_initial_config(&bochs->fb.helper, 32);
179-
if (ret)
180-
goto fini;
181-
182-
return 0;
183-
184-
fini:
185-
drm_fb_helper_fini(&bochs->fb.helper);
186-
return ret;
146+
return drm_fb_helper_fbdev_setup(bochs->dev, &bochs->fb.helper,
147+
&bochs_fb_helper_funcs, 32, 1);
187148
}
188149

189150
void bochs_fbdev_fini(struct bochs_device *bochs)
190151
{
191-
if (bochs->fb.initialized)
192-
bochs_fbdev_destroy(bochs);
193-
194-
if (bochs->fb.helper.fbdev)
195-
drm_fb_helper_fini(&bochs->fb.helper);
196-
197-
bochs->fb.initialized = false;
152+
drm_fb_helper_fbdev_teardown(bochs->dev);
198153
}

drivers/gpu/drm/bochs/bochs_kms.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,12 @@ static int bochs_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
3535
{
3636
struct bochs_device *bochs =
3737
container_of(crtc, struct bochs_device, crtc);
38-
struct bochs_framebuffer *bochs_fb;
3938
struct bochs_bo *bo;
4039
u64 gpu_addr = 0;
4140
int ret;
4241

4342
if (old_fb) {
44-
bochs_fb = to_bochs_framebuffer(old_fb);
45-
bo = gem_to_bochs_bo(bochs_fb->obj);
43+
bo = gem_to_bochs_bo(old_fb->obj[0]);
4644
ret = ttm_bo_reserve(&bo->bo, true, false, NULL);
4745
if (ret) {
4846
DRM_ERROR("failed to reserve old_fb bo\n");
@@ -55,8 +53,7 @@ static int bochs_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
5553
if (WARN_ON(crtc->primary->fb == NULL))
5654
return -EINVAL;
5755

58-
bochs_fb = to_bochs_framebuffer(crtc->primary->fb);
59-
bo = gem_to_bochs_bo(bochs_fb->obj);
56+
bo = gem_to_bochs_bo(crtc->primary->fb->obj[0]);
6057
ret = ttm_bo_reserve(&bo->bo, true, false, NULL);
6158
if (ret)
6259
return ret;

drivers/gpu/drm/bochs/bochs_mm.c

Lines changed: 0 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -457,77 +457,3 @@ int bochs_dumb_mmap_offset(struct drm_file *file, struct drm_device *dev,
457457
drm_gem_object_unreference_unlocked(obj);
458458
return 0;
459459
}
460-
461-
/* ---------------------------------------------------------------------- */
462-
463-
static void bochs_user_framebuffer_destroy(struct drm_framebuffer *fb)
464-
{
465-
struct bochs_framebuffer *bochs_fb = to_bochs_framebuffer(fb);
466-
467-
drm_gem_object_unreference_unlocked(bochs_fb->obj);
468-
drm_framebuffer_cleanup(fb);
469-
kfree(fb);
470-
}
471-
472-
static const struct drm_framebuffer_funcs bochs_fb_funcs = {
473-
.destroy = bochs_user_framebuffer_destroy,
474-
};
475-
476-
int bochs_framebuffer_init(struct drm_device *dev,
477-
struct bochs_framebuffer *gfb,
478-
const struct drm_mode_fb_cmd2 *mode_cmd,
479-
struct drm_gem_object *obj)
480-
{
481-
int ret;
482-
483-
drm_helper_mode_fill_fb_struct(dev, &gfb->base, mode_cmd);
484-
gfb->obj = obj;
485-
ret = drm_framebuffer_init(dev, &gfb->base, &bochs_fb_funcs);
486-
if (ret) {
487-
DRM_ERROR("drm_framebuffer_init failed: %d\n", ret);
488-
return ret;
489-
}
490-
return 0;
491-
}
492-
493-
static struct drm_framebuffer *
494-
bochs_user_framebuffer_create(struct drm_device *dev,
495-
struct drm_file *filp,
496-
const struct drm_mode_fb_cmd2 *mode_cmd)
497-
{
498-
struct drm_gem_object *obj;
499-
struct bochs_framebuffer *bochs_fb;
500-
int ret;
501-
502-
DRM_DEBUG_DRIVER("%dx%d, format %c%c%c%c\n",
503-
mode_cmd->width, mode_cmd->height,
504-
(mode_cmd->pixel_format) & 0xff,
505-
(mode_cmd->pixel_format >> 8) & 0xff,
506-
(mode_cmd->pixel_format >> 16) & 0xff,
507-
(mode_cmd->pixel_format >> 24) & 0xff);
508-
509-
if (mode_cmd->pixel_format != DRM_FORMAT_XRGB8888)
510-
return ERR_PTR(-ENOENT);
511-
512-
obj = drm_gem_object_lookup(filp, mode_cmd->handles[0]);
513-
if (obj == NULL)
514-
return ERR_PTR(-ENOENT);
515-
516-
bochs_fb = kzalloc(sizeof(*bochs_fb), GFP_KERNEL);
517-
if (!bochs_fb) {
518-
drm_gem_object_unreference_unlocked(obj);
519-
return ERR_PTR(-ENOMEM);
520-
}
521-
522-
ret = bochs_framebuffer_init(dev, bochs_fb, mode_cmd, obj);
523-
if (ret) {
524-
drm_gem_object_unreference_unlocked(obj);
525-
kfree(bochs_fb);
526-
return ERR_PTR(ret);
527-
}
528-
return &bochs_fb->base;
529-
}
530-
531-
const struct drm_mode_config_funcs bochs_mode_funcs = {
532-
.fb_create = bochs_user_framebuffer_create,
533-
};

0 commit comments

Comments
 (0)