Commit 6fd763d1 authored by zhenwei pi's avatar zhenwei pi Committed by Michael S. Tsirkin

virtio-crypto: change code style

Use temporary variable to make code easy to read and maintain.
	/* Pad cipher's parameters */
        vcrypto->ctrl.u.sym_create_session.op_type =
                cpu_to_le32(VIRTIO_CRYPTO_SYM_OP_CIPHER);
        vcrypto->ctrl.u.sym_create_session.u.cipher.para.algo =
                vcrypto->ctrl.header.algo;
        vcrypto->ctrl.u.sym_create_session.u.cipher.para.keylen =
                cpu_to_le32(keylen);
        vcrypto->ctrl.u.sym_create_session.u.cipher.para.op =
                cpu_to_le32(op);
-->
	sym_create_session = &ctrl->u.sym_create_session;
	sym_create_session->op_type = cpu_to_le32(VIRTIO_CRYPTO_SYM_OP_CIPHER);
	sym_create_session->u.cipher.para.algo = ctrl->header.algo;
	sym_create_session->u.cipher.para.keylen = cpu_to_le32(keylen);
	sym_create_session->u.cipher.para.op = cpu_to_le32(op);

The new style shows more obviously:
- the variable we want to operate.
- an assignment statement in a single line.

Cc: Michael S. Tsirkin <mst@redhat.com>
Cc: Jason Wang <jasowang@redhat.com>
Cc: Gonglei <arei.gonglei@huawei.com>
Reviewed-by: default avatarGonglei <arei.gonglei@huawei.com>
Signed-off-by: default avatarzhenwei pi <pizhenwei@bytedance.com>
Message-Id: <20220506131627.180784-2-pizhenwei@bytedance.com>
Signed-off-by: default avatarMichael S. Tsirkin <mst@redhat.com>
parent 7e415282
...@@ -106,23 +106,27 @@ static int virtio_crypto_alg_akcipher_init_session(struct virtio_crypto_akcipher ...@@ -106,23 +106,27 @@ static int virtio_crypto_alg_akcipher_init_session(struct virtio_crypto_akcipher
unsigned int inlen; unsigned int inlen;
int err; int err;
unsigned int num_out = 0, num_in = 0; unsigned int num_out = 0, num_in = 0;
struct virtio_crypto_op_ctrl_req *ctrl;
struct virtio_crypto_session_input *input;
pkey = kmemdup(key, keylen, GFP_ATOMIC); pkey = kmemdup(key, keylen, GFP_ATOMIC);
if (!pkey) if (!pkey)
return -ENOMEM; return -ENOMEM;
spin_lock(&vcrypto->ctrl_lock); spin_lock(&vcrypto->ctrl_lock);
memcpy(&vcrypto->ctrl.header, header, sizeof(vcrypto->ctrl.header)); ctrl = &vcrypto->ctrl;
memcpy(&vcrypto->ctrl.u, para, sizeof(vcrypto->ctrl.u)); memcpy(&ctrl->header, header, sizeof(ctrl->header));
vcrypto->input.status = cpu_to_le32(VIRTIO_CRYPTO_ERR); memcpy(&ctrl->u, para, sizeof(ctrl->u));
input = &vcrypto->input;
input->status = cpu_to_le32(VIRTIO_CRYPTO_ERR);
sg_init_one(&outhdr_sg, &vcrypto->ctrl, sizeof(vcrypto->ctrl)); sg_init_one(&outhdr_sg, ctrl, sizeof(*ctrl));
sgs[num_out++] = &outhdr_sg; sgs[num_out++] = &outhdr_sg;
sg_init_one(&key_sg, pkey, keylen); sg_init_one(&key_sg, pkey, keylen);
sgs[num_out++] = &key_sg; sgs[num_out++] = &key_sg;
sg_init_one(&inhdr_sg, &vcrypto->input, sizeof(vcrypto->input)); sg_init_one(&inhdr_sg, input, sizeof(*input));
sgs[num_out + num_in++] = &inhdr_sg; sgs[num_out + num_in++] = &inhdr_sg;
err = virtqueue_add_sgs(vcrypto->ctrl_vq, sgs, num_out, num_in, vcrypto, GFP_ATOMIC); err = virtqueue_add_sgs(vcrypto->ctrl_vq, sgs, num_out, num_in, vcrypto, GFP_ATOMIC);
...@@ -134,12 +138,12 @@ static int virtio_crypto_alg_akcipher_init_session(struct virtio_crypto_akcipher ...@@ -134,12 +138,12 @@ static int virtio_crypto_alg_akcipher_init_session(struct virtio_crypto_akcipher
!virtqueue_is_broken(vcrypto->ctrl_vq)) !virtqueue_is_broken(vcrypto->ctrl_vq))
cpu_relax(); cpu_relax();
if (le32_to_cpu(vcrypto->input.status) != VIRTIO_CRYPTO_OK) { if (le32_to_cpu(input->status) != VIRTIO_CRYPTO_OK) {
err = -EINVAL; err = -EINVAL;
goto out; goto out;
} }
ctx->session_id = le64_to_cpu(vcrypto->input.session_id); ctx->session_id = le64_to_cpu(input->session_id);
ctx->session_valid = true; ctx->session_valid = true;
err = 0; err = 0;
...@@ -149,7 +153,7 @@ static int virtio_crypto_alg_akcipher_init_session(struct virtio_crypto_akcipher ...@@ -149,7 +153,7 @@ static int virtio_crypto_alg_akcipher_init_session(struct virtio_crypto_akcipher
if (err < 0) if (err < 0)
pr_err("virtio_crypto: Create session failed status: %u\n", pr_err("virtio_crypto: Create session failed status: %u\n",
le32_to_cpu(vcrypto->input.status)); le32_to_cpu(input->status));
return err; return err;
} }
...@@ -161,23 +165,27 @@ static int virtio_crypto_alg_akcipher_close_session(struct virtio_crypto_akciphe ...@@ -161,23 +165,27 @@ static int virtio_crypto_alg_akcipher_close_session(struct virtio_crypto_akciphe
struct virtio_crypto *vcrypto = ctx->vcrypto; struct virtio_crypto *vcrypto = ctx->vcrypto;
unsigned int num_out = 0, num_in = 0, inlen; unsigned int num_out = 0, num_in = 0, inlen;
int err; int err;
struct virtio_crypto_op_ctrl_req *ctrl;
struct virtio_crypto_inhdr *ctrl_status;
spin_lock(&vcrypto->ctrl_lock); spin_lock(&vcrypto->ctrl_lock);
if (!ctx->session_valid) { if (!ctx->session_valid) {
err = 0; err = 0;
goto out; goto out;
} }
vcrypto->ctrl_status.status = VIRTIO_CRYPTO_ERR; ctrl_status = &vcrypto->ctrl_status;
vcrypto->ctrl.header.opcode = cpu_to_le32(VIRTIO_CRYPTO_AKCIPHER_DESTROY_SESSION); ctrl_status->status = VIRTIO_CRYPTO_ERR;
vcrypto->ctrl.header.queue_id = 0; ctrl = &vcrypto->ctrl;
ctrl->header.opcode = cpu_to_le32(VIRTIO_CRYPTO_AKCIPHER_DESTROY_SESSION);
ctrl->header.queue_id = 0;
destroy_session = &vcrypto->ctrl.u.destroy_session; destroy_session = &ctrl->u.destroy_session;
destroy_session->session_id = cpu_to_le64(ctx->session_id); destroy_session->session_id = cpu_to_le64(ctx->session_id);
sg_init_one(&outhdr_sg, &vcrypto->ctrl, sizeof(vcrypto->ctrl)); sg_init_one(&outhdr_sg, ctrl, sizeof(*ctrl));
sgs[num_out++] = &outhdr_sg; sgs[num_out++] = &outhdr_sg;
sg_init_one(&inhdr_sg, &vcrypto->ctrl_status.status, sizeof(vcrypto->ctrl_status.status)); sg_init_one(&inhdr_sg, &ctrl_status->status, sizeof(ctrl_status->status));
sgs[num_out + num_in++] = &inhdr_sg; sgs[num_out + num_in++] = &inhdr_sg;
err = virtqueue_add_sgs(vcrypto->ctrl_vq, sgs, num_out, num_in, vcrypto, GFP_ATOMIC); err = virtqueue_add_sgs(vcrypto->ctrl_vq, sgs, num_out, num_in, vcrypto, GFP_ATOMIC);
...@@ -189,7 +197,7 @@ static int virtio_crypto_alg_akcipher_close_session(struct virtio_crypto_akciphe ...@@ -189,7 +197,7 @@ static int virtio_crypto_alg_akcipher_close_session(struct virtio_crypto_akciphe
!virtqueue_is_broken(vcrypto->ctrl_vq)) !virtqueue_is_broken(vcrypto->ctrl_vq))
cpu_relax(); cpu_relax();
if (vcrypto->ctrl_status.status != VIRTIO_CRYPTO_OK) { if (ctrl_status->status != VIRTIO_CRYPTO_OK) {
err = -EINVAL; err = -EINVAL;
goto out; goto out;
} }
...@@ -201,7 +209,7 @@ static int virtio_crypto_alg_akcipher_close_session(struct virtio_crypto_akciphe ...@@ -201,7 +209,7 @@ static int virtio_crypto_alg_akcipher_close_session(struct virtio_crypto_akciphe
spin_unlock(&vcrypto->ctrl_lock); spin_unlock(&vcrypto->ctrl_lock);
if (err < 0) { if (err < 0) {
pr_err("virtio_crypto: Close session failed status: %u, session_id: 0x%llx\n", pr_err("virtio_crypto: Close session failed status: %u, session_id: 0x%llx\n",
vcrypto->ctrl_status.status, destroy_session->session_id); ctrl_status->status, destroy_session->session_id);
} }
return err; return err;
......
...@@ -123,6 +123,9 @@ static int virtio_crypto_alg_skcipher_init_session( ...@@ -123,6 +123,9 @@ static int virtio_crypto_alg_skcipher_init_session(
int op = encrypt ? VIRTIO_CRYPTO_OP_ENCRYPT : VIRTIO_CRYPTO_OP_DECRYPT; int op = encrypt ? VIRTIO_CRYPTO_OP_ENCRYPT : VIRTIO_CRYPTO_OP_DECRYPT;
int err; int err;
unsigned int num_out = 0, num_in = 0; unsigned int num_out = 0, num_in = 0;
struct virtio_crypto_op_ctrl_req *ctrl;
struct virtio_crypto_session_input *input;
struct virtio_crypto_sym_create_session_req *sym_create_session;
/* /*
* Avoid to do DMA from the stack, switch to using * Avoid to do DMA from the stack, switch to using
...@@ -135,24 +138,22 @@ static int virtio_crypto_alg_skcipher_init_session( ...@@ -135,24 +138,22 @@ static int virtio_crypto_alg_skcipher_init_session(
spin_lock(&vcrypto->ctrl_lock); spin_lock(&vcrypto->ctrl_lock);
/* Pad ctrl header */ /* Pad ctrl header */
vcrypto->ctrl.header.opcode = ctrl = &vcrypto->ctrl;
cpu_to_le32(VIRTIO_CRYPTO_CIPHER_CREATE_SESSION); ctrl->header.opcode = cpu_to_le32(VIRTIO_CRYPTO_CIPHER_CREATE_SESSION);
vcrypto->ctrl.header.algo = cpu_to_le32(alg); ctrl->header.algo = cpu_to_le32(alg);
/* Set the default dataqueue id to 0 */ /* Set the default dataqueue id to 0 */
vcrypto->ctrl.header.queue_id = 0; ctrl->header.queue_id = 0;
vcrypto->input.status = cpu_to_le32(VIRTIO_CRYPTO_ERR); input = &vcrypto->input;
input->status = cpu_to_le32(VIRTIO_CRYPTO_ERR);
/* Pad cipher's parameters */ /* Pad cipher's parameters */
vcrypto->ctrl.u.sym_create_session.op_type = sym_create_session = &ctrl->u.sym_create_session;
cpu_to_le32(VIRTIO_CRYPTO_SYM_OP_CIPHER); sym_create_session->op_type = cpu_to_le32(VIRTIO_CRYPTO_SYM_OP_CIPHER);
vcrypto->ctrl.u.sym_create_session.u.cipher.para.algo = sym_create_session->u.cipher.para.algo = ctrl->header.algo;
vcrypto->ctrl.header.algo; sym_create_session->u.cipher.para.keylen = cpu_to_le32(keylen);
vcrypto->ctrl.u.sym_create_session.u.cipher.para.keylen = sym_create_session->u.cipher.para.op = cpu_to_le32(op);
cpu_to_le32(keylen);
vcrypto->ctrl.u.sym_create_session.u.cipher.para.op = sg_init_one(&outhdr, ctrl, sizeof(*ctrl));
cpu_to_le32(op);
sg_init_one(&outhdr, &vcrypto->ctrl, sizeof(vcrypto->ctrl));
sgs[num_out++] = &outhdr; sgs[num_out++] = &outhdr;
/* Set key */ /* Set key */
...@@ -160,7 +161,7 @@ static int virtio_crypto_alg_skcipher_init_session( ...@@ -160,7 +161,7 @@ static int virtio_crypto_alg_skcipher_init_session(
sgs[num_out++] = &key_sg; sgs[num_out++] = &key_sg;
/* Return status and session id back */ /* Return status and session id back */
sg_init_one(&inhdr, &vcrypto->input, sizeof(vcrypto->input)); sg_init_one(&inhdr, input, sizeof(*input));
sgs[num_out + num_in++] = &inhdr; sgs[num_out + num_in++] = &inhdr;
err = virtqueue_add_sgs(vcrypto->ctrl_vq, sgs, num_out, err = virtqueue_add_sgs(vcrypto->ctrl_vq, sgs, num_out,
...@@ -180,20 +181,18 @@ static int virtio_crypto_alg_skcipher_init_session( ...@@ -180,20 +181,18 @@ static int virtio_crypto_alg_skcipher_init_session(
!virtqueue_is_broken(vcrypto->ctrl_vq)) !virtqueue_is_broken(vcrypto->ctrl_vq))
cpu_relax(); cpu_relax();
if (le32_to_cpu(vcrypto->input.status) != VIRTIO_CRYPTO_OK) { if (le32_to_cpu(input->status) != VIRTIO_CRYPTO_OK) {
spin_unlock(&vcrypto->ctrl_lock); spin_unlock(&vcrypto->ctrl_lock);
pr_err("virtio_crypto: Create session failed status: %u\n", pr_err("virtio_crypto: Create session failed status: %u\n",
le32_to_cpu(vcrypto->input.status)); le32_to_cpu(input->status));
kfree_sensitive(cipher_key); kfree_sensitive(cipher_key);
return -EINVAL; return -EINVAL;
} }
if (encrypt) if (encrypt)
ctx->enc_sess_info.session_id = ctx->enc_sess_info.session_id = le64_to_cpu(input->session_id);
le64_to_cpu(vcrypto->input.session_id);
else else
ctx->dec_sess_info.session_id = ctx->dec_sess_info.session_id = le64_to_cpu(input->session_id);
le64_to_cpu(vcrypto->input.session_id);
spin_unlock(&vcrypto->ctrl_lock); spin_unlock(&vcrypto->ctrl_lock);
...@@ -211,30 +210,30 @@ static int virtio_crypto_alg_skcipher_close_session( ...@@ -211,30 +210,30 @@ static int virtio_crypto_alg_skcipher_close_session(
struct virtio_crypto *vcrypto = ctx->vcrypto; struct virtio_crypto *vcrypto = ctx->vcrypto;
int err; int err;
unsigned int num_out = 0, num_in = 0; unsigned int num_out = 0, num_in = 0;
struct virtio_crypto_op_ctrl_req *ctrl;
struct virtio_crypto_inhdr *ctrl_status;
spin_lock(&vcrypto->ctrl_lock); spin_lock(&vcrypto->ctrl_lock);
vcrypto->ctrl_status.status = VIRTIO_CRYPTO_ERR; ctrl_status = &vcrypto->ctrl_status;
ctrl_status->status = VIRTIO_CRYPTO_ERR;
/* Pad ctrl header */ /* Pad ctrl header */
vcrypto->ctrl.header.opcode = ctrl = &vcrypto->ctrl;
cpu_to_le32(VIRTIO_CRYPTO_CIPHER_DESTROY_SESSION); ctrl->header.opcode = cpu_to_le32(VIRTIO_CRYPTO_CIPHER_DESTROY_SESSION);
/* Set the default virtqueue id to 0 */ /* Set the default virtqueue id to 0 */
vcrypto->ctrl.header.queue_id = 0; ctrl->header.queue_id = 0;
destroy_session = &vcrypto->ctrl.u.destroy_session; destroy_session = &ctrl->u.destroy_session;
if (encrypt) if (encrypt)
destroy_session->session_id = destroy_session->session_id = cpu_to_le64(ctx->enc_sess_info.session_id);
cpu_to_le64(ctx->enc_sess_info.session_id);
else else
destroy_session->session_id = destroy_session->session_id = cpu_to_le64(ctx->dec_sess_info.session_id);
cpu_to_le64(ctx->dec_sess_info.session_id);
sg_init_one(&outhdr, &vcrypto->ctrl, sizeof(vcrypto->ctrl)); sg_init_one(&outhdr, ctrl, sizeof(*ctrl));
sgs[num_out++] = &outhdr; sgs[num_out++] = &outhdr;
/* Return status and session id back */ /* Return status and session id back */
sg_init_one(&status_sg, &vcrypto->ctrl_status.status, sg_init_one(&status_sg, &ctrl_status->status, sizeof(ctrl_status->status));
sizeof(vcrypto->ctrl_status.status));
sgs[num_out + num_in++] = &status_sg; sgs[num_out + num_in++] = &status_sg;
err = virtqueue_add_sgs(vcrypto->ctrl_vq, sgs, num_out, err = virtqueue_add_sgs(vcrypto->ctrl_vq, sgs, num_out,
...@@ -249,11 +248,10 @@ static int virtio_crypto_alg_skcipher_close_session( ...@@ -249,11 +248,10 @@ static int virtio_crypto_alg_skcipher_close_session(
!virtqueue_is_broken(vcrypto->ctrl_vq)) !virtqueue_is_broken(vcrypto->ctrl_vq))
cpu_relax(); cpu_relax();
if (vcrypto->ctrl_status.status != VIRTIO_CRYPTO_OK) { if (ctrl_status->status != VIRTIO_CRYPTO_OK) {
spin_unlock(&vcrypto->ctrl_lock); spin_unlock(&vcrypto->ctrl_lock);
pr_err("virtio_crypto: Close session failed status: %u, session_id: 0x%llx\n", pr_err("virtio_crypto: Close session failed status: %u, session_id: 0x%llx\n",
vcrypto->ctrl_status.status, ctrl_status->status, destroy_session->session_id);
destroy_session->session_id);
return -EINVAL; return -EINVAL;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment