Skip to content

Commit 0a74238

Browse files
committed
crypto: acomp - Count error stats differently
Move all stat code specific to acomp into the acomp code. While we're at it, change the stats so that bytes and counts are always incremented even in case of error. This allows the reference counting to be removed as we can now increment the counters prior to the operation. After the operation we simply increase the error count if necessary. This is safe as errors can only occur synchronously (or rather, the existing code already ignored asynchronous errors which are only visible to the callback function). Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 42808e5 commit 0a74238

File tree

9 files changed

+229
-156
lines changed

9 files changed

+229
-156
lines changed

crypto/acompress.c

+60-9
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,33 @@
66
* Authors: Weigang Li <weigang.li@intel.com>
77
* Giovanni Cabiddu <giovanni.cabiddu@intel.com>
88
*/
9+
10+
#include <crypto/internal/acompress.h>
11+
#include <linux/cryptouser.h>
912
#include <linux/errno.h>
1013
#include <linux/kernel.h>
1114
#include <linux/module.h>
1215
#include <linux/seq_file.h>
1316
#include <linux/slab.h>
1417
#include <linux/string.h>
15-
#include <linux/crypto.h>
16-
#include <crypto/algapi.h>
17-
#include <linux/cryptouser.h>
18-
#include <linux/compiler.h>
1918
#include <net/netlink.h>
20-
#include <crypto/internal/acompress.h>
21-
#include <crypto/internal/scompress.h>
22-
#include "internal.h"
19+
20+
#include "compress.h"
21+
22+
struct crypto_scomp;
2323

2424
static const struct crypto_type crypto_acomp_type;
2525

26+
static inline struct acomp_alg *__crypto_acomp_alg(struct crypto_alg *alg)
27+
{
28+
return container_of(alg, struct acomp_alg, calg.base);
29+
}
30+
31+
static inline struct acomp_alg *crypto_acomp_alg(struct crypto_acomp *tfm)
32+
{
33+
return __crypto_acomp_alg(crypto_acomp_tfm(tfm)->__crt_alg);
34+
}
35+
2636
#ifdef CONFIG_NET
2737
static int crypto_acomp_report(struct sk_buff *skb, struct crypto_alg *alg)
2838
{
@@ -89,13 +99,42 @@ static unsigned int crypto_acomp_extsize(struct crypto_alg *alg)
8999
return extsize;
90100
}
91101

102+
static inline int __crypto_acomp_report_stat(struct sk_buff *skb,
103+
struct crypto_alg *alg)
104+
{
105+
struct comp_alg_common *calg = __crypto_comp_alg_common(alg);
106+
struct crypto_istat_compress *istat = comp_get_stat(calg);
107+
struct crypto_stat_compress racomp;
108+
109+
memset(&racomp, 0, sizeof(racomp));
110+
111+
strscpy(racomp.type, "acomp", sizeof(racomp.type));
112+
racomp.stat_compress_cnt = atomic64_read(&istat->compress_cnt);
113+
racomp.stat_compress_tlen = atomic64_read(&istat->compress_tlen);
114+
racomp.stat_decompress_cnt = atomic64_read(&istat->decompress_cnt);
115+
racomp.stat_decompress_tlen = atomic64_read(&istat->decompress_tlen);
116+
racomp.stat_err_cnt = atomic64_read(&istat->err_cnt);
117+
118+
return nla_put(skb, CRYPTOCFGA_STAT_ACOMP, sizeof(racomp), &racomp);
119+
}
120+
121+
#ifdef CONFIG_CRYPTO_STATS
122+
int crypto_acomp_report_stat(struct sk_buff *skb, struct crypto_alg *alg)
123+
{
124+
return __crypto_acomp_report_stat(skb, alg);
125+
}
126+
#endif
127+
92128
static const struct crypto_type crypto_acomp_type = {
93129
.extsize = crypto_acomp_extsize,
94130
.init_tfm = crypto_acomp_init_tfm,
95131
#ifdef CONFIG_PROC_FS
96132
.show = crypto_acomp_show,
97133
#endif
98134
.report = crypto_acomp_report,
135+
#ifdef CONFIG_CRYPTO_STATS
136+
.report_stat = crypto_acomp_report_stat,
137+
#endif
99138
.maskclear = ~CRYPTO_ALG_TYPE_MASK,
100139
.maskset = CRYPTO_ALG_TYPE_ACOMPRESS_MASK,
101140
.type = CRYPTO_ALG_TYPE_ACOMPRESS,
@@ -147,12 +186,24 @@ void acomp_request_free(struct acomp_req *req)
147186
}
148187
EXPORT_SYMBOL_GPL(acomp_request_free);
149188

