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