From patchwork Sat Dec 12 09:34:05 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Syed Nayyar Waris X-Patchwork-Id: 11970051 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=-12.2 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED,DKIM_SIGNED,DKIM_VALID,FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_SANE_1 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 7423DC433FE for ; Sat, 12 Dec 2020 09:35:46 +0000 (UTC) Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 31F4923D50 for ; Sat, 12 Dec 2020 09:35:46 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 31F4923D50 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:References:Message-ID: Subject:To:From:Date:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=UhraSHWmH3W0SE08si42twvckilZtMBBwopZRv9pUgo=; b=O3SNZ31zPxW9jC9z9Zzk/2oBO 45Ru+9hQFRvj0cZInKBlXt1+mjqJOAtWmBHD2OL9W26YEsdPVsYRE6TlT5VC8R8ETnXq72q/udlYW 8y8mjeq6Mf+4yhfDZ3Y/SFAI4R4zc6rklpCdbVUsw6LXkxOfJt5foluoBYuwjGNDl7yzZoB1406/0 cyy5u7Ih6wtC4xsVrT/N4jGqCzYFK9EChpFfBkeU37NyGORY3LrPCnbU/Q5ZcJ1jHa/5lRQekOjM/ IvSUfwp7L6bKl2WLzLHf8sJVsEj63yzJUbjREq13prhkCOfxosmbpugCxXIqk6OF8Y2Y3a0oToSim QIMsKtuTQ==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1ko1IM-0002UR-Qr; Sat, 12 Dec 2020 09:34:30 +0000 Received: from mail-pj1-x1043.google.com ([2607:f8b0:4864:20::1043]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1ko1IK-0002Th-As for linux-arm-kernel@lists.infradead.org; Sat, 12 Dec 2020 09:34:29 +0000 Received: by mail-pj1-x1043.google.com with SMTP id m5so3749516pjv.5 for ; Sat, 12 Dec 2020 01:34:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=WXcPd+483PEznyO4jvnkSGgGlYQfHXVeIHWzyQm2ncM=; b=Wl9vC+4ELxkCwMr9bDvZvx+KC5THZHjhPm3tmWD+M9RLgj5b8YQCFKEN7ze6cSmxoI sTgBHjeObpO7qB+0emlQqVko33ww1S/EYG0ucEwj4/LyZXKLy5o749fH6K3syw195Lcn ny35VKCZtysJpuLzDANZO9uBuAKAaRLia4/A1HBDTUrkARc4xtjjGUH01L26T1gQS/KX Ej3iX+Mm7NEjX8Wm3d0nzAjLL6N8auiVwizIFqCZsImH6wp3Tn0G2Z0TVhooQ2e/k2dc 0w3QXvxxUWeeQkTObQ9MVg13gl4qzA8dNkI1/+xyDSOoSChGiq4g6FMu2PKR9NNHB626 YRDQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=WXcPd+483PEznyO4jvnkSGgGlYQfHXVeIHWzyQm2ncM=; b=ZLIyCoZ7OReNZkNXcP9VjsUYkWgy/phcYyhS4aDih6ACfK+4tTbdsf8qQjl8DQGpXE 8N1X1vzzv/dQ7t2rTkQPDxS9dJUMlORSAKA7+4o9CZhggfSchy/bAl7yNusBQEr6No8x lxamXivbfOccKTRd87777CC0BrPdSHy8YqhqkBtbw2UozG7GiiDaBBc7nHbNdgVFIP11 WDoAIc/qbSXNX7GBus6dm/lj6Y99L9UeI4ZnB2gH5oN+tTdSaDAE8qTUZuytRa8dnQLc rPhLCYrJeoOWw+1qfviFEWJy5b6gJgh/hIU1Nce21/EW27MYQn8QHRI3rcDEDa1S28Mu j6eg== X-Gm-Message-State: AOAM5312aBuBtHN3ABRSg18BMUZ2Lh+GR6phMle4TVypRfj8C9hyhb2B HUqHoeDtue/Fq9zA387dD7A= X-Google-Smtp-Source: ABdhPJwxtpcsd0Ah66k1CFJHGyjxaH4DBV2uGAmTP6q9XwnXC7BsGcXL0R7gLJlD3YYYYtQTDgcz+g== X-Received: by 2002:a17:90a:dc07:: with SMTP id i7mr16611865pjv.163.1607765664582; Sat, 12 Dec 2020 01:34:24 -0800 (PST) Received: from syed ([106.202.80.219]) by smtp.gmail.com with ESMTPSA id s24sm8063609pfh.47.2020.12.12.01.34.16 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 12 Dec 2020 01:34:24 -0800 (PST) Date: Sat, 12 Dec 2020 15:04:05 +0530 From: Syed Nayyar Waris To: linus.walleij@linaro.org, akpm@linux-foundation.org Subject: [PATCH v2 1/2] bitmap: Modify bitmap_set_value() to check bitmap length Message-ID: <20268bfeb500ad8819e3a11aa1bea27eade4fd39.1607765147.git.syednwaris@gmail.com> References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.24 (2015-08-30) X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20201212_043428_404384_4674FFBA X-CRM114-Status: GOOD ( 20.70 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-arch@vger.kernel.org, amit.kucheria@verdurent.com, arnd@arndb.de, yamada.masahiro@socionext.com, linux-kernel@vger.kernel.org, linus.walleij@linaro.org, daniel.lezcano@linaro.org, vilhelm.gray@gmail.com, michal.simek@xilinx.com, bgolaszewski@baylibre.com, rrichter@marvell.com, linux-gpio@vger.kernel.org, linux-pm@vger.kernel.org, rui.zhang@intel.com, andriy.shevchenko@linux.intel.com, linux-arm-kernel@lists.infradead.org Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Add explicit check to see if the value being written into the bitmap does not fall outside the bitmap. The situation that it is falling outside would never be possible in the code because the boundaries are required to be correct before the function is called. The responsibility is on the caller for ensuring the boundaries are correct. The code change is simply to silence the GCC warning messages because GCC is not aware that the boundaries have already been checked. As such, we're better off using __builtin_unreachable() here because we can avoid the latency of the conditional check entirely. Cc: Arnd Bergmann Signed-off-by: Syed Nayyar Waris Acked-by: William Breathitt Gray lib/test_bitmap.c: Modify for_each_set_clump test Modify the test where bitmap_set_value() is called. bitmap_set_value() now takes an extra bitmap-width as second argument and the width of value is now present as the fourth argument. Signed-off-by: Syed Nayyar Waris gpio: xilinx: Modify bitmap_set_value() calls Modify the bitmap_set_value() calls. bitmap_set_value() now takes an extra bitmap width as second argument and the width of value is now present as the fourth argument. Cc: Michal Simek Signed-off-by: Syed Nayyar Waris --- drivers/gpio/gpio-xilinx.c | 12 ++++++------ include/linux/bitmap.h | 35 +++++++++++++++++++++-------------- lib/test_bitmap.c | 4 ++-- 3 files changed, 29 insertions(+), 22 deletions(-) diff --git a/drivers/gpio/gpio-xilinx.c b/drivers/gpio/gpio-xilinx.c index ad4ee4145db4..05dae086c4d0 100644 --- a/drivers/gpio/gpio-xilinx.c +++ b/drivers/gpio/gpio-xilinx.c @@ -151,16 +151,16 @@ static void xgpio_set_multiple(struct gpio_chip *gc, unsigned long *mask, spin_lock_irqsave(&chip->gpio_lock[0], flags); spin_lock(&chip->gpio_lock[1]); - bitmap_set_value(old, state[0], 0, width[0]); - bitmap_set_value(old, state[1], width[0], width[1]); + bitmap_set_value(old, 64, state[0], width[0], 0); + bitmap_set_value(old, 64, state[1], width[1], width[0]); bitmap_replace(new, old, bits, mask, gc->ngpio); - bitmap_set_value(old, state[0], 0, 32); - bitmap_set_value(old, state[1], 32, 32); + bitmap_set_value(old, 64, state[0], 32, 0); + bitmap_set_value(old, 64, state[1], 32, 32); state[0] = bitmap_get_value(new, 0, width[0]); state[1] = bitmap_get_value(new, width[0], width[1]); - bitmap_set_value(new, state[0], 0, 32); - bitmap_set_value(new, state[1], 32, 32); + bitmap_set_value(new, 64, state[0], 32, 0); + bitmap_set_value(new, 64, state[1], 32, 32); bitmap_xor(changed, old, new, 64); if (((u32 *)changed)[0]) diff --git a/include/linux/bitmap.h b/include/linux/bitmap.h index 386d08777342..efb6199ea1e7 100644 --- a/include/linux/bitmap.h +++ b/include/linux/bitmap.h @@ -78,8 +78,9 @@ * bitmap_get_value(map, start, nbits) Get bit value of size * 'nbits' from map at start * bitmap_set_value8(map, value, start) Set 8bit value to map at start - * bitmap_set_value(map, value, start, nbits) Set bit value of size 'nbits' - * of map at start + * bitmap_set_value(map, nbits, value, value_width, start) + * Set bit value of size value_width + * to map at start * * Note, bitmap_zero() and bitmap_fill() operate over the region of * unsigned longs, that is, bits behind bitmap till the unsigned long @@ -610,30 +611,36 @@ static inline void bitmap_set_value8(unsigned long *map, unsigned long value, } /** - * bitmap_set_value - set n-bit value within a memory region + * bitmap_set_value - set value within a memory region * @map: address to the bitmap memory region - * @value: value of nbits - * @start: bit offset of the n-bit value - * @nbits: size of value in bits (must be between 1 and BITS_PER_LONG inclusive). + * @nbits: size of map in bits + * @value: value of clump + * @value_width: size of value in bits (must be between 1 and BITS_PER_LONG inclusive) + * @start: bit offset of the value */ -static inline void bitmap_set_value(unsigned long *map, - unsigned long value, - unsigned long start, unsigned long nbits) +static inline void bitmap_set_value(unsigned long *map, unsigned long nbits, + unsigned long value, unsigned long value_width, + unsigned long start) { - const size_t index = BIT_WORD(start); + const unsigned long index = BIT_WORD(start); + const unsigned long length = BIT_WORD(nbits); const unsigned long offset = start % BITS_PER_LONG; const unsigned long ceiling = round_up(start + 1, BITS_PER_LONG); const unsigned long space = ceiling - start; - value &= GENMASK(nbits - 1, 0); + value &= GENMASK(value_width - 1, 0); - if (space >= nbits) { - map[index] &= ~(GENMASK(nbits - 1, 0) << offset); + if (space >= value_width) { + map[index] &= ~(GENMASK(value_width - 1, 0) << offset); map[index] |= value << offset; } else { map[index + 0] &= ~BITMAP_FIRST_WORD_MASK(start); map[index + 0] |= value << offset; - map[index + 1] &= ~BITMAP_LAST_WORD_MASK(start + nbits); + + if (index + 1 >= length) + __builtin_unreachable(); + + map[index + 1] &= ~BITMAP_LAST_WORD_MASK(start + value_width); map[index + 1] |= value >> space; } } diff --git a/lib/test_bitmap.c b/lib/test_bitmap.c index 1c5791ff02cb..7fafe6a0bc08 100644 --- a/lib/test_bitmap.c +++ b/lib/test_bitmap.c @@ -656,8 +656,8 @@ static void __init prepare_test_data(unsigned int index) unsigned long width = 0; for (i = 0; i < clump_test_data[index].count; i++) { - bitmap_set_value(clump_test_data[index].data, - clump_bitmap_data[(clump_test_data[index].offset)++], width, 32); + bitmap_set_value(clump_test_data[index].data, 256, + clump_bitmap_data[(clump_test_data[index].offset)++], 32, width); width += 32; } }