Skip to contentSkip to conversation

qed: do not evict in-use L2 table cache entries

PATCH Applied Stefan Hajnoczi 4 participants 7 messages Feb 27, 2012 – Mar 5, 2012 Block layer

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 qed: do not evict in-use L2 table cache entries

The L2 table cache reduces QED metadata reads that would be required when translating LBAs to offsets into the image file. Since requests execute in parallel it is possible to share an L2 table between multiple requests.

There is a potential data corruption issue when an in-use L2 table is evicted from the cache because the following situation occurs:

1. An allocating write performs an update to L2 table "A".

2. Another request needs L2 table "B" and causes table "A" to be evicted.

3. A new read request needs L2 table "A" but it is not cached.

As a result the L2 update from #1 can overlap with the L2 fetch from #3. We must avoid doing overlapping I/O requests here since the worst case outcome is that the L2 fetch completes before the L2 update and yields stale data. In that case we would effectively discard the L2 update and lose data clusters!

Thanks to Benoît Canet <[email protected]> for extensive testing and debugging which lead to discovery of this bug.

---
Please include this in -stable once it has been merged into qemu.git/master.

 block/qed-l2-cache.c |   22 ++++++++++++++++++----
 1 files changed, 18 insertions(+), 4 deletions(-)
block/qed-l2-cache.c +18 -4
index 02b81a2..e9b2aae 100644
@@ -161,11 +161,25 @@ void qed_commit_l2_cache_entry(L2TableCache *l2_cache, CachedL2Table *l2_table)
         return;
     }
 
+    /* Evict an unused cache entry so we have space.  If all entries are in use
+     * we can grow the cache temporarily and we try to shrink back down later.
+     */
     if (l2_cache->n_entries >= MAX_L2_CACHE_SIZE) {
-        entry = QTAILQ_FIRST(&l2_cache->entries);
-        QTAILQ_REMOVE(&l2_cache->entries, entry, node);
-        l2_cache->n_entries--;
-        qed_unref_l2_cache_entry(entry);
+        CachedL2Table *next;
+        QTAILQ_FOREACH_SAFE(entry, &l2_cache->entries, node, next) {
+            if (entry->ref > 1) {
+                continue;
+            }
+
+            QTAILQ_REMOVE(&l2_cache->entries, entry, node);
+            l2_cache->n_entries--;
+            qed_unref_l2_cache_entry(entry);
+
+            /* Stop evicting when we've shrunk back to max size */
+            if (l2_cache->n_entries < MAX_L2_CACHE_SIZE) {
+                break;
+            }
+        }
     }
 
     l2_cache->n_entries++;
Signature · 1 line
1.7.9
#1 lore raw
Quoted 24 lines · Stefan Hajnoczi

The L2 table cache reduces QED metadata reads that would be required when translating LBAs to offsets into the image file.  Since requests execute in parallel it is possible to share an L2 table between multiple requests.

There is a potential data corruption issue when an in-use L2 table is evicted from the cache because the following situation occurs:

 1. An allocating write performs an update to L2 table "A".

 2. Another request needs L2 table "B" and causes table "A" to be evicted.

 3. A new read request needs L2 table "A" but it is not cached.

As a result the L2 update from #1 can overlap with the L2 fetch from #3. We must avoid doing overlapping I/O requests here since the worst case outcome is that the L2 fetch completes before the L2 update and yields stale data.  In that case we would effectively discard the L2 update and lose data clusters!

Thanks to Benoît Canet <[email protected]> for extensive testing and debugging which lead to discovery of this bug.

Quoted 7 lines

---
Please include this in -stable once it has been merged into qemu.git/master.

 block/qed-l2-cache.c |   22 ++++++++++++++++++----

 1 files changed, 18 insertions(+), 4 deletions(-)

Thanks for testing this fix and confirming it works, Benoît. Feel free to reply with your Tested-by: line.

Stefan

