Skip to content

Commit 5bdebb1

Browse files
committed
drm/sysfs: sort out minor and connector device object lifetimes.
So drm was abusing device lifetimes, by having embedded device structures in the minor and connector it meant that the lifetime of the internal drm objects (drm_minor and drm_connector) were tied to the lifetime of the device files in sysfs, so if something kept those files opened the current code would kfree the objects and things would go downhill from there. Now in reality there is no need for these lifetimes to be so intertwined, especailly with hotplugging of devices where we wish to remove the sysfs and userspace facing pieces before we can unwind the internal objects due to open userspace files or mmaps, so split the objects out so the struct device is no longer embedded and do what fbdev does and just allocate and remove the sysfs inodes separately. Signed-off-by: Dave Airlie <airlied@redhat.com>
1 parent 14c8d11 commit 5bdebb1

11 files changed

Lines changed: 61 additions & 87 deletions

File tree

‎drivers/gpu/drm/drm_sysfs.c‎

Lines changed: 34 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
#include <drm/drm_core.h>
2323
#include <drm/drmP.h>
2424

25-
#define to_drm_minor(d) container_of(d, struct drm_minor, kdev)
26-
#define to_drm_connector(d) container_of(d, struct drm_connector, kdev)
25+
#define to_drm_minor(d) dev_get_drvdata(d)
26+
#define to_drm_connector(d) dev_get_drvdata(d)
2727

2828
static struct device_type drm_sysfs_device_minor = {
2929
.name = "drm_minor"
@@ -162,20 +162,6 @@ void drm_sysfs_destroy(void)
162162
drm_class = NULL;
163163
}
164164

165-
/**
166-
* drm_sysfs_device_release - do nothing
167-
* @dev: Linux device
168-
*
169-
* Normally, this would free the DRM device associated with @dev, along
170-
* with cleaning up any other stuff. But we do that in the DRM core, so
171-
* this function can just return and hope that the core does its job.
172-
*/
173-
static void drm_sysfs_device_release(struct device *dev)
174-
{
175-
memset(dev, 0, sizeof(struct device));
176-
return;
177-
}
178-
179165
/*
180166
* Connector properties
181167
*/
@@ -394,29 +380,26 @@ int drm_sysfs_connector_add(struct drm_connector *connector)
394380
int i;
395381
int ret;
396382

397-
/* We shouldn't get called more than once for the same connector */
398-
BUG_ON(device_is_registered(&connector->kdev));
399-
400-
connector->kdev.parent = &dev->primary->kdev;
401-
connector->kdev.class = drm_class;
402-
connector->kdev.release = drm_sysfs_device_release;
383+
if (connector->kdev)
384+
return 0;
403385

386+
/* We shouldn't get called more than once for the same connector */
387+
connector->kdev = device_create(drm_class, dev->primary->kdev,
388+
0, connector, "card%d-%s",
389+
dev->primary->index, drm_get_connector_name(connector));
404390
DRM_DEBUG("adding \"%s\" to sysfs\n",
405391
drm_get_connector_name(connector));
406392

407-
dev_set_name(&connector->kdev, "card%d-%s",
408-
dev->primary->index, drm_get_connector_name(connector));
409-
ret = device_register(&connector->kdev);
410-
411-
if (ret) {
412-
DRM_ERROR("failed to register connector device: %d\n", ret);
393+
if (IS_ERR(connector->kdev)) {
394+
DRM_ERROR("failed to register connector device: %ld\n", PTR_ERR(connector->kdev));
395+
ret = PTR_ERR(connector->kdev);
413396
goto out;
414397
}
415398

416399
/* Standard attributes */
417400

418401
for (attr_cnt = 0; attr_cnt < ARRAY_SIZE(connector_attrs); attr_cnt++) {
419-
ret = device_create_file(&connector->kdev, &connector_attrs[attr_cnt]);
402+
ret = device_create_file(connector->kdev, &connector_attrs[attr_cnt]);
420403
if (ret)
421404
goto err_out_files;
422405
}
@@ -433,7 +416,7 @@ int drm_sysfs_connector_add(struct drm_connector *connector)
433416
case DRM_MODE_CONNECTOR_Component:
434417
case DRM_MODE_CONNECTOR_TV:
435418
for (opt_cnt = 0; opt_cnt < ARRAY_SIZE(connector_attrs_opt1); opt_cnt++) {
436-
ret = device_create_file(&connector->kdev, &connector_attrs_opt1[opt_cnt]);
419+
ret = device_create_file(connector->kdev, &connector_attrs_opt1[opt_cnt]);
437420
if (ret)
438421
goto err_out_files;
439422
}
@@ -442,7 +425,7 @@ int drm_sysfs_connector_add(struct drm_connector *connector)
442425
break;
443426
}
444427

