From patchwork Wed Feb 15 11:10:42 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hannes Reinecke X-Patchwork-Id: 9573863 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 0762A600F6 for ; Wed, 15 Feb 2017 11:10:48 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id ECF5828417 for ; Wed, 15 Feb 2017 11:10:47 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E1AA92846C; Wed, 15 Feb 2017 11:10: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=-6.9 required=2.0 tests=BAYES_00,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 8F98B28417 for ; Wed, 15 Feb 2017 11:10:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751617AbdBOLKq (ORCPT ); Wed, 15 Feb 2017 06:10:46 -0500 Received: from mx2.suse.de ([195.135.220.15]:39575 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751600AbdBOLKq (ORCPT ); Wed, 15 Feb 2017 06:10:46 -0500 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (charybdis-ext.suse.de [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id 82656AC17; Wed, 15 Feb 2017 11:10:44 +0000 (UTC) From: Hannes Reinecke To: Jens Axboe Cc: Omar Sandoval , linux-block@vger.kernel.org, Hannes Reinecke , Hannes Reinecke Subject: [PATCH] sbitmap: boundary checks for resized bitmap Date: Wed, 15 Feb 2017 12:10:42 +0100 Message-Id: <1487157042-5621-1-git-send-email-hare@suse.de> X-Mailer: git-send-email 1.8.5.6 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 If the sbitmap gets resized we need to ensure not to overflow the original allocation. And we should limit the search in sbitmap_any_bit_set() to the available depth to avoid looking into unused space. Signed-off-by: Hannes Reinecke --- include/linux/sbitmap.h | 5 +++++ lib/sbitmap.c | 12 +++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/include/linux/sbitmap.h b/include/linux/sbitmap.h index d4e0a20..5ed3815 100644 --- a/include/linux/sbitmap.h +++ b/include/linux/sbitmap.h @@ -61,6 +61,11 @@ struct sbitmap { unsigned int map_nr; /** + * @map_nr: Number of words (cachelines) allocated for the bitmap. + */ + unsigned int max_map_nr; + + /** * @map: Allocated bitmap. */ struct sbitmap_word *map; diff --git a/lib/sbitmap.c b/lib/sbitmap.c index 55e11c4..f6f18b7 100644 --- a/lib/sbitmap.c +++ b/lib/sbitmap.c @@ -44,7 +44,7 @@ int sbitmap_init_node(struct sbitmap *sb, unsigned int depth, int shift, sb->shift = shift; sb->depth = depth; - sb->map_nr = DIV_ROUND_UP(sb->depth, bits_per_word); + sb->map_nr = sb->max_map_nr = DIV_ROUND_UP(sb->depth, bits_per_word); if (depth == 0) { sb->map = NULL; @@ -70,7 +70,8 @@ void sbitmap_resize(struct sbitmap *sb, unsigned int depth) sb->depth = depth; sb->map_nr = DIV_ROUND_UP(sb->depth, bits_per_word); - + if (sb->map_nr > sb->max_map_nr) + sb->map_nr = sb->max_map_nr; for (i = 0; i < sb->map_nr; i++) { sb->map[i].depth = min(depth, bits_per_word); depth -= sb->map[i].depth; @@ -145,7 +146,11 @@ bool sbitmap_any_bit_set(const struct sbitmap *sb) unsigned int i; for (i = 0; i < sb->map_nr; i++) { - if (sb->map[i].word) + const struct sbitmap_word *word = &sb->map[i]; + unsigned long ret; + + ret = find_first_bit(&word->word, word->depth); + if (ret < word->depth) return true; } return false; @@ -187,6 +192,7 @@ void sbitmap_show(struct sbitmap *sb, struct seq_file *m) seq_printf(m, "busy=%u\n", sbitmap_weight(sb)); seq_printf(m, "bits_per_word=%u\n", 1U << sb->shift); seq_printf(m, "map_nr=%u\n", sb->map_nr); + seq_printf(m, "max_map_nr=%u\n", sb->max_map_nr); } EXPORT_SYMBOL_GPL(sbitmap_show);