150-
int crypto_register_acomp(struct acomp_alg *alg)
189+
void comp_prepare_alg(struct comp_alg_common *alg)
151190
{
191+
struct crypto_istat_compress *istat = comp_get_stat(alg);
152192
struct crypto_alg *base = &alg->base;
153193

154-
base->cra_type = &crypto_acomp_type;
155194
base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
195+
196+
if (IS_ENABLED(CONFIG_CRYPTO_STATS))
197+
memset(istat, 0, sizeof(*istat));
198+
}
199+
200+
int crypto_register_acomp(struct acomp_alg *alg)
201+
{
202+
struct crypto_alg *base = &alg->calg.base;
203+
204+
comp_prepare_alg(&alg->calg);
205+
206+
base->cra_type = &crypto_acomp_type;
156207
base->cra_flags |= CRYPTO_ALG_TYPE_ACOMPRESS;
157208

158209
return crypto_register_alg(base);

crypto/algapi.c

-24
Original file line numberDiff line numberDiff line change
@@ -1051,30 +1051,6 @@ void crypto_stats_get(struct crypto_alg *alg)
10511051
}
10521052
EXPORT_SYMBOL_GPL(crypto_stats_get);
10531053

1054-
void crypto_stats_compress(unsigned int slen, int ret, struct crypto_alg *alg)
1055-
{
1056-
if (ret && ret != -EINPROGRESS && ret != -EBUSY) {
1057-
atomic64_inc(&alg->stats.compress.err_cnt);
1058-
} else {
1059-
atomic64_inc(&alg->stats.compress.compress_cnt);
1060-
atomic64_add(slen, &alg->stats.compress.compress_tlen);
1061-
}
1062-
crypto_alg_put(alg);
1063-
}
1064-
EXPORT_SYMBOL_GPL(crypto_stats_compress);
1065-
1066-
void crypto_stats_decompress(unsigned int slen, int ret, struct crypto_alg *alg)
1067-
{
1068-
if (ret && ret != -EINPROGRESS && ret != -EBUSY) {
1069-
atomic64_inc(&alg->stats.compress.err_cnt);
1070-
} else {
1071-
atomic64_inc(&alg->stats.compress.decompress_cnt);
1072-
atomic64_add(slen, &alg->stats.compress.decompress_tlen);
1073-
}
1074-
crypto_alg_put(alg);
1075-
}
1076-
EXPORT_SYMBOL_GPL(crypto_stats_decompress);
1077-
10781054
void crypto_stats_kpp_set_secret(struct crypto_alg *alg, int ret)
10791055
{
10801056
if (ret)

crypto/compress.h

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/* SPDX-License-Identifier: GPL-2.0-or-later */
2+
/*
3+
* Cryptographic API.
4+
*
5+
* Copyright 2015 LG Electronics Inc.
6+
* Copyright (c) 2016, Intel Corporation
7+
* Copyright (c) 2023 Herbert Xu <herbert@gondor.apana.org.au>
8+
*/
9+
#ifndef _LOCAL_CRYPTO_COMPRESS_H
10+
#define _LOCAL_CRYPTO_COMPRESS_H
11+
12+
#include "internal.h"
13+
14+
struct acomp_req;
15+
struct comp_alg_common;
16+
struct sk_buff;
17+
18+
int crypto_init_scomp_ops_async(struct crypto_tfm *tfm);
19+
struct acomp_req *crypto_acomp_scomp_alloc_ctx(struct acomp_req *req);
20+
void crypto_acomp_scomp_free_ctx(struct acomp_req *req);
21+
22+
int crypto_acomp_report_stat(struct sk_buff *skb, struct crypto_alg *alg);
23+
24+
void comp_prepare_alg(struct comp_alg_common *alg);
25+
26+
#endif /* _LOCAL_CRYPTO_COMPRESS_H */

crypto/crypto_user_stat.c

-29
Original file line numberDiff line numberDiff line change
@@ -51,31 +51,10 @@ static int crypto_report_comp(struct sk_buff *skb, struct crypto_alg *alg)
5151
memset(&rcomp, 0, sizeof(rcomp));
5252

5353
strscpy(rcomp.type, "compression", sizeof(rcomp.type));
54-
rcomp.stat_compress_cnt = atomic64_read(&alg->stats.compress.compress_cnt);
55-
rcomp.stat_compress_tlen = atomic64_read(&alg->stats.compress.compress_tlen);
56-
rcomp.stat_decompress_cnt = atomic64_read(&alg->stats.compress.decompress_cnt);
57-
rcomp.stat_decompress_tlen = atomic64_read(&alg->stats.compress.decompress_tlen);
58-
rcomp.stat_err_cnt = atomic64_read(&alg->stats.compress.err_cnt);
5954

6055
return nla_put(skb, CRYPTOCFGA_STAT_COMPRESS, sizeof(rcomp), &rcomp);
6156
}
6257

