Skip to content

Commit f57499f

Browse files
jannaumarcan
authored andcommitted
drm/apple: Allocate drm objects according to drm's expectations
drm's documentaion explicitly tells us not to use devm_kzalloc(). drm device structures might out live the device when they are in use by userspace while the device vanishes.
1 parent 239f772 commit f57499f

2 files changed

Lines changed: 37 additions & 13 deletions

File tree

drivers/gpu/drm/apple/apple_drv.c

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,16 @@ static const struct drm_plane_helper_funcs apple_plane_helper_funcs = {
113113
.atomic_update = apple_plane_atomic_update,
114114
};
115115

116+
static void apple_plane_cleanup(struct drm_plane *plane)
117+
{
118+
drm_plane_cleanup(plane);
119+
kfree(plane);
120+
}
121+
116122
static const struct drm_plane_funcs apple_plane_funcs = {
117123
.update_plane = drm_atomic_helper_update_plane,
118124
.disable_plane = drm_atomic_helper_disable_plane,
119-
.destroy = drm_plane_cleanup,
125+
.destroy = apple_plane_cleanup,
120126
.reset = drm_atomic_helper_plane_reset,
121127
.atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
122128
.atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
@@ -154,7 +160,7 @@ static struct drm_plane *apple_plane_init(struct drm_device *dev,
154160
int ret;
155161
struct drm_plane *plane;
156162

157-
plane = devm_kzalloc(dev->dev, sizeof(*plane), GFP_KERNEL);
163+
plane = kzalloc(sizeof(*plane), GFP_KERNEL);
158164

159165
ret = drm_universal_plane_init(dev, plane, possible_crtcs,
160166
&apple_plane_funcs,
@@ -247,11 +253,16 @@ static void dcp_atomic_commit_tail(struct drm_atomic_state *old_state)
247253
drm_atomic_helper_cleanup_planes(dev, old_state);
248254
}
249255

256+
static void apple_crtc_cleanup(struct drm_crtc *crtc)
257+
{
258+
drm_crtc_cleanup(crtc);
259+
kfree(to_apple_crtc(crtc));
260+
}
250261

251262
static const struct drm_crtc_funcs apple_crtc_funcs = {
252263
.atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
253264
.atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
254-
.destroy = drm_crtc_cleanup,
265+
.destroy = apple_crtc_cleanup,
255266
.page_flip = drm_atomic_helper_page_flip,
256267
.reset = drm_atomic_helper_crtc_reset,
257268
.set_config = drm_atomic_helper_set_config,
@@ -267,9 +278,15 @@ static const struct drm_mode_config_helper_funcs apple_mode_config_helpers = {
267278
.atomic_commit_tail = dcp_atomic_commit_tail,
268279
};
269280

281+
static void appledrm_connector_cleanup(struct drm_connector *connector)
282+
{
283+
drm_connector_cleanup(connector);
284+
kfree(to_apple_connector(connector));
285+
}
286+
270287
static const struct drm_connector_funcs apple_connector_funcs = {
271288
.fill_modes = drm_helper_probe_single_connector_modes,
272-
.destroy = drm_connector_cleanup,
289+
.destroy = appledrm_connector_cleanup,
273290
.reset = drm_atomic_helper_connector_reset,
274291
.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
275292
.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
@@ -297,7 +314,7 @@ static int apple_probe_per_dcp(struct device *dev,
297314
{
298315
struct apple_crtc *crtc;
299316
struct apple_connector *connector;
300-
struct drm_encoder *encoder;
317+
struct apple_encoder *enc;
301318
struct drm_plane *primary;
302319
int ret;
303320

@@ -306,21 +323,21 @@ static int apple_probe_per_dcp(struct device *dev,
306323
if (IS_ERR(primary))
307324
return PTR_ERR(primary);
308325

309-
crtc = devm_kzalloc(dev, sizeof(*crtc), GFP_KERNEL);
326+
crtc = kzalloc(sizeof(*crtc), GFP_KERNEL);
310327
ret = drm_crtc_init_with_planes(drm, &crtc->base, primary, NULL,
311328
&apple_crtc_funcs, NULL);
312329
if (ret)
313330
return ret;
314331

315332
drm_crtc_helper_add(&crtc->base, &apple_crtc_helper_funcs);
316333

317-
encoder = devm_kzalloc(dev, sizeof(*encoder), GFP_KERNEL);
318-
encoder->possible_crtcs = drm_crtc_mask(&crtc->base);
319-
ret = drm_simple_encoder_init(drm, encoder, DRM_MODE_ENCODER_TMDS);
320-
if (ret)
321-
return ret;
334+
enc = drmm_simple_encoder_alloc(drm, struct apple_encoder, base,
335+
DRM_MODE_ENCODER_TMDS);
336+
if (IS_ERR(enc))
337+
return PTR_ERR(enc);
338+
enc->base.possible_crtcs = drm_crtc_mask(&crtc->base);
322339

323-
connector = devm_kzalloc(dev, sizeof(*connector), GFP_KERNEL);
340+
connector = kzalloc(sizeof(*connector), GFP_KERNEL);
324341
drm_connector_helper_add(&connector->base,
325342
&apple_connector_helper_funcs);
326343

@@ -338,7 +355,7 @@ static int apple_probe_per_dcp(struct device *dev,
338355
crtc->dcp = dcp;
339356
dcp_link(dcp, crtc, connector);
340357

341-
return drm_connector_attach_encoder(&connector->base, encoder);
358+
return drm_connector_attach_encoder(&connector->base, &enc->base);
342359
}
343360

344361
static int apple_get_fb_resource(struct device *dev, const char *name,

drivers/gpu/drm/apple/dcp.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#define __APPLE_DCP_H__
66

77
#include <drm/drm_atomic.h>
8+
#include <drm/drm_encoder.h>
89
#include <drm/drm_fourcc.h>
910

1011
#include "dcp-internal.h"
@@ -35,6 +36,12 @@ struct apple_connector {
3536

3637
#define to_apple_connector(x) container_of(x, struct apple_connector, base)
3738

39+
struct apple_encoder {
40+
struct drm_encoder base;
41+
};
42+
43+
#define to_apple_encoder(x) container_of(x, struct apple_encoder, base)
44+
3845
void dcp_poweroff(struct platform_device *pdev);
3946
void dcp_poweron(struct platform_device *pdev);
4047
int dcp_crtc_atomic_check(struct drm_crtc *crtc, struct drm_atomic_state *state);

0 commit comments

Comments
 (0)