History

Drivers: hv: vmbus: Fix race condition with new ring_buffer_info mutex

Fix a race condition that can result in a ring buffer pointer being set to null while a "_show" function is reading the ring buffer's data. This problem was discussed here: https://lkml.org/lkml/2018/10/18/779 To fix the race condition, add a new mutex lock to the "hv_ring_buffer_info" struct. Add a new function, "hv_ringbuffer_pre_init()", where a channel's inbound and outbound ring_buffer_info mutex locks are initialized. Acquire/release the locks in the "hv_ringbuffer_cleanup()" function, which is where the ring buffer pointers are set to null. Acquire/release the locks in the four channel-level "_show" functions that access ring buffer data. Remove the "const" qualifier from the "vmbus_channel" parameter and the "rbi" variable of the channel-level "_show" functions so that the locks can be acquired/released in these functions. Acquire/release the locks in hv_ringbuffer_get_debuginfo(). Remove the "const" qualifier from the "hv_ring_buffer_info" parameter so that the locks can be acquired/released in this function.

Kimberly Brown committed 7 years ago

Commit 14948e31 parent 4713eb75 files · +79 −32Browse files

Unified · Split

5 files
  1. drivers/hv/channel_mgmt.c +2 −0
  2. drivers/hv/hyperv_vmbus.h +1 −0
  3. drivers/hv/ring_buffer.c +17 −2
  4. drivers/hv/vmbus_drv.c +53 −29
  5. include/linux/hyperv.h +6 −1
