From patchwork Fri Nov 30 01:12:32 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Axboe X-Patchwork-Id: 10705619 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 4599813BB for ; Fri, 30 Nov 2018 01:12:43 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 36F4A2FB36 for ; Fri, 30 Nov 2018 01:12:43 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2AC942FB57; Fri, 30 Nov 2018 01:12:43 +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=-7.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,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 C164B2FB36 for ; Fri, 30 Nov 2018 01:12:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726609AbeK3MUL (ORCPT ); Fri, 30 Nov 2018 07:20:11 -0500 Received: from mail-pf1-f196.google.com ([209.85.210.196]:41423 "EHLO mail-pf1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726525AbeK3MUK (ORCPT ); Fri, 30 Nov 2018 07:20:10 -0500 Received: by mail-pf1-f196.google.com with SMTP id b7so1914545pfi.8 for ; Thu, 29 Nov 2018 17:12:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel-dk.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=/BkC37cR1BMAuLA3V/wheSuC8+j44tyzekubWlTVNpI=; b=P4jNQATXG1Prp032p2SjvXDRmy2lrykiWSTY28cOMfr9lPMydgIkrARobvl6/5WTIW 77cU8XXOHox+utu2Xcr7nh5EhX1ecLEp84aNn9LhCNxVTvlrzfzg8NowSv91IfYYYjgR 7Tg8QvtncJFm2hXwaqTLsJc1rvQcxHpb+3O9K9vSwebHG6+zeYKA17yu7a5GrMohg1GL pCewvG4QJnJOnm1+FnZ/XmhsKbS0Q2GpjfwOrcwowZRm0cDrA2RI8bSQwwo0pMXX6ZqU yBiiQtmaYXPfl0kBiEJWShZShLYFSAMp0dTbGahC7bCMiPXu4ukg5UR/hVqv+wdYF4gv dtwQ== 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=/BkC37cR1BMAuLA3V/wheSuC8+j44tyzekubWlTVNpI=; b=S2mJyc+nALeQZkts9dVbullK0cCfvCU4zN3NFvxz+M4r/y//s3G1+mG154GfUCUbNp qsm6PBm7U/AQoZIVUvFhUyWnkTC5NXz5CleGy9JgGS7ZkuysbKnfHWqjj2e0m40qY+io Ho8vavU8B7q52UBpuuFuqLEQsv4AK4w/nSSgPzhNqS8zrvWWw1JIt6J3Z+I7w/a7FpFn I5xEybW+9rhMyxMWXhgdKyjIv/xSzvPFikSSwvEyn+lTfjBaxutgjtjjgtMB2CXGvZLr PE9h2zmGacdS+y75P8HU40etDt7Q8LO4tz9Vp7638Rar5HnWZmD+tAfuCJyi2O6C5np3 xh/w== X-Gm-Message-State: AA+aEWa4xGiF2bXDrlEF9N0hMyDkNlfsGWN+e/IKSL1kLc9f6Fse0NuX OgXS9sDpROPWzKkkCyHg764y04Spt0U= X-Google-Smtp-Source: AFSGD/VfjGXOZ0vx0q2vBnTBXcY2gCBn56c9acSYPDadX54A7CWxPCPYeZWocRz9ydB+36fiZ3hXlw== X-Received: by 2002:a62:56c7:: with SMTP id h68mr3830421pfj.134.1543540360738; Thu, 29 Nov 2018 17:12:40 -0800 (PST) Received: from x1.localdomain (66.29.188.166.static.utbb.net. [66.29.188.166]) by smtp.gmail.com with ESMTPSA id v15sm4841749pfn.94.2018.11.29.17.12.39 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 29 Nov 2018 17:12:39 -0800 (PST) From: Jens Axboe To: linux-block@vger.kernel.org, osandov@osandov.com Cc: Jens Axboe Subject: [PATCH 1/3] sbitmap: ensure that sbitmap maps are properly aligned Date: Thu, 29 Nov 2018 18:12:32 -0700 Message-Id: <20181130011234.32674-2-axboe@kernel.dk> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20181130011234.32674-1-axboe@kernel.dk> References: <20181130011234.32674-1-axboe@kernel.dk> 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 We try to be careful with alignment for cache purposes, but all of that is worthless if we don't actually align the maps themselves. Signed-off-by: Jens Axboe --- include/linux/sbitmap.h | 11 ++++++++--- lib/sbitmap.c | 7 +++++-- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/include/linux/sbitmap.h b/include/linux/sbitmap.h index 804a50983ec5..5cb1755d32da 100644 --- a/include/linux/sbitmap.h +++ b/include/linux/sbitmap.h @@ -63,9 +63,14 @@ struct sbitmap { unsigned int map_nr; /** - * @map: Allocated bitmap. + * @map: Aligned allocated bitmap. */ struct sbitmap_word *map; + + /** + * @map_ptr: Originally allocated map pointer + */ + void *map_ptr; }; #define SBQ_WAIT_QUEUES 8 @@ -157,8 +162,8 @@ int sbitmap_init_node(struct sbitmap *sb, unsigned int depth, int shift, */ static inline void sbitmap_free(struct sbitmap *sb) { - kfree(sb->map); - sb->map = NULL; + kfree(sb->map_ptr); + sb->map_ptr = sb->map = NULL; } /** diff --git a/lib/sbitmap.c b/lib/sbitmap.c index 45cab6bbc1c7..21e776e3128d 100644 --- a/lib/sbitmap.c +++ b/lib/sbitmap.c @@ -25,6 +25,7 @@ int sbitmap_init_node(struct sbitmap *sb, unsigned int depth, int shift, { unsigned int bits_per_word; unsigned int i; + size_t size; if (shift < 0) { shift = ilog2(BITS_PER_LONG); @@ -52,9 +53,11 @@ int sbitmap_init_node(struct sbitmap *sb, unsigned int depth, int shift, return 0; } - sb->map = kcalloc_node(sb->map_nr, sizeof(*sb->map), flags, node); - if (!sb->map) + size = sb->map_nr * sizeof(*sb->map) + L1_CACHE_BYTES - 1; + sb->map_ptr = kzalloc_node(size, flags, node); + if (!sb->map_ptr) return -ENOMEM; + sb->map = PTR_ALIGN(sb->map_ptr, L1_CACHE_BYTES); for (i = 0; i < sb->map_nr; i++) { sb->map[i].depth = min(depth, bits_per_word); From patchwork Fri Nov 30 01:12:33 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Axboe X-Patchwork-Id: 10705621 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 159D213BF for ; Fri, 30 Nov 2018 01:12:45 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0894F2FB42 for ; Fri, 30 Nov 2018 01:12:45 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id F0B6E2FB6A; Fri, 30 Nov 2018 01:12:44 +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=-7.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,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 77CA92FB42 for ; Fri, 30 Nov 2018 01:12:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726652AbeK3MUM (ORCPT ); Fri, 30 Nov 2018 07:20:12 -0500 Received: from mail-pl1-f195.google.com ([209.85.214.195]:43846 "EHLO mail-pl1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726525AbeK3MUM (ORCPT ); Fri, 30 Nov 2018 07:20:12 -0500 Received: by mail-pl1-f195.google.com with SMTP id gn14so1913673plb.10 for ; Thu, 29 Nov 2018 17:12:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel-dk.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=FL64amkHfziKUXg291XJEzD6bPujZYHM+eAGmoCvs38=; b=L4lKV+HDaFtMwQ7sBWg1gSpfTnQwtqhHeJITHqMJ4OJrjalYqS8fLYy2n+fu7OoRNj Im6AO9J7dCiYjlRmHXwVM+qdTMa+06+109SrKoqRuYYNiNiQEOX7y5tPxS4or8mStuw0 QjwCB9Okx1FnFUcRRR62YjNW+MUmFp8Ri4HSnvZmCT9kHIUzbygUceAA2azxDR8ZGfC1 aXzaMrh1ro0blqXlMxuGEi6nG3NwQc8RFFPUaLIV/erdduqfQctYCmTrcXWk/JpqBV6m UxxhGd0d8KtJ15zDn61awAVaWKjb5XaXFQqU4jpkwaxsbFjh7r26ToRAEt/9iF7C/oW5 emSg== 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=FL64amkHfziKUXg291XJEzD6bPujZYHM+eAGmoCvs38=; b=s7mnUyfCrkMgQ8DTIWpo4YEHcAAcaW82gMIRfJXwLZ1wiuj4HPfCGStMsqmz/PZbMA +HoKZQ05x/1ULiiASDH6ltjyDO5UcgP5sinXP3dy7+UtugFclDkF8OZbqX9OMmMVSrgO jo+KrX1jranpmPKnXhixNGSqtmlyFuH/W4n84wIR4elRUUllOw5TODzmR2dTB91LlHz9 VhwK83MUUiGKUKLty0K38XTyqu5OKzUAUzF6gEcqlVi2UZnb5PDcR7rQzJTglGspkvXg WcoGdGrgghHMf/NNPAcbHc01SzruD9gqjt/bXwQR1a8zkM8L0GgvwjbTM3OllIhfOi4p SLww== X-Gm-Message-State: AA+aEWYLlhSfZbdPbBJZSyqiO7bSOZfdE9QiM+mwumx+5LHrRqYZk96v YVAZXX+d+mkaGnpeVQFh/jCkulzws5g= X-Google-Smtp-Source: AFSGD/UoalAz4uVrZgM3REBPPeMOZUtAYXW/QAAmc9y/URUcn0TfBHaZAUdDwGt7QJfnoYEnfO95+Q== X-Received: by 2002:a17:902:2a66:: with SMTP id i93mr3615008plb.113.1543540362737; Thu, 29 Nov 2018 17:12:42 -0800 (PST) Received: from x1.localdomain (66.29.188.166.static.utbb.net. [66.29.188.166]) by smtp.gmail.com with ESMTPSA id v15sm4841749pfn.94.2018.11.29.17.12.40 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 29 Nov 2018 17:12:41 -0800 (PST) From: Jens Axboe To: linux-block@vger.kernel.org, osandov@osandov.com Cc: Jens Axboe Subject: [PATCH 2/3] sbitmap: ammortize cost of clearing bits Date: Thu, 29 Nov 2018 18:12:33 -0700 Message-Id: <20181130011234.32674-3-axboe@kernel.dk> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20181130011234.32674-1-axboe@kernel.dk> References: <20181130011234.32674-1-axboe@kernel.dk> 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 sbitmap maintains a set of words that we use to set and clear bits, with each bit representing a tag for blk-mq. Even though we spread the bits out and maintain a hint cache, one particular bit allocated will end up being cleared in the exact same spot. This introduces batched clearing of bits. Instead of clearing a given bit, the same bit is set in a cleared/free mask instead. If we fail allocating a bit from a given word, then we check the free mask, and batch move those cleared bits at that time. This trades 64 atomic bitops for 2 cmpxchg(). In a threaded poll test case, half the overhead of getting and clearing tags is removed with this change. On another poll test case with a single thread, performance is unchanged. Signed-off-by: Jens Axboe --- include/linux/sbitmap.h | 26 +++++++++++++++--- lib/sbitmap.c | 60 ++++++++++++++++++++++++++++++++++++++--- 2 files changed, 78 insertions(+), 8 deletions(-) diff --git a/include/linux/sbitmap.h b/include/linux/sbitmap.h index 5cb1755d32da..13eb8973bd10 100644 --- a/include/linux/sbitmap.h +++ b/include/linux/sbitmap.h @@ -30,14 +30,19 @@ struct seq_file; */ struct sbitmap_word { /** - * @word: The bitmap word itself. + * @depth: Number of bits being used in @word/@cleared */ - unsigned long word; + unsigned long depth; /** - * @depth: Number of bits being used in @word. + * @word: word holding free bits */ - unsigned long depth; + unsigned long word ____cacheline_aligned_in_smp; + + /** + * @cleared: word holding cleared bits + */ + unsigned long cleared ____cacheline_aligned_in_smp; } ____cacheline_aligned_in_smp; /** @@ -315,6 +320,19 @@ static inline void sbitmap_clear_bit(struct sbitmap *sb, unsigned int bitnr) clear_bit(SB_NR_TO_BIT(sb, bitnr), __sbitmap_word(sb, bitnr)); } +/* + * This one is special, since it doesn't actually clear the bit, rather it + * sets the corresponding bit in the ->cleared mask instead. Paired with + * the caller doing sbitmap_batch_clear() if a given index is full, which + * will clear the previously freed entries in the corresponding ->word. + */ +static inline void sbitmap_deferred_clear_bit(struct sbitmap *sb, unsigned int bitnr) +{ + unsigned long *addr = &sb->map[SB_NR_TO_INDEX(sb, bitnr)].cleared; + + set_bit(SB_NR_TO_BIT(sb, bitnr), addr); +} + static inline void sbitmap_clear_bit_unlock(struct sbitmap *sb, unsigned int bitnr) { diff --git a/lib/sbitmap.c b/lib/sbitmap.c index 21e776e3128d..04db31f4dfda 100644 --- a/lib/sbitmap.c +++ b/lib/sbitmap.c @@ -114,6 +114,58 @@ static int __sbitmap_get_word(unsigned long *word, unsigned long depth, return nr; } +/* + * See if we have deferred clears that we can batch move + */ +static inline bool sbitmap_deferred_clear(struct sbitmap *sb, int index) +{ + unsigned long mask, val; + + if (!sb->map[index].cleared) + return false; + + /* + * First get a stable cleared mask, setting the old mask to 0. + */ + do { + mask = sb->map[index].cleared; + } while (cmpxchg(&sb->map[index].cleared, mask, 0) != mask); + + /* + * Now clear the masked bits in our free word + */ + do { + val = sb->map[index].word; + } while (cmpxchg(&sb->map[index].word, val, val & ~mask) != val); + + /* + * If someone found ->cleared == 0 before we wrote ->word, then + * they could have failed when they should not have. Check for + * waiters. + */ + smp_mb__after_atomic(); + sbitmap_queue_wake_up(container_of(sb, struct sbitmap_queue, sb)); + return true; +} + +static int sbitmap_find_bit_in_index(struct sbitmap *sb, int index, + unsigned int alloc_hint, bool round_robin) +{ + int nr; + + do { + nr = __sbitmap_get_word(&sb->map[index].word, + sb->map[index].depth, alloc_hint, + !round_robin); + if (nr != -1) + break; + if (!sbitmap_deferred_clear(sb, index)) + break; + } while (1); + + return nr; +} + int sbitmap_get(struct sbitmap *sb, unsigned int alloc_hint, bool round_robin) { unsigned int i, index; @@ -132,9 +184,8 @@ int sbitmap_get(struct sbitmap *sb, unsigned int alloc_hint, bool round_robin) alloc_hint = 0; for (i = 0; i < sb->map_nr; i++) { - nr = __sbitmap_get_word(&sb->map[index].word, - sb->map[index].depth, alloc_hint, - !round_robin); + nr = sbitmap_find_bit_in_index(sb, index, alloc_hint, + round_robin); if (nr != -1) { nr += index << sb->shift; break; @@ -517,7 +568,8 @@ EXPORT_SYMBOL_GPL(sbitmap_queue_wake_up); void sbitmap_queue_clear(struct sbitmap_queue *sbq, unsigned int nr, unsigned int cpu) { - sbitmap_clear_bit_unlock(&sbq->sb, nr); + sbitmap_deferred_clear_bit(&sbq->sb, nr); + /* * Pairs with the memory barrier in set_current_state() to ensure the * proper ordering of clear_bit_unlock()/waitqueue_active() in the waker From patchwork Fri Nov 30 01:12:34 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Axboe X-Patchwork-Id: 10705623 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 5DD3B13BF for ; Fri, 30 Nov 2018 01:12:47 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4F61B2FB65 for ; Fri, 30 Nov 2018 01:12:47 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 432122FB71; Fri, 30 Nov 2018 01:12:47 +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=-7.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,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 B9D272FB65 for ; Fri, 30 Nov 2018 01:12:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726696AbeK3MUP (ORCPT ); Fri, 30 Nov 2018 07:20:15 -0500 Received: from mail-pf1-f195.google.com ([209.85.210.195]:41430 "EHLO mail-pf1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726525AbeK3MUO (ORCPT ); Fri, 30 Nov 2018 07:20:14 -0500 Received: by mail-pf1-f195.google.com with SMTP id b7so1914632pfi.8 for ; Thu, 29 Nov 2018 17:12:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel-dk.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=s2isTtU6ZW1+uv3+wCp2lDXoJRGJAYdxg3nwsKFtDNI=; b=1DCXmHQ8jOcueTJ8+ei63X9WEHVoy50JWYwhXskieIeY0j4vfKtMOackNwm+TyPbhW n4WHwkwRuTZyvb8vDOCeqRIfVZawzYlNMv2o46Ld+vlLx/cg0UjiMVYBQFY0yLYwyewg acGqmzxFMdHWpuu5JYGaDVfnwanIW39UWHs3UDbbWSWVbRyMiB7Ml8UHryUrLHFmuypD usUGIoWXmhAkUyQMNoERK4Bd+pyThGXPpQt4lfnC5YATmcywhYdRP/vp6nkNOEpvB9rg gFuxQ3HZC0igLF+Tz2qsIpkyMMP4SjeUCOGZdlhBRcgvvnriGR4uRxlof7gg2bvMJBbk 3bsA== 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=s2isTtU6ZW1+uv3+wCp2lDXoJRGJAYdxg3nwsKFtDNI=; b=cHxv1YK/6pYfmRlZpkmVWkyj0wGITKwJFXks0LBgXoZVfEZrGcYySA2Dazj9xOZqwc wUge4N5T27zFjMd+7HB42Scx3/q2R9Q6gHJS6xo+o/0ln/sYuabJO3Lpgev/4aRzTi3E 5mpvsYxRa1eSVSVL3hV+05pwnsdC2laiRQvhe7bfgB/8D7DS0sd1k3gEObhASVxCsdaB aXYLOVCzlkX4Sx8Olg2e8P2AoPvnqmY0RuAAwJqYoAzwvzmglV3DL03T3cfqUAQ+aine GoJSsSZTVSIbHmXjbvX0ReVrDFaPIXCUbRKpa0OPYen8npmQJ5RuOhXMRNLeLXPchfDM qTJA== X-Gm-Message-State: AA+aEWbJyhns9X775Z74wd+7B1DcwExVeVS6mG0MbMXNEiIflmw34gl8 +/pTrGqy/FXu/5LwBsZJ/3wKxV3d02E= X-Google-Smtp-Source: AFSGD/VRH5oWhOJ5x/hs7R5Me7UD1EXay0Pg5Un8olN5J9633ptm9RWNy5gFdxIHBf/HZWdz127rgQ== X-Received: by 2002:a62:5793:: with SMTP id i19mr3679449pfj.49.1543540364222; Thu, 29 Nov 2018 17:12:44 -0800 (PST) Received: from x1.localdomain (66.29.188.166.static.utbb.net. [66.29.188.166]) by smtp.gmail.com with ESMTPSA id v15sm4841749pfn.94.2018.11.29.17.12.42 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 29 Nov 2018 17:12:43 -0800 (PST) From: Jens Axboe To: linux-block@vger.kernel.org, osandov@osandov.com Cc: Jens Axboe Subject: [PATCH 3/3] sbitmap: optimize wakeup check Date: Thu, 29 Nov 2018 18:12:34 -0700 Message-Id: <20181130011234.32674-4-axboe@kernel.dk> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20181130011234.32674-1-axboe@kernel.dk> References: <20181130011234.32674-1-axboe@kernel.dk> 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 Even if we have no waiters on any of the sbitmap_queue wait states, we still have to loop every entry to check. We do this for every IO, so the cost adds up. Shift a bit of the cost to the slow path, when we actually have waiters. Wrap prepare_to_wait_exclusive() and finish_wait(), so we can maintain an internal count of how many are currently active. Then we can simply check this count in sbq_wake_ptr() and not have to loop if we don't have any sleepers. Convert the two users of sbitmap with waiting, blk-mq-tag and iSCSI. Signed-off-by: Jens Axboe --- block/blk-mq-tag.c | 7 +++---- drivers/target/iscsi/iscsi_target_util.c | 8 +++++--- include/linux/sbitmap.h | 19 +++++++++++++++++++ lib/sbitmap.c | 21 +++++++++++++++++++++ 4 files changed, 48 insertions(+), 7 deletions(-) diff --git a/block/blk-mq-tag.c b/block/blk-mq-tag.c index 87bc5df72d48..66c3a1c887ed 100644 --- a/block/blk-mq-tag.c +++ b/block/blk-mq-tag.c @@ -154,8 +154,7 @@ unsigned int blk_mq_get_tag(struct blk_mq_alloc_data *data) if (tag != -1) break; - prepare_to_wait_exclusive(&ws->wait, &wait, - TASK_UNINTERRUPTIBLE); + sbitmap_prepare_to_wait(bt, ws, &wait, TASK_UNINTERRUPTIBLE); tag = __blk_mq_get_tag(data, bt); if (tag != -1) @@ -167,6 +166,8 @@ unsigned int blk_mq_get_tag(struct blk_mq_alloc_data *data) bt_prev = bt; io_schedule(); + sbitmap_finish_wait(bt, ws, &wait); + data->ctx = blk_mq_get_ctx(data->q); data->hctx = blk_mq_map_queue(data->q, data->cmd_flags, data->ctx->cpu); @@ -176,8 +177,6 @@ unsigned int blk_mq_get_tag(struct blk_mq_alloc_data *data) else bt = &tags->bitmap_tags; - finish_wait(&ws->wait, &wait); - /* * If destination hw queue is changed, fake wake up on * previous queue for compensating the wake up miss, so diff --git a/drivers/target/iscsi/iscsi_target_util.c b/drivers/target/iscsi/iscsi_target_util.c index 36b742932c72..d7d03d601732 100644 --- a/drivers/target/iscsi/iscsi_target_util.c +++ b/drivers/target/iscsi/iscsi_target_util.c @@ -152,13 +152,15 @@ static int iscsit_wait_for_tag(struct se_session *se_sess, int state, int *cpup) int tag = -1; DEFINE_WAIT(wait); struct sbq_wait_state *ws; + struct sbitmap_queue *sbq; if (state == TASK_RUNNING) return tag; - ws = &se_sess->sess_tag_pool.ws[0]; + sbq = &se_sess->sess_tag_pool; + ws = &sbq->ws[0]; for (;;) { - prepare_to_wait_exclusive(&ws->wait, &wait, state); + sbitmap_prepare_to_wait(sbq, ws, &wait, state); if (signal_pending_state(state, current)) break; tag = sbitmap_queue_get(&se_sess->sess_tag_pool, cpup); @@ -167,7 +169,7 @@ static int iscsit_wait_for_tag(struct se_session *se_sess, int state, int *cpup) schedule(); } - finish_wait(&ws->wait, &wait); + sbitmap_finish_wait(sbq, ws, &wait); return tag; } diff --git a/include/linux/sbitmap.h b/include/linux/sbitmap.h index 13eb8973bd10..dbfbac0c4daa 100644 --- a/include/linux/sbitmap.h +++ b/include/linux/sbitmap.h @@ -135,6 +135,11 @@ struct sbitmap_queue { */ struct sbq_wait_state *ws; + /* + * @ws_active: count of currently active ws waitqueues + */ + atomic_t ws_active; + /** * @round_robin: Allocate bits in strict round-robin order. */ @@ -554,4 +559,18 @@ void sbitmap_queue_wake_up(struct sbitmap_queue *sbq); */ void sbitmap_queue_show(struct sbitmap_queue *sbq, struct seq_file *m); +/* + * Wrapper around prepare_to_wait_exclusive(), which maintains some extra + * internal state. + */ +void sbitmap_prepare_to_wait(struct sbitmap_queue *sbq, + struct sbq_wait_state *ws, + struct wait_queue_entry *wait, int state); + +/* + * Must be paired with sbitmap_prepare_to_wait(). + */ +void sbitmap_finish_wait(struct sbitmap_queue *sbq, struct sbq_wait_state *ws, + struct wait_queue_entry *wait); + #endif /* __LINUX_SCALE_BITMAP_H */ diff --git a/lib/sbitmap.c b/lib/sbitmap.c index 04db31f4dfda..1cc21f916276 100644 --- a/lib/sbitmap.c +++ b/lib/sbitmap.c @@ -384,6 +384,7 @@ int sbitmap_queue_init_node(struct sbitmap_queue *sbq, unsigned int depth, sbq->min_shallow_depth = UINT_MAX; sbq->wake_batch = sbq_calc_wake_batch(sbq, depth); atomic_set(&sbq->wake_index, 0); + atomic_set(&sbq->ws_active, 0); sbq->ws = kzalloc_node(SBQ_WAIT_QUEUES * sizeof(*sbq->ws), flags, node); if (!sbq->ws) { @@ -499,6 +500,9 @@ static struct sbq_wait_state *sbq_wake_ptr(struct sbitmap_queue *sbq) { int i, wake_index; + if (!atomic_read(&sbq->ws_active)) + return NULL; + wake_index = atomic_read(&sbq->wake_index); for (i = 0; i < SBQ_WAIT_QUEUES; i++) { struct sbq_wait_state *ws = &sbq->ws[wake_index]; @@ -639,3 +643,20 @@ void sbitmap_queue_show(struct sbitmap_queue *sbq, struct seq_file *m) seq_printf(m, "min_shallow_depth=%u\n", sbq->min_shallow_depth); } EXPORT_SYMBOL_GPL(sbitmap_queue_show); + +void sbitmap_prepare_to_wait(struct sbitmap_queue *sbq, + struct sbq_wait_state *ws, + struct wait_queue_entry *wait, int state) +{ + atomic_inc(&sbq->ws_active); + prepare_to_wait_exclusive(&ws->wait, wait, state); +} +EXPORT_SYMBOL_GPL(sbitmap_prepare_to_wait); + +void sbitmap_finish_wait(struct sbitmap_queue *sbq, struct sbq_wait_state *ws, + struct wait_queue_entry *wait) +{ + finish_wait(&ws->wait, wait); + atomic_dec(&sbq->ws_active); +} +EXPORT_SYMBOL_GPL(sbitmap_finish_wait);