Skip to contentSkip to conversation

mt7601u: make write with mask access atomic

PATCH Superseded Lorenzo Bianconi 3 participants 9 messages Feb 15, 2018 – Feb 28, 2018 1 message not in the archive

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

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

#1 lore raw

PATCH 1/1 mt7601u: make write with mask access atomic

Introduce __mt7601u_rr and __mt7601u_vendor_single_wr routines in order to make mt7601u_rmw and mt7601u_rmc atomic since it is possible that read and write accesses of mt7601u_rmw/mt7601u_rmc can be interleaved with a different write operation on the same register. Moreover move write trace point in __mt7601u_vendor_single_wr

---
 drivers/net/wireless/mediatek/mt7601u/mt7601u.h |  3 +-
 drivers/net/wireless/mediatek/mt7601u/usb.c     | 52 ++++++++++++++++++-------
 2 files changed, 39 insertions(+), 16 deletions(-)
drivers/net/wireless/mediatek/mt7601u/mt7601u.h +2 -1
index c7ec40475a5f..9233744451a9 100644
@@ -147,7 +147,8 @@ enum {
  * @rx_lock:		protects @rx_q.
  * @con_mon_lock:	protects @ap_bssid, @bcn_*, @avg_rssi.
  * @mutex:		ensures exclusive access from mac80211 callbacks.
- * @vendor_req_mutex:	protects @vend_buf, ensures atomicity of split writes.
+ * @vendor_req_mutex:	protects @vend_buf, ensures atomicity of read/write
+ *			accesses
  * @reg_atomic_mutex:	ensures atomicity of indirect register accesses
  *			(accesses to RF and BBP).
  * @hw_atomic_mutex:	ensures exclusive access to HW during critical
drivers/net/wireless/mediatek/mt7601u/usb.c +37 -15

100 lines not on this page · raw message

Signature · 1 line
2.14.3
#2 lore raw
Quoted 7 lines · Lorenzo Bianconi

Introduce __mt7601u_rr and __mt7601u_vendor_single_wr routines in order to make mt7601u_rmw and mt7601u_rmc atomic since it is possible that read and write accesses of mt7601u_rmw/mt7601u_rmc can be interleaved with a different write operation on the same register. Moreover move write trace point in __mt7601u_vendor_single_wr

Could you provide an example of which accesses make it problematic? Is this fixing an actual bug?

#3 lore raw
Quoted 11 lines · Lorenzo Bianconi
Quoted 7 lines

Introduce __mt7601u_rr and __mt7601u_vendor_single_wr routines in order to make mt7601u_rmw and mt7601u_rmc atomic since it is possible that read and write accesses of mt7601u_rmw/mt7601u_rmc can be interleaved with a different write operation on the same register. Moreover move write trace point in __mt7601u_vendor_single_wr

Could you provide an example of which accesses make it problematic? Is this fixing an actual bug?

Hi Jakub,

it is not an issue I had experimented, I noticed a theoretical race reviewing the code.
AFAIU, based on the current implementation it is possible that mt7601u_rmw (with mt7601u_rr) loads data from given register but its store access (mt7601u_wr) is
preceded by another mt7601u_wr on the same register. In this case the value configured by
the first mt7601u_wr executed is overwritten by the second one (the store from mt7601u_rmw)
even if the first write is setting a different subfield respect to mt7601u_rmw.

Regards,
Lorenzo

#4 lore raw
Quoted 14 lines · Lorenzo Bianconi
Quoted 11 lines · Lorenzo Bianconi
Quoted 7 lines

Introduce __mt7601u_rr and __mt7601u_vendor_single_wr routines in order to make mt7601u_rmw and mt7601u_rmc atomic since it is possible that read and write accesses of mt7601u_rmw/mt7601u_rmc can be interleaved with a different write operation on the same register. Moreover move write trace point in __mt7601u_vendor_single_wr

Could you provide an example of which accesses make it problematic? Is this fixing an actual bug?

Hi Jakub,

Quoted 11 lines

it is not an issue I had experimented, I noticed a theoretical race reviewing the code.
AFAIU, based on the current implementation it is possible that mt7601u_rmw (with mt7601u_rr) loads data from given register but its store access (mt7601u_wr) is
preceded by another mt7601u_wr on the same register. In this case the value configured by
the first mt7601u_wr executed is overwritten by the second one (the store from mt7601u_rmw)
even if the first write is setting a different subfield respect to mt7601u_rmw.

Hm.. There should be no path in the driver where that could cause problems AFAIR. We have reg_atomic_mutex and hw_atomic_mutex to protect from concurrent access to the HW where they are possible. RMW operations are non-atomic by definition, it's supposed to work like PCIe register accesses would - 32bit reads/writes are atomic, but RMW is not.

So I'm not sure what to do with this patch. Doesn't seem necessary...

#5 lore raw
Quoted 27 lines · Jakub Kicinski
Quoted 14 lines · Lorenzo Bianconi
Quoted 11 lines · Lorenzo Bianconi
Quoted 7 lines

Introduce __mt7601u_rr and __mt7601u_vendor_single_wr routines in order to make mt7601u_rmw and mt7601u_rmc atomic since it is possible that read and write accesses of mt7601u_rmw/mt7601u_rmc can be interleaved with a different write operation on the same register. Moreover move write trace point in __mt7601u_vendor_single_wr

Could you provide an example of which accesses make it problematic? Is this fixing an actual bug?

Hi Jakub,

Quoted 11 lines

it is not an issue I had experimented, I noticed a theoretical race reviewing the code.
AFAIU, based on the current implementation it is possible that mt7601u_rmw (with mt7601u_rr) loads data from given register but its store access (mt7601u_wr) is
preceded by another mt7601u_wr on the same register. In this case the value configured by
the first mt7601u_wr executed is overwritten by the second one (the store from mt7601u_rmw)
even if the first write is setting a different subfield respect to mt7601u_rmw.

Quoted 6 lines

Hm.. There should be no path in the driver where that could cause problems AFAIR. We have reg_atomic_mutex and hw_atomic_mutex to protect from concurrent access to the HW where they are possible. RMW operations are non-atomic by definition, it's supposed to work like PCIe register accesses would - 32bit reads/writes are atomic, but RMW is not.

Yes, RMW accesses are non-atomic by default but since vendor_req_mutex mutex is already there (and grabbed for RMW operations), why not use it to make write with mask access atomic without adding complexity? Moreover it would be a micro-optimisation since vendor_req_mutex would be grabbed just once instead of twice

Quoted 2 lines

So I'm not sure what to do with this patch. Doesn't seem necessary...

It is just a trivial rework of locking in usb read/write accesses, not mandatory, so if you prefer we can just drop it

Regards,
Lorenzo

#6 lore raw
Quoted 14 lines · Lorenzo Bianconi
Quoted 6 lines

Hm.. There should be no path in the driver where that could cause problems AFAIR. We have reg_atomic_mutex and hw_atomic_mutex to protect from concurrent access to the HW where they are possible. RMW operations are non-atomic by definition, it's supposed to work like PCIe register accesses would - 32bit reads/writes are atomic, but RMW is not.

Yes, RMW accesses are non-atomic by default but since vendor_req_mutex mutex is already there (and grabbed for RMW operations), why not use it to make write with mask access atomic without adding complexity? Moreover it would be a micro-optimisation since vendor_req_mutex would be grabbed just once instead of twice

Quoted 1 line
Quoted 4 lines
Quoted 1 line

So I'm not sure what to do with this patch. Doesn't seem necessary...

It is just a trivial rework of locking in usb read/write accesses, not mandatory, so if you prefer we can just drop it

You make good points :) Could you please respin stating clearly in the commit message that this is not a bug fix, but a micro-optimization and may be useful in the future?

