Skip to content

Commit af352c3

Browse files
committed
Merge branch 'compiler_types-add-endianness-dependent-__counted_by_-le-be'
Alexander Lobakin says: ==================== compiler_types: add Endianness-dependent __counted_by_{le,be} Some structures contain flexible arrays at the end and the counter for them, but the counter has explicit Endianness and thus __counted_by() can't be used directly. To increase test coverage for potential problems without breaking anything, introduce __counted_by_{le,be} defined depending on platform's Endianness to either __counted_by() when applicable or noop otherwise. The first user will be virtchnl2.h from idpf just as example with 9 flex structures having Little Endian counters. Maybe it would be a good idea to introduce such attributes on compiler level if possible, but for now let's stop on what we have. ==================== Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
2 parents 6e9b019 + 93d24ac commit af352c3

File tree

5 files changed

+28
-12
lines changed

5 files changed

+28
-12
lines changed

Documentation/conf.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ def have_command(cmd):
7575
"__rcu",
7676
"__user",
7777
"__force",
78+
"__counted_by_le",
79+
"__counted_by_be",
7880

7981
# include/linux/compiler_attributes.h:
8082
"__alias",

drivers/net/ethernet/intel/idpf/idpf_txrx.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#include <net/tcp.h>
99
#include <net/netdev_queues.h>
1010

11+
#include "virtchnl2_lan_desc.h"
12+
1113
#define IDPF_LARGE_MAX_Q 256
1214
#define IDPF_MAX_Q 16
1315
#define IDPF_MIN_Q 2

drivers/net/ethernet/intel/idpf/virtchnl2.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#ifndef _VIRTCHNL2_H_
55
#define _VIRTCHNL2_H_
66

7+
#include <linux/if_ether.h>
8+
79
/* All opcodes associated with virtchnl2 are prefixed with virtchnl2 or
810
* VIRTCHNL2. Any future opcodes, offloads/capabilities, structures,
911
* and defines must be prefixed with virtchnl2 or VIRTCHNL2 to avoid confusion.
@@ -17,8 +19,6 @@
1719
* must remain unchanged over time, so we specify explicit values for all enums.
1820
*/
1921

20-
#include "virtchnl2_lan_desc.h"
21-
2222
/* This macro is used to generate compilation errors if a structure
2323
* is not exactly the correct length.
2424
*/
@@ -555,7 +555,7 @@ VIRTCHNL2_CHECK_STRUCT_LEN(32, virtchnl2_queue_reg_chunk);
555555
struct virtchnl2_queue_reg_chunks {
556556
__le16 num_chunks;
557557
u8 pad[6];
558-
struct virtchnl2_queue_reg_chunk chunks[];
558+
struct virtchnl2_queue_reg_chunk chunks[] __counted_by_le(num_chunks);
559559
};
560560
VIRTCHNL2_CHECK_STRUCT_LEN(8, virtchnl2_queue_reg_chunks);
561561

@@ -703,7 +703,7 @@ struct virtchnl2_config_tx_queues {
703703
__le32 vport_id;
704704
__le16 num_qinfo;
705705
u8 pad[10];
706-
struct virtchnl2_txq_info qinfo[];
706+
struct virtchnl2_txq_info qinfo[] __counted_by_le(num_qinfo);
707707
};
708708
VIRTCHNL2_CHECK_STRUCT_LEN(16, virtchnl2_config_tx_queues);
709709

@@ -782,7 +782,7 @@ struct virtchnl2_config_rx_queues {
782782
__le32 vport_id;
783783
__le16 num_qinfo;
784784
u8 pad[18];
785-
struct virtchnl2_rxq_info qinfo[];
785+
struct virtchnl2_rxq_info qinfo[] __counted_by_le(num_qinfo);
786786
};
787787
VIRTCHNL2_CHECK_STRUCT_LEN(24, virtchnl2_config_rx_queues);
788788

@@ -868,7 +868,7 @@ VIRTCHNL2_CHECK_STRUCT_LEN(32, virtchnl2_vector_chunk);
868868
struct virtchnl2_vector_chunks {
869869
__le16 num_vchunks;
870870
u8 pad[14];
871-
struct virtchnl2_vector_chunk vchunks[];
871+
struct virtchnl2_vector_chunk vchunks[] __counted_by_le(num_vchunks);
872872
};
873873
VIRTCHNL2_CHECK_STRUCT_LEN(16, virtchnl2_vector_chunks);
874874

