From patchwork Mon Mar 19 00:36:22 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Lyle X-Patchwork-Id: 10291471 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 4BD55600F6 for ; Mon, 19 Mar 2018 00:37:01 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3DA7628EDF for ; Mon, 19 Mar 2018 00:37:01 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 3277F28F56; Mon, 19 Mar 2018 00:37:01 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id CDC1D28EDF for ; Mon, 19 Mar 2018 00:37:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754531AbeCSAg5 (ORCPT ); Sun, 18 Mar 2018 20:36:57 -0400 Received: from mail-pl0-f66.google.com ([209.85.160.66]:37332 "EHLO mail-pl0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754721AbeCSAgv (ORCPT ); Sun, 18 Mar 2018 20:36:51 -0400 Received: by mail-pl0-f66.google.com with SMTP id w12-v6so9225161plp.4 for ; Sun, 18 Mar 2018 17:36:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=lyle-org.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=x4sw+dYb+YsFPxQJm6iWVu+6VgfdB/jOVNOS+TFQ/Wo=; b=moQmbA2NkPX+MLOarqrKZNLMNTK1ZTOIIUqdCAhHCP5Y/XAqhuc+CJtemRbUpKUhpE M+2Hx2uDJ5LRJYhlaspEj61jd+z897qrtCzJpZKwiDqElZoQzrCP6vGkibnfoT7bEb+C 9w0UZYPIAdA/a5SUUhQislK1sT8LpTWVtQiWnypSu9+5uo1op4Y4Wgq1WfanK6SLmpyu tFCPx4MY/BV0s9tdtiD6r7jkTOQdm3a5KSPa8IVPoqKOYtp0EAdc1lP7UHS0yznZpzTd hjbdGRzyMGcnjUnoB1TyxUddLVVLheLtQEc4GV4Hya7fodO6PMvPMvGlusaiSb50CnqG 6wKQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=x4sw+dYb+YsFPxQJm6iWVu+6VgfdB/jOVNOS+TFQ/Wo=; b=COUyInyTlxJue9KNbwfJ/fdt6Gqdtgi3xaiEHCSH/vlTjfSv6LhMRNwi/3pW2pS37N PbfTxUL8ONvY3dWQ/2u4O9U2+HpDlW/xFuzWYtRNu/irjK+1QbfufMnWiaccgHY/wCGd QwYYgCelcVhLmyMPT0u9gslSqCUvK4MWGLwUZDU/bNmRS7DOROdQuVHJnVCAdxAiGdOK LCEEA+Gkqrm+luHgjvpLo4AFcdsrt3dTEWB6/0cCErSINKrzqDoZyQtaEsIcKQDTNAA3 zgIMtQ2A5JvFVhfok8sLjLRo/BtvANGSXsETI7WLvVRfVW6niH3NYd5QtVad1ywdHpXK VFcw== X-Gm-Message-State: AElRT7HMICMpsA8TGlbtD5htjdbU2E/KPqNTlMHK9rRAm2X1DzW6s3kS XzfpBCKJCZmJQ5nrRnWaX5kkcA== X-Google-Smtp-Source: AG47ELsvS/xZlWDI5asxreVQSkjkKEqs5TgeV4GDgSGSYmTjtQ4W9urqqPMpshzyRHYVfVTL9XT1zg== X-Received: by 2002:a17:902:8bc5:: with SMTP id r5-v6mr10505192plo.132.1521419810855; Sun, 18 Mar 2018 17:36:50 -0700 (PDT) Received: from midnight.lan (2600-6c52-6200-09b7-0000-0000-0000-0d66.dhcp6.chtrptr.net. [2600:6c52:6200:9b7::d66]) by smtp.gmail.com with ESMTPSA id a17sm27674857pfc.122.2018.03.18.17.36.49 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 18 Mar 2018 17:36:50 -0700 (PDT) From: Michael Lyle To: linux-bcache@vger.kernel.org, linux-block@vger.kernel.org Cc: axboe@fb.com, Tang Junhui Subject: [for-4.17 09/20] bcache: fix using of loop variable in memory shrink Date: Sun, 18 Mar 2018 17:36:22 -0700 Message-Id: <20180319003633.27225-10-mlyle@lyle.org> X-Mailer: git-send-email 2.14.1 In-Reply-To: <20180319003633.27225-1-mlyle@lyle.org> References: <20180319003633.27225-1-mlyle@lyle.org> Sender: linux-block-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Tang Junhui In bch_mca_scan(), There are some confusion and logical error in the use of loop variables. In this patch, we clarify them as: 1) nr: the number of btree nodes needs to scan, which will decrease after we scan a btree node, and should not be less than 0; 2) i: the number of btree nodes have scanned, includes both btree_cache_freeable and btree_cache, which should not be bigger than btree_cache_used; 3) freed: the number of btree nodes have freed. Signed-off-by: Tang Junhui Reviewed-by: Michael Lyle --- drivers/md/bcache/btree.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/drivers/md/bcache/btree.c b/drivers/md/bcache/btree.c index b2d4899f48d5..d64aff0b8abc 100644 --- a/drivers/md/bcache/btree.c +++ b/drivers/md/bcache/btree.c @@ -665,6 +665,7 @@ static unsigned long bch_mca_scan(struct shrinker *shrink, struct btree *b, *t; unsigned long i, nr = sc->nr_to_scan; unsigned long freed = 0; + unsigned int btree_cache_used; if (c->shrinker_disabled) return SHRINK_STOP; @@ -689,9 +690,10 @@ static unsigned long bch_mca_scan(struct shrinker *shrink, nr = min_t(unsigned long, nr, mca_can_free(c)); i = 0; + btree_cache_used = c->btree_cache_used; list_for_each_entry_safe(b, t, &c->btree_cache_freeable, list) { - if (freed >= nr) - break; + if (nr <= 0) + goto out; if (++i > 3 && !mca_reap(b, 0, false)) { @@ -699,9 +701,10 @@ static unsigned long bch_mca_scan(struct shrinker *shrink, rw_unlock(true, b); freed++; } + nr--; } - for (i = 0; (nr--) && i < c->btree_cache_used; i++) { + for (; (nr--) && i < btree_cache_used; i++) { if (list_empty(&c->btree_cache)) goto out;