From patchwork Mon Oct 10 12:35:40 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mikulas Patocka X-Patchwork-Id: 13002566 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 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.lore.kernel.org (Postfix) with ESMTPS id C8BAEC433F5 for ; Mon, 10 Oct 2022 12:35:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1665405346; h=from:from:sender:sender: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:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=x2NG3BUzKNN5k/Uu7S61iUU+Z/0sfHiDa5KFY7TNEfk=; b=RKH6dC5c+CG8SDWC1yQZ7OkJNsTx8QgGInBpIHWCYY88uGoz3MzQRqwz8UEiiSHtVXUVy8 znhX4IQDQiMVpW3mqGGf9y5wzeyvYpB89+QcJnyf79htR2KJVVNwosR8ObBri9GckHF0Rx EEJ83suLnArP0Z4KZrmTK43qTn042Xo= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-352-e_qpGuoWPGqgntZOIYmMuQ-1; Mon, 10 Oct 2022 08:35:43 -0400 X-MC-Unique: e_qpGuoWPGqgntZOIYmMuQ-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id CC27E1C07591; Mon, 10 Oct 2022 12:35:41 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (unknown [10.30.29.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id AA3FC2157F41; Mon, 10 Oct 2022 12:35:41 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (localhost [IPv6:::1]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 8722F19465A0; Mon, 10 Oct 2022 12:35:41 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 559951946597 for ; Mon, 10 Oct 2022 12:35:40 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id 372192083A68; Mon, 10 Oct 2022 12:35:40 +0000 (UTC) Received: from file01.intranet.prod.int.rdu2.redhat.com (file01.intranet.prod.int.rdu2.redhat.com [10.11.5.7]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 248F32083A6D; Mon, 10 Oct 2022 12:35:40 +0000 (UTC) Received: from file01.intranet.prod.int.rdu2.redhat.com (localhost [127.0.0.1]) by file01.intranet.prod.int.rdu2.redhat.com (8.14.4/8.14.4) with ESMTP id 29ACZeqI026614; Mon, 10 Oct 2022 08:35:40 -0400 Received: from localhost (mpatocka@localhost) by file01.intranet.prod.int.rdu2.redhat.com (8.14.4/8.14.4/Submit) with ESMTP id 29ACZek8026608; Mon, 10 Oct 2022 08:35:40 -0400 X-Authentication-Warning: file01.intranet.prod.int.rdu2.redhat.com: mpatocka owned process doing -bs Date: Mon, 10 Oct 2022 08:35:40 -0400 (EDT) From: Mikulas Patocka X-X-Sender: mpatocka@file01.intranet.prod.int.rdu2.redhat.com To: Joe Thornber , Mike Snitzer , Heinz Mauelshagen In-Reply-To: Message-ID: References: User-Agent: Alpine 2.02 (LRH 1266 2009-07-14) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 Subject: [dm-devel] [PATCH 1/4] dm-bufio: make it possible to query multiple blocks with just one lock X-BeenThere: dm-devel@redhat.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: device-mapper development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: dm-devel@redhat.com Errors-To: dm-devel-bounces@redhat.com Sender: "dm-devel" X-Scanned-By: MIMEDefang 3.1 on 10.11.54.6 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com In order to reduce locking overhead, we introduce a new API: dm_bufio_lock_read, dm_bufio_unlock_read and dm_bufio_get_unlocked. The user calls dm_bufio_lock_read, then he might call dm_bufio_get_unlocked several times and finally calls dm_bufio_unlock_read. This is intended to simplify btree walks in dm-thin. The follow-up patch will change c->lock from mutex to rw-semaphore, so that there could be multiple processes walking the btree concurently. Signed-off-by: Mikulas Patocka --- drivers/md/dm-bufio.c | 45 ++++++++++++++++++++++++++++++++++++++------- include/linux/dm-bufio.h | 15 +++++++++++++++ 2 files changed, 53 insertions(+), 7 deletions(-) -- dm-devel mailing list dm-devel@redhat.com https://listman.redhat.com/mailman/listinfo/dm-devel Index: linux-2.6/drivers/md/dm-bufio.c =================================================================== --- linux-2.6.orig/drivers/md/dm-bufio.c 2022-10-10 11:08:10.000000000 +0200 +++ linux-2.6/drivers/md/dm-bufio.c 2022-10-10 11:13:10.000000000 +0200 @@ -145,7 +145,7 @@ struct dm_buffer { unsigned char list_mode; /* LIST_* */ blk_status_t read_error; blk_status_t write_error; - unsigned accessed; + volatile unsigned accessed; unsigned hold_count; unsigned long state; unsigned long last_accessed; @@ -193,6 +193,18 @@ static void dm_bufio_unlock(struct dm_bu mutex_unlock(&c->lock); } +void dm_bufio_lock_read(struct dm_bufio_client *c) +{ + mutex_lock(&c->lock); +} +EXPORT_SYMBOL_GPL(dm_bufio_lock_read); + +void dm_bufio_unlock_read(struct dm_bufio_client *c) +{ + mutex_unlock(&c->lock); +} +EXPORT_SYMBOL_GPL(dm_bufio_unlock_read); + /*----------------------------------------------------------------*/ /* @@ -870,7 +882,8 @@ enum new_flag { NF_FRESH = 0, NF_READ = 1, NF_GET = 2, - NF_PREFETCH = 3 + NF_GET_UNLOCKED = 3, + NF_PREFETCH = 4 }; /* @@ -1013,7 +1026,7 @@ static struct dm_buffer *__bufio_new(str if (b) goto found_buffer; - if (nf == NF_GET) + if (nf == NF_GET || nf == NF_GET_UNLOCKED) return NULL; new_b = __alloc_buffer_wait(c, nf); @@ -1058,12 +1071,17 @@ found_buffer: * If the user called both dm_bufio_prefetch and dm_bufio_get on * the same buffer, it would deadlock if we waited. */ - if (nf == NF_GET && unlikely(test_bit(B_READING, &b->state))) + if ((nf == NF_GET || nf == NF_GET_UNLOCKED) && unlikely(test_bit(B_READING, &b->state))) return NULL; - b->hold_count++; - __relink_lru(b, test_bit(B_DIRTY, &b->state) || - test_bit(B_WRITING, &b->state)); + if (nf == NF_GET_UNLOCKED) { + if (!b->accessed) + b->accessed = 1; + } else { + b->hold_count++; + __relink_lru(b, test_bit(B_DIRTY, &b->state) || + test_bit(B_WRITING, &b->state)); + } return b; } @@ -1154,6 +1172,19 @@ void *dm_bufio_new(struct dm_bufio_clien } EXPORT_SYMBOL_GPL(dm_bufio_new); +void *dm_bufio_get_unlocked(struct dm_bufio_client *c, sector_t block, + struct dm_buffer **bp) +{ + int need_submit; + struct dm_buffer *b = __bufio_new(c, block, NF_GET_UNLOCKED, &need_submit, NULL); + if (b) { + *bp = b; + return b->data; + } + return NULL; +} +EXPORT_SYMBOL_GPL(dm_bufio_get_unlocked); + void dm_bufio_prefetch(struct dm_bufio_client *c, sector_t block, unsigned n_blocks) { Index: linux-2.6/include/linux/dm-bufio.h =================================================================== --- linux-2.6.orig/include/linux/dm-bufio.h 2022-10-10 11:13:10.000000000 +0200 +++ linux-2.6/include/linux/dm-bufio.h 2022-10-10 11:13:25.000000000 +0200 @@ -38,6 +38,13 @@ dm_bufio_client_create(struct block_devi void dm_bufio_client_destroy(struct dm_bufio_client *c); /* + * Lock and unlock the bufio client - this is needed if we want to call + * dm_bufio_get_unlocked. + */ +void dm_bufio_lock_read(struct dm_bufio_client *c); +void dm_bufio_unlock_read(struct dm_bufio_client *c); + +/* * Set the sector range. * When this function is called, there must be no I/O in progress on the bufio * client. @@ -76,6 +83,14 @@ void *dm_bufio_new(struct dm_bufio_clien struct dm_buffer **bp); /* + * Like dm_bufio_get, but assume that the client is already locked with + * dm_bufio_lock_read/dm_bufio_unlock_read. dm_bufio_release should not be + * called; the caller should call dm_bufio_unlock_read to release the buffer. + */ +void *dm_bufio_get_unlocked(struct dm_bufio_client *c, sector_t block, + struct dm_buffer **bp); + +/* * Prefetch the specified blocks to the cache. * The function starts to read the blocks and returns without waiting for * I/O to finish. From patchwork Mon Oct 10 12:36:14 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mikulas Patocka X-Patchwork-Id: 13002567 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 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.lore.kernel.org (Postfix) with ESMTPS id AD0F2C433FE for ; Mon, 10 Oct 2022 12:36:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1665405380; h=from:from:sender:sender: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:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=/NXZ1pc3LxIPPJvYyw1xCZkDABNaoXQp0BePED/lKM0=; b=YwFufx6JGh+VOKbgJGxTUWck64bxOzpx7eQENJ4H0O8XUkrp5/Fnz11rTjEw4ZztYw4F3H XWrfPr79eiPbGAJQB99LNx8h6uK03KEwi2CNjkG4VXJ7E5d+PiyMGZF6S3HJhrql+LQ9Da opcb5gB8DMn5b2QSlLbczcqqPRqWKiU= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-526-bZAEq-1wOrqkwqrhRIW3VQ-1; Mon, 10 Oct 2022 08:36:17 -0400 X-MC-Unique: bZAEq-1wOrqkwqrhRIW3VQ-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 27D873C10EC5; Mon, 10 Oct 2022 12:36:16 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (unknown [10.30.29.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id 16CBC145BF00; Mon, 10 Oct 2022 12:36:16 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (localhost [IPv6:::1]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id CD64B19465A0; Mon, 10 Oct 2022 12:36:15 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 9A0D11946597 for ; Mon, 10 Oct 2022 12:36:14 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id 7F077C2129; Mon, 10 Oct 2022 12:36:14 +0000 (UTC) Received: from file01.intranet.prod.int.rdu2.redhat.com (file01.intranet.prod.int.rdu2.redhat.com [10.11.5.7]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 6CC55A9F42; Mon, 10 Oct 2022 12:36:14 +0000 (UTC) Received: from file01.intranet.prod.int.rdu2.redhat.com (localhost [127.0.0.1]) by file01.intranet.prod.int.rdu2.redhat.com (8.14.4/8.14.4) with ESMTP id 29ACaElD026703; Mon, 10 Oct 2022 08:36:14 -0400 Received: from localhost (mpatocka@localhost) by file01.intranet.prod.int.rdu2.redhat.com (8.14.4/8.14.4/Submit) with ESMTP id 29ACaEit026699; Mon, 10 Oct 2022 08:36:14 -0400 X-Authentication-Warning: file01.intranet.prod.int.rdu2.redhat.com: mpatocka owned process doing -bs Date: Mon, 10 Oct 2022 08:36:14 -0400 (EDT) From: Mikulas Patocka X-X-Sender: mpatocka@file01.intranet.prod.int.rdu2.redhat.com To: Joe Thornber , Mike Snitzer , Heinz Mauelshagen In-Reply-To: Message-ID: References: User-Agent: Alpine 2.02 (LRH 1266 2009-07-14) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.5 Subject: [dm-devel] [PATCH 2/4] dm-bufio: change mutex to rw_semaphore X-BeenThere: dm-devel@redhat.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: device-mapper development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: dm-devel@redhat.com Errors-To: dm-devel-bounces@redhat.com Sender: "dm-devel" X-Scanned-By: MIMEDefang 3.1 on 10.11.54.7 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Change mutex to rw_semaphore, so that there could be multiple processes inside dm_bufio_lock_read/dm_bufio_unlock_read concurrently. Signed-off-by: Mikulas Patocka --- drivers/md/dm-bufio.c | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) -- dm-devel mailing list dm-devel@redhat.com https://listman.redhat.com/mailman/listinfo/dm-devel Index: linux-2.6/drivers/md/dm-bufio.c =================================================================== --- linux-2.6.orig/drivers/md/dm-bufio.c 2022-10-10 11:13:57.000000000 +0200 +++ linux-2.6/drivers/md/dm-bufio.c 2022-10-10 11:13:57.000000000 +0200 @@ -81,7 +81,7 @@ * context. */ struct dm_bufio_client { - struct mutex lock; + struct rw_semaphore lock; spinlock_t spinlock; bool no_sleep; @@ -174,7 +174,7 @@ static void dm_bufio_lock(struct dm_bufi if (static_branch_unlikely(&no_sleep_enabled) && c->no_sleep) spin_lock_bh(&c->spinlock); else - mutex_lock_nested(&c->lock, dm_bufio_in_request()); + down_write_nested(&c->lock, dm_bufio_in_request()); } static int dm_bufio_trylock(struct dm_bufio_client *c) @@ -182,7 +182,7 @@ static int dm_bufio_trylock(struct dm_bu if (static_branch_unlikely(&no_sleep_enabled) && c->no_sleep) return spin_trylock_bh(&c->spinlock); else - return mutex_trylock(&c->lock); + return down_write_trylock(&c->lock); } static void dm_bufio_unlock(struct dm_bufio_client *c) @@ -190,18 +190,18 @@ static void dm_bufio_unlock(struct dm_bu if (static_branch_unlikely(&no_sleep_enabled) && c->no_sleep) spin_unlock_bh(&c->spinlock); else - mutex_unlock(&c->lock); + up_write(&c->lock); } void dm_bufio_lock_read(struct dm_bufio_client *c) { - mutex_lock(&c->lock); + down_read(&c->lock); } EXPORT_SYMBOL_GPL(dm_bufio_lock_read); void dm_bufio_unlock_read(struct dm_bufio_client *c) { - mutex_unlock(&c->lock); + up_read(&c->lock); } EXPORT_SYMBOL_GPL(dm_bufio_unlock_read); @@ -1808,7 +1808,7 @@ struct dm_bufio_client *dm_bufio_client_ c->n_buffers[i] = 0; } - mutex_init(&c->lock); + init_rwsem(&c->lock); spin_lock_init(&c->spinlock); INIT_LIST_HEAD(&c->reserved_buffers); c->need_reserved_buffers = reserved_buffers; @@ -1887,7 +1887,6 @@ bad: kmem_cache_destroy(c->slab_buffer); dm_io_client_destroy(c->dm_io); bad_dm_io: - mutex_destroy(&c->lock); kfree(c); bad_client: return ERR_PTR(r); @@ -1935,7 +1934,6 @@ void dm_bufio_client_destroy(struct dm_b kmem_cache_destroy(c->slab_cache); kmem_cache_destroy(c->slab_buffer); dm_io_client_destroy(c->dm_io); - mutex_destroy(&c->lock); if (c->no_sleep) static_branch_dec(&no_sleep_enabled); kfree(c); From patchwork Mon Oct 10 12:36:54 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mikulas Patocka X-Patchwork-Id: 13002568 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 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.lore.kernel.org (Postfix) with ESMTPS id A3FDAC433F5 for ; Mon, 10 Oct 2022 12:37:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1665405419; h=from:from:sender:sender: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:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=iXSquf4U08uclJUdYUNtmbMNRbXlNq7MhtivTTD4tYg=; b=hzXPX4x/QPpErD4Jw2lJYD68j4me/I92+jC66nifBysVQYrh8PMD1h+wQepHRsXaZVC6YE ChgtS89q6BQfGkacWI+ifE22aG6lJZ7XT9R+UANjZbh1YdpktefgcmTgUHv5BBgbrjLB2U 7u7Zfm8aOSqkXFxTUQOmtFZW4/hN840= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-298-NfufSVisP2yJG4EqoF4boA-1; Mon, 10 Oct 2022 08:36:58 -0400 X-MC-Unique: NfufSVisP2yJG4EqoF4boA-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 38874101A528; Mon, 10 Oct 2022 12:36:57 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (unknown [10.30.29.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2784D145D82C; Mon, 10 Oct 2022 12:36:57 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (localhost [IPv6:::1]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 091D719465A0; Mon, 10 Oct 2022 12:36:57 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 222111946597 for ; Mon, 10 Oct 2022 12:36:55 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id 143382157F44; Mon, 10 Oct 2022 12:36:55 +0000 (UTC) Received: from file01.intranet.prod.int.rdu2.redhat.com (file01.intranet.prod.int.rdu2.redhat.com [10.11.5.7]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 03A282157F30; Mon, 10 Oct 2022 12:36:54 +0000 (UTC) Received: from file01.intranet.prod.int.rdu2.redhat.com (localhost [127.0.0.1]) by file01.intranet.prod.int.rdu2.redhat.com (8.14.4/8.14.4) with ESMTP id 29ACasRC026719; Mon, 10 Oct 2022 08:36:54 -0400 Received: from localhost (mpatocka@localhost) by file01.intranet.prod.int.rdu2.redhat.com (8.14.4/8.14.4/Submit) with ESMTP id 29ACas5x026716; Mon, 10 Oct 2022 08:36:54 -0400 X-Authentication-Warning: file01.intranet.prod.int.rdu2.redhat.com: mpatocka owned process doing -bs Date: Mon, 10 Oct 2022 08:36:54 -0400 (EDT) From: Mikulas Patocka X-X-Sender: mpatocka@file01.intranet.prod.int.rdu2.redhat.com To: Joe Thornber , Mike Snitzer , Heinz Mauelshagen In-Reply-To: Message-ID: References: User-Agent: Alpine 2.02 (LRH 1266 2009-07-14) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.6 Subject: [dm-devel] [PATCH 3/4] persistent-data: simplify struct ro_spine X-BeenThere: dm-devel@redhat.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: device-mapper development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: dm-devel@redhat.com Errors-To: dm-devel-bounces@redhat.com Sender: "dm-devel" X-Scanned-By: MIMEDefang 3.1 on 10.11.54.7 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com The struct ro_spine holds pointers to two last blocks when doing read-only btree walk. The second block is never accessed (note that the function ro_pop is never called), so we can simplify the code so that it holds pointer to just one block. Signed-off-by: Mikulas Patocka --- drivers/md/persistent-data/dm-btree-internal.h | 4 -- drivers/md/persistent-data/dm-btree-spine.c | 41 +++++-------------------- 2 files changed, 10 insertions(+), 35 deletions(-) -- dm-devel mailing list dm-devel@redhat.com https://listman.redhat.com/mailman/listinfo/dm-devel Index: linux-2.6/drivers/md/persistent-data/dm-btree-internal.h =================================================================== --- linux-2.6.orig/drivers/md/persistent-data/dm-btree-internal.h 2022-10-10 11:20:17.000000000 +0200 +++ linux-2.6/drivers/md/persistent-data/dm-btree-internal.h 2022-10-10 11:20:55.000000000 +0200 @@ -63,14 +63,12 @@ void unlock_block(struct dm_btree_info * struct ro_spine { struct dm_btree_info *info; - int count; - struct dm_block *nodes[2]; + struct dm_block *node; }; void init_ro_spine(struct ro_spine *s, struct dm_btree_info *info); void exit_ro_spine(struct ro_spine *s); int ro_step(struct ro_spine *s, dm_block_t new_child); -void ro_pop(struct ro_spine *s); struct btree_node *ro_node(struct ro_spine *s); struct shadow_spine { Index: linux-2.6/drivers/md/persistent-data/dm-btree-spine.c =================================================================== --- linux-2.6.orig/drivers/md/persistent-data/dm-btree-spine.c 2022-10-10 11:20:17.000000000 +0200 +++ linux-2.6/drivers/md/persistent-data/dm-btree-spine.c 2022-10-10 11:20:42.000000000 +0200 @@ -123,52 +123,29 @@ void unlock_block(struct dm_btree_info * void init_ro_spine(struct ro_spine *s, struct dm_btree_info *info) { s->info = info; - s->count = 0; - s->nodes[0] = NULL; - s->nodes[1] = NULL; + s->node = NULL; } void exit_ro_spine(struct ro_spine *s) { - int i; - - for (i = 0; i < s->count; i++) { - unlock_block(s->info, s->nodes[i]); - } + if (s->node) + unlock_block(s->info, s->node); } int ro_step(struct ro_spine *s, dm_block_t new_child) { - int r; - - if (s->count == 2) { - unlock_block(s->info, s->nodes[0]); - s->nodes[0] = s->nodes[1]; - s->count--; + if (s->node) { + unlock_block(s->info, s->node); + s->node = NULL; } - r = bn_read_lock(s->info, new_child, s->nodes + s->count); - if (!r) - s->count++; - - return r; -} - -void ro_pop(struct ro_spine *s) -{ - BUG_ON(!s->count); - --s->count; - unlock_block(s->info, s->nodes[s->count]); + return bn_read_lock(s->info, new_child, &s->node); } struct btree_node *ro_node(struct ro_spine *s) { - struct dm_block *block; - - BUG_ON(!s->count); - block = s->nodes[s->count - 1]; - - return dm_block_data(block); + BUG_ON(!s->node); + return dm_block_data(s->node); } /*----------------------------------------------------------------*/ From patchwork Mon Oct 10 12:37:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mikulas Patocka X-Patchwork-Id: 13002569 X-Patchwork-Delegate: snitzer@redhat.com 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 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.lore.kernel.org (Postfix) with ESMTPS id 11270C433FE for ; Mon, 10 Oct 2022 12:37:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1665405455; h=from:from:sender:sender: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:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=QzqJNOoyv1KLI8/ZtK/ot91yyOCJ3noGA279gM+1Y7I=; b=T/xIx/XqhFIY+vV4/2U4IPeyKr0Bm5WMUa6+vaGMCYwBxKdQAcx8uDP6/58bXCLmFdyNZy ThLvA15bKaBwiFgKDQlLiuyQP04cZ/3ZuMYR/z7nmPQYJllOOlyJPQ4K7PthAbYSP0EbUc mezFCibl+XsrId6JgkSF2dBbOOLycqg= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-370-UvR4B9IzOQGQtdspXvh7rw-1; Mon, 10 Oct 2022 08:37:34 -0400 X-MC-Unique: UvR4B9IzOQGQtdspXvh7rw-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 86F1F811E87; Mon, 10 Oct 2022 12:37:32 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (unknown [10.30.29.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id 76271492B07; Mon, 10 Oct 2022 12:37:32 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (localhost [IPv6:::1]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 4305C19465A0; Mon, 10 Oct 2022 12:37:32 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 67C681946597 for ; Mon, 10 Oct 2022 12:37:31 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id 487A440E992D; Mon, 10 Oct 2022 12:37:31 +0000 (UTC) Received: from file01.intranet.prod.int.rdu2.redhat.com (file01.intranet.prod.int.rdu2.redhat.com [10.11.5.7]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 388AC40E992A; Mon, 10 Oct 2022 12:37:31 +0000 (UTC) Received: from file01.intranet.prod.int.rdu2.redhat.com (localhost [127.0.0.1]) by file01.intranet.prod.int.rdu2.redhat.com (8.14.4/8.14.4) with ESMTP id 29ACbVno026797; Mon, 10 Oct 2022 08:37:31 -0400 Received: from localhost (mpatocka@localhost) by file01.intranet.prod.int.rdu2.redhat.com (8.14.4/8.14.4/Submit) with ESMTP id 29ACbV8t026793; Mon, 10 Oct 2022 08:37:31 -0400 X-Authentication-Warning: file01.intranet.prod.int.rdu2.redhat.com: mpatocka owned process doing -bs Date: Mon, 10 Oct 2022 08:37:31 -0400 (EDT) From: Mikulas Patocka X-X-Sender: mpatocka@file01.intranet.prod.int.rdu2.redhat.com To: Joe Thornber , Mike Snitzer , Heinz Mauelshagen In-Reply-To: Message-ID: References: User-Agent: Alpine 2.02 (LRH 1266 2009-07-14) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 Subject: [dm-devel] [PATCH 4/4] persistent-data: reduce lock contention while walking the btree X-BeenThere: dm-devel@redhat.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: device-mapper development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: dm-devel@redhat.com Errors-To: dm-devel-bounces@redhat.com Sender: "dm-devel" X-Scanned-By: MIMEDefang 3.1 on 10.11.54.10 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com This patch reduces lock contention in btree walks. We modify the functions init_ro_wpin, exit_ro_spine and ro_step so that they use dm_bufio_lock_read/dm_bufio_get_unlocked/dm_bufio_unlock_read. If dm_bm_fast_get_block fails, we fallback to normal locking. When doing tests on pmem and fully provisioned thin volume, it improves thoughput from 286MiB/s to 442MiB/s. fio --ioengine=psync --iodepth=1 --rw=randrw --bs=4k --direct=1 --numjobs=12 --time_based --runtime=10 --group_reporting --name=/dev/vg/thin before: READ: bw=286MiB/s WRITE: bw=286MiB/s after: READ: bw=442MiB/s WRITE: bw=442MiB/s Signed-off-by: Mikulas Patocka --- drivers/md/persistent-data/dm-block-manager.c | 32 ++++++++++++++++ drivers/md/persistent-data/dm-block-manager.h | 6 +++ drivers/md/persistent-data/dm-btree-internal.h | 2 + drivers/md/persistent-data/dm-btree-spine.c | 39 ++++++++++++++++++-- drivers/md/persistent-data/dm-transaction-manager.c | 17 ++++++++ drivers/md/persistent-data/dm-transaction-manager.h | 6 +++ 6 files changed, 99 insertions(+), 3 deletions(-) -- dm-devel mailing list dm-devel@redhat.com https://listman.redhat.com/mailman/listinfo/dm-devel Index: linux-2.6/drivers/md/persistent-data/dm-block-manager.c =================================================================== --- linux-2.6.orig/drivers/md/persistent-data/dm-block-manager.c 2022-10-10 11:26:03.000000000 +0200 +++ linux-2.6/drivers/md/persistent-data/dm-block-manager.c 2022-10-10 11:32:20.000000000 +0200 @@ -601,6 +601,38 @@ void dm_bm_unlock(struct dm_block *b) } EXPORT_SYMBOL_GPL(dm_bm_unlock); +void dm_bm_fast_lock(struct dm_block_manager *bm) +{ + dm_bufio_lock_read(bm->bufio); +} + +void dm_bm_fast_unlock(struct dm_block_manager *bm) +{ + dm_bufio_unlock_read(bm->bufio); +} + +int dm_bm_fast_get_block(struct dm_block_manager *bm, + dm_block_t b, struct dm_block_validator *v, + struct dm_block **result) +{ + int r; + struct buffer_aux *aux; + void *p; + + p = dm_bufio_get_unlocked(bm->bufio, b, (struct dm_buffer **) result); + if (IS_ERR(p)) + return PTR_ERR(p); + if (unlikely(!p)) + return -EWOULDBLOCK; + + aux = dm_bufio_get_aux_data(to_buffer(*result)); + r = dm_bm_validate_buffer(bm, to_buffer(*result), aux, v); + if (unlikely(r)) + return r; + + return 0; +} + int dm_bm_flush(struct dm_block_manager *bm) { if (dm_bm_is_read_only(bm)) Index: linux-2.6/drivers/md/persistent-data/dm-block-manager.h =================================================================== --- linux-2.6.orig/drivers/md/persistent-data/dm-block-manager.h 2022-10-10 11:26:03.000000000 +0200 +++ linux-2.6/drivers/md/persistent-data/dm-block-manager.h 2022-10-10 11:31:24.000000000 +0200 @@ -96,6 +96,12 @@ int dm_bm_write_lock_zero(struct dm_bloc void dm_bm_unlock(struct dm_block *b); +void dm_bm_fast_lock(struct dm_block_manager *bm); +void dm_bm_fast_unlock(struct dm_block_manager *bm); +int dm_bm_fast_get_block(struct dm_block_manager *bm, + dm_block_t b, struct dm_block_validator *v, + struct dm_block **result); + /* * It's a common idiom to have a superblock that should be committed last. * Index: linux-2.6/drivers/md/persistent-data/dm-btree-internal.h =================================================================== --- linux-2.6.orig/drivers/md/persistent-data/dm-btree-internal.h 2022-10-10 11:26:03.000000000 +0200 +++ linux-2.6/drivers/md/persistent-data/dm-btree-internal.h 2022-10-10 11:26:03.000000000 +0200 @@ -64,6 +64,8 @@ struct ro_spine { struct dm_btree_info *info; struct dm_block *node; + bool fast_locked; + bool fast_lock_failed; }; void init_ro_spine(struct ro_spine *s, struct dm_btree_info *info); Index: linux-2.6/drivers/md/persistent-data/dm-btree-spine.c =================================================================== --- linux-2.6.orig/drivers/md/persistent-data/dm-btree-spine.c 2022-10-10 11:26:03.000000000 +0200 +++ linux-2.6/drivers/md/persistent-data/dm-btree-spine.c 2022-10-10 11:38:00.000000000 +0200 @@ -118,27 +118,60 @@ void unlock_block(struct dm_btree_info * dm_tm_unlock(info->tm, b); } +static void bn_fast_lock(struct dm_btree_info *info) +{ + dm_tm_fast_lock(info->tm); +} + +static void bn_fast_unlock(struct dm_btree_info *info) +{ + dm_tm_fast_unlock(info->tm); +} + +static int bn_fast_get_block(struct dm_btree_info *info, dm_block_t b, + struct dm_block **result) +{ + return dm_tm_fast_get_block(info->tm, b, &btree_node_validator, result); +} + /*----------------------------------------------------------------*/ void init_ro_spine(struct ro_spine *s, struct dm_btree_info *info) { s->info = info; s->node = NULL; + s->fast_locked = false; + s->fast_lock_failed = false; } void exit_ro_spine(struct ro_spine *s) { - if (s->node) + if (s->fast_locked) + bn_fast_unlock(s->info); + else if (s->node) unlock_block(s->info, s->node); } int ro_step(struct ro_spine *s, dm_block_t new_child) { if (s->node) { - unlock_block(s->info, s->node); + if (unlikely(!s->fast_locked)) + unlock_block(s->info, s->node); s->node = NULL; } - + if (likely(!s->fast_lock_failed)) { + int r; + if (!s->fast_locked) { + bn_fast_lock(s->info); + s->fast_locked = true; + } + r = bn_fast_get_block(s->info, new_child, &s->node); + if (likely(!r)) + return 0; + s->fast_lock_failed = true; + s->fast_locked = false; + bn_fast_unlock(s->info); + } return bn_read_lock(s->info, new_child, &s->node); } Index: linux-2.6/drivers/md/persistent-data/dm-transaction-manager.c =================================================================== --- linux-2.6.orig/drivers/md/persistent-data/dm-transaction-manager.c 2022-10-10 11:26:03.000000000 +0200 +++ linux-2.6/drivers/md/persistent-data/dm-transaction-manager.c 2022-10-10 11:50:50.000000000 +0200 @@ -348,6 +348,23 @@ void dm_tm_unlock(struct dm_transaction_ } EXPORT_SYMBOL_GPL(dm_tm_unlock); +void dm_tm_fast_lock(struct dm_transaction_manager *tm) +{ + dm_bm_fast_lock(tm->is_clone ? tm->real->bm : tm->bm); +} + +void dm_tm_fast_unlock(struct dm_transaction_manager *tm) +{ + dm_bm_fast_unlock(tm->is_clone ? tm->real->bm : tm->bm); +} + +int dm_tm_fast_get_block(struct dm_transaction_manager *tm, dm_block_t b, + struct dm_block_validator *v, + struct dm_block **blk) +{ + return dm_bm_fast_get_block(tm->is_clone ? tm->real->bm : tm->bm, b, v, blk); +} + void dm_tm_inc(struct dm_transaction_manager *tm, dm_block_t b) { /* Index: linux-2.6/drivers/md/persistent-data/dm-transaction-manager.h =================================================================== --- linux-2.6.orig/drivers/md/persistent-data/dm-transaction-manager.h 2022-10-10 11:26:03.000000000 +0200 +++ linux-2.6/drivers/md/persistent-data/dm-transaction-manager.h 2022-10-10 11:30:17.000000000 +0200 @@ -96,6 +96,12 @@ int dm_tm_read_lock(struct dm_transactio void dm_tm_unlock(struct dm_transaction_manager *tm, struct dm_block *b); +void dm_tm_fast_lock(struct dm_transaction_manager *tm); +void dm_tm_fast_unlock(struct dm_transaction_manager *tm); +int dm_tm_fast_get_block(struct dm_transaction_manager *tm, dm_block_t b, + struct dm_block_validator *v, + struct dm_block **blk); + /* * Functions for altering the reference count of a block directly. */