Skip to content

Commit dfe44d1

Browse files
committed
vhost: switch to arrays of feature bits
The current interface where caller has to know in which 64 bit chunk each bit is, is inelegant and fragile. Let's simply use arrays of bits. By using unroll macros text size grows only slightly. Message-ID: <637e182e139980e5930d50b928ba5ac072d628a9.1764225384.git.mst@redhat.com> Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
1 parent 350a840 commit dfe44d1

5 files changed

Lines changed: 61 additions & 25 deletions

File tree

drivers/vhost/net.c

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,15 @@ MODULE_PARM_DESC(experimental_zcopytx, "Enable Zero Copy TX;"
6969

7070
#define VHOST_DMA_IS_DONE(len) ((__force u32)(len) >= (__force u32)VHOST_DMA_DONE_LEN)
7171

72-
static const u64 vhost_net_features[VIRTIO_FEATURES_U64S] = {
73-
VHOST_FEATURES |
74-
(1ULL << VHOST_NET_F_VIRTIO_NET_HDR) |
75-
(1ULL << VIRTIO_NET_F_MRG_RXBUF) |
76-
(1ULL << VIRTIO_F_ACCESS_PLATFORM) |
77-
(1ULL << VIRTIO_F_RING_RESET) |
78-
(1ULL << VIRTIO_F_IN_ORDER),
79-
VIRTIO_BIT(VIRTIO_NET_F_GUEST_UDP_TUNNEL_GSO) |
80-
VIRTIO_BIT(VIRTIO_NET_F_HOST_UDP_TUNNEL_GSO),
72+
static const int vhost_net_bits[] = {
73+
VHOST_FEATURES,
74+
VHOST_NET_F_VIRTIO_NET_HDR,
75+
VIRTIO_NET_F_MRG_RXBUF,
76+
VIRTIO_F_ACCESS_PLATFORM,
77+
VIRTIO_F_RING_RESET,
78+
VIRTIO_F_IN_ORDER,
79+
VIRTIO_NET_F_GUEST_UDP_TUNNEL_GSO,
80+
VIRTIO_NET_F_HOST_UDP_TUNNEL_GSO
8181
};
8282

8383
enum {
@@ -1720,6 +1720,7 @@ static long vhost_net_set_owner(struct vhost_net *n)
17201720
static long vhost_net_ioctl(struct file *f, unsigned int ioctl,
17211721
unsigned long arg)
17221722
{
1723+
const DEFINE_VHOST_FEATURES_ARRAY(vhost_net_features, vhost_net_bits);
17231724
u64 all_features[VIRTIO_FEATURES_U64S];
17241725
struct vhost_net *n = f->private_data;
17251726
void __user *argp = (void __user *)arg;

drivers/vhost/scsi.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,11 +197,14 @@ enum {
197197
};
198198

199199
/* Note: can't set VIRTIO_F_VERSION_1 yet, since that implies ANY_LAYOUT. */
200-
enum {
201-
VHOST_SCSI_FEATURES = VHOST_FEATURES | (1ULL << VIRTIO_SCSI_F_HOTPLUG) |
202-
(1ULL << VIRTIO_SCSI_F_T10_PI)
200+
static const int vhost_scsi_bits[] = {
201+
VHOST_FEATURES,
202+
VIRTIO_SCSI_F_HOTPLUG,
203+
VIRTIO_SCSI_F_T10_PI
203204
};
204205

206+
#define VHOST_SCSI_FEATURES VHOST_FEATURES_U64(vhost_scsi_bits, 0)
207+
205208
#define VHOST_SCSI_MAX_TARGET 256
206209
#define VHOST_SCSI_MAX_IO_VQ 1024
207210
#define VHOST_SCSI_MAX_EVENT 128

drivers/vhost/test.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@
2828
*/
2929
#define VHOST_TEST_PKT_WEIGHT 256
3030

31-
#define VHOST_TEST_FEATURES VHOST_FEATURES
31+
static const int vhost_test_bits[] = {
32+
VHOST_FEATURES
33+
};
34+
35+
#define VHOST_TEST_FEATURES VHOST_FEATURES_U64(vhost_test_bits, 0)
3236

3337
enum {
3438
VHOST_TEST_VQ = 0,

drivers/vhost/vhost.h

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <linux/atomic.h>
1515
#include <linux/vhost_iotlb.h>
1616
#include <linux/irqbypass.h>
17+
#include <linux/unroll.h>
1718

1819
struct vhost_work;
1920
struct vhost_task;
@@ -279,14 +280,39 @@ void vhost_iotlb_map_free(struct vhost_iotlb *iotlb,
279280
eventfd_signal((vq)->error_ctx);\
280281
} while (0)
281282

282-
enum {
283-
VHOST_FEATURES = (1ULL << VIRTIO_F_NOTIFY_ON_EMPTY) |
284-
(1ULL << VIRTIO_RING_F_INDIRECT_DESC) |
285-
(1ULL << VIRTIO_RING_F_EVENT_IDX) |
286-
(1ULL << VHOST_F_LOG_ALL) |
287-
(1ULL << VIRTIO_F_ANY_LAYOUT) |
288-
(1ULL << VIRTIO_F_VERSION_1)
289-
};
283+
#define VHOST_FEATURES \
284+
VIRTIO_F_NOTIFY_ON_EMPTY, \
285+
VIRTIO_RING_F_INDIRECT_DESC, \
286+
VIRTIO_RING_F_EVENT_IDX, \
287+
VHOST_F_LOG_ALL, \
288+
VIRTIO_F_ANY_LAYOUT, \
289+
VIRTIO_F_VERSION_1
290+
291+
static inline u64 vhost_features_u64(const int *features, int size, int idx)
292+
{
293+
u64 res = 0;
294+
295+
unrolled_count(VIRTIO_FEATURES_BITS)
296+
for (int i = 0; i < size; ++i) {
297+
int bit = features[i];
298+
299+
if (virtio_features_chk_bit(bit) && VIRTIO_U64(bit) == idx)
300+
res |= VIRTIO_BIT(bit);
301+
}
302+
return res;
303+
}
304+
305+
#define VHOST_FEATURES_U64(features, idx) \
306+
vhost_features_u64(features, ARRAY_SIZE(features), idx)
307+
308+
#define DEFINE_VHOST_FEATURES_ARRAY_ENTRY(idx, features) \
309+
[idx] = VHOST_FEATURES_U64(features, idx),
310+
311+
#define DEFINE_VHOST_FEATURES_ARRAY(array, features) \
312+
u64 array[VIRTIO_FEATURES_U64S] = { \
313+
UNROLL(VIRTIO_FEATURES_U64S, \
314+
DEFINE_VHOST_FEATURES_ARRAY_ENTRY, features) \
315+
}
290316

291317
/**
292318
* vhost_vq_set_backend - Set backend.

drivers/vhost/vsock.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,14 @@
2929
*/
3030
#define VHOST_VSOCK_PKT_WEIGHT 256
3131

32-
enum {
33-
VHOST_VSOCK_FEATURES = VHOST_FEATURES |
34-
(1ULL << VIRTIO_F_ACCESS_PLATFORM) |
35-
(1ULL << VIRTIO_VSOCK_F_SEQPACKET)
32+
static const int vhost_vsock_bits[] = {
33+
VHOST_FEATURES,
34+
VIRTIO_F_ACCESS_PLATFORM,
35+
VIRTIO_VSOCK_F_SEQPACKET
3636
};
3737

38+
#define VHOST_VSOCK_FEATURES VHOST_FEATURES_U64(vhost_vsock_bits, 0)
39+
3840
enum {
3941
VHOST_VSOCK_BACKEND_FEATURES = (1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2)
4042
};

0 commit comments

Comments
 (0)