aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRonald Tschalär <ronald@innovation.ch>2017-10-26 01:14:53 -0400
committerMarcel Holtmann <marcel@holtmann.org>2017-10-29 09:03:28 -0400
commit67d2f8781b9f00d1089aafcfa3dc09fcd0f343e2 (patch)
tree5e27a45f576578e0e464c83231ad5e3ecdc4696e
parentfac72b243cc789bb209e6eca824919b42d98cfe2 (diff)
Bluetooth: hci_ldisc: Allow sleeping while proto locks are held.
Commit dec2c92880cc5435381d50e3045ef018a762a917 ("Bluetooth: hci_ldisc: Use rwlocking to avoid closing proto races") introduced locks in hci_ldisc that are held while calling the proto functions. These locks are rwlock's, and hence do not allow sleeping while they are held. However, the proto functions that hci_bcm registers use mutexes and hence need to be able to sleep. In more detail: hci_uart_tty_receive() and hci_uart_dequeue() both acquire the rwlock, after which they call proto->recv() and proto->dequeue(), respectively. In the case of hci_bcm these point to bcm_recv() and bcm_dequeue(). The latter both acquire the bcm_device_lock, which is a mutex, so doing so results in a call to might_sleep(). But since we're holding a rwlock in hci_ldisc, that results in the following BUG (this for the dequeue case - a similar one for the receive case is omitted for brevity): BUG: sleeping function called from invalid context at kernel/locking/mutex.c in_atomic(): 1, irqs_disabled(): 0, pid: 7303, name: kworker/7:3 INFO: lockdep is turned off. CPU: 7 PID: 7303 Comm: kworker/7:3 Tainted: G W OE 4.13.2+ #17 Hardware name: Apple Inc. MacBookPro13,3/Mac-A5C67F76ED83108C, BIOS MBP133.8 Workqueue: events hci_uart_write_work [hci_uart] Call Trace: dump_stack+0x8e/0xd6 ___might_sleep+0x164/0x250 __might_sleep+0x4a/0x80 __mutex_lock+0x59/0xa00 ? lock_acquire+0xa3/0x1f0 ? lock_acquire+0xa3/0x1f0 ? hci_uart_write_work+0xd3/0x160 [hci_uart] mutex_lock_nested+0x1b/0x20 ? mutex_lock_nested+0x1b/0x20 bcm_dequeue+0x21/0xc0 [hci_uart] hci_uart_write_work+0xe6/0x160 [hci_uart] process_one_work+0x253/0x6a0 worker_thread+0x4d/0x3b0 kthread+0x133/0x150 We can't replace the mutex in hci_bcm, because there are other calls there that might sleep. Therefore this replaces the rwlock's in hci_ldisc with rw_semaphore's (which allow sleeping). This is a safer approach anyway as it reduces the restrictions on the proto callbacks. Also, because acquiring write-lock is very rare compared to acquiring the read-lock, the percpu variant of rw_semaphore is used. Lastly, because hci_uart_tx_wakeup() may be called from an IRQ context, we can't block (sleep) while trying acquire the read lock there, so we use the trylock variant. Signed-off-by: Ronald Tschalär <ronald@innovation.ch> Reviewed-by: Lukas Wunner <lukas@wunner.de> Signed-off-by: Marcel Holtmann <marcel@holtmann.org>
-rw-r--r--drivers/bluetooth/hci_ldisc.c38
-rw-r--r--drivers/bluetooth/hci_uart.h2
2 files changed, 23 insertions, 17 deletions
diff --git a/drivers/bluetooth/hci_ldisc.c b/drivers/bluetooth/hci_ldisc.c
index eec95019f15c..31def781a562 100644
--- a/drivers/bluetooth/hci_ldisc.c
+++ b/drivers/bluetooth/hci_ldisc.c
@@ -115,12 +115,12 @@ static inline struct sk_buff *hci_uart_dequeue(struct hci_uart *hu)
115 struct sk_buff *skb = hu->tx_skb; 115 struct sk_buff *skb = hu->tx_skb;
116 116
117 if (!skb) { 117 if (!skb) {
118 read_lock(&hu->proto_lock); 118 percpu_down_read(&hu->proto_lock);
119 119
120 if (test_bit(HCI_UART_PROTO_READY, &hu->flags)) 120 if (test_bit(HCI_UART_PROTO_READY, &hu->flags))
121 skb = hu->proto->dequeue(hu); 121 skb = hu->proto->dequeue(hu);
122 122
123 read_unlock(&hu->proto_lock); 123 percpu_up_read(&hu->proto_lock);
124 } else { 124 } else {
125 hu->tx_skb = NULL; 125 hu->tx_skb = NULL;
126 } 126 }
@@ -130,7 +130,14 @@ static inline struct sk_buff *hci_uart_dequeue(struct hci_uart *hu)
130 130
131int hci_uart_tx_wakeup(struct hci_uart *hu) 131int hci_uart_tx_wakeup(struct hci_uart *hu)
132{ 132{
133 read_lock(&hu->proto_lock); 133 /* This may be called in an IRQ context, so we can't sleep. Therefore
134 * we try to acquire the lock only, and if that fails we assume the
135 * tty is being closed because that is the only time the write lock is
136 * acquired. If, however, at some point in the future the write lock
137 * is also acquired in other situations, then this must be revisited.
138 */
139 if (!percpu_down_read_trylock(&hu->proto_lock))
140 return 0;
134 141
135 if (!test_bit(HCI_UART_PROTO_READY, &hu->flags)) 142 if (!test_bit(HCI_UART_PROTO_READY, &hu->flags))
136 goto no_schedule; 143 goto no_schedule;
@@ -145,7 +152,7 @@ int hci_uart_tx_wakeup(struct hci_uart *hu)
145 schedule_work(&hu->write_work); 152 schedule_work(&hu->write_work);
146 153
147no_schedule: 154no_schedule:
148 read_unlock(&hu->proto_lock); 155 percpu_up_read(&hu->proto_lock);
149 156
150 return 0; 157 return 0;
151} 158}
@@ -247,12 +254,12 @@ static int hci_uart_flush(struct hci_dev *hdev)
247 tty_ldisc_flush(tty); 254 tty_ldisc_flush(tty);
248 tty_driver_flush_buffer(tty); 255 tty_driver_flush_buffer(tty);
249 256
250 read_lock(&hu->proto_lock); 257 percpu_down_read(&hu->proto_lock);
251 258
252 if (test_bit(HCI_UART_PROTO_READY, &hu->flags)) 259 if (test_bit(HCI_UART_PROTO_READY, &hu->flags))
253 hu->proto->flush(hu); 260 hu->proto->flush(hu);
254 261
255 read_unlock(&hu->proto_lock); 262 percpu_up_read(&hu->proto_lock);
256 263
257 return 0; 264 return 0;
258} 265}
@@ -275,15 +282,15 @@ static int hci_uart_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
275 BT_DBG("%s: type %d len %d", hdev->name, hci_skb_pkt_type(skb), 282 BT_DBG("%s: type %d len %d", hdev->name, hci_skb_pkt_type(skb),
276 skb->len); 283 skb->len);
277 284
278 read_lock(&hu->proto_lock); 285 percpu_down_read(&hu->proto_lock);
279 286
280 if (!test_bit(HCI_UART_PROTO_READY, &hu->flags)) { 287 if (!test_bit(HCI_UART_PROTO_READY, &hu->flags)) {
281 read_unlock(&hu->proto_lock); 288 percpu_up_read(&hu->proto_lock);
282 return -EUNATCH; 289 return -EUNATCH;
283 } 290 }
284 291
285 hu->proto->enqueue(hu, skb); 292 hu->proto->enqueue(hu, skb);
286 read_unlock(&hu->proto_lock); 293 percpu_up_read(&hu->proto_lock);
287 294
288 hci_uart_tx_wakeup(hu); 295 hci_uart_tx_wakeup(hu);
289 296
@@ -486,7 +493,7 @@ static int hci_uart_tty_open(struct tty_struct *tty)
486 INIT_WORK(&hu->init_ready, hci_uart_init_work); 493 INIT_WORK(&hu->init_ready, hci_uart_init_work);
487 INIT_WORK(&hu->write_work, hci_uart_write_work); 494 INIT_WORK(&hu->write_work, hci_uart_write_work);
488 495
489 rwlock_init(&hu->proto_lock); 496 percpu_init_rwsem(&hu->proto_lock);
490 497
491 /* Flush any pending characters in the driver */ 498 /* Flush any pending characters in the driver */
492 tty_driver_flush_buffer(tty); 499 tty_driver_flush_buffer(tty);
@@ -503,7 +510,6 @@ static void hci_uart_tty_close(struct tty_struct *tty)
503{ 510{
504 struct hci_uart *hu = tty->disc_data; 511 struct hci_uart *hu = tty->disc_data;
505 struct hci_dev *hdev; 512 struct hci_dev *hdev;
506 unsigned long flags;
507 513
508 BT_DBG("tty %p", tty); 514 BT_DBG("tty %p", tty);
509 515
@@ -520,9 +526,9 @@ static void hci_uart_tty_close(struct tty_struct *tty)
520 cancel_work_sync(&hu->write_work); 526 cancel_work_sync(&hu->write_work);
521 527
522 if (test_bit(HCI_UART_PROTO_READY, &hu->flags)) { 528 if (test_bit(HCI_UART_PROTO_READY, &hu->flags)) {
523 write_lock_irqsave(&hu->proto_lock, flags); 529 percpu_down_write(&hu->proto_lock);
524 clear_bit(HCI_UART_PROTO_READY, &hu->flags); 530 clear_bit(HCI_UART_PROTO_READY, &hu->flags);
525 write_unlock_irqrestore(&hu->proto_lock, flags); 531 percpu_up_write(&hu->proto_lock);
526 532
527 if (hdev) { 533 if (hdev) {
528 if (test_bit(HCI_UART_REGISTERED, &hu->flags)) 534 if (test_bit(HCI_UART_REGISTERED, &hu->flags))
@@ -582,10 +588,10 @@ static void hci_uart_tty_receive(struct tty_struct *tty, const u8 *data,
582 if (!hu || tty != hu->tty) 588 if (!hu || tty != hu->tty)
583 return; 589 return;
584 590
585 read_lock(&hu->proto_lock); 591 percpu_down_read(&hu->proto_lock);
586 592
587 if (!test_bit(HCI_UART_PROTO_READY, &hu->flags)) { 593 if (!test_bit(HCI_UART_PROTO_READY, &hu->flags)) {
588 read_unlock(&hu->proto_lock); 594 percpu_up_read(&hu->proto_lock);
589 return; 595 return;
590 } 596 }
591 597
@@ -593,7 +599,7 @@ static void hci_uart_tty_receive(struct tty_struct *tty, const u8 *data,
593 * tty caller 599 * tty caller
594 */ 600 */
595 hu->proto->recv(hu, data, count); 601 hu->proto->recv(hu, data, count);
596 read_unlock(&hu->proto_lock); 602 percpu_up_read(&hu->proto_lock);
597 603
598 if (hu->hdev) 604 if (hu->hdev)
599 hu->hdev->stat.byte_rx += count; 605 hu->hdev->stat.byte_rx += count;
diff --git a/drivers/bluetooth/hci_uart.h b/drivers/bluetooth/hci_uart.h
index d9cd95d81149..66e8c68e4607 100644
--- a/drivers/bluetooth/hci_uart.h
+++ b/drivers/bluetooth/hci_uart.h
@@ -87,7 +87,7 @@ struct hci_uart {
87 struct work_struct write_work; 87 struct work_struct write_work;
88 88
89 const struct hci_uart_proto *proto; 89 const struct hci_uart_proto *proto;
90 rwlock_t proto_lock; /* Stop work for proto close */ 90 struct percpu_rw_semaphore proto_lock; /* Stop work for proto close */
91 void *priv; 91 void *priv;
92 92
93 struct sk_buff *tx_skb; 93 struct sk_buff *tx_skb;