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.
Unified · Split
| @@ -106,23 +106,27 @@ static int virtio_crypto_alg_akcipher_init_session(struct virtio_crypto_akcipher · 105 unmodified lines | |||
| 106 | unsigned int inlen; | 106 | unsigned int inlen; |
| 107 | int err; | 107 | int err; |
| 108 | unsigned int num_out = 0, num_in = 0; | 108 | unsigned int num_out = 0, num_in = 0; |
| 109 | struct virtio_crypto_op_ctrl_req *ctrl; | ||
| 110 | struct virtio_crypto_session_input *input; | ||
| 109 | 111 | ||
| 110 | pkey = kmemdup(key, keylen, GFP_ATOMIC); | 112 | pkey = kmemdup(key, keylen, GFP_ATOMIC); |
| 111 | if (!pkey) | 113 | if (!pkey) |
| 112 | return -ENOMEM; | 114 | return -ENOMEM; |
| 113 | 115 | ||
| 114 | spin_lock(&vcrypto->ctrl_lock); | 116 | spin_lock(&vcrypto->ctrl_lock); |
| 115 | memcpy(&vcrypto->ctrl.header, header, sizeof(vcrypto->ctrl.header)); | 117 | ctrl = &vcrypto->ctrl; |
| 116 | memcpy(&vcrypto->ctrl.u, para, sizeof(vcrypto->ctrl.u)); | 118 | memcpy(&ctrl->header, header, sizeof(ctrl->header)); |
| 117 | vcrypto->input.status = cpu_to_le32(VIRTIO_CRYPTO_ERR); | 119 | memcpy(&ctrl->u, para, sizeof(ctrl->u)); |
| 120 | input = &vcrypto->input; | ||
| 121 | input->status = cpu_to_le32(VIRTIO_CRYPTO_ERR); | ||
| 118 | 122 | ||
| 119 | sg_init_one(&outhdr_sg, &vcrypto->ctrl, sizeof(vcrypto->ctrl)); | 123 | sg_init_one(&outhdr_sg, ctrl, sizeof(*ctrl)); |
| 120 | sgs[num_out++] = &outhdr_sg; | 124 | sgs[num_out++] = &outhdr_sg; |
| 121 | 125 | ||
| 122 | sg_init_one(&key_sg, pkey, keylen); | 126 | sg_init_one(&key_sg, pkey, keylen); |
| 123 | sgs[num_out++] = &key_sg; | 127 | sgs[num_out++] = &key_sg; |
| 124 | 128 | ||
| 125 | sg_init_one(&inhdr_sg, &vcrypto->input, sizeof(vcrypto->input)); | 129 | sg_init_one(&inhdr_sg, input, sizeof(*input)); |
| 126 | sgs[num_out + num_in++] = &inhdr_sg; | 130 | sgs[num_out + num_in++] = &inhdr_sg; |
| 127 | 131 | ||
| 128 | err = virtqueue_add_sgs(vcrypto->ctrl_vq, sgs, num_out, num_in, vcrypto, GFP_ATOMIC); | 132 | 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 · 5 unmodified lines | |||
| 134 | !virtqueue_is_broken(vcrypto->ctrl_vq)) | 138 | !virtqueue_is_broken(vcrypto->ctrl_vq)) |
| 135 | cpu_relax(); | 139 | cpu_relax(); |
| 136 | 140 | ||
| 137 | if (le32_to_cpu(vcrypto->input.status) != VIRTIO_CRYPTO_OK) { | 141 | if (le32_to_cpu(input->status) != VIRTIO_CRYPTO_OK) { |
| 138 | err = -EINVAL; | 142 | err = -EINVAL; |
| 139 | goto out; | 143 | goto out; |
| 140 | } | 144 | } |
| 141 | 145 | ||
| 142 | ctx->session_id = le64_to_cpu(vcrypto->input.session_id); | 146 | ctx->session_id = le64_to_cpu(input->session_id); |
| 143 | ctx->session_valid = true; | 147 | ctx->session_valid = true; |
| 144 | err = 0; | 148 | err = 0; |
| 145 | 149 | ||
| @@ -149,7 +153,7 @@ out: · 3 unmodified lines | |||
| 149 | 153 | ||
| 150 | if (err < 0) | 154 | if (err < 0) |
| 151 | pr_err("virtio_crypto: Create session failed status: %u\n", | 155 | pr_err("virtio_crypto: Create session failed status: %u\n", |
| 152 | le32_to_cpu(vcrypto->input.status)); | 156 | le32_to_cpu(input->status)); |
| 153 | 157 | ||
| 154 | return err; | 158 | return err; |
| 155 | } | 159 | } |
| @@ -161,23 +165,27 @@ static int virtio_crypto_alg_akcipher_close_session(struct virtio_crypto_akciphe · 5 unmodified lines | |||
| 161 | struct virtio_crypto *vcrypto = ctx->vcrypto; | 165 | struct virtio_crypto *vcrypto = ctx->vcrypto; |
| 162 | unsigned int num_out = 0, num_in = 0, inlen; | 166 | unsigned int num_out = 0, num_in = 0, inlen; |
| 163 | int err; | 167 | int err; |
| 168 | struct virtio_crypto_op_ctrl_req *ctrl; | ||
| 169 | struct virtio_crypto_inhdr *ctrl_status; | ||
| 164 | 170 | ||
| 165 | spin_lock(&vcrypto->ctrl_lock); | 171 | spin_lock(&vcrypto->ctrl_lock); |
| 166 | if (!ctx->session_valid) { | 172 | if (!ctx->session_valid) { |
| 167 | err = 0; | 173 | err = 0; |
| 168 | goto out; | 174 | goto out; |
| 169 | } | 175 | } |
| 170 | vcrypto->ctrl_status.status = VIRTIO_CRYPTO_ERR; | 176 | ctrl_status = &vcrypto->ctrl_status; |
| 171 | vcrypto->ctrl.header.opcode = cpu_to_le32(VIRTIO_CRYPTO_AKCIPHER_DESTROY_SESSION); | 177 | ctrl_status->status = VIRTIO_CRYPTO_ERR; |
| 172 | vcrypto->ctrl.header.queue_id = 0; | 178 | ctrl = &vcrypto->ctrl; |
| 179 | ctrl->header.opcode = cpu_to_le32(VIRTIO_CRYPTO_AKCIPHER_DESTROY_SESSION); | ||
| 180 | ctrl->header.queue_id = 0; | ||
| 173 | 181 | ||
| 174 | destroy_session = &vcrypto->ctrl.u.destroy_session; | 182 | destroy_session = &ctrl->u.destroy_session; |
| 175 | destroy_session->session_id = cpu_to_le64(ctx->session_id); | 183 | destroy_session->session_id = cpu_to_le64(ctx->session_id); |
| 176 | 184 | ||
| 177 | sg_init_one(&outhdr_sg, &vcrypto->ctrl, sizeof(vcrypto->ctrl)); | 185 | sg_init_one(&outhdr_sg, ctrl, sizeof(*ctrl)); |
| 178 | sgs[num_out++] = &outhdr_sg; | 186 | sgs[num_out++] = &outhdr_sg; |
| 179 | 187 | ||
| 180 | sg_init_one(&inhdr_sg, &vcrypto->ctrl_status.status, sizeof(vcrypto->ctrl_status.status)); | 188 | sg_init_one(&inhdr_sg, &ctrl_status->status, sizeof(ctrl_status->status)); |
| 181 | sgs[num_out + num_in++] = &inhdr_sg; | 189 | sgs[num_out + num_in++] = &inhdr_sg; |
| 182 | 190 | ||
| 183 | err = virtqueue_add_sgs(vcrypto->ctrl_vq, sgs, num_out, num_in, vcrypto, GFP_ATOMIC); | 191 | 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 · 5 unmodified lines | |||
| 189 | !virtqueue_is_broken(vcrypto->ctrl_vq)) | 197 | !virtqueue_is_broken(vcrypto->ctrl_vq)) |
| 190 | cpu_relax(); | 198 | cpu_relax(); |
| 191 | 199 | ||
| 192 | if (vcrypto->ctrl_status.status != VIRTIO_CRYPTO_OK) { | 200 | if (ctrl_status->status != VIRTIO_CRYPTO_OK) { |
| 193 | err = -EINVAL; | 201 | err = -EINVAL; |
| 194 | goto out; | 202 | goto out; |
| 195 | } | 203 | } |
| @@ -201,7 +209,7 @@ out: · 5 unmodified lines | |||
| 201 | spin_unlock(&vcrypto->ctrl_lock); | 209 | spin_unlock(&vcrypto->ctrl_lock); |
| 202 | if (err < 0) { | 210 | if (err < 0) { |
| 203 | pr_err("virtio_crypto: Close session failed status: %u, session_id: 0x%llx\n", | 211 | pr_err("virtio_crypto: Close session failed status: %u, session_id: 0x%llx\n", |
| 204 | vcrypto->ctrl_status.status, destroy_session->session_id); | 212 | ctrl_status->status, destroy_session->session_id); |
| 205 | } | 213 | } |
| 206 | 214 | ||
| 207 | return err; | 215 | return err; |
| @@ -123,6 +123,9 @@ static int virtio_crypto_alg_skcipher_init_session( · 122 unmodified lines | |||
| 123 | int op = encrypt ? VIRTIO_CRYPTO_OP_ENCRYPT : VIRTIO_CRYPTO_OP_DECRYPT; | 123 | int op = encrypt ? VIRTIO_CRYPTO_OP_ENCRYPT : VIRTIO_CRYPTO_OP_DECRYPT; |
| 124 | int err; | 124 | int err; |
| 125 | unsigned int num_out = 0, num_in = 0; | 125 | unsigned int num_out = 0, num_in = 0; |
| 126 | struct virtio_crypto_op_ctrl_req *ctrl; | ||
| 127 | struct virtio_crypto_session_input *input; | ||
| 128 | struct virtio_crypto_sym_create_session_req *sym_create_session; | ||
| 126 | 129 | ||
| 127 | /* | 130 | /* |
| 128 | * Avoid to do DMA from the stack, switch to using | 131 | * Avoid to do DMA from the stack, switch to using |
| @@ -135,24 +138,22 @@ static int virtio_crypto_alg_skcipher_init_session( · 6 unmodified lines | |||
| 135 | 138 | ||
| 136 | spin_lock(&vcrypto->ctrl_lock); | 139 | spin_lock(&vcrypto->ctrl_lock); |
| 137 | /* Pad ctrl header */ | 140 | /* Pad ctrl header */ |
| 138 | vcrypto->ctrl.header.opcode = | 141 | ctrl = &vcrypto->ctrl; |
| 139 | cpu_to_le32(VIRTIO_CRYPTO_CIPHER_CREATE_SESSION); | 142 | ctrl->header.opcode = cpu_to_le32(VIRTIO_CRYPTO_CIPHER_CREATE_SESSION); |
| 140 | vcrypto->ctrl.header.algo = cpu_to_le32(alg); | 143 | ctrl->header.algo = cpu_to_le32(alg); |
| 141 | /* Set the default dataqueue id to 0 */ | 144 | /* Set the default dataqueue id to 0 */ |
| 142 | vcrypto->ctrl.header.queue_id = 0; | 145 | ctrl->header.queue_id = 0; |
| 143 | 146 | ||
| 144 | vcrypto->input.status = cpu_to_le32(VIRTIO_CRYPTO_ERR); | 147 | input = &vcrypto->input; |
| 148 | input->status = cpu_to_le32(VIRTIO_CRYPTO_ERR); | ||
| 145 | /* Pad cipher's parameters */ | 149 | /* Pad cipher's parameters */ |
| 146 | vcrypto->ctrl.u.sym_create_session.op_type = | 150 | sym_create_session = &ctrl->u.sym_create_session; |
| 147 | cpu_to_le32(VIRTIO_CRYPTO_SYM_OP_CIPHER); | 151 | sym_create_session->op_type = cpu_to_le32(VIRTIO_CRYPTO_SYM_OP_CIPHER); |
| 148 | vcrypto->ctrl.u.sym_create_session.u.cipher.para.algo = | 152 | sym_create_session->u.cipher.para.algo = ctrl->header.algo; |
| 149 | vcrypto->ctrl.header.algo; | 153 | sym_create_session->u.cipher.para.keylen = cpu_to_le32(keylen); |
| 150 | vcrypto->ctrl.u.sym_create_session.u.cipher.para.keylen = | 154 | sym_create_session->u.cipher.para.op = cpu_to_le32(op); |
| 151 | cpu_to_le32(keylen); | 155 | |
| 152 | vcrypto->ctrl.u.sym_create_session.u.cipher.para.op = | 156 | sg_init_one(&outhdr, ctrl, sizeof(*ctrl)); |
| 153 | cpu_to_le32(op); | ||
| 154 | |||
| 155 | sg_init_one(&outhdr, &vcrypto->ctrl, sizeof(vcrypto->ctrl)); | ||
| 156 | sgs[num_out++] = &outhdr; | 157 | sgs[num_out++] = &outhdr; |
| 157 | 158 | ||
| 158 | /* Set key */ | 159 | /* Set key */ |
| @@ -160,7 +161,7 @@ static int virtio_crypto_alg_skcipher_init_session( · 1 unmodified line | |||
| 160 | sgs[num_out++] = &key_sg; | 161 | sgs[num_out++] = &key_sg; |
| 161 | 162 | ||
| 162 | /* Return status and session id back */ | 163 | /* Return status and session id back */ |
| 163 | sg_init_one(&inhdr, &vcrypto->input, sizeof(vcrypto->input)); | 164 | sg_init_one(&inhdr, input, sizeof(*input)); |
| 164 | sgs[num_out + num_in++] = &inhdr; | 165 | sgs[num_out + num_in++] = &inhdr; |
| 165 | 166 | ||
| 166 | err = virtqueue_add_sgs(vcrypto->ctrl_vq, sgs, num_out, | 167 | err = virtqueue_add_sgs(vcrypto->ctrl_vq, sgs, num_out, |
| @@ -180,20 +181,18 @@ static int virtio_crypto_alg_skcipher_init_session( · 13 unmodified lines | |||
| 180 | !virtqueue_is_broken(vcrypto->ctrl_vq)) | 181 | !virtqueue_is_broken(vcrypto->ctrl_vq)) |
| 181 | cpu_relax(); | 182 | cpu_relax(); |
| 182 | 183 | ||
| 183 | if (le32_to_cpu(vcrypto->input.status) != VIRTIO_CRYPTO_OK) { | 184 | if (le32_to_cpu(input->status) != VIRTIO_CRYPTO_OK) { |
| 184 | spin_unlock(&vcrypto->ctrl_lock); | 185 | spin_unlock(&vcrypto->ctrl_lock); |
| 185 | pr_err("virtio_crypto: Create session failed status: %u\n", | 186 | pr_err("virtio_crypto: Create session failed status: %u\n", |
| 186 | le32_to_cpu(vcrypto->input.status)); | 187 | le32_to_cpu(input->status)); |
| 187 | kfree_sensitive(cipher_key); | 188 | kfree_sensitive(cipher_key); |
| 188 | return -EINVAL; | 189 | return -EINVAL; |
| 189 | } | 190 | } |
| 190 | 191 | ||
| 191 | if (encrypt) | 192 | if (encrypt) |
| 192 | ctx->enc_sess_info.session_id = | 193 | ctx->enc_sess_info.session_id = le64_to_cpu(input->session_id); |
| 193 | le64_to_cpu(vcrypto->input.session_id); | ||
| 194 | else | 194 | else |
| 195 | ctx->dec_sess_info.session_id = | 195 | ctx->dec_sess_info.session_id = le64_to_cpu(input->session_id); |
| 196 | le64_to_cpu(vcrypto->input.session_id); | ||
| 197 | 196 | ||
| 198 | spin_unlock(&vcrypto->ctrl_lock); | 197 | spin_unlock(&vcrypto->ctrl_lock); |
| 199 | 198 | ||
| @@ -211,30 +210,30 @@ static int virtio_crypto_alg_skcipher_close_session( · 11 unmodified lines | |||
| 211 | struct virtio_crypto *vcrypto = ctx->vcrypto; | 210 | struct virtio_crypto *vcrypto = ctx->vcrypto; |
| 212 | int err; | 211 | int err; |
| 213 | unsigned int num_out = 0, num_in = 0; | 212 | unsigned int num_out = 0, num_in = 0; |
| 213 | struct virtio_crypto_op_ctrl_req *ctrl; | ||
| 214 | struct virtio_crypto_inhdr *ctrl_status; | ||
| 214 | 215 | ||
| 215 | spin_lock(&vcrypto->ctrl_lock); | 216 | spin_lock(&vcrypto->ctrl_lock); |
| 216 | vcrypto->ctrl_status.status = VIRTIO_CRYPTO_ERR; | 217 | ctrl_status = &vcrypto->ctrl_status; |
| 218 | ctrl_status->status = VIRTIO_CRYPTO_ERR; | ||
| 217 | /* Pad ctrl header */ | 219 | /* Pad ctrl header */ |
| 218 | vcrypto->ctrl.header.opcode = | 220 | ctrl = &vcrypto->ctrl; |
| 219 | cpu_to_le32(VIRTIO_CRYPTO_CIPHER_DESTROY_SESSION); | 221 | ctrl->header.opcode = cpu_to_le32(VIRTIO_CRYPTO_CIPHER_DESTROY_SESSION); |
| 220 | /* Set the default virtqueue id to 0 */ | 222 | /* Set the default virtqueue id to 0 */ |
| 221 | vcrypto->ctrl.header.queue_id = 0; | 223 | ctrl->header.queue_id = 0; |
| 222 | 224 | ||
| 223 | destroy_session = &vcrypto->ctrl.u.destroy_session; | 225 | destroy_session = &ctrl->u.destroy_session; |
| 224 | 226 | ||
| 225 | if (encrypt) | 227 | if (encrypt) |
| 226 | destroy_session->session_id = | 228 | destroy_session->session_id = cpu_to_le64(ctx->enc_sess_info.session_id); |
| 227 | cpu_to_le64(ctx->enc_sess_info.session_id); | ||
| 228 | else | 229 | else |
| 229 | destroy_session->session_id = | 230 | destroy_session->session_id = cpu_to_le64(ctx->dec_sess_info.session_id); |
| 230 | cpu_to_le64(ctx->dec_sess_info.session_id); | ||
| 231 | 231 | ||
| 232 | sg_init_one(&outhdr, &vcrypto->ctrl, sizeof(vcrypto->ctrl)); | 232 | sg_init_one(&outhdr, ctrl, sizeof(*ctrl)); |
| 233 | sgs[num_out++] = &outhdr; | 233 | sgs[num_out++] = &outhdr; |
| 234 | 234 | ||
| 235 | /* Return status and session id back */ | 235 | /* Return status and session id back */ |
| 236 | sg_init_one(&status_sg, &vcrypto->ctrl_status.status, | 236 | sg_init_one(&status_sg, &ctrl_status->status, sizeof(ctrl_status->status)); |
| 237 | sizeof(vcrypto->ctrl_status.status)); | ||
| 238 | sgs[num_out + num_in++] = &status_sg; | 237 | sgs[num_out + num_in++] = &status_sg; |
| 239 | 238 | ||
| 240 | err = virtqueue_add_sgs(vcrypto->ctrl_vq, sgs, num_out, | 239 | err = virtqueue_add_sgs(vcrypto->ctrl_vq, sgs, num_out, |
| @@ -249,11 +248,10 @@ static int virtio_crypto_alg_skcipher_close_session( · 8 unmodified lines | |||
| 249 | !virtqueue_is_broken(vcrypto->ctrl_vq)) | 248 | !virtqueue_is_broken(vcrypto->ctrl_vq)) |
| 250 | cpu_relax(); | 249 | cpu_relax(); |
| 251 | 250 | ||
| 252 | if (vcrypto->ctrl_status.status != VIRTIO_CRYPTO_OK) { | 251 | if (ctrl_status->status != VIRTIO_CRYPTO_OK) { |
| 253 | spin_unlock(&vcrypto->ctrl_lock); | 252 | spin_unlock(&vcrypto->ctrl_lock); |
| 254 | pr_err("virtio_crypto: Close session failed status: %u, session_id: 0x%llx\n", | 253 | pr_err("virtio_crypto: Close session failed status: %u, session_id: 0x%llx\n", |
| 255 | vcrypto->ctrl_status.status, | 254 | ctrl_status->status, destroy_session->session_id); |
| 256 | destroy_session->session_id); | ||
| 257 | 255 | ||
| 258 | return -EINVAL; | 256 | return -EINVAL; |
| 259 | } | 257 | } |