From patchwork Fri Jul 29 14:59:08 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Sterba X-Patchwork-Id: 12932497 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8C05FC00144 for ; Fri, 29 Jul 2022 15:04:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236662AbiG2PEM (ORCPT ); Fri, 29 Jul 2022 11:04:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36008 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236674AbiG2PEK (ORCPT ); Fri, 29 Jul 2022 11:04:10 -0400 Received: from smtp-out2.suse.de (smtp-out2.suse.de [IPv6:2001:67c:2178:6::1d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 38F1C80F79 for ; Fri, 29 Jul 2022 08:04:08 -0700 (PDT) Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out2.suse.de (Postfix) with ESMTP id E07D820127; Fri, 29 Jul 2022 15:04:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1659107046; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=9nxYDyJWtLVgZrisb3iVK4cJpGZdblyabsoOlGmjFE8=; b=dpAoH9g6P5wTvnSKBpjhRR5e4QaVZYCUGAw6TQ0sVnezQz2cHyWXZet4JGdlsNkE6Cm92T iZJF1aI9vHDX4p2AAuTog0YalRtq1ViM2MfwfwK9bx4fEX7CnYl2ofQ1pmjbL4iy0GEnSI VNCQecAvanatGPIck7s2lptJecUaa0Y= Received: from ds.suse.cz (ds.suse.cz [10.100.12.205]) by relay2.suse.de (Postfix) with ESMTP id D84092C141; Fri, 29 Jul 2022 15:04:06 +0000 (UTC) Received: by ds.suse.cz (Postfix, from userid 10065) id D2A05DA85A; Fri, 29 Jul 2022 16:59:08 +0200 (CEST) From: David Sterba To: linux-btrfs@vger.kernel.org Cc: David Sterba Subject: [PATCH 1/4] btrfs: prepare more slots for checksum shash Date: Fri, 29 Jul 2022 16:59:08 +0200 Message-Id: <975b483454847df3ff97b68d77d398b684ceceeb.1659106597.git.dsterba@suse.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org There's now one checksum template fs_info::csum_shash that contains whatever the crypto API decides is the best available implementation. To allow loading other implementations after mount add more slots and symbolic names. The slot 0 is always used when calculating checksum, other slots may be set later or left empty. Signed-off-by: David Sterba --- fs/btrfs/check-integrity.c | 4 ++-- fs/btrfs/compression.c | 4 ++-- fs/btrfs/ctree.h | 13 ++++++++++++- fs/btrfs/disk-io.c | 19 ++++++++++--------- fs/btrfs/file-item.c | 4 ++-- fs/btrfs/inode.c | 4 ++-- fs/btrfs/scrub.c | 12 ++++++------ fs/btrfs/sysfs.c | 2 +- 8 files changed, 37 insertions(+), 25 deletions(-) diff --git a/fs/btrfs/check-integrity.c b/fs/btrfs/check-integrity.c index 5d20137b7b67..dbabdd01ed66 100644 --- a/fs/btrfs/check-integrity.c +++ b/fs/btrfs/check-integrity.c @@ -1647,7 +1647,7 @@ static noinline_for_stack int btrfsic_test_for_metadata( char **datav, unsigned int num_pages) { struct btrfs_fs_info *fs_info = state->fs_info; - SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); + SHASH_DESC_ON_STACK(shash, fs_info->csum_shash[CSUM_DEFAULT]); struct btrfs_header *h; u8 csum[BTRFS_CSUM_SIZE]; unsigned int i; @@ -1660,7 +1660,7 @@ static noinline_for_stack int btrfsic_test_for_metadata( if (memcmp(h->fsid, fs_info->fs_devices->fsid, BTRFS_FSID_SIZE)) return 1; - shash->tfm = fs_info->csum_shash; + shash->tfm = fs_info->csum_shash[CSUM_DEFAULT]; crypto_shash_init(shash); for (i = 0; i < num_pages; i++) { diff --git a/fs/btrfs/compression.c b/fs/btrfs/compression.c index f4564f32f6d9..096284cdf2ea 100644 --- a/fs/btrfs/compression.c +++ b/fs/btrfs/compression.c @@ -147,7 +147,7 @@ static int check_compressed_csum(struct btrfs_inode *inode, struct bio *bio, u64 disk_start) { struct btrfs_fs_info *fs_info = inode->root->fs_info; - SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); + SHASH_DESC_ON_STACK(shash, fs_info->csum_shash[CSUM_DEFAULT]); const u32 csum_size = fs_info->csum_size; const u32 sectorsize = fs_info->sectorsize; struct page *page; @@ -161,7 +161,7 @@ static int check_compressed_csum(struct btrfs_inode *inode, struct bio *bio, test_bit(BTRFS_FS_STATE_NO_CSUMS, &fs_info->fs_state)) return 0; - shash->tfm = fs_info->csum_shash; + shash->tfm = fs_info->csum_shash[CSUM_DEFAULT]; for (i = 0; i < cb->nr_pages; i++) { u32 pg_offset; diff --git a/fs/btrfs/ctree.h b/fs/btrfs/ctree.h index 9c21e214d29e..9f145b59e7d2 100644 --- a/fs/btrfs/ctree.h +++ b/fs/btrfs/ctree.h @@ -656,6 +656,13 @@ enum btrfs_exclusive_operation { BTRFS_EXCLOP_SWAP_ACTIVATE, }; +enum btrfs_csum_impl { + CSUM_DEFAULT, + CSUM_GENERIC, + CSUM_ACCEL, + CSUM_COUNT +}; + struct btrfs_fs_info { u8 chunk_tree_uuid[BTRFS_UUID_SIZE]; unsigned long flags; @@ -1036,7 +1043,11 @@ struct btrfs_fs_info { spinlock_t swapfile_pins_lock; struct rb_root swapfile_pins; - struct crypto_shash *csum_shash; + /* + * Templates for checksum calculation, slot 0 is for the currently used + * one, other slots are optional for available implementations. + */ + struct crypto_shash *csum_shash[CSUM_COUNT]; /* Type of exclusive operation running, protected by super_lock */ enum btrfs_exclusive_operation exclusive_operation; diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c index de440ebf5648..dc41db822322 100644 --- a/fs/btrfs/disk-io.c +++ b/fs/btrfs/disk-io.c @@ -100,8 +100,9 @@ void __cold btrfs_end_io_wq_exit(void) static void btrfs_free_csum_hash(struct btrfs_fs_info *fs_info) { - if (fs_info->csum_shash) - crypto_free_shash(fs_info->csum_shash); + for (int i = 0; i < CSUM_COUNT; i++) + if (fs_info->csum_shash[i]) + crypto_free_shash(fs_info->csum_shash[i]); } /* @@ -211,11 +212,11 @@ static void csum_tree_block(struct extent_buffer *buf, u8 *result) struct btrfs_fs_info *fs_info = buf->fs_info; const int num_pages = num_extent_pages(buf); const int first_page_part = min_t(u32, PAGE_SIZE, fs_info->nodesize); - SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); + SHASH_DESC_ON_STACK(shash, fs_info->csum_shash[CSUM_DEFAULT]); char *kaddr; int i; - shash->tfm = fs_info->csum_shash; + shash->tfm = fs_info->csum_shash[CSUM_DEFAULT]; crypto_shash_init(shash); kaddr = page_address(buf->pages[0]) + offset_in_page(buf->start); crypto_shash_update(shash, kaddr + BTRFS_CSUM_SIZE, @@ -290,9 +291,9 @@ static int btrfs_check_super_csum(struct btrfs_fs_info *fs_info, struct btrfs_super_block *disk_sb = (struct btrfs_super_block *)raw_disk_sb; char result[BTRFS_CSUM_SIZE]; - SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); + SHASH_DESC_ON_STACK(shash, fs_info->csum_shash[CSUM_DEFAULT]); - shash->tfm = fs_info->csum_shash; + shash->tfm = fs_info->csum_shash[CSUM_DEFAULT]; /* * The super_block structure does not span the whole @@ -2535,7 +2536,7 @@ static int btrfs_init_csum_hash(struct btrfs_fs_info *fs_info, u16 csum_type) return PTR_ERR(csum_shash); } - fs_info->csum_shash = csum_shash; + fs_info->csum_shash[CSUM_DEFAULT] = csum_shash; return 0; } @@ -4090,7 +4091,7 @@ static int write_dev_supers(struct btrfs_device *device, { struct btrfs_fs_info *fs_info = device->fs_info; struct address_space *mapping = device->bdev->bd_inode->i_mapping; - SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); + SHASH_DESC_ON_STACK(shash, fs_info->csum_shash[CSUM_DEFAULT]); int i; int errors = 0; int ret; @@ -4099,7 +4100,7 @@ static int write_dev_supers(struct btrfs_device *device, if (max_mirrors == 0) max_mirrors = BTRFS_SUPER_MIRROR_MAX; - shash->tfm = fs_info->csum_shash; + shash->tfm = fs_info->csum_shash[CSUM_DEFAULT]; for (i = 0; i < max_mirrors; i++) { struct page *page; diff --git a/fs/btrfs/file-item.c b/fs/btrfs/file-item.c index c828f971a346..6e416230733b 100644 --- a/fs/btrfs/file-item.c +++ b/fs/btrfs/file-item.c @@ -635,7 +635,7 @@ blk_status_t btrfs_csum_one_bio(struct btrfs_inode *inode, struct bio *bio, u64 offset, bool one_ordered) { struct btrfs_fs_info *fs_info = inode->root->fs_info; - SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); + SHASH_DESC_ON_STACK(shash, fs_info->csum_shash[CSUM_DEFAULT]); struct btrfs_ordered_sum *sums; struct btrfs_ordered_extent *ordered = NULL; const bool use_page_offsets = (offset == (u64)-1); @@ -663,7 +663,7 @@ blk_status_t btrfs_csum_one_bio(struct btrfs_inode *inode, struct bio *bio, sums->bytenr = bio->bi_iter.bi_sector << 9; index = 0; - shash->tfm = fs_info->csum_shash; + shash->tfm = fs_info->csum_shash[CSUM_DEFAULT]; bio_for_each_segment(bvec, bio, iter) { if (use_page_offsets) diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c index d50448bf8eed..86885f78c6c6 100644 --- a/fs/btrfs/inode.c +++ b/fs/btrfs/inode.c @@ -3344,7 +3344,7 @@ static int check_data_csum(struct inode *inode, struct btrfs_bio *bbio, u64 start) { struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); - SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); + SHASH_DESC_ON_STACK(shash, fs_info->csum_shash[CSUM_DEFAULT]); char *kaddr; u32 len = fs_info->sectorsize; const u32 csum_size = fs_info->csum_size; @@ -3358,7 +3358,7 @@ static int check_data_csum(struct inode *inode, struct btrfs_bio *bbio, csum_expected = ((u8 *)bbio->csum) + offset_sectors * csum_size; kaddr = kmap_atomic(page); - shash->tfm = fs_info->csum_shash; + shash->tfm = fs_info->csum_shash[CSUM_DEFAULT]; crypto_shash_digest(shash, kaddr + pgoff, len, csum); kunmap_atomic(kaddr); diff --git a/fs/btrfs/scrub.c b/fs/btrfs/scrub.c index e7b0323e6efd..cc2ee8b71086 100644 --- a/fs/btrfs/scrub.c +++ b/fs/btrfs/scrub.c @@ -1786,7 +1786,7 @@ static int scrub_checksum_data(struct scrub_block *sblock) { struct scrub_ctx *sctx = sblock->sctx; struct btrfs_fs_info *fs_info = sctx->fs_info; - SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); + SHASH_DESC_ON_STACK(shash, fs_info->csum_shash[CSUM_DEFAULT]); u8 csum[BTRFS_CSUM_SIZE]; struct scrub_sector *sector; char *kaddr; @@ -1798,7 +1798,7 @@ static int scrub_checksum_data(struct scrub_block *sblock) kaddr = page_address(sector->page); - shash->tfm = fs_info->csum_shash; + shash->tfm = fs_info->csum_shash[CSUM_DEFAULT]; crypto_shash_init(shash); /* @@ -1817,7 +1817,7 @@ static int scrub_checksum_tree_block(struct scrub_block *sblock) struct scrub_ctx *sctx = sblock->sctx; struct btrfs_header *h; struct btrfs_fs_info *fs_info = sctx->fs_info; - SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); + SHASH_DESC_ON_STACK(shash, fs_info->csum_shash[CSUM_DEFAULT]); u8 calculated_csum[BTRFS_CSUM_SIZE]; u8 on_disk_csum[BTRFS_CSUM_SIZE]; /* @@ -1861,7 +1861,7 @@ static int scrub_checksum_tree_block(struct scrub_block *sblock) BTRFS_UUID_SIZE)) sblock->header_error = 1; - shash->tfm = fs_info->csum_shash; + shash->tfm = fs_info->csum_shash[CSUM_DEFAULT]; crypto_shash_init(shash); crypto_shash_update(shash, kaddr + BTRFS_CSUM_SIZE, sectorsize - BTRFS_CSUM_SIZE); @@ -1883,7 +1883,7 @@ static int scrub_checksum_super(struct scrub_block *sblock) struct btrfs_super_block *s; struct scrub_ctx *sctx = sblock->sctx; struct btrfs_fs_info *fs_info = sctx->fs_info; - SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); + SHASH_DESC_ON_STACK(shash, fs_info->csum_shash[CSUM_DEFAULT]); u8 calculated_csum[BTRFS_CSUM_SIZE]; struct scrub_sector *sector; char *kaddr; @@ -1904,7 +1904,7 @@ static int scrub_checksum_super(struct scrub_block *sblock) if (!scrub_check_fsid(s->fsid, sector)) ++fail_cor; - shash->tfm = fs_info->csum_shash; + shash->tfm = fs_info->csum_shash[CSUM_DEFAULT]; crypto_shash_init(shash); crypto_shash_digest(shash, kaddr + BTRFS_CSUM_SIZE, BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE, calculated_csum); diff --git a/fs/btrfs/sysfs.c b/fs/btrfs/sysfs.c index 92a1fa8e3da6..caa84d398fec 100644 --- a/fs/btrfs/sysfs.c +++ b/fs/btrfs/sysfs.c @@ -939,7 +939,7 @@ static ssize_t btrfs_checksum_show(struct kobject *kobj, return sysfs_emit(buf, "%s (%s)\n", btrfs_super_csum_name(csum_type), - crypto_shash_driver_name(fs_info->csum_shash)); + crypto_shash_driver_name(fs_info->csum_shash[CSUM_DEFAULT])); } BTRFS_ATTR(, checksum, btrfs_checksum_show); From patchwork Fri Jul 29 14:59:10 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Sterba X-Patchwork-Id: 12932498 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 56C8BC19F2A for ; Fri, 29 Jul 2022 15:04:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236711AbiG2PEM (ORCPT ); Fri, 29 Jul 2022 11:04:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36116 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236696AbiG2PEL (ORCPT ); Fri, 29 Jul 2022 11:04:11 -0400 Received: from smtp-out1.suse.de (smtp-out1.suse.de [IPv6:2001:67c:2178:6::1c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5622617594 for ; Fri, 29 Jul 2022 08:04:10 -0700 (PDT) Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out1.suse.de (Postfix) with ESMTP id 072B7344AE; Fri, 29 Jul 2022 15:04:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1659107049; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=qwH5ZUluiwR7VvjqTY2FaxqNo9W0hVOgbNjKSSZtLN8=; b=mY6q/x95ACKTP7IlefMXt4c+vEzBvR6GTZlYn3we3XPyNsdYXjT+skDXsWqWr8CEy7HdyP nFEw7TwvLvaRQtZQ0q2CeY1r8mEP7xigQLnUvtwyzGf2PqFu2E6GsQU/o2JjWnj53HA/V6 mI/RoHlSzhmJo1eI1ayaeGTIX/ofPlw= Received: from ds.suse.cz (ds.suse.cz [10.100.12.205]) by relay2.suse.de (Postfix) with ESMTP id F29CF2C141; Fri, 29 Jul 2022 15:04:08 +0000 (UTC) Received: by ds.suse.cz (Postfix, from userid 10065) id 02489DA85A; Fri, 29 Jul 2022 16:59:10 +0200 (CEST) From: David Sterba To: linux-btrfs@vger.kernel.org Cc: David Sterba Subject: [PATCH 2/4] btrfs: assign checksum shash slots on init Date: Fri, 29 Jul 2022 16:59:10 +0200 Message-Id: <32716bb3c21666ba4019bca40675cc46130af149.1659106597.git.dsterba@suse.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org When initializing checksum implementation on first mount, assign it to the proper slot based on the driver name. If it contains "generic" it's considered the non-accelerated one. Based on that properly set the BTRFS_FS_CSUM_IMPL_FAST bit, previously it could be mistakenly set as fast despite a different checksum (eg. sha256) with generic implementation. Signed-off-by: David Sterba --- fs/btrfs/disk-io.c | 13 ++++++++++++- fs/btrfs/super.c | 2 -- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c index dc41db822322..2f5d8d2e2c48 100644 --- a/fs/btrfs/disk-io.c +++ b/fs/btrfs/disk-io.c @@ -2536,7 +2536,18 @@ static int btrfs_init_csum_hash(struct btrfs_fs_info *fs_info, u16 csum_type) return PTR_ERR(csum_shash); } - fs_info->csum_shash[CSUM_DEFAULT] = csum_shash; + /* + * Find the fastest implementation available, but keep the slots + * matching the type. + */ + if (strstr(crypto_shash_driver_name(fs_info->csum_shash[CSUM_DEFAULT]), + "generic") != NULL) { + fs_info->csum_shash[CSUM_GENERIC] = csum_shash; + clear_bit(BTRFS_FS_CSUM_IMPL_FAST, &fs_info->flags); + } else { + fs_info->csum_shash[CSUM_ACCEL] = csum_shash; + set_bit(BTRFS_FS_CSUM_IMPL_FAST, &fs_info->flags); + } return 0; } diff --git a/fs/btrfs/super.c b/fs/btrfs/super.c index 6627dd7875ee..0d306f555e09 100644 --- a/fs/btrfs/super.c +++ b/fs/btrfs/super.c @@ -1816,8 +1816,6 @@ static struct dentry *btrfs_mount_root(struct file_system_type *fs_type, } else { snprintf(s->s_id, sizeof(s->s_id), "%pg", bdev); btrfs_sb(s)->bdev_holder = fs_type; - if (!strstr(crc32c_impl(), "generic")) - set_bit(BTRFS_FS_CSUM_IMPL_FAST, &fs_info->flags); error = btrfs_fill_super(s, fs_devices, data); } if (!error) From patchwork Fri Jul 29 14:59:13 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Sterba X-Patchwork-Id: 12932499 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id EB96BC19F2B for ; Fri, 29 Jul 2022 15:04:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236747AbiG2PEO (ORCPT ); Fri, 29 Jul 2022 11:04:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36138 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236756AbiG2PEN (ORCPT ); Fri, 29 Jul 2022 11:04:13 -0400 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8C42317594 for ; Fri, 29 Jul 2022 08:04:12 -0700 (PDT) Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out2.suse.de (Postfix) with ESMTP id 3B1BE20127; Fri, 29 Jul 2022 15:04:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1659107051; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=FcV40/KjyFxf9KMvqSVJmJIHMuo0PNmfKPtC2todH/I=; b=OGBuHG8mWY9Nv2IbrM/mMsNaVA7Ahb+Khi6q9x7m9K/SKtiP6uERnAkp5MRuDd7anv89VI Vdn3oOFQ4lDnR5FMhbbRoPPUFdBpdTe+SyUhR7LKv23XqKCqnYeMp28HEDAo74NONB2r4Y 8znfeWJxrbRdsgPAd3oOsOMku5hlQmo= Received: from ds.suse.cz (ds.suse.cz [10.100.12.205]) by relay2.suse.de (Postfix) with ESMTP id 334E12C142; Fri, 29 Jul 2022 15:04:11 +0000 (UTC) Received: by ds.suse.cz (Postfix, from userid 10065) id 289C5DA85A; Fri, 29 Jul 2022 16:59:13 +0200 (CEST) From: David Sterba To: linux-btrfs@vger.kernel.org Cc: David Sterba Subject: [PATCH 3/4] btrfs: add checksum implementation selection after mount Date: Fri, 29 Jul 2022 16:59:13 +0200 Message-Id: <7f9d78aa4f3248353a4c92fc7db9720823cf765c.1659106597.git.dsterba@suse.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org The sysfs file /sys/fs/btrfs/FSID/checksum shows the filesystem checksum and the crypto module implementing it. In the scenario when there's only the default generic implementation available during mount it's selected, even if there's an unloaded module with accelerated version. This happens with sha256 that's often built-in so the crypto API may not trigger loading the modules and select the fastest implementation. Such filesystem could be left using in the generic for the whole time. Remount can't fix that and full umount/mount cycle may be impossible eg. for a root filesystem. Allow writing strings to the sysfs checksum file that will trigger loading the crypto shash again and check if the found module is the desired one. Possible values: - default - select whatever is considered default by crypto subsystem, either generic or accelerated - accel - try loading an accelerated implementation (must not contain "generic" in the name) - generic - load and select the generic implementation A typical scenario, assuming sha256 is built-in: $ mkfs.btrfs --csum sha256 $ lsmod | grep sha256 $ mount /dev /mnt $ cat ...FSID/checksum sha256 (sha256-generic) $ modprobe sha256 $ lsmod | grep sha256 sha256_ssse3 $ echo accel > ...FSID/checksum sha256 (sha256-ni) The crypto shash for all slots has the same lifetime as the mount, so it's not possible to unload the crypto module. Signed-off-by: David Sterba --- fs/btrfs/sysfs.c | 86 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 85 insertions(+), 1 deletion(-) diff --git a/fs/btrfs/sysfs.c b/fs/btrfs/sysfs.c index caa84d398fec..0909f1da425c 100644 --- a/fs/btrfs/sysfs.c +++ b/fs/btrfs/sysfs.c @@ -942,7 +942,91 @@ static ssize_t btrfs_checksum_show(struct kobject *kobj, crypto_shash_driver_name(fs_info->csum_shash[CSUM_DEFAULT])); } -BTRFS_ATTR(, checksum, btrfs_checksum_show); +static const char csum_impl[][8] = { + [CSUM_DEFAULT] = "default", + [CSUM_GENERIC] = "generic", + [CSUM_ACCEL] = "accel", +}; + +static int select_checksum(struct btrfs_fs_info *fs_info, enum btrfs_csum_impl type) +{ + /* Already set */ + if (fs_info->csum_shash[CSUM_DEFAULT] == fs_info->csum_shash[type]) + return 0; + + /* Allocate new if needed */ + if (!fs_info->csum_shash[type]) { + const u16 csum_type = btrfs_super_csum_type(fs_info->super_copy); + const char *csum_driver = btrfs_super_csum_driver(csum_type); + struct crypto_shash *shash1, *tmp; + char full_name[32]; + u32 mask = 0; + + /* + * Generic must be requested explicitly, otherwise it could + * be accelerated implementation with highest priority. + */ + scnprintf(full_name, sizeof(full_name), "%s%s", csum_driver, + (type == CSUM_GENERIC ? "-generic" : "")); + + shash1 = crypto_alloc_shash(full_name, 0, mask); + if (IS_ERR(shash1)) + return PTR_ERR(shash1); + + /* Accelerated requested but generic found */ + if (type != CSUM_GENERIC && + strstr(crypto_shash_driver_name(shash1), "generic")) { + crypto_free_shash(shash1); + return -ENOENT; + } + + tmp = cmpxchg(&fs_info->csum_shash[type], NULL, shash1); + if (tmp) { + /* Something raced in */ + crypto_free_shash(shash1); + } + } + + /* Select it */ + fs_info->csum_shash[CSUM_DEFAULT] = fs_info->csum_shash[type]; + return 0; +} + +static bool strmatch(const char *buffer, const char *string); + +static ssize_t btrfs_checksum_store(struct kobject *kobj, + struct kobj_attribute *a, + const char *buf, size_t len) +{ + struct btrfs_fs_info *fs_info = to_fs_info(kobj); + + if (!fs_info) + return -EPERM; + + /* Pick the best available, generic or accelerated */ + if (strmatch(buf, csum_impl[CSUM_DEFAULT])) { + if (fs_info->csum_shash[CSUM_ACCEL]) { + fs_info->csum_shash[CSUM_DEFAULT] = + fs_info->csum_shash[CSUM_ACCEL]; + return len; + } + ASSERT(fs_info->csum_shash[CSUM_GENERIC]); + fs_info->csum_shash[CSUM_DEFAULT] = fs_info->csum_shash[CSUM_GENERIC]; + return len; + } + + for (int i = 1; i < CSUM_COUNT; i++) { + if (strmatch(buf, csum_impl[i])) { + int ret; + + ret = select_checksum(fs_info, i); + return ret < 0 ? ret : len; + } + } + + return -EINVAL; +} +BTRFS_ATTR_RW(, checksum, btrfs_checksum_show, btrfs_checksum_store); static ssize_t btrfs_exclusive_operation_show(struct kobject *kobj, struct kobj_attribute *a, char *buf) From patchwork Fri Jul 29 14:59:15 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Sterba X-Patchwork-Id: 12932500 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7DBB2C00144 for ; Fri, 29 Jul 2022 15:04:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236808AbiG2PER (ORCPT ); Fri, 29 Jul 2022 11:04:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36176 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236696AbiG2PEP (ORCPT ); Fri, 29 Jul 2022 11:04:15 -0400 Received: from smtp-out1.suse.de (smtp-out1.suse.de [IPv6:2001:67c:2178:6::1c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AA73A17594 for ; Fri, 29 Jul 2022 08:04:14 -0700 (PDT) Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out1.suse.de (Postfix) with ESMTP id 4C1763446F; Fri, 29 Jul 2022 15:04:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1659107053; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=qct9lZFW3dBU72pMfD5lDIksolL5Y2oxZOEasnIgvqg=; b=M6RJmakCyiO6fzLMQwQRUjwXxpGMGyd4QSL5Lo3FJIRav4Q6tAup0vOs5SXOcnkwEYxdOL axSKwn9gHQ9LUzGDBk3Du3Kkk861cY9DuqpLrx+B4rTwwiuMQPAJvx8ichCs0BeNyqHgg4 KVPN9oqhbEGNL4Hu3HFXVXRUopGcO+o= Received: from ds.suse.cz (ds.suse.cz [10.100.12.205]) by relay2.suse.de (Postfix) with ESMTP id 437152C142; Fri, 29 Jul 2022 15:04:13 +0000 (UTC) Received: by ds.suse.cz (Postfix, from userid 10065) id 46F83DA85A; Fri, 29 Jul 2022 16:59:15 +0200 (CEST) From: David Sterba To: linux-btrfs@vger.kernel.org Cc: David Sterba Subject: [PATCH 4/4] btrfs: sysfs: print all loaded csums implementations Date: Fri, 29 Jul 2022 16:59:15 +0200 Message-Id: X-Mailer: git-send-email 2.36.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org Extend the sysfs FSID/checksum file and append lines with the selector strings and implementation if loaded, or 'none'. Output may look like: crc32c (crc32c-generic) generic: crc32c-generic accel: crc32c-intel Scripts that rely on single line in the file need to be updated. All available and loaded implementations can be found in /proc/crypto . Signed-off-by: David Sterba --- fs/btrfs/sysfs.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/fs/btrfs/sysfs.c b/fs/btrfs/sysfs.c index 0909f1da425c..9ce353184c46 100644 --- a/fs/btrfs/sysfs.c +++ b/fs/btrfs/sysfs.c @@ -937,9 +937,18 @@ static ssize_t btrfs_checksum_show(struct kobject *kobj, struct btrfs_fs_info *fs_info = to_fs_info(kobj); u16 csum_type = btrfs_super_csum_type(fs_info->super_copy); - return sysfs_emit(buf, "%s (%s)\n", - btrfs_super_csum_name(csum_type), - crypto_shash_driver_name(fs_info->csum_shash[CSUM_DEFAULT])); + return sysfs_emit(buf, + "%s (%s)\n" + "generic: %s\n" + "accel: %s\n", + btrfs_super_csum_name(csum_type), + crypto_shash_driver_name(fs_info->csum_shash[CSUM_DEFAULT]), + fs_info->csum_shash[CSUM_GENERIC] + ? crypto_shash_driver_name(fs_info->csum_shash[CSUM_GENERIC]) + : "none", + fs_info->csum_shash[CSUM_ACCEL] + ? crypto_shash_driver_name(fs_info->csum_shash[CSUM_ACCEL]) + : "none"); } static const char csum_impl[][8] = {