@@ -912,7 +912,7 @@ struct virtchnl2_rss_lut {
912912
__le16 lut_entries_start;
913913
__le16 lut_entries;
914914
u8 pad[4];
915-
__le32 lut[];
915+
__le32 lut[] __counted_by_le(lut_entries);
916916
};
917917
VIRTCHNL2_CHECK_STRUCT_LEN(12, virtchnl2_rss_lut);
918918

@@ -977,7 +977,7 @@ struct virtchnl2_ptype {
977977
u8 ptype_id_8;
978978
u8 proto_id_count;
979979
__le16 pad;
980-
__le16 proto_id[];
980+
__le16 proto_id[] __counted_by(proto_id_count);
981981
} __packed __aligned(2);
982982
VIRTCHNL2_CHECK_STRUCT_LEN(6, virtchnl2_ptype);
983983

@@ -1104,7 +1104,7 @@ struct virtchnl2_rss_key {
11041104
__le32 vport_id;
11051105
__le16 key_len;
11061106
u8 pad;
1107-
u8 key_flex[];
1107+
u8 key_flex[] __counted_by_le(key_len);
11081108
} __packed;
11091109
VIRTCHNL2_CHECK_STRUCT_LEN(7, virtchnl2_rss_key);
11101110

@@ -1131,7 +1131,7 @@ VIRTCHNL2_CHECK_STRUCT_LEN(16, virtchnl2_queue_chunk);
11311131
struct virtchnl2_queue_chunks {
11321132
__le16 num_chunks;
11331133
u8 pad[6];
1134-
struct virtchnl2_queue_chunk chunks[];
1134+
struct virtchnl2_queue_chunk chunks[] __counted_by_le(num_chunks);
11351135
};
11361136
VIRTCHNL2_CHECK_STRUCT_LEN(8, virtchnl2_queue_chunks);
11371137

@@ -1195,7 +1195,7 @@ struct virtchnl2_queue_vector_maps {
11951195
__le32 vport_id;
11961196
__le16 num_qv_maps;
11971197
u8 pad[10];
1198-
struct virtchnl2_queue_vector qv_maps[];
1198+
struct virtchnl2_queue_vector qv_maps[] __counted_by_le(num_qv_maps);
11991199
};
12001200
VIRTCHNL2_CHECK_STRUCT_LEN(16, virtchnl2_queue_vector_maps);
12011201

@@ -1247,7 +1247,7 @@ struct virtchnl2_mac_addr_list {
12471247
__le32 vport_id;
12481248
__le16 num_mac_addr;
12491249
u8 pad[2];
1250-
struct virtchnl2_mac_addr mac_addr_list[];
1250+
struct virtchnl2_mac_addr mac_addr_list[] __counted_by_le(num_mac_addr);
12511251
};
12521252
VIRTCHNL2_CHECK_STRUCT_LEN(8, virtchnl2_mac_addr_list);
12531253

include/linux/compiler_types.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,17 @@ struct ftrace_likely_data {
282282
#define __no_sanitize_or_inline __always_inline
283283
#endif
284284

285+
/*
286+
* Apply __counted_by() when the Endianness matches to increase test coverage.
287+
*/
288+
#ifdef __LITTLE_ENDIAN
289+
#define __counted_by_le(member) __counted_by(member)
290+
#define __counted_by_be(member)
291+
#else
292+
#define __counted_by_le(member)
293+
#define __counted_by_be(member) __counted_by(member)
294+
#endif
295+
285296
/* Do not trap wrapping arithmetic within an annotated function. */
286297
#ifdef CONFIG_UBSAN_SIGNED_WRAP
287298
# define __signed_wrap __attribute__((no_sanitize("signed-integer-overflow")))

scripts/kernel-doc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,6 +1143,7 @@ sub dump_struct($$) {
11431143
$members =~ s/\s*$attribute/ /gi;
11441144
$members =~ s/\s*__aligned\s*\([^;]*\)/ /gos;
11451145
$members =~ s/\s*__counted_by\s*\([^;]*\)/ /gos;
1146+
$members =~ s/\s*__counted_by_(le|be)\s*\([^;]*\)/ /gos;
11461147
$members =~ s/\s*__packed\s*/ /gos;
11471148
$members =~ s/\s*CRYPTO_MINALIGN_ATTR/ /gos;
11481149
$members =~ s/\s*____cacheline_aligned_in_smp/ /gos;

0 commit comments

Comments
 (0)