From patchwork Wed Feb 28 00:56:43 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Omar Sandoval X-Patchwork-Id: 10246447 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 B23C760208 for ; Wed, 28 Feb 2018 00:56:53 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A4CD928B2B for ; Wed, 28 Feb 2018 00:56:53 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 991DB28B2D; Wed, 28 Feb 2018 00:56:53 +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 2FA4628B2B for ; Wed, 28 Feb 2018 00:56:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751611AbeB1A4w (ORCPT ); Tue, 27 Feb 2018 19:56:52 -0500 Received: from mail-pf0-f194.google.com ([209.85.192.194]:37107 "EHLO mail-pf0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751789AbeB1A4v (ORCPT ); Tue, 27 Feb 2018 19:56:51 -0500 Received: by mail-pf0-f194.google.com with SMTP id s24so321553pfm.4 for ; Tue, 27 Feb 2018 16:56:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=osandov-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :in-reply-to:references; bh=LcZ3QSusf4xKCy9Rmv0eOWdYdYZ04AdOIlS2rMfe7dw=; b=UaR907Ju7Mw9pTCr3qgLucsD9Uq8BhHL2LCII36ZN9ZOryXugVcYneMrb9xUZcOQ8u 6NLfc9AC31qftBVyhwjNGJlipOVkCgdVS3obOYZG/7HjYE69yNXC+KTLKuFvneHQogs8 QjGNXqtpTt83jgfE2IxmOeii3BGjdc2P2CKhzgIoeavp2wf0dZUld1ec9Q0qQZkhr804 3RTJ1CjvjeSGaaplSdXozQZFznUZ0GNA+Fv6H5UrqztFC3Jq9RxRK33gc2EzpktFXV6G 909SHB2QYGg82VDOmfHVQFEbDrtvkVz6gTUNhXemcgjectZ5pu3phqEk/o7cfWmjeJTk aEJg== 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:in-reply-to:references; bh=LcZ3QSusf4xKCy9Rmv0eOWdYdYZ04AdOIlS2rMfe7dw=; b=Gs9yWDbcy18HddRKy1jYeB62mz/I50+xZJJP+T7jxW3+30hRXP1nt8Vw4m4iCmVmZJ JDbhE870hLwvvCY2oX8LMKW6E/RO0pzzkmObh76iYIH7K9duoG9TW6lqhncshs0el73t iwA4etuvIdorVz2CslcetCrciApoyVJXoCzY7ZsHQQuX79wJvlbHRUXeNS3MZEDKHqpa z07gpTWCmuD3HQQGpipMZGAVA+ih3RPd0+kT30JDRVM4IGOHrgwS1Z0+YhhsuecAMHE0 52i0iqhXuYkEY62ug6jmxLiXqCP0XuzoM30ZMBTAmDirU32h6x0hOZrhxCUTeifO6SQV ERYA== X-Gm-Message-State: APf1xPDMIH5iRQBSDViBdmmP2J/sy0bD5iHOm5ijpChr97fB14BRcX67 b9pwc5YlmRRulxYcjO8fH7Rz8JgJkmE= X-Google-Smtp-Source: AG47ELvYxdzn9sNvrEpoTWIgEDJLmR/okMi9mIzyjJZauMVKS29K6PFBy9A4KS4TbP8MoX43rTjhMg== X-Received: by 10.99.180.77 with SMTP id n13mr6925667pgu.361.1519779410837; Tue, 27 Feb 2018 16:56:50 -0800 (PST) Received: from vader.thefacebook.com ([2620:10d:c090:200::4:ba04]) by smtp.gmail.com with ESMTPSA id z79sm463752pfa.139.2018.02.27.16.56.49 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 27 Feb 2018 16:56:50 -0800 (PST) From: Omar Sandoval To: linux-block@vger.kernel.org Cc: Jens Axboe , kernel-team@fb.com, Tejun Heo Subject: [PATCH 2/2] sbitmap: use test_and_set_bit_lock()/clear_bit_unlock() Date: Tue, 27 Feb 2018 16:56:43 -0800 Message-Id: <8e20f73a574b84a72838ad3f4dd67be9e2db70ed.1519779182.git.osandov@fb.com> X-Mailer: git-send-email 2.16.2 In-Reply-To: References: In-Reply-To: References: 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: Omar Sandoval sbitmap_queue_get()/sbitmap_queue_clear() are used for allocating/freeing a resource, so they should provide acquire/release barrier semantics, respectively. sbitmap_get() currently contains a full barrier, which is unnecessary, so use test_and_set_bit_lock() instead of test_and_set_bit() (these are equivalent on x86_64). sbitmap_clear_bit() does not imply any barriers, which is incorrect, as accesses of the resource (e.g., request) could potentially get reordered to after the clear_bit(). Introduce sbitmap_clear_bit_unlock() and use it for sbitmap_queue_clear() (this only adds a compiler barrier on x86_64). The other existing user of sbitmap_clear_bit() (the blk-mq software queue pending map) is serialized through a spinlock and does not need this. Reported-by: Tejun Heo Signed-off-by: Omar Sandoval --- include/linux/sbitmap.h | 8 ++++++++ lib/sbitmap.c | 10 +++++----- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/include/linux/sbitmap.h b/include/linux/sbitmap.h index 0dcc60e820de..841585f6e5f2 100644 --- a/include/linux/sbitmap.h +++ b/include/linux/sbitmap.h @@ -171,6 +171,8 @@ void sbitmap_resize(struct sbitmap *sb, unsigned int depth); * starting from the last allocated bit. This is less efficient * than the default behavior (false). * + * This operation provides acquire barrier semantics if it succeeds. + * * Return: Non-negative allocated bit number if successful, -1 otherwise. */ int sbitmap_get(struct sbitmap *sb, unsigned int alloc_hint, bool round_robin); @@ -300,6 +302,12 @@ static inline void sbitmap_clear_bit(struct sbitmap *sb, unsigned int bitnr) clear_bit(SB_NR_TO_BIT(sb, bitnr), __sbitmap_word(sb, bitnr)); } +static inline void sbitmap_clear_bit_unlock(struct sbitmap *sb, + unsigned int bitnr) +{ + clear_bit_unlock(SB_NR_TO_BIT(sb, bitnr), __sbitmap_word(sb, bitnr)); +} + static inline int sbitmap_test_bit(struct sbitmap *sb, unsigned int bitnr) { return test_bit(SB_NR_TO_BIT(sb, bitnr), __sbitmap_word(sb, bitnr)); diff --git a/lib/sbitmap.c b/lib/sbitmap.c index 42b5ca0acf93..e6a9c06ec70c 100644 --- a/lib/sbitmap.c +++ b/lib/sbitmap.c @@ -100,7 +100,7 @@ static int __sbitmap_get_word(unsigned long *word, unsigned long depth, return -1; } - if (!test_and_set_bit(nr, word)) + if (!test_and_set_bit_lock(nr, word)) break; hint = nr + 1; @@ -434,9 +434,9 @@ static void sbq_wake_up(struct sbitmap_queue *sbq) /* * Pairs with the memory barrier in set_current_state() to ensure the * proper ordering of clear_bit()/waitqueue_active() in the waker and - * test_and_set_bit()/prepare_to_wait()/finish_wait() in the waiter. See - * the comment on waitqueue_active(). This is __after_atomic because we - * just did clear_bit() in the caller. + * test_and_set_bit_lock()/prepare_to_wait()/finish_wait() in the + * waiter. See the comment on waitqueue_active(). This is __after_atomic + * because we just did clear_bit_unlock() in the caller. */ smp_mb__after_atomic(); @@ -469,7 +469,7 @@ static void sbq_wake_up(struct sbitmap_queue *sbq) void sbitmap_queue_clear(struct sbitmap_queue *sbq, unsigned int nr, unsigned int cpu) { - sbitmap_clear_bit(&sbq->sb, nr); + sbitmap_clear_bit_unlock(&sbq->sb, nr); sbq_wake_up(sbq); if (likely(!sbq->round_robin && nr < sbq->sb.depth)) *per_cpu_ptr(sbq->alloc_hint, cpu) = nr;