Tested-by on v1 1/1
#2 lore raw
In reply to Stefan Hajnoczi
Quoted 75 lines · Stefan Hajnoczi

The L2 table cache reduces QED metadata reads that would be required when translating LBAs to offsets into the image file. Since requests execute in parallel it is possible to share an L2 table between multiple requests.

There is a potential data corruption issue when an in-use L2 table is evicted from the cache because the following situation occurs:

1. An allocating write performs an update to L2 table "A".

2. Another request needs L2 table "B" and causes table "A" to be evicted.

3. A new read request needs L2 table "A" but it is not cached.

As a result the L2 update from #1 can overlap with the L2 fetch from #3. We must avoid doing overlapping I/O requests here since the worst case outcome is that the L2 fetch completes before the L2 update and yields stale data. In that case we would effectively discard the L2 update and lose data clusters!

Thanks to Benoît Canet <[email protected]> for extensive testing and debugging which lead to discovery of this bug.

---
Please include this in -stable once it has been merged into qemu.git/master.

 block/qed-l2-cache.c |   22 ++++++++++++++++++----
 1 files changed, 18 insertions(+), 4 deletions(-)
block/qed-l2-cache.c +1 -0
index 02b81a2..e9b2aae 100644
@@ -161,11 +161,25 @@ void qed_commit_l2_cache_entry(L2TableCache
 *l2_cache, CachedL2Table *l2_table)
        return;
    }
 
+    /* Evict an unused cache entry so we have space.  If all entries are

in use
+ * we can grow the cache temporarily and we try to shrink back down
later.

block/qed-l2-cache.c +16 -5
+     */
    if (l2_cache->n_entries >= MAX_L2_CACHE_SIZE) {
-        entry = QTAILQ_FIRST(&l2_cache->entries);
-        QTAILQ_REMOVE(&l2_cache->entries, entry, node);
-        l2_cache->n_entries--;
-        qed_unref_l2_cache_entry(entry);
+        CachedL2Table *next;
+        QTAILQ_FOREACH_SAFE(entry, &l2_cache->entries, node, next) {
+            if (entry->ref > 1) {
+                continue;
+            }
+
+            QTAILQ_REMOVE(&l2_cache->entries, entry, node);
+            l2_cache->n_entries--;
+            qed_unref_l2_cache_entry(entry);
+
+            /* Stop evicting when we've shrunk back to max size */
+            if (l2_cache->n_entries < MAX_L2_CACHE_SIZE) {
+                break;
+            }
+        }
    }
 
    l2_cache->n_entries++;
--

1.7.9

Applied
#3 lore raw
In reply to Stefan Hajnoczi

Am 27.02.2012 14:16, schrieb Stefan Hajnoczi:

Quoted 21 lines

The L2 table cache reduces QED metadata reads that would be required when translating LBAs to offsets into the image file. Since requests execute in parallel it is possible to share an L2 table between multiple requests.

There is a potential data corruption issue when an in-use L2 table is evicted from the cache because the following situation occurs:

1. An allocating write performs an update to L2 table "A".

2. Another request needs L2 table "B" and causes table "A" to be evicted.

3. A new read request needs L2 table "A" but it is not cached.

As a result the L2 update from #1 can overlap with the L2 fetch from #3. We must avoid doing overlapping I/O requests here since the worst case outcome is that the L2 fetch completes before the L2 update and yields stale data. In that case we would effectively discard the L2 update and lose data clusters!

Quoted 5 lines

Thanks to Benoît Canet <[email protected]> for extensive testing and debugging which lead to discovery of this bug.

Thanks, applied to the block branch.

How about a qemu-iotests case?

Kevin

#4 lore raw
Quoted 25 lines · Kevin Wolf

Am 27.02.2012 14:16, schrieb Stefan Hajnoczi:

Quoted 24 lines

The L2 table cache reduces QED metadata reads that would be required when translating LBAs to offsets into the image file. Since requests execute in parallel it is possible to share an L2 table between multiple requests.

