Skip to content

Commit 3d0e637

Browse files
keesaxboe
authored andcommitted
drbd: Convert from ahash to shash
In preparing to remove all stack VLA usage from the kernel[1], this removes the discouraged use of AHASH_REQUEST_ON_STACK in favor of the smaller SHASH_DESC_ON_STACK by converting from ahash-wrapped-shash to direct shash. By removing a layer of indirection this both improves performance and reduces stack usage. The stack allocation will be made a fixed size in a later patch to the crypto subsystem. The bulk of the lines in this change are simple s/ahash/shash/, but the main logic differences are in drbd_csum_ee() and drbd_csum_bio(), which externalizes the page walking with k(un)map_atomic() instead of using scattergather. [1] https://lkml.kernel.org/r/CA+55aFzCG-zNmZwX4A2FQpadafLfEzK6CC=qPXydAacU1RqZWA@mail.gmail.com Acked-by: Lars Ellenberg <[email protected]> Signed-off-by: Kees Cook <[email protected]> Signed-off-by: Jens Axboe <[email protected]>
1 parent ca16eb3 commit 3d0e637

File tree

5 files changed

+76
-88
lines changed

5 files changed

+76
-88
lines changed

drivers/block/drbd/drbd_int.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -724,10 +724,10 @@ struct drbd_connection {
724724
struct list_head transfer_log; /* all requests not yet fully processed */
725725

726726
struct crypto_shash *cram_hmac_tfm;
727-
struct crypto_ahash *integrity_tfm; /* checksums we compute, updates protected by connection->data->mutex */
728-
struct crypto_ahash *peer_integrity_tfm; /* checksums we verify, only accessed from receiver thread */
729-
struct crypto_ahash *csums_tfm;
730-
struct crypto_ahash *verify_tfm;
727+
struct crypto_shash *integrity_tfm; /* checksums we compute, updates protected by connection->data->mutex */
728+
struct crypto_shash *peer_integrity_tfm; /* checksums we verify, only accessed from receiver thread */
729+
struct crypto_shash *csums_tfm;
730+
struct crypto_shash *verify_tfm;
731731
void *int_dig_in;
732732
void *int_dig_vv;
733733

@@ -1531,8 +1531,9 @@ static inline void ov_out_of_sync_print(struct drbd_device *device)
15311531
}
15321532

15331533

1534-
extern void drbd_csum_bio(struct crypto_ahash *, struct bio *, void *);
1535-
extern void drbd_csum_ee(struct crypto_ahash *, struct drbd_peer_request *, void *);
1534+
extern void drbd_csum_bio(struct crypto_shash *, struct bio *, void *);
1535+
extern void drbd_csum_ee(struct crypto_shash *, struct drbd_peer_request *,
1536+
void *);
15361537
/* worker callbacks */
15371538
extern int w_e_end_data_req(struct drbd_work *, int);
15381539
extern int w_e_end_rsdata_req(struct drbd_work *, int);

