From patchwork Fri Jul 29 11:58:03 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikhil Kshirsagar X-Patchwork-Id: 1661997 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=canonical.com header.i=@canonical.com header.a=rsa-sha256 header.s=20210705 header.b=kmCgDKcn; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.ubuntu.com (client-ip=91.189.94.19; helo=huckleberry.canonical.com; envelope-from=kernel-team-bounces@lists.ubuntu.com; receiver=) Received: from huckleberry.canonical.com (huckleberry.canonical.com [91.189.94.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4LvQyc5Vf6z9sGZ for ; Fri, 29 Jul 2022 21:58:31 +1000 (AEST) Received: from localhost ([127.0.0.1] helo=huckleberry.canonical.com) by huckleberry.canonical.com with esmtp (Exim 4.86_2) (envelope-from ) id 1oHOdK-0005nh-JD; Fri, 29 Jul 2022 11:58:22 +0000 Received: from smtp-relay-internal-1.internal ([10.131.114.114] helo=smtp-relay-internal-1.canonical.com) by huckleberry.canonical.com with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1oHOdI-0005nO-Nc for kernel-team@lists.ubuntu.com; Fri, 29 Jul 2022 11:58:20 +0000 Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) (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 smtp-relay-internal-1.canonical.com (Postfix) with ESMTPS id 7FD8B3F130 for ; Fri, 29 Jul 2022 11:58:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=canonical.com; s=20210705; t=1659095900; bh=G4+3ZUmQCTnhvL5bB8iyOJyVBG99Ve0kTKv1Xa2yeyM=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=kmCgDKcnZyxCBNWxa0bJ4AXcqRupyatNUQg+XISyiAG9XDTrPCVPrbOXHMG2Xm7Il MPnOXt3KnOHF6r97yWGZb74N3p2tHUnMmbpBXe3CsK0c2jFlzrqnjN0V2PvfZ2mMps O4qVYQM5w5/cPSpvlkGF8tvUPoDZm0po5AOJBJq5qWf3r5yp1TtxPc1xSDi1vVaksf QlzholGnWIDoMvpNh7Krzek8t1KrRGUm/2l6K7FPhhVZXPQRI2WCVeKdgzgP+uu/jH KhJ11Q4a+ZoEZpULsoDIIGXlnUPXH1qthn6vxuXrdO3mu27OzU8I/QLrv/pVo/W1EC aupHKat7dPApA== Received: by mail-wm1-f72.google.com with SMTP id h65-20020a1c2144000000b003a30cae106cso3902457wmh.8 for ; Fri, 29 Jul 2022 04:58:20 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=G4+3ZUmQCTnhvL5bB8iyOJyVBG99Ve0kTKv1Xa2yeyM=; b=siOMU9s8wCd2JsRR+RkWwdXJWTKEipIHekSKaAli+ioJd6AE6doDJf/x3vFE0amYLN hWT2u5YK4lx+VrqDVynnCOXwXWMUt6gDy/W/fl+oDKNzX+bSvUAjjXwik/SQjWWKr3bd hU5Dx2SfERg9B3VojVbEgghPHvWfHMK8YAF+JXHM8jBYuc5xbgTSHJW4qzgGTsiUEe93 QA2eJpeW4v3L8xtSpGZTdh7n4TObKDMfliXIrKEUfzfdMQGxTTqBl995g41T+mf6SeuL XlVrq9fFmS8+mjnFpto6i7skZhLvXWQ4G+Kq+C5X0IHMRsGeRXM02yDB4mYr13rxweWG Twtg== X-Gm-Message-State: AJIora9Zul9IAVAQd7YcPGlQ7DPhYKHPCD5Xe/R1WCnzPiZWwxpqDZuT knC2Onx7ifmcj/ULnUkA50C1R7CW0R4FSct7haTbwWvVduFmGKoGu1phVAene7OlrVi8ibV78dy Md3cDlO6ofFnqAVj79jQJU31DfFkhxstBBXQPa4tKIw== X-Received: by 2002:a7b:cbce:0:b0:3a3:2d78:f066 with SMTP id n14-20020a7bcbce000000b003a32d78f066mr2322353wmi.66.1659095900179; Fri, 29 Jul 2022 04:58:20 -0700 (PDT) X-Google-Smtp-Source: AGRyM1tI7ScJTkOWGtq0AuIYK7ToYfT2AZPFfYnxqmhG1cSsPV81d9/3WM6TA2L4Kl4+y1c9ZcAwRA== X-Received: by 2002:a7b:cbce:0:b0:3a3:2d78:f066 with SMTP id n14-20020a7bcbce000000b003a32d78f066mr2322325wmi.66.1659095899623; Fri, 29 Jul 2022 04:58:19 -0700 (PDT) Received: from localhost.localdomain ([2001:67c:1560:8007::aac:c563]) by smtp.gmail.com with ESMTPSA id r126-20020a1c2b84000000b003a2fde6ef62sm4236516wmr.7.2022.07.29.04.58.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 29 Jul 2022 04:58:19 -0700 (PDT) From: Nikhil Kshirsagar To: dariusz.gadomski@canonical.com, kernel-team@lists.ubuntu.com Subject: [PATCH 2/3] bcache: improve multithreaded bch_sectors_dirty_init() Date: Fri, 29 Jul 2022 17:28:03 +0530 Message-Id: <20220729115804.2207073-3-nikhil.kshirsagar@canonical.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220729115804.2207073-1-nikhil.kshirsagar@canonical.com> References: <20220729115804.2207073-1-nikhil.kshirsagar@canonical.com> MIME-Version: 1.0 X-BeenThere: kernel-team@lists.ubuntu.com X-Mailman-Version: 2.1.20 Precedence: list List-Id: Kernel team discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: kernel-team-bounces@lists.ubuntu.com Sender: "kernel-team" From: Coly Li Commit b144e45fc576 ("bcache: make bch_sectors_dirty_init() to be multithreaded") makes bch_sectors_dirty_init() to be much faster when counting dirty sectors by iterating all dirty keys in the btree. But it isn't in ideal shape yet, still can be improved. This patch does the following changes to improve current parallel dirty keys iteration on the btree, - Add read lock to root node when multiple threads iterating the btree, to prevent the root node gets split by I/Os from other registered bcache devices. - Remove local variable "char name[32]" and generate kernel thread name string directly when calling kthread_run(). - Allocate "struct bch_dirty_init_state state" directly on stack and avoid the unnecessary dynamic memory allocation for it. - Decrease BCH_DIRTY_INIT_THRD_MAX from 64 to 12 which is enough indeed. - Increase &state->started to count created kernel thread after it succeeds to create. - When wait for all dirty key counting threads to finish, use wait_event() to replace wait_event_interruptible(). With the above changes, the code is more clear, and some potential error conditions are avoided. Fixes: b144e45fc576 ("bcache: make bch_sectors_dirty_init() to be multithreaded") Signed-off-by: Coly Li Cc: stable@vger.kernel.org Link: https://lore.kernel.org/r/20220524102336.10684-3-colyli@suse.de Signed-off-by: Jens Axboe (cherry picked from commit 93e096c33e4fa7728d63ced0053edd389a5683c7) Signed-off-by: Nikhil Kshirsagar --- drivers/md/bcache/writeback.c | 62 ++++++++++++++--------------------- drivers/md/bcache/writeback.h | 2 +- 2 files changed, 26 insertions(+), 38 deletions(-) diff --git a/drivers/md/bcache/writeback.c b/drivers/md/bcache/writeback.c index 1c03a3b72f8a..aac3d0ca4c25 100644 --- a/drivers/md/bcache/writeback.c +++ b/drivers/md/bcache/writeback.c @@ -945,10 +945,10 @@ void bch_sectors_dirty_init(struct bcache_device *d) struct btree_iter iter; struct sectors_dirty_init op; struct cache_set *c = d->c; - struct bch_dirty_init_state *state; - char name[32]; + struct bch_dirty_init_state state; /* Just count root keys if no leaf node */ + rw_lock(0, c->root, c->root->level); if (c->root->level == 0) { bch_btree_op_init(&op.op, -1); op.inode = d->id; @@ -958,54 +958,42 @@ void bch_sectors_dirty_init(struct bcache_device *d) for_each_key_filter(&c->root->keys, k, &iter, bch_ptr_invalid) sectors_dirty_init_fn(&op.op, c->root, k); + rw_unlock(0, c->root); return; } - state = kzalloc(sizeof(struct bch_dirty_init_state), GFP_KERNEL); - if (!state) { - pr_warn("sectors dirty init failed: cannot allocate memory\n"); - return; - } - - state->c = c; - state->d = d; - state->total_threads = bch_btre_dirty_init_thread_nr(); - state->key_idx = 0; - spin_lock_init(&state->idx_lock); - atomic_set(&state->started, 0); - atomic_set(&state->enough, 0); - init_waitqueue_head(&state->wait); - - for (i = 0; i < state->total_threads; i++) { - /* Fetch latest state->enough earlier */ + state.c = c; + state.d = d; + state.total_threads = bch_btre_dirty_init_thread_nr(); + state.key_idx = 0; + spin_lock_init(&state.idx_lock); + atomic_set(&state.started, 0); + atomic_set(&state.enough, 0); + init_waitqueue_head(&state.wait); + + for (i = 0; i < state.total_threads; i++) { + /* Fetch latest state.enough earlier */ smp_mb__before_atomic(); - if (atomic_read(&state->enough)) + if (atomic_read(&state.enough)) break; - state->infos[i].state = state; - atomic_inc(&state->started); - snprintf(name, sizeof(name), "bch_dirty_init[%d]", i); - - state->infos[i].thread = - kthread_run(bch_dirty_init_thread, - &state->infos[i], - name); - if (IS_ERR(state->infos[i].thread)) { + state.infos[i].state = &state; + state.infos[i].thread = + kthread_run(bch_dirty_init_thread, &state.infos[i], + "bch_dirtcnt[%d]", i); + if (IS_ERR(state.infos[i].thread)) { pr_err("fails to run thread bch_dirty_init[%d]\n", i); for (--i; i >= 0; i--) - kthread_stop(state->infos[i].thread); + kthread_stop(state.infos[i].thread); goto out; } + atomic_inc(&state.started); } - /* - * Must wait for all threads to stop. - */ - wait_event_interruptible(state->wait, - atomic_read(&state->started) == 0); - out: - kfree(state); + /* Must wait for all threads to stop. */ + wait_event(state.wait, atomic_read(&state.started) == 0); + rw_unlock(0, c->root); } void bch_cached_dev_writeback_init(struct cached_dev *dc) diff --git a/drivers/md/bcache/writeback.h b/drivers/md/bcache/writeback.h index 02b2f9df73f6..31df716951f6 100644 --- a/drivers/md/bcache/writeback.h +++ b/drivers/md/bcache/writeback.h @@ -20,7 +20,7 @@ #define BCH_WRITEBACK_FRAGMENT_THRESHOLD_MID 57 #define BCH_WRITEBACK_FRAGMENT_THRESHOLD_HIGH 64 -#define BCH_DIRTY_INIT_THRD_MAX 64 +#define BCH_DIRTY_INIT_THRD_MAX 12 /* * 14 (16384ths) is chosen here as something that each backing device * should be a reasonable fraction of the share, and not to blow up