There is a potential data corruption issue when an in-use L2 table is evicted from the cache because the following situation occurs:

1. An allocating write performs an update to L2 table "A".

2. Another request needs L2 table "B" and causes table "A" to be evicted.

3. A new read request needs L2 table "A" but it is not cached.

As a result the L2 update from #1 can overlap with the L2 fetch from #3. We must avoid doing overlapping I/O requests here since the worst case outcome is that the L2 fetch completes before the L2 update and yields stale data. In that case we would effectively discard the L2 update and lose data clusters!

Thanks to Benoît Canet <[email protected]> for extensive testing and debugging which lead to discovery of this bug.

Quoted 6 lines
Quoted 2 lines

Thanks, applied to the block branch.

How about a qemu-iotests case?

The test case is not ready yet. I started writing one but it is racy because I haven't introduced a way of controlling AIO issue/complete for tests. My next step is to add that.

Stefan

#5 lore raw

Am 01.03.2012 17:22, schrieb Stefan Hajnoczi:

Quoted 29 lines · Kevin Wolf
Quoted 28 lines

Am 27.02.2012 14:16, schrieb Stefan Hajnoczi:

Quoted 26 lines

The L2 table cache reduces QED metadata reads that would be required when translating LBAs to offsets into the image file. Since requests execute in parallel it is possible to share an L2 table between multiple requests.

There is a potential data corruption issue when an in-use L2 table is evicted from the cache because the following situation occurs:

1. An allocating write performs an update to L2 table "A".

2. Another request needs L2 table "B" and causes table "A" to be evicted.

3. A new read request needs L2 table "A" but it is not cached.

As a result the L2 update from #1 can overlap with the L2 fetch from #3. We must avoid doing overlapping I/O requests here since the worst case outcome is that the L2 fetch completes before the L2 update and yields stale data. In that case we would effectively discard the L2 update and lose data clusters!

Thanks to Benoît Canet <[email protected]> for extensive testing and debugging which lead to discovery of this bug.

Quoted 7 lines
Quoted 3 lines

Thanks, applied to the block branch.

How about a qemu-iotests case?

The test case is not ready yet. I started writing one but it is racy because I haven't introduced a way of controlling AIO issue/complete for tests. My next step is to add that.

Will it be specific to image formats using AIO then or is it generic enough that coroutine-based drivers work with it as well?

Kevin

#6 lore raw
Quoted 32 lines · Kevin Wolf

Am 01.03.2012 17:22, schrieb Stefan Hajnoczi:

Quoted 31 lines · Kevin Wolf
Quoted 30 lines

Am 27.02.2012 14:16, schrieb Stefan Hajnoczi:

Quoted 21 lines

The L2 table cache reduces QED metadata reads that would be required when translating LBAs to offsets into the image file.  Since requests execute in parallel it is possible to share an L2 table between multiple requests.

There is a potential data corruption issue when an in-use L2 table is evicted from the cache because the following situation occurs:

  1. An allocating write performs an update to L2 table "A".

  2. Another request needs L2 table "B" and causes table "A" to be evicted.

  3. A new read request needs L2 table "A" but it is not cached.

As a result the L2 update from #1 can overlap with the L2 fetch from #3. We must avoid doing overlapping I/O requests here since the worst case outcome is that the L2 fetch completes before the L2 update and yields stale data.  In that case we would effectively discard the L2 update and lose data clusters!

Quoted 5 lines

Thanks to Benoît Canet <[email protected]> for extensive testing and debugging which lead to discovery of this bug.

Thanks, applied to the block branch.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

How about a qemu-iotests case?

The test case is not ready yet.  I started writing one but it is racy because I haven't introduced a way of controlling AIO issue/complete for tests.  My next step is to add that.

Will it be specific to image formats using AIO then or is it generic enough that coroutine-based drivers work with it as well?

I don't know yet but it would be nice to support .bdrv_co_*()-based drivers too.

Stefan