445-
ret = sysfs_create_bin_file(&connector->kdev.kobj, &edid_attr);
428+
ret = sysfs_create_bin_file(&connector->kdev->kobj, &edid_attr);
446429
if (ret)
447430
goto err_out_files;
448431

@@ -453,10 +436,11 @@ int drm_sysfs_connector_add(struct drm_connector *connector)
453436

454437
err_out_files:
455438
for (i = 0; i < opt_cnt; i++)
456-
device_remove_file(&connector->kdev, &connector_attrs_opt1[i]);
439+
device_remove_file(connector->kdev, &connector_attrs_opt1[i]);
457440
for (i = 0; i < attr_cnt; i++)
458-
device_remove_file(&connector->kdev, &connector_attrs[i]);
459-
device_unregister(&connector->kdev);
441+
device_remove_file(connector->kdev, &connector_attrs[i]);
442+
put_device(connector->kdev);
443+
device_unregister(connector->kdev);
460444

461445
out:
462446
return ret;
@@ -480,16 +464,17 @@ void drm_sysfs_connector_remove(struct drm_connector *connector)
480464
{
481465
int i;
482466

483-
if (!connector->kdev.parent)
467+
if (!connector->kdev)
484468
return;
485469
DRM_DEBUG("removing \"%s\" from sysfs\n",
486470
drm_get_connector_name(connector));
487471

488472
for (i = 0; i < ARRAY_SIZE(connector_attrs); i++)
489-
device_remove_file(&connector->kdev, &connector_attrs[i]);
490-
sysfs_remove_bin_file(&connector->kdev.kobj, &edid_attr);
491-
device_unregister(&connector->kdev);
492-
connector->kdev.parent = NULL;
473+
device_remove_file(connector->kdev, &connector_attrs[i]);
474+
sysfs_remove_bin_file(&connector->kdev->kobj, &edid_attr);
475+
put_device(connector->kdev);
476+
device_unregister(connector->kdev);
477+
connector->kdev = NULL;
493478
}
494479
EXPORT_SYMBOL(drm_sysfs_connector_remove);
495480

@@ -508,7 +493,7 @@ void drm_sysfs_hotplug_event(struct drm_device *dev)
508493

509494
DRM_DEBUG("generating hotplug event\n");
510495

511-
kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, envp);
496+
kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE, envp);
512497
}
513498
EXPORT_SYMBOL(drm_sysfs_hotplug_event);
514499

