Received: by 2002:ab2:6203:0:b0:1f5:f2ab:c469 with SMTP id o3csp2153996lqt; Mon, 22 Apr 2024 03:06:35 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXrdQn4Ho4t3AyoT6rRfBqClfRCDHR/hNTi8H+unkABjlQgfOL+U2T7WPROTxG/trL0MfHHp6UC3ZnWsKmuTxE5aOlVBNU+G+xbHfWwWw== X-Google-Smtp-Source: AGHT+IEWbbRldTzAf0e3kclbsVTjws83VECW1jH/6NbyckQRsxqoJqMhWHObalqUBwZsxtUpHLdU X-Received: by 2002:a05:6a21:33a9:b0:1a9:4570:2d3b with SMTP id yy41-20020a056a2133a900b001a945702d3bmr9505651pzb.7.1713780394887; Mon, 22 Apr 2024 03:06:34 -0700 (PDT) Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id w35-20020a634763000000b005dc86bae4b8si7362277pgk.408.2024.04.22.03.06.34 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Apr 2024 03:06:34 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-153147-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@vivo.com header.s=selector2 header.b=f6cJkofO; arc=fail (signature failed); spf=pass (google.com: domain of linux-kernel+bounces-153147-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-153147-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=vivo.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 7FECE283675 for ; Mon, 22 Apr 2024 10:06:34 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8765A13C3CA; Mon, 22 Apr 2024 10:06:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=vivo.com header.i=@vivo.com header.b="f6cJkofO" Received: from APC01-PSA-obe.outbound.protection.outlook.com (mail-psaapc01on2082.outbound.protection.outlook.com [40.107.255.82]) (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 F2E7C13C3F4 for ; Mon, 22 Apr 2024 10:06:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.255.82 ARC-Seal:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713780388; cv=fail; b=u6Y+MgrR5rO0TQ9XCde3X3LMU5XLR307Arzl3OmjyD2gN6LBPmSW/1orgjysAUkfij9jQc3uGsnkILKsjLa7kSnHn3/Tn+royv30V4F4NkLLRSnnUmPmEQ8oGIDazrzR4t80XqlfsytFEjUQ7j1E+fUtxE/wcYcoEYDT6qILE3I= ARC-Message-Signature:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713780388; c=relaxed/simple; bh=zh8sgmiVYwsPHPzQ5BzKy0mT2GO6/gUNpR7OrkEKXmc=; h=From:To:Cc:Subject:Date:Message-Id:Content-Type:MIME-Version; b=bEx6yOAA9aaAI0e4B03NLO6lCthP9Y1dv83JGXJDfAPuaS0uyFP/6PRA7p291ahp9w/wuwWrFDJ8NvJzVuMu98264f7nvBFDaSFY8q6w0lhesP2bIYnpCOqfbVsO9nyIq5yEhBEr3eOKuDWuVkFej2jggxwFbftxj07nG0M2EZs= ARC-Authentication-Results:i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=vivo.com; spf=pass smtp.mailfrom=vivo.com; dkim=pass (2048-bit key) header.d=vivo.com header.i=@vivo.com header.b=f6cJkofO; arc=fail smtp.client-ip=40.107.255.82 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=vivo.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=vivo.com ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=RjaQyNAMu7YoIl0iM4/mXCfpz/3HIJOfBdyueqXH1qmBFgnywoQvIffxjqlWYrJJ3UjuFbm/u7ZAXtgeiLh5pcAkQzpDyu5LoG87lfNfJzJwLJNQ3wf/zvjHOxXgNKy+e3AiQlN5TserxDoELrDA0lbkKnRQBkBC6ESPJv4pjmJKtmzUEwZflNKGl9NF//HZ6Gz5FgpMUvdM6ba1xYqU7U/fTy7hUWEQrnjkWwgVJA1DFakh4lLcA3TEd/gfOb/9ox2VhWO7AFPn1dy9ib6fwBFQUhs2CqfiaEvZjm3/F6R/1K+T5jUX71NDSUdOJ+Y+rMnozbY65BXsn+AeP5n5yw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=ihY3yBrqrqDnBnJCg28J+tIaZKQhbxqHnzslFW/KQOI=; b=mt4J+FklPgf3LqewaOD8BqktbwMPsR2N4P47mm7q3Od3au5QZkpw2/u5fGSX9pIYL8htypMHkp5ZGYrf8uviX5xWTOo1YDrZjWSytkGBZP9202D66a9qlvCr54x25DBwSc+DNjGkcsRS3e0JJ95nY+UBpYqCfFhqKnfJNQcc0sT/JKbi+2DWDBIJ6eo0gEan+NyXfxPWUu0cfgulZlkpLIm4fZv4spHNoVuqV0z1nWxMaoAq0lTobKeDdZAL1OXD3xaIZEHWvoAbH1ndRN9o5CuJcaCRSP3+Af+EJ9D7poFs+JTT9JNlyw0VmQdzPe3MkxgGxueAGre5jb8kF1qEsw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=vivo.com; dmarc=pass action=none header.from=vivo.com; dkim=pass header.d=vivo.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=vivo.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=ihY3yBrqrqDnBnJCg28J+tIaZKQhbxqHnzslFW/KQOI=; b=f6cJkofOzw5dMaqV81XHmuaefyxrcvJM37LMUB7HK1mFwaD+GoMpFGZErWhrSubJPUn7CrzPXLqlWKZy77dkuPX5vqs6m7I7RSBa++DubS/fCc3FYVVtkuR0R8zGF16X705+ONAZjEOqRJh3mJpB6cZl5fTYsVfT8UU7LcjtVC2pMsFxOCFmOPKUAgJ1sH+kUsmwDfqoDPXVk2MThZqyalhTFm+dMy43aKNfz+xR/HC4eqa36SfILz0HNCm46U81nVbDwmAd+pCWYVZLHmVv6wlv16BcB7VHM5ha0Wtr/D+0ptpHDrzqkcoTqmTSFofHHpuLT5oA4gxqgsj1G60tBg== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=vivo.com; Received: from TYSPR06MB6411.apcprd06.prod.outlook.com (2603:1096:400:42a::11) by SI2PR06MB5116.apcprd06.prod.outlook.com (2603:1096:4:1ab::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7472.44; Mon, 22 Apr 2024 10:06:19 +0000 Received: from TYSPR06MB6411.apcprd06.prod.outlook.com ([fe80::af33:ff9c:cf31:45e9]) by TYSPR06MB6411.apcprd06.prod.outlook.com ([fe80::af33:ff9c:cf31:45e9%3]) with mapi id 15.20.7472.044; Mon, 22 Apr 2024 10:06:19 +0000 From: Yang Yang To: Alasdair Kergon , Mike Snitzer , Mikulas Patocka , dm-devel@lists.linux.dev, linux-kernel@vger.kernel.org Cc: Yang Yang Subject: [RFC PATCH] dm: Avoid sending redundant empty flush bios to the same block device Date: Mon, 22 Apr 2024 18:05:40 +0800 Message-Id: <20240422100540.2213-1-yang.yang@vivo.com> X-Mailer: git-send-email 2.34.1 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: SI2PR01CA0046.apcprd01.prod.exchangelabs.com (2603:1096:4:193::18) To TYSPR06MB6411.apcprd06.prod.outlook.com (2603:1096:400:42a::11) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: TYSPR06MB6411:EE_|SI2PR06MB5116:EE_ X-MS-Office365-Filtering-Correlation-Id: ad5b6b13-9915-4321-e6aa-08dc62b3daf1 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?v9OpQw4Nu1jjrsAa7K+EB1CLU5519GwlsyXEfSJpdJy3BQ0FWgCe4NImXwgZ?= =?us-ascii?Q?R0wRTQkU7cY0pGVnsc3SzpidNJZz/SAJWKSy75w1FL/O5lEZyGB+6DPQHHZ/?= =?us-ascii?Q?0+R8Y9+Av6rugebokzNR2xGLtbv8L+Gga2RZ8MuRMnUwloUXMhj53lFzFlrG?= =?us-ascii?Q?f1Ql3ltLp4gl+dda1timFABmBGUzoMhR83Cfg1zM56Z0JF7U/AJxSUhHn5iN?= =?us-ascii?Q?FibgjoXJpGi53E2am/E+rYCzqq80/VeF11PgyWhCGAQrT4QftGzWK+XK/V8s?= =?us-ascii?Q?RSp22vCgeVzQjnqpSBc27l870c8Paw9HPj6TJiPCF1DHkLnKj3V3DVBmNyKa?= =?us-ascii?Q?Y65QHntJ8kSLmZ90sg/P9ExeUWSpmI6pCKjet+H/ny/hxi8CBq9bptzuruhu?= =?us-asc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ATzpEm1UcFqgZBDcT+Y?= =?us-ascii?Q?RfgV9jjVOz5osEXTV2Xl4CEVlmJERVwPOYRUQ3CQbxgZ3g=3D=3D?= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:TYSPR06MB6411.apcprd06.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230031)(376005)(52116005)(1800799015)(366007)(38350700005);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?jtCXRhi1svph1waMulrcjRAdVoqDiyFA27Gqmgi4DStVDRt9Olw3VPqrtDnl?= =?us-ascii?Q?fAfqsohPEDENlFF2WZj201lkK0vhlbGiWtUi/2feHYabDa700L2FfeuZLR7W?= =?us-ascii?Q?+Fl/i51MNss2tEQhRYZYtFgfa5hJeDvUIPt85UQ8zHSUQpf6vz3Xr9K9AfzF?= =?us-ascii?Q?dOk3lsXptlrGZ/5a6DFnpxxQSUjJDgYAzgfQW7BN7qwxUTyNm7tsprSQ5Zng?= =?us-ascii?Q?gkC682PzTKgb3BhwbReEvylBoeu0qCcSjgmJ1G8OABI6XJ+JfymHOqF0SJlb?= =?us-ascii?Q?1FlfnhsgT28OOxzpS2ibjUOUcsDXZY7orXeckyHuBNZ2OTlfBY/1RIae4yYt?= =?us-ascii?Q?bpFx6L8ghLo4EK4mFFJN5IDSlMJx0kpS/etgvoLRr8SPUgTvVg96rM28L9eY?= =?us-ascii?Q?j8mAgzPJhnD2A9kw9PTet8ds3tqurcFZ6l2WjeJEu+x9wxMfI60uCfW46g5D?= =?us-ascii?Q?IA3HEl7t3CXuHiSWxZM7tOkRkvgYc6auaqXyuWaPycYtQg4i2MCpVVriTQfa?= =?us-ascii?Q?xeMf33TbbmJKgGvJfodD+Ju0T9xvz9KbLUR+URuXKORVpz8jGthZtjW4mvLj?= =?us-ascii?Q?zn5pUN9D1FbzoonuN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?us-ascii?Q?ePdzq2LAFr9UJS7LY7tfFSKxKvCmPFJUOV1ajl+xNpEOW8LS1u+XGLZNZ851?= =?us-ascii?Q?kxTXIw1Tn8WyHYfTXwItt8OvOCB4I98LgfWy3oxJ?= X-OriginatorOrg: vivo.com X-MS-Exchange-CrossTenant-Network-Message-Id: ad5b6b13-9915-4321-e6aa-08dc62b3daf1 X-MS-Exchange-CrossTenant-AuthSource: TYSPR06MB6411.apcprd06.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Apr 2024 10:06:19.3425 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 923e42dc-48d5-4cbe-b582-1a797a6412ed X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: J4rCr9U+c9Y65GdfysmbIH5kEyp39oHTW+s1GG8ocWxAvLUTtf8FfrpUsJVoLUL7yp/2883GHZ6K1vIe40bmBg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SI2PR06MB5116 __send_empty_flush() sends empty flush bios to every target in the dm_table. However, if the num_targets exceeds the number of block devices in the dm_table's device list, it could lead to multiple invocations of __send_duplicate_bios() for the same block device. Typically, a single thread sending numerous empty flush bios to one block device is redundant, as these bios are likely to be merged by the flush state machine. In scenarios where num_targets significantly outweighs the number of block devices, such behavior may result in a noteworthy decrease in performance. This issue can be reproduced using this command line: for i in {0..1023}; do echo $((8000*$i)) 8000 linear /dev/sda2 $((16384*$i)) done | dmsetup create example With this fix, a random write with fsync workload executed with the following fio command: fio --group_reporting --name=benchmark --filename=/dev/mapper/example \ --ioengine=sync --invalidate=1 --numjobs=16 --rw=randwrite \ --blocksize=4k --size=2G --time_based --runtime=30 --fdatasync=1 results in an increase from 857 KB/s to 30.8 MB/s of the write throughput (3580% increase). Signed-off-by: Yang Yang --- drivers/md/dm-core.h | 1 + drivers/md/dm-table.c | 7 +++++ drivers/md/dm.c | 59 +++++++++++++++++++++++++++++++++++ include/linux/device-mapper.h | 6 ++++ 4 files changed, 73 insertions(+) diff --git a/drivers/md/dm-core.h b/drivers/md/dm-core.h index e6757a30dcca..7e3f2168289f 100644 --- a/drivers/md/dm-core.h +++ b/drivers/md/dm-core.h @@ -217,6 +217,7 @@ struct dm_table { /* a list of devices used by this table */ struct list_head devices; struct rw_semaphore devices_lock; + unsigned short num_devices; /* events get handed up using this callback */ void (*event_fn)(void *data); diff --git a/drivers/md/dm-table.c b/drivers/md/dm-table.c index 41f1d731ae5a..ddc60e498afb 100644 --- a/drivers/md/dm-table.c +++ b/drivers/md/dm-table.c @@ -2133,6 +2133,8 @@ void dm_table_postsuspend_targets(struct dm_table *t) int dm_table_resume_targets(struct dm_table *t) { + struct list_head *devices = dm_table_get_devices(t); + struct dm_dev_internal *dd; unsigned int i; int r = 0; @@ -2159,6 +2161,11 @@ int dm_table_resume_targets(struct dm_table *t) ti->type->resume(ti); } + t->num_devices = 0; + + list_for_each_entry(dd, devices, list) + dd->dm_dev->index = ++(t->num_devices); + return 0; } diff --git a/drivers/md/dm.c b/drivers/md/dm.c index 56aa2a8b9d71..7297235291f6 100644 --- a/drivers/md/dm.c +++ b/drivers/md/dm.c @@ -48,6 +48,8 @@ */ #define REQ_DM_POLL_LIST REQ_DRV +#define DM_MAX_TABLE_DEVICES 1024 + static const char *_name = DM_NAME; static unsigned int major; @@ -1543,10 +1545,38 @@ static unsigned int __send_duplicate_bios(struct clone_info *ci, struct dm_targe return ret; } +static inline bool has_redundant_flush(struct dm_table *t, + unsigned long **bitmap) +{ + if (t->num_devices < t->num_targets) { + /* Add a limit here to prevent excessive memory usage for bitmaps */ + if (t->num_devices >= DM_MAX_TABLE_DEVICES) + return false; + + /* dm_dev's index starts from 1, so need plus 1 here */ + *bitmap = bitmap_zalloc(t->num_devices + 1, GFP_KERNEL); + if (*bitmap) + return true; + } + + return false; +} + +static int dm_get_dev_index(struct dm_target *ti, struct dm_dev *dev, + sector_t start, sector_t len, void *data) +{ + unsigned short *index = data; + *index = dev->index; + return 0; +} + static void __send_empty_flush(struct clone_info *ci) { struct dm_table *t = ci->map; struct bio flush_bio; + unsigned long *handled_map; + unsigned int nr_handled = 0; + bool check = has_redundant_flush(t, &handled_map); /* * Use an on-stack bio for this, it's safe since we don't @@ -1562,17 +1592,46 @@ static void __send_empty_flush(struct clone_info *ci) for (unsigned int i = 0; i < t->num_targets; i++) { unsigned int bios; + unsigned short index = 0; struct dm_target *ti = dm_table_get_target(t, i); if (unlikely(ti->num_flush_bios == 0)) continue; + /* + * If the num_targets is greater than the number of block devices + * in the dm_table's devices list, __send_empty_flush() might + * invoke __send_duplicate_bios() multiple times for the same + * block device. This could lead to a substantial decrease in + * performance when num_targets significantly exceeds the number + * of block devices. + * Ensure that __send_duplicate_bios() is only called once for + * each block device. + */ + if (check) { + if (nr_handled == t->num_devices) + break; + + if (ti->type->iterate_devices) + ti->type->iterate_devices(ti, dm_get_dev_index, &index); + + if (index > 0) { + if (__test_and_set_bit(index, handled_map)) + continue; + else + nr_handled++; + } + } + atomic_add(ti->num_flush_bios, &ci->io->io_count); bios = __send_duplicate_bios(ci, ti, ti->num_flush_bios, NULL, GFP_NOWAIT); atomic_sub(ti->num_flush_bios - bios, &ci->io->io_count); } + if (check) + bitmap_free(handled_map); + /* * alloc_io() takes one extra reference for submission, so the * reference won't reach 0 without the following subtraction diff --git a/include/linux/device-mapper.h b/include/linux/device-mapper.h index 82b2195efaca..4a54b4f0a609 100644 --- a/include/linux/device-mapper.h +++ b/include/linux/device-mapper.h @@ -169,6 +169,12 @@ struct dm_dev { struct dax_device *dax_dev; blk_mode_t mode; char name[16]; + + /* + * sequential number for each dm_dev in dm_table's devices list, + * start from 1 + */ + unsigned short index; }; /* -- 2.34.1