// SPDX-License-Identifier: GPL-2.0-only1/*2* lib/bitmap.c3* Helper functions for bitmap.h.4*/56#include <linux/bitmap.h>7#include <linux/bitops.h>8#include <linux/ctype.h>9#include <linux/device.h>10#include <linux/export.h>11#include <linux/slab.h>1213/**14* DOC: bitmap introduction15*16* bitmaps provide an array of bits, implemented using an17* array of unsigned longs. The number of valid bits in a18* given bitmap does _not_ need to be an exact multiple of19* BITS_PER_LONG.20*21* The possible unused bits in the last, partially used word22* of a bitmap are 'don't care'. The implementation makes23* no particular effort to keep them zero. It ensures that24* their value will not affect the results of any operation.25* The bitmap operations that return Boolean (bitmap_empty,26* for example) or scalar (bitmap_weight, for example) results27* carefully filter out these unused bits from impacting their28* results.29*30* The byte ordering of bitmaps is more natural on little31* endian architectures. See the big-endian headers32* include/asm-ppc64/bitops.h and include/asm-s390/bitops.h33* for the best explanations of this ordering.34*/3536bool __bitmap_equal(const unsigned long *bitmap1,37const unsigned long *bitmap2, unsigned int bits)38{39unsigned int k, lim = bits/BITS_PER_LONG;40for (k = 0; k < lim; ++k)41if (bitmap1[k] != bitmap2[k])42return false;4344if (bits % BITS_PER_LONG)45if ((bitmap1[k] ^ bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))46return false;4748return true;49}50EXPORT_SYMBOL(__bitmap_equal);5152bool __bitmap_or_equal(const unsigned long *bitmap1,53const unsigned long *bitmap2,54const unsigned long *bitmap3,55unsigned int bits)56{57unsigned int k, lim = bits / BITS_PER_LONG;58unsigned long tmp;5960for (k = 0; k < lim; ++k) {61if ((bitmap1[k] | bitmap2[k]) != bitmap3[k])62return false;63}6465if (!(bits % BITS_PER_LONG))66return true;6768tmp = (bitmap1[k] | bitmap2[k]) ^ bitmap3[k];69return (tmp & BITMAP_LAST_WORD_MASK(bits)) == 0;70}7172void __bitmap_complement(unsigned long *dst, const unsigned long *src, unsigned int bits)73{74unsigned int k, lim = BITS_TO_LONGS(bits);75for (k = 0; k < lim; ++k)76dst[k] = ~src[k];77}78EXPORT_SYMBOL(__bitmap_complement);7980/**81* __bitmap_shift_right - logical right shift of the bits in a bitmap82* @dst : destination bitmap83* @src : source bitmap84* @shift : shift by this many bits85* @nbits : bitmap size, in bits86*87* Shifting right (dividing) means moving bits in the MS -> LS bit88* direction. Zeros are fed into the vacated MS positions and the89* LS bits shifted off the bottom are lost.90*/91void __bitmap_shift_right(unsigned long *dst, const unsigned long *src,92unsigned shift, unsigned nbits)93{94unsigned k, lim = BITS_TO_LONGS(nbits);95unsigned off = shift/BITS_PER_LONG, rem = shift % BITS_PER_LONG;96unsigned long mask = BITMAP_LAST_WORD_MASK(nbits);97for (k = 0; off + k < lim; ++k) {98unsigned long upper, lower;99100/*101* If shift is not word aligned, take lower rem bits of102* word above and make them the top rem bits of result.103*/104if (!rem || off + k + 1 >= lim)105upper = 0;106else {107upper = src[off + k + 1];108if (off + k + 1 == lim - 1)109upper &= mask;110upper <<= (BITS_PER_LONG - rem);111}112lower = src[off + k];113if (off + k == lim - 1)114lower &= mask;115lower >>= rem;116dst[k] = lower | upper;117}118if (off)119memset(&dst[lim - off], 0, off*sizeof(unsigned long));120}121EXPORT_SYMBOL(__bitmap_shift_right);122123124/**125* __bitmap_shift_left - logical left shift of the bits in a bitmap126* @dst : destination bitmap127* @src : source bitmap128* @shift : shift by this many bits129* @nbits : bitmap size, in bits130*131* Shifting left (multiplying) means moving bits in the LS -> MS132* direction. Zeros are fed into the vacated LS bit positions133* and those MS bits shifted off the top are lost.134*/135136void __bitmap_shift_left(unsigned long *dst, const unsigned long *src,137unsigned int shift, unsigned int nbits)138{139int k;140unsigned int lim = BITS_TO_LONGS(nbits);141unsigned int off = shift/BITS_PER_LONG, rem = shift % BITS_PER_LONG;142for (k = lim - off - 1; k >= 0; --k) {143unsigned long upper, lower;144145/*146* If shift is not word aligned, take upper rem bits of147* word below and make them the bottom rem bits of result.148*/149if (rem && k > 0)150lower = src[k - 1] >> (BITS_PER_LONG - rem);151else152lower = 0;153upper = src[k] << rem;154dst[k + off] = lower | upper;155}156if (off)157memset(dst, 0, off*sizeof(unsigned long));158}159EXPORT_SYMBOL(__bitmap_shift_left);160161/**162* bitmap_cut() - remove bit region from bitmap and right shift remaining bits163* @dst: destination bitmap, might overlap with src164* @src: source bitmap165* @first: start bit of region to be removed166* @cut: number of bits to remove167* @nbits: bitmap size, in bits168*169* Set the n-th bit of @dst iff the n-th bit of @src is set and170* n is less than @first, or the m-th bit of @src is set for any171* m such that @first <= n < nbits, and m = n + @cut.172*173* In pictures, example for a big-endian 32-bit architecture:174*175* The @src bitmap is::176*177* 31 63178* | |179* 10000000 11000001 11110010 00010101 10000000 11000001 01110010 00010101180* | | | |181* 16 14 0 32182*183* if @cut is 3, and @first is 14, bits 14-16 in @src are cut and @dst is::184*185* 31 63186* | |187* 10110000 00011000 00110010 00010101 00010000 00011000 00101110 01000010188* | | |189* 14 (bit 17 0 32190* from @src)191*192* Note that @dst and @src might overlap partially or entirely.193*194* This is implemented in the obvious way, with a shift and carry195* step for each moved bit. Optimisation is left as an exercise196* for the compiler.197*/198void bitmap_cut(unsigned long *dst, const unsigned long *src,199unsigned int first, unsigned int cut, unsigned int nbits)200{201unsigned int len = BITS_TO_LONGS(nbits);202unsigned long keep = 0, carry;203int i;204205if (first % BITS_PER_LONG) {206keep = src[first / BITS_PER_LONG] &207(~0UL >> (BITS_PER_LONG - first % BITS_PER_LONG));208}209210memmove(dst, src, len * sizeof(*dst));211212while (cut--) {213for (i = first / BITS_PER_LONG; i < len; i++) {214if (i < len - 1)215carry = dst[i + 1] & 1UL;216else217carry = 0;218219dst[i] = (dst[i] >> 1) | (carry << (BITS_PER_LONG - 1));220}221}222223dst[first / BITS_PER_LONG] &= ~0UL << (first % BITS_PER_LONG);224dst[first / BITS_PER_LONG] |= keep;225}226EXPORT_SYMBOL(bitmap_cut);227228bool __bitmap_and(unsigned long *dst, const unsigned long *bitmap1,229const unsigned long *bitmap2, unsigned int bits)230{231unsigned int k;232unsigned int lim = bits/BITS_PER_LONG;233unsigned long result = 0;234235for (k = 0; k < lim; k++)236result |= (dst[k] = bitmap1[k] & bitmap2[k]);237if (bits % BITS_PER_LONG)238result |= (dst[k] = bitmap1[k] & bitmap2[k] &239BITMAP_LAST_WORD_MASK(bits));240return result != 0;241}242EXPORT_SYMBOL(__bitmap_and);243244void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1,245const unsigned long *bitmap2, unsigned int bits)246{247unsigned int k;248unsigned int nr = BITS_TO_LONGS(bits);249250for (k = 0; k < nr; k++)251dst[k] = bitmap1[k] | bitmap2[k];252}253EXPORT_SYMBOL(__bitmap_or);254255void __bitmap_xor(unsigned long *dst, const unsigned long *bitmap1,256const unsigned long *bitmap2, unsigned int bits)257{258unsigned int k;259unsigned int nr = BITS_TO_LONGS(bits);260261for (k = 0; k < nr; k++)262dst[k] = bitmap1[k] ^ bitmap2[k];263}264EXPORT_SYMBOL(__bitmap_xor);265266bool __bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1,267const unsigned long *bitmap2, unsigned int bits)268{269unsigned int k;270unsigned int lim = bits/BITS_PER_LONG;271unsigned long result = 0;272273for (k = 0; k < lim; k++)274result |= (dst[k] = bitmap1[k] & ~bitmap2[k]);275if (bits % BITS_PER_LONG)276result |= (dst[k] = bitmap1[k] & ~bitmap2[k] &277BITMAP_LAST_WORD_MASK(bits));278return result != 0;279}280EXPORT_SYMBOL(__bitmap_andnot);281282void __bitmap_replace(unsigned long *dst,283const unsigned long *old, const unsigned long *new,284const unsigned long *mask, unsigned int nbits)285{286unsigned int k;287unsigned int nr = BITS_TO_LONGS(nbits);288289for (k = 0; k < nr; k++)290dst[k] = (old[k] & ~mask[k]) | (new[k] & mask[k]);291}292EXPORT_SYMBOL(__bitmap_replace);293294bool __bitmap_intersects(const unsigned long *bitmap1,295const unsigned long *bitmap2, unsigned int bits)296{297unsigned int k, lim = bits/BITS_PER_LONG;298for (k = 0; k < lim; ++k)299if (bitmap1[k] & bitmap2[k])300return true;301302if (bits % BITS_PER_LONG)303if ((bitmap1[k] & bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))304return true;305return false;306}307EXPORT_SYMBOL(__bitmap_intersects);308309bool __bitmap_subset(const unsigned long *bitmap1,310const unsigned long *bitmap2, unsigned int bits)311{312unsigned int k, lim = bits/BITS_PER_LONG;313for (k = 0; k < lim; ++k)314if (bitmap1[k] & ~bitmap2[k])315return false;316317if (bits % BITS_PER_LONG)318if ((bitmap1[k] & ~bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))319return false;320return true;321}322EXPORT_SYMBOL(__bitmap_subset);323324#define BITMAP_WEIGHT(FETCH, bits) \325({ \326unsigned int __bits = (bits), idx, w = 0; \327\328for (idx = 0; idx < __bits / BITS_PER_LONG; idx++) \329w += hweight_long(FETCH); \330\331if (__bits % BITS_PER_LONG) \332w += hweight_long((FETCH) & BITMAP_LAST_WORD_MASK(__bits)); \333\334w; \335})336337unsigned int __bitmap_weight(const unsigned long *bitmap, unsigned int bits)338{339return BITMAP_WEIGHT(bitmap[idx], bits);340}341EXPORT_SYMBOL(__bitmap_weight);342343unsigned int __bitmap_weight_and(const unsigned long *bitmap1,344const unsigned long *bitmap2, unsigned int bits)345{346return BITMAP_WEIGHT(bitmap1[idx] & bitmap2[idx], bits);347}348EXPORT_SYMBOL(__bitmap_weight_and);349350unsigned int __bitmap_weight_andnot(const unsigned long *bitmap1,351const unsigned long *bitmap2, unsigned int bits)352{353return BITMAP_WEIGHT(bitmap1[idx] & ~bitmap2[idx], bits);354}355EXPORT_SYMBOL(__bitmap_weight_andnot);356357unsigned int __bitmap_weighted_or(unsigned long *dst, const unsigned long *bitmap1,358const unsigned long *bitmap2, unsigned int bits)359{360return BITMAP_WEIGHT(({dst[idx] = bitmap1[idx] | bitmap2[idx]; dst[idx]; }), bits);361}362363void __bitmap_set(unsigned long *map, unsigned int start, int len)364{365unsigned long *p = map + BIT_WORD(start);366const unsigned int size = start + len;367int bits_to_set = BITS_PER_LONG - (start % BITS_PER_LONG);368unsigned long mask_to_set = BITMAP_FIRST_WORD_MASK(start);369370while (len - bits_to_set >= 0) {371*p |= mask_to_set;372len -= bits_to_set;373bits_to_set = BITS_PER_LONG;374mask_to_set = ~0UL;375p++;376}377if (len) {378mask_to_set &= BITMAP_LAST_WORD_MASK(size);379*p |= mask_to_set;380}381}382EXPORT_SYMBOL(__bitmap_set);383384void __bitmap_clear(unsigned long *map, unsigned int start, int len)385{386unsigned long *p = map + BIT_WORD(start);387const unsigned int size = start + len;388int bits_to_clear = BITS_PER_LONG - (start % BITS_PER_LONG);389unsigned long mask_to_clear = BITMAP_FIRST_WORD_MASK(start);390391while (len - bits_to_clear >= 0) {392*p &= ~mask_to_clear;393len -= bits_to_clear;394bits_to_clear = BITS_PER_LONG;395mask_to_clear = ~0UL;396p++;397}398if (len) {399mask_to_clear &= BITMAP_LAST_WORD_MASK(size);400*p &= ~mask_to_clear;401}402}403EXPORT_SYMBOL(__bitmap_clear);404405/**406* bitmap_find_next_zero_area_off - find a contiguous aligned zero area407* @map: The address to base the search on408* @size: The bitmap size in bits409* @start: The bitnumber to start searching at410* @nr: The number of zeroed bits we're looking for411* @align_mask: Alignment mask for zero area412* @align_offset: Alignment offset for zero area.413*414* The @align_mask should be one less than a power of 2; the effect is that415* the bit offset of all zero areas this function finds plus @align_offset416* is multiple of that power of 2.417*/418unsigned long bitmap_find_next_zero_area_off(unsigned long *map,419unsigned long size,420unsigned long start,421unsigned int nr,422unsigned long align_mask,423unsigned long align_offset)424{425unsigned long index, end, i;426again:427index = find_next_zero_bit(map, size, start);428429/* Align allocation */430index = __ALIGN_MASK(index + align_offset, align_mask) - align_offset;431432end = index + nr;433if (end > size)434return end;435i = find_next_bit(map, end, index);436if (i < end) {437start = i + 1;438goto again;439}440return index;441}442EXPORT_SYMBOL(bitmap_find_next_zero_area_off);443444/**445* bitmap_pos_to_ord - find ordinal of set bit at given position in bitmap446* @buf: pointer to a bitmap447* @pos: a bit position in @buf (0 <= @pos < @nbits)448* @nbits: number of valid bit positions in @buf449*450* Map the bit at position @pos in @buf (of length @nbits) to the451* ordinal of which set bit it is. If it is not set or if @pos452* is not a valid bit position, map to -1.453*454* If for example, just bits 4 through 7 are set in @buf, then @pos455* values 4 through 7 will get mapped to 0 through 3, respectively,456* and other @pos values will get mapped to -1. When @pos value 7457* gets mapped to (returns) @ord value 3 in this example, that means458* that bit 7 is the 3rd (starting with 0th) set bit in @buf.459*460* The bit positions 0 through @bits are valid positions in @buf.461*/462static int bitmap_pos_to_ord(const unsigned long *buf, unsigned int pos, unsigned int nbits)463{464if (pos >= nbits || !test_bit(pos, buf))465return -1;466467return bitmap_weight(buf, pos);468}469470/**471* bitmap_remap - Apply map defined by a pair of bitmaps to another bitmap472* @dst: remapped result473* @src: subset to be remapped474* @old: defines domain of map475* @new: defines range of map476* @nbits: number of bits in each of these bitmaps477*478* Let @old and @new define a mapping of bit positions, such that479* whatever position is held by the n-th set bit in @old is mapped480* to the n-th set bit in @new. In the more general case, allowing481* for the possibility that the weight 'w' of @new is less than the482* weight of @old, map the position of the n-th set bit in @old to483* the position of the m-th set bit in @new, where m == n % w.484*485* If either of the @old and @new bitmaps are empty, or if @src and486* @dst point to the same location, then this routine copies @src487* to @dst.488*489* The positions of unset bits in @old are mapped to themselves490* (the identity map).491*492* Apply the above specified mapping to @src, placing the result in493* @dst, clearing any bits previously set in @dst.494*495* For example, lets say that @old has bits 4 through 7 set, and496* @new has bits 12 through 15 set. This defines the mapping of bit497* position 4 to 12, 5 to 13, 6 to 14 and 7 to 15, and of all other498* bit positions unchanged. So if say @src comes into this routine499* with bits 1, 5 and 7 set, then @dst should leave with bits 1,500* 13 and 15 set.501*/502void bitmap_remap(unsigned long *dst, const unsigned long *src,503const unsigned long *old, const unsigned long *new,504unsigned int nbits)505{506unsigned int oldbit, w;507508if (dst == src) /* following doesn't handle inplace remaps */509return;510bitmap_zero(dst, nbits);511512w = bitmap_weight(new, nbits);513for_each_set_bit(oldbit, src, nbits) {514int n = bitmap_pos_to_ord(old, oldbit, nbits);515516if (n < 0 || w == 0)517set_bit(oldbit, dst); /* identity map */518else519set_bit(find_nth_bit(new, nbits, n % w), dst);520}521}522EXPORT_SYMBOL(bitmap_remap);523524/**525* bitmap_bitremap - Apply map defined by a pair of bitmaps to a single bit526* @oldbit: bit position to be mapped527* @old: defines domain of map528* @new: defines range of map529* @bits: number of bits in each of these bitmaps530*531* Let @old and @new define a mapping of bit positions, such that532* whatever position is held by the n-th set bit in @old is mapped533* to the n-th set bit in @new. In the more general case, allowing534* for the possibility that the weight 'w' of @new is less than the535* weight of @old, map the position of the n-th set bit in @old to536* the position of the m-th set bit in @new, where m == n % w.537*538* The positions of unset bits in @old are mapped to themselves539* (the identity map).540*541* Apply the above specified mapping to bit position @oldbit, returning542* the new bit position.543*544* For example, lets say that @old has bits 4 through 7 set, and545* @new has bits 12 through 15 set. This defines the mapping of bit546* position 4 to 12, 5 to 13, 6 to 14 and 7 to 15, and of all other547* bit positions unchanged. So if say @oldbit is 5, then this routine548* returns 13.549*/550int bitmap_bitremap(int oldbit, const unsigned long *old,551const unsigned long *new, int bits)552{553int w = bitmap_weight(new, bits);554int n = bitmap_pos_to_ord(old, oldbit, bits);555if (n < 0 || w == 0)556return oldbit;557else558return find_nth_bit(new, bits, n % w);559}560EXPORT_SYMBOL(bitmap_bitremap);561562#ifdef CONFIG_NUMA563/**564* bitmap_onto - translate one bitmap relative to another565* @dst: resulting translated bitmap566* @orig: original untranslated bitmap567* @relmap: bitmap relative to which translated568* @bits: number of bits in each of these bitmaps569*570* Set the n-th bit of @dst iff there exists some m such that the571* n-th bit of @relmap is set, the m-th bit of @orig is set, and572* the n-th bit of @relmap is also the m-th _set_ bit of @relmap.573* (If you understood the previous sentence the first time your574* read it, you're overqualified for your current job.)575*576* In other words, @orig is mapped onto (surjectively) @dst,577* using the map { <n, m> | the n-th bit of @relmap is the578* m-th set bit of @relmap }.579*580* Any set bits in @orig above bit number W, where W is the581* weight of (number of set bits in) @relmap are mapped nowhere.582* In particular, if for all bits m set in @orig, m >= W, then583* @dst will end up empty. In situations where the possibility584* of such an empty result is not desired, one way to avoid it is585* to use the bitmap_fold() operator, below, to first fold the586* @orig bitmap over itself so that all its set bits x are in the587* range 0 <= x < W. The bitmap_fold() operator does this by588* setting the bit (m % W) in @dst, for each bit (m) set in @orig.589*590* Example [1] for bitmap_onto():591* Let's say @relmap has bits 30-39 set, and @orig has bits592* 1, 3, 5, 7, 9 and 11 set. Then on return from this routine,593* @dst will have bits 31, 33, 35, 37 and 39 set.594*595* When bit 0 is set in @orig, it means turn on the bit in596* @dst corresponding to whatever is the first bit (if any)597* that is turned on in @relmap. Since bit 0 was off in the598* above example, we leave off that bit (bit 30) in @dst.599*600* When bit 1 is set in @orig (as in the above example), it601* means turn on the bit in @dst corresponding to whatever602* is the second bit that is turned on in @relmap. The second603* bit in @relmap that was turned on in the above example was604* bit 31, so we turned on bit 31 in @dst.605*606* Similarly, we turned on bits 33, 35, 37 and 39 in @dst,607* because they were the 4th, 6th, 8th and 10th set bits608* set in @relmap, and the 4th, 6th, 8th and 10th bits of609* @orig (i.e. bits 3, 5, 7 and 9) were also set.610*611* When bit 11 is set in @orig, it means turn on the bit in612* @dst corresponding to whatever is the twelfth bit that is613* turned on in @relmap. In the above example, there were614* only ten bits turned on in @relmap (30..39), so that bit615* 11 was set in @orig had no affect on @dst.616*617* Example [2] for bitmap_fold() + bitmap_onto():618* Let's say @relmap has these ten bits set::619*620* 40 41 42 43 45 48 53 61 74 95621*622* (for the curious, that's 40 plus the first ten terms of the623* Fibonacci sequence.)624*625* Further lets say we use the following code, invoking626* bitmap_fold() then bitmap_onto, as suggested above to627* avoid the possibility of an empty @dst result::628*629* unsigned long *tmp; // a temporary bitmap's bits630*631* bitmap_fold(tmp, orig, bitmap_weight(relmap, bits), bits);632* bitmap_onto(dst, tmp, relmap, bits);633*634* Then this table shows what various values of @dst would be, for635* various @orig's. I list the zero-based positions of each set bit.636* The tmp column shows the intermediate result, as computed by637* using bitmap_fold() to fold the @orig bitmap modulo ten638* (the weight of @relmap):639*640* =============== ============== =================641* @orig tmp @dst642* 0 0 40643* 1 1 41644* 9 9 95645* 10 0 40 [#f1]_646* 1 3 5 7 1 3 5 7 41 43 48 61647* 0 1 2 3 4 0 1 2 3 4 40 41 42 43 45648* 0 9 18 27 0 9 8 7 40 61 74 95649* 0 10 20 30 0 40650* 0 11 22 33 0 1 2 3 40 41 42 43651* 0 12 24 36 0 2 4 6 40 42 45 53652* 78 102 211 1 2 8 41 42 74 [#f1]_653* =============== ============== =================654*655* .. [#f1]656*657* For these marked lines, if we hadn't first done bitmap_fold()658* into tmp, then the @dst result would have been empty.659*660* If either of @orig or @relmap is empty (no set bits), then @dst661* will be returned empty.662*663* If (as explained above) the only set bits in @orig are in positions664* m where m >= W, (where W is the weight of @relmap) then @dst will665* once again be returned empty.666*667* All bits in @dst not set by the above rule are cleared.668*/669void bitmap_onto(unsigned long *dst, const unsigned long *orig,670const unsigned long *relmap, unsigned int bits)671{672unsigned int n, m; /* same meaning as in above comment */673674if (dst == orig) /* following doesn't handle inplace mappings */675return;676bitmap_zero(dst, bits);677678/*679* The following code is a more efficient, but less680* obvious, equivalent to the loop:681* for (m = 0; m < bitmap_weight(relmap, bits); m++) {682* n = find_nth_bit(orig, bits, m);683* if (test_bit(m, orig))684* set_bit(n, dst);685* }686*/687688m = 0;689for_each_set_bit(n, relmap, bits) {690/* m == bitmap_pos_to_ord(relmap, n, bits) */691if (test_bit(m, orig))692set_bit(n, dst);693m++;694}695}696697/**698* bitmap_fold - fold larger bitmap into smaller, modulo specified size699* @dst: resulting smaller bitmap700* @orig: original larger bitmap701* @sz: specified size702* @nbits: number of bits in each of these bitmaps703*704* For each bit oldbit in @orig, set bit oldbit mod @sz in @dst.705* Clear all other bits in @dst. See further the comment and706* Example [2] for bitmap_onto() for why and how to use this.707*/708void bitmap_fold(unsigned long *dst, const unsigned long *orig,709unsigned int sz, unsigned int nbits)710{711unsigned int oldbit;712713if (dst == orig) /* following doesn't handle inplace mappings */714return;715bitmap_zero(dst, nbits);716717for_each_set_bit(oldbit, orig, nbits)718set_bit(oldbit % sz, dst);719}720#endif /* CONFIG_NUMA */721722unsigned long *bitmap_alloc(unsigned int nbits, gfp_t flags)723{724return kmalloc_array(BITS_TO_LONGS(nbits), sizeof(unsigned long),725flags);726}727EXPORT_SYMBOL(bitmap_alloc);728729unsigned long *bitmap_zalloc(unsigned int nbits, gfp_t flags)730{731return bitmap_alloc(nbits, flags | __GFP_ZERO);732}733EXPORT_SYMBOL(bitmap_zalloc);734735unsigned long *bitmap_alloc_node(unsigned int nbits, gfp_t flags, int node)736{737return kmalloc_array_node(BITS_TO_LONGS(nbits), sizeof(unsigned long),738flags, node);739}740EXPORT_SYMBOL(bitmap_alloc_node);741742unsigned long *bitmap_zalloc_node(unsigned int nbits, gfp_t flags, int node)743{744return bitmap_alloc_node(nbits, flags | __GFP_ZERO, node);745}746EXPORT_SYMBOL(bitmap_zalloc_node);747748void bitmap_free(const unsigned long *bitmap)749{750kfree(bitmap);751}752EXPORT_SYMBOL(bitmap_free);753754static void devm_bitmap_free(void *data)755{756unsigned long *bitmap = data;757758bitmap_free(bitmap);759}760761unsigned long *devm_bitmap_alloc(struct device *dev,762unsigned int nbits, gfp_t flags)763{764unsigned long *bitmap;765int ret;766767bitmap = bitmap_alloc(nbits, flags);768if (!bitmap)769return NULL;770771ret = devm_add_action_or_reset(dev, devm_bitmap_free, bitmap);772if (ret)773return NULL;774775return bitmap;776}777EXPORT_SYMBOL_GPL(devm_bitmap_alloc);778779unsigned long *devm_bitmap_zalloc(struct device *dev,780unsigned int nbits, gfp_t flags)781{782return devm_bitmap_alloc(dev, nbits, flags | __GFP_ZERO);783}784EXPORT_SYMBOL_GPL(devm_bitmap_zalloc);785786#if BITS_PER_LONG == 64787/**788* bitmap_from_arr32 - copy the contents of u32 array of bits to bitmap789* @bitmap: array of unsigned longs, the destination bitmap790* @buf: array of u32 (in host byte order), the source bitmap791* @nbits: number of bits in @bitmap792*/793void bitmap_from_arr32(unsigned long *bitmap, const u32 *buf, unsigned int nbits)794{795unsigned int i, halfwords;796797halfwords = DIV_ROUND_UP(nbits, 32);798for (i = 0; i < halfwords; i++) {799bitmap[i/2] = (unsigned long) buf[i];800if (++i < halfwords)801bitmap[i/2] |= ((unsigned long) buf[i]) << 32;802}803804/* Clear tail bits in last word beyond nbits. */805if (nbits % BITS_PER_LONG)806bitmap[(halfwords - 1) / 2] &= BITMAP_LAST_WORD_MASK(nbits);807}808EXPORT_SYMBOL(bitmap_from_arr32);809810/**811* bitmap_to_arr32 - copy the contents of bitmap to a u32 array of bits812* @buf: array of u32 (in host byte order), the dest bitmap813* @bitmap: array of unsigned longs, the source bitmap814* @nbits: number of bits in @bitmap815*/816void bitmap_to_arr32(u32 *buf, const unsigned long *bitmap, unsigned int nbits)817{818unsigned int i, halfwords;819820halfwords = DIV_ROUND_UP(nbits, 32);821for (i = 0; i < halfwords; i++) {822buf[i] = (u32) (bitmap[i/2] & UINT_MAX);823if (++i < halfwords)824buf[i] = (u32) (bitmap[i/2] >> 32);825}826827/* Clear tail bits in last element of array beyond nbits. */828if (nbits % BITS_PER_LONG)829buf[halfwords - 1] &= (u32) (UINT_MAX >> ((-nbits) & 31));830}831EXPORT_SYMBOL(bitmap_to_arr32);832#endif833834#if BITS_PER_LONG == 32835/**836* bitmap_from_arr64 - copy the contents of u64 array of bits to bitmap837* @bitmap: array of unsigned longs, the destination bitmap838* @buf: array of u64 (in host byte order), the source bitmap839* @nbits: number of bits in @bitmap840*/841void bitmap_from_arr64(unsigned long *bitmap, const u64 *buf, unsigned int nbits)842{843int n;844845for (n = nbits; n > 0; n -= 64) {846u64 val = *buf++;847848*bitmap++ = val;849if (n > 32)850*bitmap++ = val >> 32;851}852853/*854* Clear tail bits in the last word beyond nbits.855*856* Negative index is OK because here we point to the word next857* to the last word of the bitmap, except for nbits == 0, which858* is tested implicitly.859*/860if (nbits % BITS_PER_LONG)861bitmap[-1] &= BITMAP_LAST_WORD_MASK(nbits);862}863EXPORT_SYMBOL(bitmap_from_arr64);864865/**866* bitmap_to_arr64 - copy the contents of bitmap to a u64 array of bits867* @buf: array of u64 (in host byte order), the dest bitmap868* @bitmap: array of unsigned longs, the source bitmap869* @nbits: number of bits in @bitmap870*/871void bitmap_to_arr64(u64 *buf, const unsigned long *bitmap, unsigned int nbits)872{873const unsigned long *end = bitmap + BITS_TO_LONGS(nbits);874875while (bitmap < end) {876*buf = *bitmap++;877if (bitmap < end)878*buf |= (u64)(*bitmap++) << 32;879buf++;880}881882/* Clear tail bits in the last element of array beyond nbits. */883if (nbits % 64)884buf[-1] &= GENMASK_ULL((nbits - 1) % 64, 0);885}886EXPORT_SYMBOL(bitmap_to_arr64);887#endif888889890