Received: by 2002:a25:2c96:0:0:0:0:0 with SMTP id s144csp83853ybs; Sat, 23 May 2020 22:08:12 -0700 (PDT) X-Google-Smtp-Source: ABdhPJys4zy2JRvfnTm8YOWhzFRS7PoCWOZvvVAjHb5J+xC0MHVcN86qMaewC2Ysmpuc3RH31ETX X-Received: by 2002:a50:a365:: with SMTP id 92mr9743098edn.220.1590296892507; Sat, 23 May 2020 22:08:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1590296892; cv=none; d=google.com; s=arc-20160816; b=WOgbed5A8aTvuQAzBY3XTT6eucRiCJj5dzI39GHlMcJu4j1zHrfL0kBbjMlR9/80Af O/jGswPrpU5O7R6oIIerHAnHsZf5zrKjoK620XuBtZhhM3XnCH/PfACiAdle7obXFB4W qqDdjN7z+hY+NAbNOo+7TwFt3az4NsxVtK7RpsDRXX3YmE20zV0meRF0ovi/NDteeGO9 Pj/hp2KSwnBs6v8tP2ogMTAHFVX/Ti+ewZSRdqtN+xQ6B5quS4E4ifMm4CogWZ/xovsi XtQRdJHP2qlvJzM4WUKEJ5hgj6LYTC0vxsScIywydw9Xv9AEO8o9ygYrZMQ8excT71gx CAxQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=B91dytTkvyz02DPT+HZFSDmK9s66G+Ful2iDXYepwVg=; b=YBDZllzFqXASentXP2yujgYfP2CD0znOBNWf6mbIRP8hKPzxRO/3n/VSvzElrFIe6r /bSdgmADRwMj9ZXBTwXKa8H14GegnfboYqmAichnjO8fecCnZVhvTJ1oFPSwK/M73FC8 //1EuKpSLRirKjPZumpatcTofGLww7gEPVfMiivkWcY5Q9eeERjzO+HnkFO+Wylfy05k SqJ1nuycP7OCVlpvAJCKbWddi8a2x4FUO9S4oN23LSdRQaGermqPTdP9P5RzB6tE7Z9u MbC6A9hhDG5kBi4r+9oEjUu+o/T1bux4HQBP3Ho7Evi7cZJvPzjF/xVnKHhCBl8SV+BD AcAA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=qTRoIZAW; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id u20si6998361edo.434.2020.05.23.22.07.19; Sat, 23 May 2020 22:08:12 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=qTRoIZAW; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726749AbgEXFCR (ORCPT + 99 others); Sun, 24 May 2020 01:02:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45102 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725805AbgEXFCR (ORCPT ); Sun, 24 May 2020 01:02:17 -0400 Received: from mail-pj1-x1041.google.com (mail-pj1-x1041.google.com [IPv6:2607:f8b0:4864:20::1041]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E085EC061A0E; Sat, 23 May 2020 22:02:16 -0700 (PDT) Received: by mail-pj1-x1041.google.com with SMTP id s69so6916256pjb.4; Sat, 23 May 2020 22:02:16 -0700 (PDT) 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=B91dytTkvyz02DPT+HZFSDmK9s66G+Ful2iDXYepwVg=; b=qTRoIZAWLTrkrBH6cM3Bf427OYQw81OxGndZ9Jkkagj84hsQWrHM5+issDwzXRQXAD SRkqTuDAM0aF8HYPKn8I3Tjs341MB98KRCHsx2/3+R7jHHZ1k2XKc5niaOfpY4Jd5dB1 LapvY4ZgIFauFe92Znl+7GyR4QMdgqntV6vLz6dSGmozA40tn/UgVf7XfmLW8X+dpHE7 yNVW6FCQgL8yhaQV313OUmaBlXrjaToDZMTXHOmzV79ogEvIHP2zE4t/gwHDGYXed3pP OkzOhocGUDZkXWIGmN8xUTxJ6syWlqQJ45kr5dhWSfDVaZ8HzSXflNI/bOaBXSAaFRET vXLw== 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=B91dytTkvyz02DPT+HZFSDmK9s66G+Ful2iDXYepwVg=; b=N7fPz+K0PNpZjGLAForGnuFZ9rMikZqYkuSzYvySdN4xNa08N5F0iGPzPwB9GPLnGd NvSPkwdMezfuiCrBltJBMGjcjirg04mrllVLlk2KyzhC5M1i/KXab7MWaej7Fh9G/lyj WWLHmcxXY94YxxDk8QC1YxV7nAlIQRp/nznWLL/sfmw95OmkS1Hm3rKJe/mnuCn7fxWH RC3vR5P7cmkylkdQygOadb/AZGpZ9NRGauR012PfWy42ZDRdvmlVetmT7bV0iMJl+jIZ 1qzrGTXVKMvHQDwwjW2uV6NGrCV7ALR+RZa8sTufXQeWxGcsY4MV7F71JXnq/9+o+ixN IeEw== X-Gm-Message-State: AOAM5319zPr2514WHR2A21s/Gc0EGb0EDSyNYQUzN41NqlEdZYgoSw1U 0rjXNkLlfkAFZp+rqqincW0= X-Received: by 2002:a17:90a:f087:: with SMTP id cn7mr12262337pjb.223.1590296536446; Sat, 23 May 2020 22:02:16 -0700 (PDT) Received: from syed ([106.223.122.111]) by smtp.gmail.com with ESMTPSA id i3sm10183366pfe.44.2020.05.23.22.02.12 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 23 May 2020 22:02:16 -0700 (PDT) Date: Sun, 24 May 2020 10:31:58 +0530 From: Syed Nayyar Waris To: linus.walleij@linaro.org, akpm@linux-foundation.org Cc: andriy.shevchenko@linux.intel.com, vilhelm.gray@gmail.com, arnd@arndb.de, linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v7 1/4] bitops: Introduce the the for_each_set_clump macro Message-ID: <17cb2b080b9c4c36cf84436bc5690739590acc53.1590017578.git.syednwaris@gmail.com> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.24 (2015-08-30) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This macro iterates for each group of bits (clump) with set bits, within a bitmap memory region. For each iteration, "start" is set to the bit offset of the found clump, while the respective clump value is stored to the location pointed by "clump". Additionally, the bitmap_get_value and bitmap_set_value functions are introduced to respectively get and set a value of n-bits in a bitmap memory region. The n-bits can have any size less than or equal to BITS_PER_LONG. Moreover, during setting value of n-bit in bitmap, if a situation arise that the width of next n-bit is exceeding the word boundary, then it will divide itself such that some portion of it is stored in that word, while the remaining portion is stored in the next higher word. Similar situation occurs while retrieving value of n-bits from bitmap. Cc: Arnd Bergmann Signed-off-by: Syed Nayyar Waris Reviewed-by: Andy Shevchenko Signed-off-by: William Breathitt Gray --- Changes in v7: - No change. Changes in v6: - No change. Changes in v5: - No change. Changes in v4: - No change. Changes in v3: - No change. Changes in v2: - No change. include/asm-generic/bitops/find.h | 19 ++++++++++ include/linux/bitmap.h | 61 +++++++++++++++++++++++++++++++ include/linux/bitops.h | 13 +++++++ lib/find_bit.c | 14 +++++++ 4 files changed, 107 insertions(+) diff --git a/include/asm-generic/bitops/find.h b/include/asm-generic/bitops/find.h index 9fdf21302fdf..4e6600759455 100644 --- a/include/asm-generic/bitops/find.h +++ b/include/asm-generic/bitops/find.h @@ -97,4 +97,23 @@ extern unsigned long find_next_clump8(unsigned long *clump, #define find_first_clump8(clump, bits, size) \ find_next_clump8((clump), (bits), (size), 0) +/** + * find_next_clump - find next clump with set bits in a memory region + * @clump: location to store copy of found clump + * @addr: address to base the search on + * @size: bitmap size in number of bits + * @offset: bit offset at which to start searching + * @clump_size: clump size in bits + * + * Returns the bit offset for the next set clump; the found clump value is + * copied to the location pointed by @clump. If no bits are set, returns @size. + */ +extern unsigned long find_next_clump(unsigned long *clump, + const unsigned long *addr, + unsigned long size, unsigned long offset, + unsigned long clump_size); + +#define find_first_clump(clump, bits, size, clump_size) \ + find_next_clump((clump), (bits), (size), 0, (clump_size)) + #endif /*_ASM_GENERIC_BITOPS_FIND_H_ */ diff --git a/include/linux/bitmap.h b/include/linux/bitmap.h index 99058eb81042..7ab2c65fc964 100644 --- a/include/linux/bitmap.h +++ b/include/linux/bitmap.h @@ -75,7 +75,11 @@ * bitmap_from_arr32(dst, buf, nbits) Copy nbits from u32[] buf to dst * bitmap_to_arr32(buf, src, nbits) Copy nbits from buf to u32[] dst * bitmap_get_value8(map, start) Get 8bit value from map at start + * 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 * * Note, bitmap_zero() and bitmap_fill() operate over the region of * unsigned longs, that is, bits behind bitmap till the unsigned long @@ -563,6 +567,34 @@ static inline unsigned long bitmap_get_value8(const unsigned long *map, return (map[index] >> offset) & 0xFF; } +/** + * bitmap_get_value - get a value of n-bits from the memory region + * @map: address to the bitmap memory region + * @start: bit offset of the n-bit value + * @nbits: size of value in bits + * + * Returns value of nbits located at the @start bit offset within the @map + * memory region. + */ +static inline unsigned long bitmap_get_value(const unsigned long *map, + unsigned long start, + unsigned long nbits) +{ + const size_t index = BIT_WORD(start); + const unsigned long offset = start % BITS_PER_LONG; + const unsigned long ceiling = roundup(start + 1, BITS_PER_LONG); + const unsigned long space = ceiling - start; + unsigned long value_low, value_high; + + if (space >= nbits) + return (map[index] >> offset) & GENMASK(nbits - 1, 0); + else { + value_low = map[index] & BITMAP_FIRST_WORD_MASK(start); + value_high = map[index + 1] & BITMAP_LAST_WORD_MASK(start + nbits); + return (value_low >> offset) | (value_high << space); + } +} + /** * bitmap_set_value8 - set an 8-bit value within a memory region * @map: address to the bitmap memory region @@ -579,6 +611,35 @@ static inline void bitmap_set_value8(unsigned long *map, unsigned long value, map[index] |= value << offset; } +/** + * bitmap_set_value - set n-bit 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 + */ +static inline void bitmap_set_value(unsigned long *map, + unsigned long value, + unsigned long start, unsigned long nbits) +{ + const size_t index = BIT_WORD(start); + const unsigned long offset = start % BITS_PER_LONG; + const unsigned long ceiling = roundup(start + 1, BITS_PER_LONG); + const unsigned long space = ceiling - start; + + value &= GENMASK(nbits - 1, 0); + + if (space >= nbits) { + map[index] &= ~(GENMASK(nbits + offset - 1, offset)); + map[index] |= value << offset; + } else { + map[index] &= ~BITMAP_FIRST_WORD_MASK(start); + map[index] |= value << offset; + map[index + 1] &= ~BITMAP_LAST_WORD_MASK(start + nbits); + map[index + 1] |= (value >> space); + } +} + #endif /* __ASSEMBLY__ */ #endif /* __LINUX_BITMAP_H */ diff --git a/include/linux/bitops.h b/include/linux/bitops.h index 9acf654f0b19..41c2d9ce63e7 100644 --- a/include/linux/bitops.h +++ b/include/linux/bitops.h @@ -62,6 +62,19 @@ extern unsigned long __sw_hweight64(__u64 w); (start) < (size); \ (start) = find_next_clump8(&(clump), (bits), (size), (start) + 8)) +/** + * for_each_set_clump - iterate over bitmap for each clump with set bits + * @start: bit offset to start search and to store the current iteration offset + * @clump: location to store copy of current 8-bit clump + * @bits: bitmap address to base the search on + * @size: bitmap size in number of bits + * @clump_size: clump size in bits + */ +#define for_each_set_clump(start, clump, bits, size, clump_size) \ + for ((start) = find_first_clump(&(clump), (bits), (size), (clump_size)); \ + (start) < (size); \ + (start) = find_next_clump(&(clump), (bits), (size), (start) + (clump_size), (clump_size))) + static inline int get_bitmask_order(unsigned int count) { int order; diff --git a/lib/find_bit.c b/lib/find_bit.c index 49f875f1baf7..1341bd39b32a 100644 --- a/lib/find_bit.c +++ b/lib/find_bit.c @@ -190,3 +190,17 @@ unsigned long find_next_clump8(unsigned long *clump, const unsigned long *addr, return offset; } EXPORT_SYMBOL(find_next_clump8); + +unsigned long find_next_clump(unsigned long *clump, const unsigned long *addr, + unsigned long size, unsigned long offset, + unsigned long clump_size) +{ + offset = find_next_bit(addr, size, offset); + if (offset == size) + return size; + + offset = rounddown(offset, clump_size); + *clump = bitmap_get_value(addr, offset, clump_size); + return offset; +} +EXPORT_SYMBOL(find_next_clump); -- 2.26.2