124-phy-improve-safety-of-fixed-phy-MII-register-reading.patch 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. From c36739c3cfd277a4cc9820a29dd0f4b7fbac795b Mon Sep 17 00:00:00 2001
  2. From: Russell King <rmk+kernel@arm.linux.org.uk>
  3. Date: Sun, 20 Sep 2015 18:31:36 +0100
  4. Subject: [PATCH 713/744] phy: improve safety of fixed-phy MII register reading
  5. There is no prevention of a concurrent call to both fixed_mdio_read()
  6. and fixed_phy_update_state(), which can result in the state being
  7. modified while it's being inspected. Fix this by using a seqcount
  8. to detect modifications, and memcpy()ing the state.
  9. We remain slightly naughty here, calling link_update() and updating
  10. the link status within the read-side loop - which would need rework
  11. of the design to change.
  12. Reviewed-by: Florian Fainelli <f.fainelli@gmail.com>
  13. Signed-off-by: Russell King <rmk+kernel@arm.linux.org.uk>
  14. ---
  15. drivers/net/phy/fixed_phy.c | 28 +++++++++++++++++++++-------
  16. 1 file changed, 21 insertions(+), 7 deletions(-)
  17. --- a/drivers/net/phy/fixed_phy.c
  18. +++ b/drivers/net/phy/fixed_phy.c
  19. @@ -23,6 +23,7 @@
  20. #include <linux/slab.h>
  21. #include <linux/of.h>
  22. #include <linux/gpio.h>
  23. +#include <linux/seqlock.h>
  24. #include "swphy.h"
  25. @@ -35,6 +36,7 @@ struct fixed_mdio_bus {
  26. struct fixed_phy {
  27. int addr;
  28. struct phy_device *phydev;
  29. + seqcount_t seqcount;
  30. struct fixed_phy_status status;
  31. int (*link_update)(struct net_device *, struct fixed_phy_status *);
  32. struct list_head node;
  33. @@ -59,13 +61,21 @@ static int fixed_mdio_read(struct mii_bu
  34. list_for_each_entry(fp, &fmb->phys, node) {
  35. if (fp->addr == phy_addr) {
  36. - /* Issue callback if user registered it. */
  37. - if (fp->link_update) {
  38. - fp->link_update(fp->phydev->attached_dev,
  39. - &fp->status);
  40. - fixed_phy_update(fp);
  41. - }
  42. - return swphy_read_reg(reg_num, &fp->status);
  43. + struct fixed_phy_status state;
  44. + int s;
  45. +
  46. + do {
  47. + s = read_seqcount_begin(&fp->seqcount);
  48. + /* Issue callback if user registered it. */
  49. + if (fp->link_update) {
  50. + fp->link_update(fp->phydev->attached_dev,
  51. + &fp->status);
  52. + fixed_phy_update(fp);
  53. + }
  54. + state = fp->status;
  55. + } while (read_seqcount_retry(&fp->seqcount, s));
  56. +
  57. + return swphy_read_reg(reg_num, &state);
  58. }
  59. }
  60. @@ -117,6 +127,7 @@ int fixed_phy_update_state(struct phy_de
  61. list_for_each_entry(fp, &fmb->phys, node) {
  62. if (fp->addr == phydev->addr) {
  63. + write_seqcount_begin(&fp->seqcount);
  64. #define _UPD(x) if (changed->x) \
  65. fp->status.x = status->x
  66. _UPD(link);
  67. @@ -126,6 +137,7 @@ int fixed_phy_update_state(struct phy_de
  68. _UPD(asym_pause);
  69. #undef _UPD
  70. fixed_phy_update(fp);
  71. + write_seqcount_end(&fp->seqcount);
  72. return 0;
  73. }
  74. }
  75. @@ -150,6 +162,8 @@ int fixed_phy_add(unsigned int irq, int
  76. if (!fp)
  77. return -ENOMEM;
  78. + seqcount_init(&fp->seqcount);
  79. +
  80. fmb->irqs[phy_addr] = irq;
  81. fp->addr = phy_addr;