Changes in 4.9.212 xfs: Sanity check flags of Q_XQUOTARM call powerpc/archrandom: fix arch_get_random_seed_int() mt7601u: fix bbp version check in mt7601u_wait_bbp_ready drm/sti: do not remove the drm_bridge that was never added drm/virtio: fix bounds check in virtio_gpu_cmd_get_capset() ALSA: hda: fix unused variable warning IB/rxe: replace kvfree with vfree ALSA: usb-audio: update quirk for B&W PX to remove microphone staging: comedi: ni_mio_common: protect register write overflow pwm: lpss: Release runtime-pm reference from the driver's remove callback mlxsw: reg: QEEC: Add minimum shaper fields pcrypt: use format specifier in kobject_add exportfs: fix 'passing zero to ERR_PTR()' warning drm/dp_mst: Skip validating ports during destruction, just ref net: phy: Fix not to call phy_resume() if PHY is not attached pinctrl: sh-pfc: r8a7740: Add missing REF125CK pin to gether_gmii group pinctrl: sh-pfc: r8a7740: Add missing LCD0 marks to lcd0_data24_1 group pinctrl: sh-pfc: r8a7791: Remove bogus ctrl marks from qspi_data4_b group pinctrl: sh-pfc: r8a7791: Remove bogus marks from vin1_b_data18 group pinctrl: sh-pfc: sh73a0: Add missing TO pin to tpu4_to3 group pinctrl: sh-pfc: r8a7794: Remove bogus IPSR9 field pinctrl: sh-pfc: sh7734: Add missing IPSR11 field pinctrl: sh-pfc: sh7269: Add missing PCIOR0 field pinctrl: sh-pfc: sh7734: Remove bogus IPSR10 value Input: nomadik-ske-keypad - fix a loop timeout test clk: highbank: fix refcount leak in hb_clk_init() clk: qoriq: fix refcount leak in clockgen_init() clk: socfpga: fix refcount leak clk: samsung: exynos4: fix refcount leak in exynos4_get_xom() clk: imx6q: fix refcount leak in imx6q_clocks_init() clk: imx6sx: fix refcount leak in imx6sx_clocks_init() clk: imx7d: fix refcount leak in imx7d_clocks_init() clk: vf610: fix refcount leak in vf610_clocks_init() clk: armada-370: fix refcount leak in a370_clk_init() clk: kirkwood: fix refcount leak in kirkwood_clk_init() clk: armada-xp: fix refcount leak in axp_clk_init() clk: dove: fix refcount leak in dove_clk_init() IB/usnic: Fix out of bounds index check in query pkey RDMA/ocrdma: Fix out of bounds index check in query pkey RDMA/qedr: Fix out of bounds index check in query pkey arm64: dts: apq8016-sbc: Increase load on l11 for SDCARD drm/etnaviv: NULL vs IS_ERR() buf in etnaviv_core_dump() media: s5p-jpeg: Correct step and max values for V4L2_CID_JPEG_RESTART_INTERVAL crypto: tgr192 - fix unaligned memory access ASoC: imx-sgtl5000: put of nodes if finding codec fails IB/iser: Pass the correct number of entries for dma mapped SGL rtc: cmos: ignore bogus century byte clk: sunxi-ng: sun8i-a23: Enable PLL-MIPI LDOs when ungating it iwlwifi: mvm: fix A-MPDU reference assignment tty: ipwireless: Fix potential NULL pointer dereference crypto: crypto4xx - Fix wrong ppc4xx_trng_probe()/ppc4xx_trng_remove() arguments ARM: dts: lpc32xx: add required clocks property to keypad device node ARM: dts: lpc32xx: reparent keypad controller to SIC1 ARM: dts: lpc32xx: fix ARM PrimeCell LCD controller variant ARM: dts: lpc32xx: fix ARM PrimeCell LCD controller clocks property ARM: dts: lpc32xx: phy3250: fix SD card regulator voltage iwlwifi: mvm: fix RSS config command staging: most: cdev: add missing check for cdev_add failure rtc: ds1672: fix unintended sign extension thermal: mediatek: fix register index error net: phy: fixed_phy: Fix fixed_phy not checking GPIO rtc: 88pm860x: fix unintended sign extension rtc: 88pm80x: fix unintended sign extension rtc: pm8xxx: fix unintended sign extension fbdev: chipsfb: remove set but not used variable 'size' iw_cxgb4: use tos when importing the endpoint iw_cxgb4: use tos when finding ipv6 routes pinctrl: sh-pfc: emev2: Add missing pinmux functions pinctrl: sh-pfc: r8a7791: Fix scifb2_data_c pin group pinctrl: sh-pfc: r8a7792: Fix vin1_data18_b pin group pinctrl: sh-pfc: sh73a0: Fix fsic_spdif pin groups usb: phy: twl6030-usb: fix possible use-after-free on remove block: don't use bio->bi_vcnt to figure out segment number keys: Timestamp new keys vfio_pci: Enable memory accesses before calling pci_map_rom dmaengine: mv_xor: Use correct device for DMA API cdc-wdm: pass return value of recover_from_urb_loss regulator: pv88060: Fix array out-of-bounds access regulator: pv88080: Fix array out-of-bounds access regulator: pv88090: Fix array out-of-bounds access net: dsa: qca8k: Enable delay for RGMII_ID mode drm/nouveau/bios/ramcfg: fix missing parentheses when calculating RON drm/nouveau/pmu: don't print reply values if exec is false ASoC: qcom: Fix of-node refcount unbalance in apq8016_sbc_parse_of() fs/nfs: Fix nfs_parse_devname to not modify it's argument NFS: Fix a soft lockup in the delegation recovery code clocksource/drivers/sun5i: Fail gracefully when clock rate is unavailable clocksource/drivers/exynos_mct: Fix error path in timer resources initialization mmc: sdhci-brcmstb: handle mmc_of_parse() errors during probe ARM: 8847/1: pm: fix HYP/SVC mode mismatch when MCPM is used ARM: 8848/1: virt: Align GIC version check with arm64 counterpart regulator: wm831x-dcdc: Fix list of wm831x_dcdc_ilim from mA to uA nios2: ksyms: Add missing symbol exports scsi: megaraid_sas: reduce module load time drivers/rapidio/rio_cm.c: fix potential oops in riocm_ch_listen() xen, cpu_hotplug: Prevent an out of bounds access net: sh_eth: fix a missing check of of_get_phy_mode media: ivtv: update *pos correctly in ivtv_read_pos() media: cx18: update *pos correctly in cx18_read_pos() media: wl128x: Fix an error code in fm_download_firmware() media: cx23885: check allocation return regulator: tps65086: Fix tps65086_ldoa1_ranges for selector 0xB jfs: fix bogus variable self-initialization tipc: tipc clang warning m68k: mac: Fix VIA timer counter accesses ARM: OMAP2+: Fix potentially uninitialized return value for _setup_reset() media: davinci-isif: avoid uninitialized variable use media: tw5864: Fix possible NULL pointer dereference in tw5864_handle_frame spi: tegra114: clear packed bit for unpacked mode spi: tegra114: fix for unpacked mode transfers soc/fsl/qe: Fix an error code in qe_pin_request() spi: bcm2835aux: fix driver to not allow 65535 (=-1) cs-gpios ehea: Fix a copy-paste err in ehea_init_port_res scsi: qla2xxx: Unregister chrdev if module initialization fails ARM: pxa: ssp: Fix "WARNING: invalid free of devm_ allocated data" hwmon: (w83627hf) Use request_muxed_region for Super-IO accesses tipc: set sysctl_tipc_rmem and named_timeout right range powerpc: vdso: Make vdso32 installation conditional in vdso_install ARM: dts: ls1021: Fix SGMII PCS link remaining down after PHY disconnect media: ov2659: fix unbalanced mutex_lock/unlock 6lowpan: Off by one handling ->nexthdr dmaengine: axi-dmac: Don't check the number of frames for alignment ALSA: usb-audio: Handle the error from snd_usb_mixer_apply_create_quirk() packet: in recvmsg msg_name return at least sizeof sockaddr_ll ASoC: fix valid stream condition usb: gadget: fsl: fix link error against usb-gadget module IB/mlx5: Add missing XRC options to QP optional params mask iommu/vt-d: Make kernel parameter igfx_off work with vIOMMU net: ena: fix swapped parameters when calling ena_com_indirect_table_fill_entry net: ena: fix: Free napi resources when ena_up() fails net: ena: fix incorrect test of supported hash function net: ena: fix ena_com_fill_hash_function() implementation dmaengine: tegra210-adma: restore channel status l2tp: Fix possible NULL pointer dereference media: omap_vout: potential buffer overflow in vidioc_dqbuf() media: davinci/vpbe: array underflow in vpbe_enum_outputs() platform/x86: alienware-wmi: printing the wrong error code netfilter: ebtables: CONFIG_COMPAT: reject trailing data after last rule pwm: meson: Don't disable PWM when setting duty repeatedly ARM: riscpc: fix lack of keyboard interrupts after irq conversion kdb: do a sanity check on the cpu in kdb_per_cpu() backlight: lm3630a: Return 0 on success in update_status functions thermal: cpu_cooling: Actually trace CPU load in thermal_power_cpu_get_power dmaengine: tegra210-adma: Fix crash during probe spi: spi-fsl-spi: call spi_finalize_current_message() at the end crypto: ccp - fix AES CFB error exposed by new test vectors serial: stm32: fix transmit_chars when tx is stopped misc: sgi-xp: Properly initialize buf in xpc_get_rsvd_page_pa iommu: Use right function to get group for device signal/cifs: Fix cifs_put_tcp_session to call send_sig instead of force_sig inet: frags: call inet_frags_fini() after unregister_pernet_subsys() media: vivid: fix incorrect assignment operation when setting video mode powerpc/cacheinfo: add cacheinfo_teardown, cacheinfo_rebuild drm/msm/mdp5: Fix mdp5_cfg_init error return net: netem: fix backlog accounting for corrupted GSO frames net/af_iucv: always register net_device notifier ASoC: ti: davinci-mcasp: Fix slot mask settings when using multiple AXRs rtc: pcf8563: Clear event flags and disable interrupts before requesting irq drm/msm/a3xx: remove TPL1 regs from snapshot perf/ioctl: Add check for the sample_period value dmaengine: hsu: Revert "set HSU_CH_MTSR to memory width" clk: qcom: Fix -Wunused-const-variable iommu/amd: Make iommu_disable safer mfd: intel-lpss: Release IDA resources rxrpc: Fix uninitialized error code in rxrpc_send_data_packet() devres: allow const resource arguments RDMA/hns: Fixs hw access invalid dma memory error net: pasemi: fix an use-after-free in pasemi_mac_phy_init() scsi: libfc: fix null pointer dereference on a null lport libertas_tf: Use correct channel range in lbtf_geo_init qed: reduce maximum stack frame size usb: host: xhci-hub: fix extra endianness conversion mic: avoid statically declaring a 'struct device'. x86/kgbd: Use NMI_VECTOR not APIC_DM_NMI ALSA: aoa: onyx: always initialize register read value net/mlx5: Fix mlx5_ifc_query_lag_out_bits cifs: fix rmmod regression in cifs.ko caused by force_sig changes crypto: caam - free resources in case caam_rng registration failed ext4: set error return correctly when ext4_htree_store_dirent fails ASoC: es8328: Fix copy-paste error in es8328_right_line_controls ASoC: cs4349: Use PM ops 'cs4349_runtime_pm' ASoC: wm8737: Fix copy-paste error in wm8737_snd_controls signal: Allow cifs and drbd to receive their terminating signals ASoC: sun4i-i2s: RX and TX counter registers are swapped dmaengine: dw: platform: Switch to acpi_dma_controller_register() mac80211: minstrel_ht: fix per-group max throughput rate initialization mips: avoid explicit UB in assignment of mips_io_port_base ahci: Do not export local variable ahci_em_messages Partially revert "kfifo: fix kfifo_alloc() and kfifo_init()" hwmon: (lm75) Fix write operations for negative temperatures power: supply: Init device wakeup after device_add() x86, perf: Fix the dependency of the x86 insn decoder selftest staging: greybus: light: fix a couple double frees bcma: fix incorrect update of BCMA_CORE_PCI_MDIO_DATA iio: dac: ad5380: fix incorrect assignment to val ath9k: dynack: fix possible deadlock in ath_dynack_node_{de}init net: sonic: return NETDEV_TX_OK if failed to map buffer Btrfs: fix hang when loading existing inode cache off disk hwmon: (shtc1) fix shtc1 and shtw1 id mask net: sonic: replace dev_kfree_skb in sonic_send_packet net/rds: Fix 'ib_evt_handler_call' element in 'rds_ib_stat_names' iommu/amd: Wait for completion of IOTLB flush in attach_device net: hisilicon: Fix signedness bug in hix5hd2_dev_probe() net: broadcom/bcmsysport: Fix signedness in bcm_sysport_probe() net: stmmac: dwmac-meson8b: Fix signedness bug in probe of: mdio: Fix a signedness bug in of_phy_get_and_connect() net: ethernet: stmmac: Fix signedness bug in ipq806x_gmac_of_parse() nvme: retain split access workaround for capability reads net: stmmac: gmac4+: Not all Unicast addresses may be available mac80211: accept deauth frames in IBSS mode llc: fix another potential sk_buff leak in llc_ui_sendmsg() llc: fix sk_buff refcounting in llc_conn_state_process() net: stmmac: fix length of PTP clock's name string act_mirred: Fix mirred_init_module error handling drm/msm/dsi: Implement reset correctly dmaengine: imx-sdma: fix size check for sdma script_number net: netem: fix error path for corrupted GSO frames net: netem: correct the parent's backlog when corrupted packet was dropped net: qca_spi: Move reset_count to struct qcaspi afs: Fix large file support media: ov6650: Fix incorrect use of JPEG colorspace media: ov6650: Fix some format attributes not under control media: ov6650: Fix .get_fmt() V4L2_SUBDEV_FORMAT_TRY support MIPS: Loongson: Fix return value of loongson_hwmon_init net: neigh: use long type to store jiffies delta packet: fix data-race in fanout_flow_is_huge() dmaengine: ti: edma: fix missed failure handling drm/radeon: fix bad DMA from INTERRUPT_CNTL2 arm64: dts: juno: Fix UART frequency IB/iser: Fix dma_nents type definition m68k: Call timer_interrupt() with interrupts disabled net: ethtool: Add back transceiver type net: phy: Keep reporting transceiver type can, slip: Protect tty->disc_data in write_wakeup and close with RCU firestream: fix memory leaks net: cxgb3_main: Add CAP_NET_ADMIN check to CHELSIO_GET_MEM net, ip6_tunnel: fix namespaces move net, ip_tunnel: fix namespaces move net_sched: fix datalen for ematch tcp_bbr: improve arithmetic division in bbr_update_bw() net: usb: lan78xx: Add .ndo_features_check gtp: make sure only SOCK_DGRAM UDP sockets are accepted hwmon: (adt7475) Make volt2reg return same reg as reg2volt input hwmon: (core) Simplify sysfs attribute name allocation hwmon: Deal with errors from the thermal subsystem hwmon: (core) Fix double-free in __hwmon_device_register() hwmon: (core) Do not use device managed functions for memory allocations Input: keyspan-remote - fix control-message timeouts ARM: 8950/1: ftrace/recordmcount: filter relocation types mmc: tegra: fix SDR50 tuning override mmc: sdhci: fix minimum clock rate for v3 controller Input: sur40 - fix interface sanity checks Input: gtco - fix endpoint sanity check Input: aiptek - fix endpoint sanity check Input: pegasus_notetaker - fix endpoint sanity check Input: sun4i-ts - add a check for devm_thermal_zone_of_sensor_register hwmon: (nct7802) Fix voltage limits to wrong registers scsi: RDMA/isert: Fix a recently introduced regression related to logout tracing: xen: Ordered comparison of function pointers do_last(): fetch directory ->i_mode and ->i_uid before it's too late Documentation: Document arm64 kpti control arm64: kpti: Whitelist Cortex-A CPUs that don't implement the CSV3 field coresight: etb10: Do not call smp_processor_id from preemptible coresight: tmc-etf: Do not call smp_processor_id from preemptible libertas: Fix two buffer overflows at parsing bss descriptor bcache: silence static checker warning scsi: iscsi: Avoid potential deadlock in iscsi_if_rx func md: Avoid namespace collision with bitmap API bitmap: Add bitmap_alloc(), bitmap_zalloc() and bitmap_free() netfilter: ipset: use bitmap infrastructure completely net/x25: fix nonblocking connect Linux 4.9.212 Signed-off-by: Greg Kroah-Hartman <gregkh@google.com> Change-Id: I2e83a05c5f119a7467a4d6984045d45d0c06b764
1235 lines
37 KiB
C
1235 lines
37 KiB
C
/*
|
|
* lib/bitmap.c
|
|
* Helper functions for bitmap.h.
|
|
*
|
|
* This source code is licensed under the GNU General Public License,
|
|
* Version 2. See the file COPYING for more details.
|
|
*/
|
|
#include <linux/export.h>
|
|
#include <linux/thread_info.h>
|
|
#include <linux/ctype.h>
|
|
#include <linux/errno.h>
|
|
#include <linux/bitmap.h>
|
|
#include <linux/bitops.h>
|
|
#include <linux/bug.h>
|
|
#include <linux/kernel.h>
|
|
#include <linux/slab.h>
|
|
#include <linux/string.h>
|
|
#include <linux/uaccess.h>
|
|
|
|
#include <asm/page.h>
|
|
|
|
/*
|
|
* bitmaps provide an array of bits, implemented using an an
|
|
* array of unsigned longs. The number of valid bits in a
|
|
* given bitmap does _not_ need to be an exact multiple of
|
|
* BITS_PER_LONG.
|
|
*
|
|
* The possible unused bits in the last, partially used word
|
|
* of a bitmap are 'don't care'. The implementation makes
|
|
* no particular effort to keep them zero. It ensures that
|
|
* their value will not affect the results of any operation.
|
|
* The bitmap operations that return Boolean (bitmap_empty,
|
|
* for example) or scalar (bitmap_weight, for example) results
|
|
* carefully filter out these unused bits from impacting their
|
|
* results.
|
|
*
|
|
* These operations actually hold to a slightly stronger rule:
|
|
* if you don't input any bitmaps to these ops that have some
|
|
* unused bits set, then they won't output any set unused bits
|
|
* in output bitmaps.
|
|
*
|
|
* The byte ordering of bitmaps is more natural on little
|
|
* endian architectures. See the big-endian headers
|
|
* include/asm-ppc64/bitops.h and include/asm-s390/bitops.h
|
|
* for the best explanations of this ordering.
|
|
*/
|
|
|
|
int __bitmap_equal(const unsigned long *bitmap1,
|
|
const unsigned long *bitmap2, unsigned int bits)
|
|
{
|
|
unsigned int k, lim = bits/BITS_PER_LONG;
|
|
for (k = 0; k < lim; ++k)
|
|
if (bitmap1[k] != bitmap2[k])
|
|
return 0;
|
|
|
|
if (bits % BITS_PER_LONG)
|
|
if ((bitmap1[k] ^ bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))
|
|
return 0;
|
|
|
|
return 1;
|
|
}
|
|
EXPORT_SYMBOL(__bitmap_equal);
|
|
|
|
void __bitmap_complement(unsigned long *dst, const unsigned long *src, unsigned int bits)
|
|
{
|
|
unsigned int k, lim = bits/BITS_PER_LONG;
|
|
for (k = 0; k < lim; ++k)
|
|
dst[k] = ~src[k];
|
|
|
|
if (bits % BITS_PER_LONG)
|
|
dst[k] = ~src[k];
|
|
}
|
|
EXPORT_SYMBOL(__bitmap_complement);
|
|
|
|
/**
|
|
* __bitmap_shift_right - logical right shift of the bits in a bitmap
|
|
* @dst : destination bitmap
|
|
* @src : source bitmap
|
|
* @shift : shift by this many bits
|
|
* @nbits : bitmap size, in bits
|
|
*
|
|
* Shifting right (dividing) means moving bits in the MS -> LS bit
|
|
* direction. Zeros are fed into the vacated MS positions and the
|
|
* LS bits shifted off the bottom are lost.
|
|
*/
|
|
void __bitmap_shift_right(unsigned long *dst, const unsigned long *src,
|
|
unsigned shift, unsigned nbits)
|
|
{
|
|
unsigned k, lim = BITS_TO_LONGS(nbits);
|
|
unsigned off = shift/BITS_PER_LONG, rem = shift % BITS_PER_LONG;
|
|
unsigned long mask = BITMAP_LAST_WORD_MASK(nbits);
|
|
for (k = 0; off + k < lim; ++k) {
|
|
unsigned long upper, lower;
|
|
|
|
/*
|
|
* If shift is not word aligned, take lower rem bits of
|
|
* word above and make them the top rem bits of result.
|
|
*/
|
|
if (!rem || off + k + 1 >= lim)
|
|
upper = 0;
|
|
else {
|
|
upper = src[off + k + 1];
|
|
if (off + k + 1 == lim - 1)
|
|
upper &= mask;
|
|
upper <<= (BITS_PER_LONG - rem);
|
|
}
|
|
lower = src[off + k];
|
|
if (off + k == lim - 1)
|
|
lower &= mask;
|
|
lower >>= rem;
|
|
dst[k] = lower | upper;
|
|
}
|
|
if (off)
|
|
memset(&dst[lim - off], 0, off*sizeof(unsigned long));
|
|
}
|
|
EXPORT_SYMBOL(__bitmap_shift_right);
|
|
|
|
|
|
/**
|
|
* __bitmap_shift_left - logical left shift of the bits in a bitmap
|
|
* @dst : destination bitmap
|
|
* @src : source bitmap
|
|
* @shift : shift by this many bits
|
|
* @nbits : bitmap size, in bits
|
|
*
|
|
* Shifting left (multiplying) means moving bits in the LS -> MS
|
|
* direction. Zeros are fed into the vacated LS bit positions
|
|
* and those MS bits shifted off the top are lost.
|
|
*/
|
|
|
|
void __bitmap_shift_left(unsigned long *dst, const unsigned long *src,
|
|
unsigned int shift, unsigned int nbits)
|
|
{
|
|
int k;
|
|
unsigned int lim = BITS_TO_LONGS(nbits);
|
|
unsigned int off = shift/BITS_PER_LONG, rem = shift % BITS_PER_LONG;
|
|
for (k = lim - off - 1; k >= 0; --k) {
|
|
unsigned long upper, lower;
|
|
|
|
/*
|
|
* If shift is not word aligned, take upper rem bits of
|
|
* word below and make them the bottom rem bits of result.
|
|
*/
|
|
if (rem && k > 0)
|
|
lower = src[k - 1] >> (BITS_PER_LONG - rem);
|
|
else
|
|
lower = 0;
|
|
upper = src[k] << rem;
|
|
dst[k + off] = lower | upper;
|
|
}
|
|
if (off)
|
|
memset(dst, 0, off*sizeof(unsigned long));
|
|
}
|
|
EXPORT_SYMBOL(__bitmap_shift_left);
|
|
|
|
int __bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
|
|
const unsigned long *bitmap2, unsigned int bits)
|
|
{
|
|
unsigned int k;
|
|
unsigned int lim = bits/BITS_PER_LONG;
|
|
unsigned long result = 0;
|
|
|
|
for (k = 0; k < lim; k++)
|
|
result |= (dst[k] = bitmap1[k] & bitmap2[k]);
|
|
if (bits % BITS_PER_LONG)
|
|
result |= (dst[k] = bitmap1[k] & bitmap2[k] &
|
|
BITMAP_LAST_WORD_MASK(bits));
|
|
return result != 0;
|
|
}
|
|
EXPORT_SYMBOL(__bitmap_and);
|
|
|
|
void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1,
|
|
const unsigned long *bitmap2, unsigned int bits)
|
|
{
|
|
unsigned int k;
|
|
unsigned int nr = BITS_TO_LONGS(bits);
|
|
|
|
for (k = 0; k < nr; k++)
|
|
dst[k] = bitmap1[k] | bitmap2[k];
|
|
}
|
|
EXPORT_SYMBOL(__bitmap_or);
|
|
|
|
void __bitmap_xor(unsigned long *dst, const unsigned long *bitmap1,
|
|
const unsigned long *bitmap2, unsigned int bits)
|
|
{
|
|
unsigned int k;
|
|
unsigned int nr = BITS_TO_LONGS(bits);
|
|
|
|
for (k = 0; k < nr; k++)
|
|
dst[k] = bitmap1[k] ^ bitmap2[k];
|
|
}
|
|
EXPORT_SYMBOL(__bitmap_xor);
|
|
|
|
int __bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1,
|
|
const unsigned long *bitmap2, unsigned int bits)
|
|
{
|
|
unsigned int k;
|
|
unsigned int lim = bits/BITS_PER_LONG;
|
|
unsigned long result = 0;
|
|
|
|
for (k = 0; k < lim; k++)
|
|
result |= (dst[k] = bitmap1[k] & ~bitmap2[k]);
|
|
if (bits % BITS_PER_LONG)
|
|
result |= (dst[k] = bitmap1[k] & ~bitmap2[k] &
|
|
BITMAP_LAST_WORD_MASK(bits));
|
|
return result != 0;
|
|
}
|
|
EXPORT_SYMBOL(__bitmap_andnot);
|
|
|
|
int __bitmap_intersects(const unsigned long *bitmap1,
|
|
const unsigned long *bitmap2, unsigned int bits)
|
|
{
|
|
unsigned int k, lim = bits/BITS_PER_LONG;
|
|
for (k = 0; k < lim; ++k)
|
|
if (bitmap1[k] & bitmap2[k])
|
|
return 1;
|
|
|
|
if (bits % BITS_PER_LONG)
|
|
if ((bitmap1[k] & bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))
|
|
return 1;
|
|
return 0;
|
|
}
|
|
EXPORT_SYMBOL(__bitmap_intersects);
|
|
|
|
int __bitmap_subset(const unsigned long *bitmap1,
|
|
const unsigned long *bitmap2, unsigned int bits)
|
|
{
|
|
unsigned int k, lim = bits/BITS_PER_LONG;
|
|
for (k = 0; k < lim; ++k)
|
|
if (bitmap1[k] & ~bitmap2[k])
|
|
return 0;
|
|
|
|
if (bits % BITS_PER_LONG)
|
|
if ((bitmap1[k] & ~bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))
|
|
return 0;
|
|
return 1;
|
|
}
|
|
EXPORT_SYMBOL(__bitmap_subset);
|
|
|
|
int __bitmap_weight(const unsigned long *bitmap, unsigned int bits)
|
|
{
|
|
unsigned int k, lim = bits/BITS_PER_LONG;
|
|
int w = 0;
|
|
|
|
for (k = 0; k < lim; k++)
|
|
w += hweight_long(bitmap[k]);
|
|
|
|
if (bits % BITS_PER_LONG)
|
|
w += hweight_long(bitmap[k] & BITMAP_LAST_WORD_MASK(bits));
|
|
|
|
return w;
|
|
}
|
|
EXPORT_SYMBOL(__bitmap_weight);
|
|
|
|
void bitmap_set(unsigned long *map, unsigned int start, 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) {
|
|
*p |= mask_to_set;
|
|
len -= bits_to_set;
|
|
bits_to_set = BITS_PER_LONG;
|
|
mask_to_set = ~0UL;
|
|
p++;
|
|
}
|
|
if (len) {
|
|
mask_to_set &= BITMAP_LAST_WORD_MASK(size);
|
|
*p |= mask_to_set;
|
|
}
|
|
}
|
|
EXPORT_SYMBOL(bitmap_set);
|
|
|
|
void bitmap_clear(unsigned long *map, unsigned int start, int len)
|
|
{
|
|
unsigned long *p = map + BIT_WORD(start);
|
|
const unsigned int size = start + len;
|
|
int bits_to_clear = BITS_PER_LONG - (start % BITS_PER_LONG);
|
|
unsigned long mask_to_clear = BITMAP_FIRST_WORD_MASK(start);
|
|
|
|
while (len - bits_to_clear >= 0) {
|
|
*p &= ~mask_to_clear;
|
|
len -= bits_to_clear;
|
|
bits_to_clear = BITS_PER_LONG;
|
|
mask_to_clear = ~0UL;
|
|
p++;
|
|
}
|
|
if (len) {
|
|
mask_to_clear &= BITMAP_LAST_WORD_MASK(size);
|
|
*p &= ~mask_to_clear;
|
|
}
|
|
}
|
|
EXPORT_SYMBOL(bitmap_clear);
|
|
|
|
/**
|
|
* bitmap_find_next_zero_area_off - find a contiguous aligned zero area
|
|
* @map: The address to base the search on
|
|
* @size: The bitmap size in bits
|
|
* @start: The bitnumber to start searching at
|
|
* @nr: The number of zeroed bits we're looking for
|
|
* @align_mask: Alignment mask for zero area
|
|
* @align_offset: Alignment offset for zero area.
|
|
*
|
|
* The @align_mask should be one less than a power of 2; the effect is that
|
|
* the bit offset of all zero areas this function finds plus @align_offset
|
|
* is multiple of that power of 2.
|
|
*/
|
|
unsigned long bitmap_find_next_zero_area_off(unsigned long *map,
|
|
unsigned long size,
|
|
unsigned long start,
|
|
unsigned int nr,
|
|
unsigned long align_mask,
|
|
unsigned long align_offset)
|
|
{
|
|
unsigned long index, end, i;
|
|
again:
|
|
index = find_next_zero_bit(map, size, start);
|
|
|
|
/* Align allocation */
|
|
index = __ALIGN_MASK(index + align_offset, align_mask) - align_offset;
|
|
|
|
end = index + nr;
|
|
if (end > size)
|
|
return end;
|
|
i = find_next_bit(map, end, index);
|
|
if (i < end) {
|
|
start = i + 1;
|
|
goto again;
|
|
}
|
|
return index;
|
|
}
|
|
EXPORT_SYMBOL(bitmap_find_next_zero_area_off);
|
|
|
|
/*
|
|
* Bitmap printing & parsing functions: first version by Nadia Yvette Chambers,
|
|
* second version by Paul Jackson, third by Joe Korty.
|
|
*/
|
|
|
|
#define CHUNKSZ 32
|
|
#define nbits_to_hold_value(val) fls(val)
|
|
#define BASEDEC 10 /* fancier cpuset lists input in decimal */
|
|
|
|
/**
|
|
* __bitmap_parse - convert an ASCII hex string into a bitmap.
|
|
* @buf: pointer to buffer containing string.
|
|
* @buflen: buffer size in bytes. If string is smaller than this
|
|
* then it must be terminated with a \0.
|
|
* @is_user: location of buffer, 0 indicates kernel space
|
|
* @maskp: pointer to bitmap array that will contain result.
|
|
* @nmaskbits: size of bitmap, in bits.
|
|
*
|
|
* Commas group hex digits into chunks. Each chunk defines exactly 32
|
|
* bits of the resultant bitmask. No chunk may specify a value larger
|
|
* than 32 bits (%-EOVERFLOW), and if a chunk specifies a smaller value
|
|
* then leading 0-bits are prepended. %-EINVAL is returned for illegal
|
|
* characters and for grouping errors such as "1,,5", ",44", "," and "".
|
|
* Leading and trailing whitespace accepted, but not embedded whitespace.
|
|
*/
|
|
int __bitmap_parse(const char *buf, unsigned int buflen,
|
|
int is_user, unsigned long *maskp,
|
|
int nmaskbits)
|
|
{
|
|
int c, old_c, totaldigits, ndigits, nchunks, nbits;
|
|
u32 chunk;
|
|
const char __user __force *ubuf = (const char __user __force *)buf;
|
|
|
|
bitmap_zero(maskp, nmaskbits);
|
|
|
|
nchunks = nbits = totaldigits = c = 0;
|
|
do {
|
|
chunk = 0;
|
|
ndigits = totaldigits;
|
|
|
|
/* Get the next chunk of the bitmap */
|
|
while (buflen) {
|
|
old_c = c;
|
|
if (is_user) {
|
|
if (__get_user(c, ubuf++))
|
|
return -EFAULT;
|
|
}
|
|
else
|
|
c = *buf++;
|
|
buflen--;
|
|
if (isspace(c))
|
|
continue;
|
|
|
|
/*
|
|
* If the last character was a space and the current
|
|
* character isn't '\0', we've got embedded whitespace.
|
|
* This is a no-no, so throw an error.
|
|
*/
|
|
if (totaldigits && c && isspace(old_c))
|
|
return -EINVAL;
|
|
|
|
/* A '\0' or a ',' signal the end of the chunk */
|
|
if (c == '\0' || c == ',')
|
|
break;
|
|
|
|
if (!isxdigit(c))
|
|
return -EINVAL;
|
|
|
|
/*
|
|
* Make sure there are at least 4 free bits in 'chunk'.
|
|
* If not, this hexdigit will overflow 'chunk', so
|
|
* throw an error.
|
|
*/
|
|
if (chunk & ~((1UL << (CHUNKSZ - 4)) - 1))
|
|
return -EOVERFLOW;
|
|
|
|
chunk = (chunk << 4) | hex_to_bin(c);
|
|
totaldigits++;
|
|
}
|
|
if (ndigits == totaldigits)
|
|
return -EINVAL;
|
|
if (nchunks == 0 && chunk == 0)
|
|
continue;
|
|
|
|
__bitmap_shift_left(maskp, maskp, CHUNKSZ, nmaskbits);
|
|
*maskp |= chunk;
|
|
nchunks++;
|
|
nbits += (nchunks == 1) ? nbits_to_hold_value(chunk) : CHUNKSZ;
|
|
if (nbits > nmaskbits)
|
|
return -EOVERFLOW;
|
|
} while (buflen && c == ',');
|
|
|
|
return 0;
|
|
}
|
|
EXPORT_SYMBOL(__bitmap_parse);
|
|
|
|
/**
|
|
* bitmap_parse_user - convert an ASCII hex string in a user buffer into a bitmap
|
|
*
|
|
* @ubuf: pointer to user buffer containing string.
|
|
* @ulen: buffer size in bytes. If string is smaller than this
|
|
* then it must be terminated with a \0.
|
|
* @maskp: pointer to bitmap array that will contain result.
|
|
* @nmaskbits: size of bitmap, in bits.
|
|
*
|
|
* Wrapper for __bitmap_parse(), providing it with user buffer.
|
|
*
|
|
* We cannot have this as an inline function in bitmap.h because it needs
|
|
* linux/uaccess.h to get the access_ok() declaration and this causes
|
|
* cyclic dependencies.
|
|
*/
|
|
int bitmap_parse_user(const char __user *ubuf,
|
|
unsigned int ulen, unsigned long *maskp,
|
|
int nmaskbits)
|
|
{
|
|
if (!access_ok(VERIFY_READ, ubuf, ulen))
|
|
return -EFAULT;
|
|
return __bitmap_parse((const char __force *)ubuf,
|
|
ulen, 1, maskp, nmaskbits);
|
|
|
|
}
|
|
EXPORT_SYMBOL(bitmap_parse_user);
|
|
|
|
/**
|
|
* bitmap_print_to_pagebuf - convert bitmap to list or hex format ASCII string
|
|
* @list: indicates whether the bitmap must be list
|
|
* @buf: page aligned buffer into which string is placed
|
|
* @maskp: pointer to bitmap to convert
|
|
* @nmaskbits: size of bitmap, in bits
|
|
*
|
|
* Output format is a comma-separated list of decimal numbers and
|
|
* ranges if list is specified or hex digits grouped into comma-separated
|
|
* sets of 8 digits/set. Returns the number of characters written to buf.
|
|
*
|
|
* It is assumed that @buf is a pointer into a PAGE_SIZE area and that
|
|
* sufficient storage remains at @buf to accommodate the
|
|
* bitmap_print_to_pagebuf() output.
|
|
*/
|
|
int bitmap_print_to_pagebuf(bool list, char *buf, const unsigned long *maskp,
|
|
int nmaskbits)
|
|
{
|
|
ptrdiff_t len = PTR_ALIGN(buf + PAGE_SIZE - 1, PAGE_SIZE) - buf;
|
|
int n = 0;
|
|
|
|
if (len > 1)
|
|
n = list ? scnprintf(buf, len, "%*pbl\n", nmaskbits, maskp) :
|
|
scnprintf(buf, len, "%*pb\n", nmaskbits, maskp);
|
|
return n;
|
|
}
|
|
EXPORT_SYMBOL(bitmap_print_to_pagebuf);
|
|
|
|
/**
|
|
* __bitmap_parselist - convert list format ASCII string to bitmap
|
|
* @buf: read nul-terminated user string from this buffer
|
|
* @buflen: buffer size in bytes. If string is smaller than this
|
|
* then it must be terminated with a \0.
|
|
* @is_user: location of buffer, 0 indicates kernel space
|
|
* @maskp: write resulting mask here
|
|
* @nmaskbits: number of bits in mask to be written
|
|
*
|
|
* Input format is a comma-separated list of decimal numbers and
|
|
* ranges. Consecutively set bits are shown as two hyphen-separated
|
|
* decimal numbers, the smallest and largest bit numbers set in
|
|
* the range.
|
|
* Optionally each range can be postfixed to denote that only parts of it
|
|
* should be set. The range will divided to groups of specific size.
|
|
* From each group will be used only defined amount of bits.
|
|
* Syntax: range:used_size/group_size
|
|
* Example: 0-1023:2/256 ==> 0,1,256,257,512,513,768,769
|
|
*
|
|
* Returns 0 on success, -errno on invalid input strings.
|
|
* Error values:
|
|
* %-EINVAL: second number in range smaller than first
|
|
* %-EINVAL: invalid character in string
|
|
* %-ERANGE: bit number specified too large for mask
|
|
*/
|
|
static int __bitmap_parselist(const char *buf, unsigned int buflen,
|
|
int is_user, unsigned long *maskp,
|
|
int nmaskbits)
|
|
{
|
|
unsigned int a, b, old_a, old_b;
|
|
unsigned int group_size, used_size;
|
|
int c, old_c, totaldigits, ndigits;
|
|
const char __user __force *ubuf = (const char __user __force *)buf;
|
|
int at_start, in_range, in_partial_range;
|
|
|
|
totaldigits = c = 0;
|
|
old_a = old_b = 0;
|
|
group_size = used_size = 0;
|
|
bitmap_zero(maskp, nmaskbits);
|
|
do {
|
|
at_start = 1;
|
|
in_range = 0;
|
|
in_partial_range = 0;
|
|
a = b = 0;
|
|
ndigits = totaldigits;
|
|
|
|
/* Get the next cpu# or a range of cpu#'s */
|
|
while (buflen) {
|
|
old_c = c;
|
|
if (is_user) {
|
|
if (__get_user(c, ubuf++))
|
|
return -EFAULT;
|
|
} else
|
|
c = *buf++;
|
|
buflen--;
|
|
if (isspace(c))
|
|
continue;
|
|
|
|
/* A '\0' or a ',' signal the end of a cpu# or range */
|
|
if (c == '\0' || c == ',')
|
|
break;
|
|
/*
|
|
* whitespaces between digits are not allowed,
|
|
* but it's ok if whitespaces are on head or tail.
|
|
* when old_c is whilespace,
|
|
* if totaldigits == ndigits, whitespace is on head.
|
|
* if whitespace is on tail, it should not run here.
|
|
* as c was ',' or '\0',
|
|
* the last code line has broken the current loop.
|
|
*/
|
|
if ((totaldigits != ndigits) && isspace(old_c))
|
|
return -EINVAL;
|
|
|
|
if (c == '/') {
|
|
used_size = a;
|
|
at_start = 1;
|
|
in_range = 0;
|
|
a = b = 0;
|
|
continue;
|
|
}
|
|
|
|
if (c == ':') {
|
|
old_a = a;
|
|
old_b = b;
|
|
at_start = 1;
|
|
in_range = 0;
|
|
in_partial_range = 1;
|
|
a = b = 0;
|
|
continue;
|
|
}
|
|
|
|
if (c == '-') {
|
|
if (at_start || in_range)
|
|
return -EINVAL;
|
|
b = 0;
|
|
in_range = 1;
|
|
at_start = 1;
|
|
continue;
|
|
}
|
|
|
|
if (!isdigit(c))
|
|
return -EINVAL;
|
|
|
|
b = b * 10 + (c - '0');
|
|
if (!in_range)
|
|
a = b;
|
|
at_start = 0;
|
|
totaldigits++;
|
|
}
|
|
if (ndigits == totaldigits)
|
|
continue;
|
|
if (in_partial_range) {
|
|
group_size = a;
|
|
a = old_a;
|
|
b = old_b;
|
|
old_a = old_b = 0;
|
|
}
|
|
/* if no digit is after '-', it's wrong*/
|
|
if (at_start && in_range)
|
|
return -EINVAL;
|
|
if (!(a <= b) || !(used_size <= group_size))
|
|
return -EINVAL;
|
|
if (b >= nmaskbits)
|
|
return -ERANGE;
|
|
while (a <= b) {
|
|
if (in_partial_range) {
|
|
static int pos_in_group = 1;
|
|
|
|
if (pos_in_group <= used_size)
|
|
set_bit(a, maskp);
|
|
|
|
if (a == b || ++pos_in_group > group_size)
|
|
pos_in_group = 1;
|
|
} else
|
|
set_bit(a, maskp);
|
|
a++;
|
|
}
|
|
} while (buflen && c == ',');
|
|
return 0;
|
|
}
|
|
|
|
int bitmap_parselist(const char *bp, unsigned long *maskp, int nmaskbits)
|
|
{
|
|
char *nl = strchrnul(bp, '\n');
|
|
int len = nl - bp;
|
|
|
|
return __bitmap_parselist(bp, len, 0, maskp, nmaskbits);
|
|
}
|
|
EXPORT_SYMBOL(bitmap_parselist);
|
|
|
|
|
|
/**
|
|
* bitmap_parselist_user()
|
|
*
|
|
* @ubuf: pointer to user buffer containing string.
|
|
* @ulen: buffer size in bytes. If string is smaller than this
|
|
* then it must be terminated with a \0.
|
|
* @maskp: pointer to bitmap array that will contain result.
|
|
* @nmaskbits: size of bitmap, in bits.
|
|
*
|
|
* Wrapper for bitmap_parselist(), providing it with user buffer.
|
|
*
|
|
* We cannot have this as an inline function in bitmap.h because it needs
|
|
* linux/uaccess.h to get the access_ok() declaration and this causes
|
|
* cyclic dependencies.
|
|
*/
|
|
int bitmap_parselist_user(const char __user *ubuf,
|
|
unsigned int ulen, unsigned long *maskp,
|
|
int nmaskbits)
|
|
{
|
|
if (!access_ok(VERIFY_READ, ubuf, ulen))
|
|
return -EFAULT;
|
|
return __bitmap_parselist((const char __force *)ubuf,
|
|
ulen, 1, maskp, nmaskbits);
|
|
}
|
|
EXPORT_SYMBOL(bitmap_parselist_user);
|
|
|
|
|
|
/**
|
|
* bitmap_pos_to_ord - find ordinal of set bit at given position in bitmap
|
|
* @buf: pointer to a bitmap
|
|
* @pos: a bit position in @buf (0 <= @pos < @nbits)
|
|
* @nbits: number of valid bit positions in @buf
|
|
*
|
|
* Map the bit at position @pos in @buf (of length @nbits) to the
|
|
* ordinal of which set bit it is. If it is not set or if @pos
|
|
* is not a valid bit position, map to -1.
|
|
*
|
|
* If for example, just bits 4 through 7 are set in @buf, then @pos
|
|
* values 4 through 7 will get mapped to 0 through 3, respectively,
|
|
* and other @pos values will get mapped to -1. When @pos value 7
|
|
* gets mapped to (returns) @ord value 3 in this example, that means
|
|
* that bit 7 is the 3rd (starting with 0th) set bit in @buf.
|
|
*
|
|
* The bit positions 0 through @bits are valid positions in @buf.
|
|
*/
|
|
static int bitmap_pos_to_ord(const unsigned long *buf, unsigned int pos, unsigned int nbits)
|
|
{
|
|
if (pos >= nbits || !test_bit(pos, buf))
|
|
return -1;
|
|
|
|
return __bitmap_weight(buf, pos);
|
|
}
|
|
|
|
/**
|
|
* bitmap_ord_to_pos - find position of n-th set bit in bitmap
|
|
* @buf: pointer to bitmap
|
|
* @ord: ordinal bit position (n-th set bit, n >= 0)
|
|
* @nbits: number of valid bit positions in @buf
|
|
*
|
|
* Map the ordinal offset of bit @ord in @buf to its position in @buf.
|
|
* Value of @ord should be in range 0 <= @ord < weight(buf). If @ord
|
|
* >= weight(buf), returns @nbits.
|
|
*
|
|
* If for example, just bits 4 through 7 are set in @buf, then @ord
|
|
* values 0 through 3 will get mapped to 4 through 7, respectively,
|
|
* and all other @ord values returns @nbits. When @ord value 3
|
|
* gets mapped to (returns) @pos value 7 in this example, that means
|
|
* that the 3rd set bit (starting with 0th) is at position 7 in @buf.
|
|
*
|
|
* The bit positions 0 through @nbits-1 are valid positions in @buf.
|
|
*/
|
|
unsigned int bitmap_ord_to_pos(const unsigned long *buf, unsigned int ord, unsigned int nbits)
|
|
{
|
|
unsigned int pos;
|
|
|
|
for (pos = find_first_bit(buf, nbits);
|
|
pos < nbits && ord;
|
|
pos = find_next_bit(buf, nbits, pos + 1))
|
|
ord--;
|
|
|
|
return pos;
|
|
}
|
|
|
|
/**
|
|
* bitmap_remap - Apply map defined by a pair of bitmaps to another bitmap
|
|
* @dst: remapped result
|
|
* @src: subset to be remapped
|
|
* @old: defines domain of map
|
|
* @new: defines range of map
|
|
* @nbits: number of bits in each of these bitmaps
|
|
*
|
|
* Let @old and @new define a mapping of bit positions, such that
|
|
* whatever position is held by the n-th set bit in @old is mapped
|
|
* to the n-th set bit in @new. In the more general case, allowing
|
|
* for the possibility that the weight 'w' of @new is less than the
|
|
* weight of @old, map the position of the n-th set bit in @old to
|
|
* the position of the m-th set bit in @new, where m == n % w.
|
|
*
|
|
* If either of the @old and @new bitmaps are empty, or if @src and
|
|
* @dst point to the same location, then this routine copies @src
|
|
* to @dst.
|
|
*
|
|
* The positions of unset bits in @old are mapped to themselves
|
|
* (the identify map).
|
|
*
|
|
* Apply the above specified mapping to @src, placing the result in
|
|
* @dst, clearing any bits previously set in @dst.
|
|
*
|
|
* For example, lets say that @old has bits 4 through 7 set, and
|
|
* @new has bits 12 through 15 set. This defines the mapping of bit
|
|
* position 4 to 12, 5 to 13, 6 to 14 and 7 to 15, and of all other
|
|
* bit positions unchanged. So if say @src comes into this routine
|
|
* with bits 1, 5 and 7 set, then @dst should leave with bits 1,
|
|
* 13 and 15 set.
|
|
*/
|
|
void bitmap_remap(unsigned long *dst, const unsigned long *src,
|
|
const unsigned long *old, const unsigned long *new,
|
|
unsigned int nbits)
|
|
{
|
|
unsigned int oldbit, w;
|
|
|
|
if (dst == src) /* following doesn't handle inplace remaps */
|
|
return;
|
|
bitmap_zero(dst, nbits);
|
|
|
|
w = bitmap_weight(new, nbits);
|
|
for_each_set_bit(oldbit, src, nbits) {
|
|
int n = bitmap_pos_to_ord(old, oldbit, nbits);
|
|
|
|
if (n < 0 || w == 0)
|
|
set_bit(oldbit, dst); /* identity map */
|
|
else
|
|
set_bit(bitmap_ord_to_pos(new, n % w, nbits), dst);
|
|
}
|
|
}
|
|
EXPORT_SYMBOL(bitmap_remap);
|
|
|
|
/**
|
|
* bitmap_bitremap - Apply map defined by a pair of bitmaps to a single bit
|
|
* @oldbit: bit position to be mapped
|
|
* @old: defines domain of map
|
|
* @new: defines range of map
|
|
* @bits: number of bits in each of these bitmaps
|
|
*
|
|
* Let @old and @new define a mapping of bit positions, such that
|
|
* whatever position is held by the n-th set bit in @old is mapped
|
|
* to the n-th set bit in @new. In the more general case, allowing
|
|
* for the possibility that the weight 'w' of @new is less than the
|
|
* weight of @old, map the position of the n-th set bit in @old to
|
|
* the position of the m-th set bit in @new, where m == n % w.
|
|
*
|
|
* The positions of unset bits in @old are mapped to themselves
|
|
* (the identify map).
|
|
*
|
|
* Apply the above specified mapping to bit position @oldbit, returning
|
|
* the new bit position.
|
|
*
|
|
* For example, lets say that @old has bits 4 through 7 set, and
|
|
* @new has bits 12 through 15 set. This defines the mapping of bit
|
|
* position 4 to 12, 5 to 13, 6 to 14 and 7 to 15, and of all other
|
|
* bit positions unchanged. So if say @oldbit is 5, then this routine
|
|
* returns 13.
|
|
*/
|
|
int bitmap_bitremap(int oldbit, const unsigned long *old,
|
|
const unsigned long *new, int bits)
|
|
{
|
|
int w = bitmap_weight(new, bits);
|
|
int n = bitmap_pos_to_ord(old, oldbit, bits);
|
|
if (n < 0 || w == 0)
|
|
return oldbit;
|
|
else
|
|
return bitmap_ord_to_pos(new, n % w, bits);
|
|
}
|
|
EXPORT_SYMBOL(bitmap_bitremap);
|
|
|
|
/**
|
|
* bitmap_onto - translate one bitmap relative to another
|
|
* @dst: resulting translated bitmap
|
|
* @orig: original untranslated bitmap
|
|
* @relmap: bitmap relative to which translated
|
|
* @bits: number of bits in each of these bitmaps
|
|
*
|
|
* Set the n-th bit of @dst iff there exists some m such that the
|
|
* n-th bit of @relmap is set, the m-th bit of @orig is set, and
|
|
* the n-th bit of @relmap is also the m-th _set_ bit of @relmap.
|
|
* (If you understood the previous sentence the first time your
|
|
* read it, you're overqualified for your current job.)
|
|
*
|
|
* In other words, @orig is mapped onto (surjectively) @dst,
|
|
* using the map { <n, m> | the n-th bit of @relmap is the
|
|
* m-th set bit of @relmap }.
|
|
*
|
|
* Any set bits in @orig above bit number W, where W is the
|
|
* weight of (number of set bits in) @relmap are mapped nowhere.
|
|
* In particular, if for all bits m set in @orig, m >= W, then
|
|
* @dst will end up empty. In situations where the possibility
|
|
* of such an empty result is not desired, one way to avoid it is
|
|
* to use the bitmap_fold() operator, below, to first fold the
|
|
* @orig bitmap over itself so that all its set bits x are in the
|
|
* range 0 <= x < W. The bitmap_fold() operator does this by
|
|
* setting the bit (m % W) in @dst, for each bit (m) set in @orig.
|
|
*
|
|
* Example [1] for bitmap_onto():
|
|
* Let's say @relmap has bits 30-39 set, and @orig has bits
|
|
* 1, 3, 5, 7, 9 and 11 set. Then on return from this routine,
|
|
* @dst will have bits 31, 33, 35, 37 and 39 set.
|
|
*
|
|
* When bit 0 is set in @orig, it means turn on the bit in
|
|
* @dst corresponding to whatever is the first bit (if any)
|
|
* that is turned on in @relmap. Since bit 0 was off in the
|
|
* above example, we leave off that bit (bit 30) in @dst.
|
|
*
|
|
* When bit 1 is set in @orig (as in the above example), it
|
|
* means turn on the bit in @dst corresponding to whatever
|
|
* is the second bit that is turned on in @relmap. The second
|
|
* bit in @relmap that was turned on in the above example was
|
|
* bit 31, so we turned on bit 31 in @dst.
|
|
*
|
|
* Similarly, we turned on bits 33, 35, 37 and 39 in @dst,
|
|
* because they were the 4th, 6th, 8th and 10th set bits
|
|
* set in @relmap, and the 4th, 6th, 8th and 10th bits of
|
|
* @orig (i.e. bits 3, 5, 7 and 9) were also set.
|
|
*
|
|
* When bit 11 is set in @orig, it means turn on the bit in
|
|
* @dst corresponding to whatever is the twelfth bit that is
|
|
* turned on in @relmap. In the above example, there were
|
|
* only ten bits turned on in @relmap (30..39), so that bit
|
|
* 11 was set in @orig had no affect on @dst.
|
|
*
|
|
* Example [2] for bitmap_fold() + bitmap_onto():
|
|
* Let's say @relmap has these ten bits set:
|
|
* 40 41 42 43 45 48 53 61 74 95
|
|
* (for the curious, that's 40 plus the first ten terms of the
|
|
* Fibonacci sequence.)
|
|
*
|
|
* Further lets say we use the following code, invoking
|
|
* bitmap_fold() then bitmap_onto, as suggested above to
|
|
* avoid the possibility of an empty @dst result:
|
|
*
|
|
* unsigned long *tmp; // a temporary bitmap's bits
|
|
*
|
|
* bitmap_fold(tmp, orig, bitmap_weight(relmap, bits), bits);
|
|
* bitmap_onto(dst, tmp, relmap, bits);
|
|
*
|
|
* Then this table shows what various values of @dst would be, for
|
|
* various @orig's. I list the zero-based positions of each set bit.
|
|
* The tmp column shows the intermediate result, as computed by
|
|
* using bitmap_fold() to fold the @orig bitmap modulo ten
|
|
* (the weight of @relmap).
|
|
*
|
|
* @orig tmp @dst
|
|
* 0 0 40
|
|
* 1 1 41
|
|
* 9 9 95
|
|
* 10 0 40 (*)
|
|
* 1 3 5 7 1 3 5 7 41 43 48 61
|
|
* 0 1 2 3 4 0 1 2 3 4 40 41 42 43 45
|
|
* 0 9 18 27 0 9 8 7 40 61 74 95
|
|
* 0 10 20 30 0 40
|
|
* 0 11 22 33 0 1 2 3 40 41 42 43
|
|
* 0 12 24 36 0 2 4 6 40 42 45 53
|
|
* 78 102 211 1 2 8 41 42 74 (*)
|
|
*
|
|
* (*) For these marked lines, if we hadn't first done bitmap_fold()
|
|
* into tmp, then the @dst result would have been empty.
|
|
*
|
|
* If either of @orig or @relmap is empty (no set bits), then @dst
|
|
* will be returned empty.
|
|
*
|
|
* If (as explained above) the only set bits in @orig are in positions
|
|
* m where m >= W, (where W is the weight of @relmap) then @dst will
|
|
* once again be returned empty.
|
|
*
|
|
* All bits in @dst not set by the above rule are cleared.
|
|
*/
|
|
void bitmap_onto(unsigned long *dst, const unsigned long *orig,
|
|
const unsigned long *relmap, unsigned int bits)
|
|
{
|
|
unsigned int n, m; /* same meaning as in above comment */
|
|
|
|
if (dst == orig) /* following doesn't handle inplace mappings */
|
|
return;
|
|
bitmap_zero(dst, bits);
|
|
|
|
/*
|
|
* The following code is a more efficient, but less
|
|
* obvious, equivalent to the loop:
|
|
* for (m = 0; m < bitmap_weight(relmap, bits); m++) {
|
|
* n = bitmap_ord_to_pos(orig, m, bits);
|
|
* if (test_bit(m, orig))
|
|
* set_bit(n, dst);
|
|
* }
|
|
*/
|
|
|
|
m = 0;
|
|
for_each_set_bit(n, relmap, bits) {
|
|
/* m == bitmap_pos_to_ord(relmap, n, bits) */
|
|
if (test_bit(m, orig))
|
|
set_bit(n, dst);
|
|
m++;
|
|
}
|
|
}
|
|
EXPORT_SYMBOL(bitmap_onto);
|
|
|
|
/**
|
|
* bitmap_fold - fold larger bitmap into smaller, modulo specified size
|
|
* @dst: resulting smaller bitmap
|
|
* @orig: original larger bitmap
|
|
* @sz: specified size
|
|
* @nbits: number of bits in each of these bitmaps
|
|
*
|
|
* For each bit oldbit in @orig, set bit oldbit mod @sz in @dst.
|
|
* Clear all other bits in @dst. See further the comment and
|
|
* Example [2] for bitmap_onto() for why and how to use this.
|
|
*/
|
|
void bitmap_fold(unsigned long *dst, const unsigned long *orig,
|
|
unsigned int sz, unsigned int nbits)
|
|
{
|
|
unsigned int oldbit;
|
|
|
|
if (dst == orig) /* following doesn't handle inplace mappings */
|
|
return;
|
|
bitmap_zero(dst, nbits);
|
|
|
|
for_each_set_bit(oldbit, orig, nbits)
|
|
set_bit(oldbit % sz, dst);
|
|
}
|
|
EXPORT_SYMBOL(bitmap_fold);
|
|
|
|
/*
|
|
* Common code for bitmap_*_region() routines.
|
|
* bitmap: array of unsigned longs corresponding to the bitmap
|
|
* pos: the beginning of the region
|
|
* order: region size (log base 2 of number of bits)
|
|
* reg_op: operation(s) to perform on that region of bitmap
|
|
*
|
|
* Can set, verify and/or release a region of bits in a bitmap,
|
|
* depending on which combination of REG_OP_* flag bits is set.
|
|
*
|
|
* A region of a bitmap is a sequence of bits in the bitmap, of
|
|
* some size '1 << order' (a power of two), aligned to that same
|
|
* '1 << order' power of two.
|
|
*
|
|
* Returns 1 if REG_OP_ISFREE succeeds (region is all zero bits).
|
|
* Returns 0 in all other cases and reg_ops.
|
|
*/
|
|
|
|
enum {
|
|
REG_OP_ISFREE, /* true if region is all zero bits */
|
|
REG_OP_ALLOC, /* set all bits in region */
|
|
REG_OP_RELEASE, /* clear all bits in region */
|
|
};
|
|
|
|
static int __reg_op(unsigned long *bitmap, unsigned int pos, int order, int reg_op)
|
|
{
|
|
int nbits_reg; /* number of bits in region */
|
|
int index; /* index first long of region in bitmap */
|
|
int offset; /* bit offset region in bitmap[index] */
|
|
int nlongs_reg; /* num longs spanned by region in bitmap */
|
|
int nbitsinlong; /* num bits of region in each spanned long */
|
|
unsigned long mask; /* bitmask for one long of region */
|
|
int i; /* scans bitmap by longs */
|
|
int ret = 0; /* return value */
|
|
|
|
/*
|
|
* Either nlongs_reg == 1 (for small orders that fit in one long)
|
|
* or (offset == 0 && mask == ~0UL) (for larger multiword orders.)
|
|
*/
|
|
nbits_reg = 1 << order;
|
|
index = pos / BITS_PER_LONG;
|
|
offset = pos - (index * BITS_PER_LONG);
|
|
nlongs_reg = BITS_TO_LONGS(nbits_reg);
|
|
nbitsinlong = min(nbits_reg, BITS_PER_LONG);
|
|
|
|
/*
|
|
* Can't do "mask = (1UL << nbitsinlong) - 1", as that
|
|
* overflows if nbitsinlong == BITS_PER_LONG.
|
|
*/
|
|
mask = (1UL << (nbitsinlong - 1));
|
|
mask += mask - 1;
|
|
mask <<= offset;
|
|
|
|
switch (reg_op) {
|
|
case REG_OP_ISFREE:
|
|
for (i = 0; i < nlongs_reg; i++) {
|
|
if (bitmap[index + i] & mask)
|
|
goto done;
|
|
}
|
|
ret = 1; /* all bits in region free (zero) */
|
|
break;
|
|
|
|
case REG_OP_ALLOC:
|
|
for (i = 0; i < nlongs_reg; i++)
|
|
bitmap[index + i] |= mask;
|
|
break;
|
|
|
|
case REG_OP_RELEASE:
|
|
for (i = 0; i < nlongs_reg; i++)
|
|
bitmap[index + i] &= ~mask;
|
|
break;
|
|
}
|
|
done:
|
|
return ret;
|
|
}
|
|
|
|
/**
|
|
* bitmap_find_free_region - find a contiguous aligned mem region
|
|
* @bitmap: array of unsigned longs corresponding to the bitmap
|
|
* @bits: number of bits in the bitmap
|
|
* @order: region size (log base 2 of number of bits) to find
|
|
*
|
|
* Find a region of free (zero) bits in a @bitmap of @bits bits and
|
|
* allocate them (set them to one). Only consider regions of length
|
|
* a power (@order) of two, aligned to that power of two, which
|
|
* makes the search algorithm much faster.
|
|
*
|
|
* Return the bit offset in bitmap of the allocated region,
|
|
* or -errno on failure.
|
|
*/
|
|
int bitmap_find_free_region(unsigned long *bitmap, unsigned int bits, int order)
|
|
{
|
|
unsigned int pos, end; /* scans bitmap by regions of size order */
|
|
|
|
for (pos = 0 ; (end = pos + (1U << order)) <= bits; pos = end) {
|
|
if (!__reg_op(bitmap, pos, order, REG_OP_ISFREE))
|
|
continue;
|
|
__reg_op(bitmap, pos, order, REG_OP_ALLOC);
|
|
return pos;
|
|
}
|
|
return -ENOMEM;
|
|
}
|
|
EXPORT_SYMBOL(bitmap_find_free_region);
|
|
|
|
/**
|
|
* bitmap_release_region - release allocated bitmap region
|
|
* @bitmap: array of unsigned longs corresponding to the bitmap
|
|
* @pos: beginning of bit region to release
|
|
* @order: region size (log base 2 of number of bits) to release
|
|
*
|
|
* This is the complement to __bitmap_find_free_region() and releases
|
|
* the found region (by clearing it in the bitmap).
|
|
*
|
|
* No return value.
|
|
*/
|
|
void bitmap_release_region(unsigned long *bitmap, unsigned int pos, int order)
|
|
{
|
|
__reg_op(bitmap, pos, order, REG_OP_RELEASE);
|
|
}
|
|
EXPORT_SYMBOL(bitmap_release_region);
|
|
|
|
/**
|
|
* bitmap_allocate_region - allocate bitmap region
|
|
* @bitmap: array of unsigned longs corresponding to the bitmap
|
|
* @pos: beginning of bit region to allocate
|
|
* @order: region size (log base 2 of number of bits) to allocate
|
|
*
|
|
* Allocate (set bits in) a specified region of a bitmap.
|
|
*
|
|
* Return 0 on success, or %-EBUSY if specified region wasn't
|
|
* free (not all bits were zero).
|
|
*/
|
|
int bitmap_allocate_region(unsigned long *bitmap, unsigned int pos, int order)
|
|
{
|
|
if (!__reg_op(bitmap, pos, order, REG_OP_ISFREE))
|
|
return -EBUSY;
|
|
return __reg_op(bitmap, pos, order, REG_OP_ALLOC);
|
|
}
|
|
EXPORT_SYMBOL(bitmap_allocate_region);
|
|
|
|
/**
|
|
* bitmap_from_u32array - copy the contents of a u32 array of bits to bitmap
|
|
* @bitmap: array of unsigned longs, the destination bitmap, non NULL
|
|
* @nbits: number of bits in @bitmap
|
|
* @buf: array of u32 (in host byte order), the source bitmap, non NULL
|
|
* @nwords: number of u32 words in @buf
|
|
*
|
|
* copy min(nbits, 32*nwords) bits from @buf to @bitmap, remaining
|
|
* bits between nword and nbits in @bitmap (if any) are cleared. In
|
|
* last word of @bitmap, the bits beyond nbits (if any) are kept
|
|
* unchanged.
|
|
*
|
|
* Return the number of bits effectively copied.
|
|
*/
|
|
unsigned int
|
|
bitmap_from_u32array(unsigned long *bitmap, unsigned int nbits,
|
|
const u32 *buf, unsigned int nwords)
|
|
{
|
|
unsigned int dst_idx, src_idx;
|
|
|
|
for (src_idx = dst_idx = 0; dst_idx < BITS_TO_LONGS(nbits); ++dst_idx) {
|
|
unsigned long part = 0;
|
|
|
|
if (src_idx < nwords)
|
|
part = buf[src_idx++];
|
|
|
|
#if BITS_PER_LONG == 64
|
|
if (src_idx < nwords)
|
|
part |= ((unsigned long) buf[src_idx++]) << 32;
|
|
#endif
|
|
|
|
if (dst_idx < nbits/BITS_PER_LONG)
|
|
bitmap[dst_idx] = part;
|
|
else {
|
|
unsigned long mask = BITMAP_LAST_WORD_MASK(nbits);
|
|
|
|
bitmap[dst_idx] = (bitmap[dst_idx] & ~mask)
|
|
| (part & mask);
|
|
}
|
|
}
|
|
|
|
return min_t(unsigned int, nbits, 32*nwords);
|
|
}
|
|
EXPORT_SYMBOL(bitmap_from_u32array);
|
|
|
|
/**
|
|
* bitmap_to_u32array - copy the contents of bitmap to a u32 array of bits
|
|
* @buf: array of u32 (in host byte order), the dest bitmap, non NULL
|
|
* @nwords: number of u32 words in @buf
|
|
* @bitmap: array of unsigned longs, the source bitmap, non NULL
|
|
* @nbits: number of bits in @bitmap
|
|
*
|
|
* copy min(nbits, 32*nwords) bits from @bitmap to @buf. Remaining
|
|
* bits after nbits in @buf (if any) are cleared.
|
|
*
|
|
* Return the number of bits effectively copied.
|
|
*/
|
|
unsigned int
|
|
bitmap_to_u32array(u32 *buf, unsigned int nwords,
|
|
const unsigned long *bitmap, unsigned int nbits)
|
|
{
|
|
unsigned int dst_idx = 0, src_idx = 0;
|
|
|
|
while (dst_idx < nwords) {
|
|
unsigned long part = 0;
|
|
|
|
if (src_idx < BITS_TO_LONGS(nbits)) {
|
|
part = bitmap[src_idx];
|
|
if (src_idx >= nbits/BITS_PER_LONG)
|
|
part &= BITMAP_LAST_WORD_MASK(nbits);
|
|
src_idx++;
|
|
}
|
|
|
|
buf[dst_idx++] = part & 0xffffffffUL;
|
|
|
|
#if BITS_PER_LONG == 64
|
|
if (dst_idx < nwords) {
|
|
part >>= 32;
|
|
buf[dst_idx++] = part & 0xffffffffUL;
|
|
}
|
|
#endif
|
|
}
|
|
|
|
return min_t(unsigned int, nbits, 32*nwords);
|
|
}
|
|
EXPORT_SYMBOL(bitmap_to_u32array);
|
|
|
|
/**
|
|
* bitmap_copy_le - copy a bitmap, putting the bits into little-endian order.
|
|
* @dst: destination buffer
|
|
* @src: bitmap to copy
|
|
* @nbits: number of bits in the bitmap
|
|
*
|
|
* Require nbits % BITS_PER_LONG == 0.
|
|
*/
|
|
#ifdef __BIG_ENDIAN
|
|
void bitmap_copy_le(unsigned long *dst, const unsigned long *src, unsigned int nbits)
|
|
{
|
|
unsigned int i;
|
|
|
|
for (i = 0; i < nbits/BITS_PER_LONG; i++) {
|
|
if (BITS_PER_LONG == 64)
|
|
dst[i] = cpu_to_le64(src[i]);
|
|
else
|
|
dst[i] = cpu_to_le32(src[i]);
|
|
}
|
|
}
|
|
EXPORT_SYMBOL(bitmap_copy_le);
|
|
#endif
|
|
|
|
unsigned long *bitmap_alloc(unsigned int nbits, gfp_t flags)
|
|
{
|
|
return kmalloc_array(BITS_TO_LONGS(nbits), sizeof(unsigned long),
|
|
flags);
|
|
}
|
|
EXPORT_SYMBOL(bitmap_alloc);
|
|
|
|
unsigned long *bitmap_zalloc(unsigned int nbits, gfp_t flags)
|
|
{
|
|
return bitmap_alloc(nbits, flags | __GFP_ZERO);
|
|
}
|
|
EXPORT_SYMBOL(bitmap_zalloc);
|
|
|
|
void bitmap_free(const unsigned long *bitmap)
|
|
{
|
|
kfree(bitmap);
|
|
}
|
|
EXPORT_SYMBOL(bitmap_free);
|