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
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.
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.
15 Reviewed-by: Florian Fainelli <f.fainelli@gmail.com>
16 Signed-off-by: Russell King <rmk+kernel@arm.linux.org.uk>
18 drivers/net/phy/fixed_phy.c | 28 +++++++++++++++++++++-------
19 1 file changed, 21 insertions(+), 7 deletions(-)
21 --- a/drivers/net/phy/fixed_phy.c
22 +++ b/drivers/net/phy/fixed_phy.c
24 #include <linux/slab.h>
26 #include <linux/gpio.h>
27 +#include <linux/seqlock.h>
31 @@ -35,6 +36,7 @@ struct fixed_mdio_bus {
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
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,
47 - fixed_phy_update(fp);
49 - return swphy_read_reg(reg_num, &fp->status);
50 + struct fixed_phy_status state;
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,
59 + fixed_phy_update(fp);
62 + } while (read_seqcount_retry(&fp->seqcount, s));
64 + return swphy_read_reg(reg_num, &state);
68 @@ -117,6 +127,7 @@ int fixed_phy_update_state(struct phy_de
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
76 @@ -126,6 +137,7 @@ int fixed_phy_update_state(struct phy_de
80 + write_seqcount_end(&fp->seqcount);
84 @@ -150,6 +162,8 @@ int fixed_phy_add(unsigned int irq, int
88 + seqcount_init(&fp->seqcount);
90 fmb->irqs[phy_addr] = irq;