From patchwork Thu Apr 28 20:51:12 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yury Norov X-Patchwork-Id: 12831145 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 708EAC4332F for ; Thu, 28 Apr 2022 20:51:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1351942AbiD1Uyj (ORCPT ); Thu, 28 Apr 2022 16:54:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56018 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1351520AbiD1Uyh (ORCPT ); Thu, 28 Apr 2022 16:54:37 -0400 Received: from mail-qk1-x735.google.com (mail-qk1-x735.google.com [IPv6:2607:f8b0:4864:20::735]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 09F616E8E3; Thu, 28 Apr 2022 13:51:22 -0700 (PDT) Received: by mail-qk1-x735.google.com with SMTP id x77so3772341qkb.3; Thu, 28 Apr 2022 13:51:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=sQKHNYNSaa8eVu3kbpnXB5Rjl+Crzx3Drn4KGQNwqro=; b=qpGoiNXe750pj7oN+UmBJziCkqdcVG5QXlsSYEfZFqiFPKdxAncuQ4qzZl+C1dK3vF +fnIWbtzzm8KXD1pXswEiFqVlxDmBO6OPTvX/kSkDYvnMiTNm/Rx4i6KuC3HcWXgSqlZ WXhzPizIOkK4Eq7xh7ZZe1yOqpEDRckCZH/U2eYn1qnkZSSZL0jAMe27S0wOc4FBqC4R tVpDoRZn9VD3P1AiuPm0pSoeNk/sfdbExZrX+8hXTcWgEWN/Ot3abFTBzWOVt4Yqe2rM hjrPlSqiBhEoF7WTdlP6isicxlsh6/fQ3uBjs+6aVas41uwjPzSBt1OtYQLw1unHJVMF jGLw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=sQKHNYNSaa8eVu3kbpnXB5Rjl+Crzx3Drn4KGQNwqro=; b=7TDSOnvfu9wVk8uR0u8+hflBqDRCqZVs38KiKvKkse533yWqo0r/9L3WEbhbPr4scm HGzCrxl1zdiRAn1L/d+uGHM4Z/E4SbOQGi+ZypYYzzManSRA1EaFwbIiLRNyLnQUGfaA eVVWydGXPagbeY2TF4v/WVQ1Os7N9qiYz1Z6ARv2omBfD9Hc4iXfHuGq3z4K+qLTr9Ag EQTy5vwJxh4gHJ+K1sezHBjiwIf5eiYBh/M1gw26CJsT7OGlR3k6cdCYDvOhzZCtdn0h qF+8FyxnOnRu2Q++42q/kq5BZj9TzA2yRLfopyvnU5V1UUOot15uWoBfv9jO8nlp39y6 +a4g== X-Gm-Message-State: AOAM532pzMJaO/PaGqERByCoZBXp5rtBCNUfu6APpMGXH4EkqmJp5FXh TYpzwQx6rzd1Ks7hEcxksTRfmwSpF3Y= X-Google-Smtp-Source: ABdhPJwHrUtpAiCOeYV3C40ju4BgJ7ox7zRmhHOgb1AqO0tv76fMXO6QVzbYQRclVAeod2a8jIjeLw== X-Received: by 2002:a05:620a:459f:b0:69f:6a25:f054 with SMTP id bp31-20020a05620a459f00b0069f6a25f054mr12085962qkb.560.1651179081004; Thu, 28 Apr 2022 13:51:21 -0700 (PDT) Received: from localhost ([2601:c4:c432:4da:fa85:340e:2244:1d8c]) by smtp.gmail.com with ESMTPSA id h5-20020a05622a170500b002f3818c7b92sm549709qtk.49.2022.04.28.13.51.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Apr 2022 13:51:20 -0700 (PDT) From: Yury Norov To: linux-kernel@vger.kernel.org, Alexander Gordeev , Andy Shevchenko , Christian Borntraeger , Claudio Imbrenda , David Hildenbrand , Heiko Carstens , Janosch Frank , Rasmus Villemoes , Sven Schnelle , Vasily Gorbik , Yury Norov , linux-s390@vger.kernel.org, kvm@vger.kernel.org Subject: [PATCH 1/5] lib/bitmap: extend comment for bitmap_(from,to)_arr32() Date: Thu, 28 Apr 2022 13:51:12 -0700 Message-Id: <20220428205116.861003-2-yury.norov@gmail.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220428205116.861003-1-yury.norov@gmail.com> References: <20220428205116.861003-1-yury.norov@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org On LE systems bitmaps are naturally ordered, therefore we can potentially use bitmap_copy routines when converting from 32-bit arrays, even if host system is 64-bit. But it may lead to out-of-bond access due to unsafe typecast, and the bitmap_(from,to)_arr32 comment doesn't explain that clearly Signed-off-by: Yury Norov --- include/linux/bitmap.h | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/include/linux/bitmap.h b/include/linux/bitmap.h index 983dc3f2d57b..dbdf1685debf 100644 --- a/include/linux/bitmap.h +++ b/include/linux/bitmap.h @@ -271,8 +271,12 @@ static inline void bitmap_copy_clear_tail(unsigned long *dst, } /* - * On 32-bit systems bitmaps are represented as u32 arrays internally, and - * therefore conversion is not needed when copying data from/to arrays of u32. + * On 32-bit systems bitmaps are represented as u32 arrays internally. On LE64 + * machines the order of hi and lo parts of numbers match the bitmap structure. + * In both cases conversion is not needed when copying data from/to arrays of + * u32. But in LE64 case, typecast in bitmap_copy_clear_tail() may lead + * to out-of-bound access. To avoid that, both LE and BE variants of 64-bit + * architectures are not using bitmap_copy_clear_tail(). */ #if BITS_PER_LONG == 64 void bitmap_from_arr32(unsigned long *bitmap, const u32 *buf, From patchwork Thu Apr 28 20:51:13 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yury Norov X-Patchwork-Id: 12831146 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2612FC4332F for ; Thu, 28 Apr 2022 20:51:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1351948AbiD1Uyq (ORCPT ); Thu, 28 Apr 2022 16:54:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56298 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1351947AbiD1Uyk (ORCPT ); Thu, 28 Apr 2022 16:54:40 -0400 Received: from mail-qk1-x733.google.com (mail-qk1-x733.google.com [IPv6:2607:f8b0:4864:20::733]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 57BCEC0E56; Thu, 28 Apr 2022 13:51:23 -0700 (PDT) Received: by mail-qk1-x733.google.com with SMTP id c1so4517345qkf.13; Thu, 28 Apr 2022 13:51:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=zpgfCNwBAsrogBT5FIwlctJ6I9aylyUTQSQGzBNw7UE=; b=YxOSNBL6nSIQ7v7mCasMnn/bP6CMqLNJ8WGk4NxtWHnECZmVKW/DgP4i98vJ3KhOzH 9awvJHK2cHlol65shu0uCVDrKeyX28DXB0fq7Sb0oDN6W5hSwZc7HaGufr0fMWkKBys4 ufSsEWkRPbvp7ZeYw1lSx2YmZlyMvMyhiqzqefoZALTPEeKYUM25zfTfKMUjK0udvXOL LRtye+pc1zTsExCmyKZylIEMzpgE/HdRvTwtwoQGCMsTDGwgu5CLXdutn/5wQuFC5n7m ybI27bT7GLwpv04qt5gkGdCJMcVrY+EbfqfCkZv9Rx9YOvUyjOEOqLbF2UDRqZ6kjAOl 8dzA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=zpgfCNwBAsrogBT5FIwlctJ6I9aylyUTQSQGzBNw7UE=; b=CgzPzLUGTHE61QpeIJZpLddydPRW0UG24oteBpr0hFOiNCdM6iK7YVdRY9XEBbDy3z 2wANW7aCCvF0VE2hEXlv43mW65js0I74x4FZguvmgli1zpAd/SdyNRhyakiny5Ck3gH/ NbEcXJoZ+wiwR4MLKgK9wXDxe+eWz/qF/OYOKkKugys7UT2SLYsHc441w2UIEbJb9VO/ 9ntrtQyMybj279MYIsxwwyi0cAA0Yjk079t3M+AUkwiKrcCUKs07RMkrHm+EhWEiXaEA oFGz3XKCB/aJxSYCxt4/uidjbTVF//Gw+kDpRDnzaqA9lz1HNDEK2t8kRJThi+e0Kayj vk7A== X-Gm-Message-State: AOAM533aw9m6EmRg+O1cwmv0GFLlzdg+s+Da9BgmRtpiM/Jmw3J8DiTL cZNRhyI5w54eCKDx70nX3s/o+1JP1Bk= X-Google-Smtp-Source: ABdhPJxVUW2mvkjQPeXy8dWRrYoJYUaXbEW9WhlX3Vu7KwtIimgwuoFC8gZ/3PiSVGf7kDcuUDT21Q== X-Received: by 2002:ae9:e317:0:b0:69f:b249:6beb with SMTP id v23-20020ae9e317000000b0069fb2496bebmr1900711qkf.14.1651179082224; Thu, 28 Apr 2022 13:51:22 -0700 (PDT) Received: from localhost ([2601:c4:c432:4da:fa85:340e:2244:1d8c]) by smtp.gmail.com with ESMTPSA id m190-20020a378ac7000000b0069f8d810f16sm465776qkd.85.2022.04.28.13.51.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Apr 2022 13:51:21 -0700 (PDT) From: Yury Norov To: linux-kernel@vger.kernel.org, Alexander Gordeev , Andy Shevchenko , Christian Borntraeger , Claudio Imbrenda , David Hildenbrand , Heiko Carstens , Janosch Frank , Rasmus Villemoes , Sven Schnelle , Vasily Gorbik , Yury Norov , linux-s390@vger.kernel.org, kvm@vger.kernel.org Subject: [PATCH 2/5] lib: add bitmap_{from,to}_arr64 Date: Thu, 28 Apr 2022 13:51:13 -0700 Message-Id: <20220428205116.861003-3-yury.norov@gmail.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220428205116.861003-1-yury.norov@gmail.com> References: <20220428205116.861003-1-yury.norov@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Manipulating 64-bit arrays with bitmap functions is potentially dangerous because on 32-bit BE machines the order of halfwords doesn't match. Another issue is that compiler may throw a warning about out-of-boundary access. This patch adds bitmap_{from,to}_arr64 functions in addition to existing bitmap_{from,to}_arr32. Signed-off-by: Yury Norov --- include/linux/bitmap.h | 23 ++++++++++++++++---- lib/bitmap.c | 48 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 4 deletions(-) diff --git a/include/linux/bitmap.h b/include/linux/bitmap.h index dbdf1685debf..57f1c74239d5 100644 --- a/include/linux/bitmap.h +++ b/include/linux/bitmap.h @@ -292,6 +292,24 @@ void bitmap_to_arr32(u32 *buf, const unsigned long *bitmap, (const unsigned long *) (bitmap), (nbits)) #endif +/* + * On 64-bit systems bitmaps are represented as u64 arrays internally. On LE32 + * machines the order of hi and lo parts of numbers match the bitmap structure. + * In both cases conversion is not needed when copying data from/to arrays of + * u64. + */ +#if (BITS_PER_LONG == 32) && defined(__BIG_ENDIAN) +void bitmap_from_arr64(unsigned long *bitmap, const u64 *buf, unsigned int nbits); +void bitmap_to_arr64(u64 *buf, const unsigned long *bitmap, unsigned int nbits); +#else +#define bitmap_from_arr64(bitmap, buf, nbits) \ + bitmap_copy_clear_tail((unsigned long *) (bitmap), \ + (const unsigned long *) (buf), (nbits)) +#define bitmap_to_arr64(buf, bitmap, nbits) \ + bitmap_copy_clear_tail((unsigned long *) (buf), \ + (const unsigned long *) (bitmap), (nbits)) +#endif + static inline int bitmap_and(unsigned long *dst, const unsigned long *src1, const unsigned long *src2, unsigned int nbits) { @@ -601,10 +619,7 @@ static inline void bitmap_next_set_region(unsigned long *bitmap, */ static inline void bitmap_from_u64(unsigned long *dst, u64 mask) { - dst[0] = mask & ULONG_MAX; - - if (sizeof(mask) > sizeof(unsigned long)) - dst[1] = mask >> 32; + bitmap_from_arr64(dst, &mask, 64); } /** diff --git a/lib/bitmap.c b/lib/bitmap.c index d9a4480af5b9..027b63a655fd 100644 --- a/lib/bitmap.c +++ b/lib/bitmap.c @@ -1533,5 +1533,53 @@ void bitmap_to_arr32(u32 *buf, const unsigned long *bitmap, unsigned int nbits) buf[halfwords - 1] &= (u32) (UINT_MAX >> ((-nbits) & 31)); } EXPORT_SYMBOL(bitmap_to_arr32); +#endif + +#if (BITS_PER_LONG == 32) && defined(__BIG_ENDIAN) +/** + * bitmap_from_arr64 - copy the contents of u64 array of bits to bitmap + * @bitmap: array of unsigned longs, the destination bitmap + * @buf: array of u64 (in host byte order), the source bitmap + * @nbits: number of bits in @bitmap + */ +void bitmap_from_arr64(unsigned long *bitmap, const u64 *buf, unsigned int nbits) +{ + int n; + + for (n = nbits; n > 0; n -= 64) { + u64 val = *buf++; + + *bitmap++ = val; + if (n > 32) + *bitmap++ = val >> 32; + } + + /* Clear tail bits in last word beyond nbits. */ + if (nbits % BITS_PER_LONG) + bitmap[-1] &= BITMAP_LAST_WORD_MASK(nbits); +} +EXPORT_SYMBOL(bitmap_from_arr64); + +/** + * bitmap_to_arr64 - copy the contents of bitmap to a u64 array of bits + * @buf: array of u64 (in host byte order), the dest bitmap + * @bitmap: array of unsigned longs, the source bitmap + * @nbits: number of bits in @bitmap + */ +void bitmap_to_arr64(u64 *buf, const unsigned long *bitmap, unsigned int nbits) +{ + const unsigned long *end = bitmap + BITS_TO_LONGS(nbits); + + while (bitmap < end) { + *buf = *bitmap++; + if (bitmap < end) + *buf |= (u64)(*bitmap++) << 32; + buf++; + } + /* Clear tail bits in last element of array beyond nbits. */ + if (nbits % 64) + buf[-1] &= GENMASK_ULL(nbits, 0); +} +EXPORT_SYMBOL(bitmap_to_arr64); #endif From patchwork Thu Apr 28 20:51:14 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yury Norov X-Patchwork-Id: 12831148 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 83648C433EF for ; Thu, 28 Apr 2022 20:51:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1352009AbiD1Uyv (ORCPT ); Thu, 28 Apr 2022 16:54:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56322 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1351949AbiD1Uyk (ORCPT ); Thu, 28 Apr 2022 16:54:40 -0400 Received: from mail-qk1-x735.google.com (mail-qk1-x735.google.com [IPv6:2607:f8b0:4864:20::735]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A5A9FC0E5B; Thu, 28 Apr 2022 13:51:24 -0700 (PDT) Received: by mail-qk1-x735.google.com with SMTP id e128so4539075qkd.7; Thu, 28 Apr 2022 13:51:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=1EpAoNh3djiR0hepuMZWh5K1dw4NUP6DaD4hagEgG3I=; b=f0Z5dX7wbl0R92CisFojyWE8K6d/92OfvlFaiM4rI3zIxB5QCKji7UaeMDC0t4yZQm aikCalRWglJhYnECZgsmp2vVjMNf7pvH/1P6OIgFcJynEQTRsg+Ysmz5ePKgLkwGXQtv aBn5NFDhHfffiGtIKiX9W8bb7QrCc0tLvbagvulxDyJrnoVVS3QU/NcLk/nplpXf7Vwy SgmR6hZulUr7Xu8zxii1h1hFau4ctQCbELlTGxSbEGJg4MVe7yNm2GTKJdpLgQfRvQMC w6DIL0w4Z0FlEZMtkeq6GTt5Mb8nGIUycBHj7rqt1GKpqWiYBRAP8R06GIyp4DCn8yje J08Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=1EpAoNh3djiR0hepuMZWh5K1dw4NUP6DaD4hagEgG3I=; b=jz6wjKrMfFcTV6a8neDjuQs9JTFLPwCvKsrmHBzABMl8w9UvZuIONSvZReqZ0S65fT lcWTcmPOdW/gApar3W3OtOFZoi48wPCiWDWAEhHRvhmFrfarqh9Q8ogRQdlqBV8dGKpy C9ZJYCXAJ+sBrziiJpJzEndcfz79ZicyaK2MOumtjcwr6AtRt8WD98+pOGoDgZQ14/wb qpyxstYsHMBHasmmFsWXnnJOVQD0B3rl8UA/S58E4EuVlT6ODLc4dLb67C6wAvfaoKnN 7uMBn14Lh0Pogx4l+PfyHOKDXlfj1zGuzkGYm1IVSfzqMfA5QRNBGbMHKRw+8UeslKuy puZg== X-Gm-Message-State: AOAM530vo2uzv/I/rPdHpzAlf1nSGLeCwHtKd6VFo7gpEXp4+WTnN5TU x6KtPd/YvFy1bqE/4Lev7ClTtIX0D6w= X-Google-Smtp-Source: ABdhPJxXLTFxVkZ3KgwG5707+YbXl4dlJCsL8/vtRybqZmkDB4puHhN7Wh7fO6njap7k2zXxkTsQpQ== X-Received: by 2002:a05:620a:2715:b0:69f:9e98:7501 with SMTP id b21-20020a05620a271500b0069f9e987501mr3591382qkp.346.1651179083615; Thu, 28 Apr 2022 13:51:23 -0700 (PDT) Received: from localhost ([2601:c4:c432:4da:fa85:340e:2244:1d8c]) by smtp.gmail.com with ESMTPSA id g10-20020ac8580a000000b002f35323f82csm613818qtg.30.2022.04.28.13.51.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Apr 2022 13:51:23 -0700 (PDT) From: Yury Norov To: linux-kernel@vger.kernel.org, Alexander Gordeev , Andy Shevchenko , Christian Borntraeger , Claudio Imbrenda , David Hildenbrand , Heiko Carstens , Janosch Frank , Rasmus Villemoes , Sven Schnelle , Vasily Gorbik , Yury Norov , linux-s390@vger.kernel.org, kvm@vger.kernel.org Subject: [PATCH 3/5] lib/bitmap: add test for bitmap_{from,to}_arr64 Date: Thu, 28 Apr 2022 13:51:14 -0700 Message-Id: <20220428205116.861003-4-yury.norov@gmail.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220428205116.861003-1-yury.norov@gmail.com> References: <20220428205116.861003-1-yury.norov@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Test newly added bitmap_{from,to}_arr64() functions similarly to already existing bitmap_{from,to}_arr32() tests. Signed-off-by: Yury Norov Reported-by: Guenter Roeck Signed-off-by: Yury Norov Tested-by: Guenter Roeck --- lib/test_bitmap.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/lib/test_bitmap.c b/lib/test_bitmap.c index 0c82f07f74fc..d5923a640457 100644 --- a/lib/test_bitmap.c +++ b/lib/test_bitmap.c @@ -585,6 +585,30 @@ static void __init test_bitmap_arr32(void) } } +static void __init test_bitmap_arr64(void) +{ + unsigned int nbits, next_bit; + u64 arr[EXP1_IN_BITS / 64]; + DECLARE_BITMAP(bmap2, EXP1_IN_BITS); + + memset(arr, 0xa5, sizeof(arr)); + + for (nbits = 0; nbits < EXP1_IN_BITS; ++nbits) { + memset(bmap2, 0xff, sizeof(arr)); + bitmap_to_arr64(arr, exp1, nbits); + bitmap_from_arr64(bmap2, arr, nbits); + expect_eq_bitmap(bmap2, exp1, nbits); + + next_bit = find_next_bit(bmap2, round_up(nbits, BITS_PER_LONG), nbits); + if (next_bit < round_up(nbits, BITS_PER_LONG)) + pr_err("bitmap_copy_arr64(nbits == %d:" + " tail is not safely cleared: %d\n", nbits, next_bit); + + if (nbits < EXP1_IN_BITS - 64) + expect_eq_uint(arr[DIV_ROUND_UP(nbits, 64)], 0xa5a5a5a5); + } +} + static void noinline __init test_mem_optimisations(void) { DECLARE_BITMAP(bmap1, 1024); @@ -852,6 +876,7 @@ static void __init selftest(void) test_copy(); test_replace(); test_bitmap_arr32(); + test_bitmap_arr64(); test_bitmap_parse(); test_bitmap_parselist(); test_bitmap_printlist(); From patchwork Thu Apr 28 20:51:15 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yury Norov X-Patchwork-Id: 12831147 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0B9DCC433FE for ; Thu, 28 Apr 2022 20:51:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1351994AbiD1Uyt (ORCPT ); Thu, 28 Apr 2022 16:54:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56472 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1351955AbiD1Uym (ORCPT ); Thu, 28 Apr 2022 16:54:42 -0400 Received: from mail-qv1-xf30.google.com (mail-qv1-xf30.google.com [IPv6:2607:f8b0:4864:20::f30]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0F56BC0E65; Thu, 28 Apr 2022 13:51:26 -0700 (PDT) Received: by mail-qv1-xf30.google.com with SMTP id iy15so4063600qvb.9; Thu, 28 Apr 2022 13:51:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=Xw3/FRUnNxzv2Vp5MuedmA5I6QJHauY+6s7D5TgPLEg=; b=G/eBNBoGIlKglYC+2XQOUPkYpfxoynz2J3BYBsv+O4lkvCcEz0pbzICvG1htRTn9T2 12e8ZExY8T0BJ1g5Ty5IgSjNDi+TYqskSD4VWWjYXs1SUy+6hBnhJcZQJGOVABORrsdc u/N24Md3p/ik3a7QGIL5+uNw1dNYrrUhFJyTdzro2Fs68fUvAA9cIJ+XjKnwJkLBhCdB Kg1ymQa+abuIf55Ath6HhSjSoI3kfnF+dPCeqC+CXjNwFDKHbw+lSl51Mya4manDcfVg v0KtQv+OE/DdfnK1uqur71YLQAhRPlyA1WrPtxReF9eBojO+EyIdWzQirHVkYc9pE6wN SJQQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Xw3/FRUnNxzv2Vp5MuedmA5I6QJHauY+6s7D5TgPLEg=; b=TSqt7znqXCQAFyUnHrDrre1GGVv+BA4rxeiW9l+wSltrUrOmkQ5YwkJtVVT40G+1sV eLZF1BI/C19RzIqSRoV4T2TxGVx536Ra+Td6N9EVC1Vdrmoc8RcNfsZmCR49eCRyeCG1 d35e0iId1FC+7RAviL1QOcIlMVFmqYR3QrZiSA9hKCCIrddR6cjfWjRU4ycmzyLUV2gY GgfIt3sn8J4XZz9h/TnLs5H6E08l2eiDlj3ceYSRDgunv++/BrgdI8Uzwl6DfDA2O+5Q sPN5bSU4LXbNTHi53AGE7+TI7EuRrbhrDzmq4vbB9f10utEoBIGgmHrY37NwoZvKXe2d G7Ag== X-Gm-Message-State: AOAM530FWU5hDG37avGHIzU+0Q2Wsl8vutu1M7DM9Uffj2YvPwHq2HhH 0m5je82aZGVDYPZYXtIuyEeUWXEN0og= X-Google-Smtp-Source: ABdhPJzGfrwpXemQ16feyGynDUbhkxBE1tDjYowkDP+R2bhH8PI1LM1Xz6l8jaeKjg5l/FTBJ5MYVQ== X-Received: by 2002:a05:6214:224f:b0:43f:cd6a:1d6b with SMTP id c15-20020a056214224f00b0043fcd6a1d6bmr25886639qvc.12.1651179084896; Thu, 28 Apr 2022 13:51:24 -0700 (PDT) Received: from localhost ([2601:c4:c432:4da:fa85:340e:2244:1d8c]) by smtp.gmail.com with ESMTPSA id 186-20020a3707c3000000b0069f9a8cbec2sm455491qkh.131.2022.04.28.13.51.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Apr 2022 13:51:24 -0700 (PDT) From: Yury Norov To: linux-kernel@vger.kernel.org, Alexander Gordeev , Andy Shevchenko , Christian Borntraeger , Claudio Imbrenda , David Hildenbrand , Heiko Carstens , Janosch Frank , Rasmus Villemoes , Sven Schnelle , Vasily Gorbik , Yury Norov , linux-s390@vger.kernel.org, kvm@vger.kernel.org Subject: [PATCH 4/5] KVM: s390: replace bitmap_copy with bitmap_{from,to}_arr64 where appropriate Date: Thu, 28 Apr 2022 13:51:15 -0700 Message-Id: <20220428205116.861003-5-yury.norov@gmail.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220428205116.861003-1-yury.norov@gmail.com> References: <20220428205116.861003-1-yury.norov@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Copying bitmaps from/to 64-bit arrays with bitmap_copy is not safe on 32-bit BE machines. Use designated functions instead. Signed-off-by: Yury Norov Reviewed-by: David Hildenbrand --- arch/s390/kvm/kvm-s390.c | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/arch/s390/kvm/kvm-s390.c b/arch/s390/kvm/kvm-s390.c index 76ad6408cb2c..8fcb56141689 100644 --- a/arch/s390/kvm/kvm-s390.c +++ b/arch/s390/kvm/kvm-s390.c @@ -1332,8 +1332,7 @@ static int kvm_s390_set_processor_feat(struct kvm *kvm, mutex_unlock(&kvm->lock); return -EBUSY; } - bitmap_copy(kvm->arch.cpu_feat, (unsigned long *) data.feat, - KVM_S390_VM_CPU_FEAT_NR_BITS); + bitmap_from_arr64(kvm->arch.cpu_feat, data.feat, KVM_S390_VM_CPU_FEAT_NR_BITS); mutex_unlock(&kvm->lock); VM_EVENT(kvm, 3, "SET: guest feat: 0x%16.16llx.0x%16.16llx.0x%16.16llx", data.feat[0], @@ -1504,8 +1503,7 @@ static int kvm_s390_get_processor_feat(struct kvm *kvm, { struct kvm_s390_vm_cpu_feat data; - bitmap_copy((unsigned long *) data.feat, kvm->arch.cpu_feat, - KVM_S390_VM_CPU_FEAT_NR_BITS); + bitmap_to_arr64(data.feat, kvm->arch.cpu_feat, KVM_S390_VM_CPU_FEAT_NR_BITS); if (copy_to_user((void __user *)attr->addr, &data, sizeof(data))) return -EFAULT; VM_EVENT(kvm, 3, "GET: guest feat: 0x%16.16llx.0x%16.16llx.0x%16.16llx", @@ -1520,9 +1518,7 @@ static int kvm_s390_get_machine_feat(struct kvm *kvm, { struct kvm_s390_vm_cpu_feat data; - bitmap_copy((unsigned long *) data.feat, - kvm_s390_available_cpu_feat, - KVM_S390_VM_CPU_FEAT_NR_BITS); + bitmap_to_arr64(data.feat, kvm_s390_available_cpu_feat, KVM_S390_VM_CPU_FEAT_NR_BITS); if (copy_to_user((void __user *)attr->addr, &data, sizeof(data))) return -EFAULT; VM_EVENT(kvm, 3, "GET: host feat: 0x%16.16llx.0x%16.16llx.0x%16.16llx", From patchwork Thu Apr 28 20:51:16 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yury Norov X-Patchwork-Id: 12831149 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id DD8D8C4332F for ; Thu, 28 Apr 2022 20:51:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1352014AbiD1Uyw (ORCPT ); Thu, 28 Apr 2022 16:54:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56504 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1351959AbiD1Uyn (ORCPT ); Thu, 28 Apr 2022 16:54:43 -0400 Received: from mail-qk1-x734.google.com (mail-qk1-x734.google.com [IPv6:2607:f8b0:4864:20::734]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2BA0FC0E5A; Thu, 28 Apr 2022 13:51:27 -0700 (PDT) Received: by mail-qk1-x734.google.com with SMTP id s4so4585941qkh.0; Thu, 28 Apr 2022 13:51:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=/BaooLzAKHS9Su+qBeGx1AzKlxdTJqpbBgALV7a+lkY=; b=MSB06OYuM4vzrWqwQq2U1WVVBwEbaiCPelC6EXPkuZMcw/sC0MEEGljhHmrGZ1z798 536aKbryjcWX6KGezO6vag8k8Pp+hWnwm+aoTLJ6Pk47FYEcEuXsi0GfQwJdebEcxyvR wAwgOyje2Tws5RQxcMNCaok+AI/fR0DOm+fCP1irGLWgwioHqNVXwaFgpzKhqBWSvF0e 6p0Np4HcorErIoOGYB183hT+pb4R+YbELFpj0PCKjCkpAgEVdeTT+YKNX90unvXGr1j/ rqTq+XJfwxpIvZeQFh37Z2U7BXv32Q2PWiXhzqIG3MssbY22vWlPidfP5c46y03srtEV vZPw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=/BaooLzAKHS9Su+qBeGx1AzKlxdTJqpbBgALV7a+lkY=; b=OFFty82rNeGBDZFSy6Chg3c87Yp718JegYkg3270oTKIr/qX0AznrOsbRDQ3Oj6msu 4ifavFoNY0Jub4Q9OJnvVHmkGBc2ZsBPUDqM++ha5UwujzwIJZwZWsWyh6vm8vD8bajP LPNKNta3ZOufVU54ieeV6jMR+7tyVb/WQKh1/sQQGNkrgfK16dG3GUBb/Uy4x5MKqS1Z JA7DH0qTHkOFXtZbVQd0oGFf9EVyT85hdu8jBVTDboXC052YXgVpk560a953hWl8MOFO QgSqaHJI0MAvYbmVwhdGMqzjse7kb9ouyE9OiUny9OjG7Via4nbgikrvXFTMvx09EB47 W9Iw== X-Gm-Message-State: AOAM53096Rqkq8dif0I+tM/Nb5I4/kPY/GeZfH4tCavkWSEjuXHaHVyR /C4qzoe2SB1PFsl6D7JQZR3H0amygmk= X-Google-Smtp-Source: ABdhPJwgTBNoaiyndhh2z4VT3EESH1Q5rsnilRmKWwzQG5A8cFDGXw3yE0CM9mJL0lvThT5ehcIv1A== X-Received: by 2002:a37:a3d2:0:b0:69f:74af:2aef with SMTP id m201-20020a37a3d2000000b0069f74af2aefmr10045059qke.388.1651179086102; Thu, 28 Apr 2022 13:51:26 -0700 (PDT) Received: from localhost ([2601:c4:c432:4da:fa85:340e:2244:1d8c]) by smtp.gmail.com with ESMTPSA id d65-20020a37b444000000b0069e9f79795fsm494549qkf.67.2022.04.28.13.51.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Apr 2022 13:51:25 -0700 (PDT) From: Yury Norov To: linux-kernel@vger.kernel.org, Alexander Gordeev , Andy Shevchenko , Christian Borntraeger , Claudio Imbrenda , David Hildenbrand , Heiko Carstens , Janosch Frank , Rasmus Villemoes , Sven Schnelle , Vasily Gorbik , Yury Norov , linux-s390@vger.kernel.org, kvm@vger.kernel.org Subject: [PATCH 5/5] drm/amd/pm: use bitmap_{from,to}_arr32 where appropriate Date: Thu, 28 Apr 2022 13:51:16 -0700 Message-Id: <20220428205116.861003-6-yury.norov@gmail.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220428205116.861003-1-yury.norov@gmail.com> References: <20220428205116.861003-1-yury.norov@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org The smu_v1X_0_set_allowed_mask() uses bitmap_copy() to convert bitmap to 32-bit array. This may be wrong due to endiannes issues. Fix it by switching to bitmap_{from,to}_arr32. Signed-off-by: Yury Norov --- drivers/gpu/drm/amd/pm/swsmu/smu11/smu_v11_0.c | 2 +- drivers/gpu/drm/amd/pm/swsmu/smu13/smu_v13_0.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/gpu/drm/amd/pm/swsmu/smu11/smu_v11_0.c b/drivers/gpu/drm/amd/pm/swsmu/smu11/smu_v11_0.c index b87f550af26b..5f8809f6990d 100644 --- a/drivers/gpu/drm/amd/pm/swsmu/smu11/smu_v11_0.c +++ b/drivers/gpu/drm/amd/pm/swsmu/smu11/smu_v11_0.c @@ -781,7 +781,7 @@ int smu_v11_0_set_allowed_mask(struct smu_context *smu) goto failed; } - bitmap_copy((unsigned long *)feature_mask, feature->allowed, 64); + bitmap_to_arr32(feature_mask, feature->allowed, 64); ret = smu_cmn_send_smc_msg_with_param(smu, SMU_MSG_SetAllowedFeaturesMaskHigh, feature_mask[1], NULL); diff --git a/drivers/gpu/drm/amd/pm/swsmu/smu13/smu_v13_0.c b/drivers/gpu/drm/amd/pm/swsmu/smu13/smu_v13_0.c index cf09e30bdfe0..747430ce6394 100644 --- a/drivers/gpu/drm/amd/pm/swsmu/smu13/smu_v13_0.c +++ b/drivers/gpu/drm/amd/pm/swsmu/smu13/smu_v13_0.c @@ -730,7 +730,7 @@ int smu_v13_0_set_allowed_mask(struct smu_context *smu) feature->feature_num < 64) return -EINVAL; - bitmap_copy((unsigned long *)feature_mask, feature->allowed, 64); + bitmap_to_arr32(feature_mask, feature->allowed, 64); ret = smu_cmn_send_smc_msg_with_param(smu, SMU_MSG_SetAllowedFeaturesMaskHigh, feature_mask[1], NULL);