#7 lore raw
In reply to Lorenzo Bianconi
Quoted 7 lines · Lorenzo Bianconi
Quoted 7 lines · Lorenzo Bianconi
Quoted 6 lines

Introduce __mt7601u_rr and __mt7601u_vendor_single_wr routines in order to make mt7601u_rmw and mt7601u_rmc atomic since it is possible that read and write accesses of mt7601u_rmw/mt7601u_rmc can be interleaved with a different write operation on the same register. Moreover move write trace point in __mt7601u_vendor_single_wr

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

Could you provide an example of which accesses make it problematic? Is this fixing an actual bug?

it is not an issue I had experimented, I noticed a theoretical race reviewing the code.

The commit log should always answer the question "Why?". If you find a theoretical issue in the code document that clearly in the commit log. That helps to choose correct tree and sending to stable releases etc.

https://wireless.wiki.kernel.org/en/developers/documentation/submittingpatches#commit_log_does_not_answer_why

Signature · 1 line
Kalle Valo
#8 lore raw
Quoted 17 lines
Quoted 7 lines · Lorenzo Bianconi
Quoted 7 lines · Lorenzo Bianconi
Quoted 6 lines

Introduce __mt7601u_rr and __mt7601u_vendor_single_wr routines in order to make mt7601u_rmw and mt7601u_rmc atomic since it is possible that read and write accesses of mt7601u_rmw/mt7601u_rmc can be interleaved with a different write operation on the same register. Moreover move write trace point in __mt7601u_vendor_single_wr

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

Could you provide an example of which accesses make it problematic? Is this fixing an actual bug?

it is not an issue I had experimented, I noticed a theoretical race reviewing the code.

Quoted 8 lines

The commit log should always answer the question "Why?". If you find a theoretical issue in the code document that clearly in the commit log. That helps to choose correct tree and sending to stable releases etc.

https://wireless.wiki.kernel.org/en/developers/documentation/submittingpatches#commit_log_does_not_answer_why

--
Kalle Valo

Hi Kalle,

I have already sent a v2: https://patchwork.kernel.org/patch/10225849/
Is the commit message ok?

Regards,
Lorenzo

#9 lore raw
Quoted 17 lines · Lorenzo Bianconi
Quoted 17 lines
Quoted 14 lines · Lorenzo Bianconi
Quoted 11 lines · Lorenzo Bianconi
Quoted 7 lines

Introduce __mt7601u_rr and __mt7601u_vendor_single_wr routines in order to make mt7601u_rmw and mt7601u_rmc atomic since it is possible that read and write accesses of mt7601u_rmw/mt7601u_rmc can be interleaved with a different write operation on the same register. Moreover move write trace point in __mt7601u_vendor_single_wr

Could you provide an example of which accesses make it problematic? Is this fixing an actual bug?

it is not an issue I had experimented, I noticed a theoretical race reviewing the code.

Quoted 8 lines
Quoted 5 lines

The commit log should always answer the question "Why?". If you find a theoretical issue in the code document that clearly in the commit log. That helps to choose correct tree and sending to stable releases etc.

https://wireless.wiki.kernel.org/en/developers/documentation/submittingpatches#commit_log_does_not_answer_why

I have already sent a v2: https://patchwork.kernel.org/patch/10225849/
Is the commit message ok?

Yeah, I already applied it.

Signature · 1 line
Kalle Valo