<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Windows-1252">
</head>
<body>
<div>Hey, just a ping on my comments/question bellow.</div>
<div><br>
</div>
<div>Andrey</div>
<hr style="display:inline-block;width:98%" tabindex="-1">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>From:</b> Grodzovsky, Andrey <Andrey.Grodzovsky@amd.com><br>
<b>Sent:</b> 25 November 2020 12:39<br>
<b>To:</b> Daniel Vetter <daniel@ffwll.ch><br>
<b>Cc:</b> amd-gfx list <amd-gfx@lists.freedesktop.org>; dri-devel <dri-devel@lists.freedesktop.org>; Christian König <ckoenig.leichtzumerken@gmail.com>; Rob Herring <robh@kernel.org>; Lucas Stach <l.stach@pengutronix.de>; Qiang Yu <yuq825@gmail.com>; Anholt,
 Eric <eric@anholt.net>; Pekka Paalanen <ppaalanen@gmail.com>; Deucher, Alexander <Alexander.Deucher@amd.com>; Greg KH <gregkh@linuxfoundation.org>; Wentland, Harry <Harry.Wentland@amd.com><br>
<b>Subject:</b> Re: [PATCH v3 10/12] drm/amdgpu: Avoid sysfs dirs removal post device unplug</font>
<div> </div>
</div>
<div style="background-color:#FFFFFF">
<p><br>
</p>
<div class="x_moz-cite-prefix">On 11/25/20 4:04 AM, Daniel Vetter wrote:<br>
</div>
<blockquote type="cite">
<pre class="x_moz-quote-pre">On Tue, Nov 24, 2020 at 11:27 PM Andrey Grodzovsky
<a class="x_moz-txt-link-rfc2396E" href="mailto:Andrey.Grodzovsky@amd.com"><Andrey.Grodzovsky@amd.com></a> wrote:
</pre>
<blockquote type="cite">
<pre class="x_moz-quote-pre">

On 11/24/20 9:49 AM, Daniel Vetter wrote:
</pre>
<blockquote type="cite">
<pre class="x_moz-quote-pre">On Sat, Nov 21, 2020 at 12:21:20AM -0500, Andrey Grodzovsky wrote:
</pre>
<blockquote type="cite">
<pre class="x_moz-quote-pre">Avoids NULL ptr due to kobj->sd being unset on device removal.

Signed-off-by: Andrey Grodzovsky <a class="x_moz-txt-link-rfc2396E" href="mailto:andrey.grodzovsky@amd.com"><andrey.grodzovsky@amd.com></a>
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c   | 4 +++-
  drivers/gpu/drm/amd/amdgpu/amdgpu_ucode.c | 4 +++-
  2 files changed, 6 insertions(+), 2 deletions(-)

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c
index caf828a..812e592 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c
@@ -27,6 +27,7 @@
  #include <linux/uaccess.h>
  #include <linux/reboot.h>
  #include <linux/syscalls.h>
+#include <drm/drm_drv.h>

  #include "amdgpu.h"
  #include "amdgpu_ras.h"
@@ -1043,7 +1044,8 @@ static int amdgpu_ras_sysfs_remove_feature_node(struct amdgpu_device *adev)
             .attrs = attrs,
     };

-    sysfs_remove_group(&adev->dev->kobj, &group);
+    if (!drm_dev_is_unplugged(&adev->ddev))
+            sysfs_remove_group(&adev->dev->kobj, &group);
</pre>
</blockquote>
<pre class="x_moz-quote-pre">This looks wrong. sysfs, like any other interface, should be
unconditionally thrown out when we do the drm_dev_unregister. Whether
hotunplugged or not should matter at all. Either this isn't needed at all,
or something is wrong with the ordering here. But definitely fishy.
-Daniel
</pre>
</blockquote>
<pre class="x_moz-quote-pre">