drivers/block/drbd/drbd_main.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1377,7 +1377,7 @@ void drbd_send_ack_dp(struct drbd_peer_device *peer_device, enum drbd_packet cmd
13771377
struct p_data *dp, int data_size)
13781378
{
13791379
if (peer_device->connection->peer_integrity_tfm)
1380-
data_size -= crypto_ahash_digestsize(peer_device->connection->peer_integrity_tfm);
1380+
data_size -= crypto_shash_digestsize(peer_device->connection->peer_integrity_tfm);
13811381
_drbd_send_ack(peer_device, cmd, dp->sector, cpu_to_be32(data_size),
13821382
dp->block_id);
13831383
}
@@ -1690,7 +1690,7 @@ int drbd_send_dblock(struct drbd_peer_device *peer_device, struct drbd_request *
16901690
sock = &peer_device->connection->data;
16911691
p = drbd_prepare_command(peer_device, sock);
16921692
digest_size = peer_device->connection->integrity_tfm ?
1693-
crypto_ahash_digestsize(peer_device->connection->integrity_tfm) : 0;
1693+
crypto_shash_digestsize(peer_device->connection->integrity_tfm) : 0;
16941694

16951695
if (!p)
16961696
return -EIO;
@@ -1796,7 +1796,7 @@ int drbd_send_block(struct drbd_peer_device *peer_device, enum drbd_packet cmd,
17961796
p = drbd_prepare_command(peer_device, sock);
17971797

17981798
digest_size = peer_device->connection->integrity_tfm ?
1799-
crypto_ahash_digestsize(peer_device->connection->integrity_tfm) : 0;
1799+
crypto_shash_digestsize(peer_device->connection->integrity_tfm) : 0;
18001800

18011801
if (!p)
18021802
return -EIO;
@@ -2557,11 +2557,11 @@ void conn_free_crypto(struct drbd_connection *connection)
25572557
{
25582558
drbd_free_sock(connection);
25592559

2560-
crypto_free_ahash(connection->csums_tfm);
2561-
crypto_free_ahash(connection->verify_tfm);
2560+
crypto_free_shash(connection->csums_tfm);
2561+
crypto_free_shash(connection->verify_tfm);
25622562
crypto_free_shash(connection->cram_hmac_tfm);
2563-
crypto_free_ahash(connection->integrity_tfm);
2564-
crypto_free_ahash(connection->peer_integrity_tfm);
2563+
crypto_free_shash(connection->integrity_tfm);
2564+
crypto_free_shash(connection->peer_integrity_tfm);
25652565
kfree(connection->int_dig_in);
25662566
kfree(connection->int_dig_vv);
25672567

drivers/block/drbd/drbd_nl.c

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2303,10 +2303,10 @@ check_net_options(struct drbd_connection *connection, struct net_conf *new_net_c
23032303
}
23042304

23052305
struct crypto {
2306-
struct crypto_ahash *verify_tfm;
2307-
struct crypto_ahash *csums_tfm;
2306+
struct crypto_shash *verify_tfm;
2307+
struct crypto_shash *csums_tfm;
23082308
struct crypto_shash *cram_hmac_tfm;
2309-
struct crypto_ahash *integrity_tfm;
2309+
struct crypto_shash *integrity_tfm;
23102310
};
23112311

23122312
static int
@@ -2324,36 +2324,21 @@ alloc_shash(struct crypto_shash **tfm, char *tfm_name, int err_alg)
23242324
return NO_ERROR;
23252325
}
23262326

2327-
static int
2328-
alloc_ahash(struct crypto_ahash **tfm, char *tfm_name, int err_alg)
2329-
{
2330-
if (!tfm_name[0])
2331-
return NO_ERROR;
2332-
2333-
*tfm = crypto_alloc_ahash(tfm_name, 0, CRYPTO_ALG_ASYNC);
2334-
if (IS_ERR(*tfm)) {
2335-
*tfm = NULL;
2336-
return err_alg;
2337-
}
2338-
2339-
return NO_ERROR;
2340-
}
2341-
23422327
static enum drbd_ret_code
23432328
alloc_crypto(struct crypto *crypto, struct net_conf *new_net_conf)
23442329
{
23452330
char hmac_name[CRYPTO_MAX_ALG_NAME];
23462331
enum drbd_ret_code rv;
23472332

2348-
rv = alloc_ahash(&crypto->csums_tfm, new_net_conf->csums_alg,
2333+
rv = alloc_shash(&crypto->csums_tfm, new_net_conf->csums_alg,
23492334
ERR_CSUMS_ALG);
23502335
if (rv != NO_ERROR)
23512336
return rv;
2352-
rv = alloc_ahash(&crypto->verify_tfm, new_net_conf->verify_alg,
2337+
rv = alloc_shash(&crypto->verify_tfm, new_net_conf->verify_alg,
23532338
ERR_VERIFY_ALG);
23542339
if (rv != NO_ERROR)
23552340
return rv;
2356-
rv = alloc_ahash(&crypto->integrity_tfm, new_net_conf->integrity_alg,
2341+
rv = alloc_shash(&crypto->integrity_tfm, new_net_conf->integrity_alg,
23572342
ERR_INTEGRITY_ALG);
23582343
if (rv != NO_ERROR)
23592344
return rv;
@@ -2371,9 +2356,9 @@ alloc_crypto(struct crypto *crypto, struct net_conf *new_net_conf)
23712356
static void free_crypto(struct crypto *crypto)
23722357
{
23732358
crypto_free_shash(crypto->cram_hmac_tfm);
2374-
crypto_free_ahash(crypto->integrity_tfm);
2375-
crypto_free_ahash(crypto->csums_tfm);
2376-
crypto_free_ahash(crypto->verify_tfm);
2359+
crypto_free_shash(crypto->integrity_tfm);
2360+
crypto_free_shash(crypto->csums_tfm);
2361+
crypto_free_shash(crypto->verify_tfm);
23772362
}
23782363

23792364
int drbd_adm_net_opts(struct sk_buff *skb, struct genl_info *info)
@@ -2450,17 +2435,17 @@ int drbd_adm_net_opts(struct sk_buff *skb, struct genl_info *info)
24502435
rcu_assign_pointer(connection->net_conf, new_net_conf);
24512436

24522437
if (!rsr) {
2453-
crypto_free_ahash(connection->csums_tfm);
2438+
crypto_free_shash(connection->csums_tfm);
24542439
connection->csums_tfm = crypto.csums_tfm;
24552440
crypto.csums_tfm = NULL;
24562441
}
24572442
if (!ovr) {
2458-
crypto_free_ahash(connection->verify_tfm);
2443+
crypto_free_shash(connection->verify_tfm);
24592444
connection->verify_tfm = crypto.verify_tfm;
24602445
crypto.verify_tfm = NULL;
24612446
}
24622447

2463-
crypto_free_ahash(connection->integrity_tfm);
2448+
crypto_free_shash(connection->integrity_tfm);
24642449
connection->integrity_tfm = crypto.integrity_tfm;
24652450
if (connection->cstate >= C_WF_REPORT_PARAMS && connection->agreed_pro_version >= 100)
24662451
/* Do this without trying to take connection->data.mutex again. */

drivers/block/drbd/drbd_receiver.c

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1732,7 +1732,7 @@ static int receive_Barrier(struct drbd_connection *connection, struct packet_inf
17321732
}
17331733

17341734
/* quick wrapper in case payload size != request_size (write same) */
1735-
static void drbd_csum_ee_size(struct crypto_ahash *h,
1735+
static void drbd_csum_ee_size(struct crypto_shash *h,
17361736
struct drbd_peer_request *r, void *d,
17371737
unsigned int payload_size)
17381738
{
@@ -1769,7 +1769,7 @@ read_in_block(struct drbd_peer_device *peer_device, u64 id, sector_t sector,
17691769

17701770
digest_size = 0;
17711771
if (!trim && peer_device->connection->peer_integrity_tfm) {
1772-
digest_size = crypto_ahash_digestsize(peer_device->connection->peer_integrity_tfm);
1772+
digest_size = crypto_shash_digestsize(peer_device->connection->peer_integrity_tfm);
17731773
/*
17741774
* FIXME: Receive the incoming digest into the receive buffer
17751775
* here, together with its struct p_data?
@@ -1905,7 +1905,7 @@ static int recv_dless_read(struct drbd_peer_device *peer_device, struct drbd_req
19051905

19061906
digest_size = 0;
19071907
if (peer_device->connection->peer_integrity_tfm) {
1908-
digest_size = crypto_ahash_digestsize(peer_device->connection->peer_integrity_tfm);
1908+
digest_size = crypto_shash_digestsize(peer_device->connection->peer_integrity_tfm);
19091909
err = drbd_recv_all_warn(peer_device->connection, dig_in, digest_size);
19101910
if (err)
19111911
return err;
@@ -3542,7 +3542,7 @@ static int receive_protocol(struct drbd_connection *connection, struct packet_in
35423542
int p_proto, p_discard_my_data, p_two_primaries, cf;
35433543
struct net_conf *nc, *old_net_conf, *new_net_conf = NULL;
35443544
char integrity_alg[SHARED_SECRET_MAX] = "";
3545-
struct crypto_ahash *peer_integrity_tfm = NULL;
3545+
struct crypto_shash *peer_integrity_tfm = NULL;
35463546
void *int_dig_in = NULL, *int_dig_vv = NULL;
35473547

35483548
p_proto = be32_to_cpu(p->protocol);
@@ -3623,15 +3623,15 @@ static int receive_protocol(struct drbd_connection *connection, struct packet_in
36233623
* change.
36243624
*/
36253625

3626-
peer_integrity_tfm = crypto_alloc_ahash(integrity_alg, 0, CRYPTO_ALG_ASYNC);
3626+
peer_integrity_tfm = crypto_alloc_shash(integrity_alg, 0, CRYPTO_ALG_ASYNC);
36273627
if (IS_ERR(peer_integrity_tfm)) {
36283628
peer_integrity_tfm = NULL;
36293629
drbd_err(connection, "peer data-integrity-alg %s not supported\n",
36303630
integrity_alg);
36313631
goto disconnect;
36323632
}
36333633

3634-
hash_size = crypto_ahash_digestsize(peer_integrity_tfm);
3634+
hash_size = crypto_shash_digestsize(peer_integrity_tfm);
36353635
int_dig_in = kmalloc(hash_size, GFP_KERNEL);
36363636
int_dig_vv = kmalloc(hash_size, GFP_KERNEL);
36373637
if (!(int_dig_in && int_dig_vv)) {
@@ -3661,7 +3661,7 @@ static int receive_protocol(struct drbd_connection *connection, struct packet_in
36613661
mutex_unlock(&connection->resource->conf_update);
36623662
mutex_unlock(&connection->data.mutex);
36633663

3664-
crypto_free_ahash(connection->peer_integrity_tfm);
3664+
crypto_free_shash(connection->peer_integrity_tfm);
36653665
kfree(connection->int_dig_in);
36663666
kfree(connection->int_dig_vv);
36673667
connection->peer_integrity_tfm = peer_integrity_tfm;
@@ -3679,7 +3679,7 @@ static int receive_protocol(struct drbd_connection *connection, struct packet_in
36793679
disconnect_rcu_unlock:
36803680
rcu_read_unlock();
36813681
disconnect:
3682-
crypto_free_ahash(peer_integrity_tfm);
3682+
crypto_free_shash(peer_integrity_tfm);
36833683
kfree(int_dig_in);
36843684
kfree(int_dig_vv);
36853685
conn_request_state(connection, NS(conn, C_DISCONNECTING), CS_HARD);
@@ -3691,15 +3691,16 @@ static int receive_protocol(struct drbd_connection *connection, struct packet_in
36913691
* return: NULL (alg name was "")
36923692
* ERR_PTR(error) if something goes wrong
36933693
* or the crypto hash ptr, if it worked out ok. */
3694-
static struct crypto_ahash *drbd_crypto_alloc_digest_safe(const struct drbd_device *device,
3694+
static struct crypto_shash *drbd_crypto_alloc_digest_safe(
3695+
const struct drbd_device *device,
36953696
const char *alg, const char *name)
36963697
{
3697-
struct crypto_ahash *tfm;
3698+
struct crypto_shash *tfm;
36983699

36993700
if (!alg[0])
37003701
return NULL;
37013702

3702-
tfm = crypto_alloc_ahash(alg, 0, CRYPTO_ALG_ASYNC);
3703+
tfm = crypto_alloc_shash(alg, 0, 0);
37033704
if (IS_ERR(tfm)) {
37043705
drbd_err(device, "Can not allocate \"%s\" as %s (reason: %ld)\n",
37053706
alg, name, PTR_ERR(tfm));
@@ -3752,8 +3753,8 @@ static int receive_SyncParam(struct drbd_connection *connection, struct packet_i
37523753
struct drbd_device *device;
37533754
struct p_rs_param_95 *p;
37543755
unsigned int header_size, data_size, exp_max_sz;
3755-
struct crypto_ahash *verify_tfm = NULL;
3756-
struct crypto_ahash *csums_tfm = NULL;
3756+
struct crypto_shash *verify_tfm = NULL;
3757+
struct crypto_shash *csums_tfm = NULL;
37573758
struct net_conf *old_net_conf, *new_net_conf = NULL;
37583759
struct disk_conf *old_disk_conf = NULL, *new_disk_conf = NULL;
37593760
const int apv = connection->agreed_pro_version;
@@ -3900,14 +3901,14 @@ static int receive_SyncParam(struct drbd_connection *connection, struct packet_i
39003901
if (verify_tfm) {
39013902
strcpy(new_net_conf->verify_alg, p->verify_alg);
39023903
new_net_conf->verify_alg_len = strlen(p->verify_alg) + 1;
3903-
crypto_free_ahash(peer_device->connection->verify_tfm);
3904+
crypto_free_shash(peer_device->connection->verify_tfm);
39043905
peer_device->connection->verify_tfm = verify_tfm;
39053906
drbd_info(device, "using verify-alg: \"%s\"\n", p->verify_alg);
39063907
}
39073908
if (csums_tfm) {
39083909
strcpy(new_net_conf->csums_alg, p->csums_alg);
39093910
new_net_conf->csums_alg_len = strlen(p->csums_alg) + 1;
3910-
crypto_free_ahash(peer_device->connection->csums_tfm);
3911+
crypto_free_shash(peer_device->connection->csums_tfm);
39113912
peer_device->connection->csums_tfm = csums_tfm;
39123913
drbd_info(device, "using csums-alg: \"%s\"\n", p->csums_alg);
39133914
}
@@ -3951,9 +3952,9 @@ static int receive_SyncParam(struct drbd_connection *connection, struct packet_i
39513952
mutex_unlock(&connection->resource->conf_update);
39523953
/* just for completeness: actually not needed,
39533954
* as this is not reached if csums_tfm was ok. */
3954-
crypto_free_ahash(csums_tfm);
3955+
crypto_free_shash(csums_tfm);
39553956
/* but free the verify_tfm again, if csums_tfm did not work out */
3956-
crypto_free_ahash(verify_tfm);
3957+
crypto_free_shash(verify_tfm);
39573958
conn_request_state(peer_device->connection, NS(conn, C_DISCONNECTING), CS_HARD);
39583959
return -EIO;
39593960
}

0 commit comments

Comments
 (0)