From nobody Tue Nov 4 00:09:41 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1505216039121566.4715081386088; Tue, 12 Sep 2017 04:33:59 -0700 (PDT) Received: from localhost ([::1]:35027 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1drjS2-0006fQ-8R for importer@patchew.org; Tue, 12 Sep 2017 07:33:58 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:53811) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1drjNQ-0002zU-Ly for qemu-devel@nongnu.org; Tue, 12 Sep 2017 07:29:14 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1drjNP-0004RQ-Ii for qemu-devel@nongnu.org; Tue, 12 Sep 2017 07:29:12 -0400 Received: from mx1.redhat.com ([209.132.183.28]:57820) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1drjNJ-0004KP-Qk; Tue, 12 Sep 2017 07:29:05 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id EA2A413AAD; Tue, 12 Sep 2017 11:29:04 +0000 (UTC) Received: from localhost.localdomain.com (unknown [10.42.22.189]) by smtp.corp.redhat.com (Postfix) with ESMTP id B9B5B6E1FC; Tue, 12 Sep 2017 11:29:03 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com EA2A413AAD Authentication-Results: ext-mx05.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx05.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=berrange@redhat.com From: "Daniel P. Berrange" To: qemu-devel@nongnu.org Date: Tue, 12 Sep 2017 12:28:49 +0100 Message-Id: <20170912112855.24269-2-berrange@redhat.com> In-Reply-To: <20170912112855.24269-1-berrange@redhat.com> References: <20170912112855.24269-1-berrange@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.29]); Tue, 12 Sep 2017 11:29:05 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v3 1/7] block: use 1 MB bounce buffers for crypto instead of 16KB X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , qemu-block@nongnu.org, Stefan Hajnoczi , Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Using 16KB bounce buffers creates a significant performance penalty for I/O to encrypted volumes on storage which high I/O latency (rotating rust & network drives), because it triggers lots of fairly small I/O operations. On tests with rotating rust, and cache=3Dnone|directsync, write speed increased from 2MiB/s to 32MiB/s, on a par with that achieved by the in-kernel luks driver. With other cache modes the in-kernel driver is still notably faster because it is able to report completion of the I/O request before any encryption is done, while the in-QEMU driver must encrypt the data before completion. Reviewed-by: Eric Blake Reviewed-by: Stefan Hajnoczi Signed-off-by: Daniel P. Berrange Reviewed-by: Max Reitz --- block/crypto.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/block/crypto.c b/block/crypto.c index 58ef6f2f52..cc8afe0e0d 100644 --- a/block/crypto.c +++ b/block/crypto.c @@ -379,7 +379,7 @@ static void block_crypto_close(BlockDriverState *bs) } =20 =20 -#define BLOCK_CRYPTO_MAX_SECTORS 32 +#define BLOCK_CRYPTO_MAX_SECTORS 2048 =20 static coroutine_fn int block_crypto_co_readv(BlockDriverState *bs, int64_t sector_num, @@ -396,9 +396,8 @@ block_crypto_co_readv(BlockDriverState *bs, int64_t sec= tor_num, =20 qemu_iovec_init(&hd_qiov, qiov->niov); =20 - /* Bounce buffer so we have a linear mem region for - * entire sector. XXX optimize so we avoid bounce - * buffer in case that qiov->niov =3D=3D 1 + /* Bounce buffer because we don't wish to expose cipher text + * in qiov which points to guest memory. */ cipher_data =3D qemu_try_blockalign(bs->file->bs, MIN(BLOCK_CRYPTO_MAX_SECTORS * 5= 12, @@ -464,9 +463,8 @@ block_crypto_co_writev(BlockDriverState *bs, int64_t se= ctor_num, =20 qemu_iovec_init(&hd_qiov, qiov->niov); =20 - /* Bounce buffer so we have a linear mem region for - * entire sector. XXX optimize so we avoid bounce - * buffer in case that qiov->niov =3D=3D 1 + /* Bounce buffer because we're not permitted to touch + * contents of qiov - it points to guest memory. */ cipher_data =3D qemu_try_blockalign(bs->file->bs, MIN(BLOCK_CRYPTO_MAX_SECTORS * 5= 12, --=20 2.13.5 From nobody Tue Nov 4 00:09:41 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1505215883728956.0576508773804; Tue, 12 Sep 2017 04:31:23 -0700 (PDT) Received: from localhost ([::1]:35017 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1drjPW-0004bR-IT for importer@patchew.org; Tue, 12 Sep 2017 07:31:22 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:53812) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1drjNQ-0002zV-MU for qemu-devel@nongnu.org; Tue, 12 Sep 2017 07:29:13 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1drjNP-0004Rb-O9 for qemu-devel@nongnu.org; Tue, 12 Sep 2017 07:29:12 -0400 Received: from mx1.redhat.com ([209.132.183.28]:40242) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1drjNL-0004Nh-Jh; Tue, 12 Sep 2017 07:29:07 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id B8C305D698; Tue, 12 Sep 2017 11:29:06 +0000 (UTC) Received: from localhost.localdomain.com (unknown [10.42.22.189]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2F3186C41C; Tue, 12 Sep 2017 11:29:05 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com B8C305D698 Authentication-Results: ext-mx10.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx10.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=berrange@redhat.com From: "Daniel P. Berrange" To: qemu-devel@nongnu.org Date: Tue, 12 Sep 2017 12:28:50 +0100 Message-Id: <20170912112855.24269-3-berrange@redhat.com> In-Reply-To: <20170912112855.24269-1-berrange@redhat.com> References: <20170912112855.24269-1-berrange@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Tue, 12 Sep 2017 11:29:06 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v3 2/7] crypto: expose encryption sector size in APIs X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , qemu-block@nongnu.org, Stefan Hajnoczi , Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" While current encryption schemes all have a fixed sector size of 512 bytes, this is not guaranteed to be the case in future. Expose the sector size in the APIs so the block layer can remove assumptions about fixed 512 byte sectors. Signed-off-by: Daniel P. Berrange Reviewed-by: Eric Blake Reviewed-by: Max Reitz --- crypto/block-luks.c | 6 ++++-- crypto/block-qcow.c | 1 + crypto/block.c | 6 ++++++ crypto/blockpriv.h | 1 + include/crypto/block.h | 15 +++++++++++++++ 5 files changed, 27 insertions(+), 2 deletions(-) diff --git a/crypto/block-luks.c b/crypto/block-luks.c index 36bc856084..a9062bb0f2 100644 --- a/crypto/block-luks.c +++ b/crypto/block-luks.c @@ -846,8 +846,9 @@ qcrypto_block_luks_open(QCryptoBlock *block, } } =20 + block->sector_size =3D QCRYPTO_BLOCK_LUKS_SECTOR_SIZE; block->payload_offset =3D luks->header.payload_offset * - QCRYPTO_BLOCK_LUKS_SECTOR_SIZE; + block->sector_size; =20 luks->cipher_alg =3D cipheralg; luks->cipher_mode =3D ciphermode; @@ -1240,8 +1241,9 @@ qcrypto_block_luks_create(QCryptoBlock *block, QCRYPTO_BLOCK_LUKS_SECTOR_SIZE)) * QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS); =20 + block->sector_size =3D QCRYPTO_BLOCK_LUKS_SECTOR_SIZE; block->payload_offset =3D luks->header.payload_offset * - QCRYPTO_BLOCK_LUKS_SECTOR_SIZE; + block->sector_size; =20 /* Reserve header space to match payload offset */ initfunc(block, block->payload_offset, opaque, &local_err); diff --git a/crypto/block-qcow.c b/crypto/block-qcow.c index a456fe338b..4dd594a9ba 100644 --- a/crypto/block-qcow.c +++ b/crypto/block-qcow.c @@ -80,6 +80,7 @@ qcrypto_block_qcow_init(QCryptoBlock *block, goto fail; } =20 + block->sector_size =3D QCRYPTO_BLOCK_QCOW_SECTOR_SIZE; block->payload_offset =3D 0; =20 return 0; diff --git a/crypto/block.c b/crypto/block.c index c382393d9a..a7a9ad240e 100644 --- a/crypto/block.c +++ b/crypto/block.c @@ -170,6 +170,12 @@ uint64_t qcrypto_block_get_payload_offset(QCryptoBlock= *block) } =20 =20 +uint64_t qcrypto_block_get_sector_size(QCryptoBlock *block) +{ + return block->sector_size; +} + + void qcrypto_block_free(QCryptoBlock *block) { if (!block) { diff --git a/crypto/blockpriv.h b/crypto/blockpriv.h index 0edb810e22..d227522d88 100644 --- a/crypto/blockpriv.h +++ b/crypto/blockpriv.h @@ -36,6 +36,7 @@ struct QCryptoBlock { QCryptoHashAlgorithm kdfhash; size_t niv; uint64_t payload_offset; /* In bytes */ + uint64_t sector_size; /* In bytes */ }; =20 struct QCryptoBlockDriver { diff --git a/include/crypto/block.h b/include/crypto/block.h index f0e543bee1..13232b2472 100644 --- a/include/crypto/block.h +++ b/include/crypto/block.h @@ -241,6 +241,21 @@ QCryptoHashAlgorithm qcrypto_block_get_kdf_hash(QCrypt= oBlock *block); uint64_t qcrypto_block_get_payload_offset(QCryptoBlock *block); =20 /** + * qcrypto_block_get_sector_size: + * @block: the block encryption object + * + * Get the size of sectors used for payload encryption. A new + * IV is used at the start of each sector. The encryption + * sector size is not required to match the sector size of the + * underlying storage. For example LUKS will always use a 512 + * byte sector size, even if the volume is on a disk with 4k + * sectors. + * + * Returns: the sector in bytes + */ +uint64_t qcrypto_block_get_sector_size(QCryptoBlock *block); + +/** * qcrypto_block_free: * @block: the block encryption object * --=20 2.13.5 From nobody Tue Nov 4 00:09:41 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1505215882814276.48632176902777; Tue, 12 Sep 2017 04:31:22 -0700 (PDT) Received: from localhost ([::1]:35016 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1drjPW-0004aZ-2Z for importer@patchew.org; Tue, 12 Sep 2017 07:31:22 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:53809) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1drjNQ-0002zO-J3 for qemu-devel@nongnu.org; Tue, 12 Sep 2017 07:29:13 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1drjNP-0004RH-GG for qemu-devel@nongnu.org; Tue, 12 Sep 2017 07:29:12 -0400 Received: from mx1.redhat.com ([209.132.183.28]:44276) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1drjNN-0004Pe-4q; Tue, 12 Sep 2017 07:29:09 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 4661081DEF; Tue, 12 Sep 2017 11:29:08 +0000 (UTC) Received: from localhost.localdomain.com (unknown [10.42.22.189]) by smtp.corp.redhat.com (Postfix) with ESMTP id F141D6C41C; Tue, 12 Sep 2017 11:29:06 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 4661081DEF Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=berrange@redhat.com From: "Daniel P. Berrange" To: qemu-devel@nongnu.org Date: Tue, 12 Sep 2017 12:28:51 +0100 Message-Id: <20170912112855.24269-4-berrange@redhat.com> In-Reply-To: <20170912112855.24269-1-berrange@redhat.com> References: <20170912112855.24269-1-berrange@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Tue, 12 Sep 2017 11:29:08 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v3 3/7] block: fix data type casting for crypto payload offset X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , qemu-block@nongnu.org, Stefan Hajnoczi , Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" The crypto APIs report the offset of the data payload as an uint64_t type, but the block driver is casting to size_t or ssize_t which will potentially truncate. Most of the block APIs use int64_t for offsets meanwhile, so even if using uint64_t in the crypto block driver we are still at risk of truncation. Change the block crypto driver to use uint64_t, but add asserts that the value is less than INT64_MAX. Signed-off-by: Daniel P. Berrange Reviewed-by: Eric Blake Reviewed-by: Max Reitz --- block/crypto.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/block/crypto.c b/block/crypto.c index cc8afe0e0d..d68cbac2ac 100644 --- a/block/crypto.c +++ b/block/crypto.c @@ -364,8 +364,9 @@ static int block_crypto_truncate(BlockDriverState *bs, = int64_t offset, PreallocMode prealloc, Error **errp) { BlockCrypto *crypto =3D bs->opaque; - size_t payload_offset =3D + uint64_t payload_offset =3D qcrypto_block_get_payload_offset(crypto->block); + assert(payload_offset < (INT64_MAX - offset)); =20 offset +=3D payload_offset; =20 @@ -391,8 +392,9 @@ block_crypto_co_readv(BlockDriverState *bs, int64_t sec= tor_num, uint8_t *cipher_data =3D NULL; QEMUIOVector hd_qiov; int ret =3D 0; - size_t payload_offset =3D + uint64_t payload_offset =3D qcrypto_block_get_payload_offset(crypto->block) / 512; + assert(payload_offset < (INT64_MAX / 512)); =20 qemu_iovec_init(&hd_qiov, qiov->niov); =20 @@ -458,8 +460,9 @@ block_crypto_co_writev(BlockDriverState *bs, int64_t se= ctor_num, uint8_t *cipher_data =3D NULL; QEMUIOVector hd_qiov; int ret =3D 0; - size_t payload_offset =3D + uint64_t payload_offset =3D qcrypto_block_get_payload_offset(crypto->block) / 512; + assert(payload_offset < (INT64_MAX / 512)); =20 qemu_iovec_init(&hd_qiov, qiov->niov); =20 @@ -520,7 +523,9 @@ static int64_t block_crypto_getlength(BlockDriverState = *bs) BlockCrypto *crypto =3D bs->opaque; int64_t len =3D bdrv_getlength(bs->file->bs); =20 - ssize_t offset =3D qcrypto_block_get_payload_offset(crypto->block); + uint64_t offset =3D qcrypto_block_get_payload_offset(crypto->block); + assert(offset < INT64_MAX); + assert(offset < len); =20 len -=3D offset; =20 --=20 2.13.5 From nobody Tue Nov 4 00:09:41 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1505216160282555.2776101956143; Tue, 12 Sep 2017 04:36:00 -0700 (PDT) Received: from localhost ([::1]:35039 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1drjTz-0008Vo-JI for importer@patchew.org; Tue, 12 Sep 2017 07:35:59 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:53866) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1drjNU-00033r-3Y for qemu-devel@nongnu.org; Tue, 12 Sep 2017 07:29:17 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1drjNR-0004Sd-FY for qemu-devel@nongnu.org; Tue, 12 Sep 2017 07:29:16 -0400 Received: from mx1.redhat.com ([209.132.183.28]:41148) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1drjNO-0004QV-Mp; Tue, 12 Sep 2017 07:29:10 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id C015F8553D; Tue, 12 Sep 2017 11:29:09 +0000 (UTC) Received: from localhost.localdomain.com (unknown [10.42.22.189]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7F37E6C41C; Tue, 12 Sep 2017 11:29:08 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com C015F8553D Authentication-Results: ext-mx04.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx04.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=berrange@redhat.com From: "Daniel P. Berrange" To: qemu-devel@nongnu.org Date: Tue, 12 Sep 2017 12:28:52 +0100 Message-Id: <20170912112855.24269-5-berrange@redhat.com> In-Reply-To: <20170912112855.24269-1-berrange@redhat.com> References: <20170912112855.24269-1-berrange@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Tue, 12 Sep 2017 11:29:09 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v3 4/7] block: don't use constant 512 as sector size in crypto driver X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , qemu-block@nongnu.org, Stefan Hajnoczi , Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Use the qcrypto_block_get_sector_size() value in the block crypto driver instead of hardcoding 512 as the sector size. Signed-off-by: Daniel P. Berrange --- block/crypto.c | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/block/crypto.c b/block/crypto.c index d68cbac2ac..49d6d4c058 100644 --- a/block/crypto.c +++ b/block/crypto.c @@ -392,8 +392,9 @@ block_crypto_co_readv(BlockDriverState *bs, int64_t sec= tor_num, uint8_t *cipher_data =3D NULL; QEMUIOVector hd_qiov; int ret =3D 0; + uint64_t sector_size =3D qcrypto_block_get_sector_size(crypto->block); uint64_t payload_offset =3D - qcrypto_block_get_payload_offset(crypto->block) / 512; + qcrypto_block_get_payload_offset(crypto->block) / sector_size; assert(payload_offset < (INT64_MAX / 512)); =20 qemu_iovec_init(&hd_qiov, qiov->niov); @@ -401,9 +402,9 @@ block_crypto_co_readv(BlockDriverState *bs, int64_t sec= tor_num, /* Bounce buffer because we don't wish to expose cipher text * in qiov which points to guest memory. */ - cipher_data =3D - qemu_try_blockalign(bs->file->bs, MIN(BLOCK_CRYPTO_MAX_SECTORS * 5= 12, - qiov->size)); + cipher_data =3D qemu_try_blockalign( + bs->file->bs, MIN(BLOCK_CRYPTO_MAX_SECTORS * sector_size, + qiov->size)); if (cipher_data =3D=3D NULL) { ret =3D -ENOMEM; goto cleanup; @@ -417,7 +418,7 @@ block_crypto_co_readv(BlockDriverState *bs, int64_t sec= tor_num, } =20 qemu_iovec_reset(&hd_qiov); - qemu_iovec_add(&hd_qiov, cipher_data, cur_nr_sectors * 512); + qemu_iovec_add(&hd_qiov, cipher_data, cur_nr_sectors * sector_size= ); =20 ret =3D bdrv_co_readv(bs->file, payload_offset + sector_num, @@ -428,18 +429,18 @@ block_crypto_co_readv(BlockDriverState *bs, int64_t s= ector_num, =20 if (qcrypto_block_decrypt(crypto->block, sector_num, - cipher_data, cur_nr_sectors * 512, + cipher_data, cur_nr_sectors * sector_siz= e, NULL) < 0) { ret =3D -EIO; goto cleanup; } =20 qemu_iovec_from_buf(qiov, bytes_done, - cipher_data, cur_nr_sectors * 512); + cipher_data, cur_nr_sectors * sector_size); =20 remaining_sectors -=3D cur_nr_sectors; sector_num +=3D cur_nr_sectors; - bytes_done +=3D cur_nr_sectors * 512; + bytes_done +=3D cur_nr_sectors * sector_size; } =20 cleanup: @@ -460,8 +461,9 @@ block_crypto_co_writev(BlockDriverState *bs, int64_t se= ctor_num, uint8_t *cipher_data =3D NULL; QEMUIOVector hd_qiov; int ret =3D 0; + uint64_t sector_size =3D qcrypto_block_get_sector_size(crypto->block); uint64_t payload_offset =3D - qcrypto_block_get_payload_offset(crypto->block) / 512; + qcrypto_block_get_payload_offset(crypto->block) / sector_size; assert(payload_offset < (INT64_MAX / 512)); =20 qemu_iovec_init(&hd_qiov, qiov->niov); @@ -469,9 +471,9 @@ block_crypto_co_writev(BlockDriverState *bs, int64_t se= ctor_num, /* Bounce buffer because we're not permitted to touch * contents of qiov - it points to guest memory. */ - cipher_data =3D - qemu_try_blockalign(bs->file->bs, MIN(BLOCK_CRYPTO_MAX_SECTORS * 5= 12, - qiov->size)); + cipher_data =3D qemu_try_blockalign( + bs->file->bs, MIN(BLOCK_CRYPTO_MAX_SECTORS * sector_size, + qiov->size)); if (cipher_data =3D=3D NULL) { ret =3D -ENOMEM; goto cleanup; @@ -485,18 +487,18 @@ block_crypto_co_writev(BlockDriverState *bs, int64_t = sector_num, } =20 qemu_iovec_to_buf(qiov, bytes_done, - cipher_data, cur_nr_sectors * 512); + cipher_data, cur_nr_sectors * sector_size); =20 if (qcrypto_block_encrypt(crypto->block, sector_num, - cipher_data, cur_nr_sectors * 512, + cipher_data, cur_nr_sectors * sector_siz= e, NULL) < 0) { ret =3D -EIO; goto cleanup; } =20 qemu_iovec_reset(&hd_qiov); - qemu_iovec_add(&hd_qiov, cipher_data, cur_nr_sectors * 512); + qemu_iovec_add(&hd_qiov, cipher_data, cur_nr_sectors * sector_size= ); =20 ret =3D bdrv_co_writev(bs->file, payload_offset + sector_num, @@ -507,7 +509,7 @@ block_crypto_co_writev(BlockDriverState *bs, int64_t se= ctor_num, =20 remaining_sectors -=3D cur_nr_sectors; sector_num +=3D cur_nr_sectors; - bytes_done +=3D cur_nr_sectors * 512; + bytes_done +=3D cur_nr_sectors * sector_size; } =20 cleanup: --=20 2.13.5 From nobody Tue Nov 4 00:09:41 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1505216024060830.4938529791143; Tue, 12 Sep 2017 04:33:44 -0700 (PDT) Received: from localhost ([::1]:35026 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1drjRn-0006Xv-9e for importer@patchew.org; Tue, 12 Sep 2017 07:33:43 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:53893) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1drjNV-00035Y-Io for qemu-devel@nongnu.org; Tue, 12 Sep 2017 07:29:18 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1drjNU-0004UE-8P for qemu-devel@nongnu.org; Tue, 12 Sep 2017 07:29:17 -0400 Received: from mx1.redhat.com ([209.132.183.28]:38578) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1drjNQ-0004RW-7w; Tue, 12 Sep 2017 07:29:12 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 3D8E980F75; Tue, 12 Sep 2017 11:29:11 +0000 (UTC) Received: from localhost.localdomain.com (unknown [10.42.22.189]) by smtp.corp.redhat.com (Postfix) with ESMTP id 065D56C41C; Tue, 12 Sep 2017 11:29:09 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 3D8E980F75 Authentication-Results: ext-mx03.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx03.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=berrange@redhat.com From: "Daniel P. Berrange" To: qemu-devel@nongnu.org Date: Tue, 12 Sep 2017 12:28:53 +0100 Message-Id: <20170912112855.24269-6-berrange@redhat.com> In-Reply-To: <20170912112855.24269-1-berrange@redhat.com> References: <20170912112855.24269-1-berrange@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Tue, 12 Sep 2017 11:29:11 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v3 5/7] block: convert crypto driver to bdrv_co_preadv|pwritev X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , qemu-block@nongnu.org, Stefan Hajnoczi , Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Make the crypto driver implement the bdrv_co_preadv|pwritev callbacks, and also use bdrv_co_preadv|pwritev for I/O with the protocol driver beneath. Signed-off-by: Daniel P. Berrange Reviewed-by: Max Reitz --- block/crypto.c | 104 +++++++++++++++++++++++++++++++----------------------= ---- 1 file changed, 56 insertions(+), 48 deletions(-) diff --git a/block/crypto.c b/block/crypto.c index 49d6d4c058..d004e9cef4 100644 --- a/block/crypto.c +++ b/block/crypto.c @@ -383,19 +383,23 @@ static void block_crypto_close(BlockDriverState *bs) #define BLOCK_CRYPTO_MAX_SECTORS 2048 =20 static coroutine_fn int -block_crypto_co_readv(BlockDriverState *bs, int64_t sector_num, - int remaining_sectors, QEMUIOVector *qiov) +block_crypto_co_preadv(BlockDriverState *bs, uint64_t offset, uint64_t byt= es, + QEMUIOVector *qiov, int flags) { BlockCrypto *crypto =3D bs->opaque; - int cur_nr_sectors; /* number of sectors in current iteration */ + uint64_t cur_bytes; /* number of bytes in current iteration */ uint64_t bytes_done =3D 0; uint8_t *cipher_data =3D NULL; QEMUIOVector hd_qiov; int ret =3D 0; uint64_t sector_size =3D qcrypto_block_get_sector_size(crypto->block); - uint64_t payload_offset =3D - qcrypto_block_get_payload_offset(crypto->block) / sector_size; - assert(payload_offset < (INT64_MAX / 512)); + size_t payload_offset =3D qcrypto_block_get_payload_offset(crypto->blo= ck); + uint64_t sector_num =3D offset / sector_size; + + assert(!flags); + assert(payload_offset < INT64_MAX); + assert(QEMU_IS_ALIGNED(offset, sector_size)); + assert(QEMU_IS_ALIGNED(bytes, sector_size)); =20 qemu_iovec_init(&hd_qiov, qiov->niov); =20 @@ -410,37 +414,33 @@ block_crypto_co_readv(BlockDriverState *bs, int64_t s= ector_num, goto cleanup; } =20 - while (remaining_sectors) { - cur_nr_sectors =3D remaining_sectors; + while (bytes) { + cur_bytes =3D bytes; =20 - if (cur_nr_sectors > BLOCK_CRYPTO_MAX_SECTORS) { - cur_nr_sectors =3D BLOCK_CRYPTO_MAX_SECTORS; + if (cur_bytes > (BLOCK_CRYPTO_MAX_SECTORS * sector_size)) { + cur_bytes =3D BLOCK_CRYPTO_MAX_SECTORS * sector_size; } =20 qemu_iovec_reset(&hd_qiov); - qemu_iovec_add(&hd_qiov, cipher_data, cur_nr_sectors * sector_size= ); + qemu_iovec_add(&hd_qiov, cipher_data, cur_bytes); =20 - ret =3D bdrv_co_readv(bs->file, - payload_offset + sector_num, - cur_nr_sectors, &hd_qiov); + ret =3D bdrv_co_preadv(bs->file, payload_offset + offset + bytes_d= one, + cur_bytes, &hd_qiov, 0); if (ret < 0) { goto cleanup; } =20 - if (qcrypto_block_decrypt(crypto->block, - sector_num, - cipher_data, cur_nr_sectors * sector_siz= e, - NULL) < 0) { + if (qcrypto_block_decrypt(crypto->block, sector_num, cipher_data, + cur_bytes, NULL) < 0) { ret =3D -EIO; goto cleanup; } =20 - qemu_iovec_from_buf(qiov, bytes_done, - cipher_data, cur_nr_sectors * sector_size); + qemu_iovec_from_buf(qiov, bytes_done, cipher_data, cur_bytes); =20 - remaining_sectors -=3D cur_nr_sectors; - sector_num +=3D cur_nr_sectors; - bytes_done +=3D cur_nr_sectors * sector_size; + sector_num +=3D cur_bytes / sector_size; + bytes -=3D cur_bytes; + bytes_done +=3D cur_bytes; } =20 cleanup: @@ -452,19 +452,23 @@ block_crypto_co_readv(BlockDriverState *bs, int64_t s= ector_num, =20 =20 static coroutine_fn int -block_crypto_co_writev(BlockDriverState *bs, int64_t sector_num, - int remaining_sectors, QEMUIOVector *qiov) +block_crypto_co_pwritev(BlockDriverState *bs, uint64_t offset, uint64_t by= tes, + QEMUIOVector *qiov, int flags) { BlockCrypto *crypto =3D bs->opaque; - int cur_nr_sectors; /* number of sectors in current iteration */ + uint64_t cur_bytes; /* number of bytes in current iteration */ uint64_t bytes_done =3D 0; uint8_t *cipher_data =3D NULL; QEMUIOVector hd_qiov; int ret =3D 0; uint64_t sector_size =3D qcrypto_block_get_sector_size(crypto->block); - uint64_t payload_offset =3D - qcrypto_block_get_payload_offset(crypto->block) / sector_size; - assert(payload_offset < (INT64_MAX / 512)); + uint64_t payload_offset =3D qcrypto_block_get_payload_offset(crypto->b= lock); + uint64_t sector_num =3D offset / sector_size; + + assert(!flags); + assert(payload_offset < INT64_MAX); + assert(QEMU_IS_ALIGNED(offset, sector_size)); + assert(QEMU_IS_ALIGNED(bytes, sector_size)); =20 qemu_iovec_init(&hd_qiov, qiov->niov); =20 @@ -479,37 +483,33 @@ block_crypto_co_writev(BlockDriverState *bs, int64_t = sector_num, goto cleanup; } =20 - while (remaining_sectors) { - cur_nr_sectors =3D remaining_sectors; + while (bytes) { + cur_bytes =3D bytes; =20 - if (cur_nr_sectors > BLOCK_CRYPTO_MAX_SECTORS) { - cur_nr_sectors =3D BLOCK_CRYPTO_MAX_SECTORS; + if (cur_bytes > (BLOCK_CRYPTO_MAX_SECTORS * sector_size)) { + cur_bytes =3D BLOCK_CRYPTO_MAX_SECTORS * sector_size; } =20 - qemu_iovec_to_buf(qiov, bytes_done, - cipher_data, cur_nr_sectors * sector_size); + qemu_iovec_to_buf(qiov, bytes_done, cipher_data, cur_bytes); =20 - if (qcrypto_block_encrypt(crypto->block, - sector_num, - cipher_data, cur_nr_sectors * sector_siz= e, - NULL) < 0) { + if (qcrypto_block_encrypt(crypto->block, sector_num, cipher_data, + cur_bytes, NULL) < 0) { ret =3D -EIO; goto cleanup; } =20 qemu_iovec_reset(&hd_qiov); - qemu_iovec_add(&hd_qiov, cipher_data, cur_nr_sectors * sector_size= ); + qemu_iovec_add(&hd_qiov, cipher_data, cur_bytes); =20 - ret =3D bdrv_co_writev(bs->file, - payload_offset + sector_num, - cur_nr_sectors, &hd_qiov); + ret =3D bdrv_co_pwritev(bs->file, payload_offset + offset + bytes_= done, + cur_bytes, &hd_qiov, 0); if (ret < 0) { goto cleanup; } =20 - remaining_sectors -=3D cur_nr_sectors; - sector_num +=3D cur_nr_sectors; - bytes_done +=3D cur_nr_sectors * sector_size; + sector_num +=3D cur_bytes / sector_size; + bytes -=3D cur_bytes; + bytes_done +=3D cur_bytes; } =20 cleanup: @@ -519,6 +519,13 @@ block_crypto_co_writev(BlockDriverState *bs, int64_t s= ector_num, return ret; } =20 +static void block_crypto_refresh_limits(BlockDriverState *bs, Error **errp) +{ + BlockCrypto *crypto =3D bs->opaque; + uint64_t sector_size =3D qcrypto_block_get_sector_size(crypto->block); + bs->bl.request_alignment =3D sector_size; /* No sub-sector I/O */ +} + =20 static int64_t block_crypto_getlength(BlockDriverState *bs) { @@ -618,8 +625,9 @@ BlockDriver bdrv_crypto_luks =3D { .bdrv_truncate =3D block_crypto_truncate, .create_opts =3D &block_crypto_create_opts_luks, =20 - .bdrv_co_readv =3D block_crypto_co_readv, - .bdrv_co_writev =3D block_crypto_co_writev, + .bdrv_refresh_limits =3D block_crypto_refresh_limits, + .bdrv_co_preadv =3D block_crypto_co_preadv, + .bdrv_co_pwritev =3D block_crypto_co_pwritev, .bdrv_getlength =3D block_crypto_getlength, .bdrv_get_info =3D block_crypto_get_info_luks, .bdrv_get_specific_info =3D block_crypto_get_specific_info_luks, --=20 2.13.5 From nobody Tue Nov 4 00:09:41 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1505215895574710.465086986829; Tue, 12 Sep 2017 04:31:35 -0700 (PDT) Received: from localhost ([::1]:35018 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1drjPi-0004ii-NY for importer@patchew.org; Tue, 12 Sep 2017 07:31:34 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:53925) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1drjNX-00038b-RF for qemu-devel@nongnu.org; Tue, 12 Sep 2017 07:29:21 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1drjNW-0004V3-0o for qemu-devel@nongnu.org; Tue, 12 Sep 2017 07:29:19 -0400 Received: from mx1.redhat.com ([209.132.183.28]:38634) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1drjNR-0004SR-MJ; Tue, 12 Sep 2017 07:29:13 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id C5C4C83F42; Tue, 12 Sep 2017 11:29:12 +0000 (UTC) Received: from localhost.localdomain.com (unknown [10.42.22.189]) by smtp.corp.redhat.com (Postfix) with ESMTP id 756D16C41C; Tue, 12 Sep 2017 11:29:11 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com C5C4C83F42 Authentication-Results: ext-mx03.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx03.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=berrange@redhat.com From: "Daniel P. Berrange" To: qemu-devel@nongnu.org Date: Tue, 12 Sep 2017 12:28:54 +0100 Message-Id: <20170912112855.24269-7-berrange@redhat.com> In-Reply-To: <20170912112855.24269-1-berrange@redhat.com> References: <20170912112855.24269-1-berrange@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Tue, 12 Sep 2017 11:29:12 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v3 6/7] block: convert qcrypto_block_encrypt|decrypt to take bytes offset X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , qemu-block@nongnu.org, Stefan Hajnoczi , Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Instead of sector offset, take the bytes offset when encrypting or decrypting data. Signed-off-by: Daniel P. Berrange --- block/crypto.c | 12 ++++-------- block/qcow.c | 7 +++++-- block/qcow2-cluster.c | 8 +++----- block/qcow2.c | 4 ++-- crypto/block-luks.c | 12 ++++++++---- crypto/block-qcow.c | 12 ++++++++---- crypto/block.c | 20 ++++++++++++++------ crypto/blockpriv.h | 4 ++-- include/crypto/block.h | 14 ++++++++------ 9 files changed, 54 insertions(+), 39 deletions(-) diff --git a/block/crypto.c b/block/crypto.c index d004e9cef4..1f77095336 100644 --- a/block/crypto.c +++ b/block/crypto.c @@ -394,7 +394,6 @@ block_crypto_co_preadv(BlockDriverState *bs, uint64_t o= ffset, uint64_t bytes, int ret =3D 0; uint64_t sector_size =3D qcrypto_block_get_sector_size(crypto->block); size_t payload_offset =3D qcrypto_block_get_payload_offset(crypto->blo= ck); - uint64_t sector_num =3D offset / sector_size; =20 assert(!flags); assert(payload_offset < INT64_MAX); @@ -430,15 +429,14 @@ block_crypto_co_preadv(BlockDriverState *bs, uint64_t= offset, uint64_t bytes, goto cleanup; } =20 - if (qcrypto_block_decrypt(crypto->block, sector_num, cipher_data, - cur_bytes, NULL) < 0) { + if (qcrypto_block_decrypt(crypto->block, offset + bytes_done, + cipher_data, cur_bytes, NULL) < 0) { ret =3D -EIO; goto cleanup; } =20 qemu_iovec_from_buf(qiov, bytes_done, cipher_data, cur_bytes); =20 - sector_num +=3D cur_bytes / sector_size; bytes -=3D cur_bytes; bytes_done +=3D cur_bytes; } @@ -463,7 +461,6 @@ block_crypto_co_pwritev(BlockDriverState *bs, uint64_t = offset, uint64_t bytes, int ret =3D 0; uint64_t sector_size =3D qcrypto_block_get_sector_size(crypto->block); uint64_t payload_offset =3D qcrypto_block_get_payload_offset(crypto->b= lock); - uint64_t sector_num =3D offset / sector_size; =20 assert(!flags); assert(payload_offset < INT64_MAX); @@ -492,8 +489,8 @@ block_crypto_co_pwritev(BlockDriverState *bs, uint64_t = offset, uint64_t bytes, =20 qemu_iovec_to_buf(qiov, bytes_done, cipher_data, cur_bytes); =20 - if (qcrypto_block_encrypt(crypto->block, sector_num, cipher_data, - cur_bytes, NULL) < 0) { + if (qcrypto_block_encrypt(crypto->block, offset + bytes_done, + cipher_data, cur_bytes, NULL) < 0) { ret =3D -EIO; goto cleanup; } @@ -507,7 +504,6 @@ block_crypto_co_pwritev(BlockDriverState *bs, uint64_t = offset, uint64_t bytes, goto cleanup; } =20 - sector_num +=3D cur_bytes / sector_size; bytes -=3D cur_bytes; bytes_done +=3D cur_bytes; } diff --git a/block/qcow.c b/block/qcow.c index f450b00cfc..d242301ed2 100644 --- a/block/qcow.c +++ b/block/qcow.c @@ -478,7 +478,9 @@ static int get_cluster_offset(BlockDriverState *bs, for(i =3D 0; i < s->cluster_sectors; i++) { if (i < n_start || i >=3D n_end) { memset(s->cluster_data, 0x00, 512); - if (qcrypto_block_encrypt(s->crypto, start_sec= t + i, + if (qcrypto_block_encrypt(s->crypto, + (start_sect + i) * + BDRV_SECTOR_SIZE, s->cluster_data, BDRV_SECTOR_SIZE, NULL) < 0) { @@ -668,7 +670,8 @@ static coroutine_fn int qcow_co_readv(BlockDriverState = *bs, int64_t sector_num, } if (bs->encrypted) { assert(s->crypto); - if (qcrypto_block_decrypt(s->crypto, sector_num, buf, + if (qcrypto_block_decrypt(s->crypto, + sector_num * BDRV_SECTOR_SIZE, b= uf, n * BDRV_SECTOR_SIZE, NULL) < 0)= { ret =3D -EIO; break; diff --git a/block/qcow2-cluster.c b/block/qcow2-cluster.c index 0d4824993c..09ae0b6ecc 100644 --- a/block/qcow2-cluster.c +++ b/block/qcow2-cluster.c @@ -396,15 +396,13 @@ static bool coroutine_fn do_perform_cow_encrypt(Block= DriverState *bs, { if (bytes && bs->encrypted) { BDRVQcow2State *s =3D bs->opaque; - int64_t sector =3D (s->crypt_physical_offset ? + int64_t offset =3D (s->crypt_physical_offset ? (cluster_offset + offset_in_cluster) : - (src_cluster_offset + offset_in_cluster)) - >> BDRV_SECTOR_BITS; + (src_cluster_offset + offset_in_cluster)); assert((offset_in_cluster & ~BDRV_SECTOR_MASK) =3D=3D 0); assert((bytes & ~BDRV_SECTOR_MASK) =3D=3D 0); assert(s->crypto); - if (qcrypto_block_encrypt(s->crypto, sector, buffer, - bytes, NULL) < 0) { + if (qcrypto_block_encrypt(s->crypto, offset, buffer, bytes, NULL) = < 0) { return false; } } diff --git a/block/qcow2.c b/block/qcow2.c index bae5893327..4508683ea3 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -1810,7 +1810,7 @@ static coroutine_fn int qcow2_co_preadv(BlockDriverSt= ate *bs, uint64_t offset, if (qcrypto_block_decrypt(s->crypto, (s->crypt_physical_offset ? cluster_offset + offset_in_clus= ter : - offset) >> BDRV_SECTOR_BITS, + offset), cluster_data, cur_bytes, NULL) < 0) { @@ -1945,7 +1945,7 @@ static coroutine_fn int qcow2_co_pwritev(BlockDriverS= tate *bs, uint64_t offset, if (qcrypto_block_encrypt(s->crypto, (s->crypt_physical_offset ? cluster_offset + offset_in_cluster : - offset) >> BDRV_SECTOR_BITS, + offset), cluster_data, cur_bytes, NULL) < 0) { ret =3D -EIO; diff --git a/crypto/block-luks.c b/crypto/block-luks.c index a9062bb0f2..d418ac30b8 100644 --- a/crypto/block-luks.c +++ b/crypto/block-luks.c @@ -1399,29 +1399,33 @@ static void qcrypto_block_luks_cleanup(QCryptoBlock= *block) =20 static int qcrypto_block_luks_decrypt(QCryptoBlock *block, - uint64_t startsector, + uint64_t offset, uint8_t *buf, size_t len, Error **errp) { + assert(QEMU_IS_ALIGNED(offset, QCRYPTO_BLOCK_LUKS_SECTOR_SIZE)); + assert(QEMU_IS_ALIGNED(len, QCRYPTO_BLOCK_LUKS_SECTOR_SIZE)); return qcrypto_block_decrypt_helper(block->cipher, block->niv, block->ivgen, QCRYPTO_BLOCK_LUKS_SECTOR_SIZE, - startsector, buf, len, errp); + offset, buf, len, errp); } =20 =20 static int qcrypto_block_luks_encrypt(QCryptoBlock *block, - uint64_t startsector, + uint64_t offset, uint8_t *buf, size_t len, Error **errp) { + assert(QEMU_IS_ALIGNED(offset, QCRYPTO_BLOCK_LUKS_SECTOR_SIZE)); + assert(QEMU_IS_ALIGNED(len, QCRYPTO_BLOCK_LUKS_SECTOR_SIZE)); return qcrypto_block_encrypt_helper(block->cipher, block->niv, block->ivgen, QCRYPTO_BLOCK_LUKS_SECTOR_SIZE, - startsector, buf, len, errp); + offset, buf, len, errp); } =20 =20 diff --git a/crypto/block-qcow.c b/crypto/block-qcow.c index 4dd594a9ba..8817d6aaa7 100644 --- a/crypto/block-qcow.c +++ b/crypto/block-qcow.c @@ -143,29 +143,33 @@ qcrypto_block_qcow_cleanup(QCryptoBlock *block) =20 static int qcrypto_block_qcow_decrypt(QCryptoBlock *block, - uint64_t startsector, + uint64_t offset, uint8_t *buf, size_t len, Error **errp) { + assert(QEMU_IS_ALIGNED(offset, QCRYPTO_BLOCK_QCOW_SECTOR_SIZE)); + assert(QEMU_IS_ALIGNED(len, QCRYPTO_BLOCK_QCOW_SECTOR_SIZE)); return qcrypto_block_decrypt_helper(block->cipher, block->niv, block->ivgen, QCRYPTO_BLOCK_QCOW_SECTOR_SIZE, - startsector, buf, len, errp); + offset, buf, len, errp); } =20 =20 static int qcrypto_block_qcow_encrypt(QCryptoBlock *block, - uint64_t startsector, + uint64_t offset, uint8_t *buf, size_t len, Error **errp) { + assert(QEMU_IS_ALIGNED(offset, QCRYPTO_BLOCK_QCOW_SECTOR_SIZE)); + assert(QEMU_IS_ALIGNED(len, QCRYPTO_BLOCK_QCOW_SECTOR_SIZE)); return qcrypto_block_encrypt_helper(block->cipher, block->niv, block->ivgen, QCRYPTO_BLOCK_QCOW_SECTOR_SIZE, - startsector, buf, len, errp); + offset, buf, len, errp); } =20 =20 diff --git a/crypto/block.c b/crypto/block.c index a7a9ad240e..f206d5eea8 100644 --- a/crypto/block.c +++ b/crypto/block.c @@ -127,22 +127,22 @@ QCryptoBlockInfo *qcrypto_block_get_info(QCryptoBlock= *block, =20 =20 int qcrypto_block_decrypt(QCryptoBlock *block, - uint64_t startsector, + uint64_t offset, uint8_t *buf, size_t len, Error **errp) { - return block->driver->decrypt(block, startsector, buf, len, errp); + return block->driver->decrypt(block, offset, buf, len, errp); } =20 =20 int qcrypto_block_encrypt(QCryptoBlock *block, - uint64_t startsector, + uint64_t offset, uint8_t *buf, size_t len, Error **errp) { - return block->driver->encrypt(block, startsector, buf, len, errp); + return block->driver->encrypt(block, offset, buf, len, errp); } =20 =20 @@ -194,13 +194,17 @@ int qcrypto_block_decrypt_helper(QCryptoCipher *ciphe= r, size_t niv, QCryptoIVGen *ivgen, int sectorsize, - uint64_t startsector, + uint64_t offset, uint8_t *buf, size_t len, Error **errp) { uint8_t *iv; int ret =3D -1; + uint64_t startsector =3D offset / sectorsize; + + assert(QEMU_IS_ALIGNED(offset, sectorsize)); + assert(QEMU_IS_ALIGNED(len, sectorsize)); =20 iv =3D niv ? g_new0(uint8_t, niv) : NULL; =20 @@ -243,13 +247,17 @@ int qcrypto_block_encrypt_helper(QCryptoCipher *ciphe= r, size_t niv, QCryptoIVGen *ivgen, int sectorsize, - uint64_t startsector, + uint64_t offset, uint8_t *buf, size_t len, Error **errp) { uint8_t *iv; int ret =3D -1; + uint64_t startsector =3D offset / sectorsize; + + assert(QEMU_IS_ALIGNED(offset, sectorsize)); + assert(QEMU_IS_ALIGNED(len, sectorsize)); =20 iv =3D niv ? g_new0(uint8_t, niv) : NULL; =20 diff --git a/crypto/blockpriv.h b/crypto/blockpriv.h index d227522d88..41840abcec 100644 --- a/crypto/blockpriv.h +++ b/crypto/blockpriv.h @@ -82,7 +82,7 @@ int qcrypto_block_decrypt_helper(QCryptoCipher *cipher, size_t niv, QCryptoIVGen *ivgen, int sectorsize, - uint64_t startsector, + uint64_t offset, uint8_t *buf, size_t len, Error **errp); @@ -91,7 +91,7 @@ int qcrypto_block_encrypt_helper(QCryptoCipher *cipher, size_t niv, QCryptoIVGen *ivgen, int sectorsize, - uint64_t startsector, + uint64_t offset, uint8_t *buf, size_t len, Error **errp); diff --git a/include/crypto/block.h b/include/crypto/block.h index 13232b2472..cd18f46d56 100644 --- a/include/crypto/block.h +++ b/include/crypto/block.h @@ -161,18 +161,19 @@ QCryptoBlockInfo *qcrypto_block_get_info(QCryptoBlock= *block, /** * @qcrypto_block_decrypt: * @block: the block encryption object - * @startsector: the sector from which @buf was read + * @offset: the position at which @iov was read * @buf: the buffer to decrypt * @len: the length of @buf in bytes * @errp: pointer to a NULL-initialized error object * * Decrypt @len bytes of cipher text in @buf, writing - * plain text back into @buf + * plain text back into @buf. @len and @offset must be + * a multiple of the encryption format sector size. * * Returns 0 on success, -1 on failure */ int qcrypto_block_decrypt(QCryptoBlock *block, - uint64_t startsector, + uint64_t offset, uint8_t *buf, size_t len, Error **errp); @@ -180,18 +181,19 @@ int qcrypto_block_decrypt(QCryptoBlock *block, /** * @qcrypto_block_encrypt: * @block: the block encryption object - * @startsector: the sector to which @buf will be written + * @offset: the position at which @iov will be written * @buf: the buffer to decrypt * @len: the length of @buf in bytes * @errp: pointer to a NULL-initialized error object * * Encrypt @len bytes of plain text in @buf, writing - * cipher text back into @buf + * cipher text back into @buf. @len and @offset must be + * a multiple of the encryption format sector size. * * Returns 0 on success, -1 on failure */ int qcrypto_block_encrypt(QCryptoBlock *block, - uint64_t startsector, + uint64_t offset, uint8_t *buf, size_t len, Error **errp); --=20 2.13.5 From nobody Tue Nov 4 00:09:41 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1505216151764566.6589996726075; Tue, 12 Sep 2017 04:35:51 -0700 (PDT) Received: from localhost ([::1]:35038 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1drjTr-0008No-3R for importer@patchew.org; Tue, 12 Sep 2017 07:35:51 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:53900) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1drjNW-00036N-5F for qemu-devel@nongnu.org; Tue, 12 Sep 2017 07:29:19 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1drjNV-0004Ug-AP for qemu-devel@nongnu.org; Tue, 12 Sep 2017 07:29:18 -0400 Received: from mx1.redhat.com ([209.132.183.28]:44456) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1drjNT-0004TI-64; Tue, 12 Sep 2017 07:29:15 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 3854881DE9; Tue, 12 Sep 2017 11:29:14 +0000 (UTC) Received: from localhost.localdomain.com (unknown [10.42.22.189]) by smtp.corp.redhat.com (Postfix) with ESMTP id 0879F6C41C; Tue, 12 Sep 2017 11:29:12 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 3854881DE9 Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=berrange@redhat.com From: "Daniel P. Berrange" To: qemu-devel@nongnu.org Date: Tue, 12 Sep 2017 12:28:55 +0100 Message-Id: <20170912112855.24269-8-berrange@redhat.com> In-Reply-To: <20170912112855.24269-1-berrange@redhat.com> References: <20170912112855.24269-1-berrange@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Tue, 12 Sep 2017 11:29:14 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v3 7/7] block: support passthrough of BDRV_REQ_FUA in crypto driver X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , qemu-block@nongnu.org, Stefan Hajnoczi , Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" The BDRV_REQ_FUA flag can trivially be allowed in the crypt driver as a passthrough to the underlying block driver. Signed-off-by: Daniel P. Berrange Reviewed-by: Eric Blake Reviewed-by: Max Reitz --- block/crypto.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/block/crypto.c b/block/crypto.c index 1f77095336..d1c28d62c1 100644 --- a/block/crypto.c +++ b/block/crypto.c @@ -279,6 +279,9 @@ static int block_crypto_open_generic(QCryptoBlockFormat= format, return -EINVAL; } =20 + bs->supported_write_flags =3D BDRV_REQ_FUA & + bs->file->bs->supported_write_flags; + opts =3D qemu_opts_create(opts_spec, NULL, 0, &error_abort); qemu_opts_absorb_qdict(opts, options, &local_err); if (local_err) { @@ -462,7 +465,7 @@ block_crypto_co_pwritev(BlockDriverState *bs, uint64_t = offset, uint64_t bytes, uint64_t sector_size =3D qcrypto_block_get_sector_size(crypto->block); uint64_t payload_offset =3D qcrypto_block_get_payload_offset(crypto->b= lock); =20 - assert(!flags); + assert(!(flags & ~BDRV_REQ_FUA)); assert(payload_offset < INT64_MAX); assert(QEMU_IS_ALIGNED(offset, sector_size)); assert(QEMU_IS_ALIGNED(bytes, sector_size)); @@ -499,7 +502,7 @@ block_crypto_co_pwritev(BlockDriverState *bs, uint64_t = offset, uint64_t bytes, qemu_iovec_add(&hd_qiov, cipher_data, cur_bytes); =20 ret =3D bdrv_co_pwritev(bs->file, payload_offset + offset + bytes_= done, - cur_bytes, &hd_qiov, 0); + cur_bytes, &hd_qiov, flags); if (ret < 0) { goto cleanup; } --=20 2.13.5