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
336 tasklet_init(&channel->callback_event,336 tasklet_init(&channel->callback_event,
337 vmbus_on_event, (unsigned long)channel);337 vmbus_on_event, (unsigned long)channel);
338 338
339 hv_ringbuffer_pre_init(channel);
340
339 return channel;341 return channel;
340}342}
341 343
drivers/hv/hyperv_vmbus.h+1−0
@@ -193,6 +193,7 @@ extern void hv_synic_clockevents_cleanup(void); · 192 unmodified lines
193 193
194/* Interface */194/* Interface */
195 195
196void hv_ringbuffer_pre_init(struct vmbus_channel *channel);
196 197
197int hv_ringbuffer_init(struct hv_ring_buffer_info *ring_info,198int hv_ringbuffer_init(struct hv_ring_buffer_info *ring_info,
198 struct page *pages, u32 pagecnt);199 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
166}166}
167 167
168/* Get various debug metrics for the specified ring buffer. */168/* Get various debug metrics for the specified ring buffer. */
169int hv_ringbuffer_get_debuginfo(const struct hv_ring_buffer_info *ring_info,169int hv_ringbuffer_get_debuginfo(struct hv_ring_buffer_info *ring_info,
170 struct hv_ring_buffer_debug_info *debug_info)170 struct hv_ring_buffer_debug_info *debug_info)
171{171{
172 u32 bytes_avail_towrite;172 u32 bytes_avail_towrite;
173 u32 bytes_avail_toread;173 u32 bytes_avail_toread;
174 174
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);
176 return -EINVAL;179 return -EINVAL;
180 }
177 181
178 hv_get_ringbuffer_availbytes(ring_info,182 hv_get_ringbuffer_availbytes(ring_info,
179 &bytes_avail_toread,183 &bytes_avail_toread,
@@ -184,10 +188,19 @@ int hv_ringbuffer_get_debuginfo(const struct hv_ring_buffer_info *ring_info, · 4 unmodified lines
184 debug_info->current_write_index = ring_info->ring_buffer->write_index;188 debug_info->current_write_index = ring_info->ring_buffer->write_index;
185 debug_info->current_interrupt_mask189 debug_info->current_interrupt_mask
186 = ring_info->ring_buffer->interrupt_mask;190 = ring_info->ring_buffer->interrupt_mask;
191 mutex_unlock(&ring_info->ring_buffer_mutex);
192
187 return 0;193 return 0;
188}194}
189EXPORT_SYMBOL_GPL(hv_ringbuffer_get_debuginfo);195EXPORT_SYMBOL_GPL(hv_ringbuffer_get_debuginfo);
190 196
197/* Initialize a channel's ring buffer info mutex locks */
198void 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
191/* Initialize the ring buffer. */204/* Initialize the ring buffer. */
192int hv_ringbuffer_init(struct hv_ring_buffer_info *ring_info,205int hv_ringbuffer_init(struct hv_ring_buffer_info *ring_info,
193 struct page *pages, u32 page_cnt)206 struct page *pages, u32 page_cnt)
@@ -240,8 +253,10 @@ int hv_ringbuffer_init(struct hv_ring_buffer_info *ring_info, · 46 unmodified lines
240/* Cleanup the ring buffer. */253/* Cleanup the ring buffer. */
241void hv_ringbuffer_cleanup(struct hv_ring_buffer_info *ring_info)254void hv_ringbuffer_cleanup(struct hv_ring_buffer_info *ring_info)
242{255{
256 mutex_lock(&ring_info->ring_buffer_mutex);
243 vunmap(ring_info->ring_buffer);257 vunmap(ring_info->ring_buffer);
244 ring_info->ring_buffer = NULL;258 ring_info->ring_buffer = NULL;
259 mutex_unlock(&ring_info->ring_buffer_mutex);
245}260}
246 261
247/* Write to the ring buffer. */262/* 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
1410 1410
1411struct vmbus_chan_attribute {1411struct vmbus_chan_attribute {
1412 struct attribute attr;1412 struct attribute attr;
1413 ssize_t (*show)(const struct vmbus_channel *chan, char *buf);1413 ssize_t (*show)(struct vmbus_channel *chan, char *buf);
1414 ssize_t (*store)(struct vmbus_channel *chan,1414 ssize_t (*store)(struct vmbus_channel *chan,
1415 const char *buf, size_t count);1415 const char *buf, size_t count);
1416};1416};
@@ -1429,7 +1429,7 @@ static ssize_t vmbus_chan_attr_show(struct kobject *kobj, · 12 unmodified lines
1429{1429{
1430 const struct vmbus_chan_attribute *attribute1430 const struct vmbus_chan_attribute *attribute
1431 = container_of(attr, struct vmbus_chan_attribute, attr);1431 = container_of(attr, struct vmbus_chan_attribute, attr);
1432 const struct vmbus_channel *chan1432 struct vmbus_channel *chan
1433 = container_of(kobj, struct vmbus_channel, kobj);1433 = container_of(kobj, struct vmbus_channel, kobj);
1434 1434
1435 if (!attribute->show)1435 if (!attribute->show)
@@ -1442,57 +1442,81 @@ static const struct sysfs_ops vmbus_chan_sysfs_ops = { · 6 unmodified lines
1442 .show = vmbus_chan_attr_show,1442 .show = vmbus_chan_attr_show,
1443};1443};
1444 1444
1445static ssize_t out_mask_show(const struct vmbus_channel *channel, char *buf)1445static ssize_t out_mask_show(struct vmbus_channel *channel, char *buf)
1446{1446{
1447 const struct hv_ring_buffer_info *rbi = &channel->outbound;1447 struct hv_ring_buffer_info *rbi = &channel->outbound;
1448 ssize_t ret;
1448 1449
1449 if (!rbi->ring_buffer)1450 mutex_lock(&rbi->ring_buffer_mutex);
1451 if (!rbi->ring_buffer) {
1452 mutex_unlock(&rbi->ring_buffer_mutex);
1450 return -EINVAL;1453 return -EINVAL;
1454 }
1451 1455
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;
1453}1459}
1454static VMBUS_CHAN_ATTR_RO(out_mask);1460static VMBUS_CHAN_ATTR_RO(out_mask);
1455 1461
1456static ssize_t in_mask_show(const struct vmbus_channel *channel, char *buf)1462static ssize_t in_mask_show(struct vmbus_channel *channel, char *buf)
1457{1463{
1458 const struct hv_ring_buffer_info *rbi = &channel->inbound;1464 struct hv_ring_buffer_info *rbi = &channel->inbound;
1465 ssize_t ret;
1459 1466
1460 if (!rbi->ring_buffer)1467 mutex_lock(&rbi->ring_buffer_mutex);
1468 if (!rbi->ring_buffer) {
1469 mutex_unlock(&rbi->ring_buffer_mutex);
1461 return -EINVAL;1470 return -EINVAL;
1471 }
1462 1472
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;
1464}1476}
1465static VMBUS_CHAN_ATTR_RO(in_mask);1477static VMBUS_CHAN_ATTR_RO(in_mask);
1466 1478
1467static ssize_t read_avail_show(const struct vmbus_channel *channel, char *buf)1479static ssize_t read_avail_show(struct vmbus_channel *channel, char *buf)
1468{1480{
1469 const struct hv_ring_buffer_info *rbi = &channel->inbound;1481 struct hv_ring_buffer_info *rbi = &channel->inbound;
1482 ssize_t ret;
1470 1483
1471 if (!rbi->ring_buffer)1484 mutex_lock(&rbi->ring_buffer_mutex);
1485 if (!rbi->ring_buffer) {
1486 mutex_unlock(&rbi->ring_buffer_mutex);
1472 return -EINVAL;1487 return -EINVAL;
1488 }
1473 1489
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;
1475}1493}
1476static VMBUS_CHAN_ATTR_RO(read_avail);1494static VMBUS_CHAN_ATTR_RO(read_avail);
1477 1495
1478static ssize_t write_avail_show(const struct vmbus_channel *channel, char *buf)1496static ssize_t write_avail_show(struct vmbus_channel *channel, char *buf)
1479{1497{
1480 const struct hv_ring_buffer_info *rbi = &channel->outbound;1498 struct hv_ring_buffer_info *rbi = &channel->outbound;
1499 ssize_t ret;
1481 1500
1482 if (!rbi->ring_buffer)1501 mutex_lock(&rbi->ring_buffer_mutex);
1502 if (!rbi->ring_buffer) {
1503 mutex_unlock(&rbi->ring_buffer_mutex);
1483 return -EINVAL;1504 return -EINVAL;
1505 }
1484 1506
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;
1486}1510}
1487static VMBUS_CHAN_ATTR_RO(write_avail);1511static VMBUS_CHAN_ATTR_RO(write_avail);
1488 1512
1489static ssize_t show_target_cpu(const struct vmbus_channel *channel, char *buf)1513static ssize_t show_target_cpu(struct vmbus_channel *channel, char *buf)
1490{1514{
1491 return sprintf(buf, "%u\n", channel->target_cpu);1515 return sprintf(buf, "%u\n", channel->target_cpu);
1492}1516}
1493static VMBUS_CHAN_ATTR(cpu, S_IRUGO, show_target_cpu, NULL);1517static VMBUS_CHAN_ATTR(cpu, S_IRUGO, show_target_cpu, NULL);
1494 1518
1495static ssize_t channel_pending_show(const struct vmbus_channel *channel,1519static ssize_t channel_pending_show(struct vmbus_channel *channel,
1496 char *buf)1520 char *buf)
1497{1521{
1498 return sprintf(buf, "%d\n",1522 return sprintf(buf, "%d\n",
@@ -1501,7 +1525,7 @@ static ssize_t channel_pending_show(const struct vmbus_channel *channel, · 2 unmodified lines
1501}1525}
1502static VMBUS_CHAN_ATTR(pending, S_IRUGO, channel_pending_show, NULL);1526static VMBUS_CHAN_ATTR(pending, S_IRUGO, channel_pending_show, NULL);
1503 1527
1504static ssize_t channel_latency_show(const struct vmbus_channel *channel,1528static ssize_t channel_latency_show(struct vmbus_channel *channel,
1505 char *buf)1529 char *buf)
1506{1530{
1507 return sprintf(buf, "%d\n",1531 return sprintf(buf, "%d\n",
@@ -1510,19 +1534,19 @@ static ssize_t channel_latency_show(const struct vmbus_channel *channel, · 2 unmodified lines
1510}1534}
1511static VMBUS_CHAN_ATTR(latency, S_IRUGO, channel_latency_show, NULL);1535static VMBUS_CHAN_ATTR(latency, S_IRUGO, channel_latency_show, NULL);
1512 1536
1513static ssize_t channel_interrupts_show(const struct vmbus_channel *channel, char *buf)1537static ssize_t channel_interrupts_show(struct vmbus_channel *channel, char *buf)
1514{1538{
1515 return sprintf(buf, "%llu\n", channel->interrupts);1539 return sprintf(buf, "%llu\n", channel->interrupts);
1516}1540}
1517static VMBUS_CHAN_ATTR(interrupts, S_IRUGO, channel_interrupts_show, NULL);1541static VMBUS_CHAN_ATTR(interrupts, S_IRUGO, channel_interrupts_show, NULL);
1518 1542
1519static ssize_t channel_events_show(const struct vmbus_channel *channel, char *buf)1543static ssize_t channel_events_show(struct vmbus_channel *channel, char *buf)
1520{1544{
1521 return sprintf(buf, "%llu\n", channel->sig_events);1545 return sprintf(buf, "%llu\n", channel->sig_events);
1522}1546}
1523static VMBUS_CHAN_ATTR(events, S_IRUGO, channel_events_show, NULL);1547static VMBUS_CHAN_ATTR(events, S_IRUGO, channel_events_show, NULL);
1524 1548
1525static ssize_t channel_intr_in_full_show(const struct vmbus_channel *channel,1549static ssize_t channel_intr_in_full_show(struct vmbus_channel *channel,
1526 char *buf)1550 char *buf)
1527{1551{
1528 return sprintf(buf, "%llu\n",1552 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
1530}1554}
1531static VMBUS_CHAN_ATTR(intr_in_full, 0444, channel_intr_in_full_show, NULL);1555static VMBUS_CHAN_ATTR(intr_in_full, 0444, channel_intr_in_full_show, NULL);
1532 1556
1533static ssize_t channel_intr_out_empty_show(const struct vmbus_channel *channel,1557static ssize_t channel_intr_out_empty_show(struct vmbus_channel *channel,
1534 char *buf)1558 char *buf)
1535{1559{
1536 return sprintf(buf, "%llu\n",1560 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
1538}1562}
1539static VMBUS_CHAN_ATTR(intr_out_empty, 0444, channel_intr_out_empty_show, NULL);1563static VMBUS_CHAN_ATTR(intr_out_empty, 0444, channel_intr_out_empty_show, NULL);
1540 1564
1541static ssize_t channel_out_full_first_show(const struct vmbus_channel *channel,1565static ssize_t channel_out_full_first_show(struct vmbus_channel *channel,
1542 char *buf)1566 char *buf)
1543{1567{
1544 return sprintf(buf, "%llu\n",1568 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
1546}1570}
1547static VMBUS_CHAN_ATTR(out_full_first, 0444, channel_out_full_first_show, NULL);1571static VMBUS_CHAN_ATTR(out_full_first, 0444, channel_out_full_first_show, NULL);
1548 1572
1549static ssize_t channel_out_full_total_show(const struct vmbus_channel *channel,1573static ssize_t channel_out_full_total_show(struct vmbus_channel *channel,
1550 char *buf)1574 char *buf)
1551{1575{
1552 return sprintf(buf, "%llu\n",1576 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
1554}1578}
1555static VMBUS_CHAN_ATTR(out_full_total, 0444, channel_out_full_total_show, NULL);1579static VMBUS_CHAN_ATTR(out_full_total, 0444, channel_out_full_total_show, NULL);
1556 1580
1557static ssize_t subchannel_monitor_id_show(const struct vmbus_channel *channel,1581static ssize_t subchannel_monitor_id_show(struct vmbus_channel *channel,
1558 char *buf)1582 char *buf)
1559{1583{
1560 return sprintf(buf, "%u\n", channel->offermsg.monitorid);1584 return sprintf(buf, "%u\n", channel->offermsg.monitorid);
1561}1585}
1562static VMBUS_CHAN_ATTR(monitor_id, S_IRUGO, subchannel_monitor_id_show, NULL);1586static VMBUS_CHAN_ATTR(monitor_id, S_IRUGO, subchannel_monitor_id_show, NULL);
1563 1587
1564static ssize_t subchannel_id_show(const struct vmbus_channel *channel,1588static ssize_t subchannel_id_show(struct vmbus_channel *channel,
1565 char *buf)1589 char *buf)
1566{1590{
1567 return sprintf(buf, "%u\n",1591 return sprintf(buf, "%u\n",
include/linux/hyperv.h+6−1
@@ -141,6 +141,11 @@ struct hv_ring_buffer_info { · 140 unmodified lines
141 141
142 u32 ring_datasize; /* < ring_size */142 u32 ring_datasize; /* < ring_size */
143 u32 priv_read_index;143 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;
144};149};
145 150
146 151
@@ -1206,7 +1211,7 @@ struct hv_ring_buffer_debug_info { · 1059 unmodified lines
1206};1211};
1207 1212
1208 1213
1209int hv_ringbuffer_get_debuginfo(const struct hv_ring_buffer_info *ring_info,1214int hv_ringbuffer_get_debuginfo(struct hv_ring_buffer_info *ring_info,
1210 struct hv_ring_buffer_debug_info *debug_info);1215 struct hv_ring_buffer_debug_info *debug_info);
1211 1216
1212/* Vmbus interface */1217/* 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