Received: by 2002:ad5:4acb:0:0:0:0:0 with SMTP id n11csp730775imw; Thu, 14 Jul 2022 09:47:23 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vQRg2X0Q+gh8mLb2yKU8qUtj6P2fCWx/RM4LXt7wIDlSuhrZJWOgBt+sRAUtKbqXMnOF00 X-Received: by 2002:a63:df06:0:b0:419:78b4:df25 with SMTP id u6-20020a63df06000000b0041978b4df25mr8367531pgg.87.1657817243053; Thu, 14 Jul 2022 09:47:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1657817243; cv=none; d=google.com; s=arc-20160816; b=Cba0NqebaEBZGTt12bS0lL6ld2Qzui4x8WMgwUicpb6RBLIkMlkOKDBElk7L9KCUq1 pBQblQhkNawxyCUoQQiMWrIc0Vi//z+yzEEtqpboKWEFx83BSiC2dQqgZ0VZ2pWPM5LA 7Bmy8pQ0b/HcwvnwqGZhAF/hufYWg5TBqfB+Q1ZjwxU6AB+hNEXuH2YMnID+4sH+Ackc PyFf1sHivEW3oTlzp4uqzQ41Ps/G11XMG9fmI+FZwvtxhH4c78sKd3OlKuELJ1ur00/2 tit9CvosazcJWcOYJFUU7vqsNeoOV9KdJUcQ1AKhop1payzw5OeaEqhLvpSP22VWTwPz LgrQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=HE/LbAvk+fFVqf81/4DgJq6xNFT89ccVM+dBKRoa1lc=; b=pKXqohCw4yL9t4UkpWa3sTVEq91NdwJgimAR+JbYNWaiTqKhE9uE9uVZ9t2eCz5Wt9 KhLeMeknzhW564WtdEwyDT7c9wr0oZkyonYp4NafnoAjuV14rWx4FiKPBX4VzQIn7O1u NpQKd69sTSWuUC0wWkVe5VHX+gh/ntMo42M+mk85dSLIIGqAaNtaI2kiidsuDqTHZ5Xm Xis4+nMKkBrfYQZYT1Kr/jjBVu809eTcxKVUrTDRLxZlYSS4AkSxDtvTeMBnOrO9MAWE h8mPMr56hqDHSxDtiU67MDr/GVIJ1Ef7vyE5OXIJRKybnDDX7MTgjy6gDKAHmwvQz1yY B8aA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q5-20020a170902f78500b0016c3d9ae242si2442567pln.211.2022.07.14.09.47.08; Thu, 14 Jul 2022 09:47:23 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240242AbiGNQpO (ORCPT + 99 others); Thu, 14 Jul 2022 12:45:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38560 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240144AbiGNQoV (ORCPT ); Thu, 14 Jul 2022 12:44:21 -0400 Received: from mx1.molgen.mpg.de (mx3.molgen.mpg.de [141.14.17.11]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EBECDCDE for ; Thu, 14 Jul 2022 09:43:45 -0700 (PDT) Received: from localhost.localdomain (ip5f5aeb82.dynamic.kabel-deutschland.de [95.90.235.130]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: pmenzel) by mx.molgen.mpg.de (Postfix) with ESMTPSA id D21D361EA192D; Thu, 14 Jul 2022 18:43:42 +0200 (CEST) From: Paul Menzel To: Yury Norov , Andy Shevchenko , Rasmus Villemoes Cc: Paul Menzel , linux-kernel@vger.kernel.org Subject: [PATCH v3] lib/bitmap: Make count and length parameters unsigned Date: Thu, 14 Jul 2022 18:42:07 +0200 Message-Id: <20220714164207.52410-1-pmenzel@molgen.mpg.de> X-Mailer: git-send-email 2.36.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Counts and the length are non-negative, so make them unsigned, and adapt the while condition in `__bitmap_set()` and `__bitmap_clear()` accordingly. For `__bitmap_set()` six less intstructions are used as a result: ``` __bitmap_set: __bitmap_set: movl %esi, %eax movl %esi, %eax movq %rdi, %r8 < movl %esi, %ecx movl %esi, %ecx movl %edx, %edi | movl %esi, %r8d > movl $64, %esi > andl $63, %ecx shrl $6, %eax shrl $6, %eax andl $63, %esi | movl %edx, %r9d movq $-1, %rdx | leaq (%rdi,%rax,8), %rax leaq (%r8,%rax,8), %rax | subl %ecx, %esi leal -64(%rsi,%rdi), %r8d | movq $-1, %rdi salq %cl, %rdx | salq %cl, %rdi testl %r8d, %r8d | cmpl %edx, %esi js .L88 | ja .L85 movl %r8d, %r9d < shrl $6, %r9d < leal 1(%r9), %esi < leaq (%rax,%rsi,8), %rsi < .L86: .L86: orq %rdx, (%rax) | subl %esi, %edx > orq %rdi, (%rax) > movl $64, %esi addq $8, %rax addq $8, %rax movq $-1, %rdx | movq $-1, %rdi cmpq %rsi, %rax | cmpl $63, %edx jne .L86 | ja .L86 sall $6, %r9d < subl %r9d, %r8d < .L85: .L85: testl %r8d, %r8d | testl %edx, %edx je .L84 je .L84 addl %edi, %ecx | leal (%r8,%r9), %ecx movq $-1, %rax | movq $-1, %rdx negl %ecx negl %ecx shrq %cl, %rax | shrq %cl, %rdx andq %rax, %rdx | andq %rdx, %rdi orq %rdx, (%rsi) | orq %rdi, (%rax) .L84: .L84: ret ret .L88: < movq %rax, %rsi < movl %edi, %r8d < jmp .L85 < .size __bitmap_set, .-__bitmap_set .size __bitmap_set, .-__bitmap_set .p2align 4 .p2align 4 .globl __bitmap_clear .globl __bitmap_clear .type __bitmap_clear, @function .type __bitmap_clear, @function ``` $ diff lib/bitmap.1.S lib/bitmap.2.S | diffstat unknown | 55 ++++++++++++++++++++++++------------------------------- 1 file changed, 24 insertions(+), 31 deletions(-) Signed-off-by: Paul Menzel --- v2: Update signature in header file v3: Adapt more functions to use unsigned int include/linux/bitmap.h | 20 ++++++++++---------- lib/bitmap.c | 26 +++++++++++++------------- 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/include/linux/bitmap.h b/include/linux/bitmap.h index 2e6cd5681040..9f1a097cd5d4 100644 --- a/include/linux/bitmap.h +++ b/include/linux/bitmap.h @@ -164,8 +164,8 @@ bool __bitmap_intersects(const unsigned long *bitmap1, bool __bitmap_subset(const unsigned long *bitmap1, const unsigned long *bitmap2, unsigned int nbits); int __bitmap_weight(const unsigned long *bitmap, unsigned int nbits); -void __bitmap_set(unsigned long *map, unsigned int start, int len); -void __bitmap_clear(unsigned long *map, unsigned int start, int len); +void __bitmap_set(unsigned long *map, unsigned int start, unsigned int len); +void __bitmap_clear(unsigned long *map, unsigned int start, unsigned int len); unsigned long bitmap_find_next_zero_area_off(unsigned long *map, unsigned long size, @@ -198,17 +198,17 @@ bitmap_find_next_zero_area(unsigned long *map, } int bitmap_parse(const char *buf, unsigned int buflen, - unsigned long *dst, int nbits); + unsigned long *dst, unsigned int nbits); int bitmap_parse_user(const char __user *ubuf, unsigned int ulen, - unsigned long *dst, int nbits); + unsigned long *dst, unsigned int nbits); int bitmap_parselist(const char *buf, unsigned long *maskp, - int nmaskbits); + unsigned int nmaskbits); int bitmap_parselist_user(const char __user *ubuf, unsigned int ulen, - unsigned long *dst, int nbits); + unsigned long *dst, unsigned int nbits); void bitmap_remap(unsigned long *dst, const unsigned long *src, const unsigned long *old, const unsigned long *new, unsigned int nbits); int bitmap_bitremap(int oldbit, - const unsigned long *old, const unsigned long *new, int bits); + const unsigned long *old, const unsigned long *new, unsigned int bits); void bitmap_onto(unsigned long *dst, const unsigned long *orig, const unsigned long *relmap, unsigned int bits); void bitmap_fold(unsigned long *dst, const unsigned long *orig, @@ -224,13 +224,13 @@ void bitmap_copy_le(unsigned long *dst, const unsigned long *src, unsigned int n #endif unsigned int bitmap_ord_to_pos(const unsigned long *bitmap, unsigned int ord, unsigned int nbits); int bitmap_print_to_pagebuf(bool list, char *buf, - const unsigned long *maskp, int nmaskbits); + const unsigned long *maskp, unsigned int nmaskbits); extern int bitmap_print_bitmask_to_buf(char *buf, const unsigned long *maskp, - int nmaskbits, loff_t off, size_t count); + unsigned int nmaskbits, loff_t off, size_t count); extern int bitmap_print_list_to_buf(char *buf, const unsigned long *maskp, - int nmaskbits, loff_t off, size_t count); + unsigned int nmaskbits, loff_t off, size_t count); #define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) & (BITS_PER_LONG - 1))) #define BITMAP_LAST_WORD_MASK(nbits) (~0UL >> (-(nbits) & (BITS_PER_LONG - 1))) diff --git a/lib/bitmap.c b/lib/bitmap.c index b18e31ea6e66..d529f7dffc48 100644 --- a/lib/bitmap.c +++ b/lib/bitmap.c @@ -348,14 +348,14 @@ int __bitmap_weight(const unsigned long *bitmap, unsigned int bits) } EXPORT_SYMBOL(__bitmap_weight); -void __bitmap_set(unsigned long *map, unsigned int start, int len) +void __bitmap_set(unsigned long *map, unsigned int start, unsigned int len) { unsigned long *p = map + BIT_WORD(start); const unsigned int size = start + len; int bits_to_set = BITS_PER_LONG - (start % BITS_PER_LONG); unsigned long mask_to_set = BITMAP_FIRST_WORD_MASK(start); - while (len - bits_to_set >= 0) { + while (len >= bits_to_set) { *p |= mask_to_set; len -= bits_to_set; bits_to_set = BITS_PER_LONG; @@ -369,7 +369,7 @@ void __bitmap_set(unsigned long *map, unsigned int start, int len) } EXPORT_SYMBOL(__bitmap_set); -void __bitmap_clear(unsigned long *map, unsigned int start, int len) +void __bitmap_clear(unsigned long *map, unsigned int start, unsigned int len) { unsigned long *p = map + BIT_WORD(start); const unsigned int size = start + len; @@ -445,7 +445,7 @@ EXPORT_SYMBOL(bitmap_find_next_zero_area_off); */ int bitmap_parse_user(const char __user *ubuf, unsigned int ulen, unsigned long *maskp, - int nmaskbits) + unsigned int nmaskbits) { char *buf; int ret; @@ -478,7 +478,7 @@ EXPORT_SYMBOL(bitmap_parse_user); * actually printed to @buf, excluding terminating '\0'. */ int bitmap_print_to_pagebuf(bool list, char *buf, const unsigned long *maskp, - int nmaskbits) + unsigned int nmaskbits) { ptrdiff_t len = PAGE_SIZE - offset_in_page(buf); @@ -499,7 +499,7 @@ EXPORT_SYMBOL(bitmap_print_to_pagebuf); * @count: the maximum number of bytes to print */ static int bitmap_print_to_buf(bool list, char *buf, const unsigned long *maskp, - int nmaskbits, loff_t off, size_t count) + unsigned int nmaskbits, loff_t off, size_t count) { const char *fmt = list ? "%*pbl\n" : "%*pb\n"; ssize_t size; @@ -542,7 +542,7 @@ static int bitmap_print_to_buf(bool list, char *buf, const unsigned long *maskp, * normal attribute with buf parameter and without offset, count:: * * bitmap_print_to_pagebuf(bool list, char *buf, const unsigned long *maskp, - * int nmaskbits) + * unsigned int nmaskbits) * { * } * @@ -600,7 +600,7 @@ static int bitmap_print_to_buf(bool list, char *buf, const unsigned long *maskp, * Returns the number of characters actually printed to @buf */ int bitmap_print_bitmask_to_buf(char *buf, const unsigned long *maskp, - int nmaskbits, loff_t off, size_t count) + unsigned int nmaskbits, loff_t off, size_t count) { return bitmap_print_to_buf(false, buf, maskp, nmaskbits, off, count); } @@ -618,7 +618,7 @@ EXPORT_SYMBOL(bitmap_print_bitmask_to_buf); * the print format. */ int bitmap_print_list_to_buf(char *buf, const unsigned long *maskp, - int nmaskbits, loff_t off, size_t count) + unsigned int nmaskbits, loff_t off, size_t count) { return bitmap_print_to_buf(true, buf, maskp, nmaskbits, off, count); } @@ -793,7 +793,7 @@ static const char *bitmap_parse_region(const char *str, struct region *r) * - ``-ERANGE``: bit number specified too large for mask * - ``-EOVERFLOW``: integer overflow in the input parameters */ -int bitmap_parselist(const char *buf, unsigned long *maskp, int nmaskbits) +int bitmap_parselist(const char *buf, unsigned long *maskp, unsigned int nmaskbits) { struct region r; long ret; @@ -836,7 +836,7 @@ EXPORT_SYMBOL(bitmap_parselist); */ int bitmap_parselist_user(const char __user *ubuf, unsigned int ulen, unsigned long *maskp, - int nmaskbits) + unsigned int nmaskbits) { char *buf; int ret; @@ -893,7 +893,7 @@ static const char *bitmap_get_x32_reverse(const char *start, * Leading, embedded and trailing whitespace accepted. */ int bitmap_parse(const char *start, unsigned int buflen, - unsigned long *maskp, int nmaskbits) + unsigned long *maskp, unsigned int nmaskbits) { const char *end = strnchrnul(start, buflen, '\n') - 1; int chunks = BITS_TO_U32(nmaskbits); @@ -1068,7 +1068,7 @@ EXPORT_SYMBOL(bitmap_remap); * returns 13. */ int bitmap_bitremap(int oldbit, const unsigned long *old, - const unsigned long *new, int bits) + const unsigned long *new, unsigned int bits) { int w = bitmap_weight(new, bits); int n = bitmap_pos_to_ord(old, oldbit, bits); -- 2.36.1