From patchwork Thu Oct 20 03:20:23 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yury Norov X-Patchwork-Id: 13012587 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 31321C4332F for ; Thu, 20 Oct 2022 03:26:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=1k/TI/wof4aS/3/gubrExo/utBF0WY99oAvyk4aI+ME=; b=H0Zk+xaw7MwnuC WTTmxnZ95khWkVIQzzGce6i6+acnpNbxwhWvvi0nqEtWRao861yOLTXIFCVoPfAJnibbRR4X5kMpL oCB2TP6qnYo/lY3UNHekdUQCoRYPEHtNBjsaI8KbTYfihvCg5eYD6nZIB1quDOIiAib601+ayBsUu 04YACbFZ+R824oJ0eZNQZ4q6GPG9cCQBaPAbeKUiS4QWc0u93YvY5/R3GJsRXfINR+OW6XJex61kL VY/BXnECN4Bzna3q7ALpIj/t9MP5cxgrqu5ZZhRJUc+ws8wj++a5ylBaFwdISxBcY1XwMS1sAge7R eGdKsU/zyZnoGj4HxdLQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1olMBL-009wbC-RI; Thu, 20 Oct 2022 03:25:20 +0000 Received: from desiato.infradead.org ([2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1olMBJ-009wap-Ui for linux-arm-kernel@bombadil.infradead.org; Thu, 20 Oct 2022 03:25:18 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Content-Transfer-Encoding:MIME-Version :References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description; bh=OczjChBmGkUsoBfYt2005Z8T2ggpJvwmMFUd3PcsLKA=; b=bEXzypo6IN1neUzRfGYfdBccc1 NxMImEYEGzwud2Q0hTDp3PnECFt3Oj52m4XKdAPBNuCwpv4+PawPoVZevd+0CvqCyr4f5Hav7YtJa M0Lbmbtwn8gXaAJ2tlGJUVDigoncof7vDvTSTajensvWH0LDFpXN2ziS+1JvVvwNGU26YzXRlKeYe rsK62a9vnSzuL1Lo3r9WE3VjUihx60VAKAHrW7Z6+hCRuEArLDNlayDfqYNcMRIYJugILug1DK1Xk uI5V4kXd5SzGtXb1Yy5alPExBr2AF5Co8GFrC9u9PRRh9Op9nPuHaAFQElg29CQnsdQlR+6yCJCRa xftnvJCg==; Received: from mail-oi1-x22a.google.com ([2607:f8b0:4864:20::22a]) by desiato.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1olMB3-004wOt-LI for linux-arm-kernel@lists.infradead.org; Thu, 20 Oct 2022 03:25:05 +0000 Received: by mail-oi1-x22a.google.com with SMTP id g130so21476636oia.13 for ; Wed, 19 Oct 2022 20:24:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=OczjChBmGkUsoBfYt2005Z8T2ggpJvwmMFUd3PcsLKA=; b=Tc1hvzFMF23lt+BFLnxI7WX4sMpYoLkFxJzxHhJETh1MSfcQuJmAP341e2KQwFTJsJ EmLReN2GxQD4lz92lcUHSQnuNWQr+mBaXao4mrC+eJMAnpohWbG/2WgPmvzOzZWHP9wH 6y5TEKgsLspmLNtSj9KNcWSRSj7Z8e8+O1Mq6mXoC8pfEJhSTLc1v96I+wIo4/qn6tsh Y/p71MWFrA/EB1d1oVFjdb10h2jTvoId9QBa67aCSbWtUNWbJSY/syidxU0LOd2jj0qi Z7ElXW8pg1UF7dTM0vhyEWbpSjd3/yZWoC4ApPo5xsHPCego3IonNbj+cWRTaZVgR0tl 1sxA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=OczjChBmGkUsoBfYt2005Z8T2ggpJvwmMFUd3PcsLKA=; b=1ZXYdwCc5smxvlqPMULbdEvX7yOzT92B4yCSDvNT8e4qbgGsnJ0wrsK4c8rr1TJzEW qpWOyfBl6W2pPlfJV5s0kPW1asP+GZN2Cj9CsGxvPTsYv3XKGSxV39v1rmb826tmekzv AbOqIx3awkBgnWJSv4oII//XFpmCKyjYxPwqijlIgTlsLu/KeJH5K4Yk/HVzjGPsfg8u D9TPKtc4XQJcPOmm6VEyD5EGnMoNZOo5m9g8q90MF9NGexeeW+KYGgUaw3c/+9lW4HcI JESvkBSZwlBuQV5YPs+ygH0pApmHRn4kJJNFl01NTRrzJQUMlc2BgbAjCsXMLekFpLK8 iNMw== X-Gm-Message-State: ACrzQf2iwN4L2BQWbv1zuBsav6lYJfcmAIqE018nM11YESiRnvdsZpVl YOEP94DMMrtRV+j2VYNEoKDOTFfYkSo= X-Google-Smtp-Source: AMsMyM4YQSPRdBzk2mRZPiUYN065g9bPt1mlN95Zv1RoRTX0R7OQlWZ+UIEQbpb+X8hjxt3dEjGzog== X-Received: by 2002:aca:df8b:0:b0:350:e9a8:1627 with SMTP id w133-20020acadf8b000000b00350e9a81627mr20314745oig.68.1666236295217; Wed, 19 Oct 2022 20:24:55 -0700 (PDT) Received: from localhost ([12.97.180.36]) by smtp.gmail.com with ESMTPSA id y3-20020a056870418300b0011f400edb17sm8450627oac.4.2022.10.19.20.24.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Oct 2022 20:24:54 -0700 (PDT) From: Yury Norov To: "Russell King (Oracle)" , Catalin Marinas , Mark Rutland , Will Deacon , linux-arm-kernel@lists.infradead.org Cc: Yury Norov , Linux Kernel Mailing List , Alexey Klimov , Andy Shevchenko , Andy Whitcroft , Dennis Zhou , Geert Uytterhoeven , Guenter Roeck , Kees Cook , Linus Torvalds , Rasmus Villemoes Subject: [PATCH 1/2] bitmap: add sanity check function for find_bit() Date: Wed, 19 Oct 2022 20:20:23 -0700 Message-Id: <20221020032024.1804535-2-yury.norov@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221020032024.1804535-1-yury.norov@gmail.com> References: <20221020032024.1804535-1-yury.norov@gmail.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20221020_042502_503096_E2385127 X-CRM114-Status: GOOD ( 15.56 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org find_bit() requires a pointer aligned to it's size. However some subsystems (fs, for example) cast char* variables to unsigned long* before passing them to find_bit(). Many architectures allow unaligned pointers with the cost of performance degradation. This patch adds runtime check for the pointers to be aligned. Signed-off-by: Yury Norov --- include/linux/find.h | 35 +++++++++++++++++++++++++++++++++++ lib/Kconfig.debug | 7 +++++++ 2 files changed, 42 insertions(+) diff --git a/include/linux/find.h b/include/linux/find.h index ccaf61a0f5fd..2d8f5419d787 100644 --- a/include/linux/find.h +++ b/include/linux/find.h @@ -7,6 +7,7 @@ #endif #include +#include unsigned long _find_next_bit(const unsigned long *addr1, unsigned long nbits, unsigned long start); @@ -35,6 +36,14 @@ unsigned long _find_next_bit_le(const unsigned long *addr, unsigned long size, unsigned long offset); #endif +static __always_inline +void check_find_bit(const unsigned long *addr) +{ +#ifdef CONFIG_DEBUG_BITMAP + WARN_ON_ONCE(!IS_ALIGNED((unsigned long)addr, sizeof(unsigned long))); +#endif +} + #ifndef find_next_bit /** * find_next_bit - find the next set bit in a memory region @@ -49,6 +58,8 @@ static inline unsigned long find_next_bit(const unsigned long *addr, unsigned long size, unsigned long offset) { + check_find_bit(addr); + if (small_const_nbits(size)) { unsigned long val; @@ -79,6 +90,9 @@ unsigned long find_next_and_bit(const unsigned long *addr1, const unsigned long *addr2, unsigned long size, unsigned long offset) { + check_find_bit(addr1); + check_find_bit(addr2); + if (small_const_nbits(size)) { unsigned long val; @@ -138,6 +152,8 @@ static inline unsigned long find_next_zero_bit(const unsigned long *addr, unsigned long size, unsigned long offset) { + check_find_bit(addr); + if (small_const_nbits(size)) { unsigned long val; @@ -164,6 +180,8 @@ unsigned long find_next_zero_bit(const unsigned long *addr, unsigned long size, static inline unsigned long find_first_bit(const unsigned long *addr, unsigned long size) { + check_find_bit(addr); + if (small_const_nbits(size)) { unsigned long val = *addr & GENMASK(size - 1, 0); @@ -270,6 +288,9 @@ unsigned long find_first_and_bit(const unsigned long *addr1, const unsigned long *addr2, unsigned long size) { + check_find_bit(addr1); + check_find_bit(addr2); + if (small_const_nbits(size)) { unsigned long val = *addr1 & *addr2 & GENMASK(size - 1, 0); @@ -292,6 +313,8 @@ unsigned long find_first_and_bit(const unsigned long *addr1, static inline unsigned long find_first_zero_bit(const unsigned long *addr, unsigned long size) { + check_find_bit(addr); + if (small_const_nbits(size)) { unsigned long val = *addr | ~GENMASK(size - 1, 0); @@ -313,6 +336,8 @@ unsigned long find_first_zero_bit(const unsigned long *addr, unsigned long size) static inline unsigned long find_last_bit(const unsigned long *addr, unsigned long size) { + check_find_bit(addr); + if (small_const_nbits(size)) { unsigned long val = *addr & GENMASK(size - 1, 0); @@ -417,18 +442,24 @@ extern unsigned long find_next_clump8(unsigned long *clump, static inline unsigned long find_next_zero_bit_le(const void *addr, unsigned long size, unsigned long offset) { + check_find_bit(addr); + return find_next_zero_bit(addr, size, offset); } static inline unsigned long find_next_bit_le(const void *addr, unsigned long size, unsigned long offset) { + check_find_bit(addr); + return find_next_bit(addr, size, offset); } static inline unsigned long find_first_zero_bit_le(const void *addr, unsigned long size) { + check_find_bit(addr); + return find_first_zero_bit(addr, size); } @@ -439,6 +470,8 @@ static inline unsigned long find_next_zero_bit_le(const void *addr, unsigned long size, unsigned long offset) { + check_find_bit(addr); + if (small_const_nbits(size)) { unsigned long val = *(const unsigned long *)addr; @@ -472,6 +505,8 @@ static inline unsigned long find_next_bit_le(const void *addr, unsigned long size, unsigned long offset) { + check_find_bit(addr); + if (small_const_nbits(size)) { unsigned long val = *(const unsigned long *)addr; diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug index 3fc7abffc7aa..1c7dcd33fc2a 100644 --- a/lib/Kconfig.debug +++ b/lib/Kconfig.debug @@ -543,6 +543,13 @@ endmenu # "Compiler options" menu "Generic Kernel Debugging Instruments" +config DEBUG_BITMAP + bool "Debug bitmaps" + help + Say Y here if you want to check bitmap functions parameters at + the runtime. Enable CONFIG_DEBUG_BITMAP only for debugging because + it may affect performance. + config MAGIC_SYSRQ bool "Magic SysRq key" depends on !UML