701-v6.2-0005-net-dpaa2-eth-assign-priv-mac-after-dpaa2_mac_connec.patch 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. From 06efc9b8a1360cad83cae6e71558e5458cc1fbf3 Mon Sep 17 00:00:00 2001
  2. From: Vladimir Oltean <[email protected]>
  3. Date: Tue, 29 Nov 2022 16:12:14 +0200
  4. Subject: [PATCH 07/14] net: dpaa2-eth: assign priv->mac after
  5. dpaa2_mac_connect() call
  6. There are 2 requirements for correct code:
  7. - Any time the driver accesses the priv->mac pointer at runtime, it
  8. either holds NULL to indicate a DPNI-DPNI connection (or unconnected
  9. DPNI), or a struct dpaa2_mac whose phylink instance was fully
  10. initialized (created and connected to the PHY). No changes are made to
  11. priv->mac while it is being used. Currently, rtnl_lock() watches over
  12. the call to dpaa2_eth_connect_mac(), so it serves the purpose of
  13. serializing this with all readers of priv->mac.
  14. - dpaa2_mac_connect() should run unlocked, because inside it are 2
  15. phylink calls with incompatible locking requirements: phylink_create()
  16. requires that the rtnl_mutex isn't held, and phylink_fwnode_phy_connect()
  17. requires that the rtnl_mutex is held. The only way to solve those
  18. contradictory requirements is to let dpaa2_mac_connect() take
  19. rtnl_lock() when it needs to.
  20. To solve both requirements, we need to identify the writer side of the
  21. priv->mac pointer, which can be wrapped in a mutex private to the driver
  22. in a future patch. The dpaa2_mac_connect() cannot be part of the writer
  23. side critical section, because of an AB/BA deadlock with rtnl_lock().
  24. So the strategy needs to be that where we prepare the DPMAC by calling
  25. dpaa2_mac_connect(), and only make priv->mac point to it once it's fully
  26. prepared. This ensures that the writer side critical section has the
  27. absolute minimum surface it can.
  28. The reverse strategy is adopted in the dpaa2_eth_disconnect_mac() code
  29. path. This makes sure that priv->mac is NULL when we start tearing down
  30. the DPMAC that we disconnected from, and concurrent code will simply not
  31. see it.
  32. No locking changes in this patch (concurrent code is still blocked by
  33. the rtnl_mutex).
  34. Signed-off-by: Vladimir Oltean <[email protected]>
  35. Reviewed-by: Ioana Ciornei <[email protected]>
  36. Tested-by: Ioana Ciornei <[email protected]>
  37. Signed-off-by: Paolo Abeni <[email protected]>
  38. ---
  39. .../net/ethernet/freescale/dpaa2/dpaa2-eth.c | 21 +++++++++++--------
  40. 1 file changed, 12 insertions(+), 9 deletions(-)
  41. --- a/drivers/net/ethernet/freescale/dpaa2/dpaa2-eth.c
  42. +++ b/drivers/net/ethernet/freescale/dpaa2/dpaa2-eth.c
  43. @@ -4444,9 +4444,8 @@ static int dpaa2_eth_connect_mac(struct
  44. err = dpaa2_mac_open(mac);
  45. if (err)
  46. goto err_free_mac;
  47. - priv->mac = mac;
  48. - if (dpaa2_eth_is_type_phy(priv)) {
  49. + if (dpaa2_mac_is_type_phy(mac)) {
  50. err = dpaa2_mac_connect(mac);
  51. if (err && err != -EPROBE_DEFER)
  52. netdev_err(priv->net_dev, "Error connecting to the MAC endpoint: %pe",
  53. @@ -4455,11 +4454,12 @@ static int dpaa2_eth_connect_mac(struct
  54. goto err_close_mac;
  55. }
  56. + priv->mac = mac;
  57. +
  58. return 0;
  59. err_close_mac:
  60. dpaa2_mac_close(mac);
  61. - priv->mac = NULL;
  62. err_free_mac:
  63. kfree(mac);
  64. return err;
  65. @@ -4467,15 +4467,18 @@ err_free_mac:
  66. static void dpaa2_eth_disconnect_mac(struct dpaa2_eth_priv *priv)
  67. {
  68. - if (dpaa2_eth_is_type_phy(priv))
  69. - dpaa2_mac_disconnect(priv->mac);
  70. + struct dpaa2_mac *mac = priv->mac;
  71. +
  72. + priv->mac = NULL;
  73. - if (!dpaa2_eth_has_mac(priv))
  74. + if (!mac)
  75. return;
  76. - dpaa2_mac_close(priv->mac);
  77. - kfree(priv->mac);
  78. - priv->mac = NULL;
  79. + if (dpaa2_mac_is_type_phy(mac))
  80. + dpaa2_mac_disconnect(mac);
  81. +
  82. + dpaa2_mac_close(mac);
  83. + kfree(mac);
  84. }
  85. static irqreturn_t dpni_irq0_handler_thread(int irq_num, void *arg)