Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753107AbdHJR3r (ORCPT ); Thu, 10 Aug 2017 13:29:47 -0400 Received: from mail-sn1nam02on0072.outbound.protection.outlook.com ([104.47.36.72]:21088 "EHLO NAM02-SN1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1752437AbdHJR3o (ORCPT ); Thu, 10 Aug 2017 13:29:44 -0400 Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=Yuri.Norov@cavium.com; From: Yury Norov To: Andrew Morton , Rasmus Villemoes Cc: Yury Norov , Noam Camus , Matthew Wilcox , Mauro Carvalho Chehab , linux-kernel@vger.kernel.org Subject: [PATCH] bitmap: introduce BITMAP_FROM_U64() Date: Thu, 10 Aug 2017 20:29:16 +0300 Message-Id: <20170810172916.24144-1-ynorov@caviumnetworks.com> X-Mailer: git-send-email 2.11.0 MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [160.89.204.176] X-ClientProxiedBy: VI1P189CA0031.EURP189.PROD.OUTLOOK.COM (10.165.188.44) To CY1PR07MB2117.namprd07.prod.outlook.com (10.164.73.155) X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: aa8b5bf9-18dc-4f44-c0e2-08d4e015627d X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:(300000500095)(300135000095)(300000501095)(300135300095)(22001)(300000502095)(300135100095)(2017030254152)(300000503095)(300135400095)(201703131423075)(201703031133081)(201702281549075)(300000504095)(300135200095)(300000505095)(300135600095)(300000506095)(300135500095);SRVR:CY1PR07MB2117; X-Microsoft-Exchange-Diagnostics: 1;CY1PR07MB2117;3:kKTzAsqNN8tSDsASABvqOH0uJgGIrD14EuwzBRcREXIVlfEXRtAx3SUPpt4eWu9WbQ0EDnW/Fq7l6JNBIxGv8tScQFk+OgC7aAXfurE9+AxuGRsbbrAuQ6D1lBHOHRvGjCT1GW5+yhxQL+envv51mSU54SfNfeaj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M3s2aasSkOuuxVu0sDiJUF6WJ5Ofl1nc3hEbNeN8zicQmin50kN3tf9JG0vF/Etv2EGJeRrP+MMx+HcTcJ3tUUMcQ+ezjp3uySo160Fp5ZUiqQKDlyfARmgAg42fJ109wfi4T5z85oQyBJRjGNFMP4jspLpIJoAQTWF3B+OHxc/STHMQqqHoMaF8rQF7YgOjyElmvx50aDCVA6u6G+P4viHkxg//SyNvZyfIP/Iy5QA6/P3/tbbWxDDebtSdDEUUdUg/ndpGR5wyxovcB1nvTceplqWVZv07/l1pH0=;4:wv4D2KgUX46W/iQfFuIGzEuL5WoVEjlrHXqu408EMpjjIBAYtWUjQDpeEkqr7Bum0ve0NoZLn8AJ8Og+X7ZhiBYI2FOUwJoDug6opax/52JuT7kW93AbMvHVdrJHpBm8gCnDp2r2PX+ht1CZxtSdACWQkD7weOYj0oVn3Ls5ayB1l6fyM/ZwiJaqANhc6ENTJrKi88qLuVBsobewSe4LlgYpZT33Tr0/7bp22LDPOo4w9GFp6mX531iT6617kGG7 X-Exchange-Antispam-Report-Test: UriScan:; X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(100000700101)(100105000095)(100000701101)(100105300095)(100000702101)(100105100095)(6040450)(601004)(2401047)(5005006)(8121501046)(3002001)(10201501046)(93006095)(100000703101)(100105400095)(6041248)(20161123558100)(201703131423075)(201702281528075)(201703061421075)(201703061406153)(20161123555025)(20161123564025)(20161123562025)(20161123560025)(6072148)(201708071742011)(100000704101)(100105200095)(100000705101)(100105500095);SRVR:CY1PR07MB2117;BCL:0;PCL:0;RULEID:(100000800101)(100110000095)(100000801101)(100110300095)(100000802101)(100110100095)(100000803101)(100110400095)(100000804101)(100110200095)(100000805101)(100110500095);SRVR:CY1PR07MB2117; X-Forefront-PRVS: 03950F25EC X-Forefront-Antispam-Report: SFV:NSPM;SFS:(10009020)(7370300001)(4630300001)(6009001)(6069001)(39450400003)(39410400002)(39850400002)(39400400002)(39840400002)(189002)(199003)(76506005)(1076002)(42186005)(42882006)(6666003)(50986999)(4326008)(101416001)(25786009)(72206003)(3846002)(6116002)(48376002)(6486002)(53936002)(38730400002)(50466002)(54906002)(6496005)(8666007)(478600001)(2906002)(5003940100001)(68736007)(33646002)(97736004)(189998001)(7350300001)(36756003)(66066001)(50226002)(105586002)(47776003)(106356001)(5660300001)(305945005)(81156014)(8676002)(81166006)(7736002);DIR:OUT;SFP:1101;SCL:1;SRVR:CY1PR07MB2117;H:localhost;FPR:;SPF:None;PTR:InfoNoRecords;MX:1;A:1;LANG:en; X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1;CY1PR07MB2117;23:CyBDyoAe8N8q+Chl0KbQVoCMw4bQDACma1pUI13I9?= =?us-ascii?Q?VP/bHf3WThShJIOjp8XrScFje2Dfnndtyjf4cfKCV6Cg/JYqbRtskNergfiD?= =?us-ascii?Q?Ap+O6LcPHBATI41ZQNyG15IUNTX5gBps0GuTNahjuU1QLTD/TLtcePm/Ndw1?= =?us-ascii?Q?n11iwAyZuGAyBzblScS0wsIIfj1dfxuelFgyz+fsZC1Iax88Ei/L8A6dKSwQ?= =?us-ascii?Q?2Ssv+a34wKAkPCwCwQWHZOMfikRjS1ur3mr9aq0pxmFm4WubZ0ZmGEyb1Tw1?= =?us-ascii?Q?7T6iSFsYp18jpkZlUx4yT5FRX0tS0nsYF8p2+OJGvxNe1AJytRN386DD1dHM?= =?us-ascii?Q?oW2m5kXBHCjV2AdYak1X+t4u09UgV0rb7Q8tFgxrESKhm8dIcy6WhWrxFoye?= =?us-ascii?Q?MFtgSfEpVeZVXCQxdMV0COrMPmyHuUhdQSVyzbBgxSjLZbk4J0SedftvmrE5?= =?us-ascii?Q?ARbLsVvVeyKhG75r98FW15bXf4+nw/nsJOoQu+Kk1zxFk33ss2BbNH46k/Kk?= =?us-ascii?Q?JisUKHkwn+El5aHhR4oIhOj7qucupt0mlpSkGfbAwciC1tJhEE5k4Xx8auKD?= =?us-ascii?Q?ReYft/264zUK7wIfaEdFyam1V/UdO0FuXBIU1kFsw0+eImpTGya/uQVmV+Bq?= =?us-ascii?Q?Xwb9+4WsnqZLkdmfqTTaKh4uQShg/5SImAUdHL2fUz+NuTfuzcRJ9fcrAGWk?= =?us-ascii?Q?CybeIbZk/uBK/IpZOgLw361QwNEQ0UDv9G5wNodxEDDugyNJG5GUbpniz9RW?= =?us-ascii?Q?d9gFS58cqoMrq89TcGZcTz32VT/cFaklNeYnXLKouCT2rix+Jo0ymkXZpluq?= =?us-ascii?Q?syQwZpOkXYMCwRSkjINQtIrK2hrmNcATIsAiPKcprWIAcwm+VNkFpC1PKsC6?= =?us-ascii?Q?P7m/XonqmbylqD1PnDUzsRLLRONEwMgBhkPKt0dwSUogDk91L4roJbZqEeLC?= =?us-ascii?Q?lYbDaxtEURB+NjN4hDg5JfyIjt22YFIUkyCPMjqc+uM/fDwckIg6eNM8MvTX?= =?us-ascii?Q?3WkyszvDQOMvzJn3GQNtIY9mxL5Mrav3UeJMl/Q63RY90cj7rW6VZ9OKDXrO?= =?us-ascii?Q?+LiW+GJYnGupx8Lq56RwP47OA8gEcObCSXFvJCpBmo6OkLkFZqZDfSf65mf4?= =?us-ascii?Q?45LPJaOGiYQgMmfddB5KJqwA7ul/oIk17X0kLoY90BpXX2rgWDt0PJt0xWdS?= =?us-ascii?Q?1puYlxWjCQmnzIa3LrGfTAZ4IVcQbDmrfDQJgURV55dm/hjAUaszEhuYdZtB?= =?us-ascii?Q?PWWOXnxRRoi2AiuqDM=3D?= X-Microsoft-Exchange-Diagnostics: 1;CY1PR07MB2117;6:lrE55i8H90U6X8mE+h4zYUFYbg1a0bPZkOdp0y8fg62rszXypPagaA1Ouq2GcBLw1b+afX9s/hQe9QtAK019ajbJ1h56iQDcrRCwb+noRjioD63Q1uDmLE7XqfW+LKJGtwOZDI3zdGxfwi+NKDLfA/GUbqv+PWdJusNGala2eZn0lBzuvOfnJYSyXslYEZivdMZSOoQjxM3N0K0ZXXQUBx+Fr2FE7nj6ZkbWvy+VNWBgdJBe9/qx6LlOAMFbrK+LPy5cNOVek8vDmNunYcA5QWUMWn9go+6FTTc+LEXWOOWpMMn41EwotwdTMkxOtElUiHV8Wvp5AYf7+WPSt5+kgA==;5:8GaJelPNle/LGE2TW2UJz21yf9SirdtgXPCXfal0d3MuBEYMjHa7qAu8Kjdk+N5573OTpDD5pcguxb1snlR9gcfDXGwlFlePXHRsWpesXMMEgKlJLYLrbh+5lPyFUohRAXcjPYjKGt8IrNLn/8Mybw==;24:IcS8KPA4pUtrue4LwmEv2HpJG9cS90puroAJNqtfsYwRPWaecSh+kGJk1Q6wvP1onG1rME8mYGBRrYmXY0pGcnqHdOqIkSw1JSYnj9rbX+g=;7:gp1wrgrn03fT9b5Nq71g8nseTcwkGYODxyUfAy+UlH/ZAadC5PAW/5wLyZIgL1ECobXsyGVfAYaMvc9GYdQ1Ezqr1kg4g0ykXrisiRnOW9ySNEyiQpumHBsBoOaScWABY65pbjLE3LGPd+PQkGRXto1g7DLVw6YqmSrbOFLK0mk+MnvB4YbEPxRO49OZDPk+UtystR5hgzuQrHp8RlQn7hiBxShZfKVSCoJGCX0bFMg= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-OriginatorOrg: caviumnetworks.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Aug 2017 17:29:37.8362 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY1PR07MB2117 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 4589 Lines: 120 The macro is the compile-time analogue of bitmap_from_u64() with the same purpose: convert the 64-bit number to the properly ordered pair of 32-bit parts, suitable for filling the bitmap in 32-bit BE environment. Use it to make test_bitmap_parselist() correct for 32-bit BE ABIs. Tested on BE mips/qemu. Signed-off-by: Yury Norov --- include/linux/bitmap.h | 32 ++++++++++++++++++++++++++++++++ lib/test_bitmap.c | 47 ++++++++++++++++++++++++++++++++--------------- 2 files changed, 64 insertions(+), 15 deletions(-) diff --git a/include/linux/bitmap.h b/include/linux/bitmap.h index 5797ca6fdfe2..088137c06d96 100644 --- a/include/linux/bitmap.h +++ b/include/linux/bitmap.h @@ -361,6 +361,38 @@ static inline int bitmap_parse(const char *buf, unsigned int buflen, } /* + * BITMAP_FROM_U64() - Represent u64 value in the format suitable for bitmap. + * + * Linux bitmaps are internally the arrays on unsigned longs, i.e. 32-bit + * integers in 32-bit environment, and 64-bit integers in 64-bit one. + * + * There are four combinations of endianess and length of the word in linux ABIs: + * LE64, BE64, LE32 and BE32. + * + * On 64-bit kernels 64-bit LE and BE numbers are naturally ordered in + * bitmaps and therefore don't require any special handling. + * + * On 32-bit kernels 32-bit LE ABI orders lo word of 64-bit number in the memory + * prior to hi, and 32-bit BE orders hi word prior to lo. The bitmap in other + * hand is represented as the array of 32-bit words, and the position of the + * bit N may therefore be calculated as: word #(N/32) and bit #(N%32) in that + * word. For example, bit #42 is located at 10th position of 2nd word. + * It matches 32-bit LE ABI, and we can simply let the compiler store 64-bit + * value on the memory as it usually does. But for BE we therefore need to swap + * hi and lo words manually. + * + * With all that, the macro BITMAP_FROM_U64() does explicit reorder of hi and lo + * parts of u64. For LE32 it does nothing in fact, and for BE environment it + * swaps hi and lo words, as it expected by bitmap. + */ +#if __BITS_PER_LONG == 64 +#define BITMAP_FROM_U64(n) (n) +#else +#define BITMAP_FROM_U64(n) ((unsigned long) ((u64)(n) & ULONG_MAX)), \ + ((unsigned long) ((u64)(n) >> 32)) +#endif + +/* * bitmap_from_u64 - Check and swap words within u64. * @mask: source bitmap * @dst: destination bitmap diff --git a/lib/test_bitmap.c b/lib/test_bitmap.c index 5b80dd94e4d1..99c75496af5e 100644 --- a/lib/test_bitmap.c +++ b/lib/test_bitmap.c @@ -175,24 +175,41 @@ struct test_bitmap_parselist{ const int flags; }; -static const unsigned long exp[] = {1, 2, 0x0000ffff, 0xffff0000, 0x55555555, - 0xaaaaaaaa, 0x11111111, 0x22222222, 0xffffffff, - 0xfffffffe, 0x3333333311111111, 0xffffffff77777777}; -static const unsigned long exp2[] = {0x3333333311111111, 0xffffffff77777777}; +static const unsigned long exp[] __initconst = { + BITMAP_FROM_U64(1), + BITMAP_FROM_U64(2), + BITMAP_FROM_U64(0x0000ffff), + BITMAP_FROM_U64(0xffff0000), + BITMAP_FROM_U64(0x55555555), + BITMAP_FROM_U64(0xaaaaaaaa), + BITMAP_FROM_U64(0x11111111), + BITMAP_FROM_U64(0x22222222), + BITMAP_FROM_U64(0xffffffff), + BITMAP_FROM_U64(0xfffffffe), + BITMAP_FROM_U64(0x3333333311111111), + BITMAP_FROM_U64(0xffffffff77777777) +}; + +static const unsigned long exp2[] __initconst = { + BITMAP_FROM_U64(0x3333333311111111), + BITMAP_FROM_U64(0xffffffff77777777) +}; static const struct test_bitmap_parselist parselist_tests[] __initconst = { +#define step (sizeof(u64) / sizeof(unsigned long)) + {0, "0", &exp[0], 8, 0}, - {0, "1", &exp[1], 8, 0}, - {0, "0-15", &exp[2], 32, 0}, - {0, "16-31", &exp[3], 32, 0}, - {0, "0-31:1/2", &exp[4], 32, 0}, - {0, "1-31:1/2", &exp[5], 32, 0}, - {0, "0-31:1/4", &exp[6], 32, 0}, - {0, "1-31:1/4", &exp[7], 32, 0}, - {0, "0-31:4/4", &exp[8], 32, 0}, - {0, "1-31:4/4", &exp[9], 32, 0}, - {0, "0-31:1/4,32-63:2/4", &exp[10], 64, 0}, - {0, "0-31:3/4,32-63:4/4", &exp[11], 64, 0}, + {0, "1", &exp[1 * step], 8, 0}, + {0, "0-15", &exp[2 * step], 32, 0}, + {0, "16-31", &exp[3 * step], 32, 0}, + {0, "0-31:1/2", &exp[4 * step], 32, 0}, + {0, "1-31:1/2", &exp[5 * step], 32, 0}, + {0, "0-31:1/4", &exp[6 * step], 32, 0}, + {0, "1-31:1/4", &exp[7 * step], 32, 0}, + {0, "0-31:4/4", &exp[8 * step], 32, 0}, + {0, "1-31:4/4", &exp[9 * step], 32, 0}, + {0, "0-31:1/4,32-63:2/4", &exp[10 * step], 64, 0}, + {0, "0-31:3/4,32-63:4/4", &exp[11 * step], 64, 0}, {0, "0-31:1/4,32-63:2/4,64-95:3/4,96-127:4/4", exp2, 128, 0}, -- 2.11.0