63-
static int crypto_report_acomp(struct sk_buff *skb, struct crypto_alg *alg)
64-
{
65-
struct crypto_stat_compress racomp;
66-
67-
memset(&racomp, 0, sizeof(racomp));
68-
69-
strscpy(racomp.type, "acomp", sizeof(racomp.type));
70-
racomp.stat_compress_cnt = atomic64_read(&alg->stats.compress.compress_cnt);
71-
racomp.stat_compress_tlen = atomic64_read(&alg->stats.compress.compress_tlen);
72-
racomp.stat_decompress_cnt = atomic64_read(&alg->stats.compress.decompress_cnt);
73-
racomp.stat_decompress_tlen = atomic64_read(&alg->stats.compress.decompress_tlen);
74-
racomp.stat_err_cnt = atomic64_read(&alg->stats.compress.err_cnt);
75-
76-
return nla_put(skb, CRYPTOCFGA_STAT_ACOMP, sizeof(racomp), &racomp);
77-
}
78-
7958
static int crypto_report_kpp(struct sk_buff *skb, struct crypto_alg *alg)
8059
{
8160
struct crypto_stat_kpp rkpp;
@@ -156,14 +135,6 @@ static int crypto_reportstat_one(struct crypto_alg *alg,
156135
if (crypto_report_comp(skb, alg))
157136
goto nla_put_failure;
158137
break;
159-
case CRYPTO_ALG_TYPE_ACOMPRESS:
160-
if (crypto_report_acomp(skb, alg))
161-
goto nla_put_failure;
162-
break;
163-
case CRYPTO_ALG_TYPE_SCOMPRESS:
164-
if (crypto_report_acomp(skb, alg))
165-
goto nla_put_failure;
166-
break;
167138
case CRYPTO_ALG_TYPE_KPP:
168139
if (crypto_report_kpp(skb, alg))
169140
goto nla_put_failure;

crypto/scompress.c

+15-12
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,22 @@
66
* Copyright (c) 2016, Intel Corporation
77
* Author: Giovanni Cabiddu <giovanni.cabiddu@intel.com>
88
*/
9-
#include <linux/errno.h>
9+
10+
#include <crypto/internal/acompress.h>
11+
#include <crypto/internal/scompress.h>
12+
#include <crypto/scatterwalk.h>
13+
#include <linux/cryptouser.h>
14+
#include <linux/err.h>
1015
#include <linux/kernel.h>
1116
#include <linux/module.h>
17+
#include <linux/scatterlist.h>
1218
#include <linux/seq_file.h>
1319
#include <linux/slab.h>
1420
#include <linux/string.h>
15-
#include <linux/crypto.h>
16-
#include <linux/compiler.h>
1721
#include <linux/vmalloc.h>
18-
#include <crypto/algapi.h>
19-
#include <linux/cryptouser.h>
2022
#include <net/netlink.h>
21-
#include <linux/scatterlist.h>
22-
#include <crypto/scatterwalk.h>
23-
#include <crypto/internal/acompress.h>
24-
#include <crypto/internal/scompress.h>
25-
#include "internal.h"
23+
24+
#include "compress.h"
2625

2726
struct scomp_scratch {
2827
spinlock_t lock;
@@ -248,6 +247,9 @@ static const struct crypto_type crypto_scomp_type = {
248247
.show = crypto_scomp_show,
249248
#endif
250249
.report = crypto_scomp_report,
250+
#ifdef CONFIG_CRYPTO_STATS
251+
.report_stat = crypto_acomp_report_stat,
252+
#endif
251253
.maskclear = ~CRYPTO_ALG_TYPE_MASK,
252254
.maskset = CRYPTO_ALG_TYPE_MASK,
253255
.type = CRYPTO_ALG_TYPE_SCOMPRESS,
@@ -256,10 +258,11 @@ static const struct crypto_type crypto_scomp_type = {
256258

257259
int crypto_register_scomp(struct scomp_alg *alg)
258260
{
259-
struct crypto_alg *base = &alg->base;
261+
struct crypto_alg *base = &alg->calg.base;
262+
263+
comp_prepare_alg(&alg->calg);
260264

261265
base->cra_type = &crypto_scomp_type;
262-
base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
263266
base->cra_flags |= CRYPTO_ALG_TYPE_SCOMPRESS;
264267

265268
return crypto_register_alg(base);

0 commit comments

Comments
 (0)