So technically this is needed because kobejct's sysfs directory entry kobj->sd
is set to NULL
on device removal (from sysfs_remove_dir) but because we don't finalize the device
until last reference to drm file is dropped (which can happen later) we end up
calling sysfs_remove_file/dir after
this pointer is NULL. sysfs_remove_file checks for NULL and aborts while
sysfs_remove_dir
is not and that why I guard against calls to sysfs_remove_dir.
But indeed the whole approach in the driver is incorrect, as Greg pointed out -
we should use
default groups attributes instead of explicit calls to sysfs interface and this
would save those troubles.
But again. the issue here of scope of work, converting all of amdgpu to default
groups attributes is somewhat
lengthy process with extra testing as the entire driver is papered with sysfs
references and seems to me more of a standalone
cleanup, just like switching to devm_ and drmm_ work. To me at least it seems
that it makes more sense
to finalize and push the hot unplug patches so that this new functionality can
be part of the driver sooner
and then incrementally improve it by working on those other topics. Just as
devm_/drmm_ I also added sysfs cleanup
to my TODO list in the RFC patch.
</pre>
</blockquote>
<pre class="x_moz-quote-pre">
Hm, whether you solve this with the default group stuff to
auto-remove, or remove explicitly at the right time doesn't matter
much. The underlying problem you have here is that it's done way too
late.</pre>
</blockquote>
<p>As far as I understood correctly the default group attrs by reading this<br>
article by Greg - <a class="x_moz-txt-link-freetext" href="https://www.linux.com/news/how-create-sysfs-file-correctly/">
https://www.linux.com/news/how-create-sysfs-file-correctly/</a><br>
it will be removed together with the device and not too late like now and I quote<br>
from the last paragraph there:</p>
<p>"<span style="color:rgb(34,34,34); font-family:Verdana,Geneva,sans-serif; font-size:15px; font-style:normal; font-variant-ligatures:normal; font-variant-caps:normal; font-weight:400; letter-spacing:normal; orphans:2; text-align:start; text-indent:0px; text-transform:none; white-space:normal; widows:2; word-spacing:0px; background-color:rgb(255,255,255); text-decoration-style:initial; text-decoration-color:initial; display:inline!important; float:none">By
 setting this value, you don’t have to do anything in your</span><br style="box-sizing:border-box; color:rgb(34,34,34); font-family:Verdana,Geneva,sans-serif; font-size:15px; font-style:normal; font-variant-ligatures:normal; font-variant-caps:normal; font-weight:400; letter-spacing:normal; orphans:2; text-align:start; text-indent:0px; text-transform:none; white-space:normal; widows:2; word-spacing:0px; background-color:rgb(255,255,255); text-decoration-style:initial; text-decoration-color:initial">
<tt style="box-sizing:border-box; color:rgb(34,34,34); font-size:15px; font-style:normal; font-variant-ligatures:normal; font-variant-caps:normal; font-weight:400; letter-spacing:normal; orphans:2; text-align:start; text-indent:0px; text-transform:none; white-space:normal; widows:2; word-spacing:0px; background-color:rgb(255,255,255); text-decoration-style:initial; text-decoration-color:initial">probe()</tt><span style="color:rgb(34,34,34); font-family:Verdana,Geneva,sans-serif; font-size:15px; font-style:normal; font-variant-ligatures:normal; font-variant-caps:normal; font-weight:400; letter-spacing:normal; orphans:2; text-align:start; text-indent:0px; text-transform:none; white-space:normal; widows:2; word-spacing:0px; background-color:rgb(255,255,255); text-decoration-style:initial; text-decoration-color:initial; display:inline!important; float:none"> or </span><tt style="box-sizing:border-box; color:rgb(34,34,34); font-size:15px; font-style:normal; font-variant-ligatures:normal; font-variant-caps:normal; font-weight:400; letter-spacing:normal; orphans:2; text-align:start; text-indent:0px; text-transform:none; white-space:normal; widows:2; word-spacing:0px; background-color:rgb(255,255,255); text-decoration-style:initial; text-decoration-color:initial">release()</tt><span style="color:rgb(34,34,34); font-family:Verdana,Geneva,sans-serif; font-size:15px; font-style:normal; font-variant-ligatures:normal; font-variant-caps:normal; font-weight:400; letter-spacing:normal; orphans:2; text-align:start; text-indent:0px; text-transform:none; white-space:normal; widows:2; word-spacing:0px; background-color:rgb(255,255,255); text-decoration-style:initial; text-decoration-color:initial; display:inline!important; float:none"> functions
 at all in order for the</span><br style="box-sizing:border-box; color:rgb(34,34,34); font-family:Verdana,Geneva,sans-serif; font-size:15px; font-style:normal; font-variant-ligatures:normal; font-variant-caps:normal; font-weight:400; letter-spacing:normal; orphans:2; text-align:start; text-indent:0px; text-transform:none; white-space:normal; widows:2; word-spacing:0px; background-color:rgb(255,255,255); text-decoration-style:initial; text-decoration-color:initial">
<tt style="box-sizing:border-box; color:rgb(34,34,34); font-size:15px; font-style:normal; font-variant-ligatures:normal; font-variant-caps:normal; font-weight:400; letter-spacing:normal; orphans:2; text-align:start; text-indent:0px; text-transform:none; white-space:normal; widows:2; word-spacing:0px; background-color:rgb(255,255,255); text-decoration-style:initial; text-decoration-color:initial">sysfs</tt><span style="color:rgb(34,34,34); font-family:Verdana,Geneva,sans-serif; font-size:15px; font-style:normal; font-variant-ligatures:normal; font-variant-caps:normal; font-weight:400; letter-spacing:normal; orphans:2; text-align:start; text-indent:0px; text-transform:none; white-space:normal; widows:2; word-spacing:0px; background-color:rgb(255,255,255); text-decoration-style:initial; text-decoration-color:initial; display:inline!important; float:none"> files
 to be properly created and destroyed whenever your</span><br style="box-sizing:border-box; color:rgb(34,34,34); font-family:Verdana,Geneva,sans-serif; font-size:15px; font-style:normal; font-variant-ligatures:normal; font-variant-caps:normal; font-weight:400; letter-spacing:normal; orphans:2; text-align:start; text-indent:0px; text-transform:none; white-space:normal; widows:2; word-spacing:0px; background-color:rgb(255,255,255); text-decoration-style:initial; text-decoration-color:initial">
