From patchwork Mon Nov 30 16:22:16 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Begunkov X-Patchwork-Id: 11940991 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id B30F8C63777 for ; Mon, 30 Nov 2020 16:26:22 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4C140206E3 for ; Mon, 30 Nov 2020 16:26:22 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Z1kuYjif" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728716AbgK3Q0V (ORCPT ); Mon, 30 Nov 2020 11:26:21 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37580 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725859AbgK3Q0V (ORCPT ); Mon, 30 Nov 2020 11:26:21 -0500 Received: from mail-wr1-x444.google.com (mail-wr1-x444.google.com [IPv6:2a00:1450:4864:20::444]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C115CC0613D2 for ; Mon, 30 Nov 2020 08:25:40 -0800 (PST) Received: by mail-wr1-x444.google.com with SMTP id z7so16983661wrn.3 for ; Mon, 30 Nov 2020 08:25:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=rcKTA5KGE4nWslF18pAr/90KwDnlEsEfK09+Yniqs10=; b=Z1kuYjifD0yaBDEQ4qAlCghmd6HAWMU/n3n+c0t3n2CFpA9vJNlC1hMgdw7KDJiOs+ TOFEFG4iJmTB/ug+Q1nJtoXLNMM4nZJyGr+q7oDb9H+eHs/B5wqru3L1uSkSp4GDXUxI TWORH36SyNb9X6v/tW7Syr86gHrKYuC+Bud1KfcToaaXplCr2m6PgLoKoEdMK9jL5yGN BlDum+DiAyeXaixa5W733qwHVsJ78xbj/srdc5p2cbz5ZoAtRLfJD4hzt3ZaeC65Satk B6hVbmlwvj/l5NA30fM/AtBf2ZV/FmxbWzCUs6HgjkTDaN/u6UwtwDhwNyPbzdNv4VB0 EeSQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=rcKTA5KGE4nWslF18pAr/90KwDnlEsEfK09+Yniqs10=; b=mowgqIE56+zYJAb3TjTpI2wvT9G629oKAX3a78eYTpsD7dykEgcBsfA7QYN/zoPIkI AV9mPnJmCdc6B0416jValRmwbjMeQvtOS864baKdfdNv6U0xsISo/9YPyFpf2GSo1zIa 12LnrVZqVKB8mQIWUvvfuUwHNFG41U+bzas9CYKE/l/pBkMaYcqnsbwcj8XRX1dD3orh GMFXEhMCZXhwFIz/IkY+d/e35IjxatsXCAOuB//mZN6tlpjDa4ALnQzVD6HUE3u1Q4zn HSIyARzr6+lwON6c8CkPWzIkFd8spi7Qq7qvljKW76YMCfENFfEzM77bkBCSJeNc5zZ6 rggA== X-Gm-Message-State: AOAM530o9Gv3fvAwnbM0LGgSUY373ju0E6eFM7CRFpaTNbPsTzQg5hPd HGVxnQ+QrkUN6dKmEPh5Smo= X-Google-Smtp-Source: ABdhPJyDLWsR3EEvs+2LLKm5yOvvnsUB3/x55kaCp7rpA4nFghEf7eYOgLVZf4Ncy4mwsShQcyxYmg== X-Received: by 2002:a05:6000:364:: with SMTP id f4mr29127108wrf.290.1606753539575; Mon, 30 Nov 2020 08:25:39 -0800 (PST) Received: from localhost.localdomain (host109-152-100-189.range109-152.btcentralplus.com. [109.152.100.189]) by smtp.gmail.com with ESMTPSA id l16sm29539476wrx.5.2020.11.30.08.25.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Nov 2020 08:25:39 -0800 (PST) From: Pavel Begunkov To: Chris Mason , Josef Bacik , David Sterba , linux-btrfs@vger.kernel.org Subject: [PATCH 1/2] btrfs: fix racy access to discard_ctl data Date: Mon, 30 Nov 2020 16:22:16 +0000 Message-Id: <36b18f2dcc56ff5553d700afa7b360572c8181f3.1606752605.git.asml.silence@gmail.com> X-Mailer: git-send-email 2.24.0 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org Because only one discard worker may be running at any given point, it could have been safe to modify ->prev_discard, etc. without synchronisation, if not for @override flag in btrfs_discard_schedule_work() and delayed_work_pending() returning false while workfn is running. That may lead lead to torn reads of u64 for some architectures, but that's not a big problem as only slightly affects the discard rate. Suggested-by: Josef Bacik Signed-off-by: Pavel Begunkov --- fs/btrfs/discard.c | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/fs/btrfs/discard.c b/fs/btrfs/discard.c index 1db966bf85b2..05e2b8150142 100644 --- a/fs/btrfs/discard.c +++ b/fs/btrfs/discard.c @@ -438,6 +438,7 @@ static void btrfs_discard_workfn(struct work_struct *work) int discard_index = 0; u64 trimmed = 0; u64 minlen = 0; + u64 now; discard_ctl = container_of(work, struct btrfs_discard_ctl, work.work); @@ -474,13 +475,6 @@ static void btrfs_discard_workfn(struct work_struct *work) discard_ctl->discard_extent_bytes += trimmed; } - /* - * Updated without locks as this is inside the workfn and nothing else - * is reading the values - */ - discard_ctl->prev_discard = trimmed; - discard_ctl->prev_discard_time = ktime_get_ns(); - /* Determine next steps for a block_group */ if (block_group->discard_cursor >= btrfs_block_group_end(block_group)) { if (discard_state == BTRFS_DISCARD_BITMAPS) { @@ -496,7 +490,10 @@ static void btrfs_discard_workfn(struct work_struct *work) } } + now = ktime_get_ns(); spin_lock(&discard_ctl->lock); + discard_ctl->prev_discard = trimmed; + discard_ctl->prev_discard_time = now; discard_ctl->block_group = NULL; spin_unlock(&discard_ctl->lock); From patchwork Mon Nov 30 16:22:17 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Begunkov X-Patchwork-Id: 11940993 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 525CEC64E8A for ; Mon, 30 Nov 2020 16:26:23 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id EE900206E3 for ; Mon, 30 Nov 2020 16:26:22 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="A5uOHYEQ" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728729AbgK3Q0W (ORCPT ); Mon, 30 Nov 2020 11:26:22 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37586 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725859AbgK3Q0V (ORCPT ); Mon, 30 Nov 2020 11:26:21 -0500 Received: from mail-wr1-x443.google.com (mail-wr1-x443.google.com [IPv6:2a00:1450:4864:20::443]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 749A2C0613D3 for ; Mon, 30 Nov 2020 08:25:41 -0800 (PST) Received: by mail-wr1-x443.google.com with SMTP id r3so17003878wrt.2 for ; Mon, 30 Nov 2020 08:25:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=ANsuUQtzht/WXnpYQONZLUeB7Ns2kEv0d75OPH5WvpU=; b=A5uOHYEQchfjoKbQFalJox6Ljo246PJtCUYuzezQFYcIBSViaHQe4rP1Iaym5qMPWs WeXbbgvD7GeSg82OXMKDoNKJa4Juzi0adWOK9CDSIQQq+aF9/QoCvthwlnEzvCX09CMz a56gj/3fgGUagj2I0QVeWGk+mRr3jh1NS+M8qpFMic13nn/khlyOGNBFZnVH/qcPAGf/ QRyFBwuRiVmtUhV+8Wsb7aLwwF2YZPq2IUcDP7rPlOwoPQNApM0Q0BfC3zxDIjrQzuIP +MWx8ZdvSR/e+u3/YAInVpIkJF8ybTNU2o+r4zWED6y/hhgl7NIYzdvoIskyxbgPqcLu vYsA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ANsuUQtzht/WXnpYQONZLUeB7Ns2kEv0d75OPH5WvpU=; b=ZOyYT2YQzhjH9JHXtEZ0kT4NXVVG9yz0GF8zfL0k5EGwFu9gi8+/KXq+mU2tMNzU1/ fRAHRxfuww67FxGBwxW2aTjYZiuFR44BV31dQC7n3+RxbLYRvZVQyDyn+y10EpaeCirH GKUUk6PYQdYwsqjTuS2CXr/9kx7r++Nm+4YwAWYetb50eVXVxbdq1pPuKWmYr4DDidXX WYHXymx0Yl0j3ufsxYHrktJlV6/jnrZz1hVhR2Lr9EeNkfwZqk/Dm6Wei8lmy5JyuFmW B3VClfArKDNpEPJkFFushXjKgvrEVvoUlTCMyGR0yzLqBj7Ghr+kZ0la5byOgHxec8zB WvAA== X-Gm-Message-State: AOAM532JXjg1qUjM5ZnQW9CHOKyD5GvmWrLtt+uyq4kWmoGOnx4Pa8wX v94EJdjMqPOq/jecRhjQCd8= X-Google-Smtp-Source: ABdhPJxHGign6+ad36xOPogDtd+mFxRF73hgN/n3ydYFUAeylHFFPheeXUXHwow/nehdkanoBTBQhQ== X-Received: by 2002:adf:f98a:: with SMTP id f10mr29549097wrr.154.1606753540243; Mon, 30 Nov 2020 08:25:40 -0800 (PST) Received: from localhost.localdomain (host109-152-100-189.range109-152.btcentralplus.com. [109.152.100.189]) by smtp.gmail.com with ESMTPSA id l16sm29539476wrx.5.2020.11.30.08.25.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Nov 2020 08:25:39 -0800 (PST) From: Pavel Begunkov To: Chris Mason , Josef Bacik , David Sterba , linux-btrfs@vger.kernel.org Subject: [PATCH 2/2] btrfs: don't overabuse discard lock Date: Mon, 30 Nov 2020 16:22:17 +0000 Message-Id: <87ae35a07b145227e198b097865f7cbdfdde7915.1606752605.git.asml.silence@gmail.com> X-Mailer: git-send-email 2.24.0 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org btrfs_discard_workfn() drops discard_ctl->lock just to take it again in a moment in btrfs_discard_schedule_work(). Avoid that and also reuse ktime. Signed-off-by: Pavel Begunkov --- fs/btrfs/discard.c | 43 +++++++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/fs/btrfs/discard.c b/fs/btrfs/discard.c index 05e2b8150142..4120e62d7e5a 100644 --- a/fs/btrfs/discard.c +++ b/fs/btrfs/discard.c @@ -330,28 +330,15 @@ void btrfs_discard_queue_work(struct btrfs_discard_ctl *discard_ctl, btrfs_discard_schedule_work(discard_ctl, false); } -/** - * btrfs_discard_schedule_work - responsible for scheduling the discard work - * @discard_ctl: discard control - * @override: override the current timer - * - * Discards are issued by a delayed workqueue item. @override is used to - * update the current delay as the baseline delay interval is reevaluated on - * transaction commit. This is also maxed with any other rate limit. - */ -void btrfs_discard_schedule_work(struct btrfs_discard_ctl *discard_ctl, - bool override) +static void __btrfs_discard_schedule_work(struct btrfs_discard_ctl *discard_ctl, + u64 now, bool override) { struct btrfs_block_group *block_group; - const u64 now = ktime_get_ns(); - - spin_lock(&discard_ctl->lock); if (!btrfs_run_discard_work(discard_ctl)) - goto out; - + return; if (!override && delayed_work_pending(&discard_ctl->work)) - goto out; + return; block_group = find_next_block_group(discard_ctl, now); if (block_group) { @@ -393,7 +380,24 @@ void btrfs_discard_schedule_work(struct btrfs_discard_ctl *discard_ctl, mod_delayed_work(discard_ctl->discard_workers, &discard_ctl->work, nsecs_to_jiffies(delay)); } -out: +} + +/** + * btrfs_discard_schedule_work - responsible for scheduling the discard work + * @discard_ctl: discard control + * @override: override the current timer + * + * Discards are issued by a delayed workqueue item. @override is used to + * update the current delay as the baseline delay interval is reevaluated on + * transaction commit. This is also maxed with any other rate limit. + */ +void btrfs_discard_schedule_work(struct btrfs_discard_ctl *discard_ctl, + bool override) +{ + u64 now = ktime_get_ns(); + + spin_lock(&discard_ctl->lock); + __btrfs_discard_schedule_work(discard_ctl, now, override); spin_unlock(&discard_ctl->lock); } @@ -495,9 +499,8 @@ static void btrfs_discard_workfn(struct work_struct *work) discard_ctl->prev_discard = trimmed; discard_ctl->prev_discard_time = now; discard_ctl->block_group = NULL; + __btrfs_discard_schedule_work(discard_ctl, now, false); spin_unlock(&discard_ctl->lock); - - btrfs_discard_schedule_work(discard_ctl, false); } /**