From nobody Tue Feb 10 22:16:02 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 27CF0157A58 for ; Thu, 16 May 2024 20:50:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715892604; cv=none; b=qRxoFfoZ8UWUJRPR/B+U3lfWsVWkNHLvxLxg4DSherEY81EgwGYxPFnsWLRFnPoZD5+gBxQm/x6eXat/Rh7cauS4xip9Q5Qdz0+0jctFsS98U9xKo9u5Os6sgTPJToOemAF8p5tc+wKC0bKOjm3tq77pRlEGDY0bKHanRhLXiCo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715892604; c=relaxed/simple; bh=O2ewFUdwq+gbmSDgv62d/vyBToUFpAKRIEjy2L8N0fI=; h=Date:From:To:cc:Subject:In-Reply-To:Message-ID:References: MIME-Version:Content-Type; b=AP0Ib/d8HC8FBioBgDHSA/URr7RDfTPDo7k++Xy//vuwz5ae3socp1bstKZk8Y7WYOdxDg3sY0i6YgbWHQMzIaxjifkc1pZfNRFcs1Wws/WZbr4YC1RWfDgMa9/nlvidNTmHo0KvelstBsIVwgDJNJOku8Kg42fcy4satJvyxZc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=P4igE5Zj; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="P4igE5Zj" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1715892599; 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: in-reply-to:in-reply-to:references:references; bh=O+fAnIIebPuxwkKSzMwnCOwSYdgO9pXZPB02aKazGyo=; b=P4igE5ZjdooP9qG7v2qNLmgLIHfSZ7tsIZAZl+2elxKMYYVqiVkybK73cC2lnUTBXlq+f5 HazFjuw/vqyX+Dc9eWj+g/zd8uDrlRpMvvaF0lGuOWgQ26dOot/eXGW6sEww/zuY68MY6V iri0HhOj3Ip/j+zgX2MT5mfxHz6KhI8= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-330-a75kPsHoM7esCkPxXErHZA-1; Thu, 16 May 2024 16:49:56 -0400 X-MC-Unique: a75kPsHoM7esCkPxXErHZA-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (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 E17043C025C9; Thu, 16 May 2024 20:49:55 +0000 (UTC) Received: from file1-rdu.file-001.prod.rdu2.dc.redhat.com (unknown [10.11.5.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id C7B4951BF; Thu, 16 May 2024 20:49:55 +0000 (UTC) Received: by file1-rdu.file-001.prod.rdu2.dc.redhat.com (Postfix, from userid 12668) id B336F30C1C33; Thu, 16 May 2024 20:49:55 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by file1-rdu.file-001.prod.rdu2.dc.redhat.com (Postfix) with ESMTP id AFE453FB4F; Thu, 16 May 2024 22:49:55 +0200 (CEST) Date: Thu, 16 May 2024 22:49:55 +0200 (CEST) From: Mikulas Patocka To: Benjamin Marzinski cc: Yang Yang , Alasdair Kergon , Mike Snitzer , dm-devel@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH] dm: optimize flushes In-Reply-To: <60bd4b9-8edd-7e22-ce8b-e5d0e43da195@redhat.com> Message-ID: <90f4beb-2e15-3f9-4bc2-0d13872e8ea@redhat.com> References: <20240514090445.2847-1-yang.yang@vivo.com> <20240514090445.2847-4-yang.yang@vivo.com> <60bd4b9-8edd-7e22-ce8b-e5d0e43da195@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.5 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Device mapper sends flush bios to all the targets and the targets send it to the underlying device. That may be inefficient, for example if a table contains 10 linear targets pointing to the same physical device, then device mapper would send 10 flush bios to that device - despite the fact that only one bio would be sufficient. This commit optimizes the flush behavior. It introduces a per-target variable flush_pass_around - it is set when the target supports flush optimization - currently, the dm-linear and dm-stripe targets support it. When all the targets in a table have flush_pass_around, flush_pass_around on the table is set. __send_empty_flush tests if the table has flush_pass_around - and if it has, no flush bios are sent to the targets and the list dm_table->devices is iterated and the flush bios are sent to each member of the list. Signed-off-by: Mikulas Patocka Reported-by: Yang Yang --- drivers/md/dm-core.h | 4 ++- drivers/md/dm-linear.c | 1=20 drivers/md/dm-stripe.c | 1=20 drivers/md/dm-table.c | 4 +++ drivers/md/dm.c | 47 +++++++++++++++++++++++++++++--------= ----- include/linux/device-mapper.h | 5 ++++ 6 files changed, 47 insertions(+), 15 deletions(-) Index: linux-2.6/drivers/md/dm-core.h =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- linux-2.6.orig/drivers/md/dm-core.h 2024-05-15 16:56:49.000000000 +0200 +++ linux-2.6/drivers/md/dm-core.h 2024-05-15 16:56:49.000000000 +0200 @@ -206,7 +206,9 @@ struct dm_table { =20 bool integrity_supported:1; bool singleton:1; - unsigned integrity_added:1; + bool integrity_added:1; + /* set if all the targets in the table have "flush_pass_around" set */ + bool flush_pass_around:1; =20 /* * Indicates the rw permissions for the new logical device. This Index: linux-2.6/drivers/md/dm-linear.c =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- linux-2.6.orig/drivers/md/dm-linear.c 2024-05-15 16:56:49.000000000 +02= 00 +++ linux-2.6/drivers/md/dm-linear.c 2024-05-15 16:56:49.000000000 +0200 @@ -62,6 +62,7 @@ static int linear_ctr(struct dm_target * ti->num_discard_bios =3D 1; ti->num_secure_erase_bios =3D 1; ti->num_write_zeroes_bios =3D 1; + ti->flush_pass_around =3D true; ti->private =3D lc; return 0; =20 Index: linux-2.6/drivers/md/dm-stripe.c =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- linux-2.6.orig/drivers/md/dm-stripe.c 2024-05-15 16:56:49.000000000 +02= 00 +++ linux-2.6/drivers/md/dm-stripe.c 2024-05-15 16:56:49.000000000 +0200 @@ -157,6 +157,7 @@ static int stripe_ctr(struct dm_target * ti->num_discard_bios =3D stripes; ti->num_secure_erase_bios =3D stripes; ti->num_write_zeroes_bios =3D stripes; + ti->flush_pass_around =3D true; =20 sc->chunk_size =3D chunk_size; if (chunk_size & (chunk_size - 1)) Index: linux-2.6/drivers/md/dm-table.c =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- linux-2.6.orig/drivers/md/dm-table.c 2024-05-15 16:56:49.000000000 +0200 +++ linux-2.6/drivers/md/dm-table.c 2024-05-15 16:56:49.000000000 +0200 @@ -160,6 +160,7 @@ int dm_table_create(struct dm_table **re t->type =3D DM_TYPE_NONE; t->mode =3D mode; t->md =3D md; + t->flush_pass_around =3D 1; *result =3D t; return 0; } @@ -738,6 +739,9 @@ int dm_table_add_target(struct dm_table if (ti->limit_swap_bios && !static_key_enabled(&swap_bios_enabled.key)) static_branch_enable(&swap_bios_enabled); =20 + if (!ti->flush_pass_around) + t->flush_pass_around =3D false; + return 0; =20 bad: Index: linux-2.6/include/linux/device-mapper.h =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- linux-2.6.orig/include/linux/device-mapper.h 2024-05-15 16:56:49.000000= 000 +0200 +++ linux-2.6/include/linux/device-mapper.h 2024-05-15 16:56:49.000000000 += 0200 @@ -397,6 +397,11 @@ struct dm_target { * bio_set_dev(). NOTE: ideally a target should _not_ need this. */ bool needs_bio_set_dev:1; + + /* + * Set if the target supports flush optimization + */ + bool flush_pass_around:1; }; =20 void *dm_per_bio_data(struct bio *bio, size_t data_size); Index: linux-2.6/drivers/md/dm.c =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- linux-2.6.orig/drivers/md/dm.c 2024-05-15 16:56:49.000000000 +0200 +++ linux-2.6/drivers/md/dm.c 2024-05-16 20:06:32.000000000 +0200 @@ -645,7 +645,7 @@ static struct bio *alloc_tio(struct clon =20 /* Set default bdev, but target must bio_set_dev() before issuing IO */ clone->bi_bdev =3D md->disk->part0; - if (unlikely(ti->needs_bio_set_dev)) + if (likely(ti !=3D NULL) && unlikely(ti->needs_bio_set_dev)) bio_set_dev(clone, md->disk->part0); =20 if (len) { @@ -1107,7 +1107,7 @@ static void clone_endio(struct bio *bio) blk_status_t error =3D bio->bi_status; struct dm_target_io *tio =3D clone_to_tio(bio); struct dm_target *ti =3D tio->ti; - dm_endio_fn endio =3D ti->type->end_io; + dm_endio_fn endio =3D likely(ti !=3D NULL) ? ti->type->end_io : NULL; struct dm_io *io =3D tio->io; struct mapped_device *md =3D io->md; =20 @@ -1154,7 +1154,7 @@ static void clone_endio(struct bio *bio) } =20 if (static_branch_unlikely(&swap_bios_enabled) && - unlikely(swap_bios_limit(ti, bio))) + likely(ti !=3D NULL) && unlikely(swap_bios_limit(ti, bio))) up(&md->swap_bios_semaphore); =20 free_tio(bio); @@ -1566,17 +1566,36 @@ static void __send_empty_flush(struct cl ci->sector_count =3D 0; ci->io->tio.clone.bi_iter.bi_size =3D 0; =20 - for (unsigned int i =3D 0; i < t->num_targets; i++) { - unsigned int bios; - struct dm_target *ti =3D dm_table_get_target(t, i); - - if (unlikely(ti->num_flush_bios =3D=3D 0)) - continue; - - atomic_add(ti->num_flush_bios, &ci->io->io_count); - bios =3D __send_duplicate_bios(ci, ti, ti->num_flush_bios, - NULL, GFP_NOWAIT); - atomic_sub(ti->num_flush_bios - bios, &ci->io->io_count); + if (!t->flush_pass_around) { + for (unsigned int i =3D 0; i < t->num_targets; i++) { + unsigned int bios; + struct dm_target *ti =3D dm_table_get_target(t, i); + + if (unlikely(ti->num_flush_bios =3D=3D 0)) + continue; + + atomic_add(ti->num_flush_bios, &ci->io->io_count); + bios =3D __send_duplicate_bios(ci, ti, ti->num_flush_bios, + NULL, GFP_NOWAIT); + atomic_sub(ti->num_flush_bios - bios, &ci->io->io_count); + } + } else { + /* + * Note that there's no need to grab t->devices_lock here + * because the targets that support flush pass-around don't + * modify the list of devices. + */ + struct list_head *devices =3D dm_table_get_devices(t); + unsigned int len =3D 0; + struct dm_dev_internal *dd; + list_for_each_entry(dd, devices, list) { + struct bio *clone; + clone =3D alloc_tio(ci, NULL, 0, &len, GFP_NOIO); + atomic_add(1, &ci->io->io_count); + bio_set_dev(clone, dd->dm_dev->bdev); + clone->bi_end_io =3D clone_endio; + dm_submit_bio_remap(clone, NULL); + } } =20 /*