Skip to contentSkip to conversation

encrypt in threads

Qemu-devel Vladimir Sementsov-Ogievskiy 2 participants 8 messages Nov 21, 2018 – Nov 30, 2018

lore
Share & tools
lore
#0 lore raw

Hi Daniel!

After moving compression to threads in Qcow2 it's an obvious next step to "threadyfy" encryption in Qcow2 too.

But it turned out to be not as simple as I assumed. If I call qcrypto_block_encrypt in parallel threads with the same first argument (block), it just produce wrong things (pattern verification fails in iotests)..

So, can you advise the way to parallelize encryption/decryption?

Signature · 2 lines
Best regards,
Vladimir
#1 lore raw
Quoted 11 lines · Vladimir Sementsov-Ogievskiy

Hi Daniel!

After moving compression to threads in Qcow2 it's an obvious next step to "threadyfy" encryption in Qcow2 too.

But it turned out to be not as simple as I assumed. If I call qcrypto_block_encrypt in parallel threads with the same first argument (block), it just produce wrong things (pattern verification fails in iotests)..

So, can you advise the way to parallelize encryption/decryption?

Hmm, just creating QCryptoBlock per each thread helped. Is it correct thing to do?

Signature · 2 lines
Best regards,
Vladimir
#2 lore raw
Quoted 6 lines · Vladimir Sementsov-Ogievskiy
Quoted 5 lines · Vladimir Sementsov-Ogievskiy

Hi Daniel!

After moving compression to threads in Qcow2 it's an obvious next step to "threadyfy" encryption in Qcow2 too.

Quoted 8 lines
Quoted 6 lines

But it turned out to be not as simple as I assumed. If I call qcrypto_block_encrypt in parallel threads with the same first argument (block), it just produce wrong things (pattern verification fails in iotests)..

So, can you advise the way to parallelize encryption/decryption?

Hmm, just creating QCryptoBlock per each thread helped. Is it correct thing to do?

That's rather a heavy weight approach and would cause pain when we want to support future options such as keyslot updates, and in the future, LUKSv2 with master key changes.

Probably what's needed is change to QCryptoBlock struct so that there can be multiple QCryptoCipher instances allocated - one per thread.

We might also need to introduce some locking around usage of the QCryptoIVGen object, since that has a QCryptoCipher handle too.

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#3 lore raw
Quoted 18 lines · Daniel P. Berrangé
Quoted 6 lines · Vladimir Sementsov-Ogievskiy
Quoted 5 lines · Vladimir Sementsov-Ogievskiy

Hi Daniel!

After moving compression to threads in Qcow2 it's an obvious next step to "threadyfy" encryption in Qcow2 too.

Quoted 7 lines
Quoted 6 lines

But it turned out to be not as simple as I assumed. If I call qcrypto_block_encrypt in parallel threads with the same first argument (block), it just produce wrong things (pattern verification fails in iotests)..

So, can you advise the way to parallelize encryption/decryption?

Hmm, just creating QCryptoBlock per each thread helped. Is it correct thing to do?

That's rather a heavy weight approach and would cause pain when we want to support future options such as keyslot updates, and in the future, LUKSv2 with master key changes.

Quoted 5 lines

Probably what's needed is change to QCryptoBlock struct so that there can be multiple QCryptoCipher instances allocated - one per thread.

We might also need to introduce some locking around usage of the QCryptoIVGen object, since that has a QCryptoCipher handle too.

Can we also create QCryptoIVGen per thread, as QCryptoCipher? Or it should be one? If one, why my implementation with QCryptoBlock per thread works (at least it passes iotests, hmm)

Quoted 3 lines

Regards,
Daniel

#4 lore raw
Quoted 23 lines · Vladimir Sementsov-Ogievskiy
Quoted 21 lines · Daniel P. Berrangé
Quoted 6 lines · Vladimir Sementsov-Ogievskiy
Quoted 5 lines · Vladimir Sementsov-Ogievskiy

Hi Daniel!

After moving compression to threads in Qcow2 it's an obvious next step to "threadyfy" encryption in Qcow2 too.

Quoted 7 lines
Quoted 6 lines

But it turned out to be not as simple as I assumed. If I call qcrypto_block_encrypt in parallel threads with the same first argument (block), it just produce wrong things (pattern verification fails in iotests)..

So, can you advise the way to parallelize encryption/decryption?

Hmm, just creating QCryptoBlock per each thread helped. Is it correct thing to do?

That's rather a heavy weight approach and would cause pain when we want to support future options such as keyslot updates, and in the future, LUKSv2 with master key changes.

Probably what's needed is change to QCryptoBlock struct so that there can be multiple QCryptoCipher instances allocated - one per thread.

Quoted 7 lines
Quoted 2 lines

We might also need to introduce some locking around usage of the QCryptoIVGen object, since that has a QCryptoCipher handle too.

Can we also create QCryptoIVGen per thread, as QCryptoCipher? Or it should be one? If one, why my implementation with QCryptoBlock per thread works (at least it passes iotests, hmm)

