2
0

316-ath9k-fix-race-condition-in-enabling-disabling-IRQs.patch 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. From: Felix Fietkau <nbd@nbd.name>
  2. Date: Wed, 25 Jan 2017 15:10:37 +0100
  3. Subject: [PATCH] ath9k: fix race condition in enabling/disabling IRQs
  4. The code currently relies on refcounting to disable IRQs from within the
  5. IRQ handler and re-enabling them again after the tasklet has run.
  6. However, due to race conditions sometimes the IRQ handler might be
  7. called twice, or the tasklet may not run at all (if interrupted in the
  8. middle of a reset).
  9. This can cause nasty imbalances in the irq-disable refcount which will
  10. get the driver permanently stuck until the entire radio has been stopped
  11. and started again (ath_reset will not recover from this).
  12. Instead of using this fragile logic, change the code to ensure that
  13. running the irq handler during tasklet processing is safe, and leave the
  14. refcount untouched.
  15. Cc: stable@vger.kernel.org
  16. Signed-off-by: Felix Fietkau <nbd@nbd.name>
  17. ---
  18. --- a/drivers/net/wireless/ath/ath9k/ath9k.h
  19. +++ b/drivers/net/wireless/ath/ath9k/ath9k.h
  20. @@ -998,6 +998,7 @@ struct ath_softc {
  21. struct survey_info *cur_survey;
  22. struct survey_info survey[ATH9K_NUM_CHANNELS];
  23. + spinlock_t intr_lock;
  24. struct tasklet_struct intr_tq;
  25. struct tasklet_struct bcon_tasklet;
  26. struct ath_hw *sc_ah;
  27. --- a/drivers/net/wireless/ath/ath9k/init.c
  28. +++ b/drivers/net/wireless/ath/ath9k/init.c
  29. @@ -669,6 +669,7 @@ static int ath9k_init_softc(u16 devid, s
  30. common->bt_ant_diversity = 1;
  31. spin_lock_init(&common->cc_lock);
  32. + spin_lock_init(&sc->intr_lock);
  33. spin_lock_init(&sc->sc_serial_rw);
  34. spin_lock_init(&sc->sc_pm_lock);
  35. spin_lock_init(&sc->chan_lock);
  36. --- a/drivers/net/wireless/ath/ath9k/mac.c
  37. +++ b/drivers/net/wireless/ath/ath9k/mac.c
  38. @@ -810,21 +810,12 @@ void ath9k_hw_disable_interrupts(struct
  39. }
  40. EXPORT_SYMBOL(ath9k_hw_disable_interrupts);
  41. -void ath9k_hw_enable_interrupts(struct ath_hw *ah)
  42. +static void __ath9k_hw_enable_interrupts(struct ath_hw *ah)
  43. {
  44. struct ath_common *common = ath9k_hw_common(ah);
  45. u32 sync_default = AR_INTR_SYNC_DEFAULT;
  46. u32 async_mask;
  47. - if (!(ah->imask & ATH9K_INT_GLOBAL))
  48. - return;
  49. -
  50. - if (!atomic_inc_and_test(&ah->intr_ref_cnt)) {
  51. - ath_dbg(common, INTERRUPT, "Do not enable IER ref count %d\n",
  52. - atomic_read(&ah->intr_ref_cnt));
  53. - return;
  54. - }
  55. -
  56. if (AR_SREV_9340(ah) || AR_SREV_9550(ah) || AR_SREV_9531(ah) ||
  57. AR_SREV_9561(ah))
  58. sync_default &= ~AR_INTR_SYNC_HOST1_FATAL;
  59. @@ -846,6 +837,39 @@ void ath9k_hw_enable_interrupts(struct a
  60. ath_dbg(common, INTERRUPT, "AR_IMR 0x%x IER 0x%x\n",
  61. REG_READ(ah, AR_IMR), REG_READ(ah, AR_IER));
  62. }
  63. +
  64. +void ath9k_hw_resume_interrupts(struct ath_hw *ah)
  65. +{
  66. + struct ath_common *common = ath9k_hw_common(ah);
  67. +
  68. + if (!(ah->imask & ATH9K_INT_GLOBAL))
  69. + return;
  70. +
  71. + if (atomic_read(&ah->intr_ref_cnt) != 0) {
  72. + ath_dbg(common, INTERRUPT, "Do not enable IER ref count %d\n",
  73. + atomic_read(&ah->intr_ref_cnt));
  74. + return;
  75. + }
  76. +
  77. + __ath9k_hw_enable_interrupts(ah);
  78. +}
  79. +EXPORT_SYMBOL(ath9k_hw_resume_interrupts);
  80. +
  81. +void ath9k_hw_enable_interrupts(struct ath_hw *ah)
  82. +{
  83. + struct ath_common *common = ath9k_hw_common(ah);
  84. +
  85. + if (!(ah->imask & ATH9K_INT_GLOBAL))
  86. + return;
  87. +
  88. + if (!atomic_inc_and_test(&ah->intr_ref_cnt)) {
  89. + ath_dbg(common, INTERRUPT, "Do not enable IER ref count %d\n",
  90. + atomic_read(&ah->intr_ref_cnt));
  91. + return;
  92. + }
  93. +
  94. + __ath9k_hw_enable_interrupts(ah);
  95. +}
  96. EXPORT_SYMBOL(ath9k_hw_enable_interrupts);
  97. void ath9k_hw_set_interrupts(struct ath_hw *ah)
  98. --- a/drivers/net/wireless/ath/ath9k/mac.h
  99. +++ b/drivers/net/wireless/ath/ath9k/mac.h
  100. @@ -744,6 +744,7 @@ void ath9k_hw_set_interrupts(struct ath_
  101. void ath9k_hw_enable_interrupts(struct ath_hw *ah);
  102. void ath9k_hw_disable_interrupts(struct ath_hw *ah);
  103. void ath9k_hw_kill_interrupts(struct ath_hw *ah);
  104. +void ath9k_hw_resume_interrupts(struct ath_hw *ah);
  105. void ar9002_hw_attach_mac_ops(struct ath_hw *ah);
  106. --- a/drivers/net/wireless/ath/ath9k/main.c
  107. +++ b/drivers/net/wireless/ath/ath9k/main.c
  108. @@ -374,21 +374,20 @@ void ath9k_tasklet(unsigned long data)
  109. struct ath_common *common = ath9k_hw_common(ah);
  110. enum ath_reset_type type;
  111. unsigned long flags;
  112. - u32 status = sc->intrstatus;
  113. + u32 status;
  114. u32 rxmask;
  115. + spin_lock_irqsave(&sc->intr_lock, flags);
  116. + status = sc->intrstatus;
  117. + sc->intrstatus = 0;
  118. + spin_unlock_irqrestore(&sc->intr_lock, flags);
  119. +
  120. ath9k_ps_wakeup(sc);
  121. spin_lock(&sc->sc_pcu_lock);
  122. if (status & ATH9K_INT_FATAL) {
  123. type = RESET_TYPE_FATAL_INT;
  124. ath9k_queue_reset(sc, type);
  125. -
  126. - /*
  127. - * Increment the ref. counter here so that
  128. - * interrupts are enabled in the reset routine.
  129. - */
  130. - atomic_inc(&ah->intr_ref_cnt);
  131. ath_dbg(common, RESET, "FATAL: Skipping interrupts\n");
  132. goto out;
  133. }
  134. @@ -404,11 +403,6 @@ void ath9k_tasklet(unsigned long data)
  135. type = RESET_TYPE_BB_WATCHDOG;
  136. ath9k_queue_reset(sc, type);
  137. - /*
  138. - * Increment the ref. counter here so that
  139. - * interrupts are enabled in the reset routine.
  140. - */
  141. - atomic_inc(&ah->intr_ref_cnt);
  142. ath_dbg(common, RESET,
  143. "BB_WATCHDOG: Skipping interrupts\n");
  144. goto out;
  145. @@ -421,7 +415,6 @@ void ath9k_tasklet(unsigned long data)
  146. if ((sc->gtt_cnt >= MAX_GTT_CNT) && !ath9k_hw_check_alive(ah)) {
  147. type = RESET_TYPE_TX_GTT;
  148. ath9k_queue_reset(sc, type);
  149. - atomic_inc(&ah->intr_ref_cnt);
  150. ath_dbg(common, RESET,
  151. "GTT: Skipping interrupts\n");
  152. goto out;
  153. @@ -478,7 +471,7 @@ void ath9k_tasklet(unsigned long data)
  154. ath9k_btcoex_handle_interrupt(sc, status);
  155. /* re-enable hardware interrupt */
  156. - ath9k_hw_enable_interrupts(ah);
  157. + ath9k_hw_resume_interrupts(ah);
  158. out:
  159. spin_unlock(&sc->sc_pcu_lock);
  160. ath9k_ps_restore(sc);
  161. @@ -542,7 +535,9 @@ irqreturn_t ath_isr(int irq, void *dev)
  162. return IRQ_NONE;
  163. /* Cache the status */
  164. - sc->intrstatus = status;
  165. + spin_lock(&sc->intr_lock);
  166. + sc->intrstatus |= status;
  167. + spin_unlock(&sc->intr_lock);
  168. if (status & SCHED_INTR)
  169. sched = true;
  170. @@ -588,7 +583,7 @@ chip_reset:
  171. if (sched) {
  172. /* turn off every interrupt */
  173. - ath9k_hw_disable_interrupts(ah);
  174. + ath9k_hw_kill_interrupts(ah);
  175. tasklet_schedule(&sc->intr_tq);
  176. }