/* SPDX-License-Identifier: GPL-2.0-only */1/*2* Copyright 2024 Google LLC3*4* dbitmap - dynamically sized bitmap library.5*6* Used by the binder driver to optimize the allocation of the smallest7* available descriptor ID. Each bit in the bitmap represents the state8* of an ID.9*10* A dbitmap can grow or shrink as needed. This part has been designed11* considering that users might need to briefly release their locks in12* order to allocate memory for the new bitmap. These operations then,13* are verified to determine if the grow or shrink is sill valid.14*15* This library does not provide protection against concurrent access16* by itself. Binder uses the proc->outer_lock for this purpose.17*/1819#ifndef _LINUX_DBITMAP_H20#define _LINUX_DBITMAP_H21#include <linux/bitmap.h>2223#define NBITS_MIN BITS_PER_TYPE(unsigned long)2425struct dbitmap {26unsigned int nbits;27unsigned long *map;28};2930static inline int dbitmap_enabled(struct dbitmap *dmap)31{32return !!dmap->nbits;33}3435static inline void dbitmap_free(struct dbitmap *dmap)36{37dmap->nbits = 0;38kfree(dmap->map);39}4041/* Returns the nbits that a dbitmap can shrink to, 0 if not possible. */42static inline unsigned int dbitmap_shrink_nbits(struct dbitmap *dmap)43{44unsigned int bit;4546if (dmap->nbits <= NBITS_MIN)47return 0;4849/*50* Determine if the bitmap can shrink based on the position of51* its last set bit. If the bit is within the first quarter of52* the bitmap then shrinking is possible. In this case, the53* bitmap should shrink to half its current size.54*/55bit = find_last_bit(dmap->map, dmap->nbits);56if (bit < (dmap->nbits >> 2))57return dmap->nbits >> 1;5859/* find_last_bit() returns dmap->nbits when no bits are set. */60if (bit == dmap->nbits)61return NBITS_MIN;6263return 0;64}6566/* Replace the internal bitmap with a new one of different size */67static inline void68dbitmap_replace(struct dbitmap *dmap, unsigned long *new, unsigned int nbits)69{70bitmap_copy(new, dmap->map, min(dmap->nbits, nbits));71kfree(dmap->map);72dmap->map = new;73dmap->nbits = nbits;74}7576static inline void77dbitmap_shrink(struct dbitmap *dmap, unsigned long *new, unsigned int nbits)78{79if (!new)80return;8182/*83* Verify that shrinking to @nbits is still possible. The @new84* bitmap might have been allocated without locks, so this call85* could now be outdated. In this case, free @new and move on.86*/87if (!dbitmap_enabled(dmap) || dbitmap_shrink_nbits(dmap) != nbits) {88kfree(new);89return;90}9192dbitmap_replace(dmap, new, nbits);93}9495/* Returns the nbits that a dbitmap can grow to. */96static inline unsigned int dbitmap_grow_nbits(struct dbitmap *dmap)97{98return dmap->nbits << 1;99}100101static inline void102dbitmap_grow(struct dbitmap *dmap, unsigned long *new, unsigned int nbits)103{104/*105* Verify that growing to @nbits is still possible. The @new106* bitmap might have been allocated without locks, so this call107* could now be outdated. In this case, free @new and move on.108*/109if (!dbitmap_enabled(dmap) || nbits <= dmap->nbits) {110kfree(new);111return;112}113114/*115* Check for ENOMEM after confirming the grow operation is still116* required. This ensures we only disable the dbitmap when it's117* necessary. Once the dbitmap is disabled, binder will fallback118* to slow_desc_lookup_olocked().119*/120if (!new) {121dbitmap_free(dmap);122return;123}124125dbitmap_replace(dmap, new, nbits);126}127128/*129* Finds and sets the next zero bit in the bitmap. Upon success @bit130* is populated with the index and 0 is returned. Otherwise, -ENOSPC131* is returned to indicate that a dbitmap_grow() is needed.132*/133static inline int134dbitmap_acquire_next_zero_bit(struct dbitmap *dmap, unsigned long offset,135unsigned long *bit)136{137unsigned long n;138139n = find_next_zero_bit(dmap->map, dmap->nbits, offset);140if (n == dmap->nbits)141return -ENOSPC;142143*bit = n;144set_bit(n, dmap->map);145146return 0;147}148149static inline void150dbitmap_clear_bit(struct dbitmap *dmap, unsigned long bit)151{152clear_bit(bit, dmap->map);153}154155static inline int dbitmap_init(struct dbitmap *dmap)156{157dmap->map = bitmap_zalloc(NBITS_MIN, GFP_KERNEL);158if (!dmap->map) {159dmap->nbits = 0;160return -ENOMEM;161}162163dmap->nbits = NBITS_MIN;164165return 0;166}167#endif168169170