xref: /openbmc/linux/net/ethtool/bitset.c (revision 022dacdd)
1 // SPDX-License-Identifier: GPL-2.0-only
2 
3 #include <linux/ethtool_netlink.h>
4 #include <linux/bitmap.h>
5 #include "netlink.h"
6 #include "bitset.h"
7 
8 /* Some bitmaps are internally represented as an array of unsigned long, some
9  * as an array of u32 (some even as single u32 for now). To avoid the need of
10  * wrappers on caller side, we provide two set of functions: those with "32"
11  * suffix in their names expect u32 based bitmaps, those without it expect
12  * unsigned long bitmaps.
13  */
14 
15 static u32 ethnl_lower_bits(unsigned int n)
16 {
17 	return ~(u32)0 >> (32 - n % 32);
18 }
19 
20 static u32 ethnl_upper_bits(unsigned int n)
21 {
22 	return ~(u32)0 << (n % 32);
23 }
24 
25 /**
26  * ethnl_bitmap32_clear() - Clear u32 based bitmap
27  * @dst:   bitmap to clear
28  * @start: beginning of the interval
29  * @end:   end of the interval
30  * @mod:   set if bitmap was modified
31  *
32  * Clear @nbits bits of a bitmap with indices @start <= i < @end
33  */
34 static void ethnl_bitmap32_clear(u32 *dst, unsigned int start, unsigned int end,
35 				 bool *mod)
36 {
37 	unsigned int start_word = start / 32;
38 	unsigned int end_word = end / 32;
39 	unsigned int i;
40 	u32 mask;
41 
42 	if (end <= start)
43 		return;
44 
45 	if (start % 32) {
46 		mask = ethnl_upper_bits(start);
47 		if (end_word == start_word) {
48 			mask &= ethnl_lower_bits(end);
49 			if (dst[start_word] & mask) {
50 				dst[start_word] &= ~mask;
51 				*mod = true;
52 			}
53 			return;
54 		}
55 		if (dst[start_word] & mask) {
56 			dst[start_word] &= ~mask;
57 			*mod = true;
58 		}
59 		start_word++;
60 	}
61 
62 	for (i = start_word; i < end_word; i++) {
63 		if (dst[i]) {
64 			dst[i] = 0;
65 			*mod = true;
66 		}
67 	}
68 	if (end % 32) {
69 		mask = ethnl_lower_bits(end);
70 		if (dst[end_word] & mask) {
71 			dst[end_word] &= ~mask;
72 			*mod = true;
73 		}
74 	}
75 }
76 
77 /**
78  * ethnl_bitmap32_not_zero() - Check if any bit is set in an interval
79  * @map:   bitmap to test
80  * @start: beginning of the interval
81  * @end:   end of the interval
82  *
83  * Return: true if there is non-zero bit with  index @start <= i < @end,
84  *         false if the whole interval is zero
85  */
86 static bool ethnl_bitmap32_not_zero(const u32 *map, unsigned int start,
87 				    unsigned int end)
88 {
89 	unsigned int start_word = start / 32;
90 	unsigned int end_word = end / 32;
91 	u32 mask;
92 
93 	if (end <= start)
94 		return true;
95 
96 	if (start % 32) {
97 		mask = ethnl_upper_bits(start);
98 		if (end_word == start_word) {
99 			mask &= ethnl_lower_bits(end);
100 			return map[start_word] & mask;
101 		}
102 		if (map[start_word] & mask)
103 			return true;
104 		start_word++;
105 	}
106 
107 	if (!memchr_inv(map + start_word, '\0',
108 			(end_word - start_word) * sizeof(u32)))
109 		return true;
110 	if (end % 32 == 0)
111 		return true;
112 	return map[end_word] & ethnl_lower_bits(end);
113 }
114 
115 /**
116  * ethnl_bitmap32_update() - Modify u32 based bitmap according to value/mask
117  *			     pair
118  * @dst:   bitmap to update
119  * @nbits: bit size of the bitmap
120  * @value: values to set
121  * @mask:  mask of bits to set
122  * @mod:   set to true if bitmap is modified, preserve if not
123  *
124  * Set bits in @dst bitmap which are set in @mask to values from @value, leave
125  * the rest untouched. If destination bitmap was modified, set @mod to true,
126  * leave as it is if not.
127  */
128 static void ethnl_bitmap32_update(u32 *dst, unsigned int nbits,
129 				  const u32 *value, const u32 *mask, bool *mod)
130 {
131 	while (nbits > 0) {
132 		u32 real_mask = mask ? *mask : ~(u32)0;
133 		u32 new_value;
134 
135 		if (nbits < 32)
136 			real_mask &= ethnl_lower_bits(nbits);
137 		new_value = (*dst & ~real_mask) | (*value & real_mask);
138 		if (new_value != *dst) {
139 			*dst = new_value;
140 			*mod = true;
141 		}
142 
143 		if (nbits <= 32)
144 			break;
145 		dst++;
146 		nbits -= 32;
147 		value++;
148 		if (mask)
149 			mask++;
150 	}
151 }
152 
153 static bool ethnl_bitmap32_test_bit(const u32 *map, unsigned int index)
154 {
155 	return map[index / 32] & (1U << (index % 32));
156 }
157 
158 /**
159  * ethnl_bitset32_size() - Calculate size of bitset nested attribute
160  * @val:     value bitmap (u32 based)
161  * @mask:    mask bitmap (u32 based, optional)
162  * @nbits:   bit length of the bitset
163  * @names:   array of bit names (optional)
164  * @compact: assume compact format for output
165  *
166  * Estimate length of netlink attribute composed by a later call to
167  * ethnl_put_bitset32() call with the same arguments.
168  *
169  * Return: negative error code or attribute length estimate
170  */
171 int ethnl_bitset32_size(const u32 *val, const u32 *mask, unsigned int nbits,
172 			ethnl_string_array_t names, bool compact)
173 {
174 	unsigned int len = 0;
175 
176 	/* list flag */
177 	if (!mask)
178 		len += nla_total_size(sizeof(u32));
179 	/* size */
180 	len += nla_total_size(sizeof(u32));
181 
182 	if (compact) {
183 		unsigned int nwords = DIV_ROUND_UP(nbits, 32);
184 
185 		/* value, mask */
186 		len += (mask ? 2 : 1) * nla_total_size(nwords * sizeof(u32));
187 	} else {
188 		unsigned int bits_len = 0;
189 		unsigned int bit_len, i;
190 
191 		for (i = 0; i < nbits; i++) {
192 			const char *name = names ? names[i] : NULL;
193 
194 			if (!ethnl_bitmap32_test_bit(mask ?: val, i))
195 				continue;
196 			/* index */
197 			bit_len = nla_total_size(sizeof(u32));
198 			/* name */
199 			if (name)
200 				bit_len += ethnl_strz_size(name);
201 			/* value */
202 			if (mask && ethnl_bitmap32_test_bit(val, i))
203 				bit_len += nla_total_size(0);
204 
205 			/* bit nest */
206 			bits_len += nla_total_size(bit_len);
207 		}
208 		/* bits nest */
209 		len += nla_total_size(bits_len);
210 	}
211 
212 	/* outermost nest */
213 	return nla_total_size(len);
214 }
215 
216 /**
217  * ethnl_put_bitset32() - Put a bitset nest into a message
218  * @skb:      skb with the message
219  * @attrtype: attribute type for the bitset nest
220  * @val:      value bitmap (u32 based)
221  * @mask:     mask bitmap (u32 based, optional)
222  * @nbits:    bit length of the bitset
223  * @names:    array of bit names (optional)
224  * @compact:  use compact format for the output
225  *
226  * Compose a nested attribute representing a bitset. If @mask is null, simple
227  * bitmap (bit list) is created, if @mask is provided, represent a value/mask
228  * pair. Bit names are only used in verbose mode and when provided by calller.
229  *
230  * Return: 0 on success, negative error value on error
231  */
232 int ethnl_put_bitset32(struct sk_buff *skb, int attrtype, const u32 *val,
233 		       const u32 *mask, unsigned int nbits,
234 		       ethnl_string_array_t names, bool compact)
235 {
236 	struct nlattr *nest;
237 	struct nlattr *attr;
238 
239 	nest = nla_nest_start(skb, attrtype);
240 	if (!nest)
241 		return -EMSGSIZE;
242 
243 	if (!mask && nla_put_flag(skb, ETHTOOL_A_BITSET_NOMASK))
244 		goto nla_put_failure;
245 	if (nla_put_u32(skb, ETHTOOL_A_BITSET_SIZE, nbits))
246 		goto nla_put_failure;
247 	if (compact) {
248 		unsigned int nwords = DIV_ROUND_UP(nbits, 32);
249 		unsigned int nbytes = nwords * sizeof(u32);
250 		u32 *dst;
251 
252 		attr = nla_reserve(skb, ETHTOOL_A_BITSET_VALUE, nbytes);
253 		if (!attr)
254 			goto nla_put_failure;
255 		dst = nla_data(attr);
256 		memcpy(dst, val, nbytes);
257 		if (nbits % 32)
258 			dst[nwords - 1] &= ethnl_lower_bits(nbits);
259 
260 		if (mask) {
261 			attr = nla_reserve(skb, ETHTOOL_A_BITSET_MASK, nbytes);
262 			if (!attr)
263 				goto nla_put_failure;
264 			dst = nla_data(attr);
265 			memcpy(dst, mask, nbytes);
266 			if (nbits % 32)
267 				dst[nwords - 1] &= ethnl_lower_bits(nbits);
268 		}
269 	} else {
270 		struct nlattr *bits;
271 		unsigned int i;
272 
273 		bits = nla_nest_start(skb, ETHTOOL_A_BITSET_BITS);
274 		if (!bits)
275 			goto nla_put_failure;
276 		for (i = 0; i < nbits; i++) {
277 			const char *name = names ? names[i] : NULL;
278 
279 			if (!ethnl_bitmap32_test_bit(mask ?: val, i))
280 				continue;
281 			attr = nla_nest_start(skb, ETHTOOL_A_BITSET_BITS_BIT);
282 			if (!attr)
283 				goto nla_put_failure;
284 			if (nla_put_u32(skb, ETHTOOL_A_BITSET_BIT_INDEX, i))
285 				goto nla_put_failure;
286 			if (name &&
287 			    ethnl_put_strz(skb, ETHTOOL_A_BITSET_BIT_NAME, name))
288 				goto nla_put_failure;
289 			if (mask && ethnl_bitmap32_test_bit(val, i) &&
290 			    nla_put_flag(skb, ETHTOOL_A_BITSET_BIT_VALUE))
291 				goto nla_put_failure;
292 			nla_nest_end(skb, attr);
293 		}
294 		nla_nest_end(skb, bits);
295 	}
296 
297 	nla_nest_end(skb, nest);
298 	return 0;
299 
300 nla_put_failure:
301 	nla_nest_cancel(skb, nest);
302 	return -EMSGSIZE;
303 }
304 
305 static const struct nla_policy bitset_policy[ETHTOOL_A_BITSET_MAX + 1] = {
306 	[ETHTOOL_A_BITSET_UNSPEC]	= { .type = NLA_REJECT },
307 	[ETHTOOL_A_BITSET_NOMASK]	= { .type = NLA_FLAG },
308 	[ETHTOOL_A_BITSET_SIZE]		= NLA_POLICY_MAX(NLA_U32,
309 							 ETHNL_MAX_BITSET_SIZE),
310 	[ETHTOOL_A_BITSET_BITS]		= { .type = NLA_NESTED },
311 	[ETHTOOL_A_BITSET_VALUE]	= { .type = NLA_BINARY },
312 	[ETHTOOL_A_BITSET_MASK]		= { .type = NLA_BINARY },
313 };
314 
315 static const struct nla_policy bit_policy[ETHTOOL_A_BITSET_BIT_MAX + 1] = {
316 	[ETHTOOL_A_BITSET_BIT_UNSPEC]	= { .type = NLA_REJECT },
317 	[ETHTOOL_A_BITSET_BIT_INDEX]	= { .type = NLA_U32 },
318 	[ETHTOOL_A_BITSET_BIT_NAME]	= { .type = NLA_NUL_STRING },
319 	[ETHTOOL_A_BITSET_BIT_VALUE]	= { .type = NLA_FLAG },
320 };
321 
322 /**
323  * ethnl_bitset_is_compact() - check if bitset attribute represents a compact
324  *			       bitset
325  * @bitset:  nested attribute representing a bitset
326  * @compact: pointer for return value
327  *
328  * Return: 0 on success, negative error code on failure
329  */
330 int ethnl_bitset_is_compact(const struct nlattr *bitset, bool *compact)
331 {
332 	struct nlattr *tb[ETHTOOL_A_BITSET_MAX + 1];
333 	int ret;
334 
335 	ret = nla_parse_nested(tb, ETHTOOL_A_BITSET_MAX, bitset,
336 			       bitset_policy, NULL);
337 	if (ret < 0)
338 		return ret;
339 
340 	if (tb[ETHTOOL_A_BITSET_BITS]) {
341 		if (tb[ETHTOOL_A_BITSET_VALUE] || tb[ETHTOOL_A_BITSET_MASK])
342 			return -EINVAL;
343 		*compact = false;
344 		return 0;
345 	}
346 	if (!tb[ETHTOOL_A_BITSET_SIZE] || !tb[ETHTOOL_A_BITSET_VALUE])
347 		return -EINVAL;
348 
349 	*compact = true;
350 	return 0;
351 }
352 
353 /**
354  * ethnl_name_to_idx() - look up string index for a name
355  * @names:   array of ETH_GSTRING_LEN sized strings
356  * @n_names: number of strings in the array
357  * @name:    name to look up
358  *
359  * Return: index of the string if found, -ENOENT if not found
360  */
361 static int ethnl_name_to_idx(ethnl_string_array_t names, unsigned int n_names,
362 			     const char *name)
363 {
364 	unsigned int i;
365 
366 	if (!names)
367 		return -ENOENT;
368 
369 	for (i = 0; i < n_names; i++) {
370 		/* names[i] may not be null terminated */
371 		if (!strncmp(names[i], name, ETH_GSTRING_LEN) &&
372 		    strlen(name) <= ETH_GSTRING_LEN)
373 			return i;
374 	}
375 
376 	return -ENOENT;
377 }
378 
379 static int ethnl_parse_bit(unsigned int *index, bool *val, unsigned int nbits,
380 			   const struct nlattr *bit_attr, bool no_mask,
381 			   ethnl_string_array_t names,
382 			   struct netlink_ext_ack *extack)
383 {
384 	struct nlattr *tb[ETHTOOL_A_BITSET_BIT_MAX + 1];
385 	int ret, idx;
386 
387 	ret = nla_parse_nested(tb, ETHTOOL_A_BITSET_BIT_MAX, bit_attr,
388 			       bit_policy, extack);
389 	if (ret < 0)
390 		return ret;
391 
392 	if (tb[ETHTOOL_A_BITSET_BIT_INDEX]) {
393 		const char *name;
394 
395 		idx = nla_get_u32(tb[ETHTOOL_A_BITSET_BIT_INDEX]);
396 		if (idx >= nbits) {
397 			NL_SET_ERR_MSG_ATTR(extack,
398 					    tb[ETHTOOL_A_BITSET_BIT_INDEX],
399 					    "bit index too high");
400 			return -EOPNOTSUPP;
401 		}
402 		name = names ? names[idx] : NULL;
403 		if (tb[ETHTOOL_A_BITSET_BIT_NAME] && name &&
404 		    strncmp(nla_data(tb[ETHTOOL_A_BITSET_BIT_NAME]), name,
405 			    nla_len(tb[ETHTOOL_A_BITSET_BIT_NAME]))) {
406 			NL_SET_ERR_MSG_ATTR(extack, bit_attr,
407 					    "bit index and name mismatch");
408 			return -EINVAL;
409 		}
410 	} else if (tb[ETHTOOL_A_BITSET_BIT_NAME]) {
411 		idx = ethnl_name_to_idx(names, nbits,
412 					nla_data(tb[ETHTOOL_A_BITSET_BIT_NAME]));
413 		if (idx < 0) {
414 			NL_SET_ERR_MSG_ATTR(extack,
415 					    tb[ETHTOOL_A_BITSET_BIT_NAME],
416 					    "bit name not found");
417 			return -EOPNOTSUPP;
418 		}
419 	} else {
420 		NL_SET_ERR_MSG_ATTR(extack, bit_attr,
421 				    "neither bit index nor name specified");
422 		return -EINVAL;
423 	}
424 
425 	*index = idx;
426 	*val = no_mask || tb[ETHTOOL_A_BITSET_BIT_VALUE];
427 	return 0;
428 }
429 
430 static int
431 ethnl_update_bitset32_verbose(u32 *bitmap, unsigned int nbits,
432 			      const struct nlattr *attr, struct nlattr **tb,
433 			      ethnl_string_array_t names,
434 			      struct netlink_ext_ack *extack, bool *mod)
435 {
436 	struct nlattr *bit_attr;
437 	bool no_mask;
438 	int rem;
439 	int ret;
440 
441 	if (tb[ETHTOOL_A_BITSET_VALUE]) {
442 		NL_SET_ERR_MSG_ATTR(extack, tb[ETHTOOL_A_BITSET_VALUE],
443 				    "value only allowed in compact bitset");
444 		return -EINVAL;
445 	}
446 	if (tb[ETHTOOL_A_BITSET_MASK]) {
447 		NL_SET_ERR_MSG_ATTR(extack, tb[ETHTOOL_A_BITSET_MASK],
448 				    "mask only allowed in compact bitset");
449 		return -EINVAL;
450 	}
451 
452 	no_mask = tb[ETHTOOL_A_BITSET_NOMASK];
453 	if (no_mask)
454 		ethnl_bitmap32_clear(bitmap, 0, nbits, mod);
455 
456 	nla_for_each_nested(bit_attr, tb[ETHTOOL_A_BITSET_BITS], rem) {
457 		bool old_val, new_val;
458 		unsigned int idx;
459 
460 		if (nla_type(bit_attr) != ETHTOOL_A_BITSET_BITS_BIT) {
461 			NL_SET_ERR_MSG_ATTR(extack, bit_attr,
462 					    "only ETHTOOL_A_BITSET_BITS_BIT allowed in ETHTOOL_A_BITSET_BITS");
463 			return -EINVAL;
464 		}
465 		ret = ethnl_parse_bit(&idx, &new_val, nbits, bit_attr, no_mask,
466 				      names, extack);
467 		if (ret < 0)
468 			return ret;
469 		old_val = bitmap[idx / 32] & ((u32)1 << (idx % 32));
470 		if (new_val != old_val) {
471 			if (new_val)
472 				bitmap[idx / 32] |= ((u32)1 << (idx % 32));
473 			else
474 				bitmap[idx / 32] &= ~((u32)1 << (idx % 32));
475 			*mod = true;
476 		}
477 	}
478 
479 	return 0;
480 }
481 
482 static int ethnl_compact_sanity_checks(unsigned int nbits,
483 				       const struct nlattr *nest,
484 				       struct nlattr **tb,
485 				       struct netlink_ext_ack *extack)
486 {
487 	bool no_mask = tb[ETHTOOL_A_BITSET_NOMASK];
488 	unsigned int attr_nbits, attr_nwords;
489 	const struct nlattr *test_attr;
490 
491 	if (no_mask && tb[ETHTOOL_A_BITSET_MASK]) {
492 		NL_SET_ERR_MSG_ATTR(extack, tb[ETHTOOL_A_BITSET_MASK],
493 				    "mask not allowed in list bitset");
494 		return -EINVAL;
495 	}
496 	if (!tb[ETHTOOL_A_BITSET_SIZE]) {
497 		NL_SET_ERR_MSG_ATTR(extack, nest,
498 				    "missing size in compact bitset");
499 		return -EINVAL;
500 	}
501 	if (!tb[ETHTOOL_A_BITSET_VALUE]) {
502 		NL_SET_ERR_MSG_ATTR(extack, nest,
503 				    "missing value in compact bitset");
504 		return -EINVAL;
505 	}
506 	if (!no_mask && !tb[ETHTOOL_A_BITSET_MASK]) {
507 		NL_SET_ERR_MSG_ATTR(extack, nest,
508 				    "missing mask in compact nonlist bitset");
509 		return -EINVAL;
510 	}
511 
512 	attr_nbits = nla_get_u32(tb[ETHTOOL_A_BITSET_SIZE]);
513 	attr_nwords = DIV_ROUND_UP(attr_nbits, 32);
514 	if (nla_len(tb[ETHTOOL_A_BITSET_VALUE]) != attr_nwords * sizeof(u32)) {
515 		NL_SET_ERR_MSG_ATTR(extack, tb[ETHTOOL_A_BITSET_VALUE],
516 				    "bitset value length does not match size");
517 		return -EINVAL;
518 	}
519 	if (tb[ETHTOOL_A_BITSET_MASK] &&
520 	    nla_len(tb[ETHTOOL_A_BITSET_MASK]) != attr_nwords * sizeof(u32)) {
521 		NL_SET_ERR_MSG_ATTR(extack, tb[ETHTOOL_A_BITSET_MASK],
522 				    "bitset mask length does not match size");
523 		return -EINVAL;
524 	}
525 	if (attr_nbits <= nbits)
526 		return 0;
527 
528 	test_attr = no_mask ? tb[ETHTOOL_A_BITSET_VALUE] :
529 			      tb[ETHTOOL_A_BITSET_MASK];
530 	if (ethnl_bitmap32_not_zero(nla_data(test_attr), nbits, attr_nbits)) {
531 		NL_SET_ERR_MSG_ATTR(extack, test_attr,
532 				    "cannot modify bits past kernel bitset size");
533 		return -EINVAL;
534 	}
535 	return 0;
536 }
537 
538 /**
539  * ethnl_update_bitset32() - Apply a bitset nest to a u32 based bitmap
540  * @bitmap:  bitmap to update
541  * @nbits:   size of the updated bitmap in bits
542  * @attr:    nest attribute to parse and apply
543  * @names:   array of bit names; may be null for compact format
544  * @extack:  extack for error reporting
545  * @mod:     set this to true if bitmap is modified, leave as it is if not
546  *
547  * Apply bitset netsted attribute to a bitmap. If the attribute represents
548  * a bit list, @bitmap is set to its contents; otherwise, bits in mask are
549  * set to values from value. Bitmaps in the attribute may be longer than
550  * @nbits but the message must not request modifying any bits past @nbits.
551  *
552  * Return: negative error code on failure, 0 on success
553  */
554 int ethnl_update_bitset32(u32 *bitmap, unsigned int nbits,
555 			  const struct nlattr *attr, ethnl_string_array_t names,
556 			  struct netlink_ext_ack *extack, bool *mod)
557 {
558 	struct nlattr *tb[ETHTOOL_A_BITSET_MAX + 1];
559 	unsigned int change_bits;
560 	bool no_mask;
561 	int ret;
562 
563 	if (!attr)
564 		return 0;
565 	ret = nla_parse_nested(tb, ETHTOOL_A_BITSET_MAX, attr, bitset_policy,
566 			       extack);
567 	if (ret < 0)
568 		return ret;
569 
570 	if (tb[ETHTOOL_A_BITSET_BITS])
571 		return ethnl_update_bitset32_verbose(bitmap, nbits, attr, tb,
572 						     names, extack, mod);
573 	ret = ethnl_compact_sanity_checks(nbits, attr, tb, extack);
574 	if (ret < 0)
575 		return ret;
576 
577 	no_mask = tb[ETHTOOL_A_BITSET_NOMASK];
578 	change_bits = min_t(unsigned int,
579 			    nla_get_u32(tb[ETHTOOL_A_BITSET_SIZE]), nbits);
580 	ethnl_bitmap32_update(bitmap, change_bits,
581 			      nla_data(tb[ETHTOOL_A_BITSET_VALUE]),
582 			      no_mask ? NULL :
583 					nla_data(tb[ETHTOOL_A_BITSET_MASK]),
584 			      mod);
585 	if (no_mask && change_bits < nbits)
586 		ethnl_bitmap32_clear(bitmap, change_bits, nbits, mod);
587 
588 	return 0;
589 }
590 
591 /**
592  * ethnl_parse_bitset() - Compute effective value and mask from bitset nest
593  * @val:     unsigned long based bitmap to put value into
594  * @mask:    unsigned long based bitmap to put mask into
595  * @nbits:   size of @val and @mask bitmaps
596  * @attr:    nest attribute to parse and apply
597  * @names:   array of bit names; may be null for compact format
598  * @extack:  extack for error reporting
599  *
600  * Provide @nbits size long bitmaps for value and mask so that
601  * x = (val & mask) | (x & ~mask) would modify any @nbits sized bitmap x
602  * the same way ethnl_update_bitset() with the same bitset attribute would.
603  *
604  * Return:   negative error code on failure, 0 on success
605  */
606 int ethnl_parse_bitset(unsigned long *val, unsigned long *mask,
607 		       unsigned int nbits, const struct nlattr *attr,
608 		       ethnl_string_array_t names,
609 		       struct netlink_ext_ack *extack)
610 {
611 	struct nlattr *tb[ETHTOOL_A_BITSET_MAX + 1];
612 	const struct nlattr *bit_attr;
613 	bool no_mask;
614 	int rem;
615 	int ret;
616 
617 	if (!attr)
618 		return 0;
619 	ret = nla_parse_nested(tb, ETHTOOL_A_BITSET_MAX, attr, bitset_policy,
620 			       extack);
621 	if (ret < 0)
622 		return ret;
623 	no_mask = tb[ETHTOOL_A_BITSET_NOMASK];
624 
625 	if (!tb[ETHTOOL_A_BITSET_BITS]) {
626 		unsigned int change_bits;
627 
628 		ret = ethnl_compact_sanity_checks(nbits, attr, tb, extack);
629 		if (ret < 0)
630 			return ret;
631 
632 		change_bits = nla_get_u32(tb[ETHTOOL_A_BITSET_SIZE]);
633 		bitmap_from_arr32(val, nla_data(tb[ETHTOOL_A_BITSET_VALUE]),
634 				  change_bits);
635 		if (change_bits < nbits)
636 			bitmap_clear(val, change_bits, nbits - change_bits);
637 		if (no_mask) {
638 			bitmap_fill(mask, nbits);
639 		} else {
640 			bitmap_from_arr32(mask,
641 					  nla_data(tb[ETHTOOL_A_BITSET_MASK]),
642 					  change_bits);
643 			if (change_bits < nbits)
644 				bitmap_clear(mask, change_bits,
645 					     nbits - change_bits);
646 		}
647 
648 		return 0;
649 	}
650 
651 	if (tb[ETHTOOL_A_BITSET_VALUE]) {
652 		NL_SET_ERR_MSG_ATTR(extack, tb[ETHTOOL_A_BITSET_VALUE],
653 				    "value only allowed in compact bitset");
654 		return -EINVAL;
655 	}
656 	if (tb[ETHTOOL_A_BITSET_MASK]) {
657 		NL_SET_ERR_MSG_ATTR(extack, tb[ETHTOOL_A_BITSET_MASK],
658 				    "mask only allowed in compact bitset");
659 		return -EINVAL;
660 	}
661 
662 	bitmap_zero(val, nbits);
663 	if (no_mask)
664 		bitmap_fill(mask, nbits);
665 	else
666 		bitmap_zero(mask, nbits);
667 
668 	nla_for_each_nested(bit_attr, tb[ETHTOOL_A_BITSET_BITS], rem) {
669 		unsigned int idx;
670 		bool bit_val;
671 
672 		ret = ethnl_parse_bit(&idx, &bit_val, nbits, bit_attr, no_mask,
673 				      names, extack);
674 		if (ret < 0)
675 			return ret;
676 		if (bit_val)
677 			__set_bit(idx, val);
678 		if (!no_mask)
679 			__set_bit(idx, mask);
680 	}
681 
682 	return 0;
683 }
684 
685 #if BITS_PER_LONG == 64 && defined(__BIG_ENDIAN)
686 
687 /* 64-bit big endian architectures are the only case when u32 based bitmaps
688  * and unsigned long based bitmaps have different memory layout so that we
689  * cannot simply cast the latter to the former and need actual wrappers
690  * converting the latter to the former.
691  *
692  * To reduce the number of slab allocations, the wrappers use fixed size local
693  * variables for bitmaps up to ETHNL_SMALL_BITMAP_BITS bits which is the
694  * majority of bitmaps used by ethtool.
695  */
696 #define ETHNL_SMALL_BITMAP_BITS 128
697 #define ETHNL_SMALL_BITMAP_WORDS DIV_ROUND_UP(ETHNL_SMALL_BITMAP_BITS, 32)
698 
699 int ethnl_bitset_size(const unsigned long *val, const unsigned long *mask,
700 		      unsigned int nbits, ethnl_string_array_t names,
701 		      bool compact)
702 {
703 	u32 small_mask32[ETHNL_SMALL_BITMAP_WORDS];
704 	u32 small_val32[ETHNL_SMALL_BITMAP_WORDS];
705 	u32 *mask32;
706 	u32 *val32;
707 	int ret;
708 
709 	if (nbits > ETHNL_SMALL_BITMAP_BITS) {
710 		unsigned int nwords = DIV_ROUND_UP(nbits, 32);
711 
712 		val32 = kmalloc_array(2 * nwords, sizeof(u32), GFP_KERNEL);
713 		if (!val32)
714 			return -ENOMEM;
715 		mask32 = val32 + nwords;
716 	} else {
717 		val32 = small_val32;
718 		mask32 = small_mask32;
719 	}
720 
721 	bitmap_to_arr32(val32, val, nbits);
722 	if (mask)
723 		bitmap_to_arr32(mask32, mask, nbits);
724 	else
725 		mask32 = NULL;
726 	ret = ethnl_bitset32_size(val32, mask32, nbits, names, compact);
727 
728 	if (nbits > ETHNL_SMALL_BITMAP_BITS)
729 		kfree(val32);
730 
731 	return ret;
732 }
733 
734 int ethnl_put_bitset(struct sk_buff *skb, int attrtype,
735 		     const unsigned long *val, const unsigned long *mask,
736 		     unsigned int nbits, ethnl_string_array_t names,
737 		     bool compact)
738 {
739 	u32 small_mask32[ETHNL_SMALL_BITMAP_WORDS];
740 	u32 small_val32[ETHNL_SMALL_BITMAP_WORDS];
741 	u32 *mask32;
742 	u32 *val32;
743 	int ret;
744 
745 	if (nbits > ETHNL_SMALL_BITMAP_BITS) {
746 		unsigned int nwords = DIV_ROUND_UP(nbits, 32);
747 
748 		val32 = kmalloc_array(2 * nwords, sizeof(u32), GFP_KERNEL);
749 		if (!val32)
750 			return -ENOMEM;
751 		mask32 = val32 + nwords;
752 	} else {
753 		val32 = small_val32;
754 		mask32 = small_mask32;
755 	}
756 
757 	bitmap_to_arr32(val32, val, nbits);
758 	if (mask)
759 		bitmap_to_arr32(mask32, mask, nbits);
760 	else
761 		mask32 = NULL;
762 	ret = ethnl_put_bitset32(skb, attrtype, val32, mask32, nbits, names,
763 				 compact);
764 
765 	if (nbits > ETHNL_SMALL_BITMAP_BITS)
766 		kfree(val32);
767 
768 	return ret;
769 }
770 
771 int ethnl_update_bitset(unsigned long *bitmap, unsigned int nbits,
772 			const struct nlattr *attr, ethnl_string_array_t names,
773 			struct netlink_ext_ack *extack, bool *mod)
774 {
775 	u32 small_bitmap32[ETHNL_SMALL_BITMAP_WORDS];
776 	u32 *bitmap32 = small_bitmap32;
777 	bool u32_mod = false;
778 	int ret;
779 
780 	if (nbits > ETHNL_SMALL_BITMAP_BITS) {
781 		unsigned int dst_words = DIV_ROUND_UP(nbits, 32);
782 
783 		bitmap32 = kmalloc_array(dst_words, sizeof(u32), GFP_KERNEL);
784 		if (!bitmap32)
785 			return -ENOMEM;
786 	}
787 
788 	bitmap_to_arr32(bitmap32, bitmap, nbits);
789 	ret = ethnl_update_bitset32(bitmap32, nbits, attr, names, extack,
790 				    &u32_mod);
791 	if (u32_mod) {
792 		bitmap_from_arr32(bitmap, bitmap32, nbits);
793 		*mod = true;
794 	}
795 
796 	if (nbits > ETHNL_SMALL_BITMAP_BITS)
797 		kfree(bitmap32);
798 
799 	return ret;
800 }
801 
802 #else
803 
804 /* On little endian 64-bit and all 32-bit architectures, an unsigned long
805  * based bitmap can be interpreted as u32 based one using a simple cast.
806  */
807 
808 int ethnl_bitset_size(const unsigned long *val, const unsigned long *mask,
809 		      unsigned int nbits, ethnl_string_array_t names,
810 		      bool compact)
811 {
812 	return ethnl_bitset32_size((const u32 *)val, (const u32 *)mask, nbits,
813 				   names, compact);
814 }
815 
816 int ethnl_put_bitset(struct sk_buff *skb, int attrtype,
817 		     const unsigned long *val, const unsigned long *mask,
818 		     unsigned int nbits, ethnl_string_array_t names,
819 		     bool compact)
820 {
821 	return ethnl_put_bitset32(skb, attrtype, (const u32 *)val,
822 				  (const u32 *)mask, nbits, names, compact);
823 }
824 
825 int ethnl_update_bitset(unsigned long *bitmap, unsigned int nbits,
826 			const struct nlattr *attr, ethnl_string_array_t names,
827 			struct netlink_ext_ack *extack, bool *mod)
828 {
829 	return ethnl_update_bitset32((u32 *)bitmap, nbits, attr, names, extack,
830 				     mod);
831 }
832 
833 #endif /* BITS_PER_LONG == 64 && defined(__BIG_ENDIAN) */
834