bitmap_print_bitmask_to_buf(9)
π NAME π
bitmap_print_bitmask_to_buf β convert bitmap to hex bitmask format ASCII string β¨
okay bestie, welcome to the bitmap_print_bitmask_to_buf(9) page from the Kernel API Manual π
this one is giving low-level bitmap-to-string realness and we are going to get through it together π«Ά
πΈ SYNOPSIS πΈ
int bitmap_print_bitmask_to_buf (char *buf , const unsigned long *maskp
, int nmaskbits , loff_t off , size_t count );
π¦ ARGUMENTS π¦
bufβ buffer into which string is placed πmaskpβ pointer to bitmap to convertnmaskbitsβ size of bitmap, in bitsoffβ in the string from which we are copying, We copy tobufcountβ the maximum number of bytes to print
β¨ DESCRIPTION β¨
Okay so hereβs the tea β: the sprintf("*pb[l]") format is used by drivers to export a hexadecimal bitmask and a decimal list to userspace by the sysfs ABI. Drivers might be using a normal attribute for this kind of ABIs. A normal attribute typically has a show entry like this:
static ssize_t example_attribute_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
...
return scnprintf(buf, PAGE_SIZE - offset_in_page(buf), nr_trig_max, mask);
}
So the show entry of an attribute has no offset and count parameters, and that means the file is limited to one page only. Not iconic. π
The problem is: once we have a large bitmap, we have a chance to get a bitmask or list thatβs more than one page. Especially for a list, it could be as complex as 0,3,5,7,9,... β and we have no simple way to know its exact size. It turns out bin_attribute is a way to break this limit. bin_attribute has a show entry like this:
static ssize_t
example_bin_attribute_show(struct file *filp, struct kobject *kobj,
struct bin_attribute *attr, char *buf,
loff_t offset, size_t count)
{
...
}
With the new offset and count parameters, this makes the sysfs ABI able to support file size more than one page. For example, offset could be >= 4096. bitmap_print_bitmask_to_buf and bitmap_print_list_to_buf β with their cpumap wrappers cpumap_print_bitmask_to_buf and cpumap_print_list_to_buf β make those drivers able to support large bitmask and list after they move to use bin_attribute. In result, we have to pass the corresponding parameters such as off and count from the bin_attribute show entry to this API. Slay. π
The role of cpumap_print_bitmask_to_buf and cpumap_print_list_to_buf is similar to direct sysfs_emit("*pb[l]") formatting, but the latter assumes the destination buffer is exactly one page and wonβt be more than one page. cpumap_print_bitmask_to_buf and cpumap_print_list_to_buf, on the other hand, mainly serve bin_attribute, which doesnβt work with exactly one page, and it can break the size limit of the converted decimal list and hexadecimal bitmask. Obsessed with breaking limits honestly π¦
π¨ WARNING! π¨
This function is not a replacement for sprintf. No cap. It is intended to workaround sysfs limitations discussed above and should be used carefully in the general case for the following reasons:
- Time complexity is
O(nbits^2/count), comparing toO(nbits)forsnprintf. π - Memory complexity is
O(nbits), comparing toO(1)forsnprintf. π offandcountare NOT offset and number of bits to print. Read that again bestie π- If printing part of bitmap as list, the resulting string is not a correct list representation of bitmap. Particularly, some bits within or out of related interval may be erroneously set or unset. The format of the string may be broken, so a
bitmap_parselist-like parser may fail parsing it. π¬ - If printing the whole bitmap as list by parts, user must ensure the order of calls of the function such that the offset is incremented linearly. π
- If printing the whole bitmap as list by parts, user must keep bitmap unchanged between the very first and very last call. Otherwise concatenated result may be incorrect, and format may be broken. π«
Returns the number of characters actually printed to buf β¨
π SEE ALSO π
Kernel file ./lib/bitmap-str.c and these whole besties:
bitmap_parse_user(9)bitmap_print_to_buf(9)bitmap_print_list_to_buf(9)bitmap_parselist(9)bitmap_parselist_user(9)bitmap_parse(9)
source/version: ./lib; date: October 2026; page: bitmap_print_bitmask_to_buf(9) β byeee bestie π«Άπ
π¦ where this page comes from
- nixpkgs unstable: linux-manual 7.3.1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms