Received: by 2002:a05:7412:b130:b0:e2:908c:2ebd with SMTP id az48csp2034042rdb; Sun, 19 Nov 2023 22:59:02 -0800 (PST) X-Google-Smtp-Source: AGHT+IHKsorleTrGr9vaFFGwtV2+NTtg59ff6XD/Q4V0jx+PzwDN2WacOwHUkXismqLjJiH9Xbvm X-Received: by 2002:a05:6870:808:b0:1e9:db34:a573 with SMTP id fw8-20020a056870080800b001e9db34a573mr6771916oab.26.1700463541800; Sun, 19 Nov 2023 22:59:01 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700463541; cv=none; d=google.com; s=arc-20160816; b=bJF12MkoG+bW+jSzxIZ94WmVYTGNrwBZfWwHswO1dA+40r9rDzYvIcnkwqxWyDTc+x QMwV3leUavQXVrXrfQroV0KrMezBzQb3Yay8jzCckJhS6YevHQUiOJVsb7rn7pVW5kWx mfi96f+6WY2Px7O56Xcwo0JI1ijy0DFeepaCU8gF1HgYy6rEbqd1YqA49Z7dPTE/pxZp n71sQTZqHfAoSaLV2kc8SVeYGcR/c79YKoTnp6PAYftwkbp2+iOrilPgWynLPvbheim7 mBl2VBuPuTsPyVxG6PKiyRmKxSq85TygQGv9X9p26BO3bujlHztnp+BTTcFlvwUKx2Sk ZAyg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=p3Wb7smznhQ1O5YfmYQBuWMh/Izn5DimV+3IJJnnizs=; fh=Jud09z+YlCXb0ua/W6fLKsGoEV70mCWu+XB4J+f9ujM=; b=olaIJOAQUGUjSELsgR4TypCIztyl2S2cGzaOsTjTCAh0KYMYADlspTqJJ8i5Jd+Run yCOMr5Q9n/4eX+3QCWXp6HWOlI8raF8nQ3qcDJK3eY0EWZHexH8yAGeoeLSF6l/DwR5e C73lIYz4cE/mk8Bh/grpUp31eliqdM+2BzCbW8dm8MF7YqXcK6qtMd/H8hxPui/SWNgL EfHFdQ4JqT11UCdr/K/B7g5LsKqSaT8KbWX3twfQuEYttI3nyBDMOuN370pIlkiKm8z6 V2xZ7ac9LVkG+mKNTCXeUFnbcgWzhwZ79wNhI85ydJXmVKstfQOF66v1COOb4OlMLLJs /l4A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=MCMm4SzP; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id f65-20020a625144000000b006bd3ba8e610si7408108pfb.133.2023.11.19.22.59.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 19 Nov 2023 22:59:01 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=MCMm4SzP; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id 056148053C7D; Sun, 19 Nov 2023 22:58:58 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229635AbjKTG6r (ORCPT + 99 others); Mon, 20 Nov 2023 01:58:47 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44502 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229483AbjKTG6q (ORCPT ); Mon, 20 Nov 2023 01:58:46 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3E873126 for ; Sun, 19 Nov 2023 22:58:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1700463521; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=p3Wb7smznhQ1O5YfmYQBuWMh/Izn5DimV+3IJJnnizs=; b=MCMm4SzPwCBbCwmHUUPcwkp5kCCdaLeOwSHTRx8VhwQqVzoDqhTdhSmTvl41TjZYwdN2qb LnvNwwwbocW+XxeAe84aDsyQdqQIWacPPsmw53hXMlP+shD23bjhXder/UuWxmu+UsV4V8 Z3w/WT2iwHjZcWsFxZkjg/dZijhIbvY= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-154-SFaIrzkMPGuntee77smefQ-1; Mon, 20 Nov 2023 01:58:39 -0500 X-MC-Unique: SFaIrzkMPGuntee77smefQ-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id AA32585A58B; Mon, 20 Nov 2023 06:58:38 +0000 (UTC) Received: from fedora (unknown [10.72.120.15]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 041C440C6EBA; Mon, 20 Nov 2023 06:58:33 +0000 (UTC) Date: Mon, 20 Nov 2023 14:58:29 +0800 From: Ming Lei To: Yu Kuai Cc: axboe@kernel.dk, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, yi.zhang@huawei.com, yangerkun@huawei.com, "yukuai (C)" Subject: Re: [PATCH 1/2] block: introduce new field flags in block_device Message-ID: References: <20231120093847.2228127-1-yukuai1@huaweicloud.com> <20231120093847.2228127-2-yukuai1@huaweicloud.com> <9fc5e82f-9106-e3f9-4f06-d2d79e8e99a8@huaweicloud.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <9fc5e82f-9106-e3f9-4f06-d2d79e8e99a8@huaweicloud.com> X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.2 X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Sun, 19 Nov 2023 22:58:58 -0800 (PST) On Mon, Nov 20, 2023 at 02:37:59PM +0800, Yu Kuai wrote: > Hi, > > 在 2023/11/20 11:00, Ming Lei 写道: > > On Mon, Nov 20, 2023 at 05:38:46PM +0800, Yu Kuai wrote: > > > From: Yu Kuai > > > > > > There are multiple switches in struct block_device, use seperate bool > > > fields for them is not gracefully. Add a new field flags and replace > > > swithes to a bit, there are no functional changes, and preapre to add > > > a new switch in the next patch. > > > > > > Signed-off-by: Yu Kuai > > > --- > > > block/bdev.c | 15 ++++++++------- > > > block/blk-core.c | 7 ++++--- > > > block/genhd.c | 8 +++++--- > > > block/ioctl.c | 2 +- > > > include/linux/blk_types.h | 12 ++++++------ > > > include/linux/blkdev.h | 5 +++-- > > > 6 files changed, 27 insertions(+), 22 deletions(-) > > > > > > diff --git a/block/bdev.c b/block/bdev.c > > > index fc8d28d77495..cb849bcf61ae 100644 > > > --- a/block/bdev.c > > > +++ b/block/bdev.c > > > @@ -408,10 +408,10 @@ struct block_device *bdev_alloc(struct gendisk *disk, u8 partno) > > > bdev->bd_partno = partno; > > > bdev->bd_inode = inode; > > > bdev->bd_queue = disk->queue; > > > - if (partno) > > > - bdev->bd_has_submit_bio = disk->part0->bd_has_submit_bio; > > > + if (partno && test_bit(BD_FLAG_HAS_SUBMIT_BIO, &disk->part0->flags)) > > > + set_bit(BD_FLAG_HAS_SUBMIT_BIO, &bdev->flags); > > > else > > > - bdev->bd_has_submit_bio = false; > > > + clear_bit(BD_FLAG_HAS_SUBMIT_BIO, &bdev->flags); > > > bdev->bd_stats = alloc_percpu(struct disk_stats); > > > if (!bdev->bd_stats) { > > > iput(inode); > > > @@ -612,7 +612,7 @@ static void bd_end_claim(struct block_device *bdev, void *holder) > > > bdev->bd_holder = NULL; > > > bdev->bd_holder_ops = NULL; > > > mutex_unlock(&bdev->bd_holder_lock); > > > - if (bdev->bd_write_holder) > > > + if (test_bit(BD_FLAG_WRITE_HOLDER, &bdev->flags)) > > > unblock = true; > > > } > > > if (!whole->bd_holders) > > > @@ -625,7 +625,7 @@ static void bd_end_claim(struct block_device *bdev, void *holder) > > > */ > > > if (unblock) { > > > disk_unblock_events(bdev->bd_disk); > > > - bdev->bd_write_holder = false; > > > + clear_bit(BD_FLAG_WRITE_HOLDER, &bdev->flags); > > > } > > > } > > > @@ -878,9 +878,10 @@ struct bdev_handle *bdev_open_by_dev(dev_t dev, blk_mode_t mode, void *holder, > > > * writeable reference is too fragile given the way @mode is > > > * used in blkdev_get/put(). > > > */ > > > - if ((mode & BLK_OPEN_WRITE) && !bdev->bd_write_holder && > > > + if ((mode & BLK_OPEN_WRITE) && > > > + !test_bit(BD_FLAG_WRITE_HOLDER, &bdev->flags) && > > > (disk->event_flags & DISK_EVENT_FLAG_BLOCK_ON_EXCL_WRITE)) { > > > - bdev->bd_write_holder = true; > > > + set_bit(BD_FLAG_WRITE_HOLDER, &bdev->flags); > > > unblock_events = false; > > > } > > > } > > > diff --git a/block/blk-core.c b/block/blk-core.c > > > index fdf25b8d6e78..577a693165d8 100644 > > > --- a/block/blk-core.c > > > +++ b/block/blk-core.c > > > @@ -482,7 +482,8 @@ __setup("fail_make_request=", setup_fail_make_request); > > > bool should_fail_request(struct block_device *part, unsigned int bytes) > > > { > > > - return part->bd_make_it_fail && should_fail(&fail_make_request, bytes); > > > + return test_bit(BD_FLAG_MAKE_IT_FAIL, &part->flags) && > > > + should_fail(&fail_make_request, bytes); > > > } > > > static int __init fail_make_request_debugfs(void) > > > @@ -595,7 +596,7 @@ static void __submit_bio(struct bio *bio) > > > if (unlikely(!blk_crypto_bio_prep(&bio))) > > > return; > > > - if (!bio->bi_bdev->bd_has_submit_bio) { > > > + if (!test_bit(BD_FLAG_HAS_SUBMIT_BIO, &bio->bi_bdev->flags)) { > > > blk_mq_submit_bio(bio); > > > } else if (likely(bio_queue_enter(bio) == 0)) { > > > struct gendisk *disk = bio->bi_bdev->bd_disk; > > > @@ -703,7 +704,7 @@ void submit_bio_noacct_nocheck(struct bio *bio) > > > */ > > > if (current->bio_list) > > > bio_list_add(¤t->bio_list[0], bio); > > > - else if (!bio->bi_bdev->bd_has_submit_bio) > > > + else if (!test_bit(BD_FLAG_HAS_SUBMIT_BIO, &bio->bi_bdev->flags)) > > > __submit_bio_noacct_mq(bio); > > > else > > > __submit_bio_noacct(bio); > > > diff --git a/block/genhd.c b/block/genhd.c > > > index c9d06f72c587..026f4c6d5b7e 100644 > > > --- a/block/genhd.c > > > +++ b/block/genhd.c > > > @@ -413,7 +413,8 @@ int __must_check device_add_disk(struct device *parent, struct gendisk *disk, > > > elevator_init_mq(disk->queue); > > > /* Mark bdev as having a submit_bio, if needed */ > > > - disk->part0->bd_has_submit_bio = disk->fops->submit_bio != NULL; > > > + if (disk->fops->submit_bio) > > > + set_bit(BD_FLAG_HAS_SUBMIT_BIO, &disk->part0->flags); > > > /* > > > * If the driver provides an explicit major number it also must provide > > > @@ -1062,7 +1063,8 @@ static DEVICE_ATTR(diskseq, 0444, diskseq_show, NULL); > > > ssize_t part_fail_show(struct device *dev, > > > struct device_attribute *attr, char *buf) > > > { > > > - return sprintf(buf, "%d\n", dev_to_bdev(dev)->bd_make_it_fail); > > > + return sprintf(buf, "%d\n", > > > + test_bit(BD_FLAG_MAKE_IT_FAIL, &dev_to_bdev(dev)->flags)); > > > } > > > ssize_t part_fail_store(struct device *dev, > > > @@ -1072,7 +1074,7 @@ ssize_t part_fail_store(struct device *dev, > > > int i; > > > if (count > 0 && sscanf(buf, "%d", &i) > 0) > > > - dev_to_bdev(dev)->bd_make_it_fail = i; > > > + set_bit(BD_FLAG_MAKE_IT_FAIL, &dev_to_bdev(dev)->flags); > > > return count; > > > } > > > diff --git a/block/ioctl.c b/block/ioctl.c > > > index 4160f4e6bd5b..a548c718a5fb 100644 > > > --- a/block/ioctl.c > > > +++ b/block/ioctl.c > > > @@ -394,7 +394,7 @@ static int blkdev_roset(struct block_device *bdev, unsigned cmd, > > > if (ret) > > > return ret; > > > } > > > - bdev->bd_read_only = n; > > > + set_bit(BD_FLAG_READ_ONLY, &bdev->flags); > > > return 0; > > > } > > > diff --git a/include/linux/blk_types.h b/include/linux/blk_types.h > > > index 52e264d5a830..95bd26c62655 100644 > > > --- a/include/linux/blk_types.h > > > +++ b/include/linux/blk_types.h > > > @@ -37,17 +37,20 @@ struct bio_crypt_ctx; > > > #define PAGE_SECTORS (1 << PAGE_SECTORS_SHIFT) > > > #define SECTOR_MASK (PAGE_SECTORS - 1) > > > +#define BD_FLAG_READ_ONLY 0 /* read-only-policy */ > > > +#define BD_FLAG_WRITE_HOLDER 1 > > > +#define BD_FLAG_HAS_SUBMIT_BIO 2 > > > +#define BD_FLAG_MAKE_IT_FAIL 3 > > > + > > > struct block_device { > > > sector_t bd_start_sect; > > > sector_t bd_nr_sectors; > > > struct gendisk * bd_disk; > > > struct request_queue * bd_queue; > > > struct disk_stats __percpu *bd_stats; > > > + unsigned long flags; > > > unsigned long bd_stamp; > > > - bool bd_read_only; /* read-only policy */ > > > u8 bd_partno; > > > - bool bd_write_holder; > > > - bool bd_has_submit_bio; > > > > The idea looks good, but not necessary to add new 8 bytes, and you may > > put all these flags and `bd_partno` into single 'unsigned long', and add > > helpers to retrieve part no, since there are not many ->bd_partno > > references. > > Yes, it make sense. > > By the way, take a look at component of block_device, I think they can > be reorganized to save some space, in this case, new 8 bytes won't make > this struct bigger, in fact, total size will be shrinked by 8 types > after following changes: > > diff --git a/include/linux/blk_types.h b/include/linux/blk_types.h > index 95bd26c62655..c94242a9ad92 100644 > --- a/include/linux/blk_types.h > +++ b/include/linux/blk_types.h > @@ -50,7 +50,6 @@ struct block_device { > struct disk_stats __percpu *bd_stats; > unsigned long flags; > unsigned long bd_stamp; > - u8 bd_partno; > dev_t bd_dev; > atomic_t bd_openers; > spinlock_t bd_size_lock; /* for bd_inode->i_size > updates */ > @@ -60,13 +59,14 @@ struct block_device { > const struct blk_holder_ops *bd_holder_ops; > struct mutex bd_holder_lock; > int bd_holders; > + u8 bd_partno; 'bd_partno' needs to be kept in the 1st cacheline, so not good to move it. Thanks, Ming