// 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);356357void __bitmap_set(unsigned long *map, unsigned int start, int len)358{359unsigned long *p = map + BIT_WORD(start);360const unsigned int size = start + len;361int bits_to_set = BITS_PER_LONG - (start % BITS_PER_LONG);362unsigned long mask_to_set = BITMAP_FIRST_WORD_MASK(start);363364while (len - bits_to_set >= 0) {365*p |= mask_to_set;366len -= bits_to_set;367bits_to_set = BITS_PER_LONG;368mask_to_set = ~0UL;369p++;370}371if (len) {372mask_to_set &= BITMAP_LAST_WORD_MASK(size);373*p |= mask_to_set;374}375}376EXPORT_SYMBOL(__bitmap_set);377378void __bitmap_clear(unsigned long *map, unsigned int start, int len)379{380unsigned long *p = map + BIT_WORD(start);381const unsigned int size = start + len;382int bits_to_clear = BITS_PER_LONG - (start % BITS_PER_LONG);383unsigned long mask_to_clear = BITMAP_FIRST_WORD_MASK(start);384385while (len - bits_to_clear >= 0) {386*p &= ~mask_to_clear;387len -= bits_to_clear;388bits_to_clear = BITS_PER_LONG;389mask_to_clear = ~0UL;390p++;391}392if (len) {393mask_to_clear &= BITMAP_LAST_WORD_MASK(size);394*p &= ~mask_to_clear;395}396}397EXPORT_SYMBOL(__bitmap_clear);398399/**400* bitmap_find_next_zero_area_off - find a contiguous aligned zero area401* @map: The address to base the search on402* @size: The bitmap size in bits403* @start: The bitnumber to start searching at404* @nr: The number of zeroed bits we're looking for405* @align_mask: Alignment mask for zero area406* @align_offset: Alignment offset for zero area.407*408* The @align_mask should be one less than a power of 2; the effect is that409* the bit offset of all zero areas this function finds plus @align_offset410* is multiple of that power of 2.411*/412unsigned long bitmap_find_next_zero_area_off(unsigned long *map,413unsigned long size,414unsigned long start,415unsigned int nr,416unsigned long align_mask,417unsigned long align_offset)418{419unsigned long index, end, i;420again:421index = find_next_zero_bit(map, size, start);422423/* Align allocation */424index = __ALIGN_MASK(index + align_offset, align_mask) - align_offset;425426end = index + nr;427if (end > size)428return end;429i = find_next_bit(map, end, index);430if (i < end) {431start = i + 1;432goto again;433}434return index;435}436EXPORT_SYMBOL(bitmap_find_next_zero_area_off);437438/**439* bitmap_pos_to_ord - find ordinal of set bit at given position in bitmap440* @buf: pointer to a bitmap441* @pos: a bit position in @buf (0 <= @pos < @nbits)442* @nbits: number of valid bit positions in @buf443*444* Map the bit at position @pos in @buf (of length @nbits) to the445* ordinal of which set bit it is. If it is not set or if @pos446* is not a valid bit position, map to -1.447*448* If for example, just bits 4 through 7 are set in @buf, then @pos449* values 4 through 7 will get mapped to 0 through 3, respectively,450* and other @pos values will get mapped to -1. When @pos value 7451* gets mapped to (returns) @ord value 3 in this example, that means452* that bit 7 is the 3rd (starting with 0th) set bit in @buf.453*454* The bit positions 0 through @bits are valid positions in @buf.455*/456static int bitmap_pos_to_ord(const unsigned long *buf, unsigned int pos, unsigned int nbits)457{458if (pos >= nbits || !test_bit(pos, buf))459return -1;460461return bitmap_weight(buf, pos);462}463464/**465* bitmap_remap - Apply map defined by a pair of bitmaps to another bitmap466* @dst: remapped result467* @src: subset to be remapped468* @old: defines domain of map469* @new: defines range of map470* @nbits: number of bits in each of these bitmaps471*472* Let @old and @new define a mapping of bit positions, such that473* whatever position is held by the n-th set bit in @old is mapped474* to the n-th set bit in @new. In the more general case, allowing475* for the possibility that the weight 'w' of @new is less than the476* weight of @old, map the position of the n-th set bit in @old to477* the position of the m-th set bit in @new, where m == n % w.478*479* If either of the @old and @new bitmaps are empty, or if @src and480* @dst point to the same location, then this routine copies @src481* to @dst.482*483* The positions of unset bits in @old are mapped to themselves484* (the identity map).485*486* Apply the above specified mapping to @src, placing the result in487* @dst, clearing any bits previously set in @dst.488*489* For example, lets say that @old has bits 4 through 7 set, and490* @new has bits 12 through 15 set. This defines the mapping of bit491* position 4 to 12, 5 to 13, 6 to 14 and 7 to 15, and of all other492* bit positions unchanged. So if say @src comes into this routine493* with bits 1, 5 and 7 set, then @dst should leave with bits 1,494* 13 and 15 set.495*/496void bitmap_remap(unsigned long *dst, const unsigned long *src,497const unsigned long *old, const unsigned long *new,498unsigned int nbits)499{500unsigned int oldbit, w;501502if (dst == src) /* following doesn't handle inplace remaps */503return;504bitmap_zero(dst, nbits);505506w = bitmap_weight(new, nbits);507for_each_set_bit(oldbit, src, nbits) {508int n = bitmap_pos_to_ord(old, oldbit, nbits);509510if (n < 0 || w == 0)511set_bit(oldbit, dst); /* identity map */512else513set_bit(find_nth_bit(new, nbits, n % w), dst);514}515}516EXPORT_SYMBOL(bitmap_remap);517518/**519* bitmap_bitremap - Apply map defined by a pair of bitmaps to a single bit520* @oldbit: bit position to be mapped521* @old: defines domain of map522* @new: defines range of map523* @bits: number of bits in each of these bitmaps524*525* Let @old and @new define a mapping of bit positions, such that526* whatever position is held by the n-th set bit in @old is mapped527* to the n-th set bit in @new. In the more general case, allowing528* for the possibility that the weight 'w' of @new is less than the529* weight of @old, map the position of the n-th set bit in @old to530* the position of the m-th set bit in @new, where m == n % w.531*532* The positions of unset bits in @old are mapped to themselves533* (the identity map).534*535* Apply the above specified mapping to bit position @oldbit, returning536* the new bit position.537*538* For example, lets say that @old has bits 4 through 7 set, and539* @new has bits 12 through 15 set. This defines the mapping of bit540* position 4 to 12, 5 to 13, 6 to 14 and 7 to 15, and of all other541* bit positions unchanged. So if say @oldbit is 5, then this routine542* returns 13.543*/544int bitmap_bitremap(int oldbit, const unsigned long *old,545const unsigned long *new, int bits)546{547int w = bitmap_weight(new, bits);548int n = bitmap_pos_to_ord(old, oldbit, bits);549if (n < 0 || w == 0)550return oldbit;551else552return find_nth_bit(new, bits, n % w);553}554EXPORT_SYMBOL(bitmap_bitremap);555556#ifdef CONFIG_NUMA557/**558* bitmap_onto - translate one bitmap relative to another559* @dst: resulting translated bitmap560* @orig: original untranslated bitmap561* @relmap: bitmap relative to which translated562* @bits: number of bits in each of these bitmaps563*564* Set the n-th bit of @dst iff there exists some m such that the565* n-th bit of @relmap is set, the m-th bit of @orig is set, and566* the n-th bit of @relmap is also the m-th _set_ bit of @relmap.567* (If you understood the previous sentence the first time your568* read it, you're overqualified for your current job.)569*570* In other words, @orig is mapped onto (surjectively) @dst,571* using the map { <n, m> | the n-th bit of @relmap is the572* m-th set bit of @relmap }.573*574* Any set bits in @orig above bit number W, where W is the575* weight of (number of set bits in) @relmap are mapped nowhere.576* In particular, if for all bits m set in @orig, m >= W, then577* @dst will end up empty. In situations where the possibility578* of such an empty result is not desired, one way to avoid it is579* to use the bitmap_fold() operator, below, to first fold the580* @orig bitmap over itself so that all its set bits x are in the581* range 0 <= x < W. The bitmap_fold() operator does this by582* setting the bit (m % W) in @dst, for each bit (m) set in @orig.583*584* Example [1] for bitmap_onto():585* Let's say @relmap has bits 30-39 set, and @orig has bits586* 1, 3, 5, 7, 9 and 11 set. Then on return from this routine,587* @dst will have bits 31, 33, 35, 37 and 39 set.588*589* When bit 0 is set in @orig, it means turn on the bit in590* @dst corresponding to whatever is the first bit (if any)591* that is turned on in @relmap. Since bit 0 was off in the592* above example, we leave off that bit (bit 30) in @dst.593*594* When bit 1 is set in @orig (as in the above example), it595* means turn on the bit in @dst corresponding to whatever596* is the second bit that is turned on in @relmap. The second597* bit in @relmap that was turned on in the above example was598* bit 31, so we turned on bit 31 in @dst.599*600* Similarly, we turned on bits 33, 35, 37 and 39 in @dst,601* because they were the 4th, 6th, 8th and 10th set bits602* set in @relmap, and the 4th, 6th, 8th and 10th bits of603* @orig (i.e. bits 3, 5, 7 and 9) were also set.604*605* When bit 11 is set in @orig, it means turn on the bit in606* @dst corresponding to whatever is the twelfth bit that is607* turned on in @relmap. In the above example, there were608* only ten bits turned on in @relmap (30..39), so that bit609* 11 was set in @orig had no affect on @dst.610*611* Example [2] for bitmap_fold() + bitmap_onto():612* Let's say @relmap has these ten bits set::613*614* 40 41 42 43 45 48 53 61 74 95615*616* (for the curious, that's 40 plus the first ten terms of the617* Fibonacci sequence.)618*619* Further lets say we use the following code, invoking620* bitmap_fold() then bitmap_onto, as suggested above to621* avoid the possibility of an empty @dst result::622*623* unsigned long *tmp; // a temporary bitmap's bits624*625* bitmap_fold(tmp, orig, bitmap_weight(relmap, bits), bits);626* bitmap_onto(dst, tmp, relmap, bits);627*628* Then this table shows what various values of @dst would be, for629* various @orig's. I list the zero-based positions of each set bit.630* The tmp column shows the intermediate result, as computed by631* using bitmap_fold() to fold the @orig bitmap modulo ten632* (the weight of @relmap):633*634* =============== ============== =================635* @orig tmp @dst636* 0 0 40637* 1 1 41638* 9 9 95639* 10 0 40 [#f1]_640* 1 3 5 7 1 3 5 7 41 43 48 61641* 0 1 2 3 4 0 1 2 3 4 40 41 42 43 45642* 0 9 18 27 0 9 8 7 40 61 74 95643* 0 10 20 30 0 40644* 0 11 22 33 0 1 2 3 40 41 42 43645* 0 12 24 36 0 2 4 6 40 42 45 53646* 78 102 211 1 2 8 41 42 74 [#f1]_647* =============== ============== =================648*649* .. [#f1]650*651* For these marked lines, if we hadn't first done bitmap_fold()652* into tmp, then the @dst result would have been empty.653*654* If either of @orig or @relmap is empty (no set bits), then @dst655* will be returned empty.656*657* If (as explained above) the only set bits in @orig are in positions658* m where m >= W, (where W is the weight of @relmap) then @dst will659* once again be returned empty.660*661* All bits in @dst not set by the above rule are cleared.662*/663void bitmap_onto(unsigned long *dst, const unsigned long *orig,664const unsigned long *relmap, unsigned int bits)665{666unsigned int n, m; /* same meaning as in above comment */667668if (dst == orig) /* following doesn't handle inplace mappings */669return;670bitmap_zero(dst, bits);671672/*673* The following code is a more efficient, but less674* obvious, equivalent to the loop:675* for (m = 0; m < bitmap_weight(relmap, bits); m++) {676* n = find_nth_bit(orig, bits, m);677* if (test_bit(m, orig))678* set_bit(n, dst);679* }680*/681682m = 0;683for_each_set_bit(n, relmap, bits) {684/* m == bitmap_pos_to_ord(relmap, n, bits) */685if (test_bit(m, orig))686set_bit(n, dst);687m++;688}689}690691/**692* bitmap_fold - fold larger bitmap into smaller, modulo specified size693* @dst: resulting smaller bitmap694* @orig: original larger bitmap695* @sz: specified size696* @nbits: number of bits in each of these bitmaps697*698* For each bit oldbit in @orig, set bit oldbit mod @sz in @dst.699* Clear all other bits in @dst. See further the comment and700* Example [2] for bitmap_onto() for why and how to use this.701*/702void bitmap_fold(unsigned long *dst, const unsigned long *orig,703unsigned int sz, unsigned int nbits)704{705unsigned int oldbit;706707if (dst == orig) /* following doesn't handle inplace mappings */708return;709bitmap_zero(dst, nbits);710711for_each_set_bit(oldbit, orig, nbits)712set_bit(oldbit % sz, dst);713}714#endif /* CONFIG_NUMA */715716unsigned long *bitmap_alloc(unsigned int nbits, gfp_t flags)717{718return kmalloc_array(BITS_TO_LONGS(nbits), sizeof(unsigned long),719flags);720}721EXPORT_SYMBOL(bitmap_alloc);722723unsigned long *bitmap_zalloc(unsigned int nbits, gfp_t flags)724{725return bitmap_alloc(nbits, flags | __GFP_ZERO);726}727EXPORT_SYMBOL(bitmap_zalloc);728729unsigned long *bitmap_alloc_node(unsigned int nbits, gfp_t flags, int node)730{731return kmalloc_array_node(BITS_TO_LONGS(nbits), sizeof(unsigned long),732flags, node);733}734EXPORT_SYMBOL(bitmap_alloc_node);735736unsigned long *bitmap_zalloc_node(unsigned int nbits, gfp_t flags, int node)737{738return bitmap_alloc_node(nbits, flags | __GFP_ZERO, node);739}740EXPORT_SYMBOL(bitmap_zalloc_node);741742void bitmap_free(const unsigned long *bitmap)743{744kfree(bitmap);745}746EXPORT_SYMBOL(bitmap_free);747748static void devm_bitmap_free(void *data)749{750unsigned long *bitmap = data;751752bitmap_free(bitmap);753}754755unsigned long *devm_bitmap_alloc(struct device *dev,756unsigned int nbits, gfp_t flags)757{758unsigned long *bitmap;759int ret;760761bitmap = bitmap_alloc(nbits, flags);762if (!bitmap)763return NULL;764765ret = devm_add_action_or_reset(dev, devm_bitmap_free, bitmap);766if (ret)767return NULL;768769return bitmap;770}771EXPORT_SYMBOL_GPL(devm_bitmap_alloc);772773unsigned long *devm_bitmap_zalloc(struct device *dev,774unsigned int nbits, gfp_t flags)775{776return devm_bitmap_alloc(dev, nbits, flags | __GFP_ZERO);777}778EXPORT_SYMBOL_GPL(devm_bitmap_zalloc);779780#if BITS_PER_LONG == 64781/**782* bitmap_from_arr32 - copy the contents of u32 array of bits to bitmap783* @bitmap: array of unsigned longs, the destination bitmap784* @buf: array of u32 (in host byte order), the source bitmap785* @nbits: number of bits in @bitmap786*/787void bitmap_from_arr32(unsigned long *bitmap, const u32 *buf, unsigned int nbits)788{789unsigned int i, halfwords;790791halfwords = DIV_ROUND_UP(nbits, 32);792for (i = 0; i < halfwords; i++) {793bitmap[i/2] = (unsigned long) buf[i];794if (++i < halfwords)795bitmap[i/2] |= ((unsigned long) buf[i]) << 32;796}797798/* Clear tail bits in last word beyond nbits. */799if (nbits % BITS_PER_LONG)800bitmap[(halfwords - 1) / 2] &= BITMAP_LAST_WORD_MASK(nbits);801}802EXPORT_SYMBOL(bitmap_from_arr32);803804/**805* bitmap_to_arr32 - copy the contents of bitmap to a u32 array of bits806* @buf: array of u32 (in host byte order), the dest bitmap807* @bitmap: array of unsigned longs, the source bitmap808* @nbits: number of bits in @bitmap809*/810void bitmap_to_arr32(u32 *buf, const unsigned long *bitmap, unsigned int nbits)811{812unsigned int i, halfwords;813814halfwords = DIV_ROUND_UP(nbits, 32);815for (i = 0; i < halfwords; i++) {816buf[i] = (u32) (bitmap[i/2] & UINT_MAX);817if (++i < halfwords)818buf[i] = (u32) (bitmap[i/2] >> 32);819}820821/* Clear tail bits in last element of array beyond nbits. */822if (nbits % BITS_PER_LONG)823buf[halfwords - 1] &= (u32) (UINT_MAX >> ((-nbits) & 31));824}825EXPORT_SYMBOL(bitmap_to_arr32);826#endif827828#if BITS_PER_LONG == 32829/**830* bitmap_from_arr64 - copy the contents of u64 array of bits to bitmap831* @bitmap: array of unsigned longs, the destination bitmap832* @buf: array of u64 (in host byte order), the source bitmap833* @nbits: number of bits in @bitmap834*/835void bitmap_from_arr64(unsigned long *bitmap, const u64 *buf, unsigned int nbits)836{837int n;838839for (n = nbits; n > 0; n -= 64) {840u64 val = *buf++;841842*bitmap++ = val;843if (n > 32)844*bitmap++ = val >> 32;845}846847/*848* Clear tail bits in the last word beyond nbits.849*850* Negative index is OK because here we point to the word next851* to the last word of the bitmap, except for nbits == 0, which852* is tested implicitly.853*/854if (nbits % BITS_PER_LONG)855bitmap[-1] &= BITMAP_LAST_WORD_MASK(nbits);856}857EXPORT_SYMBOL(bitmap_from_arr64);858859/**860* bitmap_to_arr64 - copy the contents of bitmap to a u64 array of bits861* @buf: array of u64 (in host byte order), the dest bitmap862* @bitmap: array of unsigned longs, the source bitmap863* @nbits: number of bits in @bitmap864*/865void bitmap_to_arr64(u64 *buf, const unsigned long *bitmap, unsigned int nbits)866{867const unsigned long *end = bitmap + BITS_TO_LONGS(nbits);868869while (bitmap < end) {870*buf = *bitmap++;871if (bitmap < end)872*buf |= (u64)(*bitmap++) << 32;873buf++;874}875876/* Clear tail bits in the last element of array beyond nbits. */877if (nbits % 64)878buf[-1] &= GENMASK_ULL((nbits - 1) % 64, 0);879}880EXPORT_SYMBOL(bitmap_to_arr64);881#endif882883884