@@ -523,34 +508,23 @@ EXPORT_SYMBOL(drm_sysfs_hotplug_event);
523508
*/
524509
int drm_sysfs_device_add(struct drm_minor *minor)
525510
{
526-
int err;
527511
char *minor_str;
528512

529-
minor->kdev.parent = minor->dev->dev;
530-
531-
minor->kdev.class = drm_class;
532-
minor->kdev.release = drm_sysfs_device_release;
533-
minor->kdev.devt = minor->device;
534-
minor->kdev.type = &drm_sysfs_device_minor;
535513
if (minor->type == DRM_MINOR_CONTROL)
536514
minor_str = "controlD%d";
537515
else if (minor->type == DRM_MINOR_RENDER)
538516
minor_str = "renderD%d";
539517
else
540518
minor_str = "card%d";
541519

542-
dev_set_name(&minor->kdev, minor_str, minor->index);
543-
544-
err = device_register(&minor->kdev);
545-
if (err) {
546-
DRM_ERROR("device add failed: %d\n", err);
547-
goto err_out;
520+
minor->kdev = device_create(drm_class, minor->dev->dev,
521+
MKDEV(DRM_MAJOR, minor->index),
522+
minor, minor_str, minor->index);
523+
if (IS_ERR(minor->kdev)) {
524+
DRM_ERROR("device create failed %ld\n", PTR_ERR(minor->kdev));
525+
return PTR_ERR(minor->kdev);
548526
}
549-
550527
return 0;
551-
552-
err_out:
553-
return err;
554528
}
555529

556530
/**
@@ -562,9 +536,9 @@ int drm_sysfs_device_add(struct drm_minor *minor)
562536
*/
563537
void drm_sysfs_device_remove(struct drm_minor *minor)
564538
{
565-
if (minor->kdev.parent)
566-
device_unregister(&minor->kdev);
567-
minor->kdev.parent = NULL;
539+
if (minor->kdev)
540+
device_destroy(drm_class, MKDEV(DRM_MAJOR, minor->index));
541+
minor->kdev = NULL;
568542
}
569543

570544

‎drivers/gpu/drm/gma500/cdv_intel_dp.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,7 @@ cdv_intel_dp_i2c_init(struct gma_connector *connector,
666666
strncpy (intel_dp->adapter.name, name, sizeof(intel_dp->adapter.name) - 1);
667667
intel_dp->adapter.name[sizeof(intel_dp->adapter.name) - 1] = '\0';
668668
intel_dp->adapter.algo_data = &intel_dp->algo;
669-
intel_dp->adapter.dev.parent = &connector->base.kdev;
669+
intel_dp->adapter.dev.parent = connector->base.kdev;
670670

671671
if (is_edp(encoder))
672672
cdv_intel_edp_panel_vdd_on(encoder);

‎drivers/gpu/drm/i915/i915_irq.c‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -942,7 +942,7 @@ static void ivybridge_parity_work(struct work_struct *work)
942942
parity_event[4] = kasprintf(GFP_KERNEL, "SLICE=%d", slice);
943943
parity_event[5] = NULL;
944944

945-
kobject_uevent_env(&dev_priv->dev->primary->kdev.kobj,
945+
kobject_uevent_env(&dev_priv->dev->primary->kdev->kobj,
946946
KOBJ_CHANGE, parity_event);
947947

948948
DRM_DEBUG("Parity error: Slice = %d, Row = %d, Bank = %d, Sub bank = %d.\n",
@@ -1539,7 +1539,7 @@ static void i915_error_work_func(struct work_struct *work)
15391539
char *reset_done_event[] = { I915_ERROR_UEVENT "=0", NULL };
15401540
int ret;
15411541

1542-
kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, error_event);
1542+
kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE, error_event);
15431543

15441544
/*
15451545
* Note that there's only one work item which does gpu resets, so we
@@ -1553,7 +1553,7 @@ static void i915_error_work_func(struct work_struct *work)
15531553
*/
15541554
if (i915_reset_in_progress(error) && !i915_terminally_wedged(error)) {
15551555
DRM_DEBUG_DRIVER("resetting chip\n");
1556-
kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE,
1556+
kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE,
15571557
reset_event);
15581558

15591559
/*
@@ -1580,7 +1580,7 @@ static void i915_error_work_func(struct work_struct *work)
15801580
smp_mb__before_atomic_inc();
15811581
atomic_inc(&dev_priv->gpu_error.reset_counter);
15821582

1583-
kobject_uevent_env(&dev->primary->kdev.kobj,
1583+
kobject_uevent_env(&dev->primary->kdev->kobj,
15841584
KOBJ_CHANGE, reset_done_event);
15851585
} else {
15861586
atomic_set(&error->reset_counter, I915_WEDGED);

‎drivers/gpu/drm/i915/i915_sysfs.c‎

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
#include "intel_drv.h"
3333
#include "i915_drv.h"
3434

35-
#define dev_to_drm_minor(d) container_of((d), struct drm_minor, kdev)
35+
#define dev_to_drm_minor(d) dev_get_drvdata((d))
3636

3737
#ifdef CONFIG_PM
3838
static u32 calc_residency(struct drm_device *dev, const u32 reg)
@@ -75,7 +75,7 @@ show_rc6_mask(struct device *kdev, struct device_attribute *attr, char *buf)
7575
static ssize_t
7676
show_rc6_ms(struct device *kdev, struct device_attribute *attr, char *buf)
7777
{
78-
struct drm_minor *dminor = dev_to_drm_minor(kdev);
78+
struct drm_minor *dminor = dev_get_drvdata(kdev);
7979
u32 rc6_residency = calc_residency(dminor->dev, GEN6_GT_GFX_RC6);
8080
return snprintf(buf, PAGE_SIZE, "%u\n", rc6_residency);
8181
}
@@ -543,19 +543,19 @@ void i915_setup_sysfs(struct drm_device *dev)
543543

544544
#ifdef CONFIG_PM
545545
if (INTEL_INFO(dev)->gen >= 6) {
546-
ret = sysfs_merge_group(&dev->primary->kdev.kobj,
546+
ret = sysfs_merge_group(&dev->primary->kdev->kobj,
547547
&rc6_attr_group);
548548
if (ret)
549549
DRM_ERROR("RC6 residency sysfs setup failed\n");
550550
}
551551
#endif
552552
if (HAS_L3_DPF(dev)) {
553-
ret = device_create_bin_file(&dev->primary->kdev, &dpf_attrs);
553+
ret = device_create_bin_file(dev->primary->kdev, &dpf_attrs);
554554
if (ret)
555555
DRM_ERROR("l3 parity sysfs setup failed\n");
556556

557557
if (NUM_L3_SLICES(dev) > 1) {
558-
ret = device_create_bin_file(&dev->primary->kdev,
558+
ret = device_create_bin_file(dev->primary->kdev,
559559
&dpf_attrs_1);
560560
if (ret)
561561
DRM_ERROR("l3 parity slice 1 setup failed\n");
@@ -564,28 +564,28 @@ void i915_setup_sysfs(struct drm_device *dev)
564564

565565
ret = 0;
566566
if (IS_VALLEYVIEW(dev))
567-
ret = sysfs_create_files(&dev->primary->kdev.kobj, vlv_attrs);
567+
ret = sysfs_create_files(&dev->primary->kdev->kobj, vlv_attrs);
568568
else if (INTEL_INFO(dev)->gen >= 6)
569-
ret = sysfs_create_files(&dev->primary->kdev.kobj, gen6_attrs);
569+
ret = sysfs_create_files(&dev->primary->kdev->kobj, gen6_attrs);
570570
if (ret)
571571
DRM_ERROR("RPS sysfs setup failed\n");
572572

573-
ret = sysfs_create_bin_file(&dev->primary->kdev.kobj,
573+
ret = sysfs_create_bin_file(&dev->primary->kdev->kobj,
574574
&error_state_attr);
575575
if (ret)
576576
DRM_ERROR("error_state sysfs setup failed\n");
577577
}
578578

579579
void i915_teardown_sysfs(struct drm_device *dev)
580580
{
581-
sysfs_remove_bin_file(&dev->primary->kdev.kobj, &error_state_attr);
581+
sysfs_remove_bin_file(&dev->primary->kdev->kobj, &error_state_attr);
582582
if (IS_VALLEYVIEW(dev))
583-
sysfs_remove_files(&dev->primary->kdev.kobj, vlv_attrs);
583+
sysfs_remove_files(&dev->primary->kdev->kobj, vlv_attrs);
584584
else
585-
sysfs_remove_files(&dev->primary->kdev.kobj, gen6_attrs);
586-
device_remove_bin_file(&dev->primary->kdev, &dpf_attrs_1);
587-
device_remove_bin_file(&dev->primary->kdev, &dpf_attrs);
585+
sysfs_remove_files(&dev->primary->kdev->kobj, gen6_attrs);
586+
device_remove_bin_file(dev->primary->kdev, &dpf_attrs_1);
587+
device_remove_bin_file(dev->primary->kdev, &dpf_attrs);
588588
#ifdef CONFIG_PM
589-
sysfs_unmerge_group(&dev->primary->kdev.kobj, &rc6_attr_group);
589+
sysfs_unmerge_group(&dev->primary->kdev->kobj, &rc6_attr_group);
590590
#endif
591591
}

‎drivers/gpu/drm/i915/intel_dp.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,7 @@ intel_dp_i2c_init(struct intel_dp *intel_dp,
737737
strncpy(intel_dp->adapter.name, name, sizeof(intel_dp->adapter.name) - 1);
738738
intel_dp->adapter.name[sizeof(intel_dp->adapter.name) - 1] = '\0';
739739
intel_dp->adapter.algo_data = &intel_dp->algo;
740-
intel_dp->adapter.dev.parent = &intel_connector->base.kdev;
740+
intel_dp->adapter.dev.parent = intel_connector->base.kdev;
741741

742742
ironlake_edp_panel_vdd_on(intel_dp);
743743
ret = i2c_dp_aux_add_bus(&intel_dp->adapter);

‎drivers/gpu/drm/i915/intel_panel.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -722,7 +722,7 @@ int intel_panel_setup_backlight(struct drm_connector *connector)
722722
}
723723
dev_priv->backlight.device =
724724
backlight_device_register("intel_backlight",
725-
&connector->kdev, dev,
725+
connector->kdev, dev,
726726
&intel_panel_bl_ops, &props);
727727

728728
if (IS_ERR(dev_priv->backlight.device)) {

‎drivers/gpu/drm/nouveau/nouveau_backlight.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ nv40_backlight_init(struct drm_connector *connector)
8282
memset(&props, 0, sizeof(struct backlight_properties));
8383
props.type = BACKLIGHT_RAW;
8484
props.max_brightness = 31;
85-
bd = backlight_device_register("nv_backlight", &connector->kdev, drm,
85+
bd = backlight_device_register("nv_backlight", connector->kdev, drm,
8686
&nv40_bl_ops, &props);
8787
if (IS_ERR(bd))
8888
return PTR_ERR(bd);
@@ -204,7 +204,7 @@ nv50_backlight_init(struct drm_connector *connector)
204204
memset(&props, 0, sizeof(struct backlight_properties));
205205
props.type = BACKLIGHT_RAW;
206206
props.max_brightness = 100;
207-
bd = backlight_device_register("nv_backlight", &connector->kdev,
207+
bd = backlight_device_register("nv_backlight", connector->kdev,
208208
nv_encoder, ops, &props);
209209
if (IS_ERR(bd))
210210
return PTR_ERR(bd);

‎drivers/gpu/drm/radeon/atombios_encoders.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ void radeon_atom_backlight_init(struct radeon_encoder *radeon_encoder,
213213
props.type = BACKLIGHT_RAW;
214214
snprintf(bl_name, sizeof(bl_name),
215215
"radeon_bl%d", dev->primary->index);
216-
bd = backlight_device_register(bl_name, &drm_connector->kdev,
216+
bd = backlight_device_register(bl_name, drm_connector->kdev,
217217
pdata, &radeon_atom_backlight_ops, &props);
218218
if (IS_ERR(bd)) {
219219
DRM_ERROR("Backlight registration failed\n");

‎drivers/gpu/drm/radeon/radeon_legacy_encoders.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ void radeon_legacy_backlight_init(struct radeon_encoder *radeon_encoder,
392392
props.type = BACKLIGHT_RAW;
393393
snprintf(bl_name, sizeof(bl_name),
394394
"radeon_bl%d", dev->primary->index);
395-
bd = backlight_device_register(bl_name, &drm_connector->kdev,
395+
bd = backlight_device_register(bl_name, drm_connector->kdev,
396396
pdata, &radeon_backlight_ops, &props);
397397
if (IS_ERR(bd)) {
398398
DRM_ERROR("Backlight registration failed\n");

‎include/drm/drmP.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1046,7 +1046,7 @@ struct drm_minor {
10461046
int index; /**< Minor device number */
10471047
int type; /**< Control or render */
10481048
dev_t device; /**< Device number for mknod */
1049-
struct device kdev; /**< Linux device */
1049+
struct device *kdev; /**< Linux device */
10501050
struct drm_device *dev;
10511051

10521052
struct dentry *debugfs_root;

0 commit comments

Comments
 (0)