On Thu, 01 Aug 2024, Gustavo Sousa <gustavo.sousa@xxxxxxxxx> wrote: > Quoting Jani Nikula (2024-07-29 11:30:06-03:00) >>In the future, the display code shall not have any idea about struct >>xe_device or struct drm_i915_private, but will need to get at the struct >>drm_device via drvdata. Store the struct drm_device pointer to drvdata >>instead of the driver specific pointer. >> >>Even though struct drm_device is embedded in both struct xe_device and >>struct drm_i915_private at offset 0, take care to check for NULL before >>using container_of() to allow for other offsets. > > I think the second half of this paragraph could be rephrased. One might > think that the text is suggesting that checking for NULL has something > to do with allowing other offsets. > > I would jump directly to mention using container_of() and would assume > that it is implied that NULL check is necessary before using it. :-) But I *am* suggesting the NULL check before container_of() has everything to do with allowing other offsets! container_of() will return a NULL pointer for a NULL pointer when the offset is 0, but will return a negative garbage pointer otherwise. BR, Jani. > > Reviewed-by: Gustavo Sousa <gustavo.sousa@xxxxxxxxx> > >> >>Signed-off-by: Jani Nikula <jani.nikula@xxxxxxxxx> >>--- >> drivers/gpu/drm/i915/i915_driver.c | 2 +- >> drivers/gpu/drm/i915/i915_drv.h | 8 ++++++-- >> drivers/gpu/drm/i915/selftests/mock_gem_device.c | 2 +- >> drivers/gpu/drm/xe/compat-i915-headers/i915_drv.h | 4 +++- >> drivers/gpu/drm/xe/xe_device.h | 8 ++++++-- >> drivers/gpu/drm/xe/xe_pci.c | 2 +- >> 6 files changed, 18 insertions(+), 8 deletions(-) >> >>diff --git a/drivers/gpu/drm/i915/i915_driver.c b/drivers/gpu/drm/i915/i915_driver.c >>index fb8e9c2fcea5..176c13c2e191 100644 >>--- a/drivers/gpu/drm/i915/i915_driver.c >>+++ b/drivers/gpu/drm/i915/i915_driver.c >>@@ -723,7 +723,7 @@ i915_driver_create(struct pci_dev *pdev, const struct pci_device_id *ent) >> if (IS_ERR(i915)) >> return i915; >> >>- pci_set_drvdata(pdev, i915); >>+ pci_set_drvdata(pdev, &i915->drm); >> >> /* Device parameters start as a copy of module parameters. */ >> i915_params_copy(&i915->params, &i915_modparams); >>diff --git a/drivers/gpu/drm/i915/i915_drv.h b/drivers/gpu/drm/i915/i915_drv.h >>index d7723dd11c80..f6edaef73db5 100644 >>--- a/drivers/gpu/drm/i915/i915_drv.h >>+++ b/drivers/gpu/drm/i915/i915_drv.h >>@@ -365,12 +365,16 @@ static inline struct drm_i915_private *to_i915(const struct drm_device *dev) >> >> static inline struct drm_i915_private *kdev_to_i915(struct device *kdev) >> { >>- return dev_get_drvdata(kdev); >>+ struct drm_device *drm = dev_get_drvdata(kdev); >>+ >>+ return drm ? to_i915(drm) : NULL; >> } >> >> static inline struct drm_i915_private *pdev_to_i915(struct pci_dev *pdev) >> { >>- return pci_get_drvdata(pdev); >>+ struct drm_device *drm = pci_get_drvdata(pdev); >>+ >>+ return drm ? to_i915(drm) : NULL; >> } >> >> static inline struct intel_gt *to_gt(const struct drm_i915_private *i915) >>diff --git a/drivers/gpu/drm/i915/selftests/mock_gem_device.c b/drivers/gpu/drm/i915/selftests/mock_gem_device.c >>index 0bd29846873b..91794ca17a58 100644 >>--- a/drivers/gpu/drm/i915/selftests/mock_gem_device.c >>+++ b/drivers/gpu/drm/i915/selftests/mock_gem_device.c >>@@ -172,7 +172,7 @@ struct drm_i915_private *mock_gem_device(void) >> return NULL; >> } >> >>- pci_set_drvdata(pdev, i915); >>+ pci_set_drvdata(pdev, &i915->drm); >> >> /* Device parameters start as a copy of module parameters. */ >> i915_params_copy(&i915->params, &i915_modparams); >>diff --git a/drivers/gpu/drm/xe/compat-i915-headers/i915_drv.h b/drivers/gpu/drm/xe/compat-i915-headers/i915_drv.h >>index 2feedddf1e40..766fba88a3c8 100644 >>--- a/drivers/gpu/drm/xe/compat-i915-headers/i915_drv.h >>+++ b/drivers/gpu/drm/xe/compat-i915-headers/i915_drv.h >>@@ -23,7 +23,9 @@ static inline struct drm_i915_private *to_i915(const struct drm_device *dev) >> >> static inline struct drm_i915_private *kdev_to_i915(struct device *kdev) >> { >>- return dev_get_drvdata(kdev); >>+ struct drm_device *drm = dev_get_drvdata(kdev); >>+ >>+ return drm ? to_i915(drm) : NULL; >> } >> >> #define IS_PLATFORM(xe, x) ((xe)->info.platform == x) >>diff --git a/drivers/gpu/drm/xe/xe_device.h b/drivers/gpu/drm/xe/xe_device.h >>index 2c96f1b2aafd..022876eebfd5 100644 >>--- a/drivers/gpu/drm/xe/xe_device.h >>+++ b/drivers/gpu/drm/xe/xe_device.h >>@@ -17,12 +17,16 @@ static inline struct xe_device *to_xe_device(const struct drm_device *dev) >> >> static inline struct xe_device *kdev_to_xe_device(struct device *kdev) >> { >>- return dev_get_drvdata(kdev); >>+ struct drm_device *drm = dev_get_drvdata(kdev); >>+ >>+ return drm ? to_xe_device(drm) : NULL; >> } >> >> static inline struct xe_device *pdev_to_xe_device(struct pci_dev *pdev) >> { >>- return pci_get_drvdata(pdev); >>+ struct drm_device *drm = pci_get_drvdata(pdev); >>+ >>+ return drm ? to_xe_device(drm) : NULL; >> } >> >> static inline struct xe_device *xe_device_const_cast(const struct xe_device *xe) >>diff --git a/drivers/gpu/drm/xe/xe_pci.c b/drivers/gpu/drm/xe/xe_pci.c >>index 7bb811b4a057..f861b8cf931a 100644 >>--- a/drivers/gpu/drm/xe/xe_pci.c >>+++ b/drivers/gpu/drm/xe/xe_pci.c >>@@ -800,7 +800,7 @@ static int xe_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent) >> if (IS_ERR(xe)) >> return PTR_ERR(xe); >> >>- pci_set_drvdata(pdev, xe); >>+ pci_set_drvdata(pdev, &xe->drm); >> >> xe_pm_assert_unbounded_bridge(xe); >> subplatform_desc = find_subplatform(xe, desc); >>-- >>2.39.2 >> -- Jani Nikula, Intel