<span style="color:rgb(34,34,34); font-family:Verdana,Geneva,sans-serif; font-size:15px; font-style:normal; font-variant-ligatures:normal; font-variant-caps:normal; font-weight:400; letter-spacing:normal; orphans:2; text-align:start; text-indent:0px; text-transform:none; white-space:normal; widows:2; word-spacing:0px; background-color:rgb(255,255,255); text-decoration-style:initial; text-decoration-color:initial; display:inline!important; float:none">device
 is added or removed from the system. And you will, most</span><br style="box-sizing:border-box; color:rgb(34,34,34); font-family:Verdana,Geneva,sans-serif; font-size:15px; font-style:normal; font-variant-ligatures:normal; font-variant-caps:normal; font-weight:400; letter-spacing:normal; orphans:2; text-align:start; text-indent:0px; text-transform:none; white-space:normal; widows:2; word-spacing:0px; background-color:rgb(255,255,255); text-decoration-style:initial; text-decoration-color:initial">
<span style="color:rgb(34,34,34); font-family:Verdana,Geneva,sans-serif; font-size:15px; font-style:normal; font-variant-ligatures:normal; font-variant-caps:normal; font-weight:400; letter-spacing:normal; orphans:2; text-align:start; text-indent:0px; text-transform:none; white-space:normal; widows:2; word-spacing:0px; background-color:rgb(255,255,255); text-decoration-style:initial; text-decoration-color:initial; display:inline!important; float:none">importantly,
 do it in a race-free manner, which is always a good thing."</span></p>
<p><span style="color:rgb(34,34,34); font-size:15px; font-style:normal; font-variant-ligatures:normal; font-variant-caps:normal; font-weight:400; letter-spacing:normal; text-align:start; text-indent:0px; text-transform:none; white-space:normal; word-spacing:0px; background-color:rgb(255,255,255); text-decoration-style:initial; text-decoration-color:initial; display:inline!important; float:none">To
 me this seems like the best solution to the late remove issue. What do<br>
you think ?</span></p>
<p><span style="color:rgb(34,34,34); font-family:Verdana,Geneva,sans-serif; font-size:15px; font-style:normal; font-variant-ligatures:normal; font-variant-caps:normal; font-weight:400; letter-spacing:normal; orphans:2; text-align:start; text-indent:0px; text-transform:none; white-space:normal; widows:2; word-spacing:0px; background-color:rgb(255,255,255); text-decoration-style:initial; text-decoration-color:initial; display:inline!important; float:none"><br>
</span></p>
<blockquote type="cite">
<pre class="x_moz-quote-pre"> sysfs removal (like all uapi interfaces) need to be removed as
part of drm_dev_unregister.</pre>
</blockquote>
<p><br>
</p>
<p>Do you mean we need to trace and aggregate all sysfs files creation within<br>
the low level drivers and then call some sysfs release function inside drm_dev_unregister<br>
to iterate and release them all ?</p>
<p><br>
</p>
<blockquote type="cite">
<pre class="x_moz-quote-pre"> I guess aside from the split into fini_hw
and fini_sw, you also need an unregister_late callback (like we have
already for drm_connector, so that e.g. backlight and similar stuff
can be unregistered).</pre>
</blockquote>
<p><br>
</p>
<p>Is this the callback you suggest to call from within drm_dev_unregister and<br>
it will be responsible to release all sysfs files created within the driver ?</p>
<p>Andrey</p>
<p><br>
</p>
<blockquote type="cite">
<pre class="x_moz-quote-pre">

Papering over the underlying bug like this doesn't really fix much,
the lifetimes are still wrong.
-Daniel

</pre>
<blockquote type="cite">
<pre class="x_moz-quote-pre">
Andrey


</pre>
<blockquote type="cite">
<pre class="x_moz-quote-pre">
</pre>
<blockquote type="cite">
<pre class="x_moz-quote-pre">
     return 0;
  }
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ucode.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_ucode.c
index 2b7c90b..54331fc 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ucode.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ucode.c
@@ -24,6 +24,7 @@
  #include <linux/firmware.h>
  #include <linux/slab.h>
  #include <linux/module.h>
+#include <drm/drm_drv.h>

  #include "amdgpu.h"
  #include "amdgpu_ucode.h"
@@ -464,7 +465,8 @@ int amdgpu_ucode_sysfs_init(struct amdgpu_device *adev)

  void amdgpu_ucode_sysfs_fini(struct amdgpu_device *adev)
  {
-    sysfs_remove_group(&adev->dev->kobj, &fw_attr_group);
+    if (!drm_dev_is_unplugged(&adev->ddev))
+            sysfs_remove_group(&adev->dev->kobj, &fw_attr_group);
  }

  static int amdgpu_ucode_init_single_fw(struct amdgpu_device *adev,
--
2.7.4

</pre>
</blockquote>
</blockquote>
</blockquote>
<pre class="x_moz-quote-pre">


</pre>
</blockquote>
</div>
</body>
</html>