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