drivers/hv/channel_mgmt.c+2−0
@@ -336,6 +336,8 @@ static struct vmbus_channel *alloc_channel(void) · 335 unmodified lines
336336 tasklet_init(&channel->callback_event,
337337 vmbus_on_event, (unsigned long)channel);
338338
339+ hv_ringbuffer_pre_init(channel);
340+
339341 return channel;
340342 }
341343
drivers/hv/hyperv_vmbus.h+1−0
@@ -193,6 +193,7 @@ extern void hv_synic_clockevents_cleanup(void); · 192 unmodified lines
193193
194194 /* Interface */
195195
196+void hv_ringbuffer_pre_init(struct vmbus_channel *channel);
196197
197198 int hv_ringbuffer_init(struct hv_ring_buffer_info *ring_info,
198199 struct page *pages, u32 pagecnt);
drivers/hv/ring_buffer.c+17−2
@@ -166,14 +166,18 @@ hv_get_ringbuffer_availbytes(const struct hv_ring_buffer_info *rbi, · 165 unmodified lines
166166 }
167167
168168 /* Get various debug metrics for the specified ring buffer. */
169−int hv_ringbuffer_get_debuginfo(const struct hv_ring_buffer_info *ring_info,
169+int hv_ringbuffer_get_debuginfo(struct hv_ring_buffer_info *ring_info,
170170 struct hv_ring_buffer_debug_info *debug_info)
171171 {
172172 u32 bytes_avail_towrite;
173173 u32 bytes_avail_toread;
174174
175− if (!ring_info->ring_buffer)
175+ mutex_lock(&ring_info->ring_buffer_mutex);
176+
177+ if (!ring_info->ring_buffer) {
178+ mutex_unlock(&ring_info->ring_buffer_mutex);
176179 return -EINVAL;
180+ }
177181
178182 hv_get_ringbuffer_availbytes(ring_info,
179183 &bytes_avail_toread,
@@ -184,10 +188,19 @@ int hv_ringbuffer_get_debuginfo(const struct hv_ring_buffer_info *ring_info, · 4 unmodified lines
184188 debug_info->current_write_index = ring_info->ring_buffer->write_index;
185189 debug_info->current_interrupt_mask
186190 = ring_info->ring_buffer->interrupt_mask;
191+ mutex_unlock(&ring_info->ring_buffer_mutex);
192+
187193 return 0;
188194 }
189195 EXPORT_SYMBOL_GPL(hv_ringbuffer_get_debuginfo);
190196
197+/* Initialize a channel's ring buffer info mutex locks */
198+void hv_ringbuffer_pre_init(struct vmbus_channel *channel)
199+{
200+ mutex_init(&channel->inbound.ring_buffer_mutex);
201+ mutex_init(&channel->outbound.ring_buffer_mutex);
202+}
203+
191204 /* Initialize the ring buffer. */
192205 int hv_ringbuffer_init(struct hv_ring_buffer_info *ring_info,
193206 struct page *pages, u32 page_cnt)
@@ -240,8 +253,10 @@ int hv_ringbuffer_init(struct hv_ring_buffer_info *ring_info, · 46 unmodified lines
240253 /* Cleanup the ring buffer. */
241254 void hv_ringbuffer_cleanup(struct hv_ring_buffer_info *ring_info)
242255 {
256+ mutex_lock(&ring_info->ring_buffer_mutex);
243257 vunmap(ring_info->ring_buffer);
244258 ring_info->ring_buffer = NULL;
259+ mutex_unlock(&ring_info->ring_buffer_mutex);
245260 }
246261
247262 /* Write to the ring buffer. */
drivers/hv/vmbus_drv.c+53−29
@@ -1410,7 +1410,7 @@ static void vmbus_chan_release(struct kobject *kobj) · 1409 unmodified lines
14101410
14111411 struct vmbus_chan_attribute {
14121412 struct attribute attr;
1413− ssize_t (*show)(const struct vmbus_channel *chan, char *buf);
1413+ ssize_t (*show)(struct vmbus_channel *chan, char *buf);
14141414 ssize_t (*store)(struct vmbus_channel *chan,
14151415 const char *buf, size_t count);
14161416 };
@@ -1429,7 +1429,7 @@ static ssize_t vmbus_chan_attr_show(struct kobject *kobj, · 12 unmodified lines
14291429 {
14301430 const struct vmbus_chan_attribute *attribute
14311431 = container_of(attr, struct vmbus_chan_attribute, attr);
1432− const struct vmbus_channel *chan
1432+ struct vmbus_channel *chan
14331433 = container_of(kobj, struct vmbus_channel, kobj);
14341434
14351435 if (!attribute->show)
@@ -1442,57 +1442,81 @@ static const struct sysfs_ops vmbus_chan_sysfs_ops = { · 6 unmodified lines
14421442 .show = vmbus_chan_attr_show,
14431443 };
14441444
1445−static ssize_t out_mask_show(const struct vmbus_channel *channel, char *buf)
1445+static ssize_t out_mask_show(struct vmbus_channel *channel, char *buf)
14461446 {
1447− const struct hv_ring_buffer_info *rbi = &channel->outbound;
1447+ struct hv_ring_buffer_info *rbi = &channel->outbound;
1448+ ssize_t ret;
14481449
1449− if (!rbi->ring_buffer)
1450+ mutex_lock(&rbi->ring_buffer_mutex);
1451+ if (!rbi->ring_buffer) {
1452+ mutex_unlock(&rbi->ring_buffer_mutex);
14501453 return -EINVAL;
1454+ }
14511455
1452− return sprintf(buf, "%u\n", rbi->ring_buffer->interrupt_mask);
1456+ ret = sprintf(buf, "%u\n", rbi->ring_buffer->interrupt_mask);
1457+ mutex_unlock(&rbi->ring_buffer_mutex);
1458+ return ret;
14531459 }
14541460 static VMBUS_CHAN_ATTR_RO(out_mask);
14551461
1456−static ssize_t in_mask_show(const struct vmbus_channel *channel, char *buf)
1462+static ssize_t in_mask_show(struct vmbus_channel *channel, char *buf)
14571463 {
1458− const struct hv_ring_buffer_info *rbi = &channel->inbound;
1464+ struct hv_ring_buffer_info *rbi = &channel->inbound;
1465+ ssize_t ret;
14591466
1460− if (!rbi->ring_buffer)
1467+ mutex_lock(&rbi->ring_buffer_mutex);
1468+ if (!rbi->ring_buffer) {
1469+ mutex_unlock(&rbi->ring_buffer_mutex);
14611470 return -EINVAL;
1471+ }
14621472
1463− return sprintf(buf, "%u\n", rbi->ring_buffer->interrupt_mask);
1473+ ret = sprintf(buf, "%u\n", rbi->ring_buffer->interrupt_mask);
1474+ mutex_unlock(&rbi->ring_buffer_mutex);
1475+ return ret;
14641476 }
14651477 static VMBUS_CHAN_ATTR_RO(in_mask);
14661478
1467−static ssize_t read_avail_show(const struct vmbus_channel *channel, char *buf)
1479+static ssize_t read_avail_show(struct vmbus_channel *channel, char *buf)
14681480 {
1469− const struct hv_ring_buffer_info *rbi = &channel->inbound;
1481+ struct hv_ring_buffer_info *rbi = &channel->inbound;
1482+ ssize_t ret;
14701483
1471− if (!rbi->ring_buffer)
1484+ mutex_lock(&rbi->ring_buffer_mutex);
1485+ if (!rbi->ring_buffer) {
1486+ mutex_unlock(&rbi->ring_buffer_mutex);
14721487 return -EINVAL;
1488+ }
14731489
1474− return sprintf(buf, "%u\n", hv_get_bytes_to_read(rbi));
1490+ ret = sprintf(buf, "%u\n", hv_get_bytes_to_read(rbi));
1491+ mutex_unlock(&rbi->ring_buffer_mutex);
1492+ return ret;
14751493 }
14761494 static VMBUS_CHAN_ATTR_RO(read_avail);
14771495
1478−static ssize_t write_avail_show(const struct vmbus_channel *channel, char *buf)
1496+static ssize_t write_avail_show(struct vmbus_channel *channel, char *buf)
14791497 {
1480− const struct hv_ring_buffer_info *rbi = &channel->outbound;
1498+ struct hv_ring_buffer_info *rbi = &channel->outbound;
1499+ ssize_t ret;
14811500
1482− if (!rbi->ring_buffer)
1501+ mutex_lock(&rbi->ring_buffer_mutex);
1502+ if (!rbi->ring_buffer) {
1503+ mutex_unlock(&rbi->ring_buffer_mutex);
14831504 return -EINVAL;
1505+ }
14841506
1485− return sprintf(buf, "%u\n", hv_get_bytes_to_write(rbi));
1507+ ret = sprintf(buf, "%u\n", hv_get_bytes_to_write(rbi));
1508+ mutex_unlock(&rbi->ring_buffer_mutex);
1509+ return ret;
14861510 }
14871511 static VMBUS_CHAN_ATTR_RO(write_avail);
14881512
1489−static ssize_t show_target_cpu(const struct vmbus_channel *channel, char *buf)
1513+static ssize_t show_target_cpu(struct vmbus_channel *channel, char *buf)
14901514 {
14911515 return sprintf(buf, "%u\n", channel->target_cpu);
14921516 }
14931517 static VMBUS_CHAN_ATTR(cpu, S_IRUGO, show_target_cpu, NULL);
14941518
1495−static ssize_t channel_pending_show(const struct vmbus_channel *channel,
1519+static ssize_t channel_pending_show(struct vmbus_channel *channel,
14961520 char *buf)
14971521 {
14981522 return sprintf(buf, "%d\n",
@@ -1501,7 +1525,7 @@ static ssize_t channel_pending_show(const struct vmbus_channel *channel, · 2 unmodified lines
15011525 }
15021526 static VMBUS_CHAN_ATTR(pending, S_IRUGO, channel_pending_show, NULL);
15031527
1504−static ssize_t channel_latency_show(const struct vmbus_channel *channel,
1528+static ssize_t channel_latency_show(struct vmbus_channel *channel,
15051529 char *buf)
15061530 {
15071531 return sprintf(buf, "%d\n",
@@ -1510,19 +1534,19 @@ static ssize_t channel_latency_show(const struct vmbus_channel *channel, · 2 unmodified lines
15101534 }
15111535 static VMBUS_CHAN_ATTR(latency, S_IRUGO, channel_latency_show, NULL);
15121536
1513−static ssize_t channel_interrupts_show(const struct vmbus_channel *channel, char *buf)
1537+static ssize_t channel_interrupts_show(struct vmbus_channel *channel, char *buf)
15141538 {
15151539 return sprintf(buf, "%llu\n", channel->interrupts);
15161540 }
15171541 static VMBUS_CHAN_ATTR(interrupts, S_IRUGO, channel_interrupts_show, NULL);
15181542
1519−static ssize_t channel_events_show(const struct vmbus_channel *channel, char *buf)
1543+static ssize_t channel_events_show(struct vmbus_channel *channel, char *buf)
15201544 {
15211545 return sprintf(buf, "%llu\n", channel->sig_events);
15221546 }
15231547 static VMBUS_CHAN_ATTR(events, S_IRUGO, channel_events_show, NULL);
15241548
1525−static ssize_t channel_intr_in_full_show(const struct vmbus_channel *channel,
1549+static ssize_t channel_intr_in_full_show(struct vmbus_channel *channel,
15261550 char *buf)
15271551 {
15281552 return sprintf(buf, "%llu\n",
@@ -1530,7 +1554,7 @@ static ssize_t channel_intr_in_full_show(const struct vmbus_channel *channel, · 1 unmodified line
15301554 }
15311555 static VMBUS_CHAN_ATTR(intr_in_full, 0444, channel_intr_in_full_show, NULL);
15321556
1533−static ssize_t channel_intr_out_empty_show(const struct vmbus_channel *channel,
1557+static ssize_t channel_intr_out_empty_show(struct vmbus_channel *channel,
15341558 char *buf)
15351559 {
15361560 return sprintf(buf, "%llu\n",
@@ -1538,7 +1562,7 @@ static ssize_t channel_intr_out_empty_show(const struct vmbus_channel *channel, · 1 unmodified line
15381562 }
15391563 static VMBUS_CHAN_ATTR(intr_out_empty, 0444, channel_intr_out_empty_show, NULL);
15401564
1541−static ssize_t channel_out_full_first_show(const struct vmbus_channel *channel,
1565+static ssize_t channel_out_full_first_show(struct vmbus_channel *channel,
15421566 char *buf)
15431567 {
15441568 return sprintf(buf, "%llu\n",
@@ -1546,7 +1570,7 @@ static ssize_t channel_out_full_first_show(const struct vmbus_channel *channel, · 1 unmodified line
15461570 }
15471571 static VMBUS_CHAN_ATTR(out_full_first, 0444, channel_out_full_first_show, NULL);
15481572
1549−static ssize_t channel_out_full_total_show(const struct vmbus_channel *channel,
1573+static ssize_t channel_out_full_total_show(struct vmbus_channel *channel,
15501574 char *buf)
15511575 {
15521576 return sprintf(buf, "%llu\n",
@@ -1554,14 +1578,14 @@ static ssize_t channel_out_full_total_show(const struct vmbus_channel *channel, · 1 unmodified line
15541578 }
15551579 static VMBUS_CHAN_ATTR(out_full_total, 0444, channel_out_full_total_show, NULL);
15561580
1557−static ssize_t subchannel_monitor_id_show(const struct vmbus_channel *channel,
1581+static ssize_t subchannel_monitor_id_show(struct vmbus_channel *channel,
15581582 char *buf)
15591583 {
15601584 return sprintf(buf, "%u\n", channel->offermsg.monitorid);
15611585 }
15621586 static VMBUS_CHAN_ATTR(monitor_id, S_IRUGO, subchannel_monitor_id_show, NULL);
15631587
1564−static ssize_t subchannel_id_show(const struct vmbus_channel *channel,
1588+static ssize_t subchannel_id_show(struct vmbus_channel *channel,
15651589 char *buf)
15661590 {
15671591 return sprintf(buf, "%u\n",
include/linux/hyperv.h+6−1
@@ -141,6 +141,11 @@ struct hv_ring_buffer_info { · 140 unmodified lines
141141
142142 u32 ring_datasize; /* < ring_size */
143143 u32 priv_read_index;
144+ /*
145+ * The ring buffer mutex lock. This lock prevents the ring buffer from
146+ * being freed while the ring buffer is being accessed.
147+ */
148+ struct mutex ring_buffer_mutex;
144149 };
145150
146151
@@ -1206,7 +1211,7 @@ struct hv_ring_buffer_debug_info { · 1059 unmodified lines
12061211 };
12071212
12081213
1209−int hv_ringbuffer_get_debuginfo(const struct hv_ring_buffer_info *ring_info,
1214+int hv_ringbuffer_get_debuginfo(struct hv_ring_buffer_info *ring_info,
12101215 struct hv_ring_buffer_debug_info *debug_info);
12111216
12121217 /* Vmbus interface */

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t