History

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.

zhenwei pi committed 4 years ago

Commit 6fd763d1 parent 7e415282 files · +59 −53Browse files

Unified · Split

2 files
  1. drivers/crypto/virtio/virtio_crypto_akcipher_algs.c +24 −16
  2. drivers/crypto/virtio/virtio_crypto_skcipher_algs.c +35 −37
drivers/crypto/virtio/virtio_crypto_akcipher_algs.c+24−16
@@ -106,23 +106,27 @@ static int virtio_crypto_alg_akcipher_init_session(struct virtio_crypto_akcipher · 105 unmodified lines
106106 unsigned int inlen;
107107 int err;
108108 unsigned int num_out = 0, num_in = 0;
109+ struct virtio_crypto_op_ctrl_req *ctrl;
110+ struct virtio_crypto_session_input *input;
109111
110112 pkey = kmemdup(key, keylen, GFP_ATOMIC);
111113 if (!pkey)
112114 return -ENOMEM;
113115
114116 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);
118122
119− sg_init_one(&outhdr_sg, &vcrypto->ctrl, sizeof(vcrypto->ctrl));
123+ sg_init_one(&outhdr_sg, ctrl, sizeof(*ctrl));
120124 sgs[num_out++] = &outhdr_sg;
121125
122126 sg_init_one(&key_sg, pkey, keylen);
123127 sgs[num_out++] = &key_sg;
124128
125− sg_init_one(&inhdr_sg, &vcrypto->input, sizeof(vcrypto->input));
129+ sg_init_one(&inhdr_sg, input, sizeof(*input));
126130 sgs[num_out + num_in++] = &inhdr_sg;
127131
128132 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
134138 !virtqueue_is_broken(vcrypto->ctrl_vq))
135139 cpu_relax();
136140
137− if (le32_to_cpu(vcrypto->input.status) != VIRTIO_CRYPTO_OK) {
141+ if (le32_to_cpu(input->status) != VIRTIO_CRYPTO_OK) {
138142 err = -EINVAL;
139143 goto out;
140144 }
141145
142− ctx->session_id = le64_to_cpu(vcrypto->input.session_id);
146+ ctx->session_id = le64_to_cpu(input->session_id);
143147 ctx->session_valid = true;
144148 err = 0;
145149
@@ -149,7 +153,7 @@ out: · 3 unmodified lines
149153
150154 if (err < 0)
151155 pr_err("virtio_crypto: Create session failed status: %u\n",
152− le32_to_cpu(vcrypto->input.status));
156+ le32_to_cpu(input->status));
153157
154158 return err;
155159 }
@@ -161,23 +165,27 @@ static int virtio_crypto_alg_akcipher_close_session(struct virtio_crypto_akciphe · 5 unmodified lines
161165 struct virtio_crypto *vcrypto = ctx->vcrypto;
162166 unsigned int num_out = 0, num_in = 0, inlen;
163167 int err;
168+ struct virtio_crypto_op_ctrl_req *ctrl;
169+ struct virtio_crypto_inhdr *ctrl_status;
164170
165171 spin_lock(&vcrypto->ctrl_lock);
166172 if (!ctx->session_valid) {
167173 err = 0;
168174 goto out;
169175 }
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;
173181
174− destroy_session = &vcrypto->ctrl.u.destroy_session;
182+ destroy_session = &ctrl->u.destroy_session;
175183 destroy_session->session_id = cpu_to_le64(ctx->session_id);
176184
177− sg_init_one(&outhdr_sg, &vcrypto->ctrl, sizeof(vcrypto->ctrl));
185+ sg_init_one(&outhdr_sg, ctrl, sizeof(*ctrl));
178186 sgs[num_out++] = &outhdr_sg;
179187
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));
181189 sgs[num_out + num_in++] = &inhdr_sg;
182190
183191 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
189197 !virtqueue_is_broken(vcrypto->ctrl_vq))
190198 cpu_relax();
191199
192− if (vcrypto->ctrl_status.status != VIRTIO_CRYPTO_OK) {
200+ if (ctrl_status->status != VIRTIO_CRYPTO_OK) {
193201 err = -EINVAL;
194202 goto out;
195203 }
@@ -201,7 +209,7 @@ out: · 5 unmodified lines
201209 spin_unlock(&vcrypto->ctrl_lock);
202210 if (err < 0) {
203211 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);
205213 }
206214
207215 return err;
drivers/crypto/virtio/virtio_crypto_skcipher_algs.c+35−37
@@ -123,6 +123,9 @@ static int virtio_crypto_alg_skcipher_init_session( · 122 unmodified lines
123123 int op = encrypt ? VIRTIO_CRYPTO_OP_ENCRYPT : VIRTIO_CRYPTO_OP_DECRYPT;
124124 int err;
125125 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;
126129
127130 /*
128131 * 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
135138
136139 spin_lock(&vcrypto->ctrl_lock);
137140 /* 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);
141144 /* Set the default dataqueue id to 0 */
142− vcrypto->ctrl.header.queue_id = 0;
145+ ctrl->header.queue_id = 0;
143146
144− vcrypto->input.status = cpu_to_le32(VIRTIO_CRYPTO_ERR);
147+ input = &vcrypto->input;
148+ input->status = cpu_to_le32(VIRTIO_CRYPTO_ERR);
145149 /* 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));
156157 sgs[num_out++] = &outhdr;
157158
158159 /* Set key */
@@ -160,7 +161,7 @@ static int virtio_crypto_alg_skcipher_init_session( · 1 unmodified line
160161 sgs[num_out++] = &key_sg;
161162
162163 /* Return status and session id back */
163− sg_init_one(&inhdr, &vcrypto->input, sizeof(vcrypto->input));
164+ sg_init_one(&inhdr, input, sizeof(*input));
164165 sgs[num_out + num_in++] = &inhdr;
165166
166167 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
180181 !virtqueue_is_broken(vcrypto->ctrl_vq))
181182 cpu_relax();
182183
183− if (le32_to_cpu(vcrypto->input.status) != VIRTIO_CRYPTO_OK) {
184+ if (le32_to_cpu(input->status) != VIRTIO_CRYPTO_OK) {
184185 spin_unlock(&vcrypto->ctrl_lock);
185186 pr_err("virtio_crypto: Create session failed status: %u\n",
186− le32_to_cpu(vcrypto->input.status));
187+ le32_to_cpu(input->status));
187188 kfree_sensitive(cipher_key);
188189 return -EINVAL;
189190 }
190191
191192 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);
194194 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);
197196
198197 spin_unlock(&vcrypto->ctrl_lock);
199198
@@ -211,30 +210,30 @@ static int virtio_crypto_alg_skcipher_close_session( · 11 unmodified lines
211210 struct virtio_crypto *vcrypto = ctx->vcrypto;
212211 int err;
213212 unsigned int num_out = 0, num_in = 0;
213+ struct virtio_crypto_op_ctrl_req *ctrl;
214+ struct virtio_crypto_inhdr *ctrl_status;
214215
215216 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;
217219 /* 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);
220222 /* Set the default virtqueue id to 0 */
221− vcrypto->ctrl.header.queue_id = 0;
223+ ctrl->header.queue_id = 0;
222224
223− destroy_session = &vcrypto->ctrl.u.destroy_session;
225+ destroy_session = &ctrl->u.destroy_session;
224226
225227 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);
228229 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);
231231
232− sg_init_one(&outhdr, &vcrypto->ctrl, sizeof(vcrypto->ctrl));
232+ sg_init_one(&outhdr, ctrl, sizeof(*ctrl));
233233 sgs[num_out++] = &outhdr;
234234
235235 /* 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));
238237 sgs[num_out + num_in++] = &status_sg;
239238
240239 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
249248 !virtqueue_is_broken(vcrypto->ctrl_vq))
250249 cpu_relax();
251250
252− if (vcrypto->ctrl_status.status != VIRTIO_CRYPTO_OK) {
251+ if (ctrl_status->status != VIRTIO_CRYPTO_OK) {
253252 spin_unlock(&vcrypto->ctrl_lock);
254253 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);
257255
258256 return -EINVAL;
259257 }

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t