The only IV generator that uses ciphers is the "essiv" one. Even that one uses the cipher in ECB mode, so there is no initialization vector required for its internal cipher. This means there's no critical shared state that would be overwritten by threads. Thus using a separate QCryptoCipher per thread for the essiv IV gen is overkill. None the less I think we should protect the IV generator calls with a mutex just to be safe. I don't think the mutex would have a notable impact on performance of the iv generator.

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#5 lore raw
Quoted 32 lines · Daniel P. Berrangé
Quoted 23 lines · Vladimir Sementsov-Ogievskiy
Quoted 21 lines · Daniel P. Berrangé
Quoted 6 lines · Vladimir Sementsov-Ogievskiy
Quoted 5 lines · Vladimir Sementsov-Ogievskiy

Hi Daniel!

After moving compression to threads in Qcow2 it's an obvious next step to "threadyfy" encryption in Qcow2 too.

Quoted 7 lines
Quoted 6 lines

But it turned out to be not as simple as I assumed. If I call qcrypto_block_encrypt in parallel threads with the same first argument (block), it just produce wrong things (pattern verification fails in iotests)..

So, can you advise the way to parallelize encryption/decryption?

Hmm, just creating QCryptoBlock per each thread helped. Is it correct thing to do?

That's rather a heavy weight approach and would cause pain when we want to support future options such as keyslot updates, and in the future, LUKSv2 with master key changes.

Probably what's needed is change to QCryptoBlock struct so that there can be multiple QCryptoCipher instances allocated - one per thread.

Quoted 7 lines
Quoted 2 lines

We might also need to introduce some locking around usage of the QCryptoIVGen object, since that has a QCryptoCipher handle too.

Can we also create QCryptoIVGen per thread, as QCryptoCipher? Or it should be one? If one, why my implementation with QCryptoBlock per thread works (at least it passes iotests, hmm)

Quoted 8 lines

The only IV generator that uses ciphers is the "essiv" one. Even that one uses the cipher in ECB mode, so there is no initialization vector required for its internal cipher. This means there's no critical shared state that would be overwritten by threads. Thus using a separate QCryptoCipher per thread for the essiv IV gen is overkill. None the less I think we should protect the IV generator calls with a mutex just to be safe. I don't think the mutex would have a notable impact on performance of the iv generator.

But if we use mutex, it means that we can't generate two ivs in parallel, and then, it becomes better to have separate iv-gen per thread, instead of only one, protected by mutex. (or I don't follow)

Quoted 4 lines

Regards,
Daniel

Signature · 2 lines
Best regards,
Vladimir
#6 lore raw
Quoted 42 lines · Vladimir Sementsov-Ogievskiy
Quoted 32 lines · Daniel P. Berrangé
Quoted 23 lines · Vladimir Sementsov-Ogievskiy
Quoted 21 lines · Daniel P. Berrangé
Quoted 6 lines · Vladimir Sementsov-Ogievskiy
Quoted 5 lines · Vladimir Sementsov-Ogievskiy

Hi Daniel!

After moving compression to threads in Qcow2 it's an obvious next step to "threadyfy" encryption in Qcow2 too.

Quoted 7 lines
Quoted 6 lines

But it turned out to be not as simple as I assumed. If I call qcrypto_block_encrypt in parallel threads with the same first argument (block), it just produce wrong things (pattern verification fails in iotests)..

So, can you advise the way to parallelize encryption/decryption?

Hmm, just creating QCryptoBlock per each thread helped. Is it correct thing to do?

That's rather a heavy weight approach and would cause pain when we want to support future options such as keyslot updates, and in the future, LUKSv2 with master key changes.

Probably what's needed is change to QCryptoBlock struct so that there can be multiple QCryptoCipher instances allocated - one per thread.

Quoted 7 lines
Quoted 2 lines

We might also need to introduce some locking around usage of the QCryptoIVGen object, since that has a QCryptoCipher handle too.

Can we also create QCryptoIVGen per thread, as QCryptoCipher? Or it should be one? If one, why my implementation with QCryptoBlock per thread works (at least it passes iotests, hmm)

Quoted 8 lines

The only IV generator that uses ciphers is the "essiv" one. Even that one uses the cipher in ECB mode, so there is no initialization vector required for its internal cipher. This means there's no critical shared state that would be overwritten by threads. Thus using a separate QCryptoCipher per thread for the essiv IV gen is overkill. None the less I think we should protect the IV generator calls with a mutex just to be safe. I don't think the mutex would have a notable impact on performance of the iv generator.

Quoted 3 lines

But if we use mutex, it means that we can't generate two ivs in parallel, and then, it becomes better to have separate iv-gen per thread, instead of only one, protected by mutex. (or I don't follow)

I'm questioning whether parallelism of the iv-gen is actually needed though. It has very short execution time, only encrypting 64 bits of data, per 512 byte sector with essiv.

If you have show performance numbers that prove a per-thread iv generator is needed for ESSIV I'd accept that, otherwise I'd just go for a mutex.

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#7 lore raw
In reply to Vladimir Sementsov-Ogievskiy
Quoted 8 lines · Vladimir Sementsov-Ogievskiy

Hi Daniel!

After moving compression to threads in Qcow2 it's an obvious next step to "threadyfy" encryption in Qcow2 too.

But it turned out to be not as simple as I assumed. If I call qcrypto_block_encrypt in parallel threads with the same first argument (block), it just produce wrong things (pattern verification fails in iotests)..

Yes, this makes sense, because the underlying crypto impls all require that their state is only used from a single thread at any time. What's likely happening is that the IV is being scrambled so we're encrypting with the wrong tweak.

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|