Skip to contentSkip to conversation

WIP: drm/dp_mst: Add support for dumping topology ref histories from debugfs

PATCH Lyude Paul 2 participants 11 messages Jan 12, 2022 – May 6, 2022 10 messages not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): CO6PR12MB54896F2BBF20F6C04D187D1AFC679@CO6PR12MB5489.namprd12.prod.outlook.com

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): CO6PR12MB5489E282DA251241843EF4E4FC6F9@CO6PR12MB5489.namprd12.prod.outlook.com

A message that is not in the archive (sent off-list or not yet received): CO6PR12MB5489D4C51655B1D8B817A50FFC7B9@CO6PR12MB5489.namprd12.prod.outlook.com

Lyude Paul

author
#5 lore raw

(CC'ing this to dri-devel, since this is basically patch review)

Alright - so first off sorry about the broken debugging patch! I thought I had tested that but I guess I hadn't tested it well enough, I'll get it fixed asap, but feel free to try getting to it before I do.

As for this patch… (comments below)

Quoted 44 lines · Wayne

[AMD Official Use Only]

Hi Lyude,

No rush and thanks for your time! : )
Just want to help clarify what I mean that "registering a callback function" for CSN of disconnecting
stream sink device (not mst branch case). Attach below the tentative patch that I planned to do. Thanks so much!

Regards,
Wayne
---
 .../display/amdgpu_dm/amdgpu_dm_mst_types.c   | 53 +++++++++++++++++++ drivers/gpu/drm/drm_dp_mst_topology.c         | 16 +++++- include/drm/drm_dp_mst_helper.h               |  1 +

 3 files changed, 68 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_mst_types.c +16 -0
index cc34a35d0bcb..d7343c64908c 100644
@@ -472,8 +472,61 @@ dm_dp_add_mst_connector(struct drm_dp_mst_topology_mgr
 *mgr,
         return connector;
  }
 
+void dm_dp_notify_csn_disconnection(struct drm_connector *connector)
+{
+       struct amdgpu_dm_connector *aconnector =
+               to_amdgpu_dm_connector(connector);
+       struct dc_sink *dc_sink = aconnector->dc_sink;
+       struct dc_link *dc_link = aconnector->dc_link;
+       struct amdgpu_device *adev = drm_to_adev(ddev);
+
+       ASSERT(dc_link);
+
+       if (dc_sink) {
+               mutex_lock(&adev->dm.dc_lock);
+
+               /*clear the remote sink of the link*/
+               dc_link_remove_remote_sink(dc_link, dc_sink);
+               dc_sink_release(dc_sink);

drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_mst_types.c dm_dp_add_mst_connector(struct drm_dp_mst_topology_mgr · line 501

Quoted 10 lines
drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_mst_types.c +7 -0
+               aconnector->dc_sink = NULL;
+
+               mutex_unlock(&adev->dm.dc_lock);
+       }
+}
+
  static const struct drm_dp_mst_topology_cbs dm_mst_cbs = {
         .add_connector = dm_dp_add_mst_connector,
+       .notify_csn_disconnection = dm_dp_notify_csn_disconnection,
  };

I still don't really think this is a good idea. This seems like it's just adding another hotplugging path to the code in order to avoid sending hotplugs for non-endpoint devices. In addition to the drm_connector issues I mentioned before, we also really need to stop doing any kind of payload maintence in hotplugging paths. The reality is any kind of payload maintanence we do outside of normal modesetting paths is a relic from legacy modesetting that I'm dropping ASAP (more on this below), and we can't keep adding to it because it dramatically complicates maintanence as well.

Sorry for repeating this point so often but - the biggest issue too is I'm still not sure what it is we're even avoiding here. We know resources aren't released consistently, and that we're able to reproduce the behavior with repeated hotplugs. We also know that if we skip sending certain hotplug events, that fixes the issue. And we know we can workaround it by adding a special case for forcing a payload release in DC. But none of those actually tell us exactly what piece of code is leaking and why, which means that any workarounds we're putting in to avoid this mysterious guilty section of code we don't entirely understand either - which means we're just adding more code in that no one actually fully understands. This just ends up making maintence difficult because every change in code nearby workarounds like this has to strugle to try to figure out said workarounds in order to avoid breaking things.

I'm actually currently running into these "later" issues right now, as recently I've (-actively-, finally!!!) been working on trying to remove as much non-atomic MST as I can because. As it turns out - a huge amount of the payload maintanence code just isn't needed at all when we stop caring about non-atomic drivers and stick everything in atomic state structs. Step 1 for updating updating the payload table, e.g. drm_dp_update_payload_part1(), is a great example of how messy things have become. Here's a small sample of some of the stuff I've seen from just that one function so far that either just don't make sense here or is totally redundant. I should note that a lot of these things also come from patches I reviwed, but didn't really look at as closely as I should have because I was swamped at work, some are historical artifacts, and others are less-than-ideal patches I got wrote myself when I was first started working on MST and didn't know the code as well as I do now:

* We try to avoid some sort of userspace issue by using drm_dp_mst_port_downstream_of_branch() to avoid releasing payloads for a branch if we can't prove it's downstream of the top of the topology. This seems to workaround either a userspace bug. This is a redundant, since that's what topology refs are already supposed to be doing to the extent is reasonably possibly. It's also unfortunately racy solution because we have to be able to handle the situation where a connector is removed from under us. That can happen at any time, including _immediately_ after we call drm_dp_mst_port_downstream_of_branch() - rendering the call not really useful.
* If we fail to validate the sink in drm_dp_update_payload_part, we don't update the payload table. I think at best this solution is racy and not useful, at worst it leaves us with a payload table that doesn't match what we attempted to set in the atomic state - which at worst brings us into undefined territory where we're just plain out of sync with the reality in hw.
* Actually fun fact - mgr->payloads and mgr->proposed_vcpis both can and definitely should be removed entirely. All of the info for mgr->payloads could just be in the atomic state, because that + the magic of atomic state duplication means we'll also have an accurate view of the previous state's payload allocations: which renders mgr->proposed_vcpis redundant.

Apologies for the long explanation again, but I hope that explains my point here a bit. I'm going to be trying to get to moving amdgpu's DSC code out of amdgpu and into DRM helpers as well soon, so I'm really determined to clean stuff up beforehand as every time I've done so it's become substantially easier to make changes to this code. Things used to be even worse before I started cleaning things up 2 or 3 years ago, where simple changes would end up getting me stuck spending hours trying to dig through lockdep or memory manegement issues. As well, I would be entirely unsurprised if bugs like this very payload leak we're working on just disappear once we've gotten rid of all the extraneous workarounds and state tracking here - especially with how many special cases we have for maintaining the payload table right now. That's certainly ended up being the case in the past with a number of other difficult to track down issues I've dealt with in MST.

Anyhow. I've been way more productive this year then last because I had over a month off work and am finally not super burnt out from my job, and so far I've been making progress on the payload state cleanup far faster then I was last year :). I think if you can't figure out where the leak is coming from even once I get the debugging patches I mentioned fixed up, it might be a good idea for us to try again after I've got some of this code cleaned up. I've got a currently WIP branch here:

https://gitlab.freedesktop.org/lyudess/linux/-/commit/624dd68fa804e64b5b2060e4735d7317090692b5

Quoted 786 lines
drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_mst_types.c +0 -0
  void amdgpu_dm_initialize_dp_connector(struct amdgpu_display_manager *dm,
drivers/gpu/drm/drm_dp_mst_topology.c +1 -0
index 857c5d15e81d..a70e26c5d084 100644
@@ -2508,6 +2508,7 @@ drm_dp_mst_handle_conn_stat(struct drm_dp_mst_branch
 *mstb,
         u8 new_pdt;
         bool new_mcs;
         bool dowork = false, create_connector = false;
+       bool notify_unplug_event = false;
 
         port = drm_dp_get_port(mstb, conn_stat->port_number);

        if (!port)

drivers/gpu/drm/drm_dp_mst_topology.c +2 -0
@@ -2520,6 +2521,9 @@ drm_dp_mst_handle_conn_stat(struct drm_dp_mst_branch
 *mstb,
                          * port, so just throw the port out and make sure we
                          * reprobe the link address of it's parent MSTB
                          */
+                       /* should also consider notify_unplug_event here.
+                        * but it's not a normal case for products in the

market
+                        */ drm_dp_mst_topology_unlink_port(mgr, port); mstb->link_address_sent = false; dowork = true;

drivers/gpu/drm/drm_dp_mst_topology.c +6 -2
@@ -2541,10 +2545,14 @@ drm_dp_mst_handle_conn_stat(struct drm_dp_mst_branch
 *mstb,
         port->ddps = conn_stat->displayport_device_plug_status;
 
         if (old_ddps != port->ddps) {
-               if (port->ddps && !port->input)
+               if (port->ddps && !port->input) {
                         drm_dp_send_enum_path_resources(mgr, mstb, port);
-               else
+               } else {
                         port->full_pbn = 0;
+                       if (port->connector &&
+                               drm_dp_mst_is_end_device(port->pdt, port-
 >mcs))
+                               notify_unplug_event = true;
+               }
         }

        new_pdt = port->input ? DP_PEER_DEVICE_NONE : conn_stat-

Quoted 1 line

peer_device_type;

drivers/gpu/drm/drm_dp_mst_topology.c +4 -0
@@ -2557,11 +2565,15 @@ drm_dp_mst_handle_conn_stat(struct drm_dp_mst_branch
 *mstb,
                 dowork = false;
         }
 
+       if (notify_unplug_event && mgr->cbs->notify_csn_disconnection)
+               mgr->cbs->notify_csn_disconnection(port->connector);
+
         if (port->connector)
                 drm_modeset_unlock(&mgr->base.lock);
         else if (create_connector)
                 drm_dp_mst_port_add_connector(mstb, port);
 
+

 out:
        drm_dp_mst_topology_put_port(port);
        if (dowork)

include/drm/drm_dp_mst_helper.h +1 -1
index 78044ac5b59b..ff9e47729841 100644
@@ -525,6 +525,7 @@ struct drm_dp_mst_topology_cbs {
          * IRQ pulse handler.
          */
         void (*poll_hpd_irq)(struct drm_dp_mst_topology_mgr *mgr);
+       void (*notify_csn_disconnection)(struct drm_connector *connector);
  };
 
  #define DP_MAX_PAYLOAD (sizeof(unsigned long) * 8)
--

2.31.0

Quoted 704 lines

-----Original Message-----
From: Lin, Wayne
Sent: Wednesday, December 8, 2021 11:39 AM
To: 'Lyude Paul' <[email protected]>
Subject: RE: [PATCH] WIP: drm/dp_mst: Add support for dumping topology ref histories from debugfs

No worries Lyude!
Thanks for keeping helping on this. Take your time : )

Quoted 694 lines

-----Original Message-----
From: Lyude Paul <[email protected]>
Sent: Wednesday, December 8, 2021 7:05 AM
To: Lin, Wayne <[email protected]>
Subject: Re: [PATCH] WIP: drm/dp_mst: Add support for dumping topology ref histories from debugfs

Sorry! I will try to get to this tomorrow, if not then sometime this week.

Quoted 674 lines · Wayne

[Public]

Hi Lyude,

Finally have some bandwidth to get back to this problem! I roughly went through this patch and I'm just aware that we already have such kind of convenient tool for a while.
I think it's definitely useful for us to track port/mstb reference count issues and I'll start to embrace this feature for cleaning up those issues. Thank you Lyude!

However, I think the issue that I was trying to fix is not related to what you suggested:
" The idea here is that if stream resources aren't being released, my guess would be that we're not dropping topology references for the port which means the connector never goes away."
The issue I was trying to fix is about releasing dc_link->remote_sinks while receiving a CSN message notifying the connection status of a sst connector of a port changed from connected to disconnected. Not the connection status changed of a mst branch device.
e.g.
src - 1st_mstb - 2nd_mstb - sst_monitor => src - 1st_mstb (disconnect) 2nd_mstb - sst_monitor

In above case, after receiving CSN, we will put topology references of 2nd mstb and its port which is connected with the sst monitor. As the result of that, we can call drm_dp_delayed_destroy_port() to unregister and put the drm connector.

However, in below case:
e.g.
src - 1st_mstb - sst_monitor => src - 1st_mstb (disconnect) sst_monitor

In this case, which is the case having problem, it definitely won't decrease the topology references count of the port which was connected to the sst monitor to zero since the port is still existing in the topology. Same as the malloc reference since the port can't get destroyed. Hence, the port still exists  and we won't call
drm_dp_delayed_destroy_port() to unregister and put the drm connector. I looked up the code and drm_dp_delayed_destroy_port() seems like the only place to call drm_connector_put() which means we can't put reference count of drm connector under this case and can't release dc_sink resource by destroying drm connector.

I would also like to point out that this resource (remote_sinks) is specific to different stream sinks. So if we're trying to release this dc_sink resource by destroying the drm connector, it conflicts the idea that you suggested before that we should always keep the drm connector until it's no longer reachable in the topology. Releasing dc_sink should be binding with the disconnection event.

I understand your concern that we should not just easily change the logic here since it's the result after solving tons of bugs before and might cause other side effect. So, just my 2 cents, what I'm thinking is to register a callback function for our driver to notify us that the remote sink is detached. This just aligns our flow handling long HPD event of legacy (sst) DP.
For sst case, once we detect long HPD event indicating the monitor is detached, we will immediately try to release the dc_link->local_sink and fire hotplug event to upper layer. Same as here, once receives a CSN message notifying a drm connector is changed from connected to disconnected, trigger the callback function and we can try to release the dc_sink resource.

Would like to know your thought and insight please : )

Btw, I got some errors and warnings while building and have some code adjustments as below : ) Thank you Lyude for your always kindly help!!

Regards,
Wayne

Quoted 353 lines

-----Original Message-----
From: Lyude Paul <[email protected]>
Sent: Wednesday, November 3, 2021 7:15 AM
To: Lin, Wayne <[email protected]>
Subject: [PATCH] WIP: drm/dp_mst: Add support for dumping topology ref histories from debugfs

TODO:
* Implement support for i915
* Finish writing this commit message
* ???
* profit

---

Hey wayne! SO-hopefully if I did this correctly then this should just work on amdgpu. What this patch should do is add a debugfs file to amdgpu called "amdgpu_dp_mst_topology_refs", and when you read the file it should print out the topology reference history of every MSTB and Port in memory, along with how many times we've hit the codepath in each backtrace. An example:

Port DP-5 (0000000005c37748) topology ref history:
  1 gets (last at    58.468973):

     drm_dp_send_link_address+0x6a5/0xa00 [drm_kms_helper]
     drm_dp_check_and_send_link_address+0xad/0xd0 [drm_kms_helper]
     drm_dp_mst_link_probe_work+0x14e/0x1a0 [drm_kms_helper]
     process_one_work+0x1e3/0x390
     worker_thread+0x50/0x3a0
     kthread+0x124/0x150
     ret_from_fork+0x1f/0x30
  1 puts (last at    58.469357):
     drm_dp_mst_topology_put_port+0x6a/0x210 [drm_kms_helper]
     drm_dp_send_link_address+0x39e/0xa00 [drm_kms_helper]
     drm_dp_check_and_send_link_address+0xad/0xd0 [drm_kms_helper]
     drm_dp_mst_link_probe_work+0x14e/0x1a0 [drm_kms_helper]
     process_one_work+0x1e3/0x390
     worker_thread+0x50/0x3a0
     kthread+0x124/0x150
     ret_from_fork+0x1f/0x30

The idea here is that if stream resources aren't being released, my guess would be that we're not dropping topology references for the port which means the connector never goes away. So, if that's really the case then once we unplug the offending connector we should be able to find a pair of gets/puts for the offending leaked connector where the get/put count doesn't match up. Also, if the frame count on the backtrace isn't long enough you can increase the value of STACK_DEPTH in drivers/gpu/drm/drm_dp_mst_topology.c and recompile to get more frames.

To enable this, first enable CONFIG_EXPERT for your kernel which will unhide CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS. Then just enable CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS, recompile, and it should be good to go.

Let me know if this works for you, and hopefully this should tell us exactly what the problem actually is here.

 .../amd/display/amdgpu_dm/amdgpu_dm_debugfs.c |  35 ++++ drivers/gpu/drm/drm_dp_mst_topology.c         | 173
++++++++++++++----
 drivers/gpu/drm/nouveau/nouveau_debugfs.c     |  35 ++++ include/drm/drm_dp_mst_helper.h               |  18 ++

 4 files changed, 228 insertions(+), 33 deletions(-)

diff --git a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_debugfs.c b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_debugfs.c index 1a68a674913c..1a14732c52b4 100644

drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_debugfs.c +19 -0
@@ -3063,6 +3063,37 @@ static int mst_topo_show(struct seq_file
 *m, void
 *unused)
       return 0;
  }
 
+#ifdef CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS
+static int mst_topology_ref_dump_show(struct seq_file *m, void
+*unused) {
+     struct amdgpu_device *adev = (struct amdgpu_device
+*)m->private;
+     struct drm_device *dev = adev_to_drm(adev);
+     struct drm_connector *connector;
+     struct drm_connector_list_iter conn_iter;
+     struct amdgpu_dm_connector *aconnector;
+
+     drm_connector_list_iter_begin(dev, &conn_iter);
+     drm_for_each_connector_iter(connector, &conn_iter) {
+             if (connector->connector_type !=
 DRM_MODE_CONNECTOR_DisplayPort)
+                     continue;
+
+             aconnector = to_amdgpu_dm_connector(connector);
+
+             /* Ensure we're only dumping the topology of a root
+mst node

*/
+             if (!aconnector->mst_mgr.max_payloads)
+                     continue;
+
+             seq_printf(m, "\nMST topology for connector %d\n", aconnector->connector_id);
+             drm_dp_mst_dump_topology_refs(m, +&aconnector->mst_mgr);
+     }
+     drm_connector_list_iter_end(&conn_iter);

drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_debugfs.c +7 -0
+
+     return 0;
+}
+
+DEFINE_SHOW_ATTRIBUTE(mst_topology_ref_dump);
+#endif
+

 /*
  * Sets trigger hpd for MST topologies.
  * All connected connectors will be rediscovered and re started as needed if val of 1 is sent.

drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_debugfs.c +4 -0
@@ -3299,6 +3330,10 @@ void dtn_debugfs_init(struct amdgpu_device
 *adev)
 
       debugfs_create_file("amdgpu_mst_topology", 0444, root,
                           adev, &mst_topo_fops);
+#ifdef CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS
+     debugfs_create_file("amdgpu_dp_mst_topology_refs", 0444,
+root, adev,
+                         &mst_topology_ref_dump_fops); #endif
       debugfs_create_file("amdgpu_dm_dtn_log", 0644, root, adev,
                           &dtn_log_fops);
drivers/gpu/drm/drm_dp_mst_topology.c +0 -1
index 1aa8702383d4..0159828c494d 100644
@@ -1366,23 +1366,6 @@ static int drm_dp_mst_wait_tx_reply(struct
 drm_dp_mst_branch *mstb,
       return ret;
  }
 
-static struct drm_dp_mst_branch *drm_dp_add_mst_branch_device(u8

lct, u8
*rad) -{
-     struct drm_dp_mst_branch *mstb;

drivers/gpu/drm/drm_dp_mst_topology.c +0 -14
-
-     mstb = kzalloc(sizeof(*mstb), GFP_KERNEL);
-     if (!mstb)
-             return NULL;
-
-     mstb->lct = lct;
-     if (lct > 1)
-             memcpy(mstb->rad, rad, lct / 2);
-     INIT_LIST_HEAD(&mstb->ports);
-     kref_init(&mstb->topology_kref);
-     kref_init(&mstb->malloc_kref);
-     return mstb;
-}
-

 static void drm_dp_free_mst_branch_device(struct kref *kref)  { struct drm_dp_mst_branch *mstb = @@ -1642,12 +1625,20 @@ topology_ref_type_to_str(enum drm_dp_mst_topology_ref_type type) return "put";
 }

drivers/gpu/drm/drm_dp_mst_topology.c +8 -0
+static const char *topology_ref_history_type_to_str(enum
+drm_dp_mst_topology_history_type type) {
+     if (type == DRM_DP_MST_TOPOLOGY_HISTORY_PORT)
+             return "Port";
+     else
+             return "MSTB";
+}
+

 static void
-__dump_topology_ref_history(struct drm_dp_mst_topology_ref_history *history,
-                         void *ptr, const char *type_str)

drivers/gpu/drm/drm_dp_mst_topology.c +2 -0
+dump_topology_ref_history(struct drm_dp_mst_topology_ref_history
+*history, struct drm_printer p)

 {
-     struct drm_printer p = drm_debug_printer(DBG_PREFIX); +     char *connector_name = NULL;
      char *buf = kzalloc(PAGE_SIZE, GFP_KERNEL); +     void *ptr;
      int i;

      if (!buf)

drivers/gpu/drm/drm_dp_mst_topology.c +2 -0
@@ -1656,14 +1647,29 @@ __dump_topology_ref_history(struct
 drm_dp_mst_topology_ref_history *history,
       if (!history->len)
               goto out;
 
+     /* Get a pointer to the actual MSTB/port so we can the
+memory

address to the kernel log */
+     if (history->type == DRM_DP_MST_TOPOLOGY_HISTORY_PORT) +             ptr = container_of(history, struct drm_dp_mst_port, topology_ref_history);
+     else
+             ptr = container_of(history, struct

drivers/gpu/drm/drm_dp_mst_topology.c +2 -0
+drm_dp_mst_branch, topology_ref_history);
+

      /* First, sort the list so that it goes from oldest to newest
       * reference entry
       */
      sort(history->entries, history->len, sizeof(*history->entries), topology_ref_history_cmp, NULL);

-     drm_printf(&p, "%s (%p) topology count reached 0, dumping history:\n",
-                type_str, ptr);
+     if (history->type == DRM_DP_MST_TOPOLOGY_HISTORY_PORT) { +             struct drm_dp_mst_port *port = ptr;

drivers/gpu/drm/drm_dp_mst_topology.c +7 -0
+
+             if (port->connector)
+                     connector_name = port->connector->name;
+     }
+     if (connector_name)
+             drm_printf(&p, "Port %s (%p) topology ref
+history:\n",

connector_name, ptr);
+     else
+             drm_printf(&p, "%s (%p) topology ref history:\n",

drivers/gpu/drm/drm_dp_mst_topology.c +2 -0
+
+topology_ref_history_type_to_str(history->type),

ptr);

      for (i = 0; i < history->len; i++) {
              const struct drm_dp_mst_topology_ref_entry *entry = @@
-
1682,24 +1688,92 @@ __dump_topology_ref_history(struct drm_dp_mst_topology_ref_history *history,
                         ts_nsec, rem_nsec / 1000, buf); }

-     /* Now free the history, since this is the only time we expose it */
-     kfree(history->entries);
 out:
      kfree(buf);
 }

drivers/gpu/drm/drm_dp_mst_topology.c +31 -0
+/**
+ * drm_dp_mst_dump_topology_refs - helper function for dumping
+the topology ref history
+ * @m: File to print to
+ * @mgr: &struct drm_dp_mst_topology_mgr to use
+ *
+ * Prints the topology ref history of all ports and MSTBs on @mgr
+that are still in memory,
+ * regardless of whether they're actually still reachable through
+the topology or not. Only enabled
+ * when %CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS is enabled. Can be
+implemented by drivers to assist
+ * with debugging leaks in the DP MST helpers.
+ */
+void drm_dp_mst_dump_topology_refs(struct seq_file *m, struct
+drm_dp_mst_topology_mgr *mgr) {
+     struct drm_dp_mst_topology_ref_history *history;
+     struct drm_printer p = drm_seq_file_printer(m);
+
+     mutex_lock(&mgr->topology_ref_history_lock);
+     list_for_each_entry(history,
+&mgr->topology_ref_history_list,
+node)
+             dump_topology_ref_history(history, p);
+     mutex_unlock(&mgr->topology_ref_history_lock);
+}
+EXPORT_SYMBOL(drm_dp_mst_dump_topology_refs);
+
+static void
+__init_topology_ref_history(struct
+drm_dp_mst_topology_ref_history

*history,
+                         struct drm_dp_mst_topology_mgr *mgr, +                         enum drm_dp_mst_topology_history_type

drivers/gpu/drm/drm_dp_mst_topology.c +12 -0
+type) {
+     history->type = type;
+     INIT_LIST_HEAD(&history->node);
+
+     mutex_lock(&mgr->topology_ref_history_lock);
+     list_add(&history->node, &mgr->topology_ref_history_list);
+     mutex_unlock(&mgr->topology_ref_history_lock);
+}
+
+static void
+__destroy_topology_ref_history(struct
+drm_dp_mst_topology_ref_history

*history,
+                            struct drm_dp_mst_topology_mgr *mgr)

drivers/gpu/drm/drm_dp_mst_topology.c +36 -0
+{
+     mutex_lock(&mgr->topology_ref_history_lock);
+     list_del(&mgr->topology_ref_history_list);
+     mutex_unlock(&mgr->topology_ref_history_lock);
+
+     kfree(history->entries);
+}
+
+static __always_inline void
+init_port_topology_history(struct drm_dp_mst_topology_mgr *mgr,
+struct drm_dp_mst_port *port) {
+     __init_topology_ref_history(&port->topology_ref_history,
+mgr,
+
+DRM_DP_MST_TOPOLOGY_HISTORY_PORT);
+}
+
+static __always_inline void
+init_mstb_topology_history(struct drm_dp_mst_topology_mgr *mgr,
+struct drm_dp_mst_branch *mstb) {
+     __init_topology_ref_history(&mstb->topology_ref_history,
+mgr,
+
+DRM_DP_MST_TOPOLOGY_HISTORY_MSTB);
+}
+
+static __always_inline void
+destroy_port_topology_history(struct drm_dp_mst_port *port) {
+     __destroy_topology_ref_history(&port->topology_ref_history,
+port->mgr); }
+
+static __always_inline void
+destroy_mstb_topology_history(struct drm_dp_mst_branch *mstb) {
+     __destroy_topology_ref_history(&mstb->topology_ref_history,
+mstb->mgr); }
+

 static __always_inline void dump_mstb_topology_history(struct drm_dp_mst_branch *mstb)  { -     __dump_topology_ref_history(&mstb->topology_ref_history, mstb,
-                                 "MSTB");
+     dump_topology_ref_history(&mstb->topology_ref_history, +drm_debug_printer(DBG_PREFIX));
 }

 static __always_inline void dump_port_topology_history(struct drm_dp_mst_port *port)  { -     __dump_topology_ref_history(&port->topology_ref_history, port,
-                                 "Port");
+     dump_topology_ref_history(&port->topology_ref_history, +drm_debug_printer(DBG_PREFIX));
 }

 static __always_inline void

drivers/gpu/drm/drm_dp_mst_topology.c +0 -0
@@ -1729,6 +1803,14 @@ topology_ref_history_unlock(struct

drm_dp_mst_topology_mgr *mgr)  }  #else  static inline void

drivers/gpu/drm/drm_dp_mst_topology.c

Quoted 2 lines
drivers/gpu/drm/drm_dp_mst_topology.c +2 -0
+init_port_topology_history(struct drm_dp_mst_topology_mgr *mgr,
+struct drm_dp_mst_port *port); static inline void

Should also add the bracket, otherwise will get warnings. => static inline void init_port_topology_history(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port) {};

drivers/gpu/drm/drm_dp_mst_topology.c

Quoted 2 lines
drivers/gpu/drm/drm_dp_mst_topology.c +2 -0
+init_mstb_topology_history(struct drm_dp_mst_topology_mgr *mgr,
+struct drm_dp_mst_branch *mstb); static inline void

Same as above
=> static inline void init_mstb_topology_history(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_branch *mstb) {};

drivers/gpu/drm/drm_dp_mst_topology.c

Quoted 2 lines
drivers/gpu/drm/drm_dp_mst_topology.c +2 -0
+destroy_port_topology_history(struct drm_dp_mst_topology_mgr
+*mgr, struct drm_dp_mst_port *port); static inline void

destroy_port_topology_history() takes one parameter only which is port.
=> destroy_port_topology_history(struct drm_dp_mst_port *port) {};

drivers/gpu/drm/drm_dp_mst_topology.c

Quoted 2 lines
drivers/gpu/drm/drm_dp_mst_topology.c +2 -0
+destroy_mstb_topology_history(struct drm_dp_mst_topology_mgr
+*mgr, struct drm_dp_mst_branch *mstb); static inline void

destroy_mstb_topology_history() takes one parameter only which is mstb => destroy_mstb_topology_history(struct drm_dp_mst_branch *mstb) {};

Quoted 222 lines

 topology_ref_history_lock(struct drm_dp_mst_topology_mgr *mgr) {} static inline void  topology_ref_history_unlock(struct drm_dp_mst_topology_mgr *mgr) {} @@ -1740,6 +1822,25 @@ dump_port_topology_history(struct drm_dp_mst_port *port) {} #define save_port_topology_ref(port, type)  #endif

drivers/gpu/drm/drm_dp_mst_topology.c +19 -0
+static struct drm_dp_mst_branch *
+drm_dp_add_mst_branch_device(struct drm_dp_mst_topology_mgr *mgr,
+u8 lct, u8 *rad) {
+     struct drm_dp_mst_branch *mstb;
+
+     mstb = kzalloc(sizeof(*mstb), GFP_KERNEL);
+     if (!mstb)
+             return NULL;
+
+     mstb->lct = lct;
+     if (lct > 1)
+             memcpy(mstb->rad, rad, lct / 2);
+     INIT_LIST_HEAD(&mstb->ports);
+     kref_init(&mstb->topology_kref);
+     kref_init(&mstb->malloc_kref);
+     init_mstb_topology_history(mgr, mstb);
+     return mstb;
+}
+

 static void drm_dp_destroy_mst_branch_device(struct kref *kref) {
      struct drm_dp_mst_branch *mstb = @@ -1747,6 +1848,7 @@ static void drm_dp_destroy_mst_branch_device(struct kref *kref)
      struct drm_dp_mst_topology_mgr *mgr = mstb->mgr;

      dump_mstb_topology_history(mstb);
+     destroy_mstb_topology_history(mstb);

      INIT_LIST_HEAD(&mstb->destroy_next);

drivers/gpu/drm/drm_dp_mst_topology.c +1 -0
@@ -1856,6 +1958,7 @@ static void drm_dp_destroy_port(struct kref
 *kref)
       struct drm_dp_mst_topology_mgr *mgr = port->mgr;
 
       dump_port_topology_history(port);
+     destroy_port_topology_history(port);
 
       /* There's nothing that needs locking to destroy an input

port yet */
      if (port->input) {

drivers/gpu/drm/drm_dp_mst_topology.c +2 -1
@@ -2135,7 +2238,7 @@ drm_dp_port_set_pdt(struct drm_dp_mst_port
 *port, u8 new_pdt,
                       ret = drm_dp_mst_register_i2c_bus(port);
               } else {
                       lct = drm_dp_calculate_rad(port, rad);
-                     mstb = drm_dp_add_mst_branch_device(lct,
 rad);
+                     mstb = drm_dp_add_mst_branch_device(mgr,
+lct, rad);

                      if (!mstb) {
                              ret = -ENOMEM; drm_err(mgr->dev, "Failed to create MSTB for port %p", port); @@ -2366,6 +2469,8 @@ drm_dp_mst_add_port(struct drm_device *dev,
       */
      drm_dp_mst_get_mstb_malloc(mstb);

+     init_port_topology_history(mgr, port);
+
      return port;
 }

drivers/gpu/drm/drm_dp_mst_topology.c +0 -0
@@ -3745,7 +3850,7 @@ int drm_dp_mst_topology_mgr_set_mst(struct

drm_dp_mst_topology_mgr *mgr, bool ms
              }

              /* add initial branch device at LCT 1 */ -             mstb = drm_dp_add_mst_branch_device(1, NULL); +             mstb = drm_dp_add_mst_branch_device(mgr, 1, NULL); if (mstb == NULL) {
                      ret = -ENOMEM;
                      goto out_unlock; @@ -5512,14 +5617,16 @@ int drm_dp_mst_topology_mgr_init(struct drm_dp_mst_topology_mgr *mgr, mutex_init(&mgr->delayed_destroy_lock); mutex_init(&mgr->up_req_lock); mutex_init(&mgr->probe_lock); -#if IS_ENABLED(CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS)
-     mutex_init(&mgr->topology_ref_history_lock); -#endif
      INIT_LIST_HEAD(&mgr->tx_msg_downq); INIT_LIST_HEAD(&mgr->destroy_port_list); INIT_LIST_HEAD(&mgr->destroy_branch_device_list); INIT_LIST_HEAD(&mgr->up_req_list);

drivers/gpu/drm/drm_dp_mst_topology.c +5 -0
+#if IS_ENABLED(CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS)
+     mutex_init(&mgr->topology_ref_history_lock);
+     INIT_LIST_HEAD(&mgr->topology_ref_history_list);
+#endif
+

      /*
       * delayed_destroy_work will be queued on a dedicated WQ, so that any
       * requeuing will be also flushed when deiniting the topology manager.

drivers/gpu/drm/nouveau/nouveau_debugfs.c +3 -0
index 1cbe01048b93..53ec7c1daada 100644
@@ -29,9 +29,13 @@
   */
 
  #include <linux/debugfs.h>
+#include <drm/drm_dp_mst_helper.h> #include <drm/drm_encoder.h>
  #include <nvif/class.h>
  #include <nvif/if0001.h>
+#include <nouveau_encoder.h>
  #include "nouveau_debugfs.h"
+#include "nouveau_display.h"
  #include "nouveau_drv.h"
 

 static int

drivers/gpu/drm/nouveau/nouveau_debugfs.c +0 -0
@@ -68,6 +72,34 @@ nouveau_debugfs_strap_peek(struct seq_file *m,

void
*data)
      return 0;
 }

drivers/gpu/drm/nouveau/nouveau_debugfs.c +28 -0
+#ifdef CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS
+static int nouveau_debugfs_mst_topology_history(struct seq_file
+*m, void *data) {
+     struct drm_info_node *node = m->private;
+     struct drm_device *dev = node->minor->dev;
+     struct drm_encoder *encoder;
+     struct nouveau_display *disp = nouveau_display(dev);
+
+     if (disp->disp.object.oclass < GF110_DISP)
+             return -EINVAL;
+
+     drm_for_each_encoder(encoder, dev) {
+             struct nv50_mstm *mstm;
+
+             /* We need the real encoder for each MST mgr */
+             if (encoder->encoder_type != DRM_MODE_ENCODER_TMDS)
+                     continue;
+             mstm = nouveau_encoder(encoder)->dp.mstm;
+             if (!mstm)
+                     continue;
+
+             seq_printf(m, "MSTM on %s:\n", encoder->name);
+             drm_dp_mst_dump_topology_refs(m, &mstm->mgr);
+     }
+     return 0;
+}
+#endif
+

 static int
 nouveau_debugfs_pstate_get(struct seq_file *m, void *data)  { @@ -213,6
+245,9 @@ static const struct file_operations nouveau_pstate_fops = {  static struct drm_info_list nouveau_debugfs_list[] = {
      { "vbios.rom",  nouveau_debugfs_vbios_image, 0, NULL }, { "strap_peek", nouveau_debugfs_strap_peek, 0, NULL },

drivers/gpu/drm/nouveau/nouveau_debugfs.c +3 -0
+#ifdef CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS
+     { "dp_mst_topology_refs",
+nouveau_debugfs_mst_topology_history, 0, NULL }, #endif

 };
 #define NOUVEAU_DEBUGFS_ENTRIES ARRAY_SIZE(nouveau_debugfs_list)

(quoted diff) +0 -0

b/include/drm/drm_dp_mst_helper.h index 78044ac5b59b..e3a73d8d97c0 100644

include/drm/drm_dp_mst_helper.h +4 -0
@@ -35,6 +35,11 @@ enum drm_dp_mst_topology_ref_type {
       DRM_DP_MST_TOPOLOGY_REF_PUT,
  };
 
+enum drm_dp_mst_topology_history_type {
+     DRM_DP_MST_TOPOLOGY_HISTORY_PORT,
+     DRM_DP_MST_TOPOLOGY_HISTORY_MSTB, };
+
  struct drm_dp_mst_topology_ref_history {
       struct drm_dp_mst_topology_ref_entry {

              enum drm_dp_mst_topology_ref_type type; @@ -43,6 +48,9 @@ struct drm_dp_mst_topology_ref_history { depot_stack_handle_t backtrace;
      } *entries;
      int len;
+
+     enum drm_dp_mst_topology_history_type type;
+     struct list_head node;
 };
 #endif /* IS_ENABLED(CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS) */

include/drm/drm_dp_mst_helper.h +4 -0
@@ -769,6 +777,12 @@ struct drm_dp_mst_topology_mgr {
        * &drm_dp_mst_branch.topology_ref_history.
        */
       struct mutex topology_ref_history_lock;
+
+     /**
+      * @topology_ref_history_list: contains a list of topology
+ref

histories for any MSTBs or
+      * ports that are still in memory
+      */
+     struct list_head topology_ref_history_list;
 #endif
 };

include/drm/drm_dp_mst_helper.h +0 -0
@@ -873,6 +887,10 @@ void drm_dp_mst_put_port_malloc(struct
 drm_dp_mst_port *port);
 

 struct drm_dp_aux *drm_dp_mst_dsc_aux_for_port(struct drm_dp_mst_port *port);

include/drm/drm_dp_mst_helper.h +4 -0
+#ifdef CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS
+void drm_dp_mst_dump_topology_refs(struct seq_file *m, struct
+drm_dp_mst_topology_mgr *mgr); #endif
+

 extern const struct drm_private_state_funcs
drm_dp_mst_topology_state_funcs;

Quoted 4 lines
Quoted 3 lines

 /**
--
2.31.1

--
Cheers,
 Lyude Paul (she/her)
 Software Engineer at Red Hat

Signature · 1 line
Cheers, Lyude Paul (she/her) Software Engineer at Red Hat
author
#6 lore raw

Hey - so, the original issue here was that payloads were not always deleted when we expected them to be - correct?

If I'm remembering that correctly, then (and I'm not 100% sure on this yet) I might already have noticed something very possibly wonky in how we handle payload allocations currently, that I found while working on the non-atomic removal branch that I linked to you in my previous response. Basically, in the step1() function it looks like that we follow this general flow with updating payloads:

* Loop through proposed payloads and compare to previously programmed payloads
- If a payload has a different allocation then it previously did, update the payload
- If the payload is new, create it
- If a payload no longer has an allocation, remove the payload

At first glance this seems totally correct - but I'm not actually entirely convinced it is? Particularly because it seems like the order in which we do creation/deletion of payloads is totally arbitrary. To explain what I mean by that, consider a state transition like this:

vcpi_slots=15 vcpi_slots=35 vcpi_slots=14
| 1 | 2 |xxxxxxxx|

Let's say we want to increase payload #1 from 15 to 50, and disable payload #2 in the same atomic commit on DRM's side. If the order we update payloads is entirely arbitrary, we could end up doing this:

* Increase VCPI slots payload #1 from 15->50 (total VCPI slots=99, overflow!)
* Decrease VCPI slots payload #2 from 35->0  (total VCPI slots=50)

Notice on the first step, we've technically overflowed the available number of VCPI slots in the payload table. This is still before step 2 though, and I could be totally wrong here - perhaps this is entirely OK in the real world, and we're allowed to overflow VC slots as long as we repair the issue before step 2. But so far I can't seem to find anything in the DP 2.0 spec that explicitly states this would be OK - which makes me think we might fail some payload allocations if we don't always ensure we avoid overflows in between VC payload table changes. Note that avoiding overflows would be as simple as just making sure we send all VC payload table changes that free VC slots before sending any that take new slots.

Again - I haven't actually confirmed this yet and am hoping to test stuff like this very soon as I'm pretty close finishing up the initial attempt at removing the non-atomic MST code in the DRM helpers now. If my theory ends up being correct here, I can fix this in my rewrite of the MST payload management code. But I figured it might be worth mentioning in the mean time in case you think it might be relevant to the problem here :).

Quoted 966 lines · Lyude Paul

(CC'ing this to dri-devel, since this is basically patch review)

Alright - so first off sorry about the broken debugging patch! I thought I had
tested that but I guess I hadn't tested it well enough, I'll get it fixed asap, but feel free to try getting to it before I do.

As for this patch… (comments below)

Quoted 52 lines · Wayne

[AMD Official Use Only]

Hi Lyude,

No rush and thanks for your time! : )
Just want to help clarify what I mean that "registering a callback function"
for CSN of disconnecting
stream sink device (not mst branch case). Attach below the tentative patch that I planned to do. Thanks so much!

Regards,
Wayne
---
 .../display/amdgpu_dm/amdgpu_dm_mst_types.c   | 53 +++++++++++++++++++ drivers/gpu/drm/drm_dp_mst_topology.c         | 16 +++++- include/drm/drm_dp_mst_helper.h               |  1 +

 3 files changed, 68 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_mst_types.c +0 -0
index cc34a35d0bcb..d7343c64908c 100644
@@ -472,8 +472,61 @@ dm_dp_add_mst_connector(struct

drm_dp_mst_topology_mgr
*mgr,
        return connector;
 }

drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_mst_types.c +22 -0
+void dm_dp_notify_csn_disconnection(struct drm_connector *connector)
+{
+       struct amdgpu_dm_connector *aconnector =
+               to_amdgpu_dm_connector(connector);
+       struct dc_sink *dc_sink = aconnector->dc_sink;
+       struct dc_link *dc_link = aconnector->dc_link;
+       struct amdgpu_device *adev = drm_to_adev(ddev);
+
+       ASSERT(dc_link);
+
+       if (dc_sink) {
+               mutex_lock(&adev->dm.dc_lock);
+
+               /*clear the remote sink of the link*/
+               dc_link_remove_remote_sink(dc_link, dc_sink);
+               dc_sink_release(dc_sink);
+               aconnector->dc_sink = NULL;
+
+               mutex_unlock(&adev->dm.dc_lock);
+       }
+}
+
Quoted 4 lines

 static const struct drm_dp_mst_topology_cbs dm_mst_cbs = { .add_connector = dm_dp_add_mst_connector,
+       .notify_csn_disconnection = dm_dp_notify_csn_disconnection, };

I still don't really think this is a good idea. This seems like it's just adding another hotplugging path to the code in order to avoid sending hotplugs
for non-endpoint devices. In addition to the drm_connector issues I mentioned
before, we also really need to stop doing any kind of payload maintence in hotplugging paths. The reality is any kind of payload maintanence we do outside of normal modesetting paths is a relic from legacy modesetting that I'm dropping ASAP (more on this below), and we can't keep adding to it because
it dramatically complicates maintanence as well.

Sorry for repeating this point so often but - the biggest issue too is I'm still not sure what it is we're even avoiding here. We know resources aren't released consistently, and that we're able to reproduce the behavior with repeated hotplugs. We also know that if we skip sending certain hotplug events, that fixes the issue. And we know we can workaround it by adding a special case for forcing a payload release in DC. But none of those actually tell us exactly what piece of code is leaking and why, which means that any workarounds we're putting in to avoid this mysterious guilty section of code we don't entirely understand either - which means we're just adding more code
in that no one actually fully understands. This just ends up making maintence
difficult because every change in code nearby workarounds like this has to strugle to try to figure out said workarounds in order to avoid breaking things.

I'm actually currently running into these "later" issues right now, as recently I've (-actively-, finally!!!) been working on trying to remove as much non-atomic MST as I can because. As it turns out - a huge amount of the payload maintanence code just isn't needed at all when we stop caring about non-atomic drivers and stick everything in atomic state structs. Step 1 for updating updating the payload table, e.g. drm_dp_update_payload_part1(), is a
great example of how messy things have become. Here's a small sample of some of the stuff I've seen from just that one function so far that either just don't make sense here or is totally redundant. I should note that a lot of these things also come from patches I reviwed, but didn't really look at as closely as I should have because I was swamped at work, some are historical artifacts, and others are less-than-ideal patches I got wrote myself when I was first started working on MST and didn't know the code as well as I do now:

 * We try to avoid some sort of userspace issue by using drm_dp_mst_port_downstream_of_branch() to avoid releasing payloads for a branch if we can't prove it's downstream of the top of the topology. This seems to workaround either a userspace bug. This is a redundant, since that's what topology refs are already supposed to be doing to the extent is
   reasonably possibly. It's also unfortunately racy solution because we have
   to be able to handle the situation where a connector is removed from under
   us. That can happen at any time, including _immediately_ after we call drm_dp_mst_port_downstream_of_branch() - rendering the call not really useful.
 * If we fail to validate the sink in drm_dp_update_payload_part, we don't update the payload table. I think at best this solution is racy and not useful, at worst it leaves us with a payload table that doesn't match what
   we attempted to set in the atomic state - which at worst brings us into undefined territory where we're just plain out of sync with the reality in
   hw.
 * Actually fun fact - mgr->payloads and mgr->proposed_vcpis both can and definitely should be removed entirely. All of the info for mgr->payloads could just be in the atomic state, because that + the magic of atomic state
   duplication means we'll also have an accurate view of the previous state's
   payload allocations: which renders mgr->proposed_vcpis redundant.

Apologies for the long explanation again, but I hope that explains my point here a bit. I'm going to be trying to get to moving amdgpu's DSC code out of amdgpu and into DRM helpers as well soon, so I'm really determined to clean stuff up beforehand as every time I've done so it's become substantially easier to make changes to this code. Things used to be even worse before I started cleaning things up 2 or 3 years ago, where simple changes would end up
getting me stuck spending hours trying to dig through lockdep or memory manegement issues. As well, I would be entirely unsurprised if bugs like this
very payload leak we're working on just disappear once we've gotten rid of all
the extraneous workarounds and state tracking here - especially with how many
special cases we have for maintaining the payload table right now. That's certainly ended up being the case in the past with a number of other difficult
to track down issues I've dealt with in MST.

Anyhow. I've been way more productive this year then last because I had over a
month off work and am finally not super burnt out from my job, and so far I've
been making progress on the payload state cleanup far faster then I was last year :). I think if you can't figure out where the leak is coming from even once I get the debugging patches I mentioned fixed up, it might be a good idea
for us to try again after I've got some of this code cleaned up. I've got a currently WIP branch here:

https://gitlab.freedesktop.org/lyudess/linux/-/commit/624dd68fa804e64b5b2060e4735d7317090692b5

Quoted 793 lines

 void amdgpu_dm_initialize_dp_connector(struct amdgpu_display_manager *dm,

drivers/gpu/drm/drm_dp_mst_topology.c +1 -0
index 857c5d15e81d..a70e26c5d084 100644
@@ -2508,6 +2508,7 @@ drm_dp_mst_handle_conn_stat(struct drm_dp_mst_branch
 *mstb,
         u8 new_pdt;
         bool new_mcs;
         bool dowork = false, create_connector = false;
+       bool notify_unplug_event = false;
 
         port = drm_dp_get_port(mstb, conn_stat->port_number);

        if (!port)

drivers/gpu/drm/drm_dp_mst_topology.c +0 -0
@@ -2520,6 +2521,9 @@ drm_dp_mst_handle_conn_stat(struct drm_dp_mst_branch
 *mstb,
                          * port, so just throw the port out and make sure

we
                         * reprobe the link address of it's parent MSTB */
+                       /* should also consider notify_unplug_event here. +                        * but it's not a normal case for products in the market
+                        */ drm_dp_mst_topology_unlink_port(mgr, port); mstb->link_address_sent = false; dowork = true;

drivers/gpu/drm/drm_dp_mst_topology.c +0 -0
@@ -2541,10 +2545,14 @@ drm_dp_mst_handle_conn_stat(struct

drm_dp_mst_branch
*mstb,
        port->ddps = conn_stat->displayport_device_plug_status;

        if (old_ddps != port->ddps) {
-               if (port->ddps && !port->input)
+               if (port->ddps && !port->input) { drm_dp_send_enum_path_resources(mgr, mstb, port); -               else
+               } else {
                        port->full_pbn = 0;
+                       if (port->connector &&
+                               drm_dp_mst_is_end_device(port->pdt, port-

Quoted 1 line

mcs))

+                               notify_unplug_event = true; +               }
        }

        new_pdt = port->input ? DP_PEER_DEVICE_NONE : conn_stat-

Quoted 1 line

peer_device_type;

drivers/gpu/drm/drm_dp_mst_topology.c +0 -0
@@ -2557,11 +2565,15 @@ drm_dp_mst_handle_conn_stat(struct

drm_dp_mst_branch
*mstb,
                dowork = false;
        }

+       if (notify_unplug_event && mgr->cbs->notify_csn_disconnection) +               mgr->cbs->notify_csn_disconnection(port->connector); +
        if (port->connector) drm_modeset_unlock(&mgr->base.lock);
        else if (create_connector) drm_dp_mst_port_add_connector(mstb, port);

+
 out:
        drm_dp_mst_topology_put_port(port);
        if (dowork)

include/drm/drm_dp_mst_helper.h +1 -1
index 78044ac5b59b..ff9e47729841 100644
@@ -525,6 +525,7 @@ struct drm_dp_mst_topology_cbs {
          * IRQ pulse handler.
          */
         void (*poll_hpd_irq)(struct drm_dp_mst_topology_mgr *mgr);
+       void (*notify_csn_disconnection)(struct drm_connector *connector);
  };
 
  #define DP_MAX_PAYLOAD (sizeof(unsigned long) * 8)
--

2.31.0

Quoted 708 lines

-----Original Message-----
From: Lin, Wayne
Sent: Wednesday, December 8, 2021 11:39 AM
To: 'Lyude Paul' <[email protected]>
Subject: RE: [PATCH] WIP: drm/dp_mst: Add support for dumping topology ref
histories from debugfs

No worries Lyude!
Thanks for keeping helping on this. Take your time : )

Quoted 697 lines

-----Original Message-----
From: Lyude Paul <[email protected]>
Sent: Wednesday, December 8, 2021 7:05 AM
To: Lin, Wayne <[email protected]>
Subject: Re: [PATCH] WIP: drm/dp_mst: Add support for dumping topology ref histories from debugfs

Sorry! I will try to get to this tomorrow, if not then sometime this week.

Quoted 677 lines · Wayne

[Public]

Hi Lyude,

Finally have some bandwidth to get back to this problem! I roughly went through this patch and I'm just aware that we already have such kind of convenient tool for a while.
I think it's definitely useful for us to track port/mstb reference count issues and I'll start to embrace this feature for cleaning up those issues. Thank you Lyude!

However, I think the issue that I was trying to fix is not related to what you suggested:
" The idea here is that if stream resources aren't being released, my guess would be that we're not dropping topology references for the port which means the connector never goes away." The issue I was trying to fix is about releasing dc_link->remote_sinks while receiving a CSN message notifying the connection status of a sst connector of a port changed from connected to disconnected. Not the connection status changed of a mst
branch device.
e.g.
src - 1st_mstb - 2nd_mstb - sst_monitor => src - 1st_mstb (disconnect) 2nd_mstb - sst_monitor

In above case, after receiving CSN, we will put topology references of 2nd mstb and its port which is connected with the sst monitor. As the result of that, we can call drm_dp_delayed_destroy_port() to unregister and put the drm connector.

However, in below case:
e.g.
src - 1st_mstb - sst_monitor => src - 1st_mstb (disconnect) sst_monitor

In this case, which is the case having problem, it definitely won't decrease the topology references count of the port which was connected to the sst monitor to zero since the port is still existing in the topology. Same as the malloc reference since the port can't get destroyed. Hence, the port still exists  and we won't call
drm_dp_delayed_destroy_port() to unregister and put the drm connector.
I looked up the code and drm_dp_delayed_destroy_port() seems like the only place to call drm_connector_put() which means we can't put reference count of drm connector under this case and can't release dc_sink resource by destroying drm connector.

I would also like to point out that this resource (remote_sinks) is specific to different stream sinks. So if we're trying to release this dc_sink resource by destroying the drm connector, it conflicts the idea that you suggested before that we should always keep the drm connector until it's no longer reachable in the topology. Releasing dc_sink should be binding with the disconnection event.

I understand your concern that we should not just easily change the logic here since it's the result after solving tons of bugs before and might cause other side effect. So, just my 2 cents, what I'm thinking is to register a callback function for our driver to notify us that the remote sink is detached. This just aligns our flow handling long HPD event of legacy (sst) DP.
For sst case, once we detect long HPD event indicating the monitor is detached, we will immediately try to release the dc_link->local_sink and fire hotplug event to upper layer. Same as here, once receives a CSN message notifying a drm connector is changed from connected to disconnected, trigger the callback function and we can try to release the dc_sink resource.

Would like to know your thought and insight please : )

Btw, I got some errors and warnings while building and have some code adjustments as below : ) Thank you Lyude for your always kindly help!!

Regards,
Wayne

Quoted 353 lines

-----Original Message-----
From: Lyude Paul <[email protected]>
Sent: Wednesday, November 3, 2021 7:15 AM
To: Lin, Wayne <[email protected]>
Subject: [PATCH] WIP: drm/dp_mst: Add support for dumping topology ref histories from debugfs

TODO:
* Implement support for i915
* Finish writing this commit message
* ???
* profit

---

Hey wayne! SO-hopefully if I did this correctly then this should just work on amdgpu. What this patch should do is add a debugfs file to amdgpu called "amdgpu_dp_mst_topology_refs", and when you read the file it should print out the topology reference history of every MSTB and Port in memory, along with how many times we've hit the codepath in each backtrace. An example:

Port DP-5 (0000000005c37748) topology ref history:
  1 gets (last at    58.468973):

     drm_dp_send_link_address+0x6a5/0xa00 [drm_kms_helper]
     drm_dp_check_and_send_link_address+0xad/0xd0 [drm_kms_helper]
     drm_dp_mst_link_probe_work+0x14e/0x1a0 [drm_kms_helper]
     process_one_work+0x1e3/0x390
     worker_thread+0x50/0x3a0
     kthread+0x124/0x150
     ret_from_fork+0x1f/0x30
  1 puts (last at    58.469357):
     drm_dp_mst_topology_put_port+0x6a/0x210 [drm_kms_helper]
     drm_dp_send_link_address+0x39e/0xa00 [drm_kms_helper]
     drm_dp_check_and_send_link_address+0xad/0xd0 [drm_kms_helper]
     drm_dp_mst_link_probe_work+0x14e/0x1a0 [drm_kms_helper]
     process_one_work+0x1e3/0x390
     worker_thread+0x50/0x3a0
     kthread+0x124/0x150
     ret_from_fork+0x1f/0x30

The idea here is that if stream resources aren't being released, my guess would be that we're not dropping topology references for the port which means the connector never goes away. So, if that's really the case then once we unplug the offending connector we should be able to find a pair of gets/puts for the offending leaked connector where the get/put count doesn't match up. Also, if the frame count on the backtrace isn't long enough you can increase the value of STACK_DEPTH in drivers/gpu/drm/drm_dp_mst_topology.c and recompile to get more frames.

To enable this, first enable CONFIG_EXPERT for your kernel which will unhide CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS. Then just enable CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS, recompile, and it should be good to go.

Let me know if this works for you, and hopefully this should tell us exactly what the problem actually is here.

 .../amd/display/amdgpu_dm/amdgpu_dm_debugfs.c |  35 ++++ drivers/gpu/drm/drm_dp_mst_topology.c         | 173
++++++++++++++----
 drivers/gpu/drm/nouveau/nouveau_debugfs.c     |  35 ++++ include/drm/drm_dp_mst_helper.h               |  18 ++

 4 files changed, 228 insertions(+), 33 deletions(-)

diff --git a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_debugfs.c b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_debugfs.c index 1a68a674913c..1a14732c52b4 100644

drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_debugfs.c +19 -0
@@ -3063,6 +3063,37 @@ static int mst_topo_show(struct seq_file
 *m, void
 *unused)
       return 0;
  }
 
+#ifdef CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS
+static int mst_topology_ref_dump_show(struct seq_file *m, void
+*unused) {
+     struct amdgpu_device *adev = (struct amdgpu_device
+*)m->private;
+     struct drm_device *dev = adev_to_drm(adev);
+     struct drm_connector *connector;
+     struct drm_connector_list_iter conn_iter;
+     struct amdgpu_dm_connector *aconnector;
+
+     drm_connector_list_iter_begin(dev, &conn_iter);
+     drm_for_each_connector_iter(connector, &conn_iter) {
+             if (connector->connector_type !=
 DRM_MODE_CONNECTOR_DisplayPort)
+                     continue;
+
+             aconnector = to_amdgpu_dm_connector(connector);
+
+             /* Ensure we're only dumping the topology of a root
+mst node

*/
+             if (!aconnector->mst_mgr.max_payloads)
+                     continue;
+
+             seq_printf(m, "\nMST topology for connector %d\n", aconnector->connector_id);
+             drm_dp_mst_dump_topology_refs(m, +&aconnector->mst_mgr);
+     }
+     drm_connector_list_iter_end(&conn_iter);

drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_debugfs.c +7 -0
+
+     return 0;
+}
+
+DEFINE_SHOW_ATTRIBUTE(mst_topology_ref_dump);
+#endif
+

 /*
  * Sets trigger hpd for MST topologies.
  * All connected connectors will be rediscovered and re started as needed if val of 1 is sent.

drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_debugfs.c +4 -0
@@ -3299,6 +3330,10 @@ void dtn_debugfs_init(struct amdgpu_device
 *adev)
 
       debugfs_create_file("amdgpu_mst_topology", 0444, root,
                           adev, &mst_topo_fops);
+#ifdef CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS
+     debugfs_create_file("amdgpu_dp_mst_topology_refs", 0444,
+root, adev,
+                         &mst_topology_ref_dump_fops); #endif
       debugfs_create_file("amdgpu_dm_dtn_log", 0644, root, adev,
                           &dtn_log_fops);
drivers/gpu/drm/drm_dp_mst_topology.c +0 -1
index 1aa8702383d4..0159828c494d 100644
@@ -1366,23 +1366,6 @@ static int drm_dp_mst_wait_tx_reply(struct
 drm_dp_mst_branch *mstb,
       return ret;
  }
 
-static struct drm_dp_mst_branch *drm_dp_add_mst_branch_device(u8

lct, u8
*rad) -{
-     struct drm_dp_mst_branch *mstb;

drivers/gpu/drm/drm_dp_mst_topology.c +0 -14
-
-     mstb = kzalloc(sizeof(*mstb), GFP_KERNEL);
-     if (!mstb)
-             return NULL;
-
-     mstb->lct = lct;
-     if (lct > 1)
-             memcpy(mstb->rad, rad, lct / 2);
-     INIT_LIST_HEAD(&mstb->ports);
-     kref_init(&mstb->topology_kref);
-     kref_init(&mstb->malloc_kref);
-     return mstb;
-}
-

 static void drm_dp_free_mst_branch_device(struct kref *kref)  { struct drm_dp_mst_branch *mstb = @@ -1642,12 +1625,20 @@ topology_ref_type_to_str(enum drm_dp_mst_topology_ref_type type) return "put";
 }

drivers/gpu/drm/drm_dp_mst_topology.c +8 -0
+static const char *topology_ref_history_type_to_str(enum
+drm_dp_mst_topology_history_type type) {
+     if (type == DRM_DP_MST_TOPOLOGY_HISTORY_PORT)
+             return "Port";
+     else
+             return "MSTB";
+}
+

 static void
-__dump_topology_ref_history(struct drm_dp_mst_topology_ref_history *history,
-                         void *ptr, const char *type_str)

drivers/gpu/drm/drm_dp_mst_topology.c +2 -0
+dump_topology_ref_history(struct drm_dp_mst_topology_ref_history
+*history, struct drm_printer p)

 {
-     struct drm_printer p = drm_debug_printer(DBG_PREFIX); +     char *connector_name = NULL;
      char *buf = kzalloc(PAGE_SIZE, GFP_KERNEL); +     void *ptr;
      int i;

      if (!buf)

drivers/gpu/drm/drm_dp_mst_topology.c +2 -0
@@ -1656,14 +1647,29 @@ __dump_topology_ref_history(struct
 drm_dp_mst_topology_ref_history *history,
       if (!history->len)
               goto out;
 
+     /* Get a pointer to the actual MSTB/port so we can the
+memory

address to the kernel log */
+     if (history->type == DRM_DP_MST_TOPOLOGY_HISTORY_PORT) +             ptr = container_of(history, struct drm_dp_mst_port, topology_ref_history);
+     else
+             ptr = container_of(history, struct

drivers/gpu/drm/drm_dp_mst_topology.c +2 -0
+drm_dp_mst_branch, topology_ref_history);
+

      /* First, sort the list so that it goes from oldest to newest
       * reference entry
       */
      sort(history->entries, history->len, sizeof(*history->entries), topology_ref_history_cmp, NULL);

-     drm_printf(&p, "%s (%p) topology count reached 0, dumping history:\n",
-                type_str, ptr);
+     if (history->type == DRM_DP_MST_TOPOLOGY_HISTORY_PORT) { +             struct drm_dp_mst_port *port = ptr;

drivers/gpu/drm/drm_dp_mst_topology.c +7 -0
+
+             if (port->connector)
+                     connector_name = port->connector->name;
+     }
+     if (connector_name)
+             drm_printf(&p, "Port %s (%p) topology ref
+history:\n",

connector_name, ptr);
+     else
+             drm_printf(&p, "%s (%p) topology ref history:\n",

drivers/gpu/drm/drm_dp_mst_topology.c +2 -0
+
+topology_ref_history_type_to_str(history->type),

ptr);

      for (i = 0; i < history->len; i++) {
              const struct drm_dp_mst_topology_ref_entry *entry = @@
-
1682,24 +1688,92 @@ __dump_topology_ref_history(struct drm_dp_mst_topology_ref_history *history,
                         ts_nsec, rem_nsec / 1000, buf); }

-     /* Now free the history, since this is the only time we expose it */
-     kfree(history->entries);
 out:
      kfree(buf);
 }

drivers/gpu/drm/drm_dp_mst_topology.c +31 -0
+/**
+ * drm_dp_mst_dump_topology_refs - helper function for dumping
+the topology ref history
+ * @m: File to print to
+ * @mgr: &struct drm_dp_mst_topology_mgr to use
+ *
+ * Prints the topology ref history of all ports and MSTBs on @mgr
+that are still in memory,
+ * regardless of whether they're actually still reachable through
+the topology or not. Only enabled
+ * when %CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS is enabled. Can be
+implemented by drivers to assist
+ * with debugging leaks in the DP MST helpers.
+ */
+void drm_dp_mst_dump_topology_refs(struct seq_file *m, struct
+drm_dp_mst_topology_mgr *mgr) {
+     struct drm_dp_mst_topology_ref_history *history;
+     struct drm_printer p = drm_seq_file_printer(m);
+
+     mutex_lock(&mgr->topology_ref_history_lock);
+     list_for_each_entry(history,
+&mgr->topology_ref_history_list,
+node)
+             dump_topology_ref_history(history, p);
+     mutex_unlock(&mgr->topology_ref_history_lock);
+}
+EXPORT_SYMBOL(drm_dp_mst_dump_topology_refs);
+
+static void
+__init_topology_ref_history(struct
+drm_dp_mst_topology_ref_history

*history,
+                         struct drm_dp_mst_topology_mgr *mgr, +                         enum drm_dp_mst_topology_history_type

drivers/gpu/drm/drm_dp_mst_topology.c +12 -0
+type) {
+     history->type = type;
+     INIT_LIST_HEAD(&history->node);
+
+     mutex_lock(&mgr->topology_ref_history_lock);
+     list_add(&history->node, &mgr->topology_ref_history_list);
+     mutex_unlock(&mgr->topology_ref_history_lock);
+}
+
+static void
+__destroy_topology_ref_history(struct
+drm_dp_mst_topology_ref_history

*history,
+                            struct drm_dp_mst_topology_mgr *mgr)

drivers/gpu/drm/drm_dp_mst_topology.c +36 -0
+{
+     mutex_lock(&mgr->topology_ref_history_lock);
+     list_del(&mgr->topology_ref_history_list);
+     mutex_unlock(&mgr->topology_ref_history_lock);
+
+     kfree(history->entries);
+}
+
+static __always_inline void
+init_port_topology_history(struct drm_dp_mst_topology_mgr *mgr,
+struct drm_dp_mst_port *port) {
+     __init_topology_ref_history(&port->topology_ref_history,
+mgr,
+
+DRM_DP_MST_TOPOLOGY_HISTORY_PORT);
+}
+
+static __always_inline void
+init_mstb_topology_history(struct drm_dp_mst_topology_mgr *mgr,
+struct drm_dp_mst_branch *mstb) {
+     __init_topology_ref_history(&mstb->topology_ref_history,
+mgr,
+
+DRM_DP_MST_TOPOLOGY_HISTORY_MSTB);
+}
+
+static __always_inline void
+destroy_port_topology_history(struct drm_dp_mst_port *port) {
+     __destroy_topology_ref_history(&port->topology_ref_history,
+port->mgr); }
+
+static __always_inline void
+destroy_mstb_topology_history(struct drm_dp_mst_branch *mstb) {
+     __destroy_topology_ref_history(&mstb->topology_ref_history,
+mstb->mgr); }
+

 static __always_inline void dump_mstb_topology_history(struct drm_dp_mst_branch *mstb)  { -     __dump_topology_ref_history(&mstb->topology_ref_history, mstb,
-                                 "MSTB");
+     dump_topology_ref_history(&mstb->topology_ref_history, +drm_debug_printer(DBG_PREFIX));
 }

 static __always_inline void dump_port_topology_history(struct drm_dp_mst_port *port)  { -     __dump_topology_ref_history(&port->topology_ref_history, port,
-                                 "Port");
+     dump_topology_ref_history(&port->topology_ref_history, +drm_debug_printer(DBG_PREFIX));
 }

 static __always_inline void

drivers/gpu/drm/drm_dp_mst_topology.c +0 -0
@@ -1729,6 +1803,14 @@ topology_ref_history_unlock(struct

drm_dp_mst_topology_mgr *mgr)  }  #else  static inline void

drivers/gpu/drm/drm_dp_mst_topology.c

Quoted 2 lines
drivers/gpu/drm/drm_dp_mst_topology.c +2 -0
+init_port_topology_history(struct drm_dp_mst_topology_mgr *mgr,
+struct drm_dp_mst_port *port); static inline void

Should also add the bracket, otherwise will get warnings. => static inline void init_port_topology_history(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port) {};

drivers/gpu/drm/drm_dp_mst_topology.c

Quoted 2 lines
drivers/gpu/drm/drm_dp_mst_topology.c +2 -0
+init_mstb_topology_history(struct drm_dp_mst_topology_mgr *mgr,
+struct drm_dp_mst_branch *mstb); static inline void

Same as above
=> static inline void init_mstb_topology_history(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_branch *mstb) {};

drivers/gpu/drm/drm_dp_mst_topology.c

Quoted 2 lines
drivers/gpu/drm/drm_dp_mst_topology.c +2 -0
+destroy_port_topology_history(struct drm_dp_mst_topology_mgr
+*mgr, struct drm_dp_mst_port *port); static inline void

destroy_port_topology_history() takes one parameter only which is port.
=> destroy_port_topology_history(struct drm_dp_mst_port *port) {};

drivers/gpu/drm/drm_dp_mst_topology.c

Quoted 2 lines
drivers/gpu/drm/drm_dp_mst_topology.c +2 -0
+destroy_mstb_topology_history(struct drm_dp_mst_topology_mgr
+*mgr, struct drm_dp_mst_branch *mstb); static inline void

destroy_mstb_topology_history() takes one parameter only which is mstb => destroy_mstb_topology_history(struct drm_dp_mst_branch *mstb) {};

Quoted 222 lines

 topology_ref_history_lock(struct drm_dp_mst_topology_mgr *mgr) {} static inline void  topology_ref_history_unlock(struct drm_dp_mst_topology_mgr *mgr) {} @@ -1740,6 +1822,25 @@ dump_port_topology_history(struct drm_dp_mst_port *port) {} #define save_port_topology_ref(port, type)  #endif

drivers/gpu/drm/drm_dp_mst_topology.c +19 -0
+static struct drm_dp_mst_branch *
+drm_dp_add_mst_branch_device(struct drm_dp_mst_topology_mgr *mgr,
+u8 lct, u8 *rad) {
+     struct drm_dp_mst_branch *mstb;
+
+     mstb = kzalloc(sizeof(*mstb), GFP_KERNEL);
+     if (!mstb)
+             return NULL;
+
+     mstb->lct = lct;
+     if (lct > 1)
+             memcpy(mstb->rad, rad, lct / 2);
+     INIT_LIST_HEAD(&mstb->ports);
+     kref_init(&mstb->topology_kref);
+     kref_init(&mstb->malloc_kref);
+     init_mstb_topology_history(mgr, mstb);
+     return mstb;
+}
+

 static void drm_dp_destroy_mst_branch_device(struct kref *kref) {
      struct drm_dp_mst_branch *mstb = @@ -1747,6 +1848,7 @@ static void drm_dp_destroy_mst_branch_device(struct kref *kref)
      struct drm_dp_mst_topology_mgr *mgr = mstb->mgr;

      dump_mstb_topology_history(mstb);
+     destroy_mstb_topology_history(mstb);

      INIT_LIST_HEAD(&mstb->destroy_next);

drivers/gpu/drm/drm_dp_mst_topology.c +1 -0
@@ -1856,6 +1958,7 @@ static void drm_dp_destroy_port(struct kref
 *kref)
       struct drm_dp_mst_topology_mgr *mgr = port->mgr;
 
       dump_port_topology_history(port);
+     destroy_port_topology_history(port);
 
       /* There's nothing that needs locking to destroy an input

port yet */
      if (port->input) {

drivers/gpu/drm/drm_dp_mst_topology.c +2 -1
@@ -2135,7 +2238,7 @@ drm_dp_port_set_pdt(struct drm_dp_mst_port
 *port, u8 new_pdt,
                       ret = drm_dp_mst_register_i2c_bus(port);
               } else {
                       lct = drm_dp_calculate_rad(port, rad);
-                     mstb = drm_dp_add_mst_branch_device(lct,
 rad);
+                     mstb = drm_dp_add_mst_branch_device(mgr,
+lct, rad);

                      if (!mstb) {
                              ret = -ENOMEM; drm_err(mgr->dev, "Failed to create MSTB for port %p", port); @@ -2366,6 +2469,8 @@ drm_dp_mst_add_port(struct drm_device *dev,
       */
      drm_dp_mst_get_mstb_malloc(mstb);

+     init_port_topology_history(mgr, port);
+
      return port;
 }

drivers/gpu/drm/drm_dp_mst_topology.c +0 -0
@@ -3745,7 +3850,7 @@ int drm_dp_mst_topology_mgr_set_mst(struct

drm_dp_mst_topology_mgr *mgr, bool ms
              }

              /* add initial branch device at LCT 1 */ -             mstb = drm_dp_add_mst_branch_device(1, NULL); +             mstb = drm_dp_add_mst_branch_device(mgr, 1, NULL); if (mstb == NULL) {
                      ret = -ENOMEM;
                      goto out_unlock; @@ -5512,14 +5617,16 @@ int drm_dp_mst_topology_mgr_init(struct drm_dp_mst_topology_mgr *mgr, mutex_init(&mgr->delayed_destroy_lock); mutex_init(&mgr->up_req_lock); mutex_init(&mgr->probe_lock); -#if IS_ENABLED(CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS)
-     mutex_init(&mgr->topology_ref_history_lock); -#endif
      INIT_LIST_HEAD(&mgr->tx_msg_downq); INIT_LIST_HEAD(&mgr->destroy_port_list); INIT_LIST_HEAD(&mgr->destroy_branch_device_list); INIT_LIST_HEAD(&mgr->up_req_list);

drivers/gpu/drm/drm_dp_mst_topology.c +5 -0
+#if IS_ENABLED(CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS)
+     mutex_init(&mgr->topology_ref_history_lock);
+     INIT_LIST_HEAD(&mgr->topology_ref_history_list);
+#endif
+

      /*
       * delayed_destroy_work will be queued on a dedicated WQ, so that any
       * requeuing will be also flushed when deiniting the topology manager.

drivers/gpu/drm/nouveau/nouveau_debugfs.c +3 -0
index 1cbe01048b93..53ec7c1daada 100644
@@ -29,9 +29,13 @@
   */
 
  #include <linux/debugfs.h>
+#include <drm/drm_dp_mst_helper.h> #include <drm/drm_encoder.h>
  #include <nvif/class.h>
  #include <nvif/if0001.h>
+#include <nouveau_encoder.h>
  #include "nouveau_debugfs.h"
+#include "nouveau_display.h"
  #include "nouveau_drv.h"
 

 static int

drivers/gpu/drm/nouveau/nouveau_debugfs.c +0 -0
@@ -68,6 +72,34 @@ nouveau_debugfs_strap_peek(struct seq_file *m,

void
*data)
      return 0;
 }

drivers/gpu/drm/nouveau/nouveau_debugfs.c +28 -0
+#ifdef CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS
+static int nouveau_debugfs_mst_topology_history(struct seq_file
+*m, void *data) {
+     struct drm_info_node *node = m->private;
+     struct drm_device *dev = node->minor->dev;
+     struct drm_encoder *encoder;
+     struct nouveau_display *disp = nouveau_display(dev);
+
+     if (disp->disp.object.oclass < GF110_DISP)
+             return -EINVAL;
+
+     drm_for_each_encoder(encoder, dev) {
+             struct nv50_mstm *mstm;
+
+             /* We need the real encoder for each MST mgr */
+             if (encoder->encoder_type != DRM_MODE_ENCODER_TMDS)
+                     continue;
+             mstm = nouveau_encoder(encoder)->dp.mstm;
+             if (!mstm)
+                     continue;
+
+             seq_printf(m, "MSTM on %s:\n", encoder->name);
+             drm_dp_mst_dump_topology_refs(m, &mstm->mgr);
+     }
+     return 0;
+}
+#endif
+

 static int
 nouveau_debugfs_pstate_get(struct seq_file *m, void *data)  { @@ -213,6
+245,9 @@ static const struct file_operations nouveau_pstate_fops = {  static struct drm_info_list nouveau_debugfs_list[] = {
      { "vbios.rom",  nouveau_debugfs_vbios_image, 0, NULL }, { "strap_peek", nouveau_debugfs_strap_peek, 0, NULL },

drivers/gpu/drm/nouveau/nouveau_debugfs.c +3 -0
+#ifdef CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS
+     { "dp_mst_topology_refs",
+nouveau_debugfs_mst_topology_history, 0, NULL }, #endif

 };
 #define NOUVEAU_DEBUGFS_ENTRIES ARRAY_SIZE(nouveau_debugfs_list)

(quoted diff) +0 -0

b/include/drm/drm_dp_mst_helper.h index 78044ac5b59b..e3a73d8d97c0 100644

include/drm/drm_dp_mst_helper.h +4 -0
@@ -35,6 +35,11 @@ enum drm_dp_mst_topology_ref_type {
       DRM_DP_MST_TOPOLOGY_REF_PUT,
  };
 
+enum drm_dp_mst_topology_history_type {
+     DRM_DP_MST_TOPOLOGY_HISTORY_PORT,
+     DRM_DP_MST_TOPOLOGY_HISTORY_MSTB, };
+
  struct drm_dp_mst_topology_ref_history {
       struct drm_dp_mst_topology_ref_entry {

              enum drm_dp_mst_topology_ref_type type; @@ -43,6 +48,9 @@ struct drm_dp_mst_topology_ref_history { depot_stack_handle_t backtrace;
      } *entries;
      int len;
+
+     enum drm_dp_mst_topology_history_type type;
+     struct list_head node;
 };
 #endif /* IS_ENABLED(CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS) */

include/drm/drm_dp_mst_helper.h +4 -0
@@ -769,6 +777,12 @@ struct drm_dp_mst_topology_mgr {
        * &drm_dp_mst_branch.topology_ref_history.
        */
       struct mutex topology_ref_history_lock;
+
+     /**
+      * @topology_ref_history_list: contains a list of topology
+ref

histories for any MSTBs or
+      * ports that are still in memory
+      */
+     struct list_head topology_ref_history_list;
 #endif
 };

include/drm/drm_dp_mst_helper.h +0 -0
@@ -873,6 +887,10 @@ void drm_dp_mst_put_port_malloc(struct
 drm_dp_mst_port *port);
 

 struct drm_dp_aux *drm_dp_mst_dsc_aux_for_port(struct drm_dp_mst_port *port);

include/drm/drm_dp_mst_helper.h +4 -0
+#ifdef CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS
+void drm_dp_mst_dump_topology_refs(struct seq_file *m, struct
+drm_dp_mst_topology_mgr *mgr); #endif
+

 extern const struct drm_private_state_funcs
drm_dp_mst_topology_state_funcs;

Quoted 4 lines
Quoted 3 lines

 /**
--
2.31.1

--
Cheers,
 Lyude Paul (she/her)
 Software Engineer at Red Hat

Signature · 1 line
Cheers, Lyude Paul (she/her) Software Engineer at Red Hat
author
#7 lore raw
Quoted 21 lines · Lyude Paul

Hey - so, the original issue here was that payloads were not always deleted when we expected them to be - correct?

If I'm remembering that correctly, then (and I'm not 100% sure on this yet) I
might already have noticed something very possibly wonky in how we handle payload allocations currently, that I found while working on the non-atomic removal branch that I linked to you in my previous response. Basically, in the
step1() function it looks like that we follow this general flow with updating
payloads:

 * Loop through proposed payloads and compare to previously programmed payloads
    - If a payload has a different allocation then it previously did, update the
      payload
    - If the payload is new, create it
    - If a payload no longer has an allocation, remove the payload

Quoted 8 lines

At first glance this seems totally correct - but I'm not actually entirely convinced it is? Particularly because it seems like the order in which we do creation/deletion of payloads is totally arbitrary. To explain what I mean by
that, consider a state transition like this:

vcpi_slots=15 vcpi_slots=35 vcpi_slots=14

Quoted 1 line

1 | 2 |xxxxxxxx|

Ugh, sorry - email client messed this up. This was supposed to look like this:

vcpi_slots=15 vcpi_slots=35 vcpi_slots=14
F| 1 | 2 |xxxxxxxxx|

Quoted 1,083 lines

Let's say we want to increase payload #1 from 15 to 50, and disable payload #2
in the same atomic commit on DRM's side. If the order we update payloads is entirely arbitrary, we could end up doing this:

 * Increase VCPI slots payload #1 from 15->50 (total VCPI slots=99, overflow!)
 * Decrease VCPI slots payload #2 from 35->0  (total VCPI slots=50)

Notice on the first step, we've technically overflowed the available number of
VCPI slots in the payload table. This is still before step 2 though, and I could be totally wrong here - perhaps this is entirely OK in the real world, and we're allowed to overflow VC slots as long as we repair the issue before step 2. But so far I can't seem to find anything in the DP 2.0 spec that explicitly states this would be OK - which makes me think we might fail some payload allocations if we don't always ensure we avoid overflows in between VC
payload table changes. Note that avoiding overflows would be as simple as just
making sure we send all VC payload table changes that free VC slots before sending any that take new slots.

Again - I haven't actually confirmed this yet and am hoping to test stuff like
this very soon as I'm pretty close finishing up the initial attempt at removing the non-atomic MST code in the DRM helpers now. If my theory ends up
being correct here, I can fix this in my rewrite of the MST payload management
code. But I figured it might be worth mentioning in the mean time in case you
think it might be relevant to the problem here :).

Quoted 1,046 lines · Lyude Paul

(CC'ing this to dri-devel, since this is basically patch review)

Alright - so first off sorry about the broken debugging patch! I thought I had
tested that but I guess I hadn't tested it well enough, I'll get it fixed asap, but feel free to try getting to it before I do.

As for this patch… (comments below)

Quoted 53 lines · Wayne

[AMD Official Use Only]

Hi Lyude,

No rush and thanks for your time! : )
Just want to help clarify what I mean that "registering a callback function"
for CSN of disconnecting
stream sink device (not mst branch case). Attach below the tentative patch
that I planned to do. Thanks so much!

Regards,
Wayne
---
 .../display/amdgpu_dm/amdgpu_dm_mst_types.c   | 53 +++++++++++++++++++ drivers/gpu/drm/drm_dp_mst_topology.c         | 16 +++++- include/drm/drm_dp_mst_helper.h               |  1 +

 3 files changed, 68 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_mst_types.c +0 -0
index cc34a35d0bcb..d7343c64908c 100644
@@ -472,8 +472,61 @@ dm_dp_add_mst_connector(struct

drm_dp_mst_topology_mgr
*mgr,
        return connector;
 }

drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_mst_types.c +22 -0
+void dm_dp_notify_csn_disconnection(struct drm_connector *connector)
+{
+       struct amdgpu_dm_connector *aconnector =
+               to_amdgpu_dm_connector(connector);
+       struct dc_sink *dc_sink = aconnector->dc_sink;
+       struct dc_link *dc_link = aconnector->dc_link;
+       struct amdgpu_device *adev = drm_to_adev(ddev);
+
+       ASSERT(dc_link);
+
+       if (dc_sink) {
+               mutex_lock(&adev->dm.dc_lock);
+
+               /*clear the remote sink of the link*/
+               dc_link_remove_remote_sink(dc_link, dc_sink);
+               dc_sink_release(dc_sink);
+               aconnector->dc_sink = NULL;
+
+               mutex_unlock(&adev->dm.dc_lock);
+       }
+}
+
Quoted 4 lines

 static const struct drm_dp_mst_topology_cbs dm_mst_cbs = { .add_connector = dm_dp_add_mst_connector,
+       .notify_csn_disconnection = dm_dp_notify_csn_disconnection, };

I still don't really think this is a good idea. This seems like it's just adding another hotplugging path to the code in order to avoid sending hotplugs
for non-endpoint devices. In addition to the drm_connector issues I mentioned
before, we also really need to stop doing any kind of payload maintence in hotplugging paths. The reality is any kind of payload maintanence we do outside of normal modesetting paths is a relic from legacy modesetting that
I'm dropping ASAP (more on this below), and we can't keep adding to it because
it dramatically complicates maintanence as well.

Sorry for repeating this point so often but - the biggest issue too is I'm still not sure what it is we're even avoiding here. We know resources aren't
released consistently, and that we're able to reproduce the behavior with repeated hotplugs. We also know that if we skip sending certain hotplug events, that fixes the issue. And we know we can workaround it by adding a special case for forcing a payload release in DC. But none of those actually
tell us exactly what piece of code is leaking and why, which means that any
workarounds we're putting in to avoid this mysterious guilty section of code
we don't entirely understand either - which means we're just adding more code
in that no one actually fully understands. This just ends up making maintence
difficult because every change in code nearby workarounds like this has to strugle to try to figure out said workarounds in order to avoid breaking things.

I'm actually currently running into these "later" issues right now, as recently I've (-actively-, finally!!!) been working on trying to remove as much non-atomic MST as I can because. As it turns out - a huge amount of the
payload maintanence code just isn't needed at all when we stop caring about
non-atomic drivers and stick everything in atomic state structs. Step 1 for
updating updating the payload table, e.g. drm_dp_update_payload_part1(), is
a
great example of how messy things have become. Here's a small sample of some
of the stuff I've seen from just that one function so far that either just don't make sense here or is totally redundant. I should note that a lot of these things also come from patches I reviwed, but didn't really look at as
closely as I should have because I was swamped at work, some are historical
artifacts, and others are less-than-ideal patches I got wrote myself when I
was first started working on MST and didn't know the code as well as I do now:

 * We try to avoid some sort of userspace issue by using drm_dp_mst_port_downstream_of_branch() to avoid releasing payloads for a
   branch if we can't prove it's downstream of the top of the topology. This
   seems to workaround either a userspace bug. This is a redundant, since that's what topology refs are already supposed to be doing to the extent
is
   reasonably possibly. It's also unfortunately racy solution because we have
   to be able to handle the situation where a connector is removed from under
   us. That can happen at any time, including _immediately_ after we call drm_dp_mst_port_downstream_of_branch() - rendering the call not really useful.
 * If we fail to validate the sink in drm_dp_update_payload_part, we don't update the payload table. I think at best this solution is racy and not useful, at worst it leaves us with a payload table that doesn't match what
   we attempted to set in the atomic state - which at worst brings us into undefined territory where we're just plain out of sync with the reality in
   hw.
 * Actually fun fact - mgr->payloads and mgr->proposed_vcpis both can and definitely should be removed entirely. All of the info for mgr-

Quoted 1 line

payloads

   could just be in the atomic state, because that + the magic of atomic state
   duplication means we'll also have an accurate view of the previous state's
   payload allocations: which renders mgr->proposed_vcpis redundant.

Apologies for the long explanation again, but I hope that explains my point
here a bit. I'm going to be trying to get to moving amdgpu's DSC code out of
amdgpu and into DRM helpers as well soon, so I'm really determined to clean
stuff up beforehand as every time I've done so it's become substantially easier to make changes to this code. Things used to be even worse before I started cleaning things up 2 or 3 years ago, where simple changes would end
up
getting me stuck spending hours trying to dig through lockdep or memory manegement issues. As well, I would be entirely unsurprised if bugs like this
very payload leak we're working on just disappear once we've gotten rid of all
the extraneous workarounds and state tracking here - especially with how many
special cases we have for maintaining the payload table right now. That's certainly ended up being the case in the past with a number of other difficult
to track down issues I've dealt with in MST.

Anyhow. I've been way more productive this year then last because I had over
a
month off work and am finally not super burnt out from my job, and so far I've
been making progress on the payload state cleanup far faster then I was last
year :). I think if you can't figure out where the leak is coming from even
once I get the debugging patches I mentioned fixed up, it might be a good idea
for us to try again after I've got some of this code cleaned up. I've got a
currently WIP branch here:

https://gitlab.freedesktop.org/lyudess/linux/-/commit/624dd68fa804e64b5b2060e4735d7317090692b5

Quoted 847 lines

 void amdgpu_dm_initialize_dp_connector(struct amdgpu_display_manager *dm,

drivers/gpu/drm/drm_dp_mst_topology.c +0 -0
index 857c5d15e81d..a70e26c5d084 100644
@@ -2508,6 +2508,7 @@ drm_dp_mst_handle_conn_stat(struct

drm_dp_mst_branch
*mstb,
        u8 new_pdt;
        bool new_mcs;
        bool dowork = false, create_connector = false; +       bool notify_unplug_event = false;

        port = drm_dp_get_port(mstb, conn_stat->port_number); if (!port)

drivers/gpu/drm/drm_dp_mst_topology.c +0 -0
@@ -2520,6 +2521,9 @@ drm_dp_mst_handle_conn_stat(struct

drm_dp_mst_branch
*mstb,
                         * port, so just throw the port out and make sure
we
                         * reprobe the link address of it's parent MSTB */
+                       /* should also consider notify_unplug_event here.
+                        * but it's not a normal case for products in the
market
+                        */ drm_dp_mst_topology_unlink_port(mgr, port); mstb->link_address_sent = false; dowork = true;

drivers/gpu/drm/drm_dp_mst_topology.c +0 -0
@@ -2541,10 +2545,14 @@ drm_dp_mst_handle_conn_stat(struct

drm_dp_mst_branch
*mstb,
        port->ddps = conn_stat->displayport_device_plug_status;

        if (old_ddps != port->ddps) {
-               if (port->ddps && !port->input)
+               if (port->ddps && !port->input) { drm_dp_send_enum_path_resources(mgr, mstb, port);
-               else
+               } else {
                        port->full_pbn = 0;
+                       if (port->connector &&
+                               drm_dp_mst_is_end_device(port->pdt, port-

Quoted 1 line

mcs))

+                               notify_unplug_event = true; +               }
        }

        new_pdt = port->input ? DP_PEER_DEVICE_NONE : conn_stat-

Quoted 1 line

peer_device_type;

drivers/gpu/drm/drm_dp_mst_topology.c +0 -0
@@ -2557,11 +2565,15 @@ drm_dp_mst_handle_conn_stat(struct

drm_dp_mst_branch
*mstb,
                dowork = false;
        }

+       if (notify_unplug_event && mgr->cbs->notify_csn_disconnection) +               mgr->cbs->notify_csn_disconnection(port->connector); +
        if (port->connector) drm_modeset_unlock(&mgr->base.lock);
        else if (create_connector) drm_dp_mst_port_add_connector(mstb, port);

+
 out:
        drm_dp_mst_topology_put_port(port);
        if (dowork)

include/drm/drm_dp_mst_helper.h +1 -0
index 78044ac5b59b..ff9e47729841 100644
@@ -525,6 +525,7 @@ struct drm_dp_mst_topology_cbs {
          * IRQ pulse handler.
          */
         void (*poll_hpd_irq)(struct drm_dp_mst_topology_mgr *mgr);
+       void (*notify_csn_disconnection)(struct drm_connector
 *connector);
  };
 

 #define DP_MAX_PAYLOAD (sizeof(unsigned long) * 8) --
2.31.0

Quoted 753 lines

-----Original Message-----
From: Lin, Wayne
Sent: Wednesday, December 8, 2021 11:39 AM
To: 'Lyude Paul' <[email protected]>
Subject: RE: [PATCH] WIP: drm/dp_mst: Add support for dumping topology ref
histories from debugfs

No worries Lyude!
Thanks for keeping helping on this. Take your time : )

Quoted 742 lines

-----Original Message-----
From: Lyude Paul <[email protected]>
Sent: Wednesday, December 8, 2021 7:05 AM
To: Lin, Wayne <[email protected]>
Subject: Re: [PATCH] WIP: drm/dp_mst: Add support for dumping topology
ref histories from debugfs

Sorry! I will try to get to this tomorrow, if not then sometime this week.

Quoted 721 lines · Wayne

[Public]

Hi Lyude,

Finally have some bandwidth to get back to this problem! I roughly went through this patch and I'm just aware that we already
have such kind of convenient tool for a while.
I think it's definitely useful for us to track port/mstb reference count issues and I'll start to embrace this feature for cleaning up
those issues. Thank you Lyude!

However, I think the issue that I was trying to fix is not related to what you suggested:
" The idea here is that if stream resources aren't being released, my guess would be that we're not dropping topology references for the port which means the connector never goes away." The issue I was trying to fix is about releasing dc_link->remote_sinks while receiving a CSN message notifying the connection status of a sst connector of a port changed from connected to disconnected. Not the connection status changed of a mst
branch device.
e.g.
src - 1st_mstb - 2nd_mstb - sst_monitor => src - 1st_mstb (disconnect) 2nd_mstb - sst_monitor

In above case, after receiving CSN, we will put topology references
of 2nd mstb and its port which is connected with the sst monitor. As
the result of that, we can call drm_dp_delayed_destroy_port() to unregister and put the drm connector.

However, in below case:
e.g.
src - 1st_mstb - sst_monitor => src - 1st_mstb (disconnect) sst_monitor

In this case, which is the case having problem, it definitely won't
decrease the topology references count of the port which was connected to the sst monitor to zero since the port is still existing in the topology. Same as the malloc reference since the port can't get destroyed. Hence, the port still exists  and we won't
call
drm_dp_delayed_destroy_port() to unregister and put the drm connector.
I looked up the code and drm_dp_delayed_destroy_port() seems like the only place to call drm_connector_put() which means we can't put
reference count of drm connector under this case and can't release dc_sink resource by destroying drm connector.

I would also like to point out that this resource (remote_sinks) is
specific to different stream sinks. So if we're trying to release this dc_sink resource by destroying the drm connector, it conflicts
the idea that you suggested before that we should always keep the drm connector until it's no longer reachable in the topology. Releasing dc_sink should be binding with the disconnection event.

I understand your concern that we should not just easily change the
logic here since it's the result after solving tons of bugs before and might cause other side effect. So, just my 2 cents, what I'm thinking is to register a callback function for our driver to notify
us that the remote sink is detached. This just aligns our flow handling long HPD event of legacy (sst) DP.
For sst case, once we detect long HPD event indicating the monitor is detached, we will immediately try to release the dc_link->local_sink and fire hotplug event to upper layer. Same as here, once receives a CSN message notifying a drm connector is changed from connected to disconnected, trigger the callback function and we can try to release the dc_sink resource.

Would like to know your thought and insight please : )

Btw, I got some errors and warnings while building and have some code adjustments as below : ) Thank you Lyude for your always kindly
help!!

Regards,
Wayne

Quoted 374 lines

-----Original Message-----
From: Lyude Paul <[email protected]>
Sent: Wednesday, November 3, 2021 7:15 AM
To: Lin, Wayne <[email protected]>
Subject: [PATCH] WIP: drm/dp_mst: Add support for dumping topology
ref histories from debugfs

TODO:
* Implement support for i915
* Finish writing this commit message
* ???
* profit

---

Hey wayne! SO-hopefully if I did this correctly then this should just work on amdgpu. What this patch should do is add a debugfs file to amdgpu called "amdgpu_dp_mst_topology_refs", and when you
read the file it should print out the topology reference history of every MSTB and Port in memory, along with how many times we've
hit the codepath in each backtrace. An example:

Port DP-5 (0000000005c37748) topology ref history:
  1 gets (last at    58.468973):

     drm_dp_send_link_address+0x6a5/0xa00 [drm_kms_helper]
     drm_dp_check_and_send_link_address+0xad/0xd0
[drm_kms_helper]
     drm_dp_mst_link_probe_work+0x14e/0x1a0 [drm_kms_helper]
     process_one_work+0x1e3/0x390
     worker_thread+0x50/0x3a0
     kthread+0x124/0x150
     ret_from_fork+0x1f/0x30
  1 puts (last at    58.469357):
     drm_dp_mst_topology_put_port+0x6a/0x210 [drm_kms_helper]
     drm_dp_send_link_address+0x39e/0xa00 [drm_kms_helper]
     drm_dp_check_and_send_link_address+0xad/0xd0
[drm_kms_helper]
     drm_dp_mst_link_probe_work+0x14e/0x1a0 [drm_kms_helper]
     process_one_work+0x1e3/0x390
     worker_thread+0x50/0x3a0
     kthread+0x124/0x150
     ret_from_fork+0x1f/0x30

The idea here is that if stream resources aren't being released, my guess would be that we're not dropping topology references for
the port which means the connector never goes away. So, if that's
really the case then once we unplug the offending connector we should be able to find a pair of gets/puts for the offending leaked connector where the get/put count doesn't match up. Also, if the frame count on the backtrace isn't long enough you can increase the value of STACK_DEPTH in drivers/gpu/drm/drm_dp_mst_topology.c and recompile to get more frames.

To enable this, first enable CONFIG_EXPERT for your kernel which will unhide CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS. Then just enable CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS, recompile, and it should be good to go.

Let me know if this works for you, and hopefully this should tell
us exactly what the problem actually is here.

 .../amd/display/amdgpu_dm/amdgpu_dm_debugfs.c |  35 ++++ drivers/gpu/drm/drm_dp_mst_topology.c         | 173
++++++++++++++----
 drivers/gpu/drm/nouveau/nouveau_debugfs.c     |  35 ++++ include/drm/drm_dp_mst_helper.h               |  18 ++

 4 files changed, 228 insertions(+), 33 deletions(-)

diff --git a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_debugfs.c b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_debugfs.c index 1a68a674913c..1a14732c52b4 100644

drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_debugfs.c +18 -0
@@ -3063,6 +3063,37 @@ static int mst_topo_show(struct seq_file
 *m, void
 *unused)
       return 0;
  }
 
+#ifdef CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS
+static int mst_topology_ref_dump_show(struct seq_file *m, void
+*unused) {
+     struct amdgpu_device *adev = (struct amdgpu_device
+*)m->private;
+     struct drm_device *dev = adev_to_drm(adev);
+     struct drm_connector *connector;
+     struct drm_connector_list_iter conn_iter;
+     struct amdgpu_dm_connector *aconnector;
+
+     drm_connector_list_iter_begin(dev, &conn_iter);
+     drm_for_each_connector_iter(connector, &conn_iter) {
+             if (connector->connector_type !=
 DRM_MODE_CONNECTOR_DisplayPort)
+                     continue;
+
+             aconnector = to_amdgpu_dm_connector(connector);
+
+             /* Ensure we're only dumping the topology of a

root
+mst node
*/
+             if (!aconnector->mst_mgr.max_payloads)
+                     continue;
+
+             seq_printf(m, "\nMST topology for connector %d\n", aconnector->connector_id);
+             drm_dp_mst_dump_topology_refs(m, +&aconnector->mst_mgr);
+     }
+     drm_connector_list_iter_end(&conn_iter);

drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_debugfs.c +7 -0
+
+     return 0;
+}
+
+DEFINE_SHOW_ATTRIBUTE(mst_topology_ref_dump);
+#endif
+

 /*
  * Sets trigger hpd for MST topologies.
  * All connected connectors will be rediscovered and re started as needed if val of 1 is sent.

drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_debugfs.c +0 -0
@@ -3299,6 +3330,10 @@ void dtn_debugfs_init(struct

amdgpu_device
*adev)

      debugfs_create_file("amdgpu_mst_topology", 0444, root, adev, &mst_topo_fops);

drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_debugfs.c +4 -0
+#ifdef CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS
+     debugfs_create_file("amdgpu_dp_mst_topology_refs", 0444,
+root, adev,
+                         &mst_topology_ref_dump_fops); #endif

      debugfs_create_file("amdgpu_dm_dtn_log", 0644, root, adev, &dtn_log_fops);

drivers/gpu/drm/drm_dp_mst_topology.c +0 -0
index 1aa8702383d4..0159828c494d 100644
@@ -1366,23 +1366,6 @@ static int

drm_dp_mst_wait_tx_reply(struct
drm_dp_mst_branch *mstb,
      return ret;
 }

-static struct drm_dp_mst_branch
*drm_dp_add_mst_branch_device(u8
lct, u8
*rad) -{
-     struct drm_dp_mst_branch *mstb;

drivers/gpu/drm/drm_dp_mst_topology.c +0 -14
-
-     mstb = kzalloc(sizeof(*mstb), GFP_KERNEL);
-     if (!mstb)
-             return NULL;
-
-     mstb->lct = lct;
-     if (lct > 1)
-             memcpy(mstb->rad, rad, lct / 2);
-     INIT_LIST_HEAD(&mstb->ports);
-     kref_init(&mstb->topology_kref);
-     kref_init(&mstb->malloc_kref);
-     return mstb;
-}
-

 static void drm_dp_free_mst_branch_device(struct kref *kref)  { struct drm_dp_mst_branch *mstb = @@ -1642,12 +1625,20 @@ topology_ref_type_to_str(enum drm_dp_mst_topology_ref_type type) return "put";
 }

drivers/gpu/drm/drm_dp_mst_topology.c +8 -0
+static const char *topology_ref_history_type_to_str(enum
+drm_dp_mst_topology_history_type type) {
+     if (type == DRM_DP_MST_TOPOLOGY_HISTORY_PORT)
+             return "Port";
+     else
+             return "MSTB";
+}
+

 static void
-__dump_topology_ref_history(struct drm_dp_mst_topology_ref_history *history,
-                         void *ptr, const char *type_str) +dump_topology_ref_history(struct drm_dp_mst_topology_ref_history
+*history, struct drm_printer p)
 {
-     struct drm_printer p = drm_debug_printer(DBG_PREFIX); +     char *connector_name = NULL;
      char *buf = kzalloc(PAGE_SIZE, GFP_KERNEL); +     void *ptr;
      int i;

      if (!buf)

drivers/gpu/drm/drm_dp_mst_topology.c +2 -0
@@ -1656,14 +1647,29 @@ __dump_topology_ref_history(struct
 drm_dp_mst_topology_ref_history *history,
       if (!history->len)
               goto out;
 
+     /* Get a pointer to the actual MSTB/port so we can the
+memory

address to the kernel log */
+     if (history->type == DRM_DP_MST_TOPOLOGY_HISTORY_PORT) +             ptr = container_of(history, struct drm_dp_mst_port,
topology_ref_history);
+     else
+             ptr = container_of(history, struct

drivers/gpu/drm/drm_dp_mst_topology.c +2 -0
+drm_dp_mst_branch, topology_ref_history);
+

      /* First, sort the list so that it goes from oldest to newest
       * reference entry
       */
      sort(history->entries, history->len, sizeof(*history->entries), topology_ref_history_cmp, NULL);

-     drm_printf(&p, "%s (%p) topology count reached 0, dumping history:\n",
-                type_str, ptr);
+     if (history->type == DRM_DP_MST_TOPOLOGY_HISTORY_PORT) { +             struct drm_dp_mst_port *port = ptr;

drivers/gpu/drm/drm_dp_mst_topology.c +7 -0
+
+             if (port->connector)
+                     connector_name = port->connector->name;
+     }
+     if (connector_name)
+             drm_printf(&p, "Port %s (%p) topology ref
+history:\n",

connector_name, ptr);
+     else
+             drm_printf(&p, "%s (%p) topology ref history:\n",

drivers/gpu/drm/drm_dp_mst_topology.c +2 -0
+
+topology_ref_history_type_to_str(history->type),

ptr);

      for (i = 0; i < history->len; i++) {
              const struct drm_dp_mst_topology_ref_entry *entry =
@@
-
1682,24 +1688,92 @@ __dump_topology_ref_history(struct drm_dp_mst_topology_ref_history *history,
                         ts_nsec, rem_nsec / 1000, buf); }

-     /* Now free the history, since this is the only time we expose it */
-     kfree(history->entries);
 out:
      kfree(buf);
 }

drivers/gpu/drm/drm_dp_mst_topology.c +7 -0
+/**
+ * drm_dp_mst_dump_topology_refs - helper function for dumping
+the topology ref history
+ * @m: File to print to
+ * @mgr: &struct drm_dp_mst_topology_mgr to use
+ *
+ * Prints the topology ref history of all ports and MSTBs on

@mgr
+that are still in memory,
+ * regardless of whether they're actually still reachable through
+the topology or not. Only enabled
+ * when %CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS is enabled. Can be

drivers/gpu/drm/drm_dp_mst_topology.c +20 -0
+implemented by drivers to assist
+ * with debugging leaks in the DP MST helpers.
+ */
+void drm_dp_mst_dump_topology_refs(struct seq_file *m, struct
+drm_dp_mst_topology_mgr *mgr) {
+     struct drm_dp_mst_topology_ref_history *history;
+     struct drm_printer p = drm_seq_file_printer(m);
+
+     mutex_lock(&mgr->topology_ref_history_lock);
+     list_for_each_entry(history,
+&mgr->topology_ref_history_list,
+node)
+             dump_topology_ref_history(history, p);
+     mutex_unlock(&mgr->topology_ref_history_lock);
+}
+EXPORT_SYMBOL(drm_dp_mst_dump_topology_refs);
+
+static void
+__init_topology_ref_history(struct
+drm_dp_mst_topology_ref_history

*history,
+                         struct drm_dp_mst_topology_mgr *mgr, +                         enum drm_dp_mst_topology_history_type

drivers/gpu/drm/drm_dp_mst_topology.c +12 -0
+type) {
+     history->type = type;
+     INIT_LIST_HEAD(&history->node);
+
+     mutex_lock(&mgr->topology_ref_history_lock);
+     list_add(&history->node, &mgr->topology_ref_history_list);
+     mutex_unlock(&mgr->topology_ref_history_lock);
+}
+
+static void
+__destroy_topology_ref_history(struct
+drm_dp_mst_topology_ref_history

*history,
+                            struct drm_dp_mst_topology_mgr *mgr)

drivers/gpu/drm/drm_dp_mst_topology.c +29 -0
+{
+     mutex_lock(&mgr->topology_ref_history_lock);
+     list_del(&mgr->topology_ref_history_list);
+     mutex_unlock(&mgr->topology_ref_history_lock);
+
+     kfree(history->entries);
+}
+
+static __always_inline void
+init_port_topology_history(struct drm_dp_mst_topology_mgr *mgr,
+struct drm_dp_mst_port *port) {
+     __init_topology_ref_history(&port->topology_ref_history,
+mgr,
+
+DRM_DP_MST_TOPOLOGY_HISTORY_PORT);
+}
+
+static __always_inline void
+init_mstb_topology_history(struct drm_dp_mst_topology_mgr *mgr,
+struct drm_dp_mst_branch *mstb) {
+     __init_topology_ref_history(&mstb->topology_ref_history,
+mgr,
+
+DRM_DP_MST_TOPOLOGY_HISTORY_MSTB);
+}
+
+static __always_inline void
+destroy_port_topology_history(struct drm_dp_mst_port *port) {
+     __destroy_topology_ref_history(&port-
Quoted 1 line

topology_ref_history,

drivers/gpu/drm/drm_dp_mst_topology.c +5 -0
+port->mgr); }
+
+static __always_inline void
+destroy_mstb_topology_history(struct drm_dp_mst_branch *mstb) {
+     __destroy_topology_ref_history(&mstb-
Quoted 1 line

topology_ref_history,

drivers/gpu/drm/drm_dp_mst_topology.c +2 -0
+mstb->mgr); }
+

 static __always_inline void dump_mstb_topology_history(struct drm_dp_mst_branch *mstb)  { -     __dump_topology_ref_history(&mstb->topology_ref_history, mstb,
-                                 "MSTB");
+     dump_topology_ref_history(&mstb->topology_ref_history, +drm_debug_printer(DBG_PREFIX));
 }

 static __always_inline void dump_port_topology_history(struct drm_dp_mst_port *port)  { -     __dump_topology_ref_history(&port->topology_ref_history, port,
-                                 "Port");
+     dump_topology_ref_history(&port->topology_ref_history, +drm_debug_printer(DBG_PREFIX));
 }

 static __always_inline void

drivers/gpu/drm/drm_dp_mst_topology.c +0 -0
@@ -1729,6 +1803,14 @@ topology_ref_history_unlock(struct

drm_dp_mst_topology_mgr *mgr)  }  #else  static inline void

drivers/gpu/drm/drm_dp_mst_topology.c

Quoted 2 lines
drivers/gpu/drm/drm_dp_mst_topology.c +2 -0
+init_port_topology_history(struct drm_dp_mst_topology_mgr *mgr,
+struct drm_dp_mst_port *port); static inline void

Should also add the bracket, otherwise will get warnings. => static inline void init_port_topology_history(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port) {};

drivers/gpu/drm/drm_dp_mst_topology.c

Quoted 2 lines
drivers/gpu/drm/drm_dp_mst_topology.c +2 -0
+init_mstb_topology_history(struct drm_dp_mst_topology_mgr *mgr,
+struct drm_dp_mst_branch *mstb); static inline void

Same as above
=> static inline void init_mstb_topology_history(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_branch *mstb) {};

drivers/gpu/drm/drm_dp_mst_topology.c

Quoted 2 lines
drivers/gpu/drm/drm_dp_mst_topology.c +2 -0
+destroy_port_topology_history(struct drm_dp_mst_topology_mgr
+*mgr, struct drm_dp_mst_port *port); static inline void

destroy_port_topology_history() takes one parameter only which is port.
=> destroy_port_topology_history(struct drm_dp_mst_port *port) {};

drivers/gpu/drm/drm_dp_mst_topology.c

Quoted 2 lines
drivers/gpu/drm/drm_dp_mst_topology.c +2 -0
+destroy_mstb_topology_history(struct drm_dp_mst_topology_mgr
+*mgr, struct drm_dp_mst_branch *mstb); static inline void

destroy_mstb_topology_history() takes one parameter only which is mstb => destroy_mstb_topology_history(struct drm_dp_mst_branch *mstb) {};

Quoted 233 lines

 topology_ref_history_lock(struct drm_dp_mst_topology_mgr *mgr) {}
static inline void  topology_ref_history_unlock(struct drm_dp_mst_topology_mgr *mgr) {} @@ -1740,6 +1822,25 @@ dump_port_topology_history(struct drm_dp_mst_port *port) {} #define save_port_topology_ref(port, type)  #endif

drivers/gpu/drm/drm_dp_mst_topology.c +2 -0
+static struct drm_dp_mst_branch *
+drm_dp_add_mst_branch_device(struct drm_dp_mst_topology_mgr

*mgr,

drivers/gpu/drm/drm_dp_mst_topology.c +17 -0
+u8 lct, u8 *rad) {
+     struct drm_dp_mst_branch *mstb;
+
+     mstb = kzalloc(sizeof(*mstb), GFP_KERNEL);
+     if (!mstb)
+             return NULL;
+
+     mstb->lct = lct;
+     if (lct > 1)
+             memcpy(mstb->rad, rad, lct / 2);
+     INIT_LIST_HEAD(&mstb->ports);
+     kref_init(&mstb->topology_kref);
+     kref_init(&mstb->malloc_kref);
+     init_mstb_topology_history(mgr, mstb);
+     return mstb;
+}
+

 static void drm_dp_destroy_mst_branch_device(struct kref *kref) {
      struct drm_dp_mst_branch *mstb = @@ -1747,6 +1848,7 @@ static void drm_dp_destroy_mst_branch_device(struct kref *kref)
      struct drm_dp_mst_topology_mgr *mgr = mstb->mgr;

      dump_mstb_topology_history(mstb);
+     destroy_mstb_topology_history(mstb);

      INIT_LIST_HEAD(&mstb->destroy_next);

drivers/gpu/drm/drm_dp_mst_topology.c +0 -0
@@ -1856,6 +1958,7 @@ static void drm_dp_destroy_port(struct

kref
*kref)
      struct drm_dp_mst_topology_mgr *mgr = port->mgr;

      dump_port_topology_history(port);
+     destroy_port_topology_history(port);

      /* There's nothing that needs locking to destroy an input port yet */
      if (port->input) {

drivers/gpu/drm/drm_dp_mst_topology.c +2 -1
@@ -2135,7 +2238,7 @@ drm_dp_port_set_pdt(struct drm_dp_mst_port
 *port, u8 new_pdt,
                       ret = drm_dp_mst_register_i2c_bus(port);
               } else {
                       lct = drm_dp_calculate_rad(port, rad);
-                     mstb = drm_dp_add_mst_branch_device(lct,
 rad);
+                     mstb = drm_dp_add_mst_branch_device(mgr,
+lct, rad);

                      if (!mstb) {
                              ret = -ENOMEM; drm_err(mgr->dev, "Failed to create
MSTB for port %p", port); @@ -2366,6 +2469,8 @@ drm_dp_mst_add_port(struct drm_device *dev,
       */
      drm_dp_mst_get_mstb_malloc(mstb);

+     init_port_topology_history(mgr, port);
+
      return port;
 }

drivers/gpu/drm/drm_dp_mst_topology.c +0 -0
@@ -3745,7 +3850,7 @@ int drm_dp_mst_topology_mgr_set_mst(struct

drm_dp_mst_topology_mgr *mgr, bool ms
              }

              /* add initial branch device at LCT 1 */ -             mstb = drm_dp_add_mst_branch_device(1, NULL); +             mstb = drm_dp_add_mst_branch_device(mgr, 1, NULL); if (mstb == NULL) {
                      ret = -ENOMEM;
                      goto out_unlock; @@ -5512,14 +5617,16 @@ int
drm_dp_mst_topology_mgr_init(struct drm_dp_mst_topology_mgr *mgr, mutex_init(&mgr->delayed_destroy_lock); mutex_init(&mgr->up_req_lock); mutex_init(&mgr->probe_lock); -#if IS_ENABLED(CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS)
-     mutex_init(&mgr->topology_ref_history_lock); -#endif
      INIT_LIST_HEAD(&mgr->tx_msg_downq); INIT_LIST_HEAD(&mgr->destroy_port_list); INIT_LIST_HEAD(&mgr->destroy_branch_device_list); INIT_LIST_HEAD(&mgr->up_req_list);

drivers/gpu/drm/drm_dp_mst_topology.c +5 -0
+#if IS_ENABLED(CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS)
+     mutex_init(&mgr->topology_ref_history_lock);
+     INIT_LIST_HEAD(&mgr->topology_ref_history_list);
+#endif
+

      /*
       * delayed_destroy_work will be queued on a dedicated WQ, so
that any
       * requeuing will be also flushed when deiniting the topology manager.

drivers/gpu/drm/nouveau/nouveau_debugfs.c +3 -0
index 1cbe01048b93..53ec7c1daada 100644
@@ -29,9 +29,13 @@
   */
 
  #include <linux/debugfs.h>
+#include <drm/drm_dp_mst_helper.h> #include <drm/drm_encoder.h>
  #include <nvif/class.h>
  #include <nvif/if0001.h>
+#include <nouveau_encoder.h>
  #include "nouveau_debugfs.h"
+#include "nouveau_display.h"
  #include "nouveau_drv.h"
 

 static int

drivers/gpu/drm/nouveau/nouveau_debugfs.c +0 -0
@@ -68,6 +72,34 @@ nouveau_debugfs_strap_peek(struct seq_file
 *m,

void
*data)
      return 0;
 }

drivers/gpu/drm/nouveau/nouveau_debugfs.c +16 -0
+#ifdef CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS
+static int nouveau_debugfs_mst_topology_history(struct seq_file
+*m, void *data) {
+     struct drm_info_node *node = m->private;
+     struct drm_device *dev = node->minor->dev;
+     struct drm_encoder *encoder;
+     struct nouveau_display *disp = nouveau_display(dev);
+
+     if (disp->disp.object.oclass < GF110_DISP)
+             return -EINVAL;
+
+     drm_for_each_encoder(encoder, dev) {
+             struct nv50_mstm *mstm;
+
+             /* We need the real encoder for each MST mgr */
+             if (encoder->encoder_type !=

DRM_MODE_ENCODER_TMDS)
+                     continue;
+             mstm = nouveau_encoder(encoder)->dp.mstm; +             if (!mstm)
+                     continue;

drivers/gpu/drm/nouveau/nouveau_debugfs.c +8 -0
+
+             seq_printf(m, "MSTM on %s:\n", encoder->name);
+             drm_dp_mst_dump_topology_refs(m, &mstm->mgr);
+     }
+     return 0;
+}
+#endif
+

 static int
 nouveau_debugfs_pstate_get(struct seq_file *m, void *data)  { @@
-213,6
+245,9 @@ static const struct file_operations nouveau_pstate_fops = {  static struct drm_info_list nouveau_debugfs_list[] = {
      { "vbios.rom",  nouveau_debugfs_vbios_image, 0, NULL }, { "strap_peek", nouveau_debugfs_strap_peek, 0, NULL },

drivers/gpu/drm/nouveau/nouveau_debugfs.c +3 -0
+#ifdef CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS
+     { "dp_mst_topology_refs",
+nouveau_debugfs_mst_topology_history, 0, NULL }, #endif

 };
 #define NOUVEAU_DEBUGFS_ENTRIES
ARRAY_SIZE(nouveau_debugfs_list)

(quoted diff) +0 -0

b/include/drm/drm_dp_mst_helper.h index
78044ac5b59b..e3a73d8d97c0
100644

include/drm/drm_dp_mst_helper.h +4 -0
@@ -35,6 +35,11 @@ enum drm_dp_mst_topology_ref_type {
       DRM_DP_MST_TOPOLOGY_REF_PUT,
  };
 
+enum drm_dp_mst_topology_history_type {
+     DRM_DP_MST_TOPOLOGY_HISTORY_PORT,
+     DRM_DP_MST_TOPOLOGY_HISTORY_MSTB, };
+
  struct drm_dp_mst_topology_ref_history {
       struct drm_dp_mst_topology_ref_entry {

              enum drm_dp_mst_topology_ref_type type; @@ -43,6 +48,9 @@ struct drm_dp_mst_topology_ref_history { depot_stack_handle_t backtrace;
      } *entries;
      int len;
+
+     enum drm_dp_mst_topology_history_type type;
+     struct list_head node;
 };
 #endif /* IS_ENABLED(CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS) */

include/drm/drm_dp_mst_helper.h +4 -0
@@ -769,6 +777,12 @@ struct drm_dp_mst_topology_mgr {
        * &drm_dp_mst_branch.topology_ref_history.
        */
       struct mutex topology_ref_history_lock;
+
+     /**
+      * @topology_ref_history_list: contains a list of topology
+ref

histories for any MSTBs or
+      * ports that are still in memory
+      */
+     struct list_head topology_ref_history_list;
 #endif
 };

include/drm/drm_dp_mst_helper.h +0 -0
@@ -873,6 +887,10 @@ void drm_dp_mst_put_port_malloc(struct
 drm_dp_mst_port *port);
 

 struct drm_dp_aux *drm_dp_mst_dsc_aux_for_port(struct drm_dp_mst_port *port);

include/drm/drm_dp_mst_helper.h +4 -0
+#ifdef CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS
+void drm_dp_mst_dump_topology_refs(struct seq_file *m, struct
+drm_dp_mst_topology_mgr *mgr); #endif
+

 extern const struct drm_private_state_funcs
drm_dp_mst_topology_state_funcs;

Quoted 4 lines
Quoted 3 lines

 /**
--
2.31.1

--
Cheers,
 Lyude Paul (she/her)
 Software Engineer at Red Hat

Signature · 3 lines
Cheers,
 Lyude Paul (she/her)
 Software Engineer at Red Hat
#8 lore raw
In reply to Lyude Paul

[Public]

Hey Lyude,

Sorry, just got chance to get back to this and BIG THANKS to elaborating in such details! Following are some of my comments : )

Quoted 8 lines

-----Original Message-----
From: Lyude Paul <[email protected]>
Sent: Wednesday, January 19, 2022 9:59 AM
To: Lin, Wayne <[email protected]>
Cc: [email protected]
Subject: Re: [PATCH] WIP: drm/dp_mst: Add support for dumping topology ref histories from debugfs

Hey - so, the original issue here was that payloads were not always deleted when we expected them to be - correct?

Actually, what I was trying to do is not relevant to payload maintenance. What I wanted to do is having a way to notify amdgpu to release its enumerated resource "dc_sink" when unplug event occurs. Currently, under specific scenarios, we can't release remote "dc_sink" and ends up can't allocate resource for new connected monitors. I'll explain the scenario underneath.

So, get back to the main issue that I was trying to solve when I pushed the patchset:
https://patchwork.kernel.org/project/dri-devel/cover/[email protected]/

I was trying to fix some problems that I observed after these 2 patches
* 09b974e8983 drm/amd/amdgpu_dm/mst: Remove ->destroy_connector() callback
* 72dc0f51591 drm/dp_mst: Remove drm_dp_mst_topology_cbs.destroy_connector

With above patches, it changes our flow to remove dc_sink (by calling dc_link_remote_sink()) when connector is about to be destroyed. And let's see below use cases:
a). Disconnection happens between mst branch devices
e.g.
src - 1st_mstb - 2nd_mstb - sst_monitor => src - 1st_mstb (disconnect) 2nd_mstb - sst_monitor

In above case, after receiving CSN, we will put topology reference count of 2nd mstb and its port which connected to the sst monitor. As you suggested before, we will destroy 2nd mst branch device and its all ports due to there is no more mst path can route to them. As the result of that, we at end call drm_dp_delayed_destroy_port() and unregister/put the drm connector and eventually call
dc_link_remote_sink() to release the dc_sinck we enumerate for the specific stream sink of sst_monitor.

However, in below case:
b). Disconnection happens between mst branch devices and SST monitor e.g.
src - 1st_mstb - sst_monitor => src - 1st_mstb (disconnect) sst_monitor

In this case, which is the case having problem, it definitely won't decrease the topology references count of the port of 1st_mstb which was connected to the sst monitor to zero since the port is still existing in the topology. Same as the malloc reference since the port can't get destroyed. Hence, the port still exists and we won't call drm_dp_delayed_destroy_port() to unregister and put the drm connector. As the result of that, we can't release corresponding remote dc_sink under this case. And after few times hotplugs, we can't create any new dc_sink since the number of stream sink is exceeding our limitation.

Hence, what I'm thinking here is to register a callback function for notifying us that the remote sink is detached. This just aligns what we do for handling long HPD event (unplug monitor from system) of legacy DP (none mst). For which case, once we detect long HPD event indicating the monitor is detached, we will immediately try to release the dc_link->local_sink and fire hotplug event to upper layer. Same as here, once receives a CSN message notifying a drm connector is changed from connected to disconnected, trigger the callback function and we can try to release the dc_sink resource.

Quoted 20 lines

If I'm remembering that correctly, then (and I'm not 100% sure on this yet) I might already have noticed something very possibly wonky in how we handle payload allocations currently, that I found while working on the non-atomic removal branch that I linked to you in my previous response. Basically, in the
step1() function it looks like that we follow this general flow with updating
payloads:

* Loop through proposed payloads and compare to previously programmed payloads
- If a payload has a different allocation then it previously did, update the payload
- If the payload is new, create it
- If a payload no longer has an allocation, remove the payload

At first glance this seems totally correct - but I'm not actually entirely convinced it is? Particularly because it seems like the order in which we do creation/deletion of payloads is totally arbitrary. To explain what I mean by that, consider a state transition like this:

vcpi_slots=15 vcpi_slots=35 vcpi_slots=14
| 1 | 2 |xxxxxxxx|

Quoted 8 lines

Let's say we want to increase payload #1 from 15 to 50, and disable payload #2 in the same atomic commit on DRM's side. If the order we update payloads is entirely arbitrary, we could end up doing this:

* Increase VCPI slots payload #1 from 15->50 (total VCPI slots=99, overflow!)
* Decrease VCPI slots payload #2 from 35->0 (total VCPI slots=50)

Notice on the first step, we've technically overflowed the available number of VCPI slots in the payload table. This is still before step 2 though, and I could be totally wrong here - perhaps this is entirely OK in the real world, and we're allowed to overflow VC slots as long as

I think it's forbidden to allocate time slots beyond 64 when we're updating payload ID table of the branch in the real world. Branch probably will NACK the DPCD write.

Quoted 2 lines

we repair the issue before step 2. But so far I can't seem to find anything in the DP 2.0 spec that explicitly states this would be OK - which makes me think we might fail some payload allocations if we don't always ensure we avoid overflows in between VC payload table changes

Agree.
For amdgpu, I think we always do deallocation (call hws->funcs.reset_hw_ctx_wrap()) before allocation (apply_single_controller_ctx_to_hw()).

Quoted 79 lines

Note that avoiding overflows would be as simple as just making sure we send all VC payload table changes that free VC slots before sending any that take new slots.

Again - I haven't actually confirmed this yet and am hoping to test stuff like this very soon as I'm pretty close finishing up the initial attempt at removing the non-atomic MST code in the DRM helpers now. If my theory ends up being correct here, I can fix this in my rewrite of the MST payload management code. But I figured it might be worth mentioning in the mean time in case you think it might be relevant to the problem here :).

Quoted 70 lines · Lyude Paul

(CC'ing this to dri-devel, since this is basically patch review)

Alright - so first off sorry about the broken debugging patch! I thought I had tested that but I guess I hadn't tested it well enough, I'll get it fixed asap, but feel free to try getting to it before I do.

As for this patch... (comments below)

Quoted 46 lines · Wayne

[AMD Official Use Only]

Hi Lyude,

No rush and thanks for your time! : ) Just want to help clarify what I mean that "registering a callback function"
for CSN of disconnecting
stream sink device (not mst branch case). Attach below the tentative patch that I planned to do. Thanks so much!

Regards,
Wayne

---
 .../display/amdgpu_dm/amdgpu_dm_mst_types.c   | 53
+++++++++++++++++++
 drivers/gpu/drm/drm_dp_mst_topology.c         | 16 +++++-
 include/drm/drm_dp_mst_helper.h               |  1 +
 3 files changed, 68 insertions(+), 2 deletions(-)

diff --git a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_mst_types.c b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_mst_types.c index cc34a35d0bcb..d7343c64908c 100644

drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_mst_types.c +16 -0
@@ -472,8 +472,61 @@ dm_dp_add_mst_connector(struct
 drm_dp_mst_topology_mgr *mgr,
        return connector;
 }
 
+void dm_dp_notify_csn_disconnection(struct drm_connector
+*connector) {
+       struct amdgpu_dm_connector *aconnector =
+               to_amdgpu_dm_connector(connector);
+       struct dc_sink *dc_sink = aconnector->dc_sink;
+       struct dc_link *dc_link = aconnector->dc_link;
+       struct amdgpu_device *adev = drm_to_adev(ddev);
+
+       ASSERT(dc_link);
+
+       if (dc_sink) {
+               mutex_lock(&adev->dm.dc_lock);
+
+               /*clear the remote sink of the link*/
+               dc_link_remove_remote_sink(dc_link, dc_sink);
+               dc_sink_release(dc_sink);

drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_mst_types.c dm_dp_add_mst_connector(struct · line 501

Quoted 10 lines
drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_mst_types.c +7 -0
+               aconnector->dc_sink = NULL;
+
+               mutex_unlock(&adev->dm.dc_lock);
+       }
+}
+
 static const struct drm_dp_mst_topology_cbs dm_mst_cbs = {
        .add_connector = dm_dp_add_mst_connector,
+       .notify_csn_disconnection = dm_dp_notify_csn_disconnection,
 };

I still don't really think this is a good idea. This seems like it's just adding another hotplugging path to the code in order to avoid sending hotplugs for non-endpoint devices. In addition to the

So, what I wanted to do is to add a callback function for endpoint devices case as described above. For disconnection of non-endpoint devices is already taken care by current code.

Quoted 8 lines
Quoted 8 lines

drm_connector issues I mentioned before, we also really need to stop doing any kind of payload maintence in hotplugging paths. The reality is any kind of payload maintanence we do outside of normal modesetting paths is a relic from legacy modesetting that I'm dropping ASAP (more on this below), and we can't keep adding to it because it dramatically complicates maintanence as well.

Quoted 14 lines
Quoted 6 lines

Sorry for repeating this point so often but - the biggest issue too is I'm still not sure what it is we're even avoiding here. We know resources aren't released consistently, and that we're able to reproduce the behavior with repeated hotplugs. We also know that if we skip sending certain hotplug events, that fixes the issue. And we know we can workaround it by adding a special case for forcing a payload

Quoted 8 lines

release in DC. But none of those actually tell us exactly what piece of code is leaking and why, which means that any workarounds we're putting in to avoid this mysterious guilty section of code we don't entirely understand either - which means we're just adding more code in that no one actually fully understands. This just ends up making maintence difficult because every change in code nearby workarounds like this has to strugle to try to figure out said workarounds in order to avoid breaking things.

Thank you Lyude for the reminder and I totally agree with you by avoiding adding workaround since it will get the code maintenance harder. And I think the root cause of the issue that I observed before is quite concrete. There is no code path for us to release dc_sink under the unplug scenario that I described above.

Quoted 57 lines
Quoted 57 lines

I'm actually currently running into these "later" issues right now, as recently I've (-actively-, finally!!!) been working on trying to remove as much non-atomic MST as I can because. As it turns out - a huge amount of the payload maintanence code just isn't needed at all when we stop caring about non-atomic drivers and stick everything in atomic state structs. Step 1 for updating updating the payload table, e.g. drm_dp_update_payload_part1(), is a great example of how messy things have become. Here's a small sample of some of the stuff I've seen from just that one function so far that either just don't make sense here or is totally redundant. I should note that a lot of these things also come from patches I reviwed, but didn't really look at as closely as I should have because I was swamped at work, some are historical artifacts, and others are less-than-ideal patches I got wrote myself when I was first started working on MST and didn't know the code as well as I do
now:

* We try to avoid some sort of userspace issue by using drm_dp_mst_port_downstream_of_branch() to avoid releasing payloads
for a
branch if we can't prove it's downstream of the top of the
topology. This
seems to workaround either a userspace bug. This is a redundant,
since
that's what topology refs are already supposed to be doing to the
extent is
reasonably possibly. It's also unfortunately racy solution because
we have
to be able to handle the situation where a connector is removed
from under
us. That can happen at any time, including _immediately_ after we
call
drm_dp_mst_port_downstream_of_branch() - rendering the call not
really
useful.
* If we fail to validate the sink in drm_dp_update_payload_part, we
don't
update the payload table. I think at best this solution is racy and
not
useful, at worst it leaves us with a payload table that doesn't
match what
we attempted to set in the atomic state - which at worst brings us
into
undefined territory where we're just plain out of sync with the
reality in
hw.
* Actually fun fact - mgr->payloads and mgr->proposed_vcpis both can
and
definitely should be removed entirely. All of the info for
mgr->payloads
could just be in the atomic state, because that + the magic of
atomic state
duplication means we'll also have an accurate view of the previous
state's
payload allocations: which renders mgr->proposed_vcpis redundant.

Quoted 1 line
Quoted 1 line

Apologies for the long explanation again, but I hope that explains my

Really really thanks for your kindly help on this : ) I'll try my best to get to your WIP branch soon. Thank you Lyude!

Quoted 880 lines
Quoted 23 lines

point here a bit. I'm going to be trying to get to moving amdgpu's DSC code out of amdgpu and into DRM helpers as well soon, so I'm really determined to clean stuff up beforehand as every time I've done so it's become substantially easier to make changes to this code. Things used to be even worse before I started cleaning things up 2 or 3 years ago, where simple changes would end up getting me stuck spending hours trying to dig through lockdep or memory manegement issues. As well, I would be entirely unsurprised if bugs like this very payload leak we're working on just disappear once we've gotten rid of all the extraneous workarounds and state tracking here - especially with how many special cases we have for maintaining the payload table right now. That's certainly ended up being the case in the past with a number of other difficult to track down issues I've dealt with in MST.

Anyhow. I've been way more productive this year then last because I had over a month off work and am finally not super burnt out from my job, and so far I've been making progress on the payload state cleanup far faster then I was last year :). I think if you can't figure out where the leak is coming from even once I get the debugging patches I mentioned fixed up, it might be a good idea for us to try again after I've got some of this code cleaned up. I've got a currently WIP branch here:

Quoted 856 lines

https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgitl
ab.freedesktop.org%2Flyudess%2Flinux%2F-%2Fcommit%2F624dd68fa804e64b5b 2060e4735d7317090692b5&amp;data=04%7C01%7Cwayne.lin%40amd.com%7Cd4bdc7 59eb274bfccc8208d9daef41fe%7C3dd8961fe4884e608e11a82d994e183d%7C0%7C0% 7C637781543416430632%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQI joiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&amp;sdata=nFpVl%2BVoF 40JPabfRMcR6Cz7ZHDwt%2BBpLDBNdXftJaA%3D&amp;reserved=0

Quoted 847 lines
drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_mst_types.c +0 -0
 void amdgpu_dm_initialize_dp_connector(struct

amdgpu_display_manager *dm, diff --git
a/drivers/gpu/drm/drm_dp_mst_topology.c
b/drivers/gpu/drm/drm_dp_mst_topology.c
index 857c5d15e81d..a70e26c5d084 100644

drivers/gpu/drm/drm_dp_mst_topology.c +1 -0
@@ -2508,6 +2508,7 @@ drm_dp_mst_handle_conn_stat(struct
 drm_dp_mst_branch *mstb,
        u8 new_pdt;
        bool new_mcs;
        bool dowork = false, create_connector = false;
+       bool notify_unplug_event = false;
 
        port = drm_dp_get_port(mstb, conn_stat->port_number);
        if (!port)
drivers/gpu/drm/drm_dp_mst_topology.c +0 -0
@@ -2520,6 +2521,9 @@ drm_dp_mst_handle_conn_stat(struct
 drm_dp_mst_branch *mstb,
                         * port, so just throw the port out and make

sure we
* reprobe the link address of it's parent
MSTB

                         */
drivers/gpu/drm/drm_dp_mst_topology.c +3 -0
+                       /* should also consider notify_unplug_event here.
+                        * but it's not a normal case for products
+in the

market
+ */

                        drm_dp_mst_topology_unlink_port(mgr, port);
                        mstb->link_address_sent = false;
                        dowork = true; @@ -2541,10 +2545,14 @@
drm_dp_mst_handle_conn_stat(struct
drm_dp_mst_branch
*mstb,
        port->ddps = conn_stat->displayport_device_plug_status;

        if (old_ddps != port->ddps) {
drivers/gpu/drm/drm_dp_mst_topology.c +1 -1
-               if (port->ddps && !port->input)
+               if (port->ddps && !port->input) {
                        drm_dp_send_enum_path_resources(mgr, mstb,

port);

drivers/gpu/drm/drm_dp_mst_topology.c +4 -1
-               else
+               } else {
                        port->full_pbn = 0;
+                       if (port->connector &&
+                               drm_dp_mst_is_end_device(port->pdt,
+port-
Quoted 1 line

mcs))

drivers/gpu/drm/drm_dp_mst_topology.c +2 -0
+                               notify_unplug_event = true;
+               }
        }
 
        new_pdt = port->input ? DP_PEER_DEVICE_NONE : conn_stat-
Quoted 1 line

peer_device_type;

drivers/gpu/drm/drm_dp_mst_topology.c +0 -0
@@ -2557,11 +2565,15 @@ drm_dp_mst_handle_conn_stat(struct

drm_dp_mst_branch
*mstb,

                dowork = false;
        }
drivers/gpu/drm/drm_dp_mst_topology.c +5 -0
+       if (notify_unplug_event &&
+mgr->cbs->notify_csn_disconnection)
+               mgr->cbs->notify_csn_disconnection(port->connector);
+
        if (port->connector)
                drm_modeset_unlock(&mgr->base.lock);
        else if (create_connector)
                drm_dp_mst_port_add_connector(mstb, port);
 
+
 out:
        drm_dp_mst_topology_put_port(port);
        if (dowork)
(quoted diff) +0 -0

b/include/drm/drm_dp_mst_helper.h index 78044ac5b59b..ff9e47729841 100644

include/drm/drm_dp_mst_helper.h +2 -1
@@ -525,6 +525,7 @@ struct drm_dp_mst_topology_cbs {
         * IRQ pulse handler.
         */
        void (*poll_hpd_irq)(struct drm_dp_mst_topology_mgr *mgr);
+       void (*notify_csn_disconnection)(struct drm_connector
+*connector);
 };
 
 #define DP_MAX_PAYLOAD (sizeof(unsigned long) * 8)
--

2.31.0

Quoted 755 lines

-----Original Message-----
From: Lin, Wayne
Sent: Wednesday, December 8, 2021 11:39 AM
To: 'Lyude Paul' <[email protected]>
Subject: RE: [PATCH] WIP: drm/dp_mst: Add support for dumping topology ref histories from debugfs

No worries Lyude!
Thanks for keeping helping on this. Take your time : )

Quoted 745 lines

-----Original Message-----
From: Lyude Paul <[email protected]>
Sent: Wednesday, December 8, 2021 7:05 AM
To: Lin, Wayne <[email protected]>
Subject: Re: [PATCH] WIP: drm/dp_mst: Add support for dumping topology ref histories from debugfs

Sorry! I will try to get to this tomorrow, if not then sometime this week.

Quoted 725 lines · Wayne

[Public]

Hi Lyude,

Finally have some bandwidth to get back to this problem! I roughly went through this patch and I'm just aware that we already have such kind of convenient tool for a while. I think it's definitely useful for us to track port/mstb reference count issues and I'll start to embrace this feature for cleaning up those issues. Thank you Lyude!

However, I think the issue that I was trying to fix is not related to what you suggested:
" The idea here is that if stream resources aren't being released, my guess would be that we're not dropping topology references for the port which means the connector never goes away." The issue I was trying to fix is about releasing dc_link->remote_sinks while receiving a CSN message notifying the connection status of a sst connector of a port changed from connected to disconnected. Not the connection status changed of a mst branch device.
e.g.
src - 1st_mstb - 2nd_mstb - sst_monitor => src - 1st_mstb (disconnect) 2nd_mstb - sst_monitor

In above case, after receiving CSN, we will put topology references of 2nd mstb and its port which is connected with the sst monitor. As the result of that, we can call drm_dp_delayed_destroy_port() to unregister and put the drm connector.

However, in below case:
e.g.
src - 1st_mstb - sst_monitor => src - 1st_mstb (disconnect) sst_monitor

In this case, which is the case having problem, it definitely won't decrease the topology references count of the port which was connected to the sst monitor to zero since the port is still existing in the topology. Same as the malloc reference since the port can't get destroyed. Hence, the port still exists and we won't call drm_dp_delayed_destroy_port() to unregister and put the drm connector.
I looked up the code and drm_dp_delayed_destroy_port() seems like the only place to call drm_connector_put() which means we can't put reference count of drm connector under this case and can't release dc_sink resource by destroying drm connector.

I would also like to point out that this resource
(remote_sinks) is specific to different stream sinks. So if
we're trying to release this dc_sink resource by destroying
the drm connector, it conflicts the idea that you suggested
before that we should always keep the drm connector until it's no longer reachable in the topology. Releasing dc_sink should be binding with the disconnection event.

I understand your concern that we should not just easily change the logic here since it's the result after solving tons of bugs before and might cause other side effect. So, just my 2 cents, what I'm thinking is to register a callback function for our driver to notify us that the remote sink is detached. This just aligns our flow handling long HPD event of legacy (sst) DP. For sst case, once we detect long HPD event indicating the monitor is detached, we will immediately try to release the dc_link->local_sink and fire hotplug event to upper layer. Same as here, once receives a CSN message notifying a drm connector is changed from connected to disconnected, trigger the callback function and we can try to release the dc_sink resource.

Would like to know your thought and insight please : )

Btw, I got some errors and warnings while building and have some code adjustments as below : ) Thank you Lyude for your always kindly help!!

Regards,
Wayne

Quoted 381 lines

-----Original Message-----
From: Lyude Paul <[email protected]>
Sent: Wednesday, November 3, 2021 7:15 AM
To: Lin, Wayne <[email protected]>
Subject: [PATCH] WIP: drm/dp_mst: Add support for dumping topology ref histories from debugfs

TODO:
* Implement support for i915
* Finish writing this commit message
* ???
* profit

---

Hey wayne! SO-hopefully if I did this correctly then this
should just work on amdgpu. What this patch should do is add
a debugfs file to amdgpu called
"amdgpu_dp_mst_topology_refs", and when you read the file it
should print out the topology reference history of every
MSTB and Port in memory, along with how many times we've hit the codepath in each backtrace. An example:

Port DP-5 (0000000005c37748) topology ref history:
1 gets (last at 58.468973):

     drm_dp_send_link_address+0x6a5/0xa00 [drm_kms_helper]
     drm_dp_check_and_send_link_address+0xad/0xd0
[drm_kms_helper]
     drm_dp_mst_link_probe_work+0x14e/0x1a0 [drm_kms_helper]
     process_one_work+0x1e3/0x390
     worker_thread+0x50/0x3a0
     kthread+0x124/0x150
     ret_from_fork+0x1f/0x30
  1 puts (last at    58.469357):
     drm_dp_mst_topology_put_port+0x6a/0x210
[drm_kms_helper]
     drm_dp_send_link_address+0x39e/0xa00 [drm_kms_helper]
     drm_dp_check_and_send_link_address+0xad/0xd0
[drm_kms_helper]
     drm_dp_mst_link_probe_work+0x14e/0x1a0 [drm_kms_helper]
     process_one_work+0x1e3/0x390
     worker_thread+0x50/0x3a0
     kthread+0x124/0x150
     ret_from_fork+0x1f/0x30

The idea here is that if stream resources aren't being released, my guess would be that we're not dropping topology references for the port which means the connector never goes away. So, if that's really the case then once we unplug the offending connector we should be able to find a pair of gets/puts for the offending leaked connector where the get/put count doesn't match up. Also, if the frame count on the backtrace isn't long enough you can increase the value of STACK_DEPTH in drivers/gpu/drm/drm_dp_mst_topology.c and recompile to get more frames.

To enable this, first enable CONFIG_EXPERT for your kernel which will unhide CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS. Then just enable CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS, recompile, and it should be good to go.

Let me know if this works for you, and hopefully this should tell us exactly what the problem actually is here.

 .../amd/display/amdgpu_dm/amdgpu_dm_debugfs.c |  35 ++++
 drivers/gpu/drm/drm_dp_mst_topology.c         | 173
++++++++++++++----
 drivers/gpu/drm/nouveau/nouveau_debugfs.c     |  35 ++++
 include/drm/drm_dp_mst_helper.h               |  18 ++
 4 files changed, 228 insertions(+), 33 deletions(-)

diff --git a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_debugfs.c b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_debugfs.c index 1a68a674913c..1a14732c52b4 100644
---
a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_debugfs.c +++ b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_debugf +++ s.c

(quoted diff) +0 -0
@@ -3063,6 +3063,37 @@ static int mst_topo_show(struct

seq_file *m, void
*unused)

      return 0;

}

(quoted diff) +14 -0
+#ifdef CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS
+static int mst_topology_ref_dump_show(struct seq_file *m,
+void
+*unused) {
+     struct amdgpu_device *adev = (struct amdgpu_device
+*)m->private;
+     struct drm_device *dev = adev_to_drm(adev);
+     struct drm_connector *connector;
+     struct drm_connector_list_iter conn_iter;
+     struct amdgpu_dm_connector *aconnector;
+
+     drm_connector_list_iter_begin(dev, &conn_iter);
+     drm_for_each_connector_iter(connector, &conn_iter) {
+             if (connector->connector_type !=

DRM_MODE_CONNECTOR_DisplayPort)

(quoted diff) +7 -0
+                     continue;
+
+             aconnector =
+to_amdgpu_dm_connector(connector);
+
+             /* Ensure we're only dumping the topology of a
+root mst node

*/

(quoted diff) +5 -0
+             if (!aconnector->mst_mgr.max_payloads)
+                     continue;
+
+             seq_printf(m, "\nMST topology for connector
+%d\n",

aconnector->connector_id);

(quoted diff) +11 -0
+             drm_dp_mst_dump_topology_refs(m,
+&aconnector->mst_mgr);
+     }
+     drm_connector_list_iter_end(&conn_iter);
+
+     return 0;
+}
+
+DEFINE_SHOW_ATTRIBUTE(mst_topology_ref_dump);
+#endif
+
 /*
  * Sets trigger hpd for MST topologies.
  * All connected connectors will be rediscovered and re

started as needed if val of 1 is sent.

(quoted diff) +0 -0
@@ -3299,6 +3330,10 @@ void dtn_debugfs_init(struct

amdgpu_device
*adev)

      debugfs_create_file("amdgpu_mst_topology", 0444, root,
                          adev, &mst_topo_fops);
(quoted diff) +5 -0
+#ifdef CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS
+     debugfs_create_file("amdgpu_dp_mst_topology_refs",
+0444, root, adev,
+                         &mst_topology_ref_dump_fops);
+#endif
      debugfs_create_file("amdgpu_dm_dtn_log", 0644, root,

adev,

                          &dtn_log_fops);
drivers/gpu/drm/drm_dp_mst_topology.c +0 -16
index 1aa8702383d4..0159828c494d 100644
@@ -1366,23 +1366,6 @@ static int
 drm_dp_mst_wait_tx_reply(struct drm_dp_mst_branch *mstb,
      return ret;
 }
 
-static struct drm_dp_mst_branch
 *drm_dp_add_mst_branch_device(u8 lct, u8
 *rad) -{
-     struct drm_dp_mst_branch *mstb;
-
-     mstb = kzalloc(sizeof(*mstb), GFP_KERNEL);
-     if (!mstb)
-             return NULL;
-
-     mstb->lct = lct;
-     if (lct > 1)
-             memcpy(mstb->rad, rad, lct / 2);
-     INIT_LIST_HEAD(&mstb->ports);
-     kref_init(&mstb->topology_kref);
-     kref_init(&mstb->malloc_kref);
-     return mstb;
-}
-
 static void drm_dp_free_mst_branch_device(struct kref
*kref)  {
      struct drm_dp_mst_branch *mstb = @@ -1642,12 +1625,20
@@ topology_ref_type_to_str(enum
drm_dp_mst_topology_ref_type type)
              return "put";

}

drivers/gpu/drm/drm_dp_mst_topology.c +7 -1
+static const char *topology_ref_history_type_to_str(enum
+drm_dp_mst_topology_history_type type) {
+     if (type == DRM_DP_MST_TOPOLOGY_HISTORY_PORT)
+             return "Port";
+     else
+             return "MSTB"; }
+
 static void
-__dump_topology_ref_history(struct

drm_dp_mst_topology_ref_history *history,

drivers/gpu/drm/drm_dp_mst_topology.c +7 -2
-                         void *ptr, const char *type_str)
+dump_topology_ref_history(struct
+drm_dp_mst_topology_ref_history *history, struct
+drm_printer p)
 {
-     struct drm_printer p = drm_debug_printer(DBG_PREFIX);
+     char *connector_name = NULL;
      char *buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
+     void *ptr;
      int i;
 
      if (!buf)
@@ -1656,14 +1647,29 @@ __dump_topology_ref_history(struct
 drm_dp_mst_topology_ref_history *history,
      if (!history->len)
              goto out;
 
+     /* Get a pointer to the actual MSTB/port so we can the
+memory

address to the kernel log */

drivers/gpu/drm/drm_dp_mst_topology.c +3 -0
+     if (history->type == DRM_DP_MST_TOPOLOGY_HISTORY_PORT)
+             ptr = container_of(history, struct
+drm_dp_mst_port,

topology_ref_history);

drivers/gpu/drm/drm_dp_mst_topology.c +4 -0
+     else
+             ptr = container_of(history, struct
+drm_dp_mst_branch, topology_ref_history);
+
      /* First, sort the list so that it goes from oldest to

newest

       * reference entry
       */
      sort(history->entries, history->len,
sizeof(*history->entries),
           topology_ref_history_cmp, NULL);

- drm_printf(&p, "%s (%p) topology count reached 0,
dumping history:\n",

drivers/gpu/drm/drm_dp_mst_topology.c +11 -1
-                type_str, ptr);
+     if (history->type == DRM_DP_MST_TOPOLOGY_HISTORY_PORT)
+{
+             struct drm_dp_mst_port *port = ptr;
+
+             if (port->connector)
+                     connector_name =
+port->connector->name;
+     }
+     if (connector_name)
+             drm_printf(&p, "Port %s (%p) topology ref
+history:\n",

connector_name, ptr);

drivers/gpu/drm/drm_dp_mst_topology.c +5 -0
+     else
+             drm_printf(&p, "%s (%p) topology ref
+history:\n",
+
+topology_ref_history_type_to_str(history->type),

ptr);

for (i = 0; i < history->len; i++) {
const struct drm_dp_mst_topology_ref_entry
*entry = @@
-
1682,24 +1688,92 @@ __dump_topology_ref_history(struct drm_dp_mst_topology_ref_history *history,

                         ts_nsec, rem_nsec / 1000, buf);
      }

- /* Now free the history, since this is the only time
we expose it */

drivers/gpu/drm/drm_dp_mst_topology.c +31 -1
-     kfree(history->entries);
 out:
      kfree(buf);
 }
 
+/**
+ * drm_dp_mst_dump_topology_refs - helper function for
+dumping the topology ref history
+ * @m: File to print to
+ * @mgr: &struct drm_dp_mst_topology_mgr to use
+ *
+ * Prints the topology ref history of all ports and MSTBs
+on @mgr that are still in memory,
+ * regardless of whether they're actually still reachable
+through the topology or not. Only enabled
+ * when %CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS is enabled.
+Can be implemented by drivers to assist
+ * with debugging leaks in the DP MST helpers.
+ */
+void drm_dp_mst_dump_topology_refs(struct seq_file *m,
+struct drm_dp_mst_topology_mgr *mgr) {
+     struct drm_dp_mst_topology_ref_history *history;
+     struct drm_printer p = drm_seq_file_printer(m);
+
+     mutex_lock(&mgr->topology_ref_history_lock);
+     list_for_each_entry(history,
+&mgr->topology_ref_history_list,
+node)
+             dump_topology_ref_history(history, p);
+     mutex_unlock(&mgr->topology_ref_history_lock);
+}
+EXPORT_SYMBOL(drm_dp_mst_dump_topology_refs);
+
+static void
+__init_topology_ref_history(struct
+drm_dp_mst_topology_ref_history

*history,

drivers/gpu/drm/drm_dp_mst_topology.c +17 -0
+                         struct drm_dp_mst_topology_mgr
+*mgr,
+                         enum
+drm_dp_mst_topology_history_type
+type) {
+     history->type = type;
+     INIT_LIST_HEAD(&history->node);
+
+     mutex_lock(&mgr->topology_ref_history_lock);
+     list_add(&history->node,
+&mgr->topology_ref_history_list);
+     mutex_unlock(&mgr->topology_ref_history_lock);
+}
+
+static void
+__destroy_topology_ref_history(struct
+drm_dp_mst_topology_ref_history

*history,

drivers/gpu/drm/drm_dp_mst_topology.c +42 -0
+                            struct drm_dp_mst_topology_mgr
+*mgr) {
+     mutex_lock(&mgr->topology_ref_history_lock);
+     list_del(&mgr->topology_ref_history_list);
+     mutex_unlock(&mgr->topology_ref_history_lock);
+
+     kfree(history->entries); }
+
+static __always_inline void
+init_port_topology_history(struct drm_dp_mst_topology_mgr
+*mgr, struct drm_dp_mst_port *port) {
+
+__init_topology_ref_history(&port->topology_ref_history,
+mgr,
+
+DRM_DP_MST_TOPOLOGY_HISTORY_PORT);
+}
+
+static __always_inline void
+init_mstb_topology_history(struct drm_dp_mst_topology_mgr
+*mgr, struct drm_dp_mst_branch *mstb) {
+
+__init_topology_ref_history(&mstb->topology_ref_history,
+mgr,
+
+DRM_DP_MST_TOPOLOGY_HISTORY_MSTB);
+}
+
+static __always_inline void
+destroy_port_topology_history(struct drm_dp_mst_port *port)
+{
+
+__destroy_topology_ref_history(&port->topology_ref_history,
+port->mgr); }
+
+static __always_inline void
+destroy_mstb_topology_history(struct drm_dp_mst_branch
+*mstb) {
+
+__destroy_topology_ref_history(&mstb->topology_ref_history,
+mstb->mgr); }
+
 static __always_inline void
 dump_mstb_topology_history(struct drm_dp_mst_branch *mstb)

{
- __dump_topology_ref_history(&mstb->topology_ref_history, mstb,

drivers/gpu/drm/drm_dp_mst_topology.c +2 -2
-                                 "MSTB");
+     dump_topology_ref_history(&mstb->topology_ref_history,
+drm_debug_printer(DBG_PREFIX));
 }
 
 static __always_inline void
 dump_port_topology_history(struct drm_dp_mst_port *port)  {
-

__dump_topology_ref_history(&port->topology_ref_history, port,

drivers/gpu/drm/drm_dp_mst_topology.c +2 -1
-                                 "Port");
+     dump_topology_ref_history(&port->topology_ref_history,
+drm_debug_printer(DBG_PREFIX));
 }
 
 static __always_inline void @@ -1729,6 +1803,14 @@

topology_ref_history_unlock(struct drm_dp_mst_topology_mgr *mgr) } #else static inline void

drivers/gpu/drm/drm_dp_mst_topology.c

Quoted 2 lines
drivers/gpu/drm/drm_dp_mst_topology.c +2 -0
+init_port_topology_history(struct drm_dp_mst_topology_mgr
+*mgr, struct drm_dp_mst_port *port); static inline void

Should also add the bracket, otherwise will get warnings. => static inline void init_port_topology_history(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port) {};

drivers/gpu/drm/drm_dp_mst_topology.c

Quoted 2 lines
drivers/gpu/drm/drm_dp_mst_topology.c +2 -0
+init_mstb_topology_history(struct drm_dp_mst_topology_mgr
+*mgr, struct drm_dp_mst_branch *mstb); static inline void

Same as above
=> static inline void init_mstb_topology_history(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_branch *mstb) {};

drivers/gpu/drm/drm_dp_mst_topology.c

Quoted 3 lines
drivers/gpu/drm/drm_dp_mst_topology.c +3 -0
+destroy_port_topology_history(struct
+drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port
+*port); static inline void

destroy_port_topology_history() takes one parameter only which is port.
=> destroy_port_topology_history(struct drm_dp_mst_port *port) {};

drivers/gpu/drm/drm_dp_mst_topology.c

Quoted 3 lines
drivers/gpu/drm/drm_dp_mst_topology.c +3 -0
+destroy_mstb_topology_history(struct
+drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_branch
+*mstb); static inline void

destroy_mstb_topology_history() takes one parameter only which is mstb => destroy_mstb_topology_history(struct drm_dp_mst_branch
*mstb) {};

Quoted 237 lines

topology_ref_history_lock(struct drm_dp_mst_topology_mgr
*mgr) {} static inline void topology_ref_history_unlock(struct drm_dp_mst_topology_mgr *mgr) {} @@ -1740,6 +1822,25 @@ dump_port_topology_history(struct drm_dp_mst_port *port) {} #define save_port_topology_ref(port, type) #endif

drivers/gpu/drm/drm_dp_mst_topology.c +20 -0
+static struct drm_dp_mst_branch *
+drm_dp_add_mst_branch_device(struct drm_dp_mst_topology_mgr
+*mgr,
+u8 lct, u8 *rad) {
+     struct drm_dp_mst_branch *mstb;
+
+     mstb = kzalloc(sizeof(*mstb), GFP_KERNEL);
+     if (!mstb)
+             return NULL;
+
+     mstb->lct = lct;
+     if (lct > 1)
+             memcpy(mstb->rad, rad, lct / 2);
+     INIT_LIST_HEAD(&mstb->ports);
+     kref_init(&mstb->topology_kref);
+     kref_init(&mstb->malloc_kref);
+     init_mstb_topology_history(mgr, mstb);
+     return mstb;
+}
+
 static void drm_dp_destroy_mst_branch_device(struct kref
*kref) {
      struct drm_dp_mst_branch *mstb = @@ -1747,6 +1848,7 @@
static void drm_dp_destroy_mst_branch_device(struct
kref *kref)
      struct drm_dp_mst_topology_mgr *mgr = mstb->mgr;

      dump_mstb_topology_history(mstb);
drivers/gpu/drm/drm_dp_mst_topology.c +1 -0
+     destroy_mstb_topology_history(mstb);
 
      INIT_LIST_HEAD(&mstb->destroy_next);
@@ -1856,6 +1958,7 @@ static void drm_dp_destroy_port(struct

kref
*kref)

      struct drm_dp_mst_topology_mgr *mgr = port->mgr;

      dump_port_topology_history(port);

+ destroy_port_topology_history(port);

      /* There's nothing that needs locking to destroy an
input port yet */
      if (port->input) {
drivers/gpu/drm/drm_dp_mst_topology.c +2 -1
@@ -2135,7 +2238,7 @@ drm_dp_port_set_pdt(struct
 drm_dp_mst_port *port, u8 new_pdt,
                      ret =
 drm_dp_mst_register_i2c_bus(port);
              } else {
                      lct = drm_dp_calculate_rad(port, rad);
-                     mstb =
 drm_dp_add_mst_branch_device(lct, rad);
+                     mstb =
+drm_dp_add_mst_branch_device(mgr, lct, rad);
                      if (!mstb) {
                              ret = -ENOMEM;
                              drm_err(mgr->dev, "Failed to

create MSTB for port %p", port); @@ -2366,6 +2469,8 @@ drm_dp_mst_add_port(struct drm_device *dev,

       */
      drm_dp_mst_get_mstb_malloc(mstb);
drivers/gpu/drm/drm_dp_mst_topology.c +2 -0
+     init_port_topology_history(mgr, port);
+
      return port;
 }
@@ -3745,7 +3850,7 @@ int

drm_dp_mst_topology_mgr_set_mst(struct
drm_dp_mst_topology_mgr *mgr, bool ms

              }

              /* add initial branch device at LCT 1 */
drivers/gpu/drm/drm_dp_mst_topology.c +2 -1
-             mstb = drm_dp_add_mst_branch_device(1, NULL);
+             mstb = drm_dp_add_mst_branch_device(mgr, 1,
+NULL);
              if (mstb == NULL) {
                      ret = -ENOMEM;
                      goto out_unlock; @@ -5512,14 +5617,16

@@ int drm_dp_mst_topology_mgr_init(struct
drm_dp_mst_topology_mgr *mgr,

      mutex_init(&mgr->delayed_destroy_lock);
      mutex_init(&mgr->up_req_lock);
      mutex_init(&mgr->probe_lock); -#if

IS_ENABLED(CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS)

drivers/gpu/drm/drm_dp_mst_topology.c +5 -2
-     mutex_init(&mgr->topology_ref_history_lock);
-#endif
      INIT_LIST_HEAD(&mgr->tx_msg_downq);
      INIT_LIST_HEAD(&mgr->destroy_port_list);
      INIT_LIST_HEAD(&mgr->destroy_branch_device_list);
      INIT_LIST_HEAD(&mgr->up_req_list);
 
+#if IS_ENABLED(CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS)
+     mutex_init(&mgr->topology_ref_history_lock);
+     INIT_LIST_HEAD(&mgr->topology_ref_history_list);
+#endif
+
      /*
       * delayed_destroy_work will be queued on a dedicated

WQ, so that any
* requeuing will be also flushed when deiniting the
topology manager.

drivers/gpu/drm/nouveau/nouveau_debugfs.c +4 -0
index 1cbe01048b93..53ec7c1daada 100644
@@ -29,9 +29,13 @@
  */
 
 #include <linux/debugfs.h>
+#include <drm/drm_dp_mst_helper.h> #include
+<drm/drm_encoder.h>
 #include <nvif/class.h>
 #include <nvif/if0001.h>
+#include <nouveau_encoder.h>
 #include "nouveau_debugfs.h"
+#include "nouveau_display.h"
 #include "nouveau_drv.h"
 
 static int
@@ -68,6 +72,34 @@ nouveau_debugfs_strap_peek(struct

seq_file *m, void
*data)

      return 0;

}

drivers/gpu/drm/nouveau/nouveau_debugfs.c +30 -0
+#ifdef CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS
+static int nouveau_debugfs_mst_topology_history(struct
+seq_file *m, void *data) {
+     struct drm_info_node *node = m->private;
+     struct drm_device *dev = node->minor->dev;
+     struct drm_encoder *encoder;
+     struct nouveau_display *disp = nouveau_display(dev);
+
+     if (disp->disp.object.oclass < GF110_DISP)
+             return -EINVAL;
+
+     drm_for_each_encoder(encoder, dev) {
+             struct nv50_mstm *mstm;
+
+             /* We need the real encoder for each MST mgr
+*/
+             if (encoder->encoder_type !=
+DRM_MODE_ENCODER_TMDS)
+                     continue;
+             mstm = nouveau_encoder(encoder)->dp.mstm;
+             if (!mstm)
+                     continue;
+
+             seq_printf(m, "MSTM on %s:\n", encoder->name);
+             drm_dp_mst_dump_topology_refs(m, &mstm->mgr);
+     }
+     return 0;
+}
+#endif
+
 static int
 nouveau_debugfs_pstate_get(struct seq_file *m, void *data)

{ @@
-213,6
+245,9 @@ static const struct file_operations nouveau_pstate_fops = { static struct drm_info_list nouveau_debugfs_list[] = {
{ "vbios.rom", nouveau_debugfs_vbios_image, 0, NULL
},
{ "strap_peek", nouveau_debugfs_strap_peek, 0, NULL },

drivers/gpu/drm/nouveau/nouveau_debugfs.c +3 -0
+#ifdef CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS
+     { "dp_mst_topology_refs",
+nouveau_debugfs_mst_topology_history, 0, NULL }, #endif
 };
 #define NOUVEAU_DEBUGFS_ENTRIES

ARRAY_SIZE(nouveau_debugfs_list)

(quoted diff) +0 -0

b/include/drm/drm_dp_mst_helper.h index
78044ac5b59b..e3a73d8d97c0
100644

include/drm/drm_dp_mst_helper.h +8 -1
@@ -35,6 +35,11 @@ enum drm_dp_mst_topology_ref_type {
      DRM_DP_MST_TOPOLOGY_REF_PUT,
 };
 
+enum drm_dp_mst_topology_history_type {
+     DRM_DP_MST_TOPOLOGY_HISTORY_PORT,
+     DRM_DP_MST_TOPOLOGY_HISTORY_MSTB, };
+
 struct drm_dp_mst_topology_ref_history {
      struct drm_dp_mst_topology_ref_entry {
              enum drm_dp_mst_topology_ref_type type; @@
-43,6
+48,9 @@ struct drm_dp_mst_topology_ref_history {
              depot_stack_handle_t backtrace;
      } *entries;
      int len;
+
+     enum drm_dp_mst_topology_history_type type;
+     struct list_head node;
 };
 #endif /* IS_ENABLED(CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS)

*/

include/drm/drm_dp_mst_helper.h +4 -0
@@ -769,6 +777,12 @@ struct drm_dp_mst_topology_mgr {
       * &drm_dp_mst_branch.topology_ref_history.
       */
      struct mutex topology_ref_history_lock;
+
+     /**
+      * @topology_ref_history_list: contains a list of
+topology ref

histories for any MSTBs or

include/drm/drm_dp_mst_helper.h +7 -0
+      * ports that are still in memory
+      */
+     struct list_head topology_ref_history_list;
 #endif
 };
@@ -873,6 +887,10 @@ void drm_dp_mst_put_port_malloc(struct
 drm_dp_mst_port *port);
 
 struct drm_dp_aux *drm_dp_mst_dsc_aux_for_port(struct
 drm_dp_mst_port *port);
 
+#ifdef CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS
+void drm_dp_mst_dump_topology_refs(struct seq_file *m,
+struct drm_dp_mst_topology_mgr *mgr); #endif
+
 extern const struct drm_private_state_funcs

drm_dp_mst_topology_state_funcs;

Quoted 4 lines
Quoted 3 lines

/**
--
2.31.1

--
Cheers,
Lyude Paul (she/her)
Software Engineer at Red Hat

Quoted 2 lines

--
Cheers, Lyude Paul (she/her) Software Engineer at Red Hat

--
Regards,
Wayne Lin

author
#9 lore raw

JFYI I should be able to respond to this soon, I think I'm following your issue a bit more (haven't gotten to go through the patches too so I'm probably going to see if I can reproduce it locally as well. Sorry for the delay - dealing with some X.org stuff and work issues at the moment

Quoted 1,204 lines · Wayne

[Public]

Hey Lyude,

Sorry, just got chance to get back to this and BIG THANKS to elaborating in such details!
Following are some of my comments : )

Quoted 10 lines

-----Original Message-----
From: Lyude Paul <[email protected]>
Sent: Wednesday, January 19, 2022 9:59 AM
To: Lin, Wayne <[email protected]>
Cc: [email protected]
Subject: Re: [PATCH] WIP: drm/dp_mst: Add support for dumping topology ref histories from debugfs

Hey - so, the original issue here was that payloads were not always deleted when we expected them to be - correct?

Actually, what I was trying to do is not relevant to payload maintenance. What I wanted to do is having a way to notify
amdgpu to release its enumerated resource "dc_sink" when unplug event occurs. Currently, under specific scenarios,
we can't release remote "dc_sink" and ends up can't allocate resource for new connected monitors. I'll explain the scenario
underneath.

So, get back to the main issue that I was trying to solve when I pushed the patchset:
https://patchwork.kernel.org/project/dri-devel/cover/[email protected]/

I was trying to fix some problems that I observed after these 2 patches
* 09b974e8983 drm/amd/amdgpu_dm/mst: Remove ->destroy_connector() callback
* 72dc0f51591 drm/dp_mst: Remove drm_dp_mst_topology_cbs.destroy_connector

With above patches, it changes our flow to remove dc_sink (by calling dc_link_remote_sink()) when connector is about to be destroyed. And let's see below use cases:
a). Disconnection happens between mst branch devices
e.g.
src - 1st_mstb - 2nd_mstb - sst_monitor => src - 1st_mstb (disconnect) 2nd_mstb - sst_monitor

In above case, after receiving CSN, we will put topology reference count of 2nd mstb and its port which connected to the sst monitor.
As you suggested before, we will destroy 2nd mst branch device and its all ports due to there is no more mst path can route to them.
As the result of that, we at end call drm_dp_delayed_destroy_port() and unregister/put the drm connector and eventually call dc_link_remote_sink() to release the dc_sinck we enumerate for the specific stream sink of sst_monitor.

However, in below case:
b). Disconnection happens between mst branch devices and SST monitor e.g.
src - 1st_mstb - sst_monitor => src - 1st_mstb (disconnect)  sst_monitor

In this case, which is the case having problem, it definitely won't decrease the topology references count of the port of 1st_mstb which
was connected to the sst monitor to zero since the port is still existing in the topology. Same as the malloc reference since the port can't get destroyed. Hence, the port still exists and we won't call drm_dp_delayed_destroy_port() to unregister and put the drm connector. As the
result of that, we can't release corresponding remote dc_sink under this case. And after few times hotplugs, we can't create any new dc_sink since the number of stream sink is exceeding our limitation.

Hence, what I'm thinking here is to register a callback function for notifying us that the remote sink is detached. This just aligns what we do for handling long HPD event (unplug monitor from system) of legacy DP (none mst). For which case, once we detect long HPD event
indicating the monitor is detached, we will immediately try to release the dc_link->local_sink and fire hotplug event to upper layer.
Same as here, once receives a CSN message notifying a drm connector is changed from connected to disconnected, trigger the callback function and we can try to release the dc_sink resource.

Quoted 30 lines

If I'm remembering that correctly, then (and I'm not 100% sure on this yet) I might already have noticed something very possibly wonky in how we handle payload allocations currently, that I found while working on the non-atomic removal branch that I linked to you in my previous response. Basically, in the
step1() function it looks like that we follow this general flow with updating
payloads:

 * Loop through proposed payloads and compare to previously programmed payloads
    - If a payload has a different allocation then it previously did, update the
      payload
    - If the payload is new, create it
    - If a payload no longer has an allocation, remove the payload

At first glance this seems totally correct - but I'm not actually entirely convinced it is? Particularly because it seems like the order in which we do creation/deletion of payloads is totally arbitrary. To explain what I mean by that, consider a state transition like this:

vcpi_slots=15 vcpi_slots=35 vcpi_slots=14

Quoted 1 line

1 | 2 |xxxxxxxx|

Let's say we want to increase payload #1 from 15 to 50, and disable payload #2 in the same atomic commit on DRM's side. If the order we update payloads is entirely arbitrary, we could end up doing this:

Quoted 8 lines

 * Increase VCPI slots payload #1 from 15->50 (total VCPI slots=99, overflow!)
 * Decrease VCPI slots payload #2 from 35->0  (total VCPI slots=50)

Notice on the first step, we've technically overflowed the available number of VCPI slots in the payload table. This is still before step 2 though, and I could be totally wrong here - perhaps this is entirely OK in the real world, and we're allowed to overflow VC slots as long as

I think it's forbidden to allocate time slots beyond 64 when we're updating payload ID table of the branch in the real world. Branch probably will NACK the DPCD write.

Quoted 5 lines

we repair the issue before step 2. But so far I can't seem to find anything in the DP 2.0 spec that explicitly states this would be OK - which
makes me think we might fail some payload allocations if we don't always ensure we avoid overflows in between VC payload table changes

Agree.
For amdgpu, I think we always do deallocation (call hws-

Quoted 1 line

funcs.reset_hw_ctx_wrap()) before allocation

(apply_single_controller_ctx_to_hw()).

Quoted 12 lines

Note that avoiding overflows would be as simple as just making sure we send all VC payload table changes that free VC slots before sending any that take new slots.

Again - I haven't actually confirmed this yet and am hoping to test stuff like this very soon as I'm pretty close finishing up the initial attempt at removing the non-atomic MST code in the DRM helpers now. If my theory ends up being correct here, I can fix this in my rewrite of the MST payload management code. But I figured it might be worth mentioning in the mean time in case you think it might be relevant to the problem here :).

Quoted 71 lines
Quoted 70 lines · Lyude Paul

(CC'ing this to dri-devel, since this is basically patch review)

Alright - so first off sorry about the broken debugging patch! I thought I had tested that but I guess I hadn't tested it well enough, I'll get it fixed asap, but feel free to try getting to it before I do.

As for this patch... (comments below)

Quoted 46 lines · Wayne

[AMD Official Use Only]

Hi Lyude,

No rush and thanks for your time! : ) Just want to help clarify what I mean that "registering a callback function"
for CSN of disconnecting
stream sink device (not mst branch case). Attach below the tentative patch that I planned to do. Thanks so much!

Regards,
Wayne
---
 .../display/amdgpu_dm/amdgpu_dm_mst_types.c   | 53
+++++++++++++++++++
 drivers/gpu/drm/drm_dp_mst_topology.c         | 16 +++++- include/drm/drm_dp_mst_helper.h               |  1 +

 3 files changed, 68 insertions(+), 2 deletions(-)

diff --git a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_mst_types.c b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_mst_types.c index cc34a35d0bcb..d7343c64908c 100644

drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_mst_types.c +16 -0
@@ -472,8 +472,61 @@ dm_dp_add_mst_connector(struct
 drm_dp_mst_topology_mgr *mgr,
         return connector;
  }
 
+void dm_dp_notify_csn_disconnection(struct drm_connector
+*connector) {
+       struct amdgpu_dm_connector *aconnector =
+               to_amdgpu_dm_connector(connector);
+       struct dc_sink *dc_sink = aconnector->dc_sink;
+       struct dc_link *dc_link = aconnector->dc_link;
+       struct amdgpu_device *adev = drm_to_adev(ddev);
+
+       ASSERT(dc_link);
+
+       if (dc_sink) {
+               mutex_lock(&adev->dm.dc_lock);
+
+               /*clear the remote sink of the link*/
+               dc_link_remove_remote_sink(dc_link, dc_sink);
+               dc_sink_release(dc_sink);

drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_mst_types.c dm_dp_add_mst_connector(struct · line 501

Quoted 10 lines
drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_mst_types.c +7 -0
+               aconnector->dc_sink = NULL;
+
+               mutex_unlock(&adev->dm.dc_lock);
+       }
+}
+
  static const struct drm_dp_mst_topology_cbs dm_mst_cbs = {
         .add_connector = dm_dp_add_mst_connector,
+       .notify_csn_disconnection = dm_dp_notify_csn_disconnection,
  };

I still don't really think this is a good idea. This seems like it's just adding another hotplugging path to the code in order to avoid sending hotplugs for non-endpoint devices. In addition to the

So, what I wanted to do is to add a callback function for endpoint devices case as described above. For disconnection of non-endpoint devices is already taken care by current code.

Quoted 8 lines
Quoted 8 lines

drm_connector issues I mentioned before, we also really need to stop doing any kind of payload maintence in hotplugging paths. The reality is any kind of payload maintanence we do outside of normal modesetting paths is a relic from legacy modesetting that I'm dropping ASAP (more on this below), and we can't keep adding to it because it dramatically complicates maintanence as well.

Quoted 14 lines
Quoted 6 lines

Sorry for repeating this point so often but - the biggest issue too is I'm still not sure what it is we're even avoiding here. We know resources aren't released consistently, and that we're able to reproduce the behavior with repeated hotplugs. We also know that if we skip sending certain hotplug events, that fixes the issue. And we know we can workaround it by adding a special case for forcing a payload

Quoted 8 lines

release in DC. But none of those actually tell us exactly what piece of code is leaking and why, which means that any workarounds we're putting in to avoid this mysterious guilty section of code we don't entirely understand either - which means we're just adding more code in that no one actually fully understands. This just ends up making maintence difficult because every change in code nearby workarounds like this has to strugle to try to figure out said workarounds in order to avoid breaking things.

Thank you Lyude for the reminder and I totally agree with you by avoiding adding workaround since it will get the code maintenance harder. And I think the root cause of the issue that I observed before is quite concrete. There is no code path for us to release dc_sink under the unplug scenario that I described above.

Quoted 57 lines
Quoted 57 lines

I'm actually currently running into these "later" issues right now, as recently I've (-actively-, finally!!!) been working on trying to remove as much non-atomic MST as I can because. As it turns out - a huge amount of the payload maintanence code just isn't needed at all when we stop caring about non-atomic drivers and stick everything in atomic state structs. Step 1 for updating updating the payload table, e.g. drm_dp_update_payload_part1(), is a great example of how messy things have become. Here's a small sample of some of the stuff I've seen from just that one function so far that either just don't make sense here or is totally redundant. I should note that a lot of these things also come from patches I reviwed, but didn't really look at as closely as I should have because I was swamped at work, some are historical artifacts, and others are less-than-ideal patches I got wrote myself when I was first started working on MST and didn't know the code as well as I do
now:

 * We try to avoid some sort of userspace issue by using drm_dp_mst_port_downstream_of_branch() to avoid releasing payloads for a
   branch if we can't prove it's downstream of the top of the topology. This
   seems to workaround either a userspace bug. This is a redundant, since
   that's what topology refs are already supposed to be doing to the extent is
   reasonably possibly. It's also unfortunately racy solution because we have
   to be able to handle the situation where a connector is removed from under
   us. That can happen at any time, including _immediately_ after we call
   drm_dp_mst_port_downstream_of_branch() - rendering the call not really
   useful.
 * If we fail to validate the sink in drm_dp_update_payload_part, we don't
   update the payload table. I think at best this solution is racy and not
   useful, at worst it leaves us with a payload table that doesn't match what
   we attempted to set in the atomic state - which at worst brings us into
   undefined territory where we're just plain out of sync with the reality in
   hw.
 * Actually fun fact - mgr->payloads and mgr->proposed_vcpis both can and
   definitely should be removed entirely. All of the info for mgr->payloads
   could just be in the atomic state, because that + the magic of atomic state
   duplication means we'll also have an accurate view of the previous state's
   payload allocations: which renders mgr->proposed_vcpis redundant.

Quoted 1 line
Quoted 1 line

Apologies for the long explanation again, but I hope that explains my

Really really thanks for your kindly help on this : ) I'll try my best to get to your WIP branch soon. Thank you Lyude!

Quoted 887 lines
Quoted 23 lines

point here a bit. I'm going to be trying to get to moving amdgpu's DSC code out of amdgpu and into DRM helpers as well soon, so I'm really determined to clean stuff up beforehand as every time I've done so it's become substantially easier to make changes to this code. Things used to be even worse before I started cleaning things up 2 or 3 years ago, where simple changes would end up getting me stuck spending hours trying to dig through lockdep or memory manegement issues. As well, I would be entirely unsurprised if bugs like this very payload leak we're working on just disappear once we've gotten rid of all the extraneous workarounds and state tracking here - especially with how many special cases we have for maintaining the payload table right now. That's certainly ended up being the case in the past with a number of other difficult to track down issues I've dealt with in MST.

Anyhow. I've been way more productive this year then last because I had over a month off work and am finally not super burnt out from my job, and so far I've been making progress on the payload state cleanup far faster then I was last year :). I think if you can't figure out where the leak is coming from even once I get the debugging patches I mentioned fixed up, it might be a good idea for us to try again after I've got some of this code cleaned up. I've got a currently WIP branch here:

Quoted 863 lines

https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgitl
ab.freedesktop.org%2Flyudess%2Flinux%2F-%2Fcommit%2F624dd68fa804e64b5b 2060e4735d7317090692b5&amp;data=04%7C01%7Cwayne.lin%40amd.com%7Cd4bdc7 59eb274bfccc8208d9daef41fe%7C3dd8961fe4884e608e11a82d994e183d%7C0%7C0% 7C637781543416430632%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQI joiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&amp;sdata=nFpVl%2BVoF 40JPabfRMcR6Cz7ZHDwt%2BBpLDBNdXftJaA%3D&amp;reserved=0

Quoted 854 lines

 void amdgpu_dm_initialize_dp_connector(struct
amdgpu_display_manager *dm, diff --git
a/drivers/gpu/drm/drm_dp_mst_topology.c
b/drivers/gpu/drm/drm_dp_mst_topology.c
index 857c5d15e81d..a70e26c5d084 100644

drivers/gpu/drm/drm_dp_mst_topology.c +1 -0
@@ -2508,6 +2508,7 @@ drm_dp_mst_handle_conn_stat(struct
 drm_dp_mst_branch *mstb,
         u8 new_pdt;
         bool new_mcs;
         bool dowork = false, create_connector = false;
+       bool notify_unplug_event = false;
 
         port = drm_dp_get_port(mstb, conn_stat->port_number);

        if (!port)

drivers/gpu/drm/drm_dp_mst_topology.c +0 -0
@@ -2520,6 +2521,9 @@ drm_dp_mst_handle_conn_stat(struct
 drm_dp_mst_branch *mstb,
                          * port, so just throw the port out and make

sure we
                         * reprobe the link address of it's parent MSTB
                         */
+                       /* should also consider notify_unplug_event here.
+                        * but it's not a normal case for products +in the
market
+                        */ drm_dp_mst_topology_unlink_port(mgr, port); mstb->link_address_sent = false; dowork = true; @@ -2541,10 +2545,14 @@ drm_dp_mst_handle_conn_stat(struct
drm_dp_mst_branch
*mstb,
        port->ddps = conn_stat->displayport_device_plug_status;

        if (old_ddps != port->ddps) {
-               if (port->ddps && !port->input)
+               if (port->ddps && !port->input) { drm_dp_send_enum_path_resources(mgr, mstb, port);
-               else
+               } else {
                        port->full_pbn = 0;
+                       if (port->connector &&
+                               drm_dp_mst_is_end_device(port->pdt, +port-

Quoted 1 line

mcs))

+                               notify_unplug_event = true; +               }
        }

        new_pdt = port->input ? DP_PEER_DEVICE_NONE : conn_stat-

Quoted 1 line

peer_device_type;

drivers/gpu/drm/drm_dp_mst_topology.c +0 -0
@@ -2557,11 +2565,15 @@ drm_dp_mst_handle_conn_stat(struct

drm_dp_mst_branch
*mstb,
                dowork = false;
        }

+       if (notify_unplug_event &&

drivers/gpu/drm/drm_dp_mst_topology.c +3 -0
+mgr->cbs->notify_csn_disconnection)
+               mgr->cbs->notify_csn_disconnection(port->connector);
+

        if (port->connector) drm_modeset_unlock(&mgr->base.lock); else if (create_connector) drm_dp_mst_port_add_connector(mstb, port);

+
 out:
        drm_dp_mst_topology_put_port(port);
        if (dowork)

(quoted diff) +0 -0

b/include/drm/drm_dp_mst_helper.h index 78044ac5b59b..ff9e47729841 100644

include/drm/drm_dp_mst_helper.h +2 -0
@@ -525,6 +525,7 @@ struct drm_dp_mst_topology_cbs {
          * IRQ pulse handler.
          */
         void (*poll_hpd_irq)(struct drm_dp_mst_topology_mgr *mgr);
+       void (*notify_csn_disconnection)(struct drm_connector
+*connector);
  };
 

 #define DP_MAX_PAYLOAD (sizeof(unsigned long) * 8) --
2.31.0

Quoted 761 lines

-----Original Message-----
From: Lin, Wayne
Sent: Wednesday, December 8, 2021 11:39 AM
To: 'Lyude Paul' <[email protected]>
Subject: RE: [PATCH] WIP: drm/dp_mst: Add support for dumping topology ref histories from debugfs

No worries Lyude!
Thanks for keeping helping on this. Take your time : )

Quoted 751 lines

-----Original Message-----
From: Lyude Paul <[email protected]>
Sent: Wednesday, December 8, 2021 7:05 AM
To: Lin, Wayne <[email protected]>
Subject: Re: [PATCH] WIP: drm/dp_mst: Add support for dumping topology ref histories from debugfs

Sorry! I will try to get to this tomorrow, if not then sometime this week.

Quoted 731 lines · Wayne

[Public]

Hi Lyude,

Finally have some bandwidth to get back to this problem! I roughly went through this patch and I'm just aware that we already have such kind of convenient tool for a while. I think it's definitely useful for us to track port/mstb reference count issues and I'll start to embrace this feature for cleaning up those issues. Thank you Lyude!

However, I think the issue that I was trying to fix is not related to what you suggested:
" The idea here is that if stream resources aren't being released, my guess would be that we're not dropping topology references for the port which means the connector never goes away."
The issue I was trying to fix is about releasing dc_link->remote_sinks while receiving a CSN message notifying the connection status of a sst connector of a port changed from connected to disconnected. Not the connection status changed of a mst branch device.
e.g.
src - 1st_mstb - 2nd_mstb - sst_monitor => src - 1st_mstb (disconnect) 2nd_mstb - sst_monitor

In above case, after receiving CSN, we will put topology references of 2nd mstb and its port which is connected with the sst monitor. As the result of that, we can call drm_dp_delayed_destroy_port() to unregister and put the drm connector.

However, in below case:
e.g.
src - 1st_mstb - sst_monitor => src - 1st_mstb (disconnect) sst_monitor

In this case, which is the case having problem, it definitely won't decrease the topology references count of the port which was connected to the sst monitor to zero since the port is still existing in the topology. Same as the malloc reference since the port can't get destroyed. Hence, the port still exists  and we won't call drm_dp_delayed_destroy_port() to unregister and put the drm connector.
I looked up the code and drm_dp_delayed_destroy_port() seems like the only place to call drm_connector_put() which means we can't put reference count of drm connector under this case and can't release dc_sink resource by destroying drm connector.

I would also like to point out that this resource (remote_sinks) is specific to different stream sinks. So if we're trying to release this dc_sink resource by destroying the drm connector, it conflicts the idea that you suggested before that we should always keep the drm connector until it's no longer reachable in the topology.
Releasing dc_sink should be binding with the disconnection event.

I understand your concern that we should not just easily change the logic here since it's the result after solving tons of bugs before and might cause other side effect. So, just my 2 cents, what I'm thinking is to register a callback function for our driver to notify us that the remote sink is detached. This just aligns our flow handling long HPD event of legacy (sst) DP.
For sst case, once we detect long HPD event indicating the monitor is detached, we will immediately try to release the dc_link->local_sink and fire hotplug event to upper layer. Same as here, once receives a CSN message notifying a drm connector is changed from connected to disconnected, trigger the callback function and we can try to release the dc_sink resource.

Would like to know your thought and insight please : )

Btw, I got some errors and warnings while building and have some code adjustments as below : ) Thank you Lyude for your always kindly help!!

Regards,
Wayne

Quoted 382 lines

-----Original Message-----
From: Lyude Paul <[email protected]>
Sent: Wednesday, November 3, 2021 7:15 AM
To: Lin, Wayne <[email protected]>
Subject: [PATCH] WIP: drm/dp_mst: Add support for dumping topology ref histories from debugfs

TODO:
* Implement support for i915
* Finish writing this commit message
* ???
* profit

---

Hey wayne! SO-hopefully if I did this correctly then this should just work on amdgpu. What this patch should do is add a debugfs file to amdgpu called "amdgpu_dp_mst_topology_refs", and when you read the file it should print out the topology reference history of every MSTB and Port in memory, along with how many times we've hit the codepath in each backtrace. An example:

Port DP-5 (0000000005c37748) topology ref history:
  1 gets (last at    58.468973):

     drm_dp_send_link_address+0x6a5/0xa00 [drm_kms_helper]
     drm_dp_check_and_send_link_address+0xad/0xd0
[drm_kms_helper]
     drm_dp_mst_link_probe_work+0x14e/0x1a0 [drm_kms_helper]
     process_one_work+0x1e3/0x390
     worker_thread+0x50/0x3a0
     kthread+0x124/0x150
     ret_from_fork+0x1f/0x30
  1 puts (last at    58.469357):
     drm_dp_mst_topology_put_port+0x6a/0x210
[drm_kms_helper]
     drm_dp_send_link_address+0x39e/0xa00 [drm_kms_helper]
     drm_dp_check_and_send_link_address+0xad/0xd0
[drm_kms_helper]
     drm_dp_mst_link_probe_work+0x14e/0x1a0 [drm_kms_helper]
     process_one_work+0x1e3/0x390
     worker_thread+0x50/0x3a0
     kthread+0x124/0x150
     ret_from_fork+0x1f/0x30

The idea here is that if stream resources aren't being released, my guess would be that we're not dropping topology references for the port which means the connector never goes away. So, if that's really the case then once we unplug the offending connector we should be able to find a pair of gets/puts for the offending leaked connector where the get/put count doesn't match up. Also, if the frame count on the backtrace isn't long enough you can increase the value of STACK_DEPTH in drivers/gpu/drm/drm_dp_mst_topology.c and recompile to get more frames.

To enable this, first enable CONFIG_EXPERT for your kernel which will unhide CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS. Then just enable CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS, recompile, and it should be good to go.

Let me know if this works for you, and hopefully this should tell us exactly what the problem actually is here.

 .../amd/display/amdgpu_dm/amdgpu_dm_debugfs.c |  35 ++++ drivers/gpu/drm/drm_dp_mst_topology.c         | 173
++++++++++++++----
 drivers/gpu/drm/nouveau/nouveau_debugfs.c     |  35 ++++ include/drm/drm_dp_mst_helper.h               |  18 ++

 4 files changed, 228 insertions(+), 33 deletions(-)

diff --git a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_debugfs.c b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_debugfs.c index 1a68a674913c..1a14732c52b4 100644
---
a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_debugfs.c +++ b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_debugf +++ s.c

(quoted diff) +0 -0
@@ -3063,6 +3063,37 @@ static int mst_topo_show(struct

seq_file *m, void
*unused)
      return 0;
 }

(quoted diff) +14 -0
+#ifdef CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS
+static int mst_topology_ref_dump_show(struct seq_file *m,
+void
+*unused) {
+     struct amdgpu_device *adev = (struct amdgpu_device
+*)m->private;
+     struct drm_device *dev = adev_to_drm(adev);
+     struct drm_connector *connector;
+     struct drm_connector_list_iter conn_iter;
+     struct amdgpu_dm_connector *aconnector;
+
+     drm_connector_list_iter_begin(dev, &conn_iter);
+     drm_for_each_connector_iter(connector, &conn_iter) {
+             if (connector->connector_type !=

DRM_MODE_CONNECTOR_DisplayPort)
+                     continue;

(quoted diff) +6 -0
+
+             aconnector =
+to_amdgpu_dm_connector(connector);
+
+             /* Ensure we're only dumping the topology of a
+root mst node

*/
+             if (!aconnector->mst_mgr.max_payloads) +                     continue;
+
+             seq_printf(m, "\nMST topology for connector +%d\n",
aconnector->connector_id);
+             drm_dp_mst_dump_topology_refs(m, +&aconnector->mst_mgr);
+     }
+     drm_connector_list_iter_end(&conn_iter);

(quoted diff) +7 -0
+
+     return 0;
+}
+
+DEFINE_SHOW_ATTRIBUTE(mst_topology_ref_dump);
+#endif
+

 /*
  * Sets trigger hpd for MST topologies.
  * All connected connectors will be rediscovered and re started as needed if val of 1 is sent.

(quoted diff) +0 -0
@@ -3299,6 +3330,10 @@ void dtn_debugfs_init(struct

amdgpu_device
*adev)

      debugfs_create_file("amdgpu_mst_topology", 0444, root, adev, &mst_topo_fops);

(quoted diff) +5 -0
+#ifdef CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS
+     debugfs_create_file("amdgpu_dp_mst_topology_refs",
+0444, root, adev,
+                         &mst_topology_ref_dump_fops);
+#endif

      debugfs_create_file("amdgpu_dm_dtn_log", 0644, root, adev,
                          &dtn_log_fops);

drivers/gpu/drm/drm_dp_mst_topology.c +0 -16
index 1aa8702383d4..0159828c494d 100644
@@ -1366,23 +1366,6 @@ static int
 drm_dp_mst_wait_tx_reply(struct drm_dp_mst_branch *mstb,
       return ret;
  }
 
-static struct drm_dp_mst_branch
 *drm_dp_add_mst_branch_device(u8 lct, u8
 *rad) -{
-     struct drm_dp_mst_branch *mstb;
-
-     mstb = kzalloc(sizeof(*mstb), GFP_KERNEL);
-     if (!mstb)
-             return NULL;
-
-     mstb->lct = lct;
-     if (lct > 1)
-             memcpy(mstb->rad, rad, lct / 2);
-     INIT_LIST_HEAD(&mstb->ports);
-     kref_init(&mstb->topology_kref);
-     kref_init(&mstb->malloc_kref);
-     return mstb;
-}
-

 static void drm_dp_free_mst_branch_device(struct kref *kref)  {
      struct drm_dp_mst_branch *mstb = @@ -1642,12 +1625,20 @@ topology_ref_type_to_str(enum drm_dp_mst_topology_ref_type type)
              return "put";
 }

drivers/gpu/drm/drm_dp_mst_topology.c +7 -0
+static const char *topology_ref_history_type_to_str(enum
+drm_dp_mst_topology_history_type type) {
+     if (type == DRM_DP_MST_TOPOLOGY_HISTORY_PORT)
+             return "Port";
+     else
+             return "MSTB"; }
+

 static void
-__dump_topology_ref_history(struct drm_dp_mst_topology_ref_history *history,
-                         void *ptr, const char *type_str)

drivers/gpu/drm/drm_dp_mst_topology.c +3 -0
+dump_topology_ref_history(struct
+drm_dp_mst_topology_ref_history *history, struct
+drm_printer p)

 {
-     struct drm_printer p = drm_debug_printer(DBG_PREFIX); +     char *connector_name = NULL;
      char *buf = kzalloc(PAGE_SIZE, GFP_KERNEL); +     void *ptr;
      int i;

      if (!buf)

drivers/gpu/drm/drm_dp_mst_topology.c +2 -0
@@ -1656,14 +1647,29 @@ __dump_topology_ref_history(struct
 drm_dp_mst_topology_ref_history *history,
       if (!history->len)
               goto out;
 
+     /* Get a pointer to the actual MSTB/port so we can the
+memory

address to the kernel log */
+     if (history->type == DRM_DP_MST_TOPOLOGY_HISTORY_PORT) +             ptr = container_of(history, struct +drm_dp_mst_port,
topology_ref_history);
+     else
+             ptr = container_of(history, struct

drivers/gpu/drm/drm_dp_mst_topology.c +2 -0
+drm_dp_mst_branch, topology_ref_history);
+

      /* First, sort the list so that it goes from oldest to newest
       * reference entry
       */
      sort(history->entries, history->len, sizeof(*history->entries), topology_ref_history_cmp, NULL);

-     drm_printf(&p, "%s (%p) topology count reached 0, dumping history:\n",
-                type_str, ptr);
+     if (history->type == DRM_DP_MST_TOPOLOGY_HISTORY_PORT)

drivers/gpu/drm/drm_dp_mst_topology.c +10 -0
+{
+             struct drm_dp_mst_port *port = ptr;
+
+             if (port->connector)
+                     connector_name =
+port->connector->name;
+     }
+     if (connector_name)
+             drm_printf(&p, "Port %s (%p) topology ref
+history:\n",

connector_name, ptr);
+     else
+             drm_printf(&p, "%s (%p) topology ref

drivers/gpu/drm/drm_dp_mst_topology.c +3 -0
+history:\n",
+
+topology_ref_history_type_to_str(history->type),

ptr);

      for (i = 0; i < history->len; i++) { const struct drm_dp_mst_topology_ref_entry *entry = @@
-
1682,24 +1688,92 @@ __dump_topology_ref_history(struct drm_dp_mst_topology_ref_history *history, ts_nsec, rem_nsec / 1000, buf); }

-     /* Now free the history, since this is the only time we expose it */
-     kfree(history->entries);
 out:
      kfree(buf);
 }

drivers/gpu/drm/drm_dp_mst_topology.c +31 -0
+/**
+ * drm_dp_mst_dump_topology_refs - helper function for
+dumping the topology ref history
+ * @m: File to print to
+ * @mgr: &struct drm_dp_mst_topology_mgr to use
+ *
+ * Prints the topology ref history of all ports and MSTBs
+on @mgr that are still in memory,
+ * regardless of whether they're actually still reachable
+through the topology or not. Only enabled
+ * when %CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS is enabled.
+Can be implemented by drivers to assist
+ * with debugging leaks in the DP MST helpers.
+ */
+void drm_dp_mst_dump_topology_refs(struct seq_file *m,
+struct drm_dp_mst_topology_mgr *mgr) {
+     struct drm_dp_mst_topology_ref_history *history;
+     struct drm_printer p = drm_seq_file_printer(m);
+
+     mutex_lock(&mgr->topology_ref_history_lock);
+     list_for_each_entry(history,
+&mgr->topology_ref_history_list,
+node)
+             dump_topology_ref_history(history, p);
+     mutex_unlock(&mgr->topology_ref_history_lock);
+}
+EXPORT_SYMBOL(drm_dp_mst_dump_topology_refs);
+
+static void
+__init_topology_ref_history(struct
+drm_dp_mst_topology_ref_history

*history,
+                         struct drm_dp_mst_topology_mgr

drivers/gpu/drm/drm_dp_mst_topology.c +16 -0
+*mgr,
+                         enum
+drm_dp_mst_topology_history_type
+type) {
+     history->type = type;
+     INIT_LIST_HEAD(&history->node);
+
+     mutex_lock(&mgr->topology_ref_history_lock);
+     list_add(&history->node,
+&mgr->topology_ref_history_list);
+     mutex_unlock(&mgr->topology_ref_history_lock);
+}
+
+static void
+__destroy_topology_ref_history(struct
+drm_dp_mst_topology_ref_history

*history,
+                            struct drm_dp_mst_topology_mgr

drivers/gpu/drm/drm_dp_mst_topology.c +41 -0
+*mgr) {
+     mutex_lock(&mgr->topology_ref_history_lock);
+     list_del(&mgr->topology_ref_history_list);
+     mutex_unlock(&mgr->topology_ref_history_lock);
+
+     kfree(history->entries); }
+
+static __always_inline void
+init_port_topology_history(struct drm_dp_mst_topology_mgr
+*mgr, struct drm_dp_mst_port *port) {
+
+__init_topology_ref_history(&port->topology_ref_history,
+mgr,
+
+DRM_DP_MST_TOPOLOGY_HISTORY_PORT);
+}
+
+static __always_inline void
+init_mstb_topology_history(struct drm_dp_mst_topology_mgr
+*mgr, struct drm_dp_mst_branch *mstb) {
+
+__init_topology_ref_history(&mstb->topology_ref_history,
+mgr,
+
+DRM_DP_MST_TOPOLOGY_HISTORY_MSTB);
+}
+
+static __always_inline void
+destroy_port_topology_history(struct drm_dp_mst_port *port)
+{
+
+__destroy_topology_ref_history(&port->topology_ref_history,
+port->mgr); }
+
+static __always_inline void
+destroy_mstb_topology_history(struct drm_dp_mst_branch
+*mstb) {
+
+__destroy_topology_ref_history(&mstb->topology_ref_history,
+mstb->mgr); }
+

 static __always_inline void dump_mstb_topology_history(struct drm_dp_mst_branch *mstb) {
- __dump_topology_ref_history(&mstb->topology_ref_history, mstb,
-                                 "MSTB");
+     dump_topology_ref_history(&mstb->topology_ref_history, +drm_debug_printer(DBG_PREFIX));
 }

 static __always_inline void dump_port_topology_history(struct drm_dp_mst_port *port)  { - __dump_topology_ref_history(&port->topology_ref_history, port,
-                                 "Port");
+     dump_topology_ref_history(&port->topology_ref_history, +drm_debug_printer(DBG_PREFIX));
 }

 static __always_inline void @@ -1729,6 +1803,14 @@ topology_ref_history_unlock(struct drm_dp_mst_topology_mgr *mgr)  }  #else  static inline void

drivers/gpu/drm/drm_dp_mst_topology.c

Quoted 2 lines
drivers/gpu/drm/drm_dp_mst_topology.c +2 -0
+init_port_topology_history(struct drm_dp_mst_topology_mgr
+*mgr, struct drm_dp_mst_port *port); static inline void

Should also add the bracket, otherwise will get warnings. => static inline void init_port_topology_history(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port) {};

drivers/gpu/drm/drm_dp_mst_topology.c

Quoted 2 lines
drivers/gpu/drm/drm_dp_mst_topology.c +2 -0
+init_mstb_topology_history(struct drm_dp_mst_topology_mgr
+*mgr, struct drm_dp_mst_branch *mstb); static inline void

Same as above
=> static inline void init_mstb_topology_history(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_branch *mstb) {};

drivers/gpu/drm/drm_dp_mst_topology.c

Quoted 3 lines
drivers/gpu/drm/drm_dp_mst_topology.c +3 -0
+destroy_port_topology_history(struct
+drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port
+*port); static inline void

destroy_port_topology_history() takes one parameter only which is port.
=> destroy_port_topology_history(struct drm_dp_mst_port *port) {};

drivers/gpu/drm/drm_dp_mst_topology.c

Quoted 3 lines
drivers/gpu/drm/drm_dp_mst_topology.c +3 -0
+destroy_mstb_topology_history(struct
+drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_branch
+*mstb); static inline void

destroy_mstb_topology_history() takes one parameter only which is mstb => destroy_mstb_topology_history(struct drm_dp_mst_branch
*mstb) {};

Quoted 237 lines

 topology_ref_history_lock(struct drm_dp_mst_topology_mgr *mgr) {} static inline void topology_ref_history_unlock(struct drm_dp_mst_topology_mgr *mgr) {} @@ -1740,6 +1822,25 @@ dump_port_topology_history(struct drm_dp_mst_port *port) {} #define save_port_topology_ref(port, type)  #endif

drivers/gpu/drm/drm_dp_mst_topology.c +20 -0
+static struct drm_dp_mst_branch *
+drm_dp_add_mst_branch_device(struct drm_dp_mst_topology_mgr
+*mgr,
+u8 lct, u8 *rad) {
+     struct drm_dp_mst_branch *mstb;
+
+     mstb = kzalloc(sizeof(*mstb), GFP_KERNEL);
+     if (!mstb)
+             return NULL;
+
+     mstb->lct = lct;
+     if (lct > 1)
+             memcpy(mstb->rad, rad, lct / 2);
+     INIT_LIST_HEAD(&mstb->ports);
+     kref_init(&mstb->topology_kref);
+     kref_init(&mstb->malloc_kref);
+     init_mstb_topology_history(mgr, mstb);
+     return mstb;
+}
+

 static void drm_dp_destroy_mst_branch_device(struct kref *kref) {
      struct drm_dp_mst_branch *mstb = @@ -1747,6 +1848,7 @@ static void drm_dp_destroy_mst_branch_device(struct kref *kref)
      struct drm_dp_mst_topology_mgr *mgr = mstb->mgr;

      dump_mstb_topology_history(mstb);
+     destroy_mstb_topology_history(mstb);

      INIT_LIST_HEAD(&mstb->destroy_next);

drivers/gpu/drm/drm_dp_mst_topology.c +0 -0
@@ -1856,6 +1958,7 @@ static void drm_dp_destroy_port(struct

kref
*kref)
      struct drm_dp_mst_topology_mgr *mgr = port->mgr;

      dump_port_topology_history(port);
+     destroy_port_topology_history(port);

      /* There's nothing that needs locking to destroy an input port yet */
      if (port->input) {

drivers/gpu/drm/drm_dp_mst_topology.c +0 -0
@@ -2135,7 +2238,7 @@ drm_dp_port_set_pdt(struct
 drm_dp_mst_port *port, u8 new_pdt,

                      ret = drm_dp_mst_register_i2c_bus(port);
              } else {
                      lct = drm_dp_calculate_rad(port, rad); -                     mstb = drm_dp_add_mst_branch_device(lct, rad);
+                     mstb = +drm_dp_add_mst_branch_device(mgr, lct, rad);
                      if (!mstb) {
                              ret = -ENOMEM; drm_err(mgr->dev, "Failed to create MSTB for port %p", port); @@ -2366,6 +2469,8 @@ drm_dp_mst_add_port(struct drm_device *dev,
       */
      drm_dp_mst_get_mstb_malloc(mstb);

+     init_port_topology_history(mgr, port);
+
      return port;
 }

drivers/gpu/drm/drm_dp_mst_topology.c +0 -0
@@ -3745,7 +3850,7 @@ int

drm_dp_mst_topology_mgr_set_mst(struct
drm_dp_mst_topology_mgr *mgr, bool ms
              }

              /* add initial branch device at LCT 1 */ -             mstb = drm_dp_add_mst_branch_device(1, NULL); +             mstb = drm_dp_add_mst_branch_device(mgr, 1, +NULL);
              if (mstb == NULL) {
                      ret = -ENOMEM;
                      goto out_unlock; @@ -5512,14 +5617,16 @@ int drm_dp_mst_topology_mgr_init(struct drm_dp_mst_topology_mgr *mgr, mutex_init(&mgr->delayed_destroy_lock); mutex_init(&mgr->up_req_lock); mutex_init(&mgr->probe_lock); -#if IS_ENABLED(CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS) -     mutex_init(&mgr->topology_ref_history_lock); -#endif
      INIT_LIST_HEAD(&mgr->tx_msg_downq); INIT_LIST_HEAD(&mgr->destroy_port_list); INIT_LIST_HEAD(&mgr->destroy_branch_device_list); INIT_LIST_HEAD(&mgr->up_req_list);

drivers/gpu/drm/drm_dp_mst_topology.c +5 -0
+#if IS_ENABLED(CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS)
+     mutex_init(&mgr->topology_ref_history_lock);
+     INIT_LIST_HEAD(&mgr->topology_ref_history_list);
+#endif
+

      /*
       * delayed_destroy_work will be queued on a dedicated WQ, so that any
       * requeuing will be also flushed when deiniting the topology manager.

drivers/gpu/drm/nouveau/nouveau_debugfs.c +4 -0
index 1cbe01048b93..53ec7c1daada 100644
@@ -29,9 +29,13 @@
   */
 
  #include <linux/debugfs.h>
+#include <drm/drm_dp_mst_helper.h> #include
+<drm/drm_encoder.h>
  #include <nvif/class.h>
  #include <nvif/if0001.h>
+#include <nouveau_encoder.h>
  #include "nouveau_debugfs.h"
+#include "nouveau_display.h"
  #include "nouveau_drv.h"
 

 static int

drivers/gpu/drm/nouveau/nouveau_debugfs.c +0 -0
@@ -68,6 +72,34 @@ nouveau_debugfs_strap_peek(struct

seq_file *m, void
*data)
      return 0;
 }

drivers/gpu/drm/nouveau/nouveau_debugfs.c +30 -0
+#ifdef CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS
+static int nouveau_debugfs_mst_topology_history(struct
+seq_file *m, void *data) {
+     struct drm_info_node *node = m->private;
+     struct drm_device *dev = node->minor->dev;
+     struct drm_encoder *encoder;
+     struct nouveau_display *disp = nouveau_display(dev);
+
+     if (disp->disp.object.oclass < GF110_DISP)
+             return -EINVAL;
+
+     drm_for_each_encoder(encoder, dev) {
+             struct nv50_mstm *mstm;
+
+             /* We need the real encoder for each MST mgr
+*/
+             if (encoder->encoder_type !=
+DRM_MODE_ENCODER_TMDS)
+                     continue;
+             mstm = nouveau_encoder(encoder)->dp.mstm;
+             if (!mstm)
+                     continue;
+
+             seq_printf(m, "MSTM on %s:\n", encoder->name);
+             drm_dp_mst_dump_topology_refs(m, &mstm->mgr);
+     }
+     return 0;
+}
+#endif
+

 static int
 nouveau_debugfs_pstate_get(struct seq_file *m, void *data) { @@
-213,6
+245,9 @@ static const struct file_operations nouveau_pstate_fops = {  static struct drm_info_list nouveau_debugfs_list[] = {
      { "vbios.rom",  nouveau_debugfs_vbios_image, 0, NULL },
      { "strap_peek", nouveau_debugfs_strap_peek, 0, NULL },

drivers/gpu/drm/nouveau/nouveau_debugfs.c +3 -0
+#ifdef CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS
+     { "dp_mst_topology_refs",
+nouveau_debugfs_mst_topology_history, 0, NULL }, #endif

 };
 #define NOUVEAU_DEBUGFS_ENTRIES
ARRAY_SIZE(nouveau_debugfs_list)

(quoted diff) +0 -0

b/include/drm/drm_dp_mst_helper.h index
78044ac5b59b..e3a73d8d97c0
100644

include/drm/drm_dp_mst_helper.h +4 -0
@@ -35,6 +35,11 @@ enum drm_dp_mst_topology_ref_type {
       DRM_DP_MST_TOPOLOGY_REF_PUT,
  };
 
+enum drm_dp_mst_topology_history_type {
+     DRM_DP_MST_TOPOLOGY_HISTORY_PORT,
+     DRM_DP_MST_TOPOLOGY_HISTORY_MSTB, };
+
  struct drm_dp_mst_topology_ref_history {
       struct drm_dp_mst_topology_ref_entry {

              enum drm_dp_mst_topology_ref_type type; @@ -43,6
+48,9 @@ struct drm_dp_mst_topology_ref_history { depot_stack_handle_t backtrace;
      } *entries;
      int len;
+
+     enum drm_dp_mst_topology_history_type type; +     struct list_head node;
 };
 #endif /* IS_ENABLED(CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS) */

include/drm/drm_dp_mst_helper.h +4 -0
@@ -769,6 +777,12 @@ struct drm_dp_mst_topology_mgr {
        * &drm_dp_mst_branch.topology_ref_history.
        */
       struct mutex topology_ref_history_lock;
+
+     /**
+      * @topology_ref_history_list: contains a list of
+topology ref

histories for any MSTBs or
+      * ports that are still in memory
+      */
+     struct list_head topology_ref_history_list; #endif
 };

include/drm/drm_dp_mst_helper.h +0 -0
@@ -873,6 +887,10 @@ void drm_dp_mst_put_port_malloc(struct
 drm_dp_mst_port *port);
 

 struct drm_dp_aux *drm_dp_mst_dsc_aux_for_port(struct drm_dp_mst_port *port);

include/drm/drm_dp_mst_helper.h +4 -0
+#ifdef CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS
+void drm_dp_mst_dump_topology_refs(struct seq_file *m,
+struct drm_dp_mst_topology_mgr *mgr); #endif
+

 extern const struct drm_private_state_funcs
drm_dp_mst_topology_state_funcs;

Quoted 4 lines
Quoted 3 lines

 /**
--
2.31.1

--
Cheers,
 Lyude Paul (she/her)
 Software Engineer at Red Hat

Quoted 2 lines

--
Cheers, Lyude Paul (she/her) Software Engineer at Red Hat

--
Regards,
Wayne Lin

Signature · 3 lines
Cheers,
 Lyude Paul (she/her)
 Software Engineer at Red Hat
#10 lore raw

[AMD Official Use Only]

Thank you Lyude!

So, in short, I'm trying to register a call back function to listen to unplug event within MST topology.

Removal of local monitor from system can be detected by HPD pin and it therefore triggers HW interrupt. We then release corresponding resource for the specific stream sink in sequence.

However, unplug event within MST topology is broadcasted by CSN sideband message which is handled by drm layer in drm_dp_mst_topology.c, not by our driver. In order to follow the same sequence as we have for removal of local monitor, I want to know is there any code path to notify driver the unplug event within MST topology.

Currently, removal of remote monitors by removing its connected mst branch device from the topology, such unplug event eventually call drm_dp_delayed_destroy_port() and we can try to release resource for those stream sinks under this sequence. However, there is no code path for the case when remove stream sinks directly from the topology (i.e. not remove monitors by indirectly removing its connected branch device). As the result, the rough idea that I want to do is adding up a notification for this case.

Thank you for your time!

Quoted 1,222 lines

-----Original Message-----
From: Lyude Paul <[email protected]>
Sent: Thursday, February 3, 2022 4:12 AM
To: Lin, Wayne <[email protected]>
Cc: [email protected]
Subject: Re: [PATCH] WIP: drm/dp_mst: Add support for dumping topology ref histories from debugfs

JFYI I should be able to respond to this soon, I think I'm following your issue a bit more (haven't gotten to go through the patches too so I'm probably going to see if I can reproduce it locally as well. Sorry for the delay - dealing with some X.org stuff and work issues at the moment

Quoted 27 lines · Wayne

[Public]

Hey Lyude,

Sorry, just got chance to get back to this and BIG THANKS to elaborating in such details!
Following are some of my comments : )

Quoted 10 lines

-----Original Message-----
From: Lyude Paul <[email protected]>
Sent: Wednesday, January 19, 2022 9:59 AM
To: Lin, Wayne <[email protected]>
Cc: [email protected]
Subject: Re: [PATCH] WIP: drm/dp_mst: Add support for dumping topology ref histories from debugfs

Hey - so, the original issue here was that payloads were not always deleted when we expected them to be - correct?

Actually, what I was trying to do is not relevant to payload maintenance. What I wanted to do is having a way to notify
amdgpu to release its enumerated resource "dc_sink" when unplug event occurs. Currently, under specific scenarios,
we can't release remote "dc_sink" and ends up can't allocate resource for new connected monitors. I'll explain the scenario
underneath.

Quoted 3 lines

So, get back to the main issue that I was trying to solve when I pushed the patchset:
https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Fpatchwork.kernel.org%2Fproject%2Fdri-

devel%2Fcover%2F20210720160342.11415-1- Wayne.Lin%40amd.com%2F&amp;data=04%7C01%7Cwayne.lin%40amd.com%7C2e19d9321b5f4e948c1208d9e6884f24%7C3dd8961fe488 4e608e11a82d994e183d%7C0%7C0%7C637794295422403301%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiL CJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&amp;sdata=rvtwQioO3pT1BoHb7S532msRa61gP1vIyE8AB7M9I4E%3D&amp;reserved=0

Quoted 1,168 lines

I was trying to fix some problems that I observed after these 2 patches
* 09b974e8983 drm/amd/amdgpu_dm/mst: Remove ->destroy_connector() callback
* 72dc0f51591 drm/dp_mst: Remove drm_dp_mst_topology_cbs.destroy_connector

With above patches, it changes our flow to remove dc_sink (by calling dc_link_remote_sink()) when connector is about to be destroyed. And let's see below use cases:
a). Disconnection happens between mst branch devices
e.g.
src - 1st_mstb - 2nd_mstb - sst_monitor => src - 1st_mstb (disconnect) 2nd_mstb - sst_monitor

In above case, after receiving CSN, we will put topology reference count of 2nd mstb and its port which connected to the sst monitor.
As you suggested before, we will destroy 2nd mst branch device and its all ports due to there is no more mst path can route to them.
As the result of that, we at end call drm_dp_delayed_destroy_port() and unregister/put the drm connector and eventually call
dc_link_remote_sink() to release the dc_sinck we enumerate for the specific
stream sink of sst_monitor.

However, in below case:
b). Disconnection happens between mst branch devices and SST monitor e.g.
src - 1st_mstb - sst_monitor => src - 1st_mstb (disconnect) sst_monitor

In this case, which is the case having problem, it definitely won't decrease the topology references count of the port of 1st_mstb which
was connected to the sst monitor to zero since the port is still existing in the topology. Same as the malloc reference since the port can't get destroyed. Hence, the port still exists and we won't call drm_dp_delayed_destroy_port() to unregister and put the drm connector. As the
result of that, we can't release corresponding remote dc_sink under this case. And after few times hotplugs, we can't create any new dc_sink since the number of stream sink is exceeding our limitation.

Hence, what I'm thinking here is to register a callback function for notifying us that the remote sink is detached. This just aligns what we do for handling long HPD event (unplug monitor from system) of legacy DP (none mst). For which case, once we detect long HPD event
indicating the monitor is detached, we will immediately try to release the dc_link->local_sink and fire hotplug event to upper layer.
Same as here, once receives a CSN message notifying a drm connector is changed from connected to disconnected, trigger the callback function and we can try to release the dc_sink resource.

Quoted 30 lines

If I'm remembering that correctly, then (and I'm not 100% sure on this yet) I might already have noticed something very possibly wonky in how we handle payload allocations currently, that I found while working on the non-atomic removal branch that I linked to you in my previous response. Basically, in the
step1() function it looks like that we follow this general flow with updating
payloads:

* Loop through proposed payloads and compare to previously programmed payloads
- If a payload has a different allocation then it previously did,
update the
payload
- If the payload is new, create it
- If a payload no longer has an allocation, remove the payload

At first glance this seems totally correct - but I'm not actually entirely convinced it is? Particularly because it seems like the order in which we do creation/deletion of payloads is totally arbitrary. To explain what I mean by that, consider a state transition like this:

vcpi_slots=15 vcpi_slots=35 vcpi_slots=14

Quoted 1 line

1 | 2 |xxxxxxxx|

Let's say we want to increase payload #1 from 15 to 50, and disable payload #2 in the same atomic commit on DRM's side. If the order we update payloads is entirely arbitrary, we could end up doing this:

Quoted 8 lines

* Increase VCPI slots payload #1 from 15->50 (total VCPI slots=99,
overflow!)
* Decrease VCPI slots payload #2 from 35->0 (total VCPI slots=50)

Notice on the first step, we've technically overflowed the available number of VCPI slots in the payload table. This is still before step 2 though, and I could be totally wrong here - perhaps this is entirely OK in the real world, and we're allowed to overflow VC slots as long as

I think it's forbidden to allocate time slots beyond 64 when we're updating payload ID table of the branch in the real world. Branch probably will NACK the DPCD write.

Quoted 5 lines

we repair the issue before step 2. But so far I can't seem to find anything in the DP 2.0 spec that explicitly states this would be OK - which
makes me think we might fail some payload allocations if we don't always ensure we avoid overflows in between VC payload table changes

Agree.
For amdgpu, I think we always do deallocation (call hws-

Quoted 1 line

funcs.reset_hw_ctx_wrap()) before allocation

(apply_single_controller_ctx_to_hw()).

Quoted 12 lines

Note that avoiding overflows would be as simple as just making sure we send all VC payload table changes that free VC slots before sending any that take new slots.

Again - I haven't actually confirmed this yet and am hoping to test stuff like this very soon as I'm pretty close finishing up the initial attempt at removing the non-atomic MST code in the DRM helpers now. If my theory ends up being correct here, I can fix this in my rewrite of the MST payload management code. But I figured it might be worth mentioning in the mean time in case you think it might be relevant to the problem here :).

Quoted 71 lines
Quoted 70 lines · Lyude Paul

(CC'ing this to dri-devel, since this is basically patch review)

Alright - so first off sorry about the broken debugging patch! I thought I had tested that but I guess I hadn't tested it well enough, I'll get it fixed asap, but feel free to try getting to it before I do.

As for this patch... (comments below)

Quoted 46 lines · Wayne

[AMD Official Use Only]

Hi Lyude,

No rush and thanks for your time! : ) Just want to help clarify what I mean that "registering a callback function"
for CSN of disconnecting
stream sink device (not mst branch case). Attach below the tentative patch that I planned to do. Thanks so much!

Regards,
Wayne

---
 .../display/amdgpu_dm/amdgpu_dm_mst_types.c   | 53
+++++++++++++++++++
 drivers/gpu/drm/drm_dp_mst_topology.c         | 16 +++++-
 include/drm/drm_dp_mst_helper.h               |  1 +
 3 files changed, 68 insertions(+), 2 deletions(-)

diff --git a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_mst_types.c b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_mst_types.c index cc34a35d0bcb..d7343c64908c 100644

drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_mst_types.c +16 -0
@@ -472,8 +472,61 @@ dm_dp_add_mst_connector(struct
 drm_dp_mst_topology_mgr *mgr,
        return connector;
 }
 
+void dm_dp_notify_csn_disconnection(struct drm_connector
+*connector) {
+       struct amdgpu_dm_connector *aconnector =
+               to_amdgpu_dm_connector(connector);
+       struct dc_sink *dc_sink = aconnector->dc_sink;
+       struct dc_link *dc_link = aconnector->dc_link;
+       struct amdgpu_device *adev = drm_to_adev(ddev);
+
+       ASSERT(dc_link);
+
+       if (dc_sink) {
+               mutex_lock(&adev->dm.dc_lock);
+
+               /*clear the remote sink of the link*/
+               dc_link_remove_remote_sink(dc_link, dc_sink);
+               dc_sink_release(dc_sink);

drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_mst_types.c dm_dp_add_mst_connector(struct · line 501

Quoted 10 lines
drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_mst_types.c +7 -0
+               aconnector->dc_sink = NULL;
+
+               mutex_unlock(&adev->dm.dc_lock);
+       }
+}
+
 static const struct drm_dp_mst_topology_cbs dm_mst_cbs = {
        .add_connector = dm_dp_add_mst_connector,
+       .notify_csn_disconnection = dm_dp_notify_csn_disconnection,
 };

I still don't really think this is a good idea. This seems like it's just adding another hotplugging path to the code in order to avoid sending hotplugs for non-endpoint devices. In addition to the

So, what I wanted to do is to add a callback function for endpoint devices case as described above. For disconnection of non-endpoint devices is already taken care by current code.

Quoted 8 lines
Quoted 8 lines

drm_connector issues I mentioned before, we also really need to stop doing any kind of payload maintence in hotplugging paths. The reality is any kind of payload maintanence we do outside of normal modesetting paths is a relic from legacy modesetting that I'm dropping ASAP (more on this below), and we can't keep adding to it because it dramatically complicates maintanence as well.

Quoted 14 lines
Quoted 6 lines

Sorry for repeating this point so often but - the biggest issue too is I'm still not sure what it is we're even avoiding here. We know resources aren't released consistently, and that we're able to reproduce the behavior with repeated hotplugs. We also know that if we skip sending certain hotplug events, that fixes the issue. And we know we can workaround it by adding a special case for forcing a payload

Quoted 8 lines

release in DC. But none of those actually tell us exactly what piece of code is leaking and why, which means that any workarounds we're putting in to avoid this mysterious guilty section of code we don't entirely understand either - which means we're just adding more code in that no one actually fully understands. This just ends up making maintence difficult because every change in code nearby workarounds like this has to strugle to try to figure out said workarounds in order to avoid breaking things.

Thank you Lyude for the reminder and I totally agree with you by avoiding adding workaround since it will get the code maintenance harder. And I think the root cause of the issue that I observed before is quite concrete. There is no code path for us to release dc_sink under the unplug scenario that I described above.

Quoted 57 lines
Quoted 57 lines

I'm actually currently running into these "later" issues right now, as recently I've (-actively-, finally!!!) been working on trying to remove as much non-atomic MST as I can because. As it turns out - a huge amount of the payload maintanence code just isn't needed at all when we stop caring about non-atomic drivers and stick everything in atomic state structs. Step 1 for updating updating the payload table, e.g. drm_dp_update_payload_part1(), is a great example of how messy things have become. Here's a small sample of some of the stuff I've seen from just that one function so far that either just don't make sense here or is totally redundant. I should note that a lot of these things also come from patches I reviwed, but didn't really look at as closely as I should have because I was swamped at work, some are historical artifacts, and others are less-than-ideal patches I got wrote myself when I was first started working on MST and didn't know the code as well as I do
now:

* We try to avoid some sort of userspace issue by using drm_dp_mst_port_downstream_of_branch() to avoid releasing payloads
for a
branch if we can't prove it's downstream of the top of the
topology. This
seems to workaround either a userspace bug. This is a redundant,
since
that's what topology refs are already supposed to be doing to the
extent is
reasonably possibly. It's also unfortunately racy solution because
we have
to be able to handle the situation where a connector is removed
from under
us. That can happen at any time, including _immediately_ after we
call
drm_dp_mst_port_downstream_of_branch() - rendering the call not
really
useful.
* If we fail to validate the sink in drm_dp_update_payload_part, we
don't
update the payload table. I think at best this solution is racy and
not
useful, at worst it leaves us with a payload table that doesn't
match what
we attempted to set in the atomic state - which at worst brings us
into
undefined territory where we're just plain out of sync with the
reality in
hw.
* Actually fun fact - mgr->payloads and mgr->proposed_vcpis both can
and
definitely should be removed entirely. All of the info for
mgr->payloads
could just be in the atomic state, because that + the magic of
atomic state
duplication means we'll also have an accurate view of the previous
state's
payload allocations: which renders mgr->proposed_vcpis redundant.

Quoted 1 line
Quoted 1 line

Apologies for the long explanation again, but I hope that explains my

Really really thanks for your kindly help on this : ) I'll try my best to get to your WIP branch soon. Thank you Lyude!

Quoted 887 lines
Quoted 886 lines

point here a bit. I'm going to be trying to get to moving amdgpu's DSC code out of amdgpu and into DRM helpers as well soon, so I'm really determined to clean stuff up beforehand as every time I've done so it's become substantially easier to make changes to this code. Things used to be even worse before I started cleaning things up 2 or 3 years ago, where simple changes would end up getting me stuck spending hours trying to dig through lockdep or memory manegement issues. As well, I would be entirely unsurprised if bugs like this very payload leak we're working on just disappear once we've gotten rid of all the extraneous workarounds and state tracking here - especially with how many special cases we have for maintaining the payload table right now. That's certainly ended up being the case in the past with a number of other difficult to track down issues I've dealt with in MST.

Anyhow. I've been way more productive this year then last because I had over a month off work and am finally not super burnt out from my job, and so far I've been making progress on the payload state cleanup far faster then I was last year :). I think if you can't figure out where the leak is coming from even once I get the debugging patches I mentioned fixed up, it might be a good idea for us to try again after I've got some of this code cleaned up. I've got a currently WIP branch here:

https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgitl
ab.freedesktop.org%2Flyudess%2Flinux%2F-%2Fcommit%2F624dd68fa804e64b5b 2060e4735d7317090692b5&amp;data=04%7C01%7Cwayne.lin%40amd.com%7Cd4bdc7 59eb274bfccc8208d9daef41fe%7C3dd8961fe4884e608e11a82d994e183d%7C0%7C0% 7C637781543416430632%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQI joiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&amp;sdata=nFpVl%2BVoF 40JPabfRMcR6Cz7ZHDwt%2BBpLDBNdXftJaA%3D&amp;reserved=0

Quoted 854 lines
drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_mst_types.c +0 -0
 void amdgpu_dm_initialize_dp_connector(struct

amdgpu_display_manager *dm, diff --git
a/drivers/gpu/drm/drm_dp_mst_topology.c
b/drivers/gpu/drm/drm_dp_mst_topology.c
index 857c5d15e81d..a70e26c5d084 100644

drivers/gpu/drm/drm_dp_mst_topology.c +1 -0
@@ -2508,6 +2508,7 @@ drm_dp_mst_handle_conn_stat(struct
 drm_dp_mst_branch *mstb,
        u8 new_pdt;
        bool new_mcs;
        bool dowork = false, create_connector = false;
+       bool notify_unplug_event = false;
 
        port = drm_dp_get_port(mstb, conn_stat->port_number);
        if (!port)
drivers/gpu/drm/drm_dp_mst_topology.c +0 -0
@@ -2520,6 +2521,9 @@ drm_dp_mst_handle_conn_stat(struct
 drm_dp_mst_branch *mstb,
                         * port, so just throw the port out and make

sure we
* reprobe the link address of it's parent
MSTB

                         */

+ /* should also consider notify_unplug_event
here.

drivers/gpu/drm/drm_dp_mst_topology.c +2 -0
+                        * but it's not a normal case for products
+in the

market
+ */

                        drm_dp_mst_topology_unlink_port(mgr, port);
                        mstb->link_address_sent = false;
                        dowork = true; @@ -2541,10 +2545,14 @@
drm_dp_mst_handle_conn_stat(struct
drm_dp_mst_branch
*mstb,
        port->ddps = conn_stat->displayport_device_plug_status;

        if (old_ddps != port->ddps) {
drivers/gpu/drm/drm_dp_mst_topology.c +1 -1
-               if (port->ddps && !port->input)
+               if (port->ddps && !port->input) {
                        drm_dp_send_enum_path_resources(mgr, mstb,

port);

drivers/gpu/drm/drm_dp_mst_topology.c +4 -1
-               else
+               } else {
                        port->full_pbn = 0;
+                       if (port->connector &&
+                               drm_dp_mst_is_end_device(port->pdt,
+port-
Quoted 1 line

mcs))

drivers/gpu/drm/drm_dp_mst_topology.c +2 -0
+                               notify_unplug_event = true;
+               }
        }
 
        new_pdt = port->input ? DP_PEER_DEVICE_NONE : conn_stat-
Quoted 1 line

peer_device_type;

drivers/gpu/drm/drm_dp_mst_topology.c +0 -0
@@ -2557,11 +2565,15 @@ drm_dp_mst_handle_conn_stat(struct

drm_dp_mst_branch
*mstb,

                dowork = false;
        }
drivers/gpu/drm/drm_dp_mst_topology.c +5 -0
+       if (notify_unplug_event &&
+mgr->cbs->notify_csn_disconnection)
+               mgr->cbs->notify_csn_disconnection(port->connector);
+
        if (port->connector)
                drm_modeset_unlock(&mgr->base.lock);
        else if (create_connector)
                drm_dp_mst_port_add_connector(mstb, port);
 
+
 out:
        drm_dp_mst_topology_put_port(port);
        if (dowork)
(quoted diff) +0 -0

b/include/drm/drm_dp_mst_helper.h index 78044ac5b59b..ff9e47729841 100644

include/drm/drm_dp_mst_helper.h +2 -1
@@ -525,6 +525,7 @@ struct drm_dp_mst_topology_cbs {
         * IRQ pulse handler.
         */
        void (*poll_hpd_irq)(struct drm_dp_mst_topology_mgr *mgr);
+       void (*notify_csn_disconnection)(struct drm_connector
+*connector);
 };
 
 #define DP_MAX_PAYLOAD (sizeof(unsigned long) * 8)
--

2.31.0

Quoted 761 lines

-----Original Message-----
From: Lin, Wayne
Sent: Wednesday, December 8, 2021 11:39 AM
To: 'Lyude Paul' <[email protected]>
Subject: RE: [PATCH] WIP: drm/dp_mst: Add support for dumping topology ref histories from debugfs

No worries Lyude!
Thanks for keeping helping on this. Take your time : )

Quoted 751 lines

-----Original Message-----
From: Lyude Paul <[email protected]>
Sent: Wednesday, December 8, 2021 7:05 AM
To: Lin, Wayne <[email protected]>
Subject: Re: [PATCH] WIP: drm/dp_mst: Add support for dumping topology ref histories from debugfs

Sorry! I will try to get to this tomorrow, if not then sometime this week.

Quoted 731 lines · Wayne

[Public]

Hi Lyude,

Finally have some bandwidth to get back to this problem! I roughly went through this patch and I'm just aware that we already have such kind of convenient tool for a while. I think it's definitely useful for us to track port/mstb reference count issues and I'll start to embrace this feature for cleaning up those issues. Thank you Lyude!

However, I think the issue that I was trying to fix is not related to what you suggested:
" The idea here is that if stream resources aren't being released, my guess would be that we're not dropping topology references for the port which means the connector never goes away."
The issue I was trying to fix is about releasing dc_link->remote_sinks while receiving a CSN message notifying the connection status of a sst connector of a port changed from connected to disconnected. Not the connection status changed of a mst branch device.
e.g.
src - 1st_mstb - 2nd_mstb - sst_monitor => src - 1st_mstb (disconnect) 2nd_mstb - sst_monitor

In above case, after receiving CSN, we will put topology references of 2nd mstb and its port which is connected with the sst monitor. As the result of that, we can call drm_dp_delayed_destroy_port() to unregister and put the drm connector.

However, in below case:
e.g.
src - 1st_mstb - sst_monitor => src - 1st_mstb (disconnect) sst_monitor

In this case, which is the case having problem, it definitely won't decrease the topology references count of the port which was connected to the sst monitor to zero since the port is still existing in the topology. Same as the malloc reference since the port can't get destroyed. Hence, the port still exists and we won't call drm_dp_delayed_destroy_port() to unregister and put the drm connector.
I looked up the code and drm_dp_delayed_destroy_port() seems like the only place to call drm_connector_put() which means we can't put reference count of drm connector under this case and can't release dc_sink resource by destroying drm connector.

I would also like to point out that this resource (remote_sinks) is specific to different stream sinks. So if we're trying to release this dc_sink resource by destroying the drm connector, it conflicts the idea that you suggested before that we should always keep the drm connector until it's no longer reachable in the topology.
Releasing dc_sink should be binding with the disconnection event.

I understand your concern that we should not just easily change the logic here since it's the result after solving tons of bugs before and might cause other side effect. So, just my 2 cents, what I'm thinking is to register a callback function for our driver to notify us that the remote sink is detached. This just aligns our flow handling long HPD event of legacy (sst) DP.
For sst case, once we detect long HPD event indicating the monitor is detached, we will immediately try to release the dc_link->local_sink and fire hotplug event to upper layer. Same as here, once receives a CSN message notifying a drm connector is changed from connected to disconnected, trigger the callback function and we can try to release the dc_sink resource.

Would like to know your thought and insight please : )

Btw, I got some errors and warnings while building and have some code adjustments as below : ) Thank you Lyude for your always kindly help!!

Regards,
Wayne

Quoted 382 lines

-----Original Message-----
From: Lyude Paul <[email protected]>
Sent: Wednesday, November 3, 2021 7:15 AM
To: Lin, Wayne <[email protected]>
Subject: [PATCH] WIP: drm/dp_mst: Add support for dumping topology ref histories from debugfs

TODO:
* Implement support for i915
* Finish writing this commit message
* ???
* profit

---

Hey wayne! SO-hopefully if I did this correctly then this should just work on amdgpu. What this patch should do is add a debugfs file to amdgpu called "amdgpu_dp_mst_topology_refs", and when you read the file it should print out the topology reference history of every MSTB and Port in memory, along with how many times we've hit the codepath in each backtrace. An example:

Port DP-5 (0000000005c37748) topology ref history:
1 gets (last at 58.468973):

     drm_dp_send_link_address+0x6a5/0xa00 [drm_kms_helper]
     drm_dp_check_and_send_link_address+0xad/0xd0
[drm_kms_helper]
     drm_dp_mst_link_probe_work+0x14e/0x1a0 [drm_kms_helper]
     process_one_work+0x1e3/0x390
     worker_thread+0x50/0x3a0
     kthread+0x124/0x150
     ret_from_fork+0x1f/0x30
  1 puts (last at    58.469357):
     drm_dp_mst_topology_put_port+0x6a/0x210
[drm_kms_helper]
     drm_dp_send_link_address+0x39e/0xa00 [drm_kms_helper]
     drm_dp_check_and_send_link_address+0xad/0xd0
[drm_kms_helper]
     drm_dp_mst_link_probe_work+0x14e/0x1a0 [drm_kms_helper]
     process_one_work+0x1e3/0x390
     worker_thread+0x50/0x3a0
     kthread+0x124/0x150
     ret_from_fork+0x1f/0x30

The idea here is that if stream resources aren't being released, my guess would be that we're not dropping topology references for the port which means the connector never goes away. So, if that's really the case then once we unplug the offending connector we should be able to find a pair of gets/puts for the offending leaked connector where the get/put count doesn't match up. Also, if the frame count on the backtrace isn't long enough you can increase the value of STACK_DEPTH in drivers/gpu/drm/drm_dp_mst_topology.c and recompile to get more frames.

To enable this, first enable CONFIG_EXPERT for your kernel which will unhide CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS. Then just enable CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS, recompile, and it should be good to go.

Let me know if this works for you, and hopefully this should tell us exactly what the problem actually is here.

 .../amd/display/amdgpu_dm/amdgpu_dm_debugfs.c |  35 ++++
 drivers/gpu/drm/drm_dp_mst_topology.c         | 173
++++++++++++++----
 drivers/gpu/drm/nouveau/nouveau_debugfs.c     |  35 ++++
 include/drm/drm_dp_mst_helper.h               |  18 ++
 4 files changed, 228 insertions(+), 33 deletions(-)

diff --git a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_debugfs.c b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_debugfs.c index 1a68a674913c..1a14732c52b4 100644
---
a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_debugfs.c +++ b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_debugf +++ s.c

(quoted diff) +0 -0
@@ -3063,6 +3063,37 @@ static int mst_topo_show(struct

seq_file *m, void
*unused)

      return 0;

}

(quoted diff) +14 -0
+#ifdef CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS
+static int mst_topology_ref_dump_show(struct seq_file *m,
+void
+*unused) {
+     struct amdgpu_device *adev = (struct amdgpu_device
+*)m->private;
+     struct drm_device *dev = adev_to_drm(adev);
+     struct drm_connector *connector;
+     struct drm_connector_list_iter conn_iter;
+     struct amdgpu_dm_connector *aconnector;
+
+     drm_connector_list_iter_begin(dev, &conn_iter);
+     drm_for_each_connector_iter(connector, &conn_iter) {
+             if (connector->connector_type !=

DRM_MODE_CONNECTOR_DisplayPort)

(quoted diff) +7 -0
+                     continue;
+
+             aconnector =
+to_amdgpu_dm_connector(connector);
+
+             /* Ensure we're only dumping the topology of a
+root mst node

*/

(quoted diff) +5 -0
+             if (!aconnector->mst_mgr.max_payloads)
+                     continue;
+
+             seq_printf(m, "\nMST topology for connector
+%d\n",

aconnector->connector_id);

(quoted diff) +11 -0
+             drm_dp_mst_dump_topology_refs(m,
+&aconnector->mst_mgr);
+     }
+     drm_connector_list_iter_end(&conn_iter);
+
+     return 0;
+}
+
+DEFINE_SHOW_ATTRIBUTE(mst_topology_ref_dump);
+#endif
+
 /*
  * Sets trigger hpd for MST topologies.
  * All connected connectors will be rediscovered and re

started as needed if val of 1 is sent.

(quoted diff) +0 -0
@@ -3299,6 +3330,10 @@ void dtn_debugfs_init(struct

amdgpu_device
*adev)

      debugfs_create_file("amdgpu_mst_topology", 0444, root,
                          adev, &mst_topo_fops);
(quoted diff) +5 -0
+#ifdef CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS
+     debugfs_create_file("amdgpu_dp_mst_topology_refs",
+0444, root, adev,
+                         &mst_topology_ref_dump_fops);
+#endif
      debugfs_create_file("amdgpu_dm_dtn_log", 0644, root,

adev,

                          &dtn_log_fops);
drivers/gpu/drm/drm_dp_mst_topology.c +0 -16
index 1aa8702383d4..0159828c494d 100644
@@ -1366,23 +1366,6 @@ static int
 drm_dp_mst_wait_tx_reply(struct drm_dp_mst_branch *mstb,
      return ret;
 }
 
-static struct drm_dp_mst_branch
 *drm_dp_add_mst_branch_device(u8 lct, u8
 *rad) -{
-     struct drm_dp_mst_branch *mstb;
-
-     mstb = kzalloc(sizeof(*mstb), GFP_KERNEL);
-     if (!mstb)
-             return NULL;
-
-     mstb->lct = lct;
-     if (lct > 1)
-             memcpy(mstb->rad, rad, lct / 2);
-     INIT_LIST_HEAD(&mstb->ports);
-     kref_init(&mstb->topology_kref);
-     kref_init(&mstb->malloc_kref);
-     return mstb;
-}
-
 static void drm_dp_free_mst_branch_device(struct kref
*kref)  {
      struct drm_dp_mst_branch *mstb = @@ -1642,12 +1625,20
@@ topology_ref_type_to_str(enum
drm_dp_mst_topology_ref_type type)
              return "put";

}

drivers/gpu/drm/drm_dp_mst_topology.c +7 -1
+static const char *topology_ref_history_type_to_str(enum
+drm_dp_mst_topology_history_type type) {
+     if (type == DRM_DP_MST_TOPOLOGY_HISTORY_PORT)
+             return "Port";
+     else
+             return "MSTB"; }
+
 static void
-__dump_topology_ref_history(struct

drm_dp_mst_topology_ref_history *history,

drivers/gpu/drm/drm_dp_mst_topology.c +7 -2
-                         void *ptr, const char *type_str)
+dump_topology_ref_history(struct
+drm_dp_mst_topology_ref_history *history, struct
+drm_printer p)
 {
-     struct drm_printer p = drm_debug_printer(DBG_PREFIX);
+     char *connector_name = NULL;
      char *buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
+     void *ptr;
      int i;
 
      if (!buf)
@@ -1656,14 +1647,29 @@ __dump_topology_ref_history(struct
 drm_dp_mst_topology_ref_history *history,
      if (!history->len)
              goto out;
 
+     /* Get a pointer to the actual MSTB/port so we can the
+memory

address to the kernel log */

drivers/gpu/drm/drm_dp_mst_topology.c +3 -0
+     if (history->type == DRM_DP_MST_TOPOLOGY_HISTORY_PORT)
+             ptr = container_of(history, struct
+drm_dp_mst_port,

topology_ref_history);

drivers/gpu/drm/drm_dp_mst_topology.c +4 -0
+     else
+             ptr = container_of(history, struct
+drm_dp_mst_branch, topology_ref_history);
+
      /* First, sort the list so that it goes from oldest to

newest

       * reference entry
       */
      sort(history->entries, history->len,
sizeof(*history->entries),
           topology_ref_history_cmp, NULL);

- drm_printf(&p, "%s (%p) topology count reached 0,
dumping history:\n",

drivers/gpu/drm/drm_dp_mst_topology.c +11 -1
-                type_str, ptr);
+     if (history->type == DRM_DP_MST_TOPOLOGY_HISTORY_PORT)
+{
+             struct drm_dp_mst_port *port = ptr;
+
+             if (port->connector)
+                     connector_name =
+port->connector->name;
+     }
+     if (connector_name)
+             drm_printf(&p, "Port %s (%p) topology ref
+history:\n",

connector_name, ptr);

drivers/gpu/drm/drm_dp_mst_topology.c +5 -0
+     else
+             drm_printf(&p, "%s (%p) topology ref
+history:\n",
+
+topology_ref_history_type_to_str(history->type),

ptr);

for (i = 0; i < history->len; i++) {
const struct drm_dp_mst_topology_ref_entry
*entry = @@
-
1682,24 +1688,92 @@ __dump_topology_ref_history(struct drm_dp_mst_topology_ref_history *history,

                         ts_nsec, rem_nsec / 1000, buf);
      }

- /* Now free the history, since this is the only time
we expose it */

drivers/gpu/drm/drm_dp_mst_topology.c +31 -1
-     kfree(history->entries);
 out:
      kfree(buf);
 }
 
+/**
+ * drm_dp_mst_dump_topology_refs - helper function for
+dumping the topology ref history
+ * @m: File to print to
+ * @mgr: &struct drm_dp_mst_topology_mgr to use
+ *
+ * Prints the topology ref history of all ports and MSTBs
+on @mgr that are still in memory,
+ * regardless of whether they're actually still reachable
+through the topology or not. Only enabled
+ * when %CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS is enabled.
+Can be implemented by drivers to assist
+ * with debugging leaks in the DP MST helpers.
+ */
+void drm_dp_mst_dump_topology_refs(struct seq_file *m,
+struct drm_dp_mst_topology_mgr *mgr) {
+     struct drm_dp_mst_topology_ref_history *history;
+     struct drm_printer p = drm_seq_file_printer(m);
+
+     mutex_lock(&mgr->topology_ref_history_lock);
+     list_for_each_entry(history,
+&mgr->topology_ref_history_list,
+node)
+             dump_topology_ref_history(history, p);
+     mutex_unlock(&mgr->topology_ref_history_lock);
+}
+EXPORT_SYMBOL(drm_dp_mst_dump_topology_refs);
+
+static void
+__init_topology_ref_history(struct
+drm_dp_mst_topology_ref_history

*history,

drivers/gpu/drm/drm_dp_mst_topology.c +17 -0
+                         struct drm_dp_mst_topology_mgr
+*mgr,
+                         enum
+drm_dp_mst_topology_history_type
+type) {
+     history->type = type;
+     INIT_LIST_HEAD(&history->node);
+
+     mutex_lock(&mgr->topology_ref_history_lock);
+     list_add(&history->node,
+&mgr->topology_ref_history_list);
+     mutex_unlock(&mgr->topology_ref_history_lock);
+}
+
+static void
+__destroy_topology_ref_history(struct
+drm_dp_mst_topology_ref_history

*history,

drivers/gpu/drm/drm_dp_mst_topology.c +42 -0
+                            struct drm_dp_mst_topology_mgr
+*mgr) {
+     mutex_lock(&mgr->topology_ref_history_lock);
+     list_del(&mgr->topology_ref_history_list);
+     mutex_unlock(&mgr->topology_ref_history_lock);
+
+     kfree(history->entries); }
+
+static __always_inline void
+init_port_topology_history(struct drm_dp_mst_topology_mgr
+*mgr, struct drm_dp_mst_port *port) {
+
+__init_topology_ref_history(&port->topology_ref_history,
+mgr,
+
+DRM_DP_MST_TOPOLOGY_HISTORY_PORT);
+}
+
+static __always_inline void
+init_mstb_topology_history(struct drm_dp_mst_topology_mgr
+*mgr, struct drm_dp_mst_branch *mstb) {
+
+__init_topology_ref_history(&mstb->topology_ref_history,
+mgr,
+
+DRM_DP_MST_TOPOLOGY_HISTORY_MSTB);
+}
+
+static __always_inline void
+destroy_port_topology_history(struct drm_dp_mst_port *port)
+{
+
+__destroy_topology_ref_history(&port->topology_ref_history,
+port->mgr); }
+
+static __always_inline void
+destroy_mstb_topology_history(struct drm_dp_mst_branch
+*mstb) {
+
+__destroy_topology_ref_history(&mstb->topology_ref_history,
+mstb->mgr); }
+
 static __always_inline void
 dump_mstb_topology_history(struct drm_dp_mst_branch *mstb)

{
- __dump_topology_ref_history(&mstb->topology_ref_history, mstb,

drivers/gpu/drm/drm_dp_mst_topology.c +2 -2
-                                 "MSTB");
+     dump_topology_ref_history(&mstb->topology_ref_history,
+drm_debug_printer(DBG_PREFIX));
 }
 
 static __always_inline void
 dump_port_topology_history(struct drm_dp_mst_port *port)  {
-

__dump_topology_ref_history(&port->topology_ref_history, port,

drivers/gpu/drm/drm_dp_mst_topology.c +2 -1
-                                 "Port");
+     dump_topology_ref_history(&port->topology_ref_history,
+drm_debug_printer(DBG_PREFIX));
 }
 
 static __always_inline void @@ -1729,6 +1803,14 @@

topology_ref_history_unlock(struct drm_dp_mst_topology_mgr *mgr) } #else static inline void

drivers/gpu/drm/drm_dp_mst_topology.c

Quoted 2 lines
drivers/gpu/drm/drm_dp_mst_topology.c +2 -0
+init_port_topology_history(struct drm_dp_mst_topology_mgr
+*mgr, struct drm_dp_mst_port *port); static inline void

Should also add the bracket, otherwise will get warnings. => static inline void init_port_topology_history(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port) {};

drivers/gpu/drm/drm_dp_mst_topology.c

Quoted 2 lines
drivers/gpu/drm/drm_dp_mst_topology.c +2 -0
+init_mstb_topology_history(struct drm_dp_mst_topology_mgr
+*mgr, struct drm_dp_mst_branch *mstb); static inline void

Same as above
=> static inline void init_mstb_topology_history(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_branch *mstb) {};

drivers/gpu/drm/drm_dp_mst_topology.c

Quoted 3 lines
drivers/gpu/drm/drm_dp_mst_topology.c +3 -0
+destroy_port_topology_history(struct
+drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port
+*port); static inline void

destroy_port_topology_history() takes one parameter only which is port.
=> destroy_port_topology_history(struct drm_dp_mst_port *port) {};

drivers/gpu/drm/drm_dp_mst_topology.c

Quoted 3 lines
drivers/gpu/drm/drm_dp_mst_topology.c +3 -0
+destroy_mstb_topology_history(struct
+drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_branch
+*mstb); static inline void

destroy_mstb_topology_history() takes one parameter only which is mstb => destroy_mstb_topology_history(struct drm_dp_mst_branch
*mstb) {};

Quoted 237 lines

topology_ref_history_lock(struct drm_dp_mst_topology_mgr
*mgr) {} static inline void topology_ref_history_unlock(struct drm_dp_mst_topology_mgr *mgr) {} @@ -1740,6 +1822,25 @@ dump_port_topology_history(struct drm_dp_mst_port *port) {} #define save_port_topology_ref(port, type) #endif

drivers/gpu/drm/drm_dp_mst_topology.c +20 -0
+static struct drm_dp_mst_branch *
+drm_dp_add_mst_branch_device(struct drm_dp_mst_topology_mgr
+*mgr,
+u8 lct, u8 *rad) {
+     struct drm_dp_mst_branch *mstb;
+
+     mstb = kzalloc(sizeof(*mstb), GFP_KERNEL);
+     if (!mstb)
+             return NULL;
+
+     mstb->lct = lct;
+     if (lct > 1)
+             memcpy(mstb->rad, rad, lct / 2);
+     INIT_LIST_HEAD(&mstb->ports);
+     kref_init(&mstb->topology_kref);
+     kref_init(&mstb->malloc_kref);
+     init_mstb_topology_history(mgr, mstb);
+     return mstb;
+}
+
 static void drm_dp_destroy_mst_branch_device(struct kref
*kref) {
      struct drm_dp_mst_branch *mstb = @@ -1747,6 +1848,7 @@
static void drm_dp_destroy_mst_branch_device(struct
kref *kref)
      struct drm_dp_mst_topology_mgr *mgr = mstb->mgr;

      dump_mstb_topology_history(mstb);
drivers/gpu/drm/drm_dp_mst_topology.c +1 -0
+     destroy_mstb_topology_history(mstb);
 
      INIT_LIST_HEAD(&mstb->destroy_next);
@@ -1856,6 +1958,7 @@ static void drm_dp_destroy_port(struct

kref
*kref)

      struct drm_dp_mst_topology_mgr *mgr = port->mgr;

      dump_port_topology_history(port);

+ destroy_port_topology_history(port);

      /* There's nothing that needs locking to destroy an
input port yet */
      if (port->input) {
drivers/gpu/drm/drm_dp_mst_topology.c +2 -1
@@ -2135,7 +2238,7 @@ drm_dp_port_set_pdt(struct
 drm_dp_mst_port *port, u8 new_pdt,
                      ret =
 drm_dp_mst_register_i2c_bus(port);
              } else {
                      lct = drm_dp_calculate_rad(port, rad);
-                     mstb =
 drm_dp_add_mst_branch_device(lct, rad);
+                     mstb =
+drm_dp_add_mst_branch_device(mgr, lct, rad);
                      if (!mstb) {
                              ret = -ENOMEM;
                              drm_err(mgr->dev, "Failed to

create MSTB for port %p", port); @@ -2366,6 +2469,8 @@ drm_dp_mst_add_port(struct drm_device *dev,

       */
      drm_dp_mst_get_mstb_malloc(mstb);
drivers/gpu/drm/drm_dp_mst_topology.c +2 -0
+     init_port_topology_history(mgr, port);
+
      return port;
 }
@@ -3745,7 +3850,7 @@ int

drm_dp_mst_topology_mgr_set_mst(struct
drm_dp_mst_topology_mgr *mgr, bool ms

              }

              /* add initial branch device at LCT 1 */
drivers/gpu/drm/drm_dp_mst_topology.c +2 -1
-             mstb = drm_dp_add_mst_branch_device(1, NULL);
+             mstb = drm_dp_add_mst_branch_device(mgr, 1,
+NULL);
              if (mstb == NULL) {
                      ret = -ENOMEM;
                      goto out_unlock; @@ -5512,14 +5617,16

@@ int drm_dp_mst_topology_mgr_init(struct
drm_dp_mst_topology_mgr *mgr,

      mutex_init(&mgr->delayed_destroy_lock);
      mutex_init(&mgr->up_req_lock);
      mutex_init(&mgr->probe_lock); -#if

IS_ENABLED(CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS)

drivers/gpu/drm/drm_dp_mst_topology.c +5 -2
-     mutex_init(&mgr->topology_ref_history_lock);
-#endif
      INIT_LIST_HEAD(&mgr->tx_msg_downq);
      INIT_LIST_HEAD(&mgr->destroy_port_list);
      INIT_LIST_HEAD(&mgr->destroy_branch_device_list);
      INIT_LIST_HEAD(&mgr->up_req_list);
 
+#if IS_ENABLED(CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS)
+     mutex_init(&mgr->topology_ref_history_lock);
+     INIT_LIST_HEAD(&mgr->topology_ref_history_list);
+#endif
+
      /*
       * delayed_destroy_work will be queued on a dedicated

WQ, so that any
* requeuing will be also flushed when deiniting the
topology manager.

drivers/gpu/drm/nouveau/nouveau_debugfs.c +4 -0
index 1cbe01048b93..53ec7c1daada 100644
@@ -29,9 +29,13 @@
  */
 
 #include <linux/debugfs.h>
+#include <drm/drm_dp_mst_helper.h> #include
+<drm/drm_encoder.h>
 #include <nvif/class.h>
 #include <nvif/if0001.h>
+#include <nouveau_encoder.h>
 #include "nouveau_debugfs.h"
+#include "nouveau_display.h"
 #include "nouveau_drv.h"
 
 static int
@@ -68,6 +72,34 @@ nouveau_debugfs_strap_peek(struct

seq_file *m, void
*data)

      return 0;

}

drivers/gpu/drm/nouveau/nouveau_debugfs.c +30 -0
+#ifdef CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS
+static int nouveau_debugfs_mst_topology_history(struct
+seq_file *m, void *data) {
+     struct drm_info_node *node = m->private;
+     struct drm_device *dev = node->minor->dev;
+     struct drm_encoder *encoder;
+     struct nouveau_display *disp = nouveau_display(dev);
+
+     if (disp->disp.object.oclass < GF110_DISP)
+             return -EINVAL;
+
+     drm_for_each_encoder(encoder, dev) {
+             struct nv50_mstm *mstm;
+
+             /* We need the real encoder for each MST mgr
+*/
+             if (encoder->encoder_type !=
+DRM_MODE_ENCODER_TMDS)
+                     continue;
+             mstm = nouveau_encoder(encoder)->dp.mstm;
+             if (!mstm)
+                     continue;
+
+             seq_printf(m, "MSTM on %s:\n", encoder->name);
+             drm_dp_mst_dump_topology_refs(m, &mstm->mgr);
+     }
+     return 0;
+}
+#endif
+
 static int
 nouveau_debugfs_pstate_get(struct seq_file *m, void *data)

{ @@
-213,6
+245,9 @@ static const struct file_operations nouveau_pstate_fops = { static struct drm_info_list nouveau_debugfs_list[] = {
{ "vbios.rom", nouveau_debugfs_vbios_image, 0, NULL
},
{ "strap_peek", nouveau_debugfs_strap_peek, 0, NULL },

drivers/gpu/drm/nouveau/nouveau_debugfs.c +3 -0
+#ifdef CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS
+     { "dp_mst_topology_refs",
+nouveau_debugfs_mst_topology_history, 0, NULL }, #endif
 };
 #define NOUVEAU_DEBUGFS_ENTRIES

ARRAY_SIZE(nouveau_debugfs_list)

(quoted diff) +0 -0

b/include/drm/drm_dp_mst_helper.h index
78044ac5b59b..e3a73d8d97c0
100644

include/drm/drm_dp_mst_helper.h +8 -1
@@ -35,6 +35,11 @@ enum drm_dp_mst_topology_ref_type {
      DRM_DP_MST_TOPOLOGY_REF_PUT,
 };
 
+enum drm_dp_mst_topology_history_type {
+     DRM_DP_MST_TOPOLOGY_HISTORY_PORT,
+     DRM_DP_MST_TOPOLOGY_HISTORY_MSTB, };
+
 struct drm_dp_mst_topology_ref_history {
      struct drm_dp_mst_topology_ref_entry {
              enum drm_dp_mst_topology_ref_type type; @@
-43,6
+48,9 @@ struct drm_dp_mst_topology_ref_history {
              depot_stack_handle_t backtrace;
      } *entries;
      int len;
+
+     enum drm_dp_mst_topology_history_type type;
+     struct list_head node;
 };
 #endif /* IS_ENABLED(CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS)

*/

include/drm/drm_dp_mst_helper.h +4 -0
@@ -769,6 +777,12 @@ struct drm_dp_mst_topology_mgr {
       * &drm_dp_mst_branch.topology_ref_history.
       */
      struct mutex topology_ref_history_lock;
+
+     /**
+      * @topology_ref_history_list: contains a list of
+topology ref

histories for any MSTBs or

include/drm/drm_dp_mst_helper.h +7 -0
+      * ports that are still in memory
+      */
+     struct list_head topology_ref_history_list;
 #endif
 };
@@ -873,6 +887,10 @@ void drm_dp_mst_put_port_malloc(struct
 drm_dp_mst_port *port);
 
 struct drm_dp_aux *drm_dp_mst_dsc_aux_for_port(struct
 drm_dp_mst_port *port);
 
+#ifdef CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS
+void drm_dp_mst_dump_topology_refs(struct seq_file *m,
+struct drm_dp_mst_topology_mgr *mgr); #endif
+
 extern const struct drm_private_state_funcs

drm_dp_mst_topology_state_funcs;

Quoted 4 lines
Quoted 3 lines

/**
--
2.31.1

--
Cheers,
Lyude Paul (she/her)
Software Engineer at Red Hat

Quoted 6 lines
Quoted 2 lines

--
Cheers, Lyude Paul (she/her) Software Engineer at Red Hat

--
Regards,
Wayne Lin

Quoted 4 lines

--
Cheers,
Lyude Paul (she/her)
Software Engineer at Red Hat

--
Regards,
Wayne Lin

A message that is not in the archive (sent off-list or not yet received): CO6PR12MB5489AA5549DC251E202BD58AFC3E9@CO6PR12MB5489.namprd12.prod.outlook.com

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): CO6PR12MB54892DC34D5D2FA0886657BFFC099@CO6PR12MB5489.namprd12.prod.outlook.com

author
#16 lore raw

(Adding this back to the dri-devel mailing list since I didn't notice it got dropped from there)

Hm, some comments on this issue down below. Sorry for the delayed response, I was going to try this right after I finished the MST legacy removal but that's ending up taking longer than I hoped.

Quoted 19 lines · Wayne

[AMD Official Use Only]

Oops...
I didn't notice that I replied to wrong mail previously : P Sorry for the confusion and reply to the correct thread.

Good day!
I probably know why you can't reproduce the issue there. Please refer to the attached
patch file which makes me easier to explain this.

In dm_dp_mst_get_modes(), we will create a new dc_sink by calling dc_link_add_remote_sink() and add that dc_sink into the array dc_link-

Quoted 1 line

remote_sinks[].

However, if we find that aconnector->dc_sink is not null, then we won't create a new
dc_sink for that aconnector and won't add the sink to the array dc_link-

Quoted 1 line

remote_sinks[].

Quoted 16 lines

When the issue that I mentioned before occurs, we won't be able to release the dc_sink
for the aconnector when we unplug the sst monitor. Hence, we can't create new dc_sink
for the latter on connected new stream sink of that port. Which leads to two issues
1. Unplug monitor and plug in another monitor "to the same port" =>  Since we use the same dc_sink to reflect the capability of the new connected stream
sink, we might send out inappropriate stream to the new connected sink.
2. Because we can't release dc_sink in the array dc_link->remote_sinks[], when we
plug/unplug sst monitor to more than 4 different mst port, we will break the criteria
"dc_link->sink_count >= MAX_SINKS_PER_LINK" in link_add_remote_sink_helper().

Ok, a lot of this seems to be happening in amdgpu which certainly explains why I had so much trouble following along with this originally (also, having learned a bit more about how DC works definitely has helped a bit). I already see a bunch of issues though with how this code is structured that would definitely break things, though note I haven't sat down and tried this on a real machine yet - will try tomorrow.

So - it seems like dc_sink == a bunch of hotplugging state for a dm connector, which actually tells me for one that this is definitely the wrong spot for this code. get_modes() really should just handle filling out DRM modes and pretty much nothing else, because callers from DRM aren't going to expect side-effects like this when get_modes() is called - which could potentially lead to all sorts of weirdness down the line if the DRM call paths that use this ever change. i915 and nouveau have good examples of what these functions should typically look like, and amdgpu actually seems to mostly follow this advice for it's other get_modes() callback.

Note there's also another problem here: the assumption "no EDID == disconnected" isn't correct. It's totally possible to run into EDID-less displays if the display is some ancient pre-historic relic, or if the ROM (or EEPROM? can't remember what type of chip computer displays tend to use…) chip in the monitor containing the EDID has died. Note that the second situation is suprisingly more common then one might think! I ran into a 140Hz 4k ASUS display where this happened, and I know another friend of mine who had the ROM in their display die randomly as well.

So with this being said: I, think the solution here is actually just to add/remove dc_sink from dm_dp_mst_detect() based on the CSN, and drop the calls from get_modes()? detect() should always be called before the connector has been removed, and we could use the return status from drm_dp_mst_detect_port() in order to tell whether or not we should add/remove the dc_sink.

If you don't get to it first tomorrow, I'll see if I can reproduce this locally and try writing up a patch.

Also a final note: I don't think it's a good idea in general for there to be a fixed size array of dc_sinks like this. Those probably should just be stored somehow on a per-connector basis. I say this because in addition to the fact MST connectors aren't destroyed immediately (which could lead to a situation where we have more then 6 connected MST ports briefly before their destructors run, e.g. during suspend/resume reprobing).

Also, one question below:

Quoted 19 lines

Anyway, these are my steps to reproduce the issue.
I use a startech mst 1to4 mst hub, a sst monitor and a mst monitor.

1. boot up with sst monitor connected to the hub and plug the hub to the system.
2. unplug/plug sst monitor to all left three different DP output ports. Which is used to
Pollute all four aconnectors to have them keep the stale dc_sinks.
3. plug in the mst monitor to arbitrary ports. Since the new connected mst monitor will
have us add new mst port to the topology and attach a new aconnector for that mst port,
we'll try to create a new dc_sink to that new aconnector and break the sink_count
limitation. As you can see in the log the error message : "*ERROR* Unable to add a
remote sink"

Quoted 5 lines

(Note that different hub might influence the result. Some hubs will pull down the HPD
when there is no monitor connected to its downstream ports which will run into different
code flow)

by "pull down" I assume you mean doing a long HPD instead of short (or vice versa?).

Quoted 1,927 lines

I also attach the dmesg log under different debug level (0x104 & 0x116) for your reference.

Please let me know if you can reproduce then : )

Sorry about that Lyude and hope this time I send to the right thread.. Really appreciate for your help!!!

Regards,
Wayne

Quoted 1,915 lines

-----Original Message-----
From: Lyude Paul <[email protected]>
Sent: Wednesday, March 2, 2022 9:10 AM
To: Lin, Wayne <[email protected]>
Subject: Re: [PATCH] WIP: drm/dp_mst: Add support for dumping topology ref histories from debugfs

Ran into some issues with my system yesterday (hooray being the first to find
a rather annoying zsh bug that broke my scripts!), but I've finally got things
setup. I'm not able to reproduce this issue though (I've tried hotplugging the
way you mentioned here, but nothing seems to leak), which makes me think there's something special in your setup we might not have noticed.

Do you think you could get some drm logs with drm.debug=0x116 log_buf_len=50M
from when this happens so I can get a better idea of the drm events leading up
to this? Also, if you've been testing this in a GUI could you confirm whether
this happens when you're just in a plain old VT?

Quoted 5 lines · Lyude Paul

Alright - starting to look into this now and setting up a system to try reproducing this, will let you know as soon as I've got results. Sorry again
about all of the delays

Quoted 86 lines
Quoted 85 lines · Lyude Paul

I didn't get a chance yet, but I've gotten through most of my backlog now.
Sorry about the delay - I've been catching up since I had to take two weeks
off work very unexpectedly. I'll put aside time to look at this first thing
monday.

Quoted 76 lines · Wayne

[AMD Official Use Only]

Hi Lyude,

Good day!
Sorry to bother you, but would like to know if you can reproduce the issue?

Thanks!

Regards,
Wayne

Quoted 63 lines

-----Original Message-----
From: Lyude Paul <[email protected]>
Sent: Thursday, February 3, 2022 4:12 AM
To: Lin, Wayne <[email protected]>
Cc: [email protected]
Subject: Re: [PATCH] WIP: drm/dp_mst: Add support for dumping topology
ref
histories from debugfs

JFYI I should be able to respond to this soon, I think I'm following
your
issue a bit more (haven't gotten to go through the patches too so I'm
probably
going to see if I can reproduce it locally as well. Sorry for the delay
-
dealing with some X.org stuff and work issues at the moment

Quoted 41 lines · Wayne

[Public]

Hey Lyude,

Sorry, just got chance to get back to this and BIG THANKS to elaborating
in
such details!
Following are some of my comments : )

Quoted 10 lines

-----Original Message-----
From: Lyude Paul <[email protected]>
Sent: Wednesday, January 19, 2022 9:59 AM
To: Lin, Wayne <[email protected]>
Cc: [email protected]
Subject: Re: [PATCH] WIP: drm/dp_mst: Add support for dumping topology
ref
histories from debugfs

Quoted 3 lines

Hey - so, the original issue here was that payloads were not always
deleted when we expected them to be - correct?

Actually, what I was trying to do is not relevant to payload maintenance.
What I wanted to do is having a way to notify
amdgpu to release its enumerated resource "dc_sink" when unplug event
occurs. Currently, under specific scenarios,
we can't release remote "dc_sink" and ends up can't allocate resource
for
new connected monitors. I'll explain the scenario underneath.

So, get back to the main issue that I was trying to solve when I pushed
the
patchset:

https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Fpatchwork.kernel.org%2Fproject%2Fdri&amp;data=04%7C01%7Cw
ayne.lin%40amd.com%7Cbbc27043edc04bf8067c08d9fbe9620c%7C3dd8961fe4884e608e 11a82d994e183d%7C0%7C0%7C6378178020798 98960%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6 Ik1haWwiLCJXVCI6Mn0%3D%7C3000&amp;sdat a=RVUc8KgTO3mhFL9qemshHJqiyXJs%2Fvytx21AMyUaW%2Bs%3D&amp;reserved=0

Quoted 1,781 lines
Quoted 1,781 lines
Quoted 1,781 lines
Quoted 1,781 lines
Quoted 1 line

-

devel%2Fcover%2F20210720160342.11415-1- Wayne.Lin%40amd.com%2F&amp;data=04%7C01%7Cwayne.lin%40amd.com%7C2e 19d9
32
1b
5f4e948c1208d9e6884f24%7C3dd8961fe488 4e608e11a82d994e183d%7C0%7C0%7C637794295422403301%7CUnknown%7CTWFp bGZs
b3
d8
eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiL CJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&amp;sdata=rvtwQioO3pT1BoHb7S5 32ms
Ra
61
gP1vIyE8AB7M9I4E%3D&amp;reserved=0

Quoted 1,763 lines

I was trying to fix some problems that I observed after these 2 patches
* 09b974e8983 drm/amd/amdgpu_dm/mst: Remove -

Quoted 1 line

destroy_connector()

callback
* 72dc0f51591 drm/dp_mst: Remove
drm_dp_mst_topology_cbs.destroy_connector

With above patches, it changes our flow to remove dc_sink (by calling
dc_link_remote_sink()) when connector is about to be destroyed. And let's see below use cases:
a). Disconnection happens between mst branch devices e.g.
src - 1st_mstb - 2nd_mstb - sst_monitor => src - 1st_mstb (disconnect)
2nd_mstb - sst_monitor

In above case, after receiving CSN, we will put topology reference
count
of
2nd mstb and its port which connected to the sst monitor. As you suggested before, we will destroy 2nd mst branch device and
its
all
ports due to there is no more mst path can route to them. As the result of that, we at end call drm_dp_delayed_destroy_port()
and
unregister/put the drm connector and eventually call dc_link_remote_sink() to release the dc_sinck we enumerate for the
specific
stream sink of sst_monitor.

However, in below case:
b). Disconnection happens between mst branch devices and SST monitor
e.g.
src - 1st_mstb - sst_monitor => src - 1st_mstb (disconnect) sst_monitor

In this case, which is the case having problem, it definitely won't
decrease
the topology references count of the port of 1st_mstb which was connected to the sst monitor to zero since the port is still existing in
the topology. Same as the malloc reference since the port can't get
destroyed. Hence, the port still exists and we won't call drm_dp_delayed_destroy_port() to unregister and put the drm connector.
As
the
result of that, we can't release corresponding remote dc_sink under
this
case. And after few times hotplugs, we can't create any new dc_sink since the number of stream sink is exceeding our limitation.

Hence, what I'm thinking here is to register a callback function for
notifying us that the remote sink is detached. This just aligns what
we
do
for handling long HPD event (unplug monitor from system) of legacy
DP
(none
mst). For which case, once we detect long HPD event indicating the monitor is detached, we will immediately try to release
the
dc_link->local_sink and fire hotplug event to upper layer. Same as here, once receives a CSN message notifying a drm connector
is
changed from connected to disconnected, trigger the callback function and we can try to release the dc_sink resource.

Quoted 57 lines

If I'm remembering that correctly, then (and I'm not 100% sure on
this
yet) I might already have noticed something very possibly wonky in
how we handle payload allocations currently, that I found while
working on
the non-atomic removal branch that I linked to you in my previous response. Basically, in the
step1() function it looks like that we follow this general flow
with
updating
payloads:

 * Loop through proposed payloads and compare to previously programmed
   payloads
    - If a payload has a different allocation then it previously
did,
update the
      payload
    - If the payload is new, create it
    - If a payload no longer has an allocation, remove the payload

At first glance this seems totally correct - but I'm not actually
entirely
convinced it is? Particularly because it seems like the order in
which
we do creation/deletion of payloads is totally arbitrary. To explain
what
I mean by that, consider a state transition like this:

vcpi_slots=15 vcpi_slots=35 vcpi_slots=14

Quoted 1 line

1 | 2 |xxxxxxxx|

Let's say we want to increase payload #1 from 15 to 50, and disable
payload #2 in the same atomic commit on DRM's side. If the order
we
update payloads is entirely arbitrary, we could end up doing this:

 * Increase VCPI slots payload #1 from 15->50 (total VCPI slots=99,
overflow!)
 * Decrease VCPI slots payload #2 from 35->0  (total VCPI slots=50)

Quoted 11 lines

Notice on the first step, we've technically overflowed the available
number of VCPI slots in the payload table. This is still before
step
2
though, and I could be totally wrong here - perhaps this is entirely
OK in
the real world, and we're allowed to overflow VC slots as long as

I think it's forbidden to allocate time slots beyond 64 when we're
updating
payload ID table of the branch in the real world. Branch probably
will NACK the DPCD write.

Quoted 11 lines

we repair the issue before step 2. But so far I can't seem to find
anything in the DP 2.0 spec that explicitly states this would be
OK
-
which
makes me think we might fail some payload allocations if we don't
always
ensure we avoid overflows in between VC payload table changes

Agree.
For amdgpu, I think we always do deallocation (call hws-

Quoted 1 line

funcs.reset_hw_ctx_wrap()) before allocation

(apply_single_controller_ctx_to_hw()).

Quoted 23 lines

Note that avoiding overflows would be as simple as just making sure
we
send all VC payload table changes that free VC slots before sending
any that take new slots.

Again - I haven't actually confirmed this yet and am hoping to test
stuff
like this very soon as I'm pretty close finishing up the initial
attempt
at removing the non-atomic MST code in the DRM helpers now. If my
theory
ends up being correct here, I can fix this in my rewrite of the
MST payload management code. But I figured it might be worth mentioning in
the mean time in case you think it might be relevant to the problem here :).

Quoted 93 lines
Quoted 92 lines · Lyude Paul

(CC'ing this to dri-devel, since this is basically patch review)

Alright - so first off sorry about the broken debugging patch! I
thought I had tested that but I guess I hadn't tested it well
enough,
I'll get it fixed asap, but feel free to try getting to it before
I
do.

As for this patch... (comments below)

Quoted 62 lines · Wayne

[AMD Official Use Only]

Hi Lyude,

No rush and thanks for your time! : ) Just want to help clarify
what
I mean that "registering a callback function"
for CSN of disconnecting
stream sink device (not mst branch case). Attach below the tentative
patch that I planned to do. Thanks so much!

Regards,
Wayne
---
 .../display/amdgpu_dm/amdgpu_dm_mst_types.c   | 53
+++++++++++++++++++
 drivers/gpu/drm/drm_dp_mst_topology.c         | 16 +++++- include/drm/drm_dp_mst_helper.h               |  1 +

 3 files changed, 68 insertions(+), 2 deletions(-)

diff --git a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_mst_type s.c b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_mst_type s.c
index cc34a35d0bcb..d7343c64908c 100644
---
a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_mst_type s.c
+++
b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_mst_type s.c

(quoted diff) +22 -0
@@ -472,8 +472,61 @@ dm_dp_add_mst_connector(struct
 drm_dp_mst_topology_mgr *mgr,
         return connector;
  }
 
+void dm_dp_notify_csn_disconnection(struct drm_connector
+*connector) {
+       struct amdgpu_dm_connector *aconnector =
+               to_amdgpu_dm_connector(connector);
+       struct dc_sink *dc_sink = aconnector->dc_sink;
+       struct dc_link *dc_link = aconnector->dc_link;
+       struct amdgpu_device *adev = drm_to_adev(ddev);
+
+       ASSERT(dc_link);
+
+       if (dc_sink) {
+               mutex_lock(&adev->dm.dc_lock);
+
+               /*clear the remote sink of the link*/
+               dc_link_remove_remote_sink(dc_link,
 dc_sink);
+               dc_sink_release(dc_sink);
+               aconnector->dc_sink = NULL;
+
+               mutex_unlock(&adev->dm.dc_lock);
+       }
+}
+
Quoted 6 lines

 static const struct drm_dp_mst_topology_cbs dm_mst_cbs = {
        .add_connector = dm_dp_add_mst_connector, +       .notify_csn_disconnection = dm_dp_notify_csn_disconnection,
 };

I still don't really think this is a good idea. This seems like
it's
just adding another hotplugging path to the code in order to avoid
sending hotplugs for non-endpoint devices. In addition to the

So, what I wanted to do is to add a callback function for endpoint
devices
case as described above. For disconnection of non-endpoint devices
is
already taken care by current code.

Quoted 45 lines
Quoted 45 lines

drm_connector issues I mentioned before, we also really need to
stop
doing any kind of payload maintence in hotplugging paths. The
reality
is any kind of payload maintanence we do outside of normal modesetting
paths is a relic from legacy modesetting that I'm dropping ASAP
(more
on this below), and we can't keep adding to it because it dramatically
complicates maintanence as well.

Sorry for repeating this point so often but - the biggest issue
too
is
I'm still not sure what it is we're even avoiding here. We know
resources aren't released consistently, and that we're able to
reproduce the behavior with repeated hotplugs. We also know that
if
we
skip sending certain hotplug events, that fixes the issue. And
we
know
we can workaround it by adding a special case for forcing a payload
release in DC. But none of those actually tell us exactly what
piece
of code is leaking and why, which means that any workarounds we're
putting in to avoid this mysterious guilty section of code we
don't
entirely understand either - which means we're just adding more
code

Quoted 8 lines
Quoted 8 lines

in that no one actually fully understands. This just ends up making
maintence difficult because every change in code nearby workarounds
like this has to strugle to try to figure out said workarounds
in
order to avoid breaking things.

Thank you Lyude for the reminder and I totally agree with you by avoiding
adding workaround since it will get the code maintenance harder. And
I
think the root cause of the issue that I observed before is quite
concrete.
There is no code path for us to release dc_sink under the unplug scenario
that I described above.

Quoted 115 lines
Quoted 115 lines

I'm actually currently running into these "later" issues right
now,
as
recently I've (-actively-, finally!!!) been working on trying to
remove as much non-atomic MST as I can because. As it turns out
-
a
huge amount of the payload maintanence code just isn't needed at
all
when we stop caring about non-atomic drivers and stick everything
in
atomic state structs. Step 1 for updating updating the payload
table,
e.g. drm_dp_update_payload_part1(), is a great example of how
messy
things have become. Here's a small sample of some of the stuff
I've
seen from just that one function so far that either just don't
make
sense here or is totally redundant. I should note that a lot of
these
things also come from patches I reviwed, but didn't really look
at
as
closely as I should have because I was swamped at work, some are
historical artifacts, and others are less-than-ideal patches I
got
wrote myself when I was first started working on MST and didn't
know
the code as well as I do
now:

 * We try to avoid some sort of userspace issue by using drm_dp_mst_port_downstream_of_branch() to avoid releasing payloads
for a
   branch if we can't prove it's downstream of the top of the
topology. This
   seems to workaround either a userspace bug. This is a redundant,
since
   that's what topology refs are already supposed to be doing to
the
extent is
   reasonably possibly. It's also unfortunately racy solution
because
we have
   to be able to handle the situation where a connector is removed
from under
   us. That can happen at any time, including _immediately_ after
we
call
   drm_dp_mst_port_downstream_of_branch() - rendering the call
not
really
   useful.
 * If we fail to validate the sink in drm_dp_update_payload_part,
we
don't
   update the payload table. I think at best this solution is
racy
and
not
   useful, at worst it leaves us with a payload table that doesn't
match what
   we attempted to set in the atomic state - which at worst brings
us
into
   undefined territory where we're just plain out of sync with
the
reality in
   hw.
 * Actually fun fact - mgr->payloads and mgr->proposed_vcpis both
can
and
   definitely should be removed entirely. All of the info for
mgr->payloads
   could just be in the atomic state, because that + the magic
of
atomic state
   duplication means we'll also have an accurate view of the previous
state's
   payload allocations: which renders mgr->proposed_vcpis redundant.

Quoted 3 lines
Quoted 3 lines

Apologies for the long explanation again, but I hope that explains
my

Really really thanks for your kindly help on this : ) I'll try my best to get to your WIP branch soon. Thank you Lyude!

Quoted 1,264 lines
Quoted 76 lines

point here a bit. I'm going to be trying to get to moving amdgpu's
DSC
code out of amdgpu and into DRM helpers as well soon, so I'm really
determined to clean stuff up beforehand as every time I've done
so
it's become substantially easier to make changes to this code.
Things
used to be even worse before I started cleaning things up 2 or 3
years
ago, where simple changes would end up getting me stuck spending
hours
trying to dig through lockdep or memory manegement issues. As
well,
I
would be entirely unsurprised if bugs like this very payload leak
we're working on just disappear once we've gotten rid of all the
extraneous workarounds and state tracking here - especially with
how
many special cases we have for maintaining the payload table right
now. That's certainly ended up being the case in the past with a
number of other difficult to track down issues I've dealt with
in
MST.

Anyhow. I've been way more productive this year then last because
I
had over a month off work and am finally not super burnt out from
my
job, and so far I've been making progress on the payload state
cleanup
far faster then I was last year :). I think if you can't figure
out
where the leak is coming from even once I get the debugging patches
I
mentioned fixed up, it might be a good idea for us to try again
after
I've got some of this code cleaned up. I've got a currently WIP
branch
here:

https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgitl
ab.freedesktop.org%2Flyudess%2Flinux%2F- %2Fcommit%2F624dd68fa804e64b5b 2060e4735d7317090692b5&amp;data=04%7C01%7Cwayne.lin%40amd.co m%7C
d4
bd
c7 59eb274bfccc8208d9daef41fe%7C3dd8961fe4884e608e11a82d994e183 d%7C
0%
7C
0% 7C637781543416430632%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjA wMDA
iL

Quoted 1,187 lines

CJ
QI joiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&amp;sdata=n FpVl
%2
BV
oF 40JPabfRMcR6Cz7ZHDwt%2BBpLDBNdXftJaA%3D&amp;reserved=0

Quoted 1,177 lines

 void amdgpu_dm_initialize_dp_connector(struct
amdgpu_display_manager *dm, diff --git
a/drivers/gpu/drm/drm_dp_mst_topology.c
b/drivers/gpu/drm/drm_dp_mst_topology.c
index 857c5d15e81d..a70e26c5d084 100644

drivers/gpu/drm/drm_dp_mst_topology.c +1 -0
@@ -2508,6 +2508,7 @@ drm_dp_mst_handle_conn_stat(struct
 drm_dp_mst_branch *mstb,
         u8 new_pdt;
         bool new_mcs;
         bool dowork = false, create_connector = false;
+       bool notify_unplug_event = false;
 

        port = drm_dp_get_port(mstb, conn_stat-

Quoted 1 line

port_number);

        if (!port)

drivers/gpu/drm/drm_dp_mst_topology.c +0 -0
@@ -2520,6 +2521,9 @@ drm_dp_mst_handle_conn_stat(struct
 drm_dp_mst_branch *mstb,
                          * port, so just throw the port

out
and
make
sure we
                         * reprobe the link address of it's
parent
MSTB
                         */
+                       /* should also consider notify_unplug_event
here.
+                        * but it's not a normal case for products
+in the
market
+                        */

drm_dp_mst_topology_unlink_port(mgr,
port);
                        mstb->link_address_sent = false; dowork = true; @@ -2541,10 +2545,14 @@
drm_dp_mst_handle_conn_stat(struct drm_dp_mst_branch
*mstb,
        port->ddps = conn_stat-

Quoted 1 line

displayport_device_plug_status;

        if (old_ddps != port->ddps) {
-               if (port->ddps && !port->input)
+               if (port->ddps && !port->input) {

drm_dp_send_enum_path_resources(mgr,
mstb,
port);
-               else
+               } else {
                        port->full_pbn = 0;
+                       if (port->connector &&
+
drm_dp_mst_is_end_device(port-

Quoted 1 line

pdt,

+port-

Quoted 1 line

mcs))

+                               notify_unplug_event = true;
+               }
        }

        new_pdt = port->input ? DP_PEER_DEVICE_NONE :
conn_stat-

Quoted 1 line

peer_device_type;

drivers/gpu/drm/drm_dp_mst_topology.c +0 -0
@@ -2557,11 +2565,15 @@ drm_dp_mst_handle_conn_stat(struct

drm_dp_mst_branch
*mstb,
                dowork = false;
        }

+       if (notify_unplug_event && +mgr->cbs->notify_csn_disconnection)
+               mgr->cbs->notify_csn_disconnection(port-

Quoted 1 line

connector);

+
        if (port->connector) drm_modeset_unlock(&mgr->base.lock); else if (create_connector) drm_dp_mst_port_add_connector(mstb, port);

+
 out:
        drm_dp_mst_topology_put_port(port);
        if (dowork)

(quoted diff) +0 -0

b/include/drm/drm_dp_mst_helper.h index
78044ac5b59b..ff9e47729841
100644

include/drm/drm_dp_mst_helper.h +0 -0
@@ -525,6 +525,7 @@ struct drm_dp_mst_topology_cbs {
          * IRQ pulse handler.
          */

        void (*poll_hpd_irq)(struct
drm_dp_mst_topology_mgr
*mgr);
+       void (*notify_csn_disconnection)(struct
drm_connector
+*connector);
 };

 #define DP_MAX_PAYLOAD (sizeof(unsigned long) * 8) --
2.31.0

Quoted 1,061 lines

-----Original Message-----
From: Lin, Wayne
Sent: Wednesday, December 8, 2021 11:39 AM
To: 'Lyude Paul' <[email protected]>
Subject: RE: [PATCH] WIP: drm/dp_mst: Add support for dumping
topology ref histories from debugfs

No worries Lyude!
Thanks for keeping helping on this. Take your time : )

Quoted 1,050 lines

-----Original Message-----
From: Lyude Paul <[email protected]>
Sent: Wednesday, December 8, 2021 7:05 AM
To: Lin, Wayne <[email protected]>
Subject: Re: [PATCH] WIP: drm/dp_mst: Add support for dumping
topology ref histories from debugfs

Sorry! I will try to get to this tomorrow, if not then sometime
this week.

Quoted 1,032 lines · Wayne

[Public]

Hi Lyude,

Finally have some bandwidth to get back to this
problem!
I roughly went through this patch and I'm just aware
that
we
already have such kind of convenient tool for a
while.
I think it's definitely useful for us to track
port/mstb
reference count issues and I'll start to embrace
this
feature
for cleaning up those issues. Thank you Lyude!

However, I think the issue that I was trying to fix
is
not
related to what you suggested:
" The idea here is that if stream resources aren't
being
released, my guess would be that we're not dropping
topology
references for the port which means the connector
never
goes
away."
The issue I was trying to fix is about releasing
dc_link->remote_sinks while receiving a CSN message
notifying
the connection status of a sst connector of a port
changed
from connected to disconnected. Not the connection
status
changed of a mst branch device.
e.g.
src - 1st_mstb - 2nd_mstb - sst_monitor => src -
1st_mstb
(disconnect) 2nd_mstb - sst_monitor

In above case, after receiving CSN, we will put
topology
references of 2nd mstb and its port which is
connected
with
the sst monitor. As the result of that, we can call
drm_dp_delayed_destroy_port() to unregister and put
the
drm
connector.

However, in below case:
e.g.
src - 1st_mstb - sst_monitor => src - 1st_mstb
(disconnect)
sst_monitor

In this case, which is the case having problem, it
definitely
won't decrease the topology references count of the
port
which
was connected to the sst monitor to zero since the
port
is
still existing in the topology. Same as the malloc
reference
since the port can't get destroyed. Hence, the port
still
exists  and we won't call
drm_dp_delayed_destroy_port() to unregister and put
the
drm
connector.
I looked up the code and
drm_dp_delayed_destroy_port()
seems
like the only place to call drm_connector_put()
which
means
we
can't put reference count of drm connector under
this
case
and
can't release dc_sink resource by destroying drm
connector.

I would also like to point out that this resource
(remote_sinks) is specific to different stream
sinks. So
if
we're trying to release this dc_sink resource by
destroying
the drm connector, it conflicts the idea that you
suggested
before that we should always keep the drm connector
until
it's
no longer reachable in the topology.
Releasing dc_sink should be binding with the
disconnection
event.

I understand your concern that we should not just
easily
change the logic here since it's the result after
solving
tons
of bugs before and might cause other side effect.
So,
just
my
2 cents, what I'm thinking is to register a callback
function
for our driver to notify us that the remote sink is
detached.
This just aligns our flow handling long HPD event of
legacy
(sst) DP.
For sst case, once we detect long HPD event
indicating
the
monitor is detached, we will immediately try to
release
the
dc_link->local_sink and fire hotplug event to upper
layer.
Same as here, once receives a CSN message notifying
a
drm
connector is changed from connected to disconnected,
trigger
the callback function and we can try to release the
dc_sink
resource.

Would like to know your thought and insight please :
)

Btw, I got some errors and warnings while building
and
have
some code adjustments as below : ) Thank you Lyude
for
your
always kindly help!!

Regards,
Wayne
-----Original Message-----
From: Lyude Paul <[email protected]>
Sent: Wednesday, November 3, 2021 7:15 AM
To: Lin, Wayne <[email protected]>
Subject: [PATCH] WIP: drm/dp_mst: Add support for
dumping
topology ref histories from debugfs

TODO:
* Implement support for i915
* Finish writing this commit message
* ???
* profit

Signed-off-by: Lyude Paul <[email protected]>
---

Hey wayne! SO-hopefully if I did this correctly
then
this
should just work on amdgpu. What this patch should
do
is
add
a debugfs file to amdgpu called
"amdgpu_dp_mst_topology_refs", and when you read
the
file
it
should print out the topology reference history of
every
MSTB and Port in memory, along with how many times
we've
hit
the codepath in each backtrace. An example:

Port DP-5 (0000000005c37748) topology ref history:
  1 gets (last at    58.468973):
     drm_dp_send_link_address+0x6a5/0xa00
[drm_kms_helper]
     drm_dp_check_and_send_link_address+0xad/0xd0
[drm_kms_helper]
     drm_dp_mst_link_probe_work+0x14e/0x1a0
[drm_kms_helper]
     process_one_work+0x1e3/0x390
     worker_thread+0x50/0x3a0
     kthread+0x124/0x150
     ret_from_fork+0x1f/0x30
  1 puts (last at    58.469357):
     drm_dp_mst_topology_put_port+0x6a/0x210
[drm_kms_helper]
     drm_dp_send_link_address+0x39e/0xa00
[drm_kms_helper]
     drm_dp_check_and_send_link_address+0xad/0xd0
[drm_kms_helper]
     drm_dp_mst_link_probe_work+0x14e/0x1a0
[drm_kms_helper]
     process_one_work+0x1e3/0x390
     worker_thread+0x50/0x3a0
     kthread+0x124/0x150
     ret_from_fork+0x1f/0x30

The idea here is that if stream resources aren't
being
released, my guess would be that we're not
dropping
topology
references for the port which means the connector
never
goes
away. So, if that's really the case then once we
unplug
the
offending connector we should be able to find a
pair
of
gets/puts for the offending leaked connector where
the
get/put count doesn't match up. Also, if the frame
count
on
the backtrace isn't long enough you can increase
the
value
of STACK_DEPTH in
drivers/gpu/drm/drm_dp_mst_topology.c
and
recompile to get more frames.

To enable this, first enable CONFIG_EXPERT for
your
kernel
which will unhide
CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS.
Then just enable
CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS,
recompile, and it should be good to go.

Let me know if this works for you, and hopefully
this
should
tell us exactly what the problem actually is here.

 .../amd/display/amdgpu_dm/amdgpu_dm_debugfs.c |
35
++++
 drivers/gpu/drm/drm_dp_mst_topology.c         |
173
++++++++++++++----
 drivers/gpu/drm/nouveau/nouveau_debugfs.c     |
35
++++
 include/drm/drm_dp_mst_helper.h               |
18
++
 4 files changed, 228 insertions(+), 33
deletions(-)

diff --git
a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_
debu
gf
s.
c
b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_
debu
gf
s.
c
index 1a68a674913c..1a14732c52b4 100644
---
a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_
debu
gf
s.
c
+++
b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_
debu
gf
+++ s.c
@@ -3063,6 +3063,37 @@ static int
mst_topo_show(struct
seq_file *m, void
*unused)
      return 0;
 }

+#ifdef CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS
+static int mst_topology_ref_dump_show(struct
seq_file
*m,
+void
+*unused) {
+     struct amdgpu_device *adev = (struct
amdgpu_device
+*)m->private;
+     struct drm_device *dev = adev_to_drm(adev);
+     struct drm_connector *connector;
+     struct drm_connector_list_iter conn_iter;
+     struct amdgpu_dm_connector *aconnector;
+
+     drm_connector_list_iter_begin(dev,
&conn_iter);
+     drm_for_each_connector_iter(connector,
&conn_iter)
{
+             if (connector->connector_type !=
DRM_MODE_CONNECTOR_DisplayPort)
+                     continue;
+
+             aconnector =
+to_amdgpu_dm_connector(connector);
+
+             /* Ensure we're only dumping the
topology
of
a
+root mst node
*/
+             if (!aconnector-
mst_mgr.max_payloads)
+                     continue;
+
+             seq_printf(m, "\nMST topology for
connector
+%d\n",
aconnector->connector_id);
+             drm_dp_mst_dump_topology_refs(m,
+&aconnector->mst_mgr);
+     }
+     drm_connector_list_iter_end(&conn_iter);
+
+     return 0;
+}
+
+DEFINE_SHOW_ATTRIBUTE(mst_topology_ref_dump);
+#endif
+
 /*
  * Sets trigger hpd for MST topologies.
  * All connected connectors will be rediscovered
and
re
started as needed if val of 1 is sent.
@@ -3299,6 +3330,10 @@ void
dtn_debugfs_init(struct
amdgpu_device
*adev)

      debugfs_create_file("amdgpu_mst_topology",
0444,
root,
                          adev, &mst_topo_fops);
+#ifdef CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS
+
debugfs_create_file("amdgpu_dp_mst_topology_refs",
+0444, root, adev,
+
&mst_topology_ref_dump_fops);
+#endif
      debugfs_create_file("amdgpu_dm_dtn_log",
0644,
root,
adev,
                          &dtn_log_fops);

diff --git a/drivers/gpu/drm/drm_dp_mst_topology.c
b/drivers/gpu/drm/drm_dp_mst_topology.c
index 1aa8702383d4..0159828c494d 100644
--- a/drivers/gpu/drm/drm_dp_mst_topology.c
+++ b/drivers/gpu/drm/drm_dp_mst_topology.c
@@ -1366,23 +1366,6 @@ static int
drm_dp_mst_wait_tx_reply(struct drm_dp_mst_branch
*mstb,
      return ret;
 }

-static struct drm_dp_mst_branch
*drm_dp_add_mst_branch_device(u8 lct, u8
*rad) -{
-     struct drm_dp_mst_branch *mstb;
-
-     mstb = kzalloc(sizeof(*mstb), GFP_KERNEL);
-     if (!mstb)
-             return NULL;
-
-     mstb->lct = lct;
-     if (lct > 1)
-             memcpy(mstb->rad, rad, lct / 2);
-     INIT_LIST_HEAD(&mstb->ports);
-     kref_init(&mstb->topology_kref);
-     kref_init(&mstb->malloc_kref);
-     return mstb;
-}
-
 static void drm_dp_free_mst_branch_device(struct
kref
*kref)  {
      struct drm_dp_mst_branch *mstb = @@ -1642,12
+1625,20
@@ topology_ref_type_to_str(enum
drm_dp_mst_topology_ref_type type)
              return "put";
 }

+static const char
*topology_ref_history_type_to_str(enum
+drm_dp_mst_topology_history_type type) {
+     if (type ==
DRM_DP_MST_TOPOLOGY_HISTORY_PORT)
+             return "Port";
+     else
+             return "MSTB"; }
+
 static void
-__dump_topology_ref_history(struct
drm_dp_mst_topology_ref_history *history,
-                         void *ptr, const char
*type_str)
+dump_topology_ref_history(struct
+drm_dp_mst_topology_ref_history *history, struct
+drm_printer p)
 {
-     struct drm_printer p =
drm_debug_printer(DBG_PREFIX);
+     char *connector_name = NULL;
      char *buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
+     void *ptr;
      int i;

      if (!buf)
@@ -1656,14 +1647,29 @@
__dump_topology_ref_history(struct
drm_dp_mst_topology_ref_history *history,
      if (!history->len)
              goto out;

+     /* Get a pointer to the actual MSTB/port so
we
can
the
+memory
address to the kernel log */
+     if (history->type ==
DRM_DP_MST_TOPOLOGY_HISTORY_PORT)
+             ptr = container_of(history, struct
+drm_dp_mst_port,
topology_ref_history);
+     else
+             ptr = container_of(history, struct
+drm_dp_mst_branch, topology_ref_history);
+
      /* First, sort the list so that it goes from
oldest
to
newest
       * reference entry
       */
      sort(history->entries, history->len,
sizeof(*history->entries),
           topology_ref_history_cmp, NULL);

-     drm_printf(&p, "%s (%p) topology count
reached
0,
dumping history:\n",
-                type_str, ptr);
+     if (history->type ==
DRM_DP_MST_TOPOLOGY_HISTORY_PORT)
+{
+             struct drm_dp_mst_port *port = ptr;
+
+             if (port->connector)
+                     connector_name =
+port->connector->name;
+     }
+     if (connector_name)
+             drm_printf(&p, "Port %s (%p)
topology
ref
+history:\n",
connector_name, ptr);
+     else
+             drm_printf(&p, "%s (%p) topology ref
+history:\n",
+
+topology_ref_history_type_to_str(history->type),
ptr);

      for (i = 0; i < history->len; i++) {
              const struct
drm_dp_mst_topology_ref_entry
*entry = @@
-
1682,24 +1688,92 @@
__dump_topology_ref_history(struct
drm_dp_mst_topology_ref_history *history,
                         ts_nsec, rem_nsec / 1000,
buf);
      }

-     /* Now free the history, since this is the
only
time
we expose it */
-     kfree(history->entries);
 out:
      kfree(buf);
 }

+/**
+ * drm_dp_mst_dump_topology_refs - helper
function
for
+dumping the topology ref history
+ * @m: File to print to
+ * @mgr: &struct drm_dp_mst_topology_mgr to use
+ *
+ * Prints the topology ref history of all ports
and
MSTBs
+on @mgr that are still in memory,
+ * regardless of whether they're actually still
reachable
+through the topology or not. Only enabled
+ * when %CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS is
enabled.
+Can be implemented by drivers to assist
+ * with debugging leaks in the DP MST helpers.
+ */
+void drm_dp_mst_dump_topology_refs(struct
seq_file
*m,
+struct drm_dp_mst_topology_mgr *mgr) {
+     struct drm_dp_mst_topology_ref_history
*history;
+     struct drm_printer p =
drm_seq_file_printer(m);
+
+     mutex_lock(&mgr->topology_ref_history_lock);
+     list_for_each_entry(history,
+&mgr->topology_ref_history_list,
+node)
+             dump_topology_ref_history(history,
p);
+     mutex_unlock(&mgr-
topology_ref_history_lock);
+}
+EXPORT_SYMBOL(drm_dp_mst_dump_topology_refs);
+
+static void
+__init_topology_ref_history(struct
+drm_dp_mst_topology_ref_history
*history,
+                         struct
drm_dp_mst_topology_mgr
+*mgr,
+                         enum
+drm_dp_mst_topology_history_type
+type) {
+     history->type = type;
+     INIT_LIST_HEAD(&history->node);
+
+     mutex_lock(&mgr->topology_ref_history_lock);
+     list_add(&history->node,
+&mgr->topology_ref_history_list);
+     mutex_unlock(&mgr-
topology_ref_history_lock);
+}
+
+static void
+__destroy_topology_ref_history(struct
+drm_dp_mst_topology_ref_history
*history,
+                            struct
drm_dp_mst_topology_mgr
+*mgr) {
+     mutex_lock(&mgr->topology_ref_history_lock);
+     list_del(&mgr->topology_ref_history_list);
+     mutex_unlock(&mgr-
topology_ref_history_lock);
+
+     kfree(history->entries); }
+
+static __always_inline void
+init_port_topology_history(struct
drm_dp_mst_topology_mgr
+*mgr, struct drm_dp_mst_port *port) {
+
+__init_topology_ref_history(&port-
topology_ref_history,
+mgr,
+
+DRM_DP_MST_TOPOLOGY_HISTORY_PORT);
+}
+
+static __always_inline void
+init_mstb_topology_history(struct
drm_dp_mst_topology_mgr
+*mgr, struct drm_dp_mst_branch *mstb) {
+
+__init_topology_ref_history(&mstb-
topology_ref_history,
+mgr,
+
+DRM_DP_MST_TOPOLOGY_HISTORY_MSTB);
+}
+
+static __always_inline void
+destroy_port_topology_history(struct
drm_dp_mst_port
*port)
+{
+
+__destroy_topology_ref_history(&port-
topology_ref_history,
+port->mgr); }
+
+static __always_inline void
+destroy_mstb_topology_history(struct
drm_dp_mst_branch
+*mstb) {
+
+__destroy_topology_ref_history(&mstb-
topology_ref_history,
+mstb->mgr); }
+
 static __always_inline void
 dump_mstb_topology_history(struct
drm_dp_mst_branch
*mstb)
{
-
__dump_topology_ref_history(&mstb-
topology_ref_history,
mstb,
-                                 "MSTB");
+     dump_topology_ref_history(&mstb-
topology_ref_history,
+drm_debug_printer(DBG_PREFIX));
 }

 static __always_inline void
 dump_port_topology_history(struct drm_dp_mst_port
*port)
{
-
__dump_topology_ref_history(&port-
topology_ref_history,
port,
-                                 "Port");
+     dump_topology_ref_history(&port-
topology_ref_history,
+drm_debug_printer(DBG_PREFIX));
 }

 static __always_inline void @@ -1729,6 +1803,14
@@
topology_ref_history_unlock(struct
drm_dp_mst_topology_mgr *mgr)  }  #else  static
inline
void
+init_port_topology_history(struct
drm_dp_mst_topology_mgr
+*mgr, struct drm_dp_mst_port *port); static
inline
void
Should also add the bracket, otherwise will get
warnings.
=> static inline void
init_port_topology_history(struct
drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port
*port)
{};

+init_mstb_topology_history(struct
drm_dp_mst_topology_mgr
+*mgr, struct drm_dp_mst_branch *mstb); static
inline
void
Same as above
=> static inline void
init_mstb_topology_history(struct
drm_dp_mst_topology_mgr *mgr, struct
drm_dp_mst_branch
*mstb)
{};

+destroy_port_topology_history(struct
+drm_dp_mst_topology_mgr *mgr, struct
drm_dp_mst_port
+*port); static inline void
destroy_port_topology_history() takes one parameter
only
which
is port.
=> destroy_port_topology_history(struct
drm_dp_mst_port
*port)
{};

+destroy_mstb_topology_history(struct
+drm_dp_mst_topology_mgr *mgr, struct
drm_dp_mst_branch
+*mstb); static inline void
destroy_mstb_topology_history() takes one parameter
only
which
is mstb => destroy_mstb_topology_history(struct
drm_dp_mst_branch
*mstb) {};

 topology_ref_history_lock(struct
drm_dp_mst_topology_mgr
*mgr) {} static inline void
topology_ref_history_unlock(struct
drm_dp_mst_topology_mgr *mgr) {} @@ -1740,6
+1822,25
@@
dump_port_topology_history(struct drm_dp_mst_port
*port)
{}
#define save_port_topology_ref(port, type)  #endif

+static struct drm_dp_mst_branch *
+drm_dp_add_mst_branch_device(struct
drm_dp_mst_topology_mgr
+*mgr,
+u8 lct, u8 *rad) {
+     struct drm_dp_mst_branch *mstb;
+
+     mstb = kzalloc(sizeof(*mstb), GFP_KERNEL);
+     if (!mstb)
+             return NULL;
+
+     mstb->lct = lct;
+     if (lct > 1)
+             memcpy(mstb->rad, rad, lct / 2);
+     INIT_LIST_HEAD(&mstb->ports);
+     kref_init(&mstb->topology_kref);
+     kref_init(&mstb->malloc_kref);
+     init_mstb_topology_history(mgr, mstb);
+     return mstb;
+}
+
 static void
drm_dp_destroy_mst_branch_device(struct
kref
*kref) {
      struct drm_dp_mst_branch *mstb = @@ -1747,6
+1848,7
@@
static void
drm_dp_destroy_mst_branch_device(struct
kref *kref)
      struct drm_dp_mst_topology_mgr *mgr = mstb-
mgr;

      dump_mstb_topology_history(mstb);
+     destroy_mstb_topology_history(mstb);

      INIT_LIST_HEAD(&mstb->destroy_next);

@@ -1856,6 +1958,7 @@ static void
drm_dp_destroy_port(struct
kref
*kref)
      struct drm_dp_mst_topology_mgr *mgr = port-
mgr;

      dump_port_topology_history(port);
+     destroy_port_topology_history(port);

      /* There's nothing that needs locking to
destroy
an
input port yet */
      if (port->input) {
@@ -2135,7 +2238,7 @@ drm_dp_port_set_pdt(struct
drm_dp_mst_port *port, u8 new_pdt,
                      ret =
drm_dp_mst_register_i2c_bus(port);
              } else {
                      lct =
drm_dp_calculate_rad(port,
rad);
-                     mstb =
drm_dp_add_mst_branch_device(lct, rad);
+                     mstb =
+drm_dp_add_mst_branch_device(mgr, lct, rad);
                      if (!mstb) {
                              ret = -ENOMEM;
                              drm_err(mgr->dev,
"Failed
to
create MSTB for port %p", port); @@ -2366,6
+2469,8 @@
drm_dp_mst_add_port(struct drm_device *dev,
       */
      drm_dp_mst_get_mstb_malloc(mstb);

+     init_port_topology_history(mgr, port);
+
      return port;
 }

@@ -3745,7 +3850,7 @@ int
drm_dp_mst_topology_mgr_set_mst(struct
drm_dp_mst_topology_mgr *mgr, bool ms
              }

              /* add initial branch device at LCT
1 */
-             mstb =
drm_dp_add_mst_branch_device(1,
NULL);
+             mstb =
drm_dp_add_mst_branch_device(mgr,
1,
+NULL);
              if (mstb == NULL) {
                      ret = -ENOMEM;
                      goto out_unlock; @@ -5512,14
+5617,16
@@ int drm_dp_mst_topology_mgr_init(struct
drm_dp_mst_topology_mgr *mgr,
      mutex_init(&mgr->delayed_destroy_lock);
      mutex_init(&mgr->up_req_lock);
      mutex_init(&mgr->probe_lock); -#if
IS_ENABLED(CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS)
-     mutex_init(&mgr->topology_ref_history_lock);
-#endif
      INIT_LIST_HEAD(&mgr->tx_msg_downq);
      INIT_LIST_HEAD(&mgr->destroy_port_list);
      INIT_LIST_HEAD(&mgr-
destroy_branch_device_list);
      INIT_LIST_HEAD(&mgr->up_req_list);

+#if
IS_ENABLED(CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS)
+     mutex_init(&mgr->topology_ref_history_lock);
+     INIT_LIST_HEAD(&mgr-
topology_ref_history_list);
+#endif
+
      /*
       * delayed_destroy_work will be queued on a
dedicated
WQ, so that any
       * requeuing will be also flushed when
deiniting
the
topology manager.
diff --git
a/drivers/gpu/drm/nouveau/nouveau_debugfs.c
b/drivers/gpu/drm/nouveau/nouveau_debugfs.c
index 1cbe01048b93..53ec7c1daada 100644
--- a/drivers/gpu/drm/nouveau/nouveau_debugfs.c
+++ b/drivers/gpu/drm/nouveau/nouveau_debugfs.c
@@ -29,9 +29,13 @@
  */

 #include <linux/debugfs.h>
+#include <drm/drm_dp_mst_helper.h> #include
+<drm/drm_encoder.h>
 #include <nvif/class.h>
 #include <nvif/if0001.h>
+#include <nouveau_encoder.h>
 #include "nouveau_debugfs.h"
+#include "nouveau_display.h"
 #include "nouveau_drv.h"

 static int
@@ -68,6 +72,34 @@
nouveau_debugfs_strap_peek(struct
seq_file *m, void
*data)
      return 0;
 }

+#ifdef CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS
+static int
nouveau_debugfs_mst_topology_history(struct
+seq_file *m, void *data) {
+     struct drm_info_node *node = m->private;
+     struct drm_device *dev = node->minor->dev;
+     struct drm_encoder *encoder;
+     struct nouveau_display *disp =
nouveau_display(dev);
+
+     if (disp->disp.object.oclass < GF110_DISP)
+             return -EINVAL;
+
+     drm_for_each_encoder(encoder, dev) {
+             struct nv50_mstm *mstm;
+
+             /* We need the real encoder for each
MST
mgr
+*/
+             if (encoder->encoder_type !=
+DRM_MODE_ENCODER_TMDS)
+                     continue;
+             mstm = nouveau_encoder(encoder)-
dp.mstm;
+             if (!mstm)
+                     continue;
+
+             seq_printf(m, "MSTM on %s:\n",
encoder-
name);
+             drm_dp_mst_dump_topology_refs(m,
&mstm-
mgr);
+     }
+     return 0;
+}
+#endif
+
 static int
 nouveau_debugfs_pstate_get(struct seq_file *m,
void
*data)
{ @@
-213,6
+245,9 @@ static const struct file_operations
nouveau_pstate_fops = {  static struct
drm_info_list
nouveau_debugfs_list[] = {
      { "vbios.rom",  nouveau_debugfs_vbios_image,
0,
NULL
},
      { "strap_peek", nouveau_debugfs_strap_peek,
0,
NULL
},
+#ifdef CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS
+     { "dp_mst_topology_refs",
+nouveau_debugfs_mst_topology_history, 0, NULL },
#endif
 };
 #define NOUVEAU_DEBUGFS_ENTRIES
ARRAY_SIZE(nouveau_debugfs_list)

diff --git a/include/drm/drm_dp_mst_helper.h
b/include/drm/drm_dp_mst_helper.h index
78044ac5b59b..e3a73d8d97c0
100644
--- a/include/drm/drm_dp_mst_helper.h
+++ b/include/drm/drm_dp_mst_helper.h
@@ -35,6 +35,11 @@ enum
drm_dp_mst_topology_ref_type {
      DRM_DP_MST_TOPOLOGY_REF_PUT,
 };

+enum drm_dp_mst_topology_history_type {
+     DRM_DP_MST_TOPOLOGY_HISTORY_PORT,
+     DRM_DP_MST_TOPOLOGY_HISTORY_MSTB, };
+
 struct drm_dp_mst_topology_ref_history {
      struct drm_dp_mst_topology_ref_entry {
              enum drm_dp_mst_topology_ref_type
type;
@@
-43,6
+48,9 @@ struct drm_dp_mst_topology_ref_history {
              depot_stack_handle_t backtrace;
      } *entries;
      int len;
+
+     enum drm_dp_mst_topology_history_type type;
+     struct list_head node;
 };
 #endif /*
IS_ENABLED(CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS)
*/

@@ -769,6 +777,12 @@ struct
drm_dp_mst_topology_mgr {
       * &drm_dp_mst_branch.topology_ref_history.
       */
      struct mutex topology_ref_history_lock;
+
+     /**
+      * @topology_ref_history_list: contains a
list
of
+topology ref
histories for any MSTBs or
+      * ports that are still in memory
+      */
+     struct list_head topology_ref_history_list;
 #endif
 };

@@ -873,6 +887,10 @@ void
drm_dp_mst_put_port_malloc(struct
drm_dp_mst_port *port);

 struct drm_dp_aux
*drm_dp_mst_dsc_aux_for_port(struct
drm_dp_mst_port *port);

+#ifdef CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS
+void drm_dp_mst_dump_topology_refs(struct
seq_file
*m,
+struct drm_dp_mst_topology_mgr *mgr); #endif
+
 extern const struct drm_private_state_funcs
drm_dp_mst_topology_state_funcs;

 /**
--
2.31.1

--
Cheers,
 Lyude Paul (she/her)
 Software Engineer at Red Hat

Quoted 2 lines

--
Cheers, Lyude Paul (she/her) Software Engineer at Red Hat

--
Regards,
Wayne Lin

Quoted 7 lines
Quoted 6 lines
Quoted 5 lines
Quoted 4 lines

--
Cheers,
 Lyude Paul (she/her)
 Software Engineer at Red Hat

--
Cheers,
 Lyude Paul (she/her)
 Software Engineer at Red Hat

Signature · 3 lines
Cheers,
 Lyude Paul (she/her)
 Software Engineer at Red Hat
#17 lore raw

[Public]

Quoted 77 lines

-----Original Message-----
From: Lyude Paul <[email protected]>
Sent: Wednesday, March 16, 2022 8:48 AM
To: Lin, Wayne <[email protected]>
Cc: [email protected]; [email protected]
Subject: Re: [PATCH] WIP: drm/dp_mst: Add support for dumping topology ref histories from debugfs

(Adding this back to the dri-devel mailing list since I didn't notice it got dropped from there)

Hm, some comments on this issue down below. Sorry for the delayed response, I was going to try this right after I finished the MST legacy removal but that's ending up taking longer than I hoped.

Quoted 19 lines · Wayne

[AMD Official Use Only]

Oops...
I didn't notice that I replied to wrong mail previously : P Sorry for the confusion and reply to the correct thread.

Good day!
I probably know why you can't reproduce the issue there. Please refer to the attached
patch file which makes me easier to explain this.

In dm_dp_mst_get_modes(), we will create a new dc_sink by calling dc_link_add_remote_sink() and add that dc_sink into the array dc_link-

Quoted 1 line

remote_sinks[].

However, if we find that aconnector->dc_sink is not null, then we won't create a new
dc_sink for that aconnector and won't add the sink to the array dc_link-

Quoted 1 line

remote_sinks[].

Quoted 16 lines

When the issue that I mentioned before occurs, we won't be able to release the dc_sink
for the aconnector when we unplug the sst monitor. Hence, we can't create new dc_sink
for the latter on connected new stream sink of that port. Which leads to two issues
1. Unplug monitor and plug in another monitor "to the same port" => Since we use the same dc_sink to reflect the capability of the new connected stream
sink, we might send out inappropriate stream to the new connected sink.
2. Because we can't release dc_sink in the array dc_link->remote_sinks[], when we
plug/unplug sst monitor to more than 4 different mst port, we will break the criteria
"dc_link->sink_count >= MAX_SINKS_PER_LINK" in link_add_remote_sink_helper().

Ok, a lot of this seems to be happening in amdgpu which certainly explains why I had so much trouble following along with this originally (also, having learned a bit more about how DC works definitely has helped a bit). I already see a bunch of issues though with how this code is structured that would definitely break things, though note I haven't sat down and tried this on a real machine yet - will try tomorrow.

So - it seems like dc_sink == a bunch of hotplugging state for a dm connector, which actually tells me for one that this is definitely the wrong spot for this code. get_modes() really should just handle filling out DRM modes and pretty much nothing else, because callers from DRM aren't going to expect side-effects like this when get_modes() is called - which could potentially lead to all sorts of weirdness down the line if the DRM call paths that use this ever change. i915 and nouveau have good examples of what these functions should typically look like, and amdgpu actually seems to mostly follow this advice for it's other get_modes() callback.

Note there's also another problem here: the assumption "no EDID == disconnected" isn't correct. It's totally possible to run into EDID-less displays if the display is some ancient pre-historic relic, or if the ROM (or EEPROM? can't remember what type of chip computer displays tend to use...) chip in the monitor containing the EDID has died. Note that the second situation is suprisingly more common then one might think! I ran into a 140Hz 4k ASUS display where this happened, and I know another friend of mine who had the ROM in their display die randomly as well.

Quoted 6 lines

So with this being said: I, think the solution here is actually just to add/remove dc_sink from dm_dp_mst_detect() based on the CSN, and drop the calls from get_modes()? detect() should always be called before the connector has been removed, and we could use the return status from drm_dp_mst_detect_port() in order to tell whether or not we should add/remove the dc_sink.

Thank you Lyude for guiding me through! After your elaboration, I think I roughly know the idea from drm perspective now. Let me get through this and will get back to you.

Quoted 7 lines

If you don't get to it first tomorrow, I'll see if I can reproduce this locally and try writing up a patch.

Also a final note: I don't think it's a good idea in general for there to be a fixed size array of dc_sinks like this. Those probably should just be stored somehow on a per-connector basis. I say this because in addition to the fact

Understand and agree with that. I plan to remove the constraint as well. Thanks!

Quoted 24 lines

MST connectors aren't destroyed immediately (which could lead to a situation where we have more then 6 connected MST ports briefly before their destructors run, e.g. during suspend/resume reprobing).

Also, one question below:

Quoted 19 lines

Anyway, these are my steps to reproduce the issue.
I use a startech mst 1to4 mst hub, a sst monitor and a mst monitor.

1. boot up with sst monitor connected to the hub and plug the hub to the system.
2. unplug/plug sst monitor to all left three different DP output ports. Which is used to
Pollute all four aconnectors to have them keep the stale dc_sinks.
3. plug in the mst monitor to arbitrary ports. Since the new connected mst monitor will
have us add new mst port to the topology and attach a new aconnector for that mst port,
we'll try to create a new dc_sink to that new aconnector and break the sink_count
limitation. As you can see in the log the error message : "*ERROR* Unable to add a
remote sink"

Quoted 8 lines
Quoted 5 lines

(Note that different hub might influence the result. Some hubs will pull down the HPD
when there is no monitor connected to its downstream ports which will run into different
code flow)

by "pull down" I assume you mean doing a long HPD instead of short (or vice versa?).

Yes, long HPD!

Thanks for your help Lyude!

Quoted 1,933 lines
Quoted 5 lines

I also attach the dmesg log under different debug level (0x104 & 0x116) for your reference.

Quoted 124 lines

Please let me know if you can reproduce then : )

Sorry about that Lyude and hope this time I send to the right thread.. Really appreciate for your help!!!

Regards,
Wayne

Quoted 117 lines

-----Original Message-----
From: Lyude Paul <[email protected]>
Sent: Wednesday, March 2, 2022 9:10 AM
To: Lin, Wayne <[email protected]>
Subject: Re: [PATCH] WIP: drm/dp_mst: Add support for dumping topology ref histories from debugfs

Ran into some issues with my system yesterday (hooray being the first to find
a rather annoying zsh bug that broke my scripts!), but I've finally got things
setup. I'm not able to reproduce this issue though (I've tried hotplugging the
way you mentioned here, but nothing seems to leak), which makes me think there's something special in your setup we might not have noticed.

Do you think you could get some drm logs with drm.debug=0x116 log_buf_len=50M
from when this happens so I can get a better idea of the drm events leading up
to this? Also, if you've been testing this in a GUI could you confirm whether
this happens when you're just in a plain old VT?

Quoted 91 lines · Lyude Paul

Alright - starting to look into this now and setting up a system to try reproducing this, will let you know as soon as I've got results. Sorry again
about all of the delays

Quoted 85 lines · Lyude Paul

I didn't get a chance yet, but I've gotten through most of my backlog now.
Sorry about the delay - I've been catching up since I had to take two weeks
off work very unexpectedly. I'll put aside time to look at this first thing
monday.

Quoted 76 lines · Wayne

[AMD Official Use Only]

Hi Lyude,

Good day!
Sorry to bother you, but would like to know if you can reproduce the issue?

Thanks!

Regards,
Wayne

Quoted 63 lines

-----Original Message-----
From: Lyude Paul <[email protected]>
Sent: Thursday, February 3, 2022 4:12 AM
To: Lin, Wayne <[email protected]>
Cc: [email protected]
Subject: Re: [PATCH] WIP: drm/dp_mst: Add support for dumping topology
ref
histories from debugfs

JFYI I should be able to respond to this soon, I think I'm following
your
issue a bit more (haven't gotten to go through the patches too so I'm
probably
going to see if I can reproduce it locally as well. Sorry for the delay
-
dealing with some X.org stuff and work issues at the moment

Quoted 41 lines · Wayne

[Public]

Hey Lyude,

Sorry, just got chance to get back to this and BIG THANKS to elaborating
in
such details!
Following are some of my comments : )

Quoted 10 lines

-----Original Message-----
From: Lyude Paul <[email protected]>
Sent: Wednesday, January 19, 2022 9:59 AM
To: Lin, Wayne <[email protected]>
Cc: [email protected]
Subject: Re: [PATCH] WIP: drm/dp_mst: Add support for dumping topology
ref
histories from debugfs

Quoted 3 lines

Hey - so, the original issue here was that payloads were not always
deleted when we expected them to be - correct?

Actually, what I was trying to do is not relevant to payload maintenance.
What I wanted to do is having a way to notify
amdgpu to release its enumerated resource "dc_sink" when unplug event
occurs. Currently, under specific scenarios,
we can't release remote "dc_sink" and ends up can't allocate resource
for
new connected monitors. I'll explain the scenario underneath.

So, get back to the main issue that I was trying to solve when I pushed
the
patchset:

https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Fpatchwork.kernel.org%2Fproject%2Fdri&amp;data=04%7C01%7Cw
ayne.lin%40amd.com%7Cd77e829cfd3a466e25e508da06e6a9dd%7C3dd8961fe4884e608e11a82d994e183d%7C0%7C0%7C637829885024 474689%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&amp;sda ta=MvKilTT1CkBqZVK1N24JyNPjiPYORV%2FsCwvEzwx%2FIAk%3D&amp;reserved=0

Quoted 1,794 lines
Quoted 1,794 lines

ayne.lin%40amd.com%7Cbbc27043edc04bf8067c08d9fbe9620c%7C3dd8961fe4884e608e 11a82d994e183d%7C0%7C0%7C6378178020798 98960%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6 Ik1haWwiLCJXVCI6Mn0%3D%7C3000&amp;sdat a=RVUc8KgTO3mhFL9qemshHJqiyXJs%2Fvytx21AMyUaW%2Bs%3D&amp;reserved=0

Quoted 1,788 lines
Quoted 1,787 lines
Quoted 1,786 lines
Quoted 1,785 lines
Quoted 1 line

-

devel%2Fcover%2F20210720160342.11415-1- Wayne.Lin%40amd.com%2F&amp;data=04%7C01%7Cwayne.lin%40amd.com%7C2e 19d9
32
1b
5f4e948c1208d9e6884f24%7C3dd8961fe488 4e608e11a82d994e183d%7C0%7C0%7C637794295422403301%7CUnknown%7CTWFp bGZs
b3
d8
eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiL CJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&amp;sdata=rvtwQioO3pT1BoHb7S5 32ms
Ra
61
gP1vIyE8AB7M9I4E%3D&amp;reserved=0

Quoted 1,757 lines

I was trying to fix some problems that I observed after these 2 patches
* 09b974e8983 drm/amd/amdgpu_dm/mst: Remove -

Quoted 1 line

destroy_connector()

callback
* 72dc0f51591 drm/dp_mst: Remove
drm_dp_mst_topology_cbs.destroy_connector

With above patches, it changes our flow to remove dc_sink (by calling
dc_link_remote_sink()) when connector is about to be destroyed. And let's see below use cases:
a). Disconnection happens between mst branch devices e.g.
src - 1st_mstb - 2nd_mstb - sst_monitor => src - 1st_mstb (disconnect)
2nd_mstb - sst_monitor

In above case, after receiving CSN, we will put topology reference
count
of
2nd mstb and its port which connected to the sst monitor. As you suggested before, we will destroy 2nd mst branch device and
its
all
ports due to there is no more mst path can route to them. As the result of that, we at end call drm_dp_delayed_destroy_port()
and
unregister/put the drm connector and eventually call
dc_link_remote_sink() to release the dc_sinck we enumerate for
the
specific
stream sink of sst_monitor.

However, in below case:
b). Disconnection happens between mst branch devices and SST monitor
e.g.
src - 1st_mstb - sst_monitor => src - 1st_mstb (disconnect) sst_monitor

In this case, which is the case having problem, it definitely won't
decrease
the topology references count of the port of 1st_mstb which was connected to the sst monitor to zero since the port is still existing in
the topology. Same as the malloc reference since the port can't get
destroyed. Hence, the port still exists and we won't call drm_dp_delayed_destroy_port() to unregister and put the drm connector.
As
the
result of that, we can't release corresponding remote dc_sink under
this
case. And after few times hotplugs, we can't create any new dc_sink since the number of stream sink is exceeding our limitation.

Hence, what I'm thinking here is to register a callback function for
notifying us that the remote sink is detached. This just aligns what
we
do
for handling long HPD event (unplug monitor from system) of legacy
DP
(none
mst). For which case, once we detect long HPD event indicating the monitor is detached, we will immediately try to release
the
dc_link->local_sink and fire hotplug event to upper layer. Same as here, once receives a CSN message notifying a drm connector
is
changed from connected to disconnected, trigger the callback function and we can try to release the dc_sink resource.

Quoted 57 lines

If I'm remembering that correctly, then (and I'm not 100% sure on
this
yet) I might already have noticed something very possibly wonky in
how we handle payload allocations currently, that I found while
working on
the non-atomic removal branch that I linked to you in my previous response. Basically, in the
step1() function it looks like that we follow this general flow
with
updating
payloads:

* Loop through proposed payloads and compare to previously
programmed
payloads
- If a payload has a different allocation then it
previously
did,
update the
payload
- If the payload is new, create it
- If a payload no longer has an allocation, remove the
payload

At first glance this seems totally correct - but I'm not actually
entirely
convinced it is? Particularly because it seems like the order in
which
we do creation/deletion of payloads is totally arbitrary. To explain
what
I mean by that, consider a state transition like this:

vcpi_slots=15 vcpi_slots=35 vcpi_slots=14

Quoted 1 line

1 | 2 |xxxxxxxx|

Let's say we want to increase payload #1 from 15 to 50, and disable
payload #2 in the same atomic commit on DRM's side. If the order
we
update payloads is entirely arbitrary, we could end up doing this:

* Increase VCPI slots payload #1 from 15->50 (total VCPI
slots=99,
overflow!)
* Decrease VCPI slots payload #2 from 35->0 (total VCPI
slots=50)

Quoted 11 lines

Notice on the first step, we've technically overflowed the available
number of VCPI slots in the payload table. This is still before
step
2
though, and I could be totally wrong here - perhaps this is entirely
OK in
the real world, and we're allowed to overflow VC slots as long as

I think it's forbidden to allocate time slots beyond 64 when we're
updating
payload ID table of the branch in the real world. Branch probably
will NACK the DPCD write.

Quoted 11 lines

we repair the issue before step 2. But so far I can't seem to find
anything in the DP 2.0 spec that explicitly states this would be
OK
-
which
makes me think we might fail some payload allocations if we don't
always
ensure we avoid overflows in between VC payload table changes

Agree.
For amdgpu, I think we always do deallocation (call hws-

Quoted 1 line

funcs.reset_hw_ctx_wrap()) before allocation

(apply_single_controller_ctx_to_hw()).

Quoted 23 lines

Note that avoiding overflows would be as simple as just making sure
we
send all VC payload table changes that free VC slots before sending
any that take new slots.

Again - I haven't actually confirmed this yet and am hoping to test
stuff
like this very soon as I'm pretty close finishing up the initial
attempt
at removing the non-atomic MST code in the DRM helpers now. If my
theory
ends up being correct here, I can fix this in my rewrite of the
MST payload management code. But I figured it might be worth mentioning in
the mean time in case you think it might be relevant to the problem here :).

Quoted 93 lines
Quoted 92 lines · Lyude Paul

(CC'ing this to dri-devel, since this is basically patch review)

Alright - so first off sorry about the broken debugging patch! I
thought I had tested that but I guess I hadn't tested it well
enough,
I'll get it fixed asap, but feel free to try getting to it before
I
do.

As for this patch... (comments below)

Quoted 66 lines · Wayne

[AMD Official Use Only]

Hi Lyude,

No rush and thanks for your time! : ) Just want to help clarify
what
I mean that "registering a callback function"
for CSN of disconnecting
stream sink device (not mst branch case). Attach below the tentative
patch that I planned to do. Thanks so much!

Regards,
Wayne

---
 .../display/amdgpu_dm/amdgpu_dm_mst_types.c   | 53
+++++++++++++++++++
 drivers/gpu/drm/drm_dp_mst_topology.c         | 16 +++++-
 include/drm/drm_dp_mst_helper.h               |  1 +
 3 files changed, 68 insertions(+), 2 deletions(-)

diff --git a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_mst_type s.c b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_mst_type s.c
index cc34a35d0bcb..d7343c64908c 100644
---
a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_mst_type s.c
+++
b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_mst_type s.c

(quoted diff) +23 -0
@@ -472,8 +472,61 @@ dm_dp_add_mst_connector(struct
 drm_dp_mst_topology_mgr *mgr,
        return connector;
 }
 
+void dm_dp_notify_csn_disconnection(struct drm_connector
+*connector) {
+       struct amdgpu_dm_connector *aconnector =
+               to_amdgpu_dm_connector(connector);
+       struct dc_sink *dc_sink = aconnector->dc_sink;
+       struct dc_link *dc_link = aconnector->dc_link;
+       struct amdgpu_device *adev = drm_to_adev(ddev);
+
+       ASSERT(dc_link);
+
+       if (dc_sink) {
+               mutex_lock(&adev->dm.dc_lock);
+
+               /*clear the remote sink of the link*/
+               dc_link_remove_remote_sink(dc_link,
 dc_sink);
+               dc_sink_release(dc_sink);
+               aconnector->dc_sink = NULL;
+
+               mutex_unlock(&adev->dm.dc_lock);
+       }
+}
+
 static const struct drm_dp_mst_topology_cbs dm_mst_cbs =
 {
        .add_connector = dm_dp_add_mst_connector,
+       .notify_csn_disconnection =
Quoted 2 lines

dm_dp_notify_csn_disconnection,
};

I still don't really think this is a good idea. This seems like
it's
just adding another hotplugging path to the code in order to avoid
sending hotplugs for non-endpoint devices. In addition to the

So, what I wanted to do is to add a callback function for endpoint
devices
case as described above. For disconnection of non-endpoint devices
is
already taken care by current code.

Quoted 45 lines
Quoted 45 lines

drm_connector issues I mentioned before, we also really need to
stop
doing any kind of payload maintence in hotplugging paths. The
reality
is any kind of payload maintanence we do outside of normal modesetting
paths is a relic from legacy modesetting that I'm dropping ASAP
(more
on this below), and we can't keep adding to it because it dramatically
complicates maintanence as well.

Sorry for repeating this point so often but - the biggest issue
too
is
I'm still not sure what it is we're even avoiding here. We know
resources aren't released consistently, and that we're able to
reproduce the behavior with repeated hotplugs. We also know that
if
we
skip sending certain hotplug events, that fixes the issue. And
we
know
we can workaround it by adding a special case for forcing a payload
release in DC. But none of those actually tell us exactly what
piece
of code is leaking and why, which means that any workarounds we're
putting in to avoid this mysterious guilty section of code we
don't
entirely understand either - which means we're just adding more
code

Quoted 8 lines
Quoted 8 lines

in that no one actually fully understands. This just ends up making
maintence difficult because every change in code nearby workarounds
like this has to strugle to try to figure out said workarounds
in
order to avoid breaking things.

Thank you Lyude for the reminder and I totally agree with you by avoiding
adding workaround since it will get the code maintenance harder. And
I
think the root cause of the issue that I observed before is quite
concrete.
There is no code path for us to release dc_sink under the unplug scenario
that I described above.

Quoted 115 lines
Quoted 115 lines

I'm actually currently running into these "later" issues right
now,
as
recently I've (-actively-, finally!!!) been working on trying to
remove as much non-atomic MST as I can because. As it turns out
-
a
huge amount of the payload maintanence code just isn't needed at
all
when we stop caring about non-atomic drivers and stick everything
in
atomic state structs. Step 1 for updating updating the payload
table,
e.g. drm_dp_update_payload_part1(), is a great example of how
messy
things have become. Here's a small sample of some of the stuff
I've
seen from just that one function so far that either just don't
make
sense here or is totally redundant. I should note that a lot of
these
things also come from patches I reviwed, but didn't really look
at
as
closely as I should have because I was swamped at work, some are
historical artifacts, and others are less-than-ideal patches I
got
wrote myself when I was first started working on MST and didn't
know
the code as well as I do
now:

* We try to avoid some sort of userspace issue by using drm_dp_mst_port_downstream_of_branch() to avoid releasing
payloads
for a
branch if we can't prove it's downstream of the top of
the
topology. This
seems to workaround either a userspace bug. This is a
redundant,
since
that's what topology refs are already supposed to be
doing to
the
extent is
reasonably possibly. It's also unfortunately racy
solution
because
we have
to be able to handle the situation where a connector is
removed
from under
us. That can happen at any time, including _immediately_
after
we
call
drm_dp_mst_port_downstream_of_branch() - rendering the
call
not
really
useful.
* If we fail to validate the sink in
drm_dp_update_payload_part,
we
don't
update the payload table. I think at best this solution
is
racy
and
not
useful, at worst it leaves us with a payload table that
doesn't
match what
we attempted to set in the atomic state - which at worst
brings
us
into
undefined territory where we're just plain out of sync
with
the
reality in
hw.
* Actually fun fact - mgr->payloads and mgr->proposed_vcpis
both
can
and
definitely should be removed entirely. All of the info
for
mgr->payloads
could just be in the atomic state, because that + the
magic
of
atomic state
duplication means we'll also have an accurate view of the
previous
state's
payload allocations: which renders mgr->proposed_vcpis
redundant.

Quoted 3 lines
Quoted 3 lines

Apologies for the long explanation again, but I hope that explains
my

Really really thanks for your kindly help on this : ) I'll try my best to get to your WIP branch soon. Thank you Lyude!

Quoted 1,264 lines
Quoted 1,263 lines

point here a bit. I'm going to be trying to get to moving amdgpu's
DSC
code out of amdgpu and into DRM helpers as well soon, so I'm really
determined to clean stuff up beforehand as every time I've done
so
it's become substantially easier to make changes to this code.
Things
used to be even worse before I started cleaning things up 2 or 3
years
ago, where simple changes would end up getting me stuck spending
hours
trying to dig through lockdep or memory manegement issues. As
well,
I
would be entirely unsurprised if bugs like this very payload leak
we're working on just disappear once we've gotten rid of all the
extraneous workarounds and state tracking here - especially with
how
many special cases we have for maintaining the payload table right
now. That's certainly ended up being the case in the past with a
number of other difficult to track down issues I've dealt with
in
MST.

Anyhow. I've been way more productive this year then last because
I
had over a month off work and am finally not super burnt out from
my
job, and so far I've been making progress on the payload state
cleanup
far faster then I was last year :). I think if you can't figure
out
where the leak is coming from even once I get the debugging patches
I
mentioned fixed up, it might be a good idea for us to try again
after
I've got some of this code cleaned up. I've got a currently WIP
branch
here:

https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgitl
ab.freedesktop.org%2Flyudess%2Flinux%2F- %2Fcommit%2F624dd68fa804e64b5b 2060e4735d7317090692b5&amp;data=04%7C01%7Cwayne.lin%40amd.co m%7C
d4
bd
c7 59eb274bfccc8208d9daef41fe%7C3dd8961fe4884e608e11a82d994e183 d%7C
0%
7C
0% 7C637781543416430632%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjA wMDA
iL
CJ
QI joiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&amp;sdata=n FpVl
%2
BV
oF 40JPabfRMcR6Cz7ZHDwt%2BBpLDBNdXftJaA%3D&amp;reserved=0

Quoted 1,177 lines

void amdgpu_dm_initialize_dp_connector(struct
amdgpu_display_manager *dm, diff --git
a/drivers/gpu/drm/drm_dp_mst_topology.c
b/drivers/gpu/drm/drm_dp_mst_topology.c
index 857c5d15e81d..a70e26c5d084 100644

drivers/gpu/drm/drm_dp_mst_topology.c +1 -0
@@ -2508,6 +2508,7 @@ drm_dp_mst_handle_conn_stat(struct
 drm_dp_mst_branch *mstb,
        u8 new_pdt;
        bool new_mcs;
        bool dowork = false, create_connector = false;
+       bool notify_unplug_event = false;
 
        port = drm_dp_get_port(mstb, conn_stat-
Quoted 1 line

port_number);

        if (!port)
drivers/gpu/drm/drm_dp_mst_topology.c +0 -0
@@ -2520,6 +2521,9 @@ drm_dp_mst_handle_conn_stat(struct
 drm_dp_mst_branch *mstb,
                         * port, so just throw the port

out
and
make
sure we
* reprobe the link address of
it's
parent
MSTB

                         */

+ /* should also consider
notify_unplug_event
here.
+ * but it's not a normal case for
products
+in the
market
+ */

drm_dp_mst_topology_unlink_port(mgr,

port);
                        mstb->link_address_sent = false;
                        dowork = true; @@ -2541,10

+2545,14 @@
drm_dp_mst_handle_conn_stat(struct
drm_dp_mst_branch
*mstb,

        port->ddps = conn_stat-
Quoted 1 line

displayport_device_plug_status;

        if (old_ddps != port->ddps) {
drivers/gpu/drm/drm_dp_mst_topology.c +1 -1
-               if (port->ddps && !port->input)
+               if (port->ddps && !port->input) {

drm_dp_send_enum_path_resources(mgr,
mstb,
port);

drivers/gpu/drm/drm_dp_mst_topology.c +3 -1
-               else
+               } else {
                        port->full_pbn = 0;
+                       if (port->connector &&
+

drm_dp_mst_is_end_device(port-

Quoted 1 line

pdt,

+port-

Quoted 1 line

mcs))

+ notify_unplug_event =
true;
+ }

        }

        new_pdt = port->input ? DP_PEER_DEVICE_NONE :

conn_stat-

Quoted 1 line

peer_device_type;

drivers/gpu/drm/drm_dp_mst_topology.c +0 -0
@@ -2557,11 +2565,15 @@ drm_dp_mst_handle_conn_stat(struct

drm_dp_mst_branch
*mstb,

                dowork = false;
        }
drivers/gpu/drm/drm_dp_mst_topology.c +3 -0
+       if (notify_unplug_event &&
+mgr->cbs->notify_csn_disconnection)
+               mgr->cbs->notify_csn_disconnection(port-
Quoted 1 line

connector);

drivers/gpu/drm/drm_dp_mst_topology.c +2 -0
+
        if (port->connector)
                drm_modeset_unlock(&mgr->base.lock);
        else if (create_connector)
                drm_dp_mst_port_add_connector(mstb, port);
 
+
 out:
        drm_dp_mst_topology_put_port(port);
        if (dowork)
(quoted diff) +0 -0

b/include/drm/drm_dp_mst_helper.h index
78044ac5b59b..ff9e47729841
100644

include/drm/drm_dp_mst_helper.h +0 -0
@@ -525,6 +525,7 @@ struct drm_dp_mst_topology_cbs {
         * IRQ pulse handler.
         */
        void (*poll_hpd_irq)(struct

drm_dp_mst_topology_mgr
*mgr);
+ void (*notify_csn_disconnection)(struct
drm_connector
+*connector);
};

#define DP_MAX_PAYLOAD (sizeof(unsigned long) * 8)
--
2.31.0

Quoted 1,061 lines

-----Original Message-----
From: Lin, Wayne
Sent: Wednesday, December 8, 2021 11:39 AM
To: 'Lyude Paul' <[email protected]>
Subject: RE: [PATCH] WIP: drm/dp_mst: Add support for dumping
topology ref histories from debugfs

No worries Lyude!
Thanks for keeping helping on this. Take your time : )

Quoted 1,050 lines

-----Original Message-----
From: Lyude Paul <[email protected]>
Sent: Wednesday, December 8, 2021 7:05 AM
To: Lin, Wayne <[email protected]>
Subject: Re: [PATCH] WIP: drm/dp_mst: Add support for
dumping
topology ref histories from debugfs

Sorry! I will try to get to this tomorrow, if not then
sometime
this week.

On Tue, 2021-11-30 at 08:41 +0000, Lin, Wayne wrote:
[Public]

Hi Lyude,

Finally have some bandwidth to get back to this
problem!
I roughly went through this patch and I'm just aware
that
we
already have such kind of convenient tool for a
while.
I think it's definitely useful for us to track
port/mstb
reference count issues and I'll start to embrace
this
feature
for cleaning up those issues. Thank you Lyude!

However, I think the issue that I was trying to fix
is
not
related to what you suggested:
" The idea here is that if stream resources aren't
being
released, my guess would be that we're not dropping
topology
references for the port which means the connector
never
goes
away."
The issue I was trying to fix is about releasing
dc_link->remote_sinks while receiving a CSN message
notifying
the connection status of a sst connector of a port
changed
from connected to disconnected. Not the connection
status
changed of a mst branch device.
e.g.
src - 1st_mstb - 2nd_mstb - sst_monitor => src -
1st_mstb
(disconnect) 2nd_mstb - sst_monitor

In above case, after receiving CSN, we will put
topology
references of 2nd mstb and its port which is
connected
with
the sst monitor. As the result of that, we can call
drm_dp_delayed_destroy_port() to unregister and put
the
drm
connector.

However, in below case:
e.g.
src - 1st_mstb - sst_monitor => src - 1st_mstb
(disconnect)
sst_monitor

In this case, which is the case having problem, it
definitely
won't decrease the topology references count of the
port
which
was connected to the sst monitor to zero since the
port
is
still existing in the topology. Same as the malloc
reference
since the port can't get destroyed. Hence, the port
still
exists and we won't call
drm_dp_delayed_destroy_port() to unregister and put
the
drm
connector.
I looked up the code and
drm_dp_delayed_destroy_port()
seems
like the only place to call drm_connector_put()
which
means
we
can't put reference count of drm connector under
this
case
and
can't release dc_sink resource by destroying drm
connector.

I would also like to point out that this resource
(remote_sinks) is specific to different stream
sinks. So
if
we're trying to release this dc_sink resource by
destroying
the drm connector, it conflicts the idea that you
suggested
before that we should always keep the drm connector
until
it's
no longer reachable in the topology.
Releasing dc_sink should be binding with the
disconnection
event.

I understand your concern that we should not just
easily
change the logic here since it's the result after
solving
tons
of bugs before and might cause other side effect.
So,
just
my
2 cents, what I'm thinking is to register a callback
function
for our driver to notify us that the remote sink is
detached.
This just aligns our flow handling long HPD event of
legacy
(sst) DP.
For sst case, once we detect long HPD event
indicating
the
monitor is detached, we will immediately try to
release
the
dc_link->local_sink and fire hotplug event to upper
layer.
Same as here, once receives a CSN message notifying
a
drm
connector is changed from connected to disconnected,
trigger
the callback function and we can try to release the
dc_sink
resource.

Would like to know your thought and insight please :
)

Btw, I got some errors and warnings while building
and
have
some code adjustments as below : ) Thank you Lyude
for
your
always kindly help!!

Regards,
Wayne
-----Original Message-----
From: Lyude Paul <[email protected]>
Sent: Wednesday, November 3, 2021 7:15 AM
To: Lin, Wayne <[email protected]>
Subject: [PATCH] WIP: drm/dp_mst: Add support for
dumping
topology ref histories from debugfs

TODO:
* Implement support for i915
* Finish writing this commit message
* ???
* profit

Signed-off-by: Lyude Paul <[email protected]>
---

Hey wayne! SO-hopefully if I did this correctly
then
this
should just work on amdgpu. What this patch should
do
is
add
a debugfs file to amdgpu called
"amdgpu_dp_mst_topology_refs", and when you read
the
file
it
should print out the topology reference history of
every
MSTB and Port in memory, along with how many times
we've
hit
the codepath in each backtrace. An example:

Port DP-5 (0000000005c37748) topology ref history:
1 gets (last at 58.468973):
drm_dp_send_link_address+0x6a5/0xa00
[drm_kms_helper]
drm_dp_check_and_send_link_address+0xad/0xd0
[drm_kms_helper]
drm_dp_mst_link_probe_work+0x14e/0x1a0
[drm_kms_helper]
process_one_work+0x1e3/0x390
worker_thread+0x50/0x3a0
kthread+0x124/0x150
ret_from_fork+0x1f/0x30
1 puts (last at 58.469357):
drm_dp_mst_topology_put_port+0x6a/0x210
[drm_kms_helper]
drm_dp_send_link_address+0x39e/0xa00
[drm_kms_helper]
drm_dp_check_and_send_link_address+0xad/0xd0
[drm_kms_helper]
drm_dp_mst_link_probe_work+0x14e/0x1a0
[drm_kms_helper]
process_one_work+0x1e3/0x390
worker_thread+0x50/0x3a0
kthread+0x124/0x150
ret_from_fork+0x1f/0x30

The idea here is that if stream resources aren't
being
released, my guess would be that we're not
dropping
topology
references for the port which means the connector
never
goes
away. So, if that's really the case then once we
unplug
the
offending connector we should be able to find a
pair
of
gets/puts for the offending leaked connector where
the
get/put count doesn't match up. Also, if the frame
count
on
the backtrace isn't long enough you can increase
the
value
of STACK_DEPTH in
drivers/gpu/drm/drm_dp_mst_topology.c
and
recompile to get more frames.

To enable this, first enable CONFIG_EXPERT for
your
kernel
which will unhide
CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS.
Then just enable
CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS,
recompile, and it should be good to go.

Let me know if this works for you, and hopefully
this
should
tell us exactly what the problem actually is here.

.../amd/display/amdgpu_dm/amdgpu_dm_debugfs.c |
35
++++
drivers/gpu/drm/drm_dp_mst_topology.c |
173
++++++++++++++----
drivers/gpu/drm/nouveau/nouveau_debugfs.c |
35
++++
include/drm/drm_dp_mst_helper.h |
18
++
4 files changed, 228 insertions(+), 33
deletions(-)

diff --git
a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_
debu
gf
s.
c
b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_
debu
gf
s.
c
index 1a68a674913c..1a14732c52b4 100644
---
a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_
debu
gf
s.
c
+++
b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_
debu
gf
+++ s.c
@@ -3063,6 +3063,37 @@ static int
mst_topo_show(struct
seq_file *m, void
*unused)
return 0;
}

+#ifdef CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS
+static int mst_topology_ref_dump_show(struct
seq_file
*m,
+void
+*unused) {
+ struct amdgpu_device *adev = (struct
amdgpu_device
+*)m->private;
+ struct drm_device *dev = adev_to_drm(adev);
+ struct drm_connector *connector;
+ struct drm_connector_list_iter conn_iter;
+ struct amdgpu_dm_connector *aconnector;
+
+ drm_connector_list_iter_begin(dev,
&conn_iter);
+ drm_for_each_connector_iter(connector,
&conn_iter)
{
+ if (connector->connector_type !=
DRM_MODE_CONNECTOR_DisplayPort)
+ continue;
+
+ aconnector =
+to_amdgpu_dm_connector(connector);
+
+ /* Ensure we're only dumping the
topology
of
a
+root mst node
*/
+ if (!aconnector-
mst_mgr.max_payloads)
+ continue;
+
+ seq_printf(m, "\nMST topology for
connector
+%d\n",
aconnector->connector_id);
+ drm_dp_mst_dump_topology_refs(m,
+&aconnector->mst_mgr);
+ }
+ drm_connector_list_iter_end(&conn_iter);
+
+ return 0;
+}
+
+DEFINE_SHOW_ATTRIBUTE(mst_topology_ref_dump);
+#endif
+
/*
* Sets trigger hpd for MST topologies.
* All connected connectors will be rediscovered
and
re
started as needed if val of 1 is sent.
@@ -3299,6 +3330,10 @@ void
dtn_debugfs_init(struct
amdgpu_device
*adev)

debugfs_create_file("amdgpu_mst_topology",
0444,
root,
adev, &mst_topo_fops);
+#ifdef CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS
+
debugfs_create_file("amdgpu_dp_mst_topology_refs",
+0444, root, adev,
+
&mst_topology_ref_dump_fops);
+#endif
debugfs_create_file("amdgpu_dm_dtn_log",
0644,
root,
adev,
&dtn_log_fops);

diff --git a/drivers/gpu/drm/drm_dp_mst_topology.c
b/drivers/gpu/drm/drm_dp_mst_topology.c
index 1aa8702383d4..0159828c494d 100644
--- a/drivers/gpu/drm/drm_dp_mst_topology.c
+++ b/drivers/gpu/drm/drm_dp_mst_topology.c
@@ -1366,23 +1366,6 @@ static int
drm_dp_mst_wait_tx_reply(struct drm_dp_mst_branch
*mstb,
return ret;
}

-static struct drm_dp_mst_branch
*drm_dp_add_mst_branch_device(u8 lct, u8
*rad) -{
- struct drm_dp_mst_branch *mstb;
-
- mstb = kzalloc(sizeof(*mstb), GFP_KERNEL);
- if (!mstb)
- return NULL;
-
- mstb->lct = lct;
- if (lct > 1)
- memcpy(mstb->rad, rad, lct / 2);
- INIT_LIST_HEAD(&mstb->ports);
- kref_init(&mstb->topology_kref);
- kref_init(&mstb->malloc_kref);
- return mstb;
-}
-
static void drm_dp_free_mst_branch_device(struct
kref
*kref) {
struct drm_dp_mst_branch *mstb = @@ -1642,12
+1625,20
@@ topology_ref_type_to_str(enum
drm_dp_mst_topology_ref_type type)
return "put";
}

+static const char
*topology_ref_history_type_to_str(enum
+drm_dp_mst_topology_history_type type) {
+ if (type ==
DRM_DP_MST_TOPOLOGY_HISTORY_PORT)
+ return "Port";
+ else
+ return "MSTB"; }
+
static void
-__dump_topology_ref_history(struct
drm_dp_mst_topology_ref_history *history,
- void *ptr, const char
*type_str)
+dump_topology_ref_history(struct
+drm_dp_mst_topology_ref_history *history, struct
+drm_printer p)
{
- struct drm_printer p =
drm_debug_printer(DBG_PREFIX);
+ char *connector_name = NULL;
char *buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
+ void *ptr;
int i;

if (!buf)
@@ -1656,14 +1647,29 @@
__dump_topology_ref_history(struct
drm_dp_mst_topology_ref_history *history,
if (!history->len)
goto out;

+ /* Get a pointer to the actual MSTB/port so
we
can
the
+memory
address to the kernel log */
+ if (history->type ==
DRM_DP_MST_TOPOLOGY_HISTORY_PORT)
+ ptr = container_of(history, struct
+drm_dp_mst_port,
topology_ref_history);
+ else
+ ptr = container_of(history, struct
+drm_dp_mst_branch, topology_ref_history);
+
/* First, sort the list so that it goes from
oldest
to
newest
* reference entry
*/
sort(history->entries, history->len,
sizeof(*history->entries),
topology_ref_history_cmp, NULL);

- drm_printf(&p, "%s (%p) topology count
reached
0,
dumping history:\n",
- type_str, ptr);
+ if (history->type ==
DRM_DP_MST_TOPOLOGY_HISTORY_PORT)
+{
+ struct drm_dp_mst_port *port = ptr;
+
+ if (port->connector)
+ connector_name =
+port->connector->name;
+ }
+ if (connector_name)
+ drm_printf(&p, "Port %s (%p)
topology
ref
+history:\n",
connector_name, ptr);
+ else
+ drm_printf(&p, "%s (%p) topology ref
+history:\n",
+
+topology_ref_history_type_to_str(history->type),
ptr);

for (i = 0; i < history->len; i++) {
const struct
drm_dp_mst_topology_ref_entry
*entry = @@
-
1682,24 +1688,92 @@
__dump_topology_ref_history(struct
drm_dp_mst_topology_ref_history *history,
ts_nsec, rem_nsec / 1000,
buf);
}

- /* Now free the history, since this is the
only
time
we expose it */
- kfree(history->entries);
out:
kfree(buf);
}

+/**
+ * drm_dp_mst_dump_topology_refs - helper
function
for
+dumping the topology ref history
+ * @m: File to print to
+ * @mgr: &struct drm_dp_mst_topology_mgr to use
+ *
+ * Prints the topology ref history of all ports
and
MSTBs
+on @mgr that are still in memory,
+ * regardless of whether they're actually still
reachable
+through the topology or not. Only enabled
+ * when %CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS is
enabled.
+Can be implemented by drivers to assist
+ * with debugging leaks in the DP MST helpers.
+ */
+void drm_dp_mst_dump_topology_refs(struct
seq_file
*m,
+struct drm_dp_mst_topology_mgr *mgr) {
+ struct drm_dp_mst_topology_ref_history
*history;
+ struct drm_printer p =
drm_seq_file_printer(m);
+
+ mutex_lock(&mgr->topology_ref_history_lock);
+ list_for_each_entry(history,
+&mgr->topology_ref_history_list,
+node)
+ dump_topology_ref_history(history,
p);
+ mutex_unlock(&mgr-
topology_ref_history_lock);
+}
+EXPORT_SYMBOL(drm_dp_mst_dump_topology_refs);
+
+static void
+__init_topology_ref_history(struct
+drm_dp_mst_topology_ref_history
*history,
+ struct
drm_dp_mst_topology_mgr
+*mgr,
+ enum
+drm_dp_mst_topology_history_type
+type) {
+ history->type = type;
+ INIT_LIST_HEAD(&history->node);
+
+ mutex_lock(&mgr->topology_ref_history_lock);
+ list_add(&history->node,
+&mgr->topology_ref_history_list);
+ mutex_unlock(&mgr-
topology_ref_history_lock);
+}
+
+static void
+__destroy_topology_ref_history(struct
+drm_dp_mst_topology_ref_history
*history,
+ struct
drm_dp_mst_topology_mgr
+*mgr) {
+ mutex_lock(&mgr->topology_ref_history_lock);
+ list_del(&mgr->topology_ref_history_list);
+ mutex_unlock(&mgr-
topology_ref_history_lock);
+
+ kfree(history->entries); }
+
+static __always_inline void
+init_port_topology_history(struct
drm_dp_mst_topology_mgr
+*mgr, struct drm_dp_mst_port *port) {
+
+__init_topology_ref_history(&port-
topology_ref_history,
+mgr,
+
+DRM_DP_MST_TOPOLOGY_HISTORY_PORT);
+}
+
+static __always_inline void
+init_mstb_topology_history(struct
drm_dp_mst_topology_mgr
+*mgr, struct drm_dp_mst_branch *mstb) {
+
+__init_topology_ref_history(&mstb-
topology_ref_history,
+mgr,
+
+DRM_DP_MST_TOPOLOGY_HISTORY_MSTB);
+}
+
+static __always_inline void
+destroy_port_topology_history(struct
drm_dp_mst_port
*port)
+{
+
+__destroy_topology_ref_history(&port-
topology_ref_history,
+port->mgr); }
+
+static __always_inline void
+destroy_mstb_topology_history(struct
drm_dp_mst_branch
+*mstb) {
+
+__destroy_topology_ref_history(&mstb-
topology_ref_history,
+mstb->mgr); }
+
static __always_inline void
dump_mstb_topology_history(struct
drm_dp_mst_branch
*mstb)
{
-
__dump_topology_ref_history(&mstb-
topology_ref_history,
mstb,
- "MSTB");
+ dump_topology_ref_history(&mstb-
topology_ref_history,
+drm_debug_printer(DBG_PREFIX));
}

static __always_inline void
dump_port_topology_history(struct drm_dp_mst_port
*port)
{
-
__dump_topology_ref_history(&port-
topology_ref_history,
port,
- "Port");
+ dump_topology_ref_history(&port-
topology_ref_history,
+drm_debug_printer(DBG_PREFIX));
}

static __always_inline void @@ -1729,6 +1803,14
@@
topology_ref_history_unlock(struct
drm_dp_mst_topology_mgr *mgr) } #else static
inline
void
+init_port_topology_history(struct
drm_dp_mst_topology_mgr
+*mgr, struct drm_dp_mst_port *port); static
inline
void
Should also add the bracket, otherwise will get
warnings.
=> static inline void
init_port_topology_history(struct
drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port
*port)
{};

+init_mstb_topology_history(struct
drm_dp_mst_topology_mgr
+*mgr, struct drm_dp_mst_branch *mstb); static
inline
void
Same as above
=> static inline void
init_mstb_topology_history(struct
drm_dp_mst_topology_mgr *mgr, struct
drm_dp_mst_branch
*mstb)
{};

+destroy_port_topology_history(struct
+drm_dp_mst_topology_mgr *mgr, struct
drm_dp_mst_port
+*port); static inline void
destroy_port_topology_history() takes one parameter
only
which
is port.
=> destroy_port_topology_history(struct
drm_dp_mst_port
*port)
{};

+destroy_mstb_topology_history(struct
+drm_dp_mst_topology_mgr *mgr, struct
drm_dp_mst_branch
+*mstb); static inline void
destroy_mstb_topology_history() takes one parameter
only
which
is mstb => destroy_mstb_topology_history(struct
drm_dp_mst_branch
*mstb) {};

topology_ref_history_lock(struct
drm_dp_mst_topology_mgr
*mgr) {} static inline void
topology_ref_history_unlock(struct
drm_dp_mst_topology_mgr *mgr) {} @@ -1740,6
+1822,25
@@
dump_port_topology_history(struct drm_dp_mst_port
*port)
{}
#define save_port_topology_ref(port, type) #endif

+static struct drm_dp_mst_branch *
+drm_dp_add_mst_branch_device(struct
drm_dp_mst_topology_mgr
+*mgr,
+u8 lct, u8 *rad) {
+ struct drm_dp_mst_branch *mstb;
+
+ mstb = kzalloc(sizeof(*mstb), GFP_KERNEL);
+ if (!mstb)
+ return NULL;
+
+ mstb->lct = lct;
+ if (lct > 1)
+ memcpy(mstb->rad, rad, lct / 2);
+ INIT_LIST_HEAD(&mstb->ports);
+ kref_init(&mstb->topology_kref);
+ kref_init(&mstb->malloc_kref);
+ init_mstb_topology_history(mgr, mstb);
+ return mstb;
+}
+
static void
drm_dp_destroy_mst_branch_device(struct
kref
*kref) {
struct drm_dp_mst_branch *mstb = @@ -1747,6
+1848,7
@@
static void
drm_dp_destroy_mst_branch_device(struct
kref *kref)
struct drm_dp_mst_topology_mgr *mgr = mstb-
mgr;

dump_mstb_topology_history(mstb);
+ destroy_mstb_topology_history(mstb);

INIT_LIST_HEAD(&mstb->destroy_next);

@@ -1856,6 +1958,7 @@ static void
drm_dp_destroy_port(struct
kref
*kref)
struct drm_dp_mst_topology_mgr *mgr = port-
mgr;

dump_port_topology_history(port);
+ destroy_port_topology_history(port);

/* There's nothing that needs locking to
destroy
an
input port yet */
if (port->input) {
@@ -2135,7 +2238,7 @@ drm_dp_port_set_pdt(struct
drm_dp_mst_port *port, u8 new_pdt,
ret =
drm_dp_mst_register_i2c_bus(port);
} else {
lct =
drm_dp_calculate_rad(port,
rad);
- mstb =
drm_dp_add_mst_branch_device(lct, rad);
+ mstb =
+drm_dp_add_mst_branch_device(mgr, lct, rad);
if (!mstb) {
ret = -ENOMEM;
drm_err(mgr->dev,
"Failed
to
create MSTB for port %p", port); @@ -2366,6
+2469,8 @@
drm_dp_mst_add_port(struct drm_device *dev,
*/
drm_dp_mst_get_mstb_malloc(mstb);

+ init_port_topology_history(mgr, port);
+
return port;
}

@@ -3745,7 +3850,7 @@ int
drm_dp_mst_topology_mgr_set_mst(struct
drm_dp_mst_topology_mgr *mgr, bool ms
}

/* add initial branch device at LCT
1 */
- mstb =
drm_dp_add_mst_branch_device(1,
NULL);
+ mstb =
drm_dp_add_mst_branch_device(mgr,
1,
+NULL);
if (mstb == NULL) {
ret = -ENOMEM;
goto out_unlock; @@ -5512,14
+5617,16
@@ int drm_dp_mst_topology_mgr_init(struct
drm_dp_mst_topology_mgr *mgr,
mutex_init(&mgr->delayed_destroy_lock);
mutex_init(&mgr->up_req_lock);
mutex_init(&mgr->probe_lock); -#if
IS_ENABLED(CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS)
- mutex_init(&mgr->topology_ref_history_lock);
-#endif
INIT_LIST_HEAD(&mgr->tx_msg_downq);
INIT_LIST_HEAD(&mgr->destroy_port_list);
INIT_LIST_HEAD(&mgr-
destroy_branch_device_list);
INIT_LIST_HEAD(&mgr->up_req_list);

+#if
IS_ENABLED(CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS)
+ mutex_init(&mgr->topology_ref_history_lock);
+ INIT_LIST_HEAD(&mgr-
topology_ref_history_list);
+#endif
+
/*
* delayed_destroy_work will be queued on a
dedicated
WQ, so that any
* requeuing will be also flushed when
deiniting
the
topology manager.
diff --git
a/drivers/gpu/drm/nouveau/nouveau_debugfs.c
b/drivers/gpu/drm/nouveau/nouveau_debugfs.c
index 1cbe01048b93..53ec7c1daada 100644
--- a/drivers/gpu/drm/nouveau/nouveau_debugfs.c
+++ b/drivers/gpu/drm/nouveau/nouveau_debugfs.c
@@ -29,9 +29,13 @@
*/

#include <linux/debugfs.h>
+#include <drm/drm_dp_mst_helper.h> #include
+<drm/drm_encoder.h>
#include <nvif/class.h>
#include <nvif/if0001.h>
+#include <nouveau_encoder.h>
#include "nouveau_debugfs.h"
+#include "nouveau_display.h"
#include "nouveau_drv.h"

static int
@@ -68,6 +72,34 @@
nouveau_debugfs_strap_peek(struct
seq_file *m, void
*data)
return 0;
}

+#ifdef CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS
+static int
nouveau_debugfs_mst_topology_history(struct
+seq_file *m, void *data) {
+ struct drm_info_node *node = m->private;
+ struct drm_device *dev = node->minor->dev;
+ struct drm_encoder *encoder;
+ struct nouveau_display *disp =
nouveau_display(dev);
+
+ if (disp->disp.object.oclass < GF110_DISP)
+ return -EINVAL;
+
+ drm_for_each_encoder(encoder, dev) {
+ struct nv50_mstm *mstm;
+
+ /* We need the real encoder for each
MST
mgr
+*/
+ if (encoder->encoder_type !=
+DRM_MODE_ENCODER_TMDS)
+ continue;
+ mstm = nouveau_encoder(encoder)-
dp.mstm;
+ if (!mstm)
+ continue;
+
+ seq_printf(m, "MSTM on %s:\n",
encoder-
name);
+ drm_dp_mst_dump_topology_refs(m,
&mstm-
mgr);
+ }
+ return 0;
+}
+#endif
+
static int
nouveau_debugfs_pstate_get(struct seq_file *m,
void
*data)
{ @@
-213,6
+245,9 @@ static const struct file_operations
nouveau_pstate_fops = { static struct
drm_info_list
nouveau_debugfs_list[] = {
{ "vbios.rom", nouveau_debugfs_vbios_image,
0,
NULL
},
{ "strap_peek", nouveau_debugfs_strap_peek,
0,
NULL
},
+#ifdef CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS
+ { "dp_mst_topology_refs",
+nouveau_debugfs_mst_topology_history, 0, NULL },
#endif
};
#define NOUVEAU_DEBUGFS_ENTRIES
ARRAY_SIZE(nouveau_debugfs_list)

diff --git a/include/drm/drm_dp_mst_helper.h
b/include/drm/drm_dp_mst_helper.h index
78044ac5b59b..e3a73d8d97c0
100644
--- a/include/drm/drm_dp_mst_helper.h
+++ b/include/drm/drm_dp_mst_helper.h
@@ -35,6 +35,11 @@ enum
drm_dp_mst_topology_ref_type {
DRM_DP_MST_TOPOLOGY_REF_PUT,
};

+enum drm_dp_mst_topology_history_type {
+ DRM_DP_MST_TOPOLOGY_HISTORY_PORT,
+ DRM_DP_MST_TOPOLOGY_HISTORY_MSTB, };
+
struct drm_dp_mst_topology_ref_history {
struct drm_dp_mst_topology_ref_entry {
enum drm_dp_mst_topology_ref_type
type;
@@
-43,6
+48,9 @@ struct drm_dp_mst_topology_ref_history {
depot_stack_handle_t backtrace;
} *entries;
int len;
+
+ enum drm_dp_mst_topology_history_type type;
+ struct list_head node;
};
#endif /*
IS_ENABLED(CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS)
*/

@@ -769,6 +777,12 @@ struct
drm_dp_mst_topology_mgr {
* &drm_dp_mst_branch.topology_ref_history.
*/
struct mutex topology_ref_history_lock;
+
+ /**
+ * @topology_ref_history_list: contains a
list
of
+topology ref
histories for any MSTBs or
+ * ports that are still in memory
+ */
+ struct list_head topology_ref_history_list;
#endif
};

@@ -873,6 +887,10 @@ void
drm_dp_mst_put_port_malloc(struct
drm_dp_mst_port *port);

struct drm_dp_aux
*drm_dp_mst_dsc_aux_for_port(struct
drm_dp_mst_port *port);

+#ifdef CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS
+void drm_dp_mst_dump_topology_refs(struct
seq_file
*m,
+struct drm_dp_mst_topology_mgr *mgr); #endif
+
extern const struct drm_private_state_funcs
drm_dp_mst_topology_state_funcs;

/**
--
2.31.1

--
Cheers,
Lyude Paul (she/her)
Software Engineer at Red Hat

Quoted 6 lines
Quoted 2 lines

--
Cheers, Lyude Paul (she/her) Software Engineer at Red Hat

--
Regards,
Wayne Lin

--
Cheers,
Lyude Paul (she/her)
Software Engineer at Red Hat

Quoted 5 lines
Quoted 4 lines

--
Cheers,
Lyude Paul (she/her)
Software Engineer at Red Hat

Quoted 4 lines

--
Cheers,
Lyude Paul (she/her)
Software Engineer at Red Hat

--
Regards,
Wayne Lin

author
#18 lore raw

Hey! Figured I'd check if there's been any status updates here since it's been a while, just to make sure I haven't dropped this issue from my radar. No problem if you're busy :)

Quoted 2,384 lines · Wayne

[Public]

Quoted 99 lines

-----Original Message-----
From: Lyude Paul <[email protected]>
Sent: Wednesday, March 16, 2022 8:48 AM
To: Lin, Wayne <[email protected]>
Cc: [email protected]; [email protected]
Subject: Re: [PATCH] WIP: drm/dp_mst: Add support for dumping topology ref histories from debugfs

(Adding this back to the dri-devel mailing list since I didn't notice it got
dropped from there)

Hm, some comments on this issue down below. Sorry for the delayed response, I
was going to try this right after I finished the MST legacy removal but that's
ending up taking longer than I hoped.

Quoted 34 lines · Wayne

[AMD Official Use Only]

Oops...
I didn't notice that I replied to wrong mail previously : P Sorry for the confusion and reply to the correct thread.

Good day!
I probably know why you can't reproduce the issue there. Please refer to the
attached
patch file which makes me easier to explain this.

In dm_dp_mst_get_modes(), we will create a new dc_sink by calling dc_link_add_remote_sink() and add that dc_sink into the array dc_link-

Quoted 1 line

remote_sinks[].

However, if we find that aconnector->dc_sink is not null, then we won't create a new
dc_sink for that aconnector and won't add the sink to the array dc_link-

Quoted 1 line

remote_sinks[].

When the issue that I mentioned before occurs, we won't be able to release
the dc_sink
for the aconnector when we unplug the sst monitor. Hence, we can't create
new dc_sink
for the latter on connected new stream sink of that port. Which leads to two
issues
1. Unplug monitor and plug in another monitor "to the same port" =>  Since we use the same dc_sink to reflect the capability of the new connected stream
sink, we might send out inappropriate stream to the new connected sink.
2. Because we can't release dc_sink in the array dc_link-

Quoted 7 lines
Quoted 1 line

remote_sinks[],

when we
plug/unplug sst monitor to more than 4 different mst port, we will break the
criteria
"dc_link->sink_count >= MAX_SINKS_PER_LINK" in link_add_remote_sink_helper().

Ok, a lot of this seems to be happening in amdgpu which certainly explains why
I had so much trouble following along with this originally (also, having learned a bit more about how DC works definitely has helped a bit). I already
see a bunch of issues though with how this code is structured that would definitely break things, though note I haven't sat down and tried this on a
real machine yet - will try tomorrow.

So - it seems like dc_sink == a bunch of hotplugging state for a dm connector,
which actually tells me for one that this is definitely the wrong spot for this code. get_modes() really should just handle filling out DRM modes and pretty much nothing else, because callers from DRM aren't going to expect side-effects like this when get_modes() is called - which could potentially
lead to all sorts of weirdness down the line if the DRM call paths that use
this ever change. i915 and nouveau have good examples of what these functions
should typically look like, and amdgpu actually seems to mostly follow this
advice for it's other get_modes() callback.

Note there's also another problem here: the assumption "no EDID == disconnected" isn't correct. It's totally possible to run into EDID-less displays if the display is some ancient pre-historic relic, or if the ROM (or
EEPROM? can't remember what type of chip computer displays tend to use...) chip
in the monitor containing the EDID has died. Note that the second situation is
suprisingly more common then one might think! I ran into a 140Hz 4k ASUS display where this happened, and I know another friend of mine who had the ROM
in their display die randomly as well.

Quoted 8 lines

So with this being said: I, think the solution here is actually just to add/remove dc_sink from dm_dp_mst_detect() based on the CSN, and drop the calls from get_modes()? detect() should always be called before the connector
has been removed, and we could use the return status from drm_dp_mst_detect_port() in order to tell whether or not we should add/remove
the dc_sink.

Thank you Lyude for guiding me through! After your elaboration, I think I roughly
know the idea from drm perspective now. Let me get through this and will get back
to you.

Quoted 10 lines

If you don't get to it first tomorrow, I'll see if I can reproduce this locally and try writing up a patch.

Also a final note: I don't think it's a good idea in general for there to be a
fixed size array of dc_sinks like this. Those probably should just be stored
somehow on a per-connector basis. I say this because in addition to the fact

Understand and agree with that. I plan to remove the constraint as well. Thanks!

Quoted 35 lines

MST connectors aren't destroyed immediately (which could lead to a situation
where we have more then 6 connected MST ports briefly before their destructors
run, e.g. during suspend/resume reprobing).

Also, one question below:

Quoted 21 lines

Anyway, these are my steps to reproduce the issue.
I use a startech mst 1to4 mst hub, a sst monitor and a mst monitor.

1. boot up with sst monitor connected to the hub and plug the hub to the system.
2. unplug/plug sst monitor to all left three different DP output ports. Which is used to
Pollute all four aconnectors to have them keep the stale dc_sinks.
3. plug in the mst monitor to arbitrary ports. Since the new connected mst
monitor will
have us add new mst port to the topology and attach a new aconnector for that mst port,
we'll try to create a new dc_sink to that new aconnector and break the sink_count
limitation. As you can see in the log the error message : "*ERROR* Unable to
add a
remote sink"

Quoted 6 lines

(Note that different hub might influence the result. Some hubs will pull down the HPD
when there is no monitor connected to its downstream ports which will run
into different
code flow)

Quoted 3 lines

by "pull down" I assume you mean doing a long HPD instead of short (or vice
versa?).

Yes, long HPD!

Thanks for your help Lyude!

Quoted 2,203 lines
Quoted 6 lines

I also attach the dmesg log under different debug level (0x104 & 0x116) for
your reference.

Quoted 141 lines

Please let me know if you can reproduce then : )

Sorry about that Lyude and hope this time I send to the right thread.. Really appreciate for your help!!!

Regards,
Wayne

Quoted 134 lines

-----Original Message-----
From: Lyude Paul <[email protected]>
Sent: Wednesday, March 2, 2022 9:10 AM
To: Lin, Wayne <[email protected]>
Subject: Re: [PATCH] WIP: drm/dp_mst: Add support for dumping topology ref
histories from debugfs

Ran into some issues with my system yesterday (hooray being the first to
find
a rather annoying zsh bug that broke my scripts!), but I've finally got
things
setup. I'm not able to reproduce this issue though (I've tried hotplugging
the
way you mentioned here, but nothing seems to leak), which makes me think
there's something special in your setup we might not have noticed.

Do you think you could get some drm logs with drm.debug=0x116 log_buf_len=50M
from when this happens so I can get a better idea of the drm events leading up
to this? Also, if you've been testing this in a GUI could you confirm whether
this happens when you're just in a plain old VT?

Quoted 103 lines · Lyude Paul

Alright - starting to look into this now and setting up a system to try
reproducing this, will let you know as soon as I've got results. Sorry
again
about all of the delays

Quoted 95 lines · Lyude Paul

I didn't get a chance yet, but I've gotten through most of my backlog
now.
Sorry about the delay - I've been catching up since I had to take two
weeks
off work very unexpectedly. I'll put aside time to look at this first
thing
monday.

Quoted 83 lines · Wayne

[AMD Official Use Only]

Hi Lyude,

Good day!
Sorry to bother you, but would like to know if you can reproduce the
issue?

Thanks!

Regards,
Wayne

Quoted 69 lines

-----Original Message-----
From: Lyude Paul <[email protected]>
Sent: Thursday, February 3, 2022 4:12 AM
To: Lin, Wayne <[email protected]>
Cc: [email protected]
Subject: Re: [PATCH] WIP: drm/dp_mst: Add support for dumping topology
ref
histories from debugfs

JFYI I should be able to respond to this soon, I think I'm following
your
issue a bit more (haven't gotten to go through the patches too so
I'm
probably
going to see if I can reproduce it locally as well. Sorry for the
delay
-
dealing with some X.org stuff and work issues at the moment

Quoted 45 lines · Wayne

[Public]

Hey Lyude,

Sorry, just got chance to get back to this and BIG THANKS to elaborating
in
such details!
Following are some of my comments : )

Quoted 11 lines

-----Original Message-----
From: Lyude Paul <[email protected]>
Sent: Wednesday, January 19, 2022 9:59 AM
To: Lin, Wayne <[email protected]>
Cc: [email protected]
Subject: Re: [PATCH] WIP: drm/dp_mst: Add support for dumping
topology
ref
histories from debugfs

Quoted 4 lines

Hey - so, the original issue here was that payloads were not
always
deleted when we expected them to be - correct?

Actually, what I was trying to do is not relevant to payload maintenance.
What I wanted to do is having a way to notify amdgpu to release its enumerated resource "dc_sink" when unplug
event
occurs. Currently, under specific scenarios,
we can't release remote "dc_sink" and ends up can't allocate resource
for
new connected monitors. I'll explain the scenario underneath.

So, get back to the main issue that I was trying to solve when I
pushed
the
patchset:

https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Fpatchwork.kernel.org%2Fproject%2Fdri&amp;data=04%7C01%7Cw
ayne.lin%40amd.com%7Cd77e829cfd3a466e25e508da06e6a9dd%7C3dd8961fe4884e608e 11a82d994e183d%7C0%7C0%7C637829885024 474689%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI 6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&amp;sda ta=MvKilTT1CkBqZVK1N24JyNPjiPYORV%2FsCwvEzwx%2FIAk%3D&amp;reserved=0

Quoted 2,044 lines
Quoted 2,044 lines

ayne.lin%40amd.com%7Cbbc27043edc04bf8067c08d9fbe9620c%7C3dd8961fe4884e 608e
11a82d994e183d%7C0%7C0%7C6378178020798 98960%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJB TiI6
Ik1haWwiLCJXVCI6Mn0%3D%7C3000&amp;sdat a=RVUc8KgTO3mhFL9qemshHJqiyXJs%2Fvytx21AMyUaW%2Bs%3D&amp;reserved=0

Quoted 2,036 lines
Quoted 2,035 lines
Quoted 2,034 lines
Quoted 2,033 lines
Quoted 1 line

-

devel%2Fcover%2F20210720160342.11415-1- Wayne.Lin%40amd.com%2F&amp;data=04%7C01%7Cwayne.lin%40amd.com% 7C2e
19d9
32
1b
5f4e948c1208d9e6884f24%7C3dd8961fe488 4e608e11a82d994e183d%7C0%7C0%7C637794295422403301%7CUnknown%7C TWFp
bGZs
b3
d8
eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiL CJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&amp;sdata=rvtwQioO3pT1BoH b7S5
32ms
Ra
61
gP1vIyE8AB7M9I4E%3D&amp;reserved=0

Quoted 2,002 lines

I was trying to fix some problems that I observed after these 2
patches
* 09b974e8983 drm/amd/amdgpu_dm/mst: Remove -

Quoted 1 line

destroy_connector()

callback
* 72dc0f51591 drm/dp_mst: Remove
drm_dp_mst_topology_cbs.destroy_connector

With above patches, it changes our flow to remove dc_sink (by
calling
dc_link_remote_sink()) when connector is about to be destroyed. And let's see below use cases:
a). Disconnection happens between mst branch devices e.g.
src - 1st_mstb - 2nd_mstb - sst_monitor => src - 1st_mstb (disconnect)
2nd_mstb - sst_monitor

In above case, after receiving CSN, we will put topology reference
count
of
2nd mstb and its port which connected to the sst monitor. As you suggested before, we will destroy 2nd mst branch device
and
its
all
ports due to there is no more mst path can route to them. As the result of that, we at end call drm_dp_delayed_destroy_port()
and
unregister/put the drm connector and eventually call dc_link_remote_sink() to release the dc_sinck we enumerate for
the
specific
stream sink of sst_monitor.

However, in below case:
b). Disconnection happens between mst branch devices and SST monitor
e.g.
src - 1st_mstb - sst_monitor => src - 1st_mstb (disconnect) sst_monitor

In this case, which is the case having problem, it definitely
won't
decrease
the topology references count of the port of 1st_mstb which was connected to the sst monitor to zero since the port is still
existing in
the topology. Same as the malloc reference since the port can't
get
destroyed. Hence, the port still exists and we won't call drm_dp_delayed_destroy_port() to unregister and put the drm connector.
As
the
result of that, we can't release corresponding remote dc_sink
under
this
case. And after few times hotplugs, we can't create any new dc_sink since the number of stream sink is exceeding our limitation.

Hence, what I'm thinking here is to register a callback function
for
notifying us that the remote sink is detached. This just aligns
what
we
do
for handling long HPD event (unplug monitor from system) of legacy
DP
(none
mst). For which case, once we detect long HPD event indicating the monitor is detached, we will immediately try to
release
the
dc_link->local_sink and fire hotplug event to upper layer. Same as here, once receives a CSN message notifying a drm connector
is
changed from connected to disconnected, trigger the callback function and we can try to release the dc_sink resource.

Quoted 63 lines

If I'm remembering that correctly, then (and I'm not 100% sure
on
this
yet) I might already have noticed something very possibly wonky in
how we handle payload allocations currently, that I found while
working on
the non-atomic removal branch that I linked to you in my previous response. Basically, in the
step1() function it looks like that we follow this general flow
with
updating
payloads:

 * Loop through proposed payloads and compare to previously
programmed
   payloads
    - If a payload has a different allocation then it previously
did,
update the
      payload
    - If the payload is new, create it
    - If a payload no longer has an allocation, remove the payload

At first glance this seems totally correct - but I'm not actually
entirely
convinced it is? Particularly because it seems like the order
in
which
we do creation/deletion of payloads is totally arbitrary. To
explain
what
I mean by that, consider a state transition like this:

vcpi_slots=15 vcpi_slots=35 vcpi_slots=14

Quoted 1 line

1 | 2 |xxxxxxxx|

Let's say we want to increase payload #1 from 15 to 50, and
disable
payload #2 in the same atomic commit on DRM's side. If the order
we
update payloads is entirely arbitrary, we could end up doing
this:

 * Increase VCPI slots payload #1 from 15->50 (total VCPI slots=99,
overflow!)
 * Decrease VCPI slots payload #2 from 35->0  (total VCPI slots=50)

Quoted 13 lines

Notice on the first step, we've technically overflowed the available
number of VCPI slots in the payload table. This is still before
step
2
though, and I could be totally wrong here - perhaps this is
entirely
OK in
the real world, and we're allowed to overflow VC slots as long
as

I think it's forbidden to allocate time slots beyond 64 when we're
updating
payload ID table of the branch in the real world. Branch probably
will NACK the DPCD write.

Quoted 7 lines

we repair the issue before step 2. But so far I can't seem to
find
anything in the DP 2.0 spec that explicitly states this would
be
OK

Quoted 8 lines

-
which
makes me think we might fail some payload allocations if we
don't
always
ensure we avoid overflows in between VC payload table changes

Agree.
For amdgpu, I think we always do deallocation (call hws-

Quoted 1 line

funcs.reset_hw_ctx_wrap()) before allocation

(apply_single_controller_ctx_to_hw()).

Quoted 30 lines

Note that avoiding overflows would be as simple as just making
sure
we
send all VC payload table changes that free VC slots before
sending
any that take new slots.

Again - I haven't actually confirmed this yet and am hoping to
test
stuff
like this very soon as I'm pretty close finishing up the initial
attempt
at removing the non-atomic MST code in the DRM helpers now. If
my
theory
ends up being correct here, I can fix this in my rewrite of
the
MST payload management code. But I figured it might be worth
mentioning in
the mean time in case you think it might be relevant to the
problem here :).

Quoted 107 lines
Quoted 106 lines · Lyude Paul

(CC'ing this to dri-devel, since this is basically patch review)

Alright - so first off sorry about the broken debugging patch! I
thought I had tested that but I guess I hadn't tested it well
enough,
I'll get it fixed asap, but feel free to try getting to it
before
I
do.

As for this patch... (comments below)

Quoted 71 lines · Wayne

[AMD Official Use Only]

Hi Lyude,

No rush and thanks for your time! : ) Just want to help
clarify
what
I mean that "registering a callback function" for CSN of disconnecting
stream sink device (not mst branch case). Attach below the
tentative
patch that I planned to do. Thanks so much!

Regards,
Wayne
---
 .../display/amdgpu_dm/amdgpu_dm_mst_types.c   | 53
+++++++++++++++++++
 drivers/gpu/drm/drm_dp_mst_topology.c         | 16
+++++-
 include/drm/drm_dp_mst_helper.h               |  1 +

 3 files changed, 68 insertions(+), 2 deletions(-)

diff --git a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_mst_ type
s.c b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_mst_ type
s.c
index cc34a35d0bcb..d7343c64908c 100644
---
a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_mst_ type
s.c
+++
b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_mst_ type
s.c

(quoted diff) +1 -0
@@ -472,8 +472,61 @@ dm_dp_add_mst_connector(struct
 drm_dp_mst_topology_mgr *mgr,
         return connector;
  }
 
+void dm_dp_notify_csn_disconnection(struct

drm_connector
+*connector) {
+       struct amdgpu_dm_connector *aconnector = +               to_amdgpu_dm_connector(connector); +       struct dc_sink *dc_sink = aconnector->dc_sink; +       struct dc_link *dc_link = aconnector->dc_link; +       struct amdgpu_device *adev = drm_to_adev(ddev);

(quoted diff) +8 -0
+
+       ASSERT(dc_link);
+
+       if (dc_sink) {
+               mutex_lock(&adev->dm.dc_lock);
+
+               /*clear the remote sink of the link*/
+               dc_link_remove_remote_sink(dc_link,

dc_sink);
+               dc_sink_release(dc_sink);
+               aconnector->dc_sink = NULL;

(quoted diff) +5 -0
+
+               mutex_unlock(&adev->dm.dc_lock);
+       }
+}
+
Quoted 7 lines

 static const struct drm_dp_mst_topology_cbs
dm_mst_cbs =
{
        .add_connector = dm_dp_add_mst_connector,
+       .notify_csn_disconnection =
dm_dp_notify_csn_disconnection,
 };

I still don't really think this is a good idea. This seems
like
it's
just adding another hotplugging path to the code in order to
avoid
sending hotplugs for non-endpoint devices. In addition to
the

So, what I wanted to do is to add a callback function for endpoint
devices
case as described above. For disconnection of non-endpoint devices
is
already taken care by current code.

Quoted 61 lines
Quoted 61 lines

drm_connector issues I mentioned before, we also really need
to
stop
doing any kind of payload maintence in hotplugging paths.
The
reality
is any kind of payload maintanence we do outside of normal
modesetting
paths is a relic from legacy modesetting that I'm dropping
ASAP
(more
on this below), and we can't keep adding to it because it
dramatically
complicates maintanence as well.

Sorry for repeating this point so often but - the biggest
issue
too
is
I'm still not sure what it is we're even avoiding here. We
know
resources aren't released consistently, and that we're able
to
reproduce the behavior with repeated hotplugs. We also know
that
if
we
skip sending certain hotplug events, that fixes the issue.
And
we
know
we can workaround it by adding a special case for forcing a
payload
release in DC. But none of those actually tell us exactly
what
piece
of code is leaking and why, which means that any workarounds
we're
putting in to avoid this mysterious guilty section of code
we
don't
entirely understand either - which means we're just adding
more
code
in that no one actually fully understands. This just

Quoted 8 lines
Quoted 8 lines

ends up
making
maintence difficult because every change in code nearby workarounds
like this has to strugle to try to figure out said workarounds
in
order to avoid breaking things.

Thank you Lyude for the reminder and I totally agree with you by
avoiding
adding workaround since it will get the code maintenance harder.
And
I
think the root cause of the issue that I observed before is quite
concrete.
There is no code path for us to release dc_sink under the unplug
scenario
that I described above.

Quoted 131 lines
Quoted 131 lines

I'm actually currently running into these "later" issues right
now,
as
recently I've (-actively-, finally!!!) been working on trying to
remove as much non-atomic MST as I can because. As it turns
out
-
a
huge amount of the payload maintanence code just isn't needed at
all
when we stop caring about non-atomic drivers and stick everything
in
atomic state structs. Step 1 for updating updating the payload
table,
e.g. drm_dp_update_payload_part1(), is a great example of
how
messy
things have become. Here's a small sample of some of the stuff
I've
seen from just that one function so far that either just don't
make
sense here or is totally redundant. I should note that a lot
of
these
things also come from patches I reviwed, but didn't really
look
at
as
closely as I should have because I was swamped at work, some
are
historical artifacts, and others are less-than-ideal patches
I
got
wrote myself when I was first started working on MST and didn't
know
the code as well as I do
now:

 * We try to avoid some sort of userspace issue by using drm_dp_mst_port_downstream_of_branch() to avoid releasing
payloads
for a
   branch if we can't prove it's downstream of the top of
the
topology. This
   seems to workaround either a userspace bug. This is a redundant,
since
   that's what topology refs are already supposed to be doing to
the
extent is
   reasonably possibly. It's also unfortunately racy solution
because
we have
   to be able to handle the situation where a connector is
removed
from under
   us. That can happen at any time, including _immediately_
after
we
call
   drm_dp_mst_port_downstream_of_branch() - rendering the
call
not
really
   useful.
 * If we fail to validate the sink in drm_dp_update_payload_part,
we
don't
   update the payload table. I think at best this solution
is
racy
and
not
   useful, at worst it leaves us with a payload table that
doesn't
match what
   we attempted to set in the atomic state - which at worst
brings
us
into
   undefined territory where we're just plain out of sync
with
the
reality in
   hw.
 * Actually fun fact - mgr->payloads and mgr-

Quoted 1 line

proposed_vcpis

both
can
and
   definitely should be removed entirely. All of the info
for
mgr->payloads
   could just be in the atomic state, because that + the magic
of
atomic state
   duplication means we'll also have an accurate view of the
previous
state's
   payload allocations: which renders mgr-

Quoted 7 lines
Quoted 7 lines
Quoted 1 line

proposed_vcpis

redundant.

Apologies for the long explanation again, but I hope that
explains
my

Really really thanks for your kindly help on this : ) I'll try my best to get to your WIP branch soon. Thank you Lyude!

Quoted 1,426 lines
Quoted 1,425 lines

point here a bit. I'm going to be trying to get to moving
amdgpu's
DSC
code out of amdgpu and into DRM helpers as well soon, so I'm
really
determined to clean stuff up beforehand as every time I've
done
so
it's become substantially easier to make changes to this code.
Things
used to be even worse before I started cleaning things up 2
or 3
years
ago, where simple changes would end up getting me stuck spending
hours
trying to dig through lockdep or memory manegement issues.
As
well,
I
would be entirely unsurprised if bugs like this very payload
leak
we're working on just disappear once we've gotten rid of all
the
extraneous workarounds and state tracking here - especially
with
how
many special cases we have for maintaining the payload table
right
now. That's certainly ended up being the case in the past
with a
number of other difficult to track down issues I've dealt
with
in
MST.

Anyhow. I've been way more productive this year then last
because
I
had over a month off work and am finally not super burnt out
from
my
job, and so far I've been making progress on the payload state
cleanup
far faster then I was last year :). I think if you can't figure
out
where the leak is coming from even once I get the debugging
patches
I
mentioned fixed up, it might be a good idea for us to try
again
after
I've got some of this code cleaned up. I've got a currently
WIP
branch
here:

https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgitl
ab.freedesktop.org%2Flyudess%2Flinux%2F- %2Fcommit%2F624dd68fa804e64b5b 2060e4735d7317090692b5&amp;data=04%7C01%7Cwayne.lin%40am d.co
m%7C
d4
bd
c7 59eb274bfccc8208d9daef41fe%7C3dd8961fe4884e608e11a82d994 e183
d%7C
0%
7C
0% 7C637781543416430632%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4 wLjA
wMDA
iL
CJ
QI joiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&amp;sda ta=n
FpVl
%2
BV
oF 40JPabfRMcR6Cz7ZHDwt%2BBpLDBNdXftJaA%3D&amp;reserved=0

Quoted 1,319 lines

 void amdgpu_dm_initialize_dp_connector(struct
amdgpu_display_manager *dm, diff --git
a/drivers/gpu/drm/drm_dp_mst_topology.c
b/drivers/gpu/drm/drm_dp_mst_topology.c
index 857c5d15e81d..a70e26c5d084 100644

drivers/gpu/drm/drm_dp_mst_topology.c +0 -0
@@ -2508,6 +2508,7 @@

drm_dp_mst_handle_conn_stat(struct drm_dp_mst_branch *mstb,
        u8 new_pdt;
        bool new_mcs;
        bool dowork = false, create_connector = false; +       bool notify_unplug_event = false;

        port = drm_dp_get_port(mstb, conn_stat-

Quoted 1 line

port_number);

        if (!port)

drivers/gpu/drm/drm_dp_mst_topology.c +0 -0
@@ -2520,6 +2521,9 @@

drm_dp_mst_handle_conn_stat(struct drm_dp_mst_branch *mstb,
                         * port, so just throw the port
out
and
make
sure we
                         * reprobe the link address of it's
parent
MSTB
                         */
+                       /* should also consider notify_unplug_event
here.
+                        * but it's not a normal case for
products
+in the
market
+                        */

drm_dp_mst_topology_unlink_port(mgr, port);
                        mstb->link_address_sent = false;
                        dowork = true; @@ -2541,10 +2545,14 @@ drm_dp_mst_handle_conn_stat(struct drm_dp_mst_branch
*mstb,
        port->ddps = conn_stat-

Quoted 1 line

displayport_device_plug_status;

        if (old_ddps != port->ddps) {
-               if (port->ddps && !port->input)
+               if (port->ddps && !port->input) {

drm_dp_send_enum_path_resources(mgr,
mstb,
port);
-               else
+               } else {
                        port->full_pbn = 0;
+                       if (port->connector &&
+
drm_dp_mst_is_end_device(port-

Quoted 1 line

pdt,

+port-

Quoted 1 line

mcs))

+                               notify_unplug_event = true;
+               }
        }

        new_pdt = port->input ? DP_PEER_DEVICE_NONE :
conn_stat-

Quoted 1 line

peer_device_type;

drivers/gpu/drm/drm_dp_mst_topology.c +0 -0
@@ -2557,11 +2565,15 @@

drm_dp_mst_handle_conn_stat(struct
drm_dp_mst_branch
*mstb,
                dowork = false;
        }

+       if (notify_unplug_event &&
+mgr->cbs->notify_csn_disconnection)
+               mgr->cbs-

Quoted 2 lines

notify_csn_disconnection(port-
connector);

+
        if (port->connector) drm_modeset_unlock(&mgr->base.lock); else if (create_connector) drm_dp_mst_port_add_connector(mstb, port);

+
 out:
        drm_dp_mst_topology_put_port(port);
        if (dowork)

(quoted diff) +0 -0

b/include/drm/drm_dp_mst_helper.h index
78044ac5b59b..ff9e47729841
100644

include/drm/drm_dp_mst_helper.h +0 -0
@@ -525,6 +525,7 @@ struct drm_dp_mst_topology_cbs {
          * IRQ pulse handler.
          */

        void (*poll_hpd_irq)(struct
drm_dp_mst_topology_mgr
*mgr);
+       void (*notify_csn_disconnection)(struct
drm_connector
+*connector);
 };

 #define DP_MAX_PAYLOAD (sizeof(unsigned long) * 8) --
2.31.0

Quoted 1,195 lines

-----Original Message-----
From: Lin, Wayne
Sent: Wednesday, December 8, 2021 11:39 AM
To: 'Lyude Paul' <[email protected]>
Subject: RE: [PATCH] WIP: drm/dp_mst: Add support
for
dumping
topology ref histories from debugfs

No worries Lyude!
Thanks for keeping helping on this. Take your time :
)

-----Original Message-----
From: Lyude Paul <[email protected]>
Sent: Wednesday, December 8, 2021 7:05 AM
To: Lin, Wayne <[email protected]>
Subject: Re: [PATCH] WIP: drm/dp_mst: Add support
for
dumping
topology ref histories from debugfs

Sorry! I will try to get to this tomorrow, if not
then
sometime
this week.

On Tue, 2021-11-30 at 08:41 +0000, Lin, Wayne
wrote:
[Public]

Hi Lyude,

Finally have some bandwidth to get back to this
problem!
I roughly went through this patch and I'm just
aware
that
we
already have such kind of convenient tool for a
while.
I think it's definitely useful for us to track
port/mstb
reference count issues and I'll start to embrace
this
feature
for cleaning up those issues. Thank you Lyude!

However, I think the issue that I was trying to
fix
is
not
related to what you suggested:
" The idea here is that if stream resources
aren't
being
released, my guess would be that we're not
dropping
topology
references for the port which means the
connector
never
goes
away."
The issue I was trying to fix is about releasing
dc_link->remote_sinks while receiving a CSN
message
notifying
the connection status of a sst connector of a
port
changed
from connected to disconnected. Not the
connection
status
changed of a mst branch device.
e.g.
src - 1st_mstb - 2nd_mstb - sst_monitor => src -
1st_mstb
(disconnect) 2nd_mstb - sst_monitor

In above case, after receiving CSN, we will put
topology
references of 2nd mstb and its port which is
connected
with
the sst monitor. As the result of that, we can
call
drm_dp_delayed_destroy_port() to unregister and
put
the
drm
connector.

However, in below case:
e.g.
src - 1st_mstb - sst_monitor => src - 1st_mstb
(disconnect)
sst_monitor

In this case, which is the case having problem,
it
definitely
won't decrease the topology references count of
the
port
which
was connected to the sst monitor to zero since
the
port
is
still existing in the topology. Same as the
malloc
reference
since the port can't get destroyed. Hence, the
port
still
exists  and we won't call
drm_dp_delayed_destroy_port() to unregister and
put
the
drm
connector.
I looked up the code and
drm_dp_delayed_destroy_port()
seems
like the only place to call drm_connector_put()
which
means
we
can't put reference count of drm connector under
this
case
and
can't release dc_sink resource by destroying drm
connector.

I would also like to point out that this
resource
(remote_sinks) is specific to different stream
sinks. So
if
we're trying to release this dc_sink resource by
destroying
the drm connector, it conflicts the idea that
you
suggested
before that we should always keep the drm
connector
until
it's
no longer reachable in the topology.
Releasing dc_sink should be binding with the
disconnection
event.

I understand your concern that we should not
just
easily
change the logic here since it's the result
after
solving
tons
of bugs before and might cause other side
effect.
So,
just
my
2 cents, what I'm thinking is to register a
callback
function
for our driver to notify us that the remote sink
is
detached.
This just aligns our flow handling long HPD
event of
legacy
(sst) DP.
For sst case, once we detect long HPD event
indicating
the
monitor is detached, we will immediately try to
release
the
dc_link->local_sink and fire hotplug event to
upper
layer.
Same as here, once receives a CSN message
notifying
a
drm
connector is changed from connected to
disconnected,
trigger
the callback function and we can try to release
the
dc_sink
resource.

Would like to know your thought and insight
please :
)

Btw, I got some errors and warnings while
building
and
have
some code adjustments as below : ) Thank you
Lyude
for
your
always kindly help!!

Regards,
Wayne
-----Original Message-----
From: Lyude Paul <[email protected]>
Sent: Wednesday, November 3, 2021 7:15 AM
To: Lin, Wayne <[email protected]>
Subject: [PATCH] WIP: drm/dp_mst: Add support
for
dumping
topology ref histories from debugfs

TODO:
* Implement support for i915
* Finish writing this commit message
* ???
* profit

Signed-off-by: Lyude Paul <[email protected]>
---

Hey wayne! SO-hopefully if I did this
correctly
then
this
should just work on amdgpu. What this patch
should
do
is
add
a debugfs file to amdgpu called
"amdgpu_dp_mst_topology_refs", and when you
read
the
file
it
should print out the topology reference
history of
every
MSTB and Port in memory, along with how many
times
we've
hit
the codepath in each backtrace. An example:

Port DP-5 (0000000005c37748) topology ref
history:
  1 gets (last at    58.468973):
     drm_dp_send_link_address+0x6a5/0xa00
[drm_kms_helper]

drm_dp_check_and_send_link_address+0xad/0xd0
[drm_kms_helper]
     drm_dp_mst_link_probe_work+0x14e/0x1a0
[drm_kms_helper]
     process_one_work+0x1e3/0x390
     worker_thread+0x50/0x3a0
     kthread+0x124/0x150
     ret_from_fork+0x1f/0x30
  1 puts (last at    58.469357):
     drm_dp_mst_topology_put_port+0x6a/0x210
[drm_kms_helper]
     drm_dp_send_link_address+0x39e/0xa00
[drm_kms_helper]

drm_dp_check_and_send_link_address+0xad/0xd0
[drm_kms_helper]
     drm_dp_mst_link_probe_work+0x14e/0x1a0
[drm_kms_helper]
     process_one_work+0x1e3/0x390
     worker_thread+0x50/0x3a0
     kthread+0x124/0x150
     ret_from_fork+0x1f/0x30

The idea here is that if stream resources
aren't
being
released, my guess would be that we're not
dropping
topology
references for the port which means the
connector
never
goes
away. So, if that's really the case then once
we
unplug
the
offending connector we should be able to find
a
pair
of
gets/puts for the offending leaked connector
where
the
get/put count doesn't match up. Also, if the
frame
count
on
the backtrace isn't long enough you can
increase
the
value
of STACK_DEPTH in
drivers/gpu/drm/drm_dp_mst_topology.c
and
recompile to get more frames.

To enable this, first enable CONFIG_EXPERT for
your
kernel
which will unhide
CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS.
Then just enable
CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS,
recompile, and it should be good to go.

Let me know if this works for you, and
hopefully
this
should
tell us exactly what the problem actually is
here.

 .../amd/display/amdgpu_dm/amdgpu_dm_debugfs.c
|
35
++++
 drivers/gpu/drm/drm_dp_mst_topology.c
|
173
++++++++++++++----
 drivers/gpu/drm/nouveau/nouveau_debugfs.c
|
35
++++
 include/drm/drm_dp_mst_helper.h
|
18
++
 4 files changed, 228 insertions(+), 33
deletions(-)

diff --git
a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu
_dm_
debu
gf
s.
c
b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu
_dm_
debu
gf
s.
c
index 1a68a674913c..1a14732c52b4 100644
---
a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu
_dm_
debu
gf
s.
c
+++
b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu
_dm_
debu
gf
+++ s.c
@@ -3063,6 +3063,37 @@ static int
mst_topo_show(struct
seq_file *m, void
*unused)
      return 0;
 }

+#ifdef CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS
+static int mst_topology_ref_dump_show(struct
seq_file
*m,
+void
+*unused) {
+     struct amdgpu_device *adev = (struct
amdgpu_device
+*)m->private;
+     struct drm_device *dev =
adev_to_drm(adev);
+     struct drm_connector *connector;
+     struct drm_connector_list_iter
conn_iter;
+     struct amdgpu_dm_connector *aconnector;
+
+     drm_connector_list_iter_begin(dev,
&conn_iter);
+     drm_for_each_connector_iter(connector,
&conn_iter)
{
+             if (connector->connector_type !=
DRM_MODE_CONNECTOR_DisplayPort)
+                     continue;
+
+             aconnector =
+to_amdgpu_dm_connector(connector);
+
+             /* Ensure we're only dumping the
topology
of
a
+root mst node
*/
+             if (!aconnector-
mst_mgr.max_payloads)
+                     continue;
+
+             seq_printf(m, "\nMST topology
for
connector
+%d\n",
aconnector->connector_id);
+             drm_dp_mst_dump_topology_refs(m,
+&aconnector->mst_mgr);
+     }
+     drm_connector_list_iter_end(&conn_iter);
+
+     return 0;
+}
+
+DEFINE_SHOW_ATTRIBUTE(mst_topology_ref_dump);
+#endif
+
 /*
  * Sets trigger hpd for MST topologies.
  * All connected connectors will be
rediscovered
and
re
started as needed if val of 1 is sent.
@@ -3299,6 +3330,10 @@ void
dtn_debugfs_init(struct
amdgpu_device
*adev)


debugfs_create_file("amdgpu_mst_topology",
0444,
root,
                          adev,
&mst_topo_fops);
+#ifdef CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS
+
debugfs_create_file("amdgpu_dp_mst_topology_re
fs",
+0444, root, adev,
+
&mst_topology_ref_dump_fops);
+#endif
      debugfs_create_file("amdgpu_dm_dtn_log",
0644,
root,
adev,
                          &dtn_log_fops);

diff --git
a/drivers/gpu/drm/drm_dp_mst_topology.c
b/drivers/gpu/drm/drm_dp_mst_topology.c
index 1aa8702383d4..0159828c494d 100644
--- a/drivers/gpu/drm/drm_dp_mst_topology.c
+++ b/drivers/gpu/drm/drm_dp_mst_topology.c
@@ -1366,23 +1366,6 @@ static int
drm_dp_mst_wait_tx_reply(struct
drm_dp_mst_branch
*mstb,
      return ret;
 }

-static struct drm_dp_mst_branch
*drm_dp_add_mst_branch_device(u8 lct, u8
*rad) -{
-     struct drm_dp_mst_branch *mstb;
-
-     mstb = kzalloc(sizeof(*mstb),
GFP_KERNEL);
-     if (!mstb)
-             return NULL;
-
-     mstb->lct = lct;
-     if (lct > 1)
-             memcpy(mstb->rad, rad, lct / 2);
-     INIT_LIST_HEAD(&mstb->ports);
-     kref_init(&mstb->topology_kref);
-     kref_init(&mstb->malloc_kref);
-     return mstb;
-}
-
 static void
drm_dp_free_mst_branch_device(struct
kref
*kref)  {
      struct drm_dp_mst_branch *mstb = @@ -
1642,12
+1625,20
@@ topology_ref_type_to_str(enum
drm_dp_mst_topology_ref_type type)
              return "put";
 }

+static const char
*topology_ref_history_type_to_str(enum
+drm_dp_mst_topology_history_type type) {
+     if (type ==
DRM_DP_MST_TOPOLOGY_HISTORY_PORT)
+             return "Port";
+     else
+             return "MSTB"; }
+
 static void
-__dump_topology_ref_history(struct
drm_dp_mst_topology_ref_history *history,
-                         void *ptr, const
char
*type_str)
+dump_topology_ref_history(struct
+drm_dp_mst_topology_ref_history *history,
struct
+drm_printer p)
 {
-     struct drm_printer p =
drm_debug_printer(DBG_PREFIX);
+     char *connector_name = NULL;
      char *buf = kzalloc(PAGE_SIZE,
GFP_KERNEL);
+     void *ptr;
      int i;

      if (!buf)
@@ -1656,14 +1647,29 @@
__dump_topology_ref_history(struct
drm_dp_mst_topology_ref_history *history,
      if (!history->len)
              goto out;

+     /* Get a pointer to the actual MSTB/port
so
we
can
the
+memory
address to the kernel log */
+     if (history->type ==
DRM_DP_MST_TOPOLOGY_HISTORY_PORT)
+             ptr = container_of(history,
struct
+drm_dp_mst_port,
topology_ref_history);
+     else
+             ptr = container_of(history,
struct
+drm_dp_mst_branch, topology_ref_history);
+
      /* First, sort the list so that it goes
from
oldest
to
newest
       * reference entry
       */
      sort(history->entries, history->len,
sizeof(*history->entries),
           topology_ref_history_cmp, NULL);

-     drm_printf(&p, "%s (%p) topology count
reached
0,
dumping history:\n",
-                type_str, ptr);
+     if (history->type ==
DRM_DP_MST_TOPOLOGY_HISTORY_PORT)
+{
+             struct drm_dp_mst_port *port =
ptr;
+
+             if (port->connector)
+                     connector_name =
+port->connector->name;
+     }
+     if (connector_name)
+             drm_printf(&p, "Port %s (%p)
topology
ref
+history:\n",
connector_name, ptr);
+     else
+             drm_printf(&p, "%s (%p) topology
ref
+history:\n",
+
+topology_ref_history_type_to_str(history-
type),
ptr);

      for (i = 0; i < history->len; i++) {
              const struct
drm_dp_mst_topology_ref_entry
*entry = @@
-
1682,24 +1688,92 @@
__dump_topology_ref_history(struct
drm_dp_mst_topology_ref_history *history,
                         ts_nsec, rem_nsec /
1000,
buf);
      }

-     /* Now free the history, since this is
the
only
time
we expose it */
-     kfree(history->entries);
 out:
      kfree(buf);
 }

+/**
+ * drm_dp_mst_dump_topology_refs - helper
function
for
+dumping the topology ref history
+ * @m: File to print to
+ * @mgr: &struct drm_dp_mst_topology_mgr to
use
+ *
+ * Prints the topology ref history of all
ports
and
MSTBs
+on @mgr that are still in memory,
+ * regardless of whether they're actually
still
reachable
+through the topology or not. Only enabled
+ * when
%CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS is
enabled.
+Can be implemented by drivers to assist
+ * with debugging leaks in the DP MST
helpers.
+ */
+void drm_dp_mst_dump_topology_refs(struct
seq_file
*m,
+struct drm_dp_mst_topology_mgr *mgr) {
+     struct drm_dp_mst_topology_ref_history
*history;
+     struct drm_printer p =
drm_seq_file_printer(m);
+
+     mutex_lock(&mgr-
topology_ref_history_lock);
+     list_for_each_entry(history,
+&mgr->topology_ref_history_list,
+node)
+
dump_topology_ref_history(history,
p);
+     mutex_unlock(&mgr-
topology_ref_history_lock);
+}
+EXPORT_SYMBOL(drm_dp_mst_dump_topology_refs);
+
+static void
+__init_topology_ref_history(struct
+drm_dp_mst_topology_ref_history
*history,
+                         struct
drm_dp_mst_topology_mgr
+*mgr,
+                         enum
+drm_dp_mst_topology_history_type
+type) {
+     history->type = type;
+     INIT_LIST_HEAD(&history->node);
+
+     mutex_lock(&mgr-
topology_ref_history_lock);
+     list_add(&history->node,
+&mgr->topology_ref_history_list);
+     mutex_unlock(&mgr-
topology_ref_history_lock);
+}
+
+static void
+__destroy_topology_ref_history(struct
+drm_dp_mst_topology_ref_history
*history,
+                            struct
drm_dp_mst_topology_mgr
+*mgr) {
+     mutex_lock(&mgr-
topology_ref_history_lock);
+     list_del(&mgr-
topology_ref_history_list);
+     mutex_unlock(&mgr-
topology_ref_history_lock);
+
+     kfree(history->entries); }
+
+static __always_inline void
+init_port_topology_history(struct
drm_dp_mst_topology_mgr
+*mgr, struct drm_dp_mst_port *port) {
+
+__init_topology_ref_history(&port-
topology_ref_history,
+mgr,
+
+DRM_DP_MST_TOPOLOGY_HISTORY_PORT);
+}
+
+static __always_inline void
+init_mstb_topology_history(struct
drm_dp_mst_topology_mgr
+*mgr, struct drm_dp_mst_branch *mstb) {
+
+__init_topology_ref_history(&mstb-
topology_ref_history,
+mgr,
+
+DRM_DP_MST_TOPOLOGY_HISTORY_MSTB);
+}
+
+static __always_inline void
+destroy_port_topology_history(struct
drm_dp_mst_port
*port)
+{
+
+__destroy_topology_ref_history(&port-
topology_ref_history,
+port->mgr); }
+
+static __always_inline void
+destroy_mstb_topology_history(struct
drm_dp_mst_branch
+*mstb) {
+
+__destroy_topology_ref_history(&mstb-
topology_ref_history,
+mstb->mgr); }
+
 static __always_inline void
 dump_mstb_topology_history(struct
drm_dp_mst_branch
*mstb)
{
-
__dump_topology_ref_history(&mstb-
topology_ref_history,
mstb,
-                                 "MSTB");
+     dump_topology_ref_history(&mstb-
topology_ref_history,
+drm_debug_printer(DBG_PREFIX));
 }

 static __always_inline void
 dump_port_topology_history(struct
drm_dp_mst_port
*port)
{
-
__dump_topology_ref_history(&port-
topology_ref_history,
port,
-                                 "Port");
+     dump_topology_ref_history(&port-
topology_ref_history,
+drm_debug_printer(DBG_PREFIX));
 }

 static __always_inline void @@ -1729,6
+1803,14
@@
topology_ref_history_unlock(struct
drm_dp_mst_topology_mgr *mgr)  }  #else
static
inline
void
+init_port_topology_history(struct
drm_dp_mst_topology_mgr
+*mgr, struct drm_dp_mst_port *port); static
inline
void
Should also add the bracket, otherwise will get
warnings.
=> static inline void
init_port_topology_history(struct
drm_dp_mst_topology_mgr *mgr, struct
drm_dp_mst_port
*port)
{};

+init_mstb_topology_history(struct
drm_dp_mst_topology_mgr
+*mgr, struct drm_dp_mst_branch *mstb); static
inline
void
Same as above
=> static inline void
init_mstb_topology_history(struct
drm_dp_mst_topology_mgr *mgr, struct
drm_dp_mst_branch
*mstb)
{};

+destroy_port_topology_history(struct
+drm_dp_mst_topology_mgr *mgr, struct
drm_dp_mst_port
+*port); static inline void
destroy_port_topology_history() takes one
parameter
only
which
is port.
=> destroy_port_topology_history(struct
drm_dp_mst_port
*port)
{};

+destroy_mstb_topology_history(struct
+drm_dp_mst_topology_mgr *mgr, struct
drm_dp_mst_branch
+*mstb); static inline void
destroy_mstb_topology_history() takes one
parameter
only
which
is mstb => destroy_mstb_topology_history(struct
drm_dp_mst_branch
*mstb) {};

 topology_ref_history_lock(struct
drm_dp_mst_topology_mgr
*mgr) {} static inline void
topology_ref_history_unlock(struct
drm_dp_mst_topology_mgr *mgr) {} @@ -1740,6
+1822,25
@@
dump_port_topology_history(struct
drm_dp_mst_port
*port)
{}
#define save_port_topology_ref(port, type)
#endif

+static struct drm_dp_mst_branch *
+drm_dp_add_mst_branch_device(struct
drm_dp_mst_topology_mgr
+*mgr,
+u8 lct, u8 *rad) {
+     struct drm_dp_mst_branch *mstb;
+
+     mstb = kzalloc(sizeof(*mstb),
GFP_KERNEL);
+     if (!mstb)
+             return NULL;
+
+     mstb->lct = lct;
+     if (lct > 1)
+             memcpy(mstb->rad, rad, lct / 2);
+     INIT_LIST_HEAD(&mstb->ports);
+     kref_init(&mstb->topology_kref);
+     kref_init(&mstb->malloc_kref);
+     init_mstb_topology_history(mgr, mstb);
+     return mstb;
+}
+
 static void
drm_dp_destroy_mst_branch_device(struct
kref
*kref) {
      struct drm_dp_mst_branch *mstb = @@ -
1747,6
+1848,7
@@
static void
drm_dp_destroy_mst_branch_device(struct
kref *kref)
      struct drm_dp_mst_topology_mgr *mgr =
mstb-
mgr;

      dump_mstb_topology_history(mstb);
+     destroy_mstb_topology_history(mstb);

      INIT_LIST_HEAD(&mstb->destroy_next);

@@ -1856,6 +1958,7 @@ static void
drm_dp_destroy_port(struct
kref
*kref)
      struct drm_dp_mst_topology_mgr *mgr =
port-
mgr;

      dump_port_topology_history(port);
+     destroy_port_topology_history(port);

      /* There's nothing that needs locking to
destroy
an
input port yet */
      if (port->input) {
@@ -2135,7 +2238,7 @@
drm_dp_port_set_pdt(struct
drm_dp_mst_port *port, u8 new_pdt,
                      ret =
drm_dp_mst_register_i2c_bus(port);
              } else {
                      lct =
drm_dp_calculate_rad(port,
rad);
-                     mstb =
drm_dp_add_mst_branch_device(lct, rad);
+                     mstb =
+drm_dp_add_mst_branch_device(mgr, lct, rad);
                      if (!mstb) {
                              ret = -ENOMEM;
                              drm_err(mgr-
dev,
"Failed
to
create MSTB for port %p", port); @@ -2366,6
+2469,8 @@
drm_dp_mst_add_port(struct drm_device *dev,
       */
      drm_dp_mst_get_mstb_malloc(mstb);

+     init_port_topology_history(mgr, port);
+
      return port;
 }

@@ -3745,7 +3850,7 @@ int
drm_dp_mst_topology_mgr_set_mst(struct
drm_dp_mst_topology_mgr *mgr, bool ms
              }

              /* add initial branch device at
LCT
1 */
-             mstb =
drm_dp_add_mst_branch_device(1,
NULL);
+             mstb =
drm_dp_add_mst_branch_device(mgr,
1,
+NULL);
              if (mstb == NULL) {
                      ret = -ENOMEM;
                      goto out_unlock; @@ -
5512,14
+5617,16
@@ int drm_dp_mst_topology_mgr_init(struct
drm_dp_mst_topology_mgr *mgr,
      mutex_init(&mgr->delayed_destroy_lock);
      mutex_init(&mgr->up_req_lock);
      mutex_init(&mgr->probe_lock); -#if
IS_ENABLED(CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_RE
FS)
-     mutex_init(&mgr-
topology_ref_history_lock);
-#endif
      INIT_LIST_HEAD(&mgr->tx_msg_downq);
      INIT_LIST_HEAD(&mgr->destroy_port_list);
      INIT_LIST_HEAD(&mgr-
destroy_branch_device_list);
      INIT_LIST_HEAD(&mgr->up_req_list);

+#if
IS_ENABLED(CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_RE
FS)
+     mutex_init(&mgr-
topology_ref_history_lock);
+     INIT_LIST_HEAD(&mgr-
topology_ref_history_list);
+#endif
+
      /*
       * delayed_destroy_work will be queued
on a
dedicated
WQ, so that any
       * requeuing will be also flushed when
deiniting
the
topology manager.
diff --git
a/drivers/gpu/drm/nouveau/nouveau_debugfs.c
b/drivers/gpu/drm/nouveau/nouveau_debugfs.c
index 1cbe01048b93..53ec7c1daada 100644
---
a/drivers/gpu/drm/nouveau/nouveau_debugfs.c
+++
b/drivers/gpu/drm/nouveau/nouveau_debugfs.c
@@ -29,9 +29,13 @@
  */

 #include <linux/debugfs.h>
+#include <drm/drm_dp_mst_helper.h> #include
+<drm/drm_encoder.h>
 #include <nvif/class.h>
 #include <nvif/if0001.h>
+#include <nouveau_encoder.h>
 #include "nouveau_debugfs.h"
+#include "nouveau_display.h"
 #include "nouveau_drv.h"

 static int
@@ -68,6 +72,34 @@
nouveau_debugfs_strap_peek(struct
seq_file *m, void
*data)
      return 0;
 }

+#ifdef CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS
+static int
nouveau_debugfs_mst_topology_history(struct
+seq_file *m, void *data) {
+     struct drm_info_node *node = m->private;
+     struct drm_device *dev = node->minor-
dev;
+     struct drm_encoder *encoder;
+     struct nouveau_display *disp =
nouveau_display(dev);
+
+     if (disp->disp.object.oclass <
GF110_DISP)
+             return -EINVAL;
+
+     drm_for_each_encoder(encoder, dev) {
+             struct nv50_mstm *mstm;
+
+             /* We need the real encoder for
each
MST
mgr
+*/
+             if (encoder->encoder_type !=
+DRM_MODE_ENCODER_TMDS)
+                     continue;
+             mstm = nouveau_encoder(encoder)-
dp.mstm;
+             if (!mstm)
+                     continue;
+
+             seq_printf(m, "MSTM on %s:\n",
encoder-
name);
+             drm_dp_mst_dump_topology_refs(m,
&mstm-
mgr);
+     }
+     return 0;
+}
+#endif
+
 static int
 nouveau_debugfs_pstate_get(struct seq_file
*m,
void
*data)
{ @@
-213,6
+245,9 @@ static const struct file_operations
nouveau_pstate_fops = {  static struct
drm_info_list
nouveau_debugfs_list[] = {
      { "vbios.rom",
nouveau_debugfs_vbios_image,
0,
NULL
},
      { "strap_peek",
nouveau_debugfs_strap_peek,
0,
NULL
},
+#ifdef CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS
+     { "dp_mst_topology_refs",
+nouveau_debugfs_mst_topology_history, 0, NULL
},
#endif
 };
 #define NOUVEAU_DEBUGFS_ENTRIES
ARRAY_SIZE(nouveau_debugfs_list)

diff --git a/include/drm/drm_dp_mst_helper.h
b/include/drm/drm_dp_mst_helper.h index
78044ac5b59b..e3a73d8d97c0
100644
--- a/include/drm/drm_dp_mst_helper.h
+++ b/include/drm/drm_dp_mst_helper.h
@@ -35,6 +35,11 @@ enum
drm_dp_mst_topology_ref_type {
      DRM_DP_MST_TOPOLOGY_REF_PUT,
 };

+enum drm_dp_mst_topology_history_type {
+     DRM_DP_MST_TOPOLOGY_HISTORY_PORT,
+     DRM_DP_MST_TOPOLOGY_HISTORY_MSTB, };
+
 struct drm_dp_mst_topology_ref_history {
      struct drm_dp_mst_topology_ref_entry {
              enum
drm_dp_mst_topology_ref_type
type;
@@
-43,6
+48,9 @@ struct
drm_dp_mst_topology_ref_history {
              depot_stack_handle_t backtrace;
      } *entries;
      int len;
+
+     enum drm_dp_mst_topology_history_type
type;
+     struct list_head node;
 };
 #endif /*
IS_ENABLED(CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_RE
FS)
*/

@@ -769,6 +777,12 @@ struct
drm_dp_mst_topology_mgr {
       *
&drm_dp_mst_branch.topology_ref_history.
       */
      struct mutex topology_ref_history_lock;
+
+     /**
+      * @topology_ref_history_list: contains
a
list
of
+topology ref
histories for any MSTBs or
+      * ports that are still in memory
+      */
+     struct list_head
topology_ref_history_list;
 #endif
 };

@@ -873,6 +887,10 @@ void
drm_dp_mst_put_port_malloc(struct
drm_dp_mst_port *port);

 struct drm_dp_aux
*drm_dp_mst_dsc_aux_for_port(struct
drm_dp_mst_port *port);

+#ifdef CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS
+void drm_dp_mst_dump_topology_refs(struct
seq_file
*m,
+struct drm_dp_mst_topology_mgr *mgr); #endif
+
 extern const struct drm_private_state_funcs
drm_dp_mst_topology_state_funcs;

 /**
--
2.31.1

--
Cheers,
 Lyude Paul (she/her)
 Software Engineer at Red Hat

Quoted 6 lines
Quoted 2 lines

--
Cheers, Lyude Paul (she/her) Software Engineer at Red Hat

--
Regards,
Wayne Lin

--
Cheers,
 Lyude Paul (she/her)
 Software Engineer at Red Hat

Quoted 5 lines
Quoted 4 lines

--
Cheers,
 Lyude Paul (she/her)
 Software Engineer at Red Hat

Quoted 4 lines

--
Cheers,
 Lyude Paul (she/her)
 Software Engineer at Red Hat

--
Regards,
Wayne Lin

Signature · 3 lines
Cheers,
 Lyude Paul (she/her)
 Software Engineer at Red Hat
#19 lore raw

[AMD Official Use Only - General]

Thank you Lyude!

I just have a tentative patch set to deal with this. Would like to give a test on some platforms first. If the result is positive, will send out for review immediately.

Thanks for tracking on this : )

Regards,
Wayne Lin

Quoted 2,405 lines

-----Original Message-----
From: Lyude Paul <[email protected]>
Sent: Thursday, April 21, 2022 7:01 AM
To: Lin, Wayne <[email protected]>
Cc: [email protected]; [email protected]
Subject: Re: [PATCH] WIP: drm/dp_mst: Add support for dumping topology ref histories from debugfs

Hey! Figured I'd check if there's been any status updates here since it's been a while, just to make sure I haven't dropped this issue from my radar. No problem if you're busy :)

Quoted 165 lines · Wayne

[Public]

Quoted 99 lines

-----Original Message-----
From: Lyude Paul <[email protected]>
Sent: Wednesday, March 16, 2022 8:48 AM
To: Lin, Wayne <[email protected]>
Cc: [email protected]; [email protected]
Subject: Re: [PATCH] WIP: drm/dp_mst: Add support for dumping topology ref histories from debugfs

(Adding this back to the dri-devel mailing list since I didn't notice it got
dropped from there)

Hm, some comments on this issue down below. Sorry for the delayed response, I
was going to try this right after I finished the MST legacy removal but that's
ending up taking longer than I hoped.

Quoted 34 lines · Wayne

[AMD Official Use Only]

Oops...
I didn't notice that I replied to wrong mail previously : P Sorry for the confusion and reply to the correct thread.

Good day!
I probably know why you can't reproduce the issue there. Please refer to the
attached
patch file which makes me easier to explain this.

In dm_dp_mst_get_modes(), we will create a new dc_sink by calling dc_link_add_remote_sink() and add that dc_sink into the array dc_link-

Quoted 1 line

remote_sinks[].

However, if we find that aconnector->dc_sink is not null, then we won't create a new
dc_sink for that aconnector and won't add the sink to the array dc_link-

Quoted 1 line

remote_sinks[].

When the issue that I mentioned before occurs, we won't be able to release
the dc_sink
for the aconnector when we unplug the sst monitor. Hence, we can't create
new dc_sink
for the latter on connected new stream sink of that port. Which leads to two
issues
1. Unplug monitor and plug in another monitor "to the same port" => Since we use the same dc_sink to reflect the capability of the new connected stream
sink, we might send out inappropriate stream to the new connected sink.
2. Because we can't release dc_sink in the array dc_link-

Quoted 7 lines
Quoted 1 line

remote_sinks[],

when we
plug/unplug sst monitor to more than 4 different mst port, we will break the
criteria
"dc_link->sink_count >= MAX_SINKS_PER_LINK" in link_add_remote_sink_helper().

Ok, a lot of this seems to be happening in amdgpu which certainly explains why
I had so much trouble following along with this originally (also, having learned a bit more about how DC works definitely has helped a bit). I already
see a bunch of issues though with how this code is structured that would definitely break things, though note I haven't sat down and tried this on a
real machine yet - will try tomorrow.

So - it seems like dc_sink == a bunch of hotplugging state for a dm connector,
which actually tells me for one that this is definitely the wrong spot for this code. get_modes() really should just handle filling out DRM modes and pretty much nothing else, because callers from DRM aren't going to expect side-effects like this when get_modes() is called - which could potentially
lead to all sorts of weirdness down the line if the DRM call paths that use
this ever change. i915 and nouveau have good examples of what these functions
should typically look like, and amdgpu actually seems to mostly follow this
advice for it's other get_modes() callback.

Note there's also another problem here: the assumption "no EDID == disconnected" isn't correct. It's totally possible to run into EDID-less displays if the display is some ancient pre-historic relic, or if the ROM (or
EEPROM? can't remember what type of chip computer displays tend to use...) chip
in the monitor containing the EDID has died. Note that the second situation is
suprisingly more common then one might think! I ran into a 140Hz 4k ASUS display where this happened, and I know another friend of mine who had the ROM
in their display die randomly as well.

Quoted 8 lines

So with this being said: I, think the solution here is actually just to add/remove dc_sink from dm_dp_mst_detect() based on the CSN, and drop the calls from get_modes()? detect() should always be called before the connector
has been removed, and we could use the return status from drm_dp_mst_detect_port() in order to tell whether or not we should add/remove
the dc_sink.

Thank you Lyude for guiding me through! After your elaboration, I think I roughly
know the idea from drm perspective now. Let me get through this and will get back
to you.

Quoted 10 lines

If you don't get to it first tomorrow, I'll see if I can reproduce this locally and try writing up a patch.

Also a final note: I don't think it's a good idea in general for there to be a
fixed size array of dc_sinks like this. Those probably should just be stored
somehow on a per-connector basis. I say this because in addition to the fact

Understand and agree with that. I plan to remove the constraint as well. Thanks!

Quoted 35 lines

MST connectors aren't destroyed immediately (which could lead to a situation
where we have more then 6 connected MST ports briefly before their destructors
run, e.g. during suspend/resume reprobing).

Also, one question below:

Quoted 27 lines

Anyway, these are my steps to reproduce the issue.
I use a startech mst 1to4 mst hub, a sst monitor and a mst monitor.

1. boot up with sst monitor connected to the hub and plug the hub to the system.
2. unplug/plug sst monitor to all left three different DP output ports. Which is used to
Pollute all four aconnectors to have them keep the stale dc_sinks.
3. plug in the mst monitor to arbitrary ports. Since the new connected mst
monitor will
have us add new mst port to the topology and attach a new aconnector for that mst port,
we'll try to create a new dc_sink to that new aconnector and break the sink_count
limitation. As you can see in the log the error message : "*ERROR* Unable to
add a
remote sink"

(Note that different hub might influence the result. Some hubs will pull down the HPD
when there is no monitor connected to its downstream ports which will run
into different
code flow)

Quoted 155 lines
Quoted 3 lines

by "pull down" I assume you mean doing a long HPD instead of short (or vice
versa?).

Yes, long HPD!

Thanks for your help Lyude!

Quoted 148 lines
Quoted 147 lines

I also attach the dmesg log under different debug level (0x104 & 0x116) for
your reference.

Please let me know if you can reproduce then : )

Sorry about that Lyude and hope this time I send to the right thread.. Really appreciate for your help!!!

Regards,
Wayne

Quoted 134 lines

-----Original Message-----
From: Lyude Paul <[email protected]>
Sent: Wednesday, March 2, 2022 9:10 AM
To: Lin, Wayne <[email protected]>
Subject: Re: [PATCH] WIP: drm/dp_mst: Add support for dumping topology ref
histories from debugfs

Ran into some issues with my system yesterday (hooray being the first to
find
a rather annoying zsh bug that broke my scripts!), but I've finally got
things
setup. I'm not able to reproduce this issue though (I've tried hotplugging
the
way you mentioned here, but nothing seems to leak), which makes me think
there's something special in your setup we might not have noticed.

Do you think you could get some drm logs with drm.debug=0x116 log_buf_len=50M
from when this happens so I can get a better idea of the drm events leading up
to this? Also, if you've been testing this in a GUI could you confirm whether
this happens when you're just in a plain old VT?

Quoted 103 lines · Lyude Paul

Alright - starting to look into this now and setting up a system to try
reproducing this, will let you know as soon as I've got results. Sorry
again
about all of the delays

Quoted 95 lines · Lyude Paul

I didn't get a chance yet, but I've gotten through most of my backlog
now.
Sorry about the delay - I've been catching up since I had to take two
weeks
off work very unexpectedly. I'll put aside time to look at this first
thing
monday.

Quoted 83 lines · Wayne

[AMD Official Use Only]

Hi Lyude,

Good day!
Sorry to bother you, but would like to know if you can reproduce the
issue?

Thanks!

Regards,
Wayne

Quoted 69 lines

-----Original Message-----
From: Lyude Paul <[email protected]>
Sent: Thursday, February 3, 2022 4:12 AM
To: Lin, Wayne <[email protected]>
Cc: [email protected]
Subject: Re: [PATCH] WIP: drm/dp_mst: Add support for dumping topology
ref
histories from debugfs

JFYI I should be able to respond to this soon, I think I'm following
your
issue a bit more (haven't gotten to go through the patches too so
I'm
probably
going to see if I can reproduce it locally as well. Sorry for the
delay
-
dealing with some X.org stuff and work issues at the moment

Quoted 45 lines · Wayne

[Public]

Hey Lyude,

Sorry, just got chance to get back to this and BIG THANKS to elaborating
in
such details!
Following are some of my comments : )

Quoted 11 lines

-----Original Message-----
From: Lyude Paul <[email protected]>
Sent: Wednesday, January 19, 2022 9:59 AM
To: Lin, Wayne <[email protected]>
Cc: [email protected]
Subject: Re: [PATCH] WIP: drm/dp_mst: Add support for dumping
topology
ref
histories from debugfs

Quoted 4 lines

Hey - so, the original issue here was that payloads were not
always
deleted when we expected them to be - correct?

Actually, what I was trying to do is not relevant to payload maintenance.
What I wanted to do is having a way to notify amdgpu to release its enumerated resource "dc_sink" when unplug
event
occurs. Currently, under specific scenarios,
we can't release remote "dc_sink" and ends up can't allocate resource
for
new connected monitors. I'll explain the scenario underneath.

So, get back to the main issue that I was trying to solve when I
pushed
the
patchset:

https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Fpatchwork.kernel.org%2Fproject%2Fdri&amp;data=05%7C01
%7Cwayne.lin%40amd.com%7C5f734314a44c4f8f82b908da2321b4c2%7C3dd8961fe4884e608e11a82d994e183d%7C0%7C0%7C63786092 4939974636%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000 %7C%7C%7C&amp;sdata=VtrMzZ2Evr%2BRCUCHIAUPUxqsRuuH9CW3jrNczubY%2BFc%3D&amp;reserved=0

Quoted 2,055 lines
Quoted 2,055 lines

ayne.lin%40amd.com%7Cd77e829cfd3a466e25e508da06e6a9dd%7C3dd8961fe4884e608e 11a82d994e183d%7C0%7C0%7C637829885024 474689%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI 6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&amp;sda ta=MvKilTT1CkBqZVK1N24JyNPjiPYORV%2FsCwvEzwx%2FIAk%3D&amp;reserved=0

Quoted 2,049 lines
Quoted 2,048 lines

ayne.lin%40amd.com%7Cbbc27043edc04bf8067c08d9fbe9620c%7C3dd8961fe4884e 608e
11a82d994e183d%7C0%7C0%7C6378178020798 98960%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJB TiI6
Ik1haWwiLCJXVCI6Mn0%3D%7C3000&amp;sdat a=RVUc8KgTO3mhFL9qemshHJqiyXJs%2Fvytx21AMyUaW%2Bs%3D&amp;reserved=0

Quoted 2,029 lines
Quoted 2,029 lines
Quoted 2,029 lines
Quoted 2,029 lines
Quoted 1 line

-

devel%2Fcover%2F20210720160342.11415-1- Wayne.Lin%40amd.com%2F&amp;data=04%7C01%7Cwayne.lin%40amd.com% 7C2e
19d9
32
1b
5f4e948c1208d9e6884f24%7C3dd8961fe488 4e608e11a82d994e183d%7C0%7C0%7C637794295422403301%7CUnknown%7C TWFp
bGZs
b3
d8
eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiL CJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&amp;sdata=rvtwQioO3pT1BoH b7S5
32ms
Ra
61
gP1vIyE8AB7M9I4E%3D&amp;reserved=0

Quoted 2,008 lines

I was trying to fix some problems that I observed after these 2
patches
* 09b974e8983 drm/amd/amdgpu_dm/mst: Remove -

Quoted 1 line

destroy_connector()

callback
* 72dc0f51591 drm/dp_mst: Remove
drm_dp_mst_topology_cbs.destroy_connector

With above patches, it changes our flow to remove dc_sink (by
calling
dc_link_remote_sink()) when connector is about to be destroyed. And let's see below use cases:
a). Disconnection happens between mst branch devices e.g.
src - 1st_mstb - 2nd_mstb - sst_monitor => src - 1st_mstb (disconnect)
2nd_mstb - sst_monitor

In above case, after receiving CSN, we will put topology reference
count
of
2nd mstb and its port which connected to the sst monitor. As you suggested before, we will destroy 2nd mst branch device
and
its
all
ports due to there is no more mst path can route to them. As the result of that, we at end call drm_dp_delayed_destroy_port()
and
unregister/put the drm connector and eventually call
dc_link_remote_sink() to release the dc_sinck we enumerate
for
the
specific
stream sink of sst_monitor.

However, in below case:
b). Disconnection happens between mst branch devices and SST monitor
e.g.
src - 1st_mstb - sst_monitor => src - 1st_mstb (disconnect) sst_monitor

In this case, which is the case having problem, it definitely
won't
decrease
the topology references count of the port of 1st_mstb which was connected to the sst monitor to zero since the port is still
existing in
the topology. Same as the malloc reference since the port can't
get
destroyed. Hence, the port still exists and we won't call drm_dp_delayed_destroy_port() to unregister and put the drm connector.
As
the
result of that, we can't release corresponding remote dc_sink
under
this
case. And after few times hotplugs, we can't create any new dc_sink since the number of stream sink is exceeding our limitation.

Hence, what I'm thinking here is to register a callback function
for
notifying us that the remote sink is detached. This just aligns
what
we
do
for handling long HPD event (unplug monitor from system) of legacy
DP
(none
mst). For which case, once we detect long HPD event indicating the monitor is detached, we will immediately try to
release
the
dc_link->local_sink and fire hotplug event to upper layer. Same as here, once receives a CSN message notifying a drm connector
is
changed from connected to disconnected, trigger the callback function and we can try to release the dc_sink resource.

Quoted 63 lines

If I'm remembering that correctly, then (and I'm not 100% sure
on
this
yet) I might already have noticed something very possibly wonky in
how we handle payload allocations currently, that I found while
working on
the non-atomic removal branch that I linked to you in my previous response. Basically, in the
step1() function it looks like that we follow this general flow
with
updating
payloads:

* Loop through proposed payloads and compare to
previously
programmed
payloads
- If a payload has a different allocation then it
previously
did,
update the
payload
- If the payload is new, create it
- If a payload no longer has an allocation, remove the
payload

At first glance this seems totally correct - but I'm not actually
entirely
convinced it is? Particularly because it seems like the order
in
which
we do creation/deletion of payloads is totally arbitrary. To
explain
what
I mean by that, consider a state transition like this:

vcpi_slots=15 vcpi_slots=35 vcpi_slots=14

Quoted 1 line

1 | 2 |xxxxxxxx|

Let's say we want to increase payload #1 from 15 to 50, and
disable
payload #2 in the same atomic commit on DRM's side. If the order
we
update payloads is entirely arbitrary, we could end up doing
this:

* Increase VCPI slots payload #1 from 15->50 (total VCPI
slots=99,
overflow!)
* Decrease VCPI slots payload #2 from 35->0 (total VCPI
slots=50)

Quoted 13 lines

Notice on the first step, we've technically overflowed the available
number of VCPI slots in the payload table. This is still before
step
2
though, and I could be totally wrong here - perhaps this is
entirely
OK in
the real world, and we're allowed to overflow VC slots as long
as

I think it's forbidden to allocate time slots beyond 64 when we're
updating
payload ID table of the branch in the real world. Branch probably
will NACK the DPCD write.

Quoted 7 lines

we repair the issue before step 2. But so far I can't seem to
find
anything in the DP 2.0 spec that explicitly states this would
be
OK

Quoted 8 lines

-
which
makes me think we might fail some payload allocations if we
don't
always
ensure we avoid overflows in between VC payload table changes

Agree.
For amdgpu, I think we always do deallocation (call hws-

Quoted 1 line

funcs.reset_hw_ctx_wrap()) before allocation

(apply_single_controller_ctx_to_hw()).

Quoted 30 lines

Note that avoiding overflows would be as simple as just making
sure
we
send all VC payload table changes that free VC slots before
sending
any that take new slots.

Again - I haven't actually confirmed this yet and am hoping to
test
stuff
like this very soon as I'm pretty close finishing up the initial
attempt
at removing the non-atomic MST code in the DRM helpers now. If
my
theory
ends up being correct here, I can fix this in my rewrite of
the
MST payload management code. But I figured it might be worth
mentioning in
the mean time in case you think it might be relevant to the
problem here :).

Quoted 107 lines
Quoted 106 lines · Lyude Paul

(CC'ing this to dri-devel, since this is basically patch review)

Alright - so first off sorry about the broken debugging patch! I
thought I had tested that but I guess I hadn't tested it well
enough,
I'll get it fixed asap, but feel free to try getting to it
before
I
do.

As for this patch... (comments below)

Quoted 78 lines · Wayne

[AMD Official Use Only]

Hi Lyude,

No rush and thanks for your time! : ) Just want to
help
clarify
what
I mean that "registering a callback function"
for CSN of disconnecting
stream sink device (not mst branch case). Attach below
the
tentative
patch that I planned to do. Thanks so much!

Regards,
Wayne
---
.../display/amdgpu_dm/amdgpu_dm_mst_types.c | 53
+++++++++++++++++++
drivers/gpu/drm/drm_dp_mst_topology.c | 16
+++++-
include/drm/drm_dp_mst_helper.h | 1 +
3 files changed, 68 insertions(+), 2 deletions(-)

diff --git
a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_mst_
type
s.c
b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_mst_
type
s.c
index cc34a35d0bcb..d7343c64908c 100644
---
a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_mst_
type
s.c
+++
b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_mst_
type
s.c
@@ -472,8 +472,61 @@ dm_dp_add_mst_connector(struct
drm_dp_mst_topology_mgr *mgr,
return connector;
}

+void dm_dp_notify_csn_disconnection(struct
drm_connector
+*connector) {
+ struct amdgpu_dm_connector *aconnector =
+ to_amdgpu_dm_connector(connector);
+ struct dc_sink *dc_sink = aconnector->dc_sink;
+ struct dc_link *dc_link = aconnector->dc_link;
+ struct amdgpu_device *adev =
drm_to_adev(ddev);
+
+ ASSERT(dc_link);
+
+ if (dc_sink) {
+ mutex_lock(&adev->dm.dc_lock);
+
+ /*clear the remote sink of the link*/
+ dc_link_remove_remote_sink(dc_link,
dc_sink);
+ dc_sink_release(dc_sink);
+ aconnector->dc_sink = NULL;
+
+ mutex_unlock(&adev->dm.dc_lock);
+ }
+}
+
static const struct drm_dp_mst_topology_cbs
dm_mst_cbs =
{
.add_connector = dm_dp_add_mst_connector,
+ .notify_csn_disconnection =
dm_dp_notify_csn_disconnection,
};

I still don't really think this is a good idea. This seems
like
it's
just adding another hotplugging path to the code in order to
avoid
sending hotplugs for non-endpoint devices. In addition to
the

So, what I wanted to do is to add a callback function for endpoint
devices
case as described above. For disconnection of non-endpoint devices
is
already taken care by current code.

Quoted 61 lines
Quoted 61 lines

drm_connector issues I mentioned before, we also really need
to
stop
doing any kind of payload maintence in hotplugging paths.
The
reality
is any kind of payload maintanence we do outside of normal
modesetting
paths is a relic from legacy modesetting that I'm dropping
ASAP
(more
on this below), and we can't keep adding to it because it
dramatically
complicates maintanence as well.

Sorry for repeating this point so often but - the biggest
issue
too
is
I'm still not sure what it is we're even avoiding here. We
know
resources aren't released consistently, and that we're able
to
reproduce the behavior with repeated hotplugs. We also know
that
if
we
skip sending certain hotplug events, that fixes the issue.
And
we
know
we can workaround it by adding a special case for forcing a
payload
release in DC. But none of those actually tell us exactly
what
piece
of code is leaking and why, which means that any workarounds
we're
putting in to avoid this mysterious guilty section of code
we
don't
entirely understand either - which means we're just adding
more
code
in that no one actually fully understands. This just

Quoted 8 lines
Quoted 8 lines

ends up
making
maintence difficult because every change in code nearby workarounds
like this has to strugle to try to figure out said workarounds
in
order to avoid breaking things.

Thank you Lyude for the reminder and I totally agree with you by
avoiding
adding workaround since it will get the code maintenance harder.
And
I
think the root cause of the issue that I observed before is quite
concrete.
There is no code path for us to release dc_sink under the unplug
scenario
that I described above.

Quoted 131 lines
Quoted 131 lines

I'm actually currently running into these "later" issues right
now,
as
recently I've (-actively-, finally!!!) been working on trying to
remove as much non-atomic MST as I can because. As it turns
out
-
a
huge amount of the payload maintanence code just isn't needed at
all
when we stop caring about non-atomic drivers and stick everything
in
atomic state structs. Step 1 for updating updating the payload
table,
e.g. drm_dp_update_payload_part1(), is a great example of
how
messy
things have become. Here's a small sample of some of the stuff
I've
seen from just that one function so far that either just don't
make
sense here or is totally redundant. I should note that a lot
of
these
things also come from patches I reviwed, but didn't really
look
at
as
closely as I should have because I was swamped at work, some
are
historical artifacts, and others are less-than-ideal patches
I
got
wrote myself when I was first started working on MST and didn't
know
the code as well as I do
now:

* We try to avoid some sort of userspace issue by using drm_dp_mst_port_downstream_of_branch() to avoid
releasing
payloads
for a
branch if we can't prove it's downstream of the top
of
the
topology. This
seems to workaround either a userspace bug. This is a
redundant,
since
that's what topology refs are already supposed to be
doing to
the
extent is
reasonably possibly. It's also unfortunately racy
solution
because
we have
to be able to handle the situation where a connector
is
removed
from under
us. That can happen at any time, including
_immediately_
after
we
call
drm_dp_mst_port_downstream_of_branch() - rendering
the
call
not
really
useful.
* If we fail to validate the sink in
drm_dp_update_payload_part,
we
don't
update the payload table. I think at best this
solution
is
racy
and
not
useful, at worst it leaves us with a payload table
that
doesn't
match what
we attempted to set in the atomic state - which at
worst
brings
us
into
undefined territory where we're just plain out of
sync
with
the
reality in
hw.
* Actually fun fact - mgr->payloads and mgr-

Quoted 1 line

proposed_vcpis

both
can
and
definitely should be removed entirely. All of the
info
for
mgr->payloads
could just be in the atomic state, because that + the
magic
of
atomic state
duplication means we'll also have an accurate view of
the
previous
state's
payload allocations: which renders mgr-

Quoted 7 lines
Quoted 7 lines
Quoted 1 line

proposed_vcpis

redundant.

Apologies for the long explanation again, but I hope that
explains
my

Really really thanks for your kindly help on this : ) I'll try my best to get to your WIP branch soon. Thank you Lyude!

Quoted 1,426 lines
Quoted 96 lines

point here a bit. I'm going to be trying to get to moving
amdgpu's
DSC
code out of amdgpu and into DRM helpers as well soon, so I'm
really
determined to clean stuff up beforehand as every time I've
done
so
it's become substantially easier to make changes to this code.
Things
used to be even worse before I started cleaning things up 2
or 3
years
ago, where simple changes would end up getting me stuck spending
hours
trying to dig through lockdep or memory manegement issues.
As
well,
I
would be entirely unsurprised if bugs like this very payload
leak
we're working on just disappear once we've gotten rid of all
the
extraneous workarounds and state tracking here - especially
with
how
many special cases we have for maintaining the payload table
right
now. That's certainly ended up being the case in the past
with a
number of other difficult to track down issues I've dealt
with
in
MST.

Anyhow. I've been way more productive this year then last
because
I
had over a month off work and am finally not super burnt out
from
my
job, and so far I've been making progress on the payload state
cleanup
far faster then I was last year :). I think if you can't figure
out
where the leak is coming from even once I get the debugging
patches
I
mentioned fixed up, it might be a good idea for us to try
again
after
I've got some of this code cleaned up. I've got a currently
WIP
branch
here:

https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgitl
ab.freedesktop.org%2Flyudess%2Flinux%2F- %2Fcommit%2F624dd68fa804e64b5b 2060e4735d7317090692b5&amp;data=04%7C01%7Cwayne.lin%40am d.co
m%7C
d4
bd
c7 59eb274bfccc8208d9daef41fe%7C3dd8961fe4884e608e11a82d994 e183
d%7C
0%
7C
0% 7C637781543416430632%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4 wLjA
wMDA
iL
CJ

Quoted 1,329 lines

QI joiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&amp;sda ta=n
FpVl
%2
BV
oF 40JPabfRMcR6Cz7ZHDwt%2BBpLDBNdXftJaA%3D&amp;reserved=0

Quoted 1,319 lines

void amdgpu_dm_initialize_dp_connector(struct
amdgpu_display_manager *dm, diff --git
a/drivers/gpu/drm/drm_dp_mst_topology.c
b/drivers/gpu/drm/drm_dp_mst_topology.c
index 857c5d15e81d..a70e26c5d084 100644
--- a/drivers/gpu/drm/drm_dp_mst_topology.c
+++ b/drivers/gpu/drm/drm_dp_mst_topology.c
@@ -2508,6 +2508,7 @@
drm_dp_mst_handle_conn_stat(struct
drm_dp_mst_branch *mstb,
u8 new_pdt;
bool new_mcs;
bool dowork = false, create_connector = false;
+ bool notify_unplug_event = false;

port = drm_dp_get_port(mstb, conn_stat-
port_number);
if (!port)
@@ -2520,6 +2521,9 @@
drm_dp_mst_handle_conn_stat(struct
drm_dp_mst_branch *mstb,
* port, so just throw the
port
out
and
make
sure we
* reprobe the link address of
it's
parent
MSTB
*/
+ /* should also consider
notify_unplug_event
here.
+ * but it's not a normal case
for
products
+in the
market
+ */

drm_dp_mst_topology_unlink_port(mgr,
port);
mstb->link_address_sent =
false;
dowork = true; @@ -2541,10
+2545,14 @@
drm_dp_mst_handle_conn_stat(struct
drm_dp_mst_branch
*mstb,
port->ddps = conn_stat-
displayport_device_plug_status;

if (old_ddps != port->ddps) {
- if (port->ddps && !port->input)
+ if (port->ddps && !port->input) {

drm_dp_send_enum_path_resources(mgr,
mstb,
port);
- else
+ } else {
port->full_pbn = 0;
+ if (port->connector &&
+
drm_dp_mst_is_end_device(port-
pdt,
+port-
mcs))
+ notify_unplug_event =
true;
+ }
}

new_pdt = port->input ? DP_PEER_DEVICE_NONE :
conn_stat-
peer_device_type;
@@ -2557,11 +2565,15 @@
drm_dp_mst_handle_conn_stat(struct
drm_dp_mst_branch
*mstb,
dowork = false;
}

+ if (notify_unplug_event &&
+mgr->cbs->notify_csn_disconnection)
+ mgr->cbs-
notify_csn_disconnection(port-
connector);
+
if (port->connector)
drm_modeset_unlock(&mgr->base.lock);
else if (create_connector)
drm_dp_mst_port_add_connector(mstb,
port);

+
out:
drm_dp_mst_topology_put_port(port);
if (dowork)
diff --git a/include/drm/drm_dp_mst_helper.h
b/include/drm/drm_dp_mst_helper.h index
78044ac5b59b..ff9e47729841
100644
--- a/include/drm/drm_dp_mst_helper.h
+++ b/include/drm/drm_dp_mst_helper.h
@@ -525,6 +525,7 @@ struct drm_dp_mst_topology_cbs {
* IRQ pulse handler.
*/
void (*poll_hpd_irq)(struct
drm_dp_mst_topology_mgr
*mgr);
+ void (*notify_csn_disconnection)(struct
drm_connector
+*connector);
};

#define DP_MAX_PAYLOAD (sizeof(unsigned long) * 8)
--
2.31.0

-----Original Message-----
From: Lin, Wayne
Sent: Wednesday, December 8, 2021 11:39 AM
To: 'Lyude Paul' <[email protected]>
Subject: RE: [PATCH] WIP: drm/dp_mst: Add support
for
dumping
topology ref histories from debugfs

No worries Lyude!
Thanks for keeping helping on this. Take your time :
)

-----Original Message-----
From: Lyude Paul <[email protected]>
Sent: Wednesday, December 8, 2021 7:05 AM
To: Lin, Wayne <[email protected]>
Subject: Re: [PATCH] WIP: drm/dp_mst: Add support
for
dumping
topology ref histories from debugfs

Sorry! I will try to get to this tomorrow, if not
then
sometime
this week.

On Tue, 2021-11-30 at 08:41 +0000, Lin, Wayne
wrote:
[Public]

Hi Lyude,

Finally have some bandwidth to get back to this
problem!
I roughly went through this patch and I'm just
aware
that
we
already have such kind of convenient tool for a
while.
I think it's definitely useful for us to track
port/mstb
reference count issues and I'll start to embrace
this
feature
for cleaning up those issues. Thank you Lyude!

However, I think the issue that I was trying to
fix
is
not
related to what you suggested:
" The idea here is that if stream resources
aren't
being
released, my guess would be that we're not
dropping
topology
references for the port which means the
connector
never
goes
away."
The issue I was trying to fix is about releasing
dc_link->remote_sinks while receiving a CSN
message
notifying
the connection status of a sst connector of a
port
changed
from connected to disconnected. Not the
connection
status
changed of a mst branch device.
e.g.
src - 1st_mstb - 2nd_mstb - sst_monitor => src -
1st_mstb
(disconnect) 2nd_mstb - sst_monitor

In above case, after receiving CSN, we will put
topology
references of 2nd mstb and its port which is
connected
with
the sst monitor. As the result of that, we can
call
drm_dp_delayed_destroy_port() to unregister and
put
the
drm
connector.

However, in below case:
e.g.
src - 1st_mstb - sst_monitor => src - 1st_mstb
(disconnect)
sst_monitor

In this case, which is the case having problem,
it
definitely
won't decrease the topology references count of
the
port
which
was connected to the sst monitor to zero since
the
port
is
still existing in the topology. Same as the
malloc
reference
since the port can't get destroyed. Hence, the
port
still
exists and we won't call
drm_dp_delayed_destroy_port() to unregister and
put
the
drm
connector.
I looked up the code and
drm_dp_delayed_destroy_port()
seems
like the only place to call drm_connector_put()
which
means
we
can't put reference count of drm connector under
this
case
and
can't release dc_sink resource by destroying drm
connector.

I would also like to point out that this
resource
(remote_sinks) is specific to different stream
sinks. So
if
we're trying to release this dc_sink resource by
destroying
the drm connector, it conflicts the idea that
you
suggested
before that we should always keep the drm
connector
until
it's
no longer reachable in the topology.
Releasing dc_sink should be binding with the
disconnection
event.

I understand your concern that we should not
just
easily
change the logic here since it's the result
after
solving
tons
of bugs before and might cause other side
effect.
So,
just
my
2 cents, what I'm thinking is to register a
callback
function
for our driver to notify us that the remote sink
is
detached.
This just aligns our flow handling long HPD
event of
legacy
(sst) DP.
For sst case, once we detect long HPD event
indicating
the
monitor is detached, we will immediately try to
release
the
dc_link->local_sink and fire hotplug event to
upper
layer.
Same as here, once receives a CSN message
notifying
a
drm
connector is changed from connected to
disconnected,
trigger
the callback function and we can try to release
the
dc_sink
resource.

Would like to know your thought and insight
please :
)

Btw, I got some errors and warnings while
building
and
have
some code adjustments as below : ) Thank you
Lyude
for
your
always kindly help!!

Regards,
Wayne
-----Original Message-----
From: Lyude Paul <[email protected]>
Sent: Wednesday, November 3, 2021 7:15 AM
To: Lin, Wayne <[email protected]>
Subject: [PATCH] WIP: drm/dp_mst: Add support
for
dumping
topology ref histories from debugfs

TODO:
* Implement support for i915
* Finish writing this commit message
* ???
* profit

Signed-off-by: Lyude Paul <[email protected]>
---

Hey wayne! SO-hopefully if I did this
correctly
then
this
should just work on amdgpu. What this patch
should
do
is
add
a debugfs file to amdgpu called
"amdgpu_dp_mst_topology_refs", and when you
read
the
file
it
should print out the topology reference
history of
every
MSTB and Port in memory, along with how many
times
we've
hit
the codepath in each backtrace. An example:

Port DP-5 (0000000005c37748) topology ref
history:
1 gets (last at 58.468973):
drm_dp_send_link_address+0x6a5/0xa00
[drm_kms_helper]

drm_dp_check_and_send_link_address+0xad/0xd0
[drm_kms_helper]
drm_dp_mst_link_probe_work+0x14e/0x1a0
[drm_kms_helper]
process_one_work+0x1e3/0x390
worker_thread+0x50/0x3a0
kthread+0x124/0x150
ret_from_fork+0x1f/0x30
1 puts (last at 58.469357):
drm_dp_mst_topology_put_port+0x6a/0x210
[drm_kms_helper]
drm_dp_send_link_address+0x39e/0xa00
[drm_kms_helper]

drm_dp_check_and_send_link_address+0xad/0xd0
[drm_kms_helper]
drm_dp_mst_link_probe_work+0x14e/0x1a0
[drm_kms_helper]
process_one_work+0x1e3/0x390
worker_thread+0x50/0x3a0
kthread+0x124/0x150
ret_from_fork+0x1f/0x30

The idea here is that if stream resources
aren't
being
released, my guess would be that we're not
dropping
topology
references for the port which means the
connector
never
goes
away. So, if that's really the case then once
we
unplug
the
offending connector we should be able to find
a
pair
of
gets/puts for the offending leaked connector
where
the
get/put count doesn't match up. Also, if the
frame
count
on
the backtrace isn't long enough you can
increase
the
value
of STACK_DEPTH in
drivers/gpu/drm/drm_dp_mst_topology.c
and
recompile to get more frames.

To enable this, first enable CONFIG_EXPERT for
your
kernel
which will unhide
CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS.
Then just enable
CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS,
recompile, and it should be good to go.

Let me know if this works for you, and
hopefully
this
should
tell us exactly what the problem actually is
here.

.../amd/display/amdgpu_dm/amdgpu_dm_debugfs.c
|
35
++++
drivers/gpu/drm/drm_dp_mst_topology.c
|
173
++++++++++++++----
drivers/gpu/drm/nouveau/nouveau_debugfs.c
|
35
++++
include/drm/drm_dp_mst_helper.h
|
18
++
4 files changed, 228 insertions(+), 33
deletions(-)

diff --git
a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu
_dm_
debu
gf
s.
c
b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu
_dm_
debu
gf
s.
c
index 1a68a674913c..1a14732c52b4 100644
---
a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu
_dm_
debu
gf
s.
c
+++
b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu
_dm_
debu
gf
+++ s.c
@@ -3063,6 +3063,37 @@ static int
mst_topo_show(struct
seq_file *m, void
*unused)
return 0;
}

+#ifdef CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS
+static int mst_topology_ref_dump_show(struct
seq_file
*m,
+void
+*unused) {
+ struct amdgpu_device *adev = (struct
amdgpu_device
+*)m->private;
+ struct drm_device *dev =
adev_to_drm(adev);
+ struct drm_connector *connector;
+ struct drm_connector_list_iter
conn_iter;
+ struct amdgpu_dm_connector *aconnector;
+
+ drm_connector_list_iter_begin(dev,
&conn_iter);
+ drm_for_each_connector_iter(connector,
&conn_iter)
{
+ if (connector->connector_type !=
DRM_MODE_CONNECTOR_DisplayPort)
+ continue;
+
+ aconnector =
+to_amdgpu_dm_connector(connector);
+
+ /* Ensure we're only dumping the
topology
of
a
+root mst node
*/
+ if (!aconnector-
mst_mgr.max_payloads)
+ continue;
+
+ seq_printf(m, "\nMST topology
for
connector
+%d\n",
aconnector->connector_id);
+ drm_dp_mst_dump_topology_refs(m,
+&aconnector->mst_mgr);
+ }
+ drm_connector_list_iter_end(&conn_iter);
+
+ return 0;
+}
+
+DEFINE_SHOW_ATTRIBUTE(mst_topology_ref_dump);
+#endif
+
/*
* Sets trigger hpd for MST topologies.
* All connected connectors will be
rediscovered
and
re
started as needed if val of 1 is sent.
@@ -3299,6 +3330,10 @@ void
dtn_debugfs_init(struct
amdgpu_device
*adev)

debugfs_create_file("amdgpu_mst_topology",
0444,
root,
adev,
&mst_topo_fops);
+#ifdef CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS
+
debugfs_create_file("amdgpu_dp_mst_topology_re
fs",
+0444, root, adev,
+
&mst_topology_ref_dump_fops);
+#endif
debugfs_create_file("amdgpu_dm_dtn_log",
0644,
root,
adev,
&dtn_log_fops);

diff --git
a/drivers/gpu/drm/drm_dp_mst_topology.c
b/drivers/gpu/drm/drm_dp_mst_topology.c
index 1aa8702383d4..0159828c494d 100644
--- a/drivers/gpu/drm/drm_dp_mst_topology.c
+++ b/drivers/gpu/drm/drm_dp_mst_topology.c
@@ -1366,23 +1366,6 @@ static int
drm_dp_mst_wait_tx_reply(struct
drm_dp_mst_branch
*mstb,
return ret;
}

-static struct drm_dp_mst_branch
*drm_dp_add_mst_branch_device(u8 lct, u8
*rad) -{
- struct drm_dp_mst_branch *mstb;
-
- mstb = kzalloc(sizeof(*mstb),
GFP_KERNEL);
- if (!mstb)
- return NULL;
-
- mstb->lct = lct;
- if (lct > 1)
- memcpy(mstb->rad, rad, lct / 2);
- INIT_LIST_HEAD(&mstb->ports);
- kref_init(&mstb->topology_kref);
- kref_init(&mstb->malloc_kref);
- return mstb;
-}
-
static void
drm_dp_free_mst_branch_device(struct
kref
*kref) {
struct drm_dp_mst_branch *mstb = @@ -
1642,12
+1625,20
@@ topology_ref_type_to_str(enum
drm_dp_mst_topology_ref_type type)
return "put";
}

+static const char
*topology_ref_history_type_to_str(enum
+drm_dp_mst_topology_history_type type) {
+ if (type ==
DRM_DP_MST_TOPOLOGY_HISTORY_PORT)
+ return "Port";
+ else
+ return "MSTB"; }
+
static void
-__dump_topology_ref_history(struct
drm_dp_mst_topology_ref_history *history,
- void *ptr, const
char
*type_str)
+dump_topology_ref_history(struct
+drm_dp_mst_topology_ref_history *history,
struct
+drm_printer p)
{
- struct drm_printer p =
drm_debug_printer(DBG_PREFIX);
+ char *connector_name = NULL;
char *buf = kzalloc(PAGE_SIZE,
GFP_KERNEL);
+ void *ptr;
int i;

if (!buf)
@@ -1656,14 +1647,29 @@
__dump_topology_ref_history(struct
drm_dp_mst_topology_ref_history *history,
if (!history->len)
goto out;

+ /* Get a pointer to the actual MSTB/port
so
we
can
the
+memory
address to the kernel log */
+ if (history->type ==
DRM_DP_MST_TOPOLOGY_HISTORY_PORT)
+ ptr = container_of(history,
struct
+drm_dp_mst_port,
topology_ref_history);
+ else
+ ptr = container_of(history,
struct
+drm_dp_mst_branch, topology_ref_history);
+
/* First, sort the list so that it goes
from
oldest
to
newest
* reference entry
*/
sort(history->entries, history->len,
sizeof(*history->entries),
topology_ref_history_cmp, NULL);

- drm_printf(&p, "%s (%p) topology count
reached
0,
dumping history:\n",
- type_str, ptr);
+ if (history->type ==
DRM_DP_MST_TOPOLOGY_HISTORY_PORT)
+{
+ struct drm_dp_mst_port *port =
ptr;
+
+ if (port->connector)
+ connector_name =
+port->connector->name;
+ }
+ if (connector_name)
+ drm_printf(&p, "Port %s (%p)
topology
ref
+history:\n",
connector_name, ptr);
+ else
+ drm_printf(&p, "%s (%p) topology
ref
+history:\n",
+
+topology_ref_history_type_to_str(history-
type),
ptr);

for (i = 0; i < history->len; i++) {
const struct
drm_dp_mst_topology_ref_entry
*entry = @@
-
1682,24 +1688,92 @@
__dump_topology_ref_history(struct
drm_dp_mst_topology_ref_history *history,
ts_nsec, rem_nsec /
1000,
buf);
}

- /* Now free the history, since this is
the
only
time
we expose it */
- kfree(history->entries);
out:
kfree(buf);
}

+/**
+ * drm_dp_mst_dump_topology_refs - helper
function
for
+dumping the topology ref history
+ * @m: File to print to
+ * @mgr: &struct drm_dp_mst_topology_mgr to
use
+ *
+ * Prints the topology ref history of all
ports
and
MSTBs
+on @mgr that are still in memory,
+ * regardless of whether they're actually
still
reachable
+through the topology or not. Only enabled
+ * when
%CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS is
enabled.
+Can be implemented by drivers to assist
+ * with debugging leaks in the DP MST
helpers.
+ */
+void drm_dp_mst_dump_topology_refs(struct
seq_file
*m,
+struct drm_dp_mst_topology_mgr *mgr) {
+ struct drm_dp_mst_topology_ref_history
*history;
+ struct drm_printer p =
drm_seq_file_printer(m);
+
+ mutex_lock(&mgr-
topology_ref_history_lock);
+ list_for_each_entry(history,
+&mgr->topology_ref_history_list,
+node)
+
dump_topology_ref_history(history,
p);
+ mutex_unlock(&mgr-
topology_ref_history_lock);
+}
+EXPORT_SYMBOL(drm_dp_mst_dump_topology_refs);
+
+static void
+__init_topology_ref_history(struct
+drm_dp_mst_topology_ref_history
*history,
+ struct
drm_dp_mst_topology_mgr
+*mgr,
+ enum
+drm_dp_mst_topology_history_type
+type) {
+ history->type = type;
+ INIT_LIST_HEAD(&history->node);
+
+ mutex_lock(&mgr-
topology_ref_history_lock);
+ list_add(&history->node,
+&mgr->topology_ref_history_list);
+ mutex_unlock(&mgr-
topology_ref_history_lock);
+}
+
+static void
+__destroy_topology_ref_history(struct
+drm_dp_mst_topology_ref_history
*history,
+ struct
drm_dp_mst_topology_mgr
+*mgr) {
+ mutex_lock(&mgr-
topology_ref_history_lock);
+ list_del(&mgr-
topology_ref_history_list);
+ mutex_unlock(&mgr-
topology_ref_history_lock);
+
+ kfree(history->entries); }
+
+static __always_inline void
+init_port_topology_history(struct
drm_dp_mst_topology_mgr
+*mgr, struct drm_dp_mst_port *port) {
+
+__init_topology_ref_history(&port-
topology_ref_history,
+mgr,
+
+DRM_DP_MST_TOPOLOGY_HISTORY_PORT);
+}
+
+static __always_inline void
+init_mstb_topology_history(struct
drm_dp_mst_topology_mgr
+*mgr, struct drm_dp_mst_branch *mstb) {
+
+__init_topology_ref_history(&mstb-
topology_ref_history,
+mgr,
+
+DRM_DP_MST_TOPOLOGY_HISTORY_MSTB);
+}
+
+static __always_inline void
+destroy_port_topology_history(struct
drm_dp_mst_port
*port)
+{
+
+__destroy_topology_ref_history(&port-
topology_ref_history,
+port->mgr); }
+
+static __always_inline void
+destroy_mstb_topology_history(struct
drm_dp_mst_branch
+*mstb) {
+
+__destroy_topology_ref_history(&mstb-
topology_ref_history,
+mstb->mgr); }
+
static __always_inline void
dump_mstb_topology_history(struct
drm_dp_mst_branch
*mstb)
{
-
__dump_topology_ref_history(&mstb-
topology_ref_history,
mstb,
- "MSTB");
+ dump_topology_ref_history(&mstb-
topology_ref_history,
+drm_debug_printer(DBG_PREFIX));
}

static __always_inline void
dump_port_topology_history(struct
drm_dp_mst_port
*port)
{
-
__dump_topology_ref_history(&port-
topology_ref_history,
port,
- "Port");
+ dump_topology_ref_history(&port-
topology_ref_history,
+drm_debug_printer(DBG_PREFIX));
}

static __always_inline void @@ -1729,6
+1803,14
@@
topology_ref_history_unlock(struct
drm_dp_mst_topology_mgr *mgr) } #else
static
inline
void
+init_port_topology_history(struct
drm_dp_mst_topology_mgr
+*mgr, struct drm_dp_mst_port *port); static
inline
void
Should also add the bracket, otherwise will get
warnings.
=> static inline void
init_port_topology_history(struct
drm_dp_mst_topology_mgr *mgr, struct
drm_dp_mst_port
*port)
{};

+init_mstb_topology_history(struct
drm_dp_mst_topology_mgr
+*mgr, struct drm_dp_mst_branch *mstb); static
inline
void
Same as above
=> static inline void
init_mstb_topology_history(struct
drm_dp_mst_topology_mgr *mgr, struct
drm_dp_mst_branch
*mstb)
{};

+destroy_port_topology_history(struct
+drm_dp_mst_topology_mgr *mgr, struct
drm_dp_mst_port
+*port); static inline void
destroy_port_topology_history() takes one
parameter
only
which
is port.
=> destroy_port_topology_history(struct
drm_dp_mst_port
*port)
{};

+destroy_mstb_topology_history(struct
+drm_dp_mst_topology_mgr *mgr, struct
drm_dp_mst_branch
+*mstb); static inline void
destroy_mstb_topology_history() takes one
parameter
only
which
is mstb => destroy_mstb_topology_history(struct
drm_dp_mst_branch
*mstb) {};

topology_ref_history_lock(struct
drm_dp_mst_topology_mgr
*mgr) {} static inline void
topology_ref_history_unlock(struct
drm_dp_mst_topology_mgr *mgr) {} @@ -1740,6
+1822,25
@@
dump_port_topology_history(struct
drm_dp_mst_port
*port)
{}
#define save_port_topology_ref(port, type)
#endif

+static struct drm_dp_mst_branch *
+drm_dp_add_mst_branch_device(struct
drm_dp_mst_topology_mgr
+*mgr,
+u8 lct, u8 *rad) {
+ struct drm_dp_mst_branch *mstb;
+
+ mstb = kzalloc(sizeof(*mstb),
GFP_KERNEL);
+ if (!mstb)
+ return NULL;
+
+ mstb->lct = lct;
+ if (lct > 1)
+ memcpy(mstb->rad, rad, lct / 2);
+ INIT_LIST_HEAD(&mstb->ports);
+ kref_init(&mstb->topology_kref);
+ kref_init(&mstb->malloc_kref);
+ init_mstb_topology_history(mgr, mstb);
+ return mstb;
+}
+
static void
drm_dp_destroy_mst_branch_device(struct
kref
*kref) {
struct drm_dp_mst_branch *mstb = @@ -
1747,6
+1848,7
@@
static void
drm_dp_destroy_mst_branch_device(struct
kref *kref)
struct drm_dp_mst_topology_mgr *mgr =
mstb-
mgr;

dump_mstb_topology_history(mstb);
+ destroy_mstb_topology_history(mstb);

INIT_LIST_HEAD(&mstb->destroy_next);

@@ -1856,6 +1958,7 @@ static void
drm_dp_destroy_port(struct
kref
*kref)
struct drm_dp_mst_topology_mgr *mgr =
port-
mgr;

dump_port_topology_history(port);
+ destroy_port_topology_history(port);

/* There's nothing that needs locking to
destroy
an
input port yet */
if (port->input) {
@@ -2135,7 +2238,7 @@
drm_dp_port_set_pdt(struct
drm_dp_mst_port *port, u8 new_pdt,
ret =
drm_dp_mst_register_i2c_bus(port);
} else {
lct =
drm_dp_calculate_rad(port,
rad);
- mstb =
drm_dp_add_mst_branch_device(lct, rad);
+ mstb =
+drm_dp_add_mst_branch_device(mgr, lct, rad);
if (!mstb) {
ret = -ENOMEM;
drm_err(mgr-
dev,
"Failed
to
create MSTB for port %p", port); @@ -2366,6
+2469,8 @@
drm_dp_mst_add_port(struct drm_device *dev,
*/
drm_dp_mst_get_mstb_malloc(mstb);

+ init_port_topology_history(mgr, port);
+
return port;
}

@@ -3745,7 +3850,7 @@ int
drm_dp_mst_topology_mgr_set_mst(struct
drm_dp_mst_topology_mgr *mgr, bool ms
}

/* add initial branch device at
LCT
1 */
- mstb =
drm_dp_add_mst_branch_device(1,
NULL);
+ mstb =
drm_dp_add_mst_branch_device(mgr,
1,
+NULL);
if (mstb == NULL) {
ret = -ENOMEM;
goto out_unlock; @@ -
5512,14
+5617,16
@@ int drm_dp_mst_topology_mgr_init(struct
drm_dp_mst_topology_mgr *mgr,
mutex_init(&mgr->delayed_destroy_lock);
mutex_init(&mgr->up_req_lock);
mutex_init(&mgr->probe_lock); -#if
IS_ENABLED(CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_RE
FS)
- mutex_init(&mgr-
topology_ref_history_lock);
-#endif
INIT_LIST_HEAD(&mgr->tx_msg_downq);
INIT_LIST_HEAD(&mgr->destroy_port_list);
INIT_LIST_HEAD(&mgr-
destroy_branch_device_list);
INIT_LIST_HEAD(&mgr->up_req_list);

+#if
IS_ENABLED(CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_RE
FS)
+ mutex_init(&mgr-
topology_ref_history_lock);
+ INIT_LIST_HEAD(&mgr-
topology_ref_history_list);
+#endif
+
/*
* delayed_destroy_work will be queued
on a
dedicated
WQ, so that any
* requeuing will be also flushed when
deiniting
the
topology manager.
diff --git
a/drivers/gpu/drm/nouveau/nouveau_debugfs.c
b/drivers/gpu/drm/nouveau/nouveau_debugfs.c
index 1cbe01048b93..53ec7c1daada 100644
---
a/drivers/gpu/drm/nouveau/nouveau_debugfs.c
+++
b/drivers/gpu/drm/nouveau/nouveau_debugfs.c
@@ -29,9 +29,13 @@
*/

#include <linux/debugfs.h>
+#include <drm/drm_dp_mst_helper.h> #include
+<drm/drm_encoder.h>
#include <nvif/class.h>
#include <nvif/if0001.h>
+#include <nouveau_encoder.h>
#include "nouveau_debugfs.h"
+#include "nouveau_display.h"
#include "nouveau_drv.h"

static int
@@ -68,6 +72,34 @@
nouveau_debugfs_strap_peek(struct
seq_file *m, void
*data)
return 0;
}

+#ifdef CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS
+static int
nouveau_debugfs_mst_topology_history(struct
+seq_file *m, void *data) {
+ struct drm_info_node *node = m->private;
+ struct drm_device *dev = node->minor-
dev;
+ struct drm_encoder *encoder;
+ struct nouveau_display *disp =
nouveau_display(dev);
+
+ if (disp->disp.object.oclass <
GF110_DISP)
+ return -EINVAL;
+
+ drm_for_each_encoder(encoder, dev) {
+ struct nv50_mstm *mstm;
+
+ /* We need the real encoder for
each
MST
mgr
+*/
+ if (encoder->encoder_type !=
+DRM_MODE_ENCODER_TMDS)
+ continue;
+ mstm = nouveau_encoder(encoder)-
dp.mstm;
+ if (!mstm)
+ continue;
+
+ seq_printf(m, "MSTM on %s:\n",
encoder-
name);
+ drm_dp_mst_dump_topology_refs(m,
&mstm-
mgr);
+ }
+ return 0;
+}
+#endif
+
static int
nouveau_debugfs_pstate_get(struct seq_file
*m,
void
*data)
{ @@
-213,6
+245,9 @@ static const struct file_operations
nouveau_pstate_fops = { static struct
drm_info_list
nouveau_debugfs_list[] = {
{ "vbios.rom",
nouveau_debugfs_vbios_image,
0,
NULL
},
{ "strap_peek",
nouveau_debugfs_strap_peek,
0,
NULL
},
+#ifdef CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS
+ { "dp_mst_topology_refs",
+nouveau_debugfs_mst_topology_history, 0, NULL
},
#endif
};
#define NOUVEAU_DEBUGFS_ENTRIES
ARRAY_SIZE(nouveau_debugfs_list)

diff --git a/include/drm/drm_dp_mst_helper.h
b/include/drm/drm_dp_mst_helper.h index
78044ac5b59b..e3a73d8d97c0
100644
--- a/include/drm/drm_dp_mst_helper.h
+++ b/include/drm/drm_dp_mst_helper.h
@@ -35,6 +35,11 @@ enum
drm_dp_mst_topology_ref_type {
DRM_DP_MST_TOPOLOGY_REF_PUT,
};

+enum drm_dp_mst_topology_history_type {
+ DRM_DP_MST_TOPOLOGY_HISTORY_PORT,
+ DRM_DP_MST_TOPOLOGY_HISTORY_MSTB, };
+
struct drm_dp_mst_topology_ref_history {
struct drm_dp_mst_topology_ref_entry {
enum
drm_dp_mst_topology_ref_type
type;
@@
-43,6
+48,9 @@ struct
drm_dp_mst_topology_ref_history {
depot_stack_handle_t backtrace;
} *entries;
int len;
+
+ enum drm_dp_mst_topology_history_type
type;
+ struct list_head node;
};
#endif /*
IS_ENABLED(CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_RE
FS)
*/

@@ -769,6 +777,12 @@ struct
drm_dp_mst_topology_mgr {
*
&drm_dp_mst_branch.topology_ref_history.
*/
struct mutex topology_ref_history_lock;
+
+ /**
+ * @topology_ref_history_list: contains
a
list
of
+topology ref
histories for any MSTBs or
+ * ports that are still in memory
+ */
+ struct list_head
topology_ref_history_list;
#endif
};

@@ -873,6 +887,10 @@ void
drm_dp_mst_put_port_malloc(struct
drm_dp_mst_port *port);

struct drm_dp_aux
*drm_dp_mst_dsc_aux_for_port(struct
drm_dp_mst_port *port);

+#ifdef CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS
+void drm_dp_mst_dump_topology_refs(struct
seq_file
*m,
+struct drm_dp_mst_topology_mgr *mgr); #endif
+
extern const struct drm_private_state_funcs
drm_dp_mst_topology_state_funcs;

/**
--
2.31.1

--
Cheers,
Lyude Paul (she/her)
Software Engineer at Red Hat

Quoted 2 lines

--
Cheers, Lyude Paul (she/her) Software Engineer at Red Hat

--
Regards,
Wayne Lin

Quoted 7 lines
Quoted 6 lines
Quoted 5 lines
Quoted 4 lines

--
Cheers,
Lyude Paul (she/her)
Software Engineer at Red Hat

--
Cheers,
Lyude Paul (she/her)
Software Engineer at Red Hat

Quoted 9 lines
Quoted 4 lines

--
Cheers,
Lyude Paul (she/her)
Software Engineer at Red Hat

--
Regards,
Wayne Lin

--
Cheers,
Lyude Paul (she/her)
Software Engineer at Red Hat

#20 lore raw

[Public]

Hi Lyude,

Just a heads-up that I should be able to push my patch set out for code review next week. Thanks for your kind help all the time!

Regards,
Wayne

Quoted 2,605 lines

-----Original Message-----
From: Lin, Wayne
Sent: Monday, April 25, 2022 1:02 PM
To: Lyude Paul <[email protected]>
Cc: [email protected]; [email protected]
Subject: RE: [PATCH] WIP: drm/dp_mst: Add support for dumping topology ref histories from debugfs

Thank you Lyude!

I just have a tentative patch set to deal with this. Would like to give a test on some platforms first.
If the result is positive, will send out for review immediately.

Thanks for tracking on this : )

Regards,
Wayne Lin

Quoted 6 lines

-----Original Message-----
From: Lyude Paul <[email protected]>
Sent: Thursday, April 21, 2022 7:01 AM
To: Lin, Wayne <[email protected]>
Cc: [email protected]; [email protected]
Subject: Re: [PATCH] WIP: drm/dp_mst: Add support for dumping topology

ref histories from debugfs

Quoted 2 lines

Hey! Figured I'd check if there's been any status updates here since it's

been

Quoted 12 lines

a while, just to make sure I haven't dropped this issue from my radar. No problem if you're busy :)

Quoted 8 lines · Wayne

[Public]

Quoted 6 lines

-----Original Message-----
From: Lyude Paul <[email protected]>
Sent: Wednesday, March 16, 2022 8:48 AM
To: Lin, Wayne <[email protected]>
Cc: [email protected]; [email protected]
Subject: Re: [PATCH] WIP: drm/dp_mst: Add support for dumping

topology ref

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines

histories from debugfs

(Adding this back to the dri-devel mailing list since I didn't notice it got
dropped from there)

Hm, some comments on this issue down below. Sorry for the delayed response, I
was going to try this right after I finished the MST legacy removal but that's
ending up taking longer than I hoped.

On Tue, 2022-03-08 at 01:46 +0000, Lin, Wayne wrote:

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

[AMD Official Use Only]

Oops...
I didn't notice that I replied to wrong mail previously : P Sorry for the confusion and reply to the correct thread.

Good day!
I probably know why you can't reproduce the issue there. Please

refer to

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

the
attached
patch file which makes me easier to explain this.

In dm_dp_mst_get_modes(), we will create a new dc_sink by calling dc_link_add_remote_sink() and add that dc_sink into the array

dc_link-

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 1 line

remote_sinks[].

However, if we find that aconnector->dc_sink is not null, then we

won't

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

create a new
dc_sink for that aconnector and won't add the sink to the array

dc_link-

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 1 line

remote_sinks[].

When the issue that I mentioned before occurs, we won't be able to release
the dc_sink
for the aconnector when we unplug the sst monitor. Hence, we can't create
new dc_sink
for the latter on connected new stream sink of that port. Which leads

to

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

two
issues
1. Unplug monitor and plug in another monitor "to the same port" =>  Since we use the same dc_sink to reflect the capability of the new connected stream
sink, we might send out inappropriate stream to the new connected

sink.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

2. Because we can't release dc_sink in the array dc_link-

Quoted 1 line

remote_sinks[],

when we
plug/unplug sst monitor to more than 4 different mst port, we will

break

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 4 lines

the
criteria
"dc_link->sink_count >= MAX_SINKS_PER_LINK" in
link_add_remote_sink_helper().

Ok, a lot of this seems to be happening in amdgpu which certainly

explains

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

why
I had so much trouble following along with this originally (also, having learned a bit more about how DC works definitely has helped a bit). I already
see a bunch of issues though with how this code is structured that

would

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

definitely break things, though note I haven't sat down and tried this on a
real machine yet - will try tomorrow.

So - it seems like dc_sink == a bunch of hotplugging state for a dm connector,
which actually tells me for one that this is definitely the wrong spot for this code. get_modes() really should just handle filling out DRM modes

and

Quoted 1 line
Quoted 1 line
Quoted 1 line

pretty much nothing else, because callers from DRM aren't going to

expect

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

side-effects like this when get_modes() is called - which could potentially
lead to all sorts of weirdness down the line if the DRM call paths that use
this ever change. i915 and nouveau have good examples of what these functions
should typically look like, and amdgpu actually seems to mostly follow this
advice for it's other get_modes() callback.

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

Note there's also another problem here: the assumption "no EDID == disconnected" isn't correct. It's totally possible to run into EDID-less displays if the display is some ancient pre-historic relic, or if the ROM (or
EEPROM? can't remember what type of chip computer displays tend to

use...)

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

chip
in the monitor containing the EDID has died. Note that the second situation is
suprisingly more common then one might think! I ran into a 140Hz 4k

ASUS

Quoted 1 line
Quoted 1 line
Quoted 1 line

display where this happened, and I know another friend of mine who

had the

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

ROM
in their display die randomly as well.

So with this being said: I, think the solution here is actually just to add/remove dc_sink from dm_dp_mst_detect() based on the CSN, and

drop the

Quoted 10 lines
Quoted 10 lines
Quoted 6 lines

calls from get_modes()? detect() should always be called before the connector
has been removed, and we could use the return status from drm_dp_mst_detect_port() in order to tell whether or not we should add/remove
the dc_sink.

Thank you Lyude for guiding me through! After your elaboration, I think I roughly
know the idea from drm perspective now. Let me get through this and

will get

Quoted 23 lines
Quoted 23 lines

back
to you.

Quoted 10 lines

If you don't get to it first tomorrow, I'll see if I can reproduce this locally and try writing up a patch.

Also a final note: I don't think it's a good idea in general for there to be a
fixed size array of dc_sinks like this. Those probably should just be stored
somehow on a per-connector basis. I say this because in addition to the fact

Understand and agree with that. I plan to remove the constraint as well. Thanks!

Quoted 6 lines

MST connectors aren't destroyed immediately (which could lead to a situation
where we have more then 6 connected MST ports briefly before their destructors
run, e.g. during suspend/resume reprobing).

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

Also, one question below:

Quoted 5 lines

Anyway, these are my steps to reproduce the issue.
I use a startech mst 1to4 mst hub, a sst monitor and a mst monitor.

1. boot up with sst monitor connected to the hub and plug the hub to

the

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

system.
2. unplug/plug sst monitor to all left three different DP output ports. Which is used to
Pollute all four aconnectors to have them keep the stale dc_sinks.
3. plug in the mst monitor to arbitrary ports. Since the new connected mst
monitor will
have us add new mst port to the topology and attach a new

aconnector for

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

that mst port,
we'll try to create a new dc_sink to that new aconnector and break

the

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

sink_count
limitation. As you can see in the log the error message : "*ERROR* Unable to
add a
remote sink"

(Note that different hub might influence the result. Some hubs will

pull

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

down the HPD
when there is no monitor connected to its downstream ports which

will

Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
Quoted 3 lines

run
into different
code flow)

by "pull down" I assume you mean doing a long HPD instead of short (or vice
versa?).

Quoted 6 lines
Quoted 6 lines

Yes, long HPD!

Thanks for your help Lyude!

Quoted 3 lines
Quoted 3 lines

I also attach the dmesg log under different debug level (0x104 &

0x116)

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

for
your reference.

Please let me know if you can reproduce then : )

Sorry about that Lyude and hope this time I send to the right thread.. Really appreciate for your help!!!

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

Regards,
Wayne

Quoted 5 lines

-----Original Message-----
From: Lyude Paul <[email protected]>
Sent: Wednesday, March 2, 2022 9:10 AM
To: Lin, Wayne <[email protected]>
Subject: Re: [PATCH] WIP: drm/dp_mst: Add support for dumping

topology

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

ref
histories from debugfs

Ran into some issues with my system yesterday (hooray being the

first

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

to
find
a rather annoying zsh bug that broke my scripts!), but I've finally got
things
setup. I'm not able to reproduce this issue though (I've tried hotplugging
the
way you mentioned here, but nothing seems to leak), which makes

me

Quoted 31 lines
Quoted 31 lines
Quoted 31 lines
Quoted 31 lines
Quoted 31 lines

think
there's something special in your setup we might not have noticed.

Do you think you could get some drm logs with drm.debug=0x116 log_buf_len=50M
from when this happens so I can get a better idea of the drm events leading up
to this? Also, if you've been testing this in a GUI could you confirm whether
this happens when you're just in a plain old VT?

Quoted 19 lines · Lyude Paul

Alright - starting to look into this now and setting up a system to try
reproducing this, will let you know as soon as I've got results. Sorry
again
about all of the delays

Quoted 11 lines · Lyude Paul

I didn't get a chance yet, but I've gotten through most of my backlog
now.
Sorry about the delay - I've been catching up since I had to take two
weeks
off work very unexpectedly. I'll put aside time to look at this first
thing
monday.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines · Wayne
Quoted 6 lines

[AMD Official Use Only]

Hi Lyude,

Good day!
Sorry to bother you, but would like to know if you can

reproduce

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

the
issue?

Thanks!

Regards,
Wayne

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

-----Original Message-----
From: Lyude Paul <[email protected]>
Sent: Thursday, February 3, 2022 4:12 AM
To: Lin, Wayne <[email protected]>
Cc: [email protected]
Subject: Re: [PATCH] WIP: drm/dp_mst: Add support for

dumping

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

topology
ref
histories from debugfs

JFYI I should be able to respond to this soon, I think I'm following
your
issue a bit more (haven't gotten to go through the patches

too

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

so
I'm
probably
going to see if I can reproduce it locally as well. Sorry for the
delay
-
dealing with some X.org stuff and work issues at the

moment

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines · Wayne
Quoted 5 lines

[Public]

Hey Lyude,

Sorry, just got chance to get back to this and BIG THANKS

to

Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines

elaborating
in
such details!
Following are some of my comments : )

Quoted 15 lines

-----Original Message-----
From: Lyude Paul <[email protected]>
Sent: Wednesday, January 19, 2022 9:59 AM
To: Lin, Wayne <[email protected]>
Cc: [email protected]
Subject: Re: [PATCH] WIP: drm/dp_mst: Add support for dumping
topology
ref
histories from debugfs

Hey - so, the original issue here was that payloads were not
always
deleted when we expected them to be - correct?

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

Actually, what I was trying to do is not relevant to payload maintenance.
What I wanted to do is having a way to notify amdgpu to release its enumerated resource "dc_sink"

when

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

unplug
event
occurs. Currently, under specific scenarios, we can't release remote "dc_sink" and ends up can't

allocate

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

resource
for
new connected monitors. I'll explain the scenario underneath.

Quoted 9 lines
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

So, get back to the main issue that I was trying to solve when I
pushed
the
patchset:

https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Fpatc
hwork.kernel.org%2Fproject%2Fdri&amp;data=05%7C01

Quoted 1 line

%7Cwayne.lin%40amd.com%7C5f734314a44c4f8f82b908da2321b4c2%7C3dd8 961fe4884e608e11a82d994e183d%7C0%7C0%7C63786092

Quoted 1 line

4939974636%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIj oiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000

Quoted 1 line

%7C%7C%7C&amp;sdata=VtrMzZ2Evr%2BRCUCHIAUPUxqsRuuH9CW3jrNczu bY%2BFc%3D&amp;reserved=0

Quoted 1 line
Quoted 1 line
Quoted 1 line

ayne.lin%40amd.com%7Cd77e829cfd3a466e25e508da06e6a9dd%7C3dd8961f e4884e608e

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

11a82d994e183d%7C0%7C0%7C637829885024

474689%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2 luMzIiLCJBTiI

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&amp;sda

ta=MvKilTT1CkBqZVK1N24JyNPjiPYORV%2FsCwvEzwx%2FIAk%3D&amp;rese rved=0

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

ayne.lin%40amd.com%7Cbbc27043edc04bf8067c08d9fbe9620c%7C3dd8961fe 4884e

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

608e 11a82d994e183d%7C0%7C0%7C6378178020798

98960%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2l uMzIiLCJB

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

TiI6 Ik1haWwiLCJXVCI6Mn0%3D%7C3000&amp;sdat

a=RVUc8KgTO3mhFL9qemshHJqiyXJs%2Fvytx21AMyUaW%2Bs%3D&amp;res erved=0

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line

-

devel%2Fcover%2F20210720160342.11415-1-

Wayne.Lin%40amd.com%2F&amp;data=04%7C01%7Cwayne.lin%40amd.com %

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

7C2e
19d9
32
1b
5f4e948c1208d9e6884f24%7C3dd8961fe488

4e608e11a82d994e183d%7C0%7C0%7C637794295422403301%7CUnknown%7 C

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

TWFp
bGZs
b3
d8
eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiL

CJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&amp;sdata=rvtwQioO3pT1BoH

Quoted 26 lines
Quoted 26 lines
Quoted 26 lines
Quoted 26 lines
Quoted 26 lines
Quoted 26 lines
Quoted 26 lines
Quoted 26 lines
Quoted 26 lines

b7S5
32ms
Ra
61
gP1vIyE8AB7M9I4E%3D&amp;reserved=0

Quoted 21 lines

I was trying to fix some problems that I observed after these 2
patches
* 09b974e8983 drm/amd/amdgpu_dm/mst: Remove -

Quoted 1 line

destroy_connector()

callback
* 72dc0f51591 drm/dp_mst: Remove
drm_dp_mst_topology_cbs.destroy_connector

With above patches, it changes our flow to remove dc_sink (by
calling
dc_link_remote_sink()) when connector is about to be destroyed. And let's see below use cases:
a). Disconnection happens between mst branch devices e.g.
src - 1st_mstb - 2nd_mstb - sst_monitor => src - 1st_mstb (disconnect)
2nd_mstb - sst_monitor

Quoted 16 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

In above case, after receiving CSN, we will put topology reference
count
of
2nd mstb and its port which connected to the sst monitor. As you suggested before, we will destroy 2nd mst branch device
and

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

its
all
ports due to there is no more mst path can route to them. As the result of that, we at end call drm_dp_delayed_destroy_port()
and
unregister/put the drm connector and eventually call dc_link_remote_sink() to release the dc_sinck we

enumerate

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

for
the
specific
stream sink of sst_monitor.

However, in below case:
b). Disconnection happens between mst branch devices

and SST

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

monitor
e.g.
src - 1st_mstb - sst_monitor => src - 1st_mstb (disconnect) sst_monitor

In this case, which is the case having problem, it definitely
won't
decrease
the topology references count of the port of 1st_mstb

which

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

was connected to the sst monitor to zero since the port is still
existing in
the topology. Same as the malloc reference since the port can't
get
destroyed. Hence, the port still exists and we won't call drm_dp_delayed_destroy_port() to unregister and put

the drm

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

connector.
As
the
result of that, we can't release corresponding remote dc_sink
under
this
case. And after few times hotplugs, we can't create any

new

Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines

dc_sink since the number of stream sink is exceeding our limitation.

Hence, what I'm thinking here is to register a callback function
for
notifying us that the remote sink is detached. This just aligns
what
we
do
for handling long HPD event (unplug monitor from system)

of

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

legacy
DP
(none
mst). For which case, once we detect long HPD event indicating the monitor is detached, we will immediately try to
release
the
dc_link->local_sink and fire hotplug event to upper layer. Same as here, once receives a CSN message notifying a

drm

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

connector
is
changed from connected to disconnected, trigger the

callback

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

function and we can try to release the dc_sink resource.

Quoted 2 lines

If I'm remembering that correctly, then (and I'm not

100%

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

sure
on
this
yet) I might already have noticed something very

possibly

Quoted 38 lines
Quoted 38 lines
Quoted 38 lines
Quoted 38 lines
Quoted 38 lines
Quoted 38 lines
Quoted 38 lines
Quoted 38 lines
Quoted 38 lines
Quoted 38 lines
Quoted 38 lines

wonky in
how we handle payload allocations currently, that I found while
working on
the non-atomic removal branch that I linked to you in my previous response. Basically, in the
step1() function it looks like that we follow this general flow
with
updating
payloads:

 * Loop through proposed payloads and compare to previously
programmed
   payloads
    - If a payload has a different allocation then it previously
did,
update the
      payload
    - If the payload is new, create it
    - If a payload no longer has an allocation, remove the payload

At first glance this seems totally correct - but I'm not actually
entirely
convinced it is? Particularly because it seems like the order
in
which
we do creation/deletion of payloads is totally arbitrary. To
explain
what
I mean by that, consider a state transition like this:

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

vcpi_slots=15 vcpi_slots=35 vcpi_slots=14

Quoted 1 line

1 | 2 |xxxxxxxx|

Let's say we want to increase payload #1 from 15 to 50, and
disable
payload #2 in the same atomic commit on DRM's side. If

the

Quoted 63 lines
Quoted 63 lines
Quoted 63 lines
Quoted 63 lines
Quoted 63 lines
Quoted 63 lines
Quoted 63 lines
Quoted 63 lines
Quoted 63 lines
Quoted 63 lines
Quoted 12 lines

order
we
update payloads is entirely arbitrary, we could end up doing
this:

 * Increase VCPI slots payload #1 from 15->50 (total VCPI slots=99,
overflow!)
 * Decrease VCPI slots payload #2 from 35->0  (total VCPI slots=50)

Quoted 13 lines

Notice on the first step, we've technically overflowed the available
number of VCPI slots in the payload table. This is still before
step
2
though, and I could be totally wrong here - perhaps this is
entirely
OK in
the real world, and we're allowed to overflow VC slots as long
as

I think it's forbidden to allocate time slots beyond 64 when we're
updating
payload ID table of the branch in the real world. Branch probably
will NACK the DPCD write.

Quoted 7 lines

we repair the issue before step 2. But so far I can't seem to
find
anything in the DP 2.0 spec that explicitly states this would
be
OK

Quoted 8 lines

-
which
makes me think we might fail some payload allocations if we
don't
always
ensure we avoid overflows in between VC payload table changes

Agree.
For amdgpu, I think we always do deallocation (call hws-

Quoted 1 line

funcs.reset_hw_ctx_wrap()) before allocation

(apply_single_controller_ctx_to_hw()).

Quoted 9 lines

Note that avoiding overflows would be as simple as just making
sure
we
send all VC payload table changes that free VC slots before
sending
any that take new slots.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

Again - I haven't actually confirmed this yet and am hoping to
test
stuff
like this very soon as I'm pretty close finishing up the initial
attempt
at removing the non-atomic MST code in the DRM

helpers

Quoted 35 lines
Quoted 35 lines
Quoted 35 lines
Quoted 35 lines
Quoted 35 lines
Quoted 35 lines
Quoted 35 lines
Quoted 35 lines
Quoted 35 lines
Quoted 35 lines
Quoted 35 lines

now. If
my
theory
ends up being correct here, I can fix this in my rewrite of
the
MST payload management code. But I figured it might be worth
mentioning in
the mean time in case you think it might be relevant to the
problem here :).

Quoted 21 lines · Lyude Paul

(CC'ing this to dri-devel, since this is basically patch
review)

Alright - so first off sorry about the broken debugging
patch! I
thought I had tested that but I guess I hadn't tested it
well
enough,
I'll get it fixed asap, but feel free to try getting to
it
before
I
do.

As for this patch... (comments below)

On Mon, 2021-12-20 at 02:17 +0000, Lin, Wayne wrote:
[AMD Official Use Only]

Hi Lyude,

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

No rush and thanks for your time! : ) Just want to
help
clarify
what
I mean that "registering a callback function"
for CSN of disconnecting
stream sink device (not mst branch case). Attach

below

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

the
tentative
patch that I planned to do. Thanks so much!

Quoted 13 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

Regards,
Wayne
---

 .../display/amdgpu_dm/amdgpu_dm_mst_types.c   | 53

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

+++++++++++++++++++
 drivers/gpu/drm/drm_dp_mst_topology.c         | 16
+++++-
 include/drm/drm_dp_mst_helper.h               |  1 +
 3 files changed, 68 insertions(+), 2 deletions(-)

diff --git

a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_mst_

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

type
s.c

b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_mst_

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

type
s.c
index cc34a35d0bcb..d7343c64908c 100644
---

a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_mst_

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

type
s.c
+++

b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_mst_

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

type
s.c
@@ -472,8 +472,61 @@

dm_dp_add_mst_connector(struct

Quoted 48 lines
Quoted 48 lines
Quoted 48 lines
Quoted 48 lines
Quoted 48 lines
Quoted 48 lines
Quoted 48 lines
Quoted 48 lines
Quoted 48 lines
Quoted 48 lines
Quoted 47 lines
Quoted 47 lines

drm_dp_mst_topology_mgr *mgr,
        return connector;
 }

+void dm_dp_notify_csn_disconnection(struct
drm_connector
+*connector) {
+       struct amdgpu_dm_connector *aconnector =
+               to_amdgpu_dm_connector(connector);
+       struct dc_sink *dc_sink = aconnector->dc_sink;
+       struct dc_link *dc_link = aconnector->dc_link;
+       struct amdgpu_device *adev =
drm_to_adev(ddev);
+
+       ASSERT(dc_link);
+
+       if (dc_sink) {
+               mutex_lock(&adev->dm.dc_lock);
+
+               /*clear the remote sink of the link*/
+               dc_link_remove_remote_sink(dc_link,
dc_sink);
+               dc_sink_release(dc_sink);
+               aconnector->dc_sink = NULL;
+
+               mutex_unlock(&adev->dm.dc_lock);
+       }
+}
+
 static const struct drm_dp_mst_topology_cbs
dm_mst_cbs =
{
        .add_connector = dm_dp_add_mst_connector,
+       .notify_csn_disconnection =
dm_dp_notify_csn_disconnection,
 };

I still don't really think this is a good idea. This
seems
like
it's
just adding another hotplugging path to the code in
order to
avoid
sending hotplugs for non-endpoint devices. In addition
to
the

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

So, what I wanted to do is to add a callback function for endpoint
devices
case as described above. For disconnection of non-

endpoint

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

devices
is
already taken care by current code.

Quoted 1 line
Quoted 1 line

drm_connector issues I mentioned before, we also

really

Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
Quoted 20 lines

need
to
stop
doing any kind of payload maintence in hotplugging
paths.
The
reality
is any kind of payload maintanence we do outside of
normal
modesetting
paths is a relic from legacy modesetting that I'm
dropping
ASAP
(more
on this below), and we can't keep adding to it because
it
dramatically
complicates maintanence as well.

Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

Sorry for repeating this point so often but - the
biggest
issue
too

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

is
I'm still not sure what it is we're even avoiding here.
We
know
resources aren't released consistently, and that we're
able
to
reproduce the behavior with repeated hotplugs. We

also

Quoted 23 lines
Quoted 23 lines
Quoted 23 lines
Quoted 23 lines
Quoted 23 lines
Quoted 23 lines
Quoted 23 lines
Quoted 23 lines
Quoted 23 lines
Quoted 23 lines
Quoted 23 lines
Quoted 23 lines

know
that
if
we
skip sending certain hotplug events, that fixes the
issue.
And
we
know
we can workaround it by adding a special case for
forcing a
payload
release in DC. But none of those actually tell us
exactly
what
piece
of code is leaking and why, which means that any
workarounds
we're
putting in to avoid this mysterious guilty section of
code
we
don't

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

entirely understand either - which means we're just
adding
more
code
in that no one actually fully understands. This just
ends up
making
maintence difficult because every change in code

nearby

Quoted 22 lines
Quoted 22 lines
Quoted 22 lines
Quoted 22 lines
Quoted 22 lines
Quoted 22 lines
Quoted 22 lines
Quoted 22 lines
Quoted 22 lines
Quoted 22 lines
Quoted 5 lines
Quoted 5 lines

workarounds
like this has to strugle to try to figure out said
workarounds
in
order to avoid breaking things.

Thank you Lyude for the reminder and I totally agree with you by
avoiding
adding workaround since it will get the code maintenance harder.
And
I
think the root cause of the issue that I observed before is quite
concrete.
There is no code path for us to release dc_sink under the unplug
scenario
that I described above.

Quoted 1 line
Quoted 1 line
Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

I'm actually currently running into these "later" issues
right
now,
as

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

recently I've (-actively-, finally!!!) been working on
trying to
remove as much non-atomic MST as I can because. As it
turns
out
-
a
huge amount of the payload maintanence code just

isn't

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

needed at
all
when we stop caring about non-atomic drivers and

stick

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

everything
in
atomic state structs. Step 1 for updating updating the
payload
table,
e.g. drm_dp_update_payload_part1(), is a great

example

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

of
how
messy
things have become. Here's a small sample of some of

the

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

stuff
I've
seen from just that one function so far that either just
don't
make
sense here or is totally redundant. I should note that a
lot

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

of
these
things also come from patches I reviwed, but didn't
really
look
at
as
closely as I should have because I was swamped at

work,

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

some
are
historical artifacts, and others are less-than-ideal
patches
I
got
wrote myself when I was first started working on MST

and

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

didn't
know
the code as well as I do
now:

 * We try to avoid some sort of userspace issue by

using

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

   drm_dp_mst_port_downstream_of_branch() to

avoid

Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
Quoted 20 lines

releasing
payloads
for a
   branch if we can't prove it's downstream of the top
of
the
topology. This
   seems to workaround either a userspace bug. This is a
redundant,
since
   that's what topology refs are already supposed to be
doing to
the
extent is
   reasonably possibly. It's also unfortunately racy
solution
because
we have
   to be able to handle the situation where a connector
is

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

removed
from under
   us. That can happen at any time, including
_immediately_
after
we
call
   drm_dp_mst_port_downstream_of_branch() -

rendering

Quoted 60 lines
Quoted 60 lines
Quoted 60 lines
Quoted 60 lines
Quoted 60 lines
Quoted 60 lines
Quoted 60 lines
Quoted 60 lines
Quoted 60 lines
Quoted 60 lines
Quoted 48 lines
Quoted 48 lines

the
call
not
really
   useful.
 * If we fail to validate the sink in
drm_dp_update_payload_part,
we
don't
   update the payload table. I think at best this
solution
is
racy
and
not
   useful, at worst it leaves us with a payload table
that
doesn't
match what
   we attempted to set in the atomic state - which at
worst
brings
us
into
   undefined territory where we're just plain out of
sync
with
the
reality in
   hw.
 * Actually fun fact - mgr->payloads and mgr-
proposed_vcpis
both
can
and
   definitely should be removed entirely. All of the
info
for
mgr->payloads
   could just be in the atomic state, because that + the
magic
of
atomic state
   duplication means we'll also have an accurate view of
the
previous
state's
   payload allocations: which renders mgr-

Quoted 7 lines
Quoted 7 lines

proposed_vcpis
redundant.

Apologies for the long explanation again, but I hope
that
explains
my

Really really thanks for your kindly help on this : ) I'll try my best to get to your WIP branch soon. Thank you Lyude!

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

point here a bit. I'm going to be trying to get to
moving
amdgpu's
DSC
code out of amdgpu and into DRM helpers as well

soon, so

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

I'm
really
determined to clean stuff up beforehand as every time
I've
done
so

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

it's become substantially easier to make changes to this
code.
Things
used to be even worse before I started cleaning things
up 2
or 3
years
ago, where simple changes would end up getting me

stuck

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

spending
hours
trying to dig through lockdep or memory manegement
issues.
As
well,
I
would be entirely unsurprised if bugs like this very
payload
leak
we're working on just disappear once we've gotten rid

of

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

all
the
extraneous workarounds and state tracking here -
especially
with
how
many special cases we have for maintaining the

payload

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

table
right
now. That's certainly ended up being the case in the
past
with a
number of other difficult to track down issues I've
dealt
with
in
MST.

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

Anyhow. I've been way more productive this year then
last
because
I
had over a month off work and am finally not super

burnt

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

out
from
my
job, and so far I've been making progress on the

payload

Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines

state
cleanup
far faster then I was last year :). I think if you can't
figure
out
where the leak is coming from even once I get the
debugging
patches
I
mentioned fixed up, it might be a good idea for us to
try
again

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

after
I've got some of this code cleaned up. I've got a
currently
WIP
branch
here:

https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgitl

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

ab.freedesktop.org%2Flyudess%2Flinux%2F-
%2Fcommit%2F624dd68fa804e64b5b

2060e4735d7317090692b5&amp;data=04%7C01%7Cwayne.lin%40am

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

d.co
m%7C
d4
bd
c7

59eb274bfccc8208d9daef41fe%7C3dd8961fe4884e608e11a82d994

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

e183
d%7C
0%
7C
0%

7C637781543416430632%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

wLjA
wMDA
iL
CJ
QI

joiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&amp;sda

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

ta=n
FpVl
%2
BV
oF

40JPabfRMcR6Cz7ZHDwt%2BBpLDBNdXftJaA%3D&amp;reserved=0

Quoted 70 lines
Quoted 70 lines
Quoted 70 lines
Quoted 70 lines
Quoted 70 lines
Quoted 70 lines
Quoted 70 lines
Quoted 70 lines
Quoted 70 lines
Quoted 70 lines
Quoted 70 lines
Quoted 70 lines

 void amdgpu_dm_initialize_dp_connector(struct
amdgpu_display_manager *dm, diff --git
a/drivers/gpu/drm/drm_dp_mst_topology.c
b/drivers/gpu/drm/drm_dp_mst_topology.c
index 857c5d15e81d..a70e26c5d084 100644
--- a/drivers/gpu/drm/drm_dp_mst_topology.c
+++ b/drivers/gpu/drm/drm_dp_mst_topology.c
@@ -2508,6 +2508,7 @@
drm_dp_mst_handle_conn_stat(struct
drm_dp_mst_branch *mstb,
        u8 new_pdt;
        bool new_mcs;
        bool dowork = false, create_connector = false;
+       bool notify_unplug_event = false;

        port = drm_dp_get_port(mstb, conn_stat-
port_number);
        if (!port)
@@ -2520,6 +2521,9 @@
drm_dp_mst_handle_conn_stat(struct
drm_dp_mst_branch *mstb,
                         * port, so just throw the
port
out
and
make
sure we
                         * reprobe the link address of
it's
parent
MSTB
                         */
+                       /* should also consider
notify_unplug_event
here.
+                        * but it's not a normal case
for
products
+in the
market
+                        */

drm_dp_mst_topology_unlink_port(mgr,
port);
                        mstb->link_address_sent =
false;
                        dowork = true; @@ -2541,10
+2545,14 @@
drm_dp_mst_handle_conn_stat(struct
drm_dp_mst_branch
*mstb,
        port->ddps = conn_stat-
displayport_device_plug_status;

        if (old_ddps != port->ddps) {
-               if (port->ddps && !port->input)
+               if (port->ddps && !port->input) {

drm_dp_send_enum_path_resources(mgr,
mstb,
port);
-               else
+               } else {
                        port->full_pbn = 0;
+                       if (port->connector &&
+
drm_dp_mst_is_end_device(port-
pdt,

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

+port-
mcs))
+                               notify_unplug_event =
true;
+               }
        }

        new_pdt = port->input ?

DP_PEER_DEVICE_NONE :

Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines

conn_stat-
peer_device_type;
@@ -2557,11 +2565,15 @@
drm_dp_mst_handle_conn_stat(struct
drm_dp_mst_branch
*mstb,
                dowork = false;
        }

+       if (notify_unplug_event &&
+mgr->cbs->notify_csn_disconnection)
+               mgr->cbs-
notify_csn_disconnection(port-
connector);
+
        if (port->connector)
                drm_modeset_unlock(&mgr->base.lock);
        else if (create_connector)
                drm_dp_mst_port_add_connector(mstb,
port);

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

+
 out:
        drm_dp_mst_topology_put_port(port);
        if (dowork)
diff --git a/include/drm/drm_dp_mst_helper.h
b/include/drm/drm_dp_mst_helper.h index
78044ac5b59b..ff9e47729841
100644
--- a/include/drm/drm_dp_mst_helper.h
+++ b/include/drm/drm_dp_mst_helper.h
@@ -525,6 +525,7 @@ struct

drm_dp_mst_topology_cbs {

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

         * IRQ pulse handler.
         */
        void (*poll_hpd_irq)(struct
drm_dp_mst_topology_mgr
*mgr);
+       void (*notify_csn_disconnection)(struct
drm_connector
+*connector);
 };

 #define DP_MAX_PAYLOAD (sizeof(unsigned long) *

8)

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

--
2.31.0

-----Original Message-----
From: Lin, Wayne
Sent: Wednesday, December 8, 2021 11:39 AM
To: 'Lyude Paul' <[email protected]>
Subject: RE: [PATCH] WIP: drm/dp_mst: Add

support

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

for
dumping
topology ref histories from debugfs

No worries Lyude!
Thanks for keeping helping on this. Take your time :
)

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

-----Original Message-----
From: Lyude Paul <[email protected]>
Sent: Wednesday, December 8, 2021 7:05 AM
To: Lin, Wayne <[email protected]>
Subject: Re: [PATCH] WIP: drm/dp_mst: Add

support

Quoted 62 lines
Quoted 62 lines
Quoted 62 lines
Quoted 62 lines
Quoted 62 lines
Quoted 62 lines
Quoted 62 lines
Quoted 62 lines
Quoted 62 lines
Quoted 62 lines
Quoted 62 lines
Quoted 62 lines

for
dumping
topology ref histories from debugfs

Sorry! I will try to get to this tomorrow, if not
then
sometime
this week.

On Tue, 2021-11-30 at 08:41 +0000, Lin, Wayne
wrote:
[Public]

Hi Lyude,

Finally have some bandwidth to get back to this
problem!
I roughly went through this patch and I'm just
aware
that
we
already have such kind of convenient tool for a
while.
I think it's definitely useful for us to track
port/mstb
reference count issues and I'll start to embrace
this
feature
for cleaning up those issues. Thank you Lyude!

However, I think the issue that I was trying to
fix
is
not
related to what you suggested:
" The idea here is that if stream resources
aren't
being
released, my guess would be that we're not
dropping
topology
references for the port which means the
connector
never
goes
away."
The issue I was trying to fix is about releasing
dc_link->remote_sinks while receiving a CSN
message
notifying
the connection status of a sst connector of a
port
changed
from connected to disconnected. Not the
connection
status
changed of a mst branch device.
e.g.
src - 1st_mstb - 2nd_mstb - sst_monitor => src -
1st_mstb
(disconnect) 2nd_mstb - sst_monitor

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

In above case, after receiving CSN, we will put
topology
references of 2nd mstb and its port which is
connected
with
the sst monitor. As the result of that, we can
call
drm_dp_delayed_destroy_port() to unregister

and

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

put
the
drm
connector.

However, in below case:
e.g.
src - 1st_mstb - sst_monitor => src - 1st_mstb
(disconnect)
sst_monitor

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

In this case, which is the case having problem,
it
definitely
won't decrease the topology references count

of

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

the
port
which
was connected to the sst monitor to zero since
the
port
is

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

still existing in the topology. Same as the
malloc
reference
since the port can't get destroyed. Hence, the
port
still
exists  and we won't call
drm_dp_delayed_destroy_port() to unregister

and

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

put
the
drm
connector.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

I looked up the code and
drm_dp_delayed_destroy_port()
seems
like the only place to call drm_connector_put()
which
means
we
can't put reference count of drm connector

under

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

this
case
and
can't release dc_sink resource by destroying

drm

Quoted 78 lines
Quoted 78 lines
Quoted 78 lines
Quoted 78 lines
Quoted 78 lines
Quoted 78 lines
Quoted 78 lines
Quoted 78 lines
Quoted 78 lines
Quoted 78 lines
Quoted 78 lines
Quoted 78 lines

connector.

I would also like to point out that this
resource
(remote_sinks) is specific to different stream
sinks. So
if
we're trying to release this dc_sink resource by
destroying
the drm connector, it conflicts the idea that
you
suggested
before that we should always keep the drm
connector
until
it's
no longer reachable in the topology.
Releasing dc_sink should be binding with the
disconnection
event.

I understand your concern that we should not
just
easily
change the logic here since it's the result
after
solving
tons
of bugs before and might cause other side
effect.
So,
just
my
2 cents, what I'm thinking is to register a
callback
function
for our driver to notify us that the remote sink
is
detached.
This just aligns our flow handling long HPD
event of
legacy
(sst) DP.
For sst case, once we detect long HPD event
indicating
the
monitor is detached, we will immediately try to
release
the
dc_link->local_sink and fire hotplug event to
upper
layer.
Same as here, once receives a CSN message
notifying
a
drm
connector is changed from connected to
disconnected,
trigger
the callback function and we can try to release
the
dc_sink
resource.

Would like to know your thought and insight
please :
)

Btw, I got some errors and warnings while
building
and
have
some code adjustments as below : ) Thank you
Lyude
for
your
always kindly help!!

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

Regards,
Wayne
-----Original Message-----
From: Lyude Paul <[email protected]>
Sent: Wednesday, November 3, 2021 7:15 AM
To: Lin, Wayne <[email protected]>
Subject: [PATCH] WIP: drm/dp_mst: Add

support

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

for
dumping
topology ref histories from debugfs

TODO:
* Implement support for i915
* Finish writing this commit message
* ???
* profit

Signed-off-by: Lyude Paul

<[email protected]>

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines

---

Hey wayne! SO-hopefully if I did this
correctly
then
this
should just work on amdgpu. What this patch
should
do
is
add
a debugfs file to amdgpu called
"amdgpu_dp_mst_topology_refs", and when

you

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

read
the
file
it
should print out the topology reference
history of
every
MSTB and Port in memory, along with how

many

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

times
we've
hit
the codepath in each backtrace. An example:

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

Port DP-5 (0000000005c37748) topology ref
history:
  1 gets (last at    58.468973):
     drm_dp_send_link_address+0x6a5/0xa00
[drm_kms_helper]

drm_dp_check_and_send_link_address+0xad/0xd0

Quoted 16 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

[drm_kms_helper]

     drm_dp_mst_link_probe_work+0x14e/0x1a0

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

[drm_kms_helper]
     process_one_work+0x1e3/0x390
     worker_thread+0x50/0x3a0
     kthread+0x124/0x150
     ret_from_fork+0x1f/0x30
  1 puts (last at    58.469357):

     drm_dp_mst_topology_put_port+0x6a/0x210

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

[drm_kms_helper]
     drm_dp_send_link_address+0x39e/0xa00
[drm_kms_helper]

drm_dp_check_and_send_link_address+0xad/0xd0

Quoted 44 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

[drm_kms_helper]

     drm_dp_mst_link_probe_work+0x14e/0x1a0

Quoted 41 lines
Quoted 41 lines
Quoted 41 lines
Quoted 41 lines
Quoted 41 lines
Quoted 41 lines
Quoted 41 lines
Quoted 41 lines
Quoted 41 lines
Quoted 41 lines
Quoted 41 lines

[drm_kms_helper]
     process_one_work+0x1e3/0x390
     worker_thread+0x50/0x3a0
     kthread+0x124/0x150
     ret_from_fork+0x1f/0x30

The idea here is that if stream resources
aren't
being
released, my guess would be that we're not
dropping
topology
references for the port which means the
connector
never
goes
away. So, if that's really the case then once
we
unplug
the
offending connector we should be able to find
a
pair
of
gets/puts for the offending leaked connector
where
the
get/put count doesn't match up. Also, if the
frame
count
on
the backtrace isn't long enough you can
increase
the
value
of STACK_DEPTH in
drivers/gpu/drm/drm_dp_mst_topology.c
and
recompile to get more frames.

To enable this, first enable CONFIG_EXPERT

for

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

your
kernel
which will unhide

CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Then just enable

CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS,

Quoted 19 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

recompile, and it should be good to go.

Let me know if this works for you, and
hopefully
this
should
tell us exactly what the problem actually is
here.

 .../amd/display/amdgpu_dm/amdgpu_dm_debugfs.c

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

|
35
++++
 drivers/gpu/drm/drm_dp_mst_topology.c
|
173
++++++++++++++----

Quoted 14 lines

 drivers/gpu/drm/nouveau/nouveau_debugfs.c

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines

|
35
++++
 include/drm/drm_dp_mst_helper.h
|
18
++
 4 files changed, 228 insertions(+), 33
deletions(-)

diff --git

a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

_dm_
debu
gf
s.
c

b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

_dm_
debu
gf
s.
c
index 1a68a674913c..1a14732c52b4 100644
---

a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

_dm_
debu
gf
s.
c
+++

b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

_dm_
debu
gf
+++ s.c
@@ -3063,6 +3063,37 @@ static int
mst_topo_show(struct
seq_file *m, void
*unused)
      return 0;
 }

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

+#ifdef

CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

+static int

mst_topology_ref_dump_show(struct

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

seq_file
*m,
+void
+*unused) {
+     struct amdgpu_device *adev = (struct

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

amdgpu_device
+*)m->private;
+     struct drm_device *dev =
adev_to_drm(adev);
+     struct drm_connector *connector;
+     struct drm_connector_list_iter
conn_iter;
+     struct amdgpu_dm_connector

*aconnector;

Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines

+
+     drm_connector_list_iter_begin(dev,
&conn_iter);
+     drm_for_each_connector_iter(connector,
&conn_iter)
{
+             if (connector->connector_type !=
DRM_MODE_CONNECTOR_DisplayPort)
+                     continue;
+
+             aconnector =
+to_amdgpu_dm_connector(connector);
+
+             /* Ensure we're only dumping the
topology
of
a
+root mst node
*/
+             if (!aconnector-
mst_mgr.max_payloads)

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

+                     continue;
+
+             seq_printf(m, "\nMST topology
for
connector
+%d\n",
aconnector->connector_id);

+             drm_dp_mst_dump_topology_refs(m,

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

+&aconnector->mst_mgr);
+     }

+     drm_connector_list_iter_end(&conn_iter);

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

+
+     return 0;
+}
+

+DEFINE_SHOW_ATTRIBUTE(mst_topology_ref_dump);

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

+#endif
+
 /*
  * Sets trigger hpd for MST topologies.
  * All connected connectors will be
rediscovered
and
re

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

started as needed if val of 1 is sent.
@@ -3299,6 +3330,10 @@ void
dtn_debugfs_init(struct
amdgpu_device
*adev)

debugfs_create_file("amdgpu_mst_topology",

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

0444,
root,
                          adev,
&mst_topo_fops);
+#ifdef

CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

+

debugfs_create_file("amdgpu_dp_mst_topology_re

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

fs",
+0444, root, adev,
+
&mst_topology_ref_dump_fops);
+#endif

Quoted 11 lines

      debugfs_create_file("amdgpu_dm_dtn_log",

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

0644,
root,
adev,
                          &dtn_log_fops);

diff --git
a/drivers/gpu/drm/drm_dp_mst_topology.c
b/drivers/gpu/drm/drm_dp_mst_topology.c
index 1aa8702383d4..0159828c494d 100644
---

a/drivers/gpu/drm/drm_dp_mst_topology.c

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

+++

b/drivers/gpu/drm/drm_dp_mst_topology.c

Quoted 82 lines
Quoted 82 lines
Quoted 82 lines
Quoted 82 lines
Quoted 82 lines
Quoted 82 lines
Quoted 82 lines
Quoted 82 lines
Quoted 82 lines
Quoted 82 lines
Quoted 82 lines
Quoted 82 lines

@@ -1366,23 +1366,6 @@ static int
drm_dp_mst_wait_tx_reply(struct
drm_dp_mst_branch
*mstb,
      return ret;
 }

-static struct drm_dp_mst_branch
*drm_dp_add_mst_branch_device(u8 lct, u8
*rad) -{
-     struct drm_dp_mst_branch *mstb;
-
-     mstb = kzalloc(sizeof(*mstb),
GFP_KERNEL);
-     if (!mstb)
-             return NULL;
-
-     mstb->lct = lct;
-     if (lct > 1)
-             memcpy(mstb->rad, rad, lct / 2);
-     INIT_LIST_HEAD(&mstb->ports);
-     kref_init(&mstb->topology_kref);
-     kref_init(&mstb->malloc_kref);
-     return mstb;
-}
-
 static void
drm_dp_free_mst_branch_device(struct
kref
*kref)  {
      struct drm_dp_mst_branch *mstb = @@ -
1642,12
+1625,20
@@ topology_ref_type_to_str(enum
drm_dp_mst_topology_ref_type type)
              return "put";
 }

+static const char
*topology_ref_history_type_to_str(enum
+drm_dp_mst_topology_history_type type) {
+     if (type ==
DRM_DP_MST_TOPOLOGY_HISTORY_PORT)
+             return "Port";
+     else
+             return "MSTB"; }
+
 static void
-__dump_topology_ref_history(struct
drm_dp_mst_topology_ref_history *history,
-                         void *ptr, const
char
*type_str)
+dump_topology_ref_history(struct
+drm_dp_mst_topology_ref_history *history,
struct
+drm_printer p)
 {
-     struct drm_printer p =
drm_debug_printer(DBG_PREFIX);
+     char *connector_name = NULL;
      char *buf = kzalloc(PAGE_SIZE,
GFP_KERNEL);
+     void *ptr;
      int i;

      if (!buf)
@@ -1656,14 +1647,29 @@
__dump_topology_ref_history(struct
drm_dp_mst_topology_ref_history *history,
      if (!history->len)
              goto out;

+     /* Get a pointer to the actual MSTB/port
so
we
can
the
+memory
address to the kernel log */
+     if (history->type ==
DRM_DP_MST_TOPOLOGY_HISTORY_PORT)

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

+             ptr = container_of(history,
struct
+drm_dp_mst_port,
topology_ref_history);
+     else
+             ptr = container_of(history,
struct
+drm_dp_mst_branch,

topology_ref_history);

Quoted 65 lines
Quoted 65 lines
Quoted 65 lines
Quoted 65 lines
Quoted 65 lines
Quoted 65 lines
Quoted 65 lines
Quoted 65 lines
Quoted 65 lines
Quoted 65 lines
Quoted 65 lines
Quoted 65 lines

+
      /* First, sort the list so that it goes
from
oldest
to
newest
       * reference entry
       */
      sort(history->entries, history->len,
sizeof(*history->entries),
           topology_ref_history_cmp, NULL);

-     drm_printf(&p, "%s (%p) topology count
reached
0,
dumping history:\n",
-                type_str, ptr);
+     if (history->type ==
DRM_DP_MST_TOPOLOGY_HISTORY_PORT)
+{
+             struct drm_dp_mst_port *port =
ptr;
+
+             if (port->connector)
+                     connector_name =
+port->connector->name;
+     }
+     if (connector_name)
+             drm_printf(&p, "Port %s (%p)
topology
ref
+history:\n",
connector_name, ptr);
+     else
+             drm_printf(&p, "%s (%p) topology
ref
+history:\n",
+
+topology_ref_history_type_to_str(history-
type),
ptr);

      for (i = 0; i < history->len; i++) {
              const struct
drm_dp_mst_topology_ref_entry
*entry = @@
-
1682,24 +1688,92 @@
__dump_topology_ref_history(struct
drm_dp_mst_topology_ref_history *history,
                         ts_nsec, rem_nsec /
1000,
buf);
      }

-     /* Now free the history, since this is
the
only
time
we expose it */
-     kfree(history->entries);
 out:
      kfree(buf);
 }

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

+/**
+ * drm_dp_mst_dump_topology_refs -

helper

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

function
for
+dumping the topology ref history
+ * @m: File to print to
+ * @mgr: &struct

drm_dp_mst_topology_mgr to

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

use
+ *
+ * Prints the topology ref history of all
ports
and

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

MSTBs
+on @mgr that are still in memory,
+ * regardless of whether they're actually
still
reachable
+through the topology or not. Only enabled
+ * when

%CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS is

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

enabled.
+Can be implemented by drivers to assist
+ * with debugging leaks in the DP MST
helpers.
+ */
+void

drm_dp_mst_dump_topology_refs(struct

Quoted 17 lines
Quoted 17 lines
Quoted 17 lines
Quoted 17 lines
Quoted 17 lines
Quoted 17 lines
Quoted 17 lines
Quoted 17 lines
Quoted 17 lines
Quoted 17 lines
Quoted 17 lines
Quoted 17 lines

seq_file
*m,
+struct drm_dp_mst_topology_mgr *mgr) {
+     struct drm_dp_mst_topology_ref_history
*history;
+     struct drm_printer p =
drm_seq_file_printer(m);
+
+     mutex_lock(&mgr-
topology_ref_history_lock);
+     list_for_each_entry(history,
+&mgr->topology_ref_history_list,
+node)
+
dump_topology_ref_history(history,
p);
+     mutex_unlock(&mgr-

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

topology_ref_history_lock);
+}

+EXPORT_SYMBOL(drm_dp_mst_dump_topology_refs);

Quoted 44 lines
Quoted 44 lines
Quoted 44 lines
Quoted 44 lines
Quoted 44 lines
Quoted 44 lines
Quoted 44 lines
Quoted 44 lines
Quoted 44 lines
Quoted 44 lines
Quoted 44 lines
Quoted 44 lines

+
+static void
+__init_topology_ref_history(struct
+drm_dp_mst_topology_ref_history
*history,
+                         struct
drm_dp_mst_topology_mgr
+*mgr,
+                         enum
+drm_dp_mst_topology_history_type
+type) {
+     history->type = type;
+     INIT_LIST_HEAD(&history->node);
+
+     mutex_lock(&mgr-
topology_ref_history_lock);
+     list_add(&history->node,
+&mgr->topology_ref_history_list);
+     mutex_unlock(&mgr-
topology_ref_history_lock);
+}
+
+static void
+__destroy_topology_ref_history(struct
+drm_dp_mst_topology_ref_history
*history,
+                            struct
drm_dp_mst_topology_mgr
+*mgr) {
+     mutex_lock(&mgr-
topology_ref_history_lock);
+     list_del(&mgr-
topology_ref_history_list);
+     mutex_unlock(&mgr-
topology_ref_history_lock);
+
+     kfree(history->entries); }
+
+static __always_inline void
+init_port_topology_history(struct
drm_dp_mst_topology_mgr
+*mgr, struct drm_dp_mst_port *port) {
+
+__init_topology_ref_history(&port-

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

topology_ref_history,
+mgr,
+

+DRM_DP_MST_TOPOLOGY_HISTORY_PORT);

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

+}
+
+static __always_inline void
+init_mstb_topology_history(struct
drm_dp_mst_topology_mgr
+*mgr, struct drm_dp_mst_branch *mstb) {
+
+__init_topology_ref_history(&mstb-

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

topology_ref_history,
+mgr,
+

+DRM_DP_MST_TOPOLOGY_HISTORY_MSTB);

Quoted 73 lines
Quoted 73 lines
Quoted 73 lines
Quoted 73 lines
Quoted 73 lines
Quoted 73 lines
Quoted 73 lines
Quoted 73 lines
Quoted 73 lines
Quoted 73 lines
Quoted 73 lines
Quoted 73 lines

+}
+
+static __always_inline void
+destroy_port_topology_history(struct
drm_dp_mst_port
*port)
+{
+
+__destroy_topology_ref_history(&port-
topology_ref_history,
+port->mgr); }
+
+static __always_inline void
+destroy_mstb_topology_history(struct
drm_dp_mst_branch
+*mstb) {
+
+__destroy_topology_ref_history(&mstb-
topology_ref_history,
+mstb->mgr); }
+
 static __always_inline void
 dump_mstb_topology_history(struct
drm_dp_mst_branch
*mstb)
{
-
__dump_topology_ref_history(&mstb-
topology_ref_history,
mstb,
-                                 "MSTB");
+     dump_topology_ref_history(&mstb-
topology_ref_history,
+drm_debug_printer(DBG_PREFIX));
 }

 static __always_inline void
 dump_port_topology_history(struct
drm_dp_mst_port
*port)
{
-
__dump_topology_ref_history(&port-
topology_ref_history,
port,
-                                 "Port");
+     dump_topology_ref_history(&port-
topology_ref_history,
+drm_debug_printer(DBG_PREFIX));
 }

 static __always_inline void @@ -1729,6
+1803,14
@@
topology_ref_history_unlock(struct
drm_dp_mst_topology_mgr *mgr)  }  #else
static
inline
void
+init_port_topology_history(struct
drm_dp_mst_topology_mgr
+*mgr, struct drm_dp_mst_port *port); static
inline
void
Should also add the bracket, otherwise will get
warnings.
=> static inline void
init_port_topology_history(struct
drm_dp_mst_topology_mgr *mgr, struct
drm_dp_mst_port
*port)
{};

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

+init_mstb_topology_history(struct
drm_dp_mst_topology_mgr
+*mgr, struct drm_dp_mst_branch *mstb);

static

Quoted 24 lines
Quoted 24 lines
Quoted 24 lines
Quoted 24 lines
Quoted 24 lines
Quoted 24 lines
Quoted 24 lines
Quoted 24 lines
Quoted 24 lines
Quoted 24 lines
Quoted 24 lines
Quoted 24 lines

inline
void
Same as above
=> static inline void
init_mstb_topology_history(struct
drm_dp_mst_topology_mgr *mgr, struct
drm_dp_mst_branch
*mstb)
{};

+destroy_port_topology_history(struct
+drm_dp_mst_topology_mgr *mgr, struct
drm_dp_mst_port
+*port); static inline void
destroy_port_topology_history() takes one
parameter
only
which
is port.
=> destroy_port_topology_history(struct
drm_dp_mst_port
*port)
{};

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

+destroy_mstb_topology_history(struct
+drm_dp_mst_topology_mgr *mgr, struct
drm_dp_mst_branch
+*mstb); static inline void
destroy_mstb_topology_history() takes one
parameter
only
which
is mstb =>

destroy_mstb_topology_history(struct

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drm_dp_mst_branch
*mstb) {};

 topology_ref_history_lock(struct
drm_dp_mst_topology_mgr
*mgr) {} static inline void
topology_ref_history_unlock(struct
drm_dp_mst_topology_mgr *mgr) {} @@ -

1740,6

Quoted 72 lines
Quoted 72 lines
Quoted 72 lines
Quoted 72 lines
Quoted 72 lines
Quoted 72 lines
Quoted 72 lines
Quoted 72 lines
Quoted 72 lines
Quoted 72 lines
Quoted 72 lines
Quoted 72 lines

+1822,25
@@
dump_port_topology_history(struct
drm_dp_mst_port
*port)
{}
#define save_port_topology_ref(port, type)
#endif

+static struct drm_dp_mst_branch *
+drm_dp_add_mst_branch_device(struct
drm_dp_mst_topology_mgr
+*mgr,
+u8 lct, u8 *rad) {
+     struct drm_dp_mst_branch *mstb;
+
+     mstb = kzalloc(sizeof(*mstb),
GFP_KERNEL);
+     if (!mstb)
+             return NULL;
+
+     mstb->lct = lct;
+     if (lct > 1)
+             memcpy(mstb->rad, rad, lct / 2);
+     INIT_LIST_HEAD(&mstb->ports);
+     kref_init(&mstb->topology_kref);
+     kref_init(&mstb->malloc_kref);
+     init_mstb_topology_history(mgr, mstb);
+     return mstb;
+}
+
 static void
drm_dp_destroy_mst_branch_device(struct
kref
*kref) {
      struct drm_dp_mst_branch *mstb = @@ -
1747,6
+1848,7
@@
static void
drm_dp_destroy_mst_branch_device(struct
kref *kref)
      struct drm_dp_mst_topology_mgr *mgr =
mstb-
mgr;

      dump_mstb_topology_history(mstb);
+     destroy_mstb_topology_history(mstb);

      INIT_LIST_HEAD(&mstb->destroy_next);

@@ -1856,6 +1958,7 @@ static void
drm_dp_destroy_port(struct
kref
*kref)
      struct drm_dp_mst_topology_mgr *mgr =
port-
mgr;

      dump_port_topology_history(port);
+     destroy_port_topology_history(port);

      /* There's nothing that needs locking to
destroy
an
input port yet */
      if (port->input) {
@@ -2135,7 +2238,7 @@
drm_dp_port_set_pdt(struct
drm_dp_mst_port *port, u8 new_pdt,
                      ret =
drm_dp_mst_register_i2c_bus(port);

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

              } else {
                      lct =
drm_dp_calculate_rad(port,
rad);
-                     mstb =
drm_dp_add_mst_branch_device(lct, rad);
+                     mstb =
+drm_dp_add_mst_branch_device(mgr, lct,

rad);

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

                      if (!mstb) {
                              ret = -ENOMEM;
                              drm_err(mgr-
dev,
"Failed
to
create MSTB for port %p", port); @@ -2366,6
+2469,8 @@
drm_dp_mst_add_port(struct drm_device

*dev,

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines

       */
      drm_dp_mst_get_mstb_malloc(mstb);

+     init_port_topology_history(mgr, port);
+
      return port;
 }

@@ -3745,7 +3850,7 @@ int
drm_dp_mst_topology_mgr_set_mst(struct
drm_dp_mst_topology_mgr *mgr, bool ms
              }

Quoted 16 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

              /* add initial branch device at
LCT
1 */
-             mstb =
drm_dp_add_mst_branch_device(1,
NULL);
+             mstb =
drm_dp_add_mst_branch_device(mgr,

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

1,
+NULL);
              if (mstb == NULL) {
                      ret = -ENOMEM;
                      goto out_unlock; @@ -
5512,14
+5617,16
@@ int

drm_dp_mst_topology_mgr_init(struct

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

drm_dp_mst_topology_mgr *mgr,
      mutex_init(&mgr->delayed_destroy_lock);
      mutex_init(&mgr->up_req_lock);
      mutex_init(&mgr->probe_lock); -#if

IS_ENABLED(CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_RE

Quoted 13 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

FS)
-     mutex_init(&mgr-
topology_ref_history_lock);
-#endif
      INIT_LIST_HEAD(&mgr->tx_msg_downq);
      INIT_LIST_HEAD(&mgr-

destroy_port_list);

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

      INIT_LIST_HEAD(&mgr-
destroy_branch_device_list);
      INIT_LIST_HEAD(&mgr->up_req_list);

+#if

IS_ENABLED(CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_RE

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

FS)
+     mutex_init(&mgr-
topology_ref_history_lock);
+     INIT_LIST_HEAD(&mgr-
topology_ref_history_list);

Quoted 13 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

+#endif
+
      /*
       * delayed_destroy_work will be queued
on a

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

dedicated
WQ, so that any
       * requeuing will be also flushed when
deiniting
the
topology manager.
diff --git

a/drivers/gpu/drm/nouveau/nouveau_debugfs.c

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

b/drivers/gpu/drm/nouveau/nouveau_debugfs.c

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

index 1cbe01048b93..53ec7c1daada 100644
---

a/drivers/gpu/drm/nouveau/nouveau_debugfs.c

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

+++

b/drivers/gpu/drm/nouveau/nouveau_debugfs.c

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

@@ -29,9 +29,13 @@
  */

 #include <linux/debugfs.h>
+#include <drm/drm_dp_mst_helper.h>

#include

Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
Quoted 16 lines

+<drm/drm_encoder.h>
 #include <nvif/class.h>
 #include <nvif/if0001.h>
+#include <nouveau_encoder.h>
 #include "nouveau_debugfs.h"
+#include "nouveau_display.h"
 #include "nouveau_drv.h"

 static int
@@ -68,6 +72,34 @@
nouveau_debugfs_strap_peek(struct
seq_file *m, void
*data)
      return 0;
 }

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

+#ifdef

CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

+static int

nouveau_debugfs_mst_topology_history(struct

Quoted 33 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

+seq_file *m, void *data) {
+     struct drm_info_node *node = m-

private;

Quoted 30 lines
Quoted 30 lines
Quoted 30 lines
Quoted 30 lines
Quoted 30 lines
Quoted 30 lines
Quoted 30 lines
Quoted 30 lines
Quoted 30 lines
Quoted 30 lines
Quoted 30 lines

+     struct drm_device *dev = node->minor-
dev;
+     struct drm_encoder *encoder;
+     struct nouveau_display *disp =
nouveau_display(dev);
+
+     if (disp->disp.object.oclass <
GF110_DISP)
+             return -EINVAL;
+
+     drm_for_each_encoder(encoder, dev) {
+             struct nv50_mstm *mstm;
+
+             /* We need the real encoder for
each
MST
mgr
+*/
+             if (encoder->encoder_type !=
+DRM_MODE_ENCODER_TMDS)
+                     continue;
+             mstm = nouveau_encoder(encoder)-
dp.mstm;
+             if (!mstm)
+                     continue;
+
+             seq_printf(m, "MSTM on %s:\n",
encoder-
name);

+             drm_dp_mst_dump_topology_refs(m,

Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines

&mstm-
mgr);
+     }
+     return 0;
+}
+#endif
+
 static int
 nouveau_debugfs_pstate_get(struct seq_file
*m,
void
*data)
{ @@
-213,6
+245,9 @@ static const struct file_operations
nouveau_pstate_fops = {  static struct
drm_info_list
nouveau_debugfs_list[] = {
      { "vbios.rom",
nouveau_debugfs_vbios_image,
0,

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

NULL
},
      { "strap_peek",
nouveau_debugfs_strap_peek,
0,
NULL
},
+#ifdef

CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

+     { "dp_mst_topology_refs",
+nouveau_debugfs_mst_topology_history, 0,

NULL

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

},
#endif
 };
 #define NOUVEAU_DEBUGFS_ENTRIES
ARRAY_SIZE(nouveau_debugfs_list)

diff --git

a/include/drm/drm_dp_mst_helper.h

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

b/include/drm/drm_dp_mst_helper.h index
78044ac5b59b..e3a73d8d97c0
100644
--- a/include/drm/drm_dp_mst_helper.h
+++ b/include/drm/drm_dp_mst_helper.h
@@ -35,6 +35,11 @@ enum
drm_dp_mst_topology_ref_type {
      DRM_DP_MST_TOPOLOGY_REF_PUT,
 };

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

+enum drm_dp_mst_topology_history_type {

+     DRM_DP_MST_TOPOLOGY_HISTORY_PORT,

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

+     DRM_DP_MST_TOPOLOGY_HISTORY_MSTB, };

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

+
 struct drm_dp_mst_topology_ref_history {
      struct drm_dp_mst_topology_ref_entry {
              enum
drm_dp_mst_topology_ref_type
type;
@@

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

-43,6
+48,9 @@ struct
drm_dp_mst_topology_ref_history {
              depot_stack_handle_t backtrace;
      } *entries;
      int len;
+
+     enum

drm_dp_mst_topology_history_type

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

type;
+     struct list_head node;
 };
 #endif /*

IS_ENABLED(CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_RE

Quoted 28 lines
Quoted 28 lines
Quoted 28 lines
Quoted 28 lines
Quoted 28 lines
Quoted 28 lines
Quoted 28 lines
Quoted 28 lines
Quoted 28 lines
Quoted 28 lines
Quoted 28 lines
Quoted 28 lines

FS)
*/

@@ -769,6 +777,12 @@ struct
drm_dp_mst_topology_mgr {
       *
&drm_dp_mst_branch.topology_ref_history.
       */
      struct mutex topology_ref_history_lock;
+
+     /**
+      * @topology_ref_history_list: contains
a
list
of
+topology ref
histories for any MSTBs or
+      * ports that are still in memory
+      */
+     struct list_head
topology_ref_history_list;
 #endif
 };

@@ -873,6 +887,10 @@ void
drm_dp_mst_put_port_malloc(struct
drm_dp_mst_port *port);

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

 struct drm_dp_aux
*drm_dp_mst_dsc_aux_for_port(struct
drm_dp_mst_port *port);

+#ifdef

CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

+void

drm_dp_mst_dump_topology_refs(struct

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

seq_file
*m,
+struct drm_dp_mst_topology_mgr *mgr);

#endif

Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
Quoted 15 lines

+
 extern const struct drm_private_state_funcs
drm_dp_mst_topology_state_funcs;

 /**
--
2.31.1

--
Cheers,
 Lyude Paul (she/her)
 Software Engineer at Red Hat

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

--
Cheers, Lyude Paul (she/her) Software Engineer at Red

Hat

Quoted 33 lines
Quoted 19 lines
Quoted 19 lines
Quoted 18 lines
Quoted 17 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 4 lines

--
Regards,
Wayne Lin

Quoted 7 lines
Quoted 6 lines
Quoted 5 lines
Quoted 4 lines

--
Cheers,
 Lyude Paul (she/her)
 Software Engineer at Red Hat

--
Cheers,
 Lyude Paul (she/her)
 Software Engineer at Red Hat

Quoted 9 lines
Quoted 4 lines

--
Cheers,
 Lyude Paul (she/her)
 Software Engineer at Red Hat

--
Regards,
Wayne Lin

--
Cheers,
Lyude Paul (she/her)
Software Engineer at Red Hat