Skip to content

Commit 112f23c

Browse files
stefano-garzarellamstsirkin
authored andcommitted
vdpa_sim: move buffer allocation in the devices
Currently, the vdpa_sim core does not use the buffer, but only allocates it. The buffer is used by devices differently, and some future devices may not use it. So let's move all its management inside the devices. Add a new `free` device callback called to clean up the resources allocated by the device. Signed-off-by: Stefano Garzarella <sgarzare@redhat.com> Message-Id: <20230407133658.66339-2-sgarzare@redhat.com> Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
1 parent 5b250fa commit 112f23c

4 files changed

Lines changed: 57 additions & 21 deletions

File tree

drivers/vdpa/vdpa_sim/vdpa_sim.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -261,10 +261,6 @@ struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *dev_attr,
261261
for (i = 0; i < vdpasim->dev_attr.nas; i++)
262262
vhost_iotlb_init(&vdpasim->iommu[i], max_iotlb_entries, 0);
263263

264-
vdpasim->buffer = kvmalloc(dev_attr->buffer_size, GFP_KERNEL);
265-
if (!vdpasim->buffer)
266-
goto err_iommu;
267-
268264
for (i = 0; i < dev_attr->nvqs; i++)
269265
vringh_set_iotlb(&vdpasim->vqs[i].vring, &vdpasim->iommu[0],
270266
&vdpasim->iommu_lock);
@@ -714,7 +710,8 @@ static void vdpasim_free(struct vdpa_device *vdpa)
714710
vringh_kiov_cleanup(&vdpasim->vqs[i].in_iov);
715711
}
716712

717-
kvfree(vdpasim->buffer);
713+
vdpasim->dev_attr.free(vdpasim);
714+
718715
for (i = 0; i < vdpasim->dev_attr.nas; i++)
719716
vhost_iotlb_reset(&vdpasim->iommu[i]);
720717
kfree(vdpasim->iommu);

drivers/vdpa/vdpa_sim/vdpa_sim.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ struct vdpasim_dev_attr {
3939
u64 supported_features;
4040
size_t alloc_size;
4141
size_t config_size;
42-
size_t buffer_size;
4342
int nvqs;
4443
u32 id;
4544
u32 ngroups;
@@ -51,6 +50,7 @@ struct vdpasim_dev_attr {
5150
int (*get_stats)(struct vdpasim *vdpasim, u16 idx,
5251
struct sk_buff *msg,
5352
struct netlink_ext_ack *extack);
53+
void (*free)(struct vdpasim *vdpasim);
5454
};
5555

5656
/* State of each vdpasim device */
@@ -67,7 +67,6 @@ struct vdpasim {
6767
void *config;
6868
struct vhost_iotlb *iommu;
6969
bool *iommu_pt;
70-
void *buffer;
7170
u32 status;
7271
u32 generation;
7372
u64 features;

drivers/vdpa/vdpa_sim/vdpa_sim_blk.c

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@
4343
#define VDPASIM_BLK_AS_NUM 1
4444
#define VDPASIM_BLK_GROUP_NUM 1
4545

46+
struct vdpasim_blk {
47+
struct vdpasim vdpasim;
48+
void *buffer;
49+
};
50+
51+
static struct vdpasim_blk *sim_to_blk(struct vdpasim *vdpasim)
52+
{
53+
return container_of(vdpasim, struct vdpasim_blk, vdpasim);
54+
}
55+
4656
static char vdpasim_blk_id[VIRTIO_BLK_ID_BYTES] = "vdpa_blk_sim";
4757

4858
static bool vdpasim_blk_check_range(struct vdpasim *vdpasim, u64 start_sector,
@@ -78,6 +88,7 @@ static bool vdpasim_blk_check_range(struct vdpasim *vdpasim, u64 start_sector,
7888
static bool vdpasim_blk_handle_req(struct vdpasim *vdpasim,
7989
struct vdpasim_virtqueue *vq)
8090
{
91+
struct vdpasim_blk *blk = sim_to_blk(vdpasim);
8192
size_t pushed = 0, to_pull, to_push;
8293
struct virtio_blk_outhdr hdr;
8394
bool handled = false;
@@ -144,8 +155,7 @@ static bool vdpasim_blk_handle_req(struct vdpasim *vdpasim,
144155
}
145156

146157
bytes = vringh_iov_push_iotlb(&vq->vring, &vq->in_iov,
147-
vdpasim->buffer + offset,
148-
to_push);
158+
blk->buffer + offset, to_push);
149159
if (bytes < 0) {
150160
dev_dbg(&vdpasim->vdpa.dev,
151161
"vringh_iov_push_iotlb() error: %zd offset: 0x%llx len: 0x%zx\n",
@@ -166,8 +176,7 @@ static bool vdpasim_blk_handle_req(struct vdpasim *vdpasim,
166176
}
167177

168178
bytes = vringh_iov_pull_iotlb(&vq->vring, &vq->out_iov,
169-
vdpasim->buffer + offset,
170-
to_pull);
179+
blk->buffer + offset, to_pull);
171180
if (bytes < 0) {
172181
dev_dbg(&vdpasim->vdpa.dev,
173182
"vringh_iov_pull_iotlb() error: %zd offset: 0x%llx len: 0x%zx\n",
@@ -247,7 +256,7 @@ static bool vdpasim_blk_handle_req(struct vdpasim *vdpasim,
247256
}
248257

249258
if (type == VIRTIO_BLK_T_WRITE_ZEROES) {
250-
memset(vdpasim->buffer + offset, 0,
259+
memset(blk->buffer + offset, 0,
251260
num_sectors << SECTOR_SHIFT);
252261
}
253262

@@ -353,6 +362,13 @@ static void vdpasim_blk_get_config(struct vdpasim *vdpasim, void *config)
353362

354363
}
355364

365+
static void vdpasim_blk_free(struct vdpasim *vdpasim)
366+
{
367+
struct vdpasim_blk *blk = sim_to_blk(vdpasim);
368+
369+
kvfree(blk->buffer);
370+
}
371+
356372
static void vdpasim_blk_mgmtdev_release(struct device *dev)
357373
{
358374
}
@@ -366,6 +382,7 @@ static int vdpasim_blk_dev_add(struct vdpa_mgmt_dev *mdev, const char *name,
366382
const struct vdpa_dev_set_config *config)
367383
{
368384
struct vdpasim_dev_attr dev_attr = {};
385+
struct vdpasim_blk *blk;
369386
struct vdpasim *simdev;
370387
int ret;
371388

@@ -376,16 +393,25 @@ static int vdpasim_blk_dev_add(struct vdpa_mgmt_dev *mdev, const char *name,
376393
dev_attr.nvqs = VDPASIM_BLK_VQ_NUM;
377394
dev_attr.ngroups = VDPASIM_BLK_GROUP_NUM;
378395
dev_attr.nas = VDPASIM_BLK_AS_NUM;
379-
dev_attr.alloc_size = sizeof(struct vdpasim);
396+
dev_attr.alloc_size = sizeof(struct vdpasim_blk);
380397
dev_attr.config_size = sizeof(struct virtio_blk_config);
381398
dev_attr.get_config = vdpasim_blk_get_config;
382399
dev_attr.work_fn = vdpasim_blk_work;
383-
dev_attr.buffer_size = VDPASIM_BLK_CAPACITY << SECTOR_SHIFT;
400+
dev_attr.free = vdpasim_blk_free;
384401

385402
simdev = vdpasim_create(&dev_attr, config);
386403
if (IS_ERR(simdev))
387404
return PTR_ERR(simdev);
388405

406+
blk = sim_to_blk(simdev);
407+
408+
blk->buffer = kvmalloc(VDPASIM_BLK_CAPACITY << SECTOR_SHIFT,
409+
GFP_KERNEL);
410+
if (!blk->buffer) {
411+
ret = -ENOMEM;
412+
goto put_dev;
413+
}
414+
389415
ret = _vdpa_register_device(&simdev->vdpa, VDPASIM_BLK_VQ_NUM);
390416
if (ret)
391417
goto put_dev;

drivers/vdpa/vdpa_sim/vdpa_sim_net.c

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ struct vdpasim_net{
5858
struct vdpasim_dataq_stats tx_stats;
5959
struct vdpasim_dataq_stats rx_stats;
6060
struct vdpasim_cq_stats cq_stats;
61+
void *buffer;
6162
};
6263

6364
static struct vdpasim_net *sim_to_net(struct vdpasim *vdpasim)
@@ -87,14 +88,15 @@ static bool receive_filter(struct vdpasim *vdpasim, size_t len)
8788
size_t hdr_len = modern ? sizeof(struct virtio_net_hdr_v1) :
8889
sizeof(struct virtio_net_hdr);
8990
struct virtio_net_config *vio_config = vdpasim->config;
91+
struct vdpasim_net *net = sim_to_net(vdpasim);
9092

9193
if (len < ETH_ALEN + hdr_len)
9294
return false;
9395

94-
if (is_broadcast_ether_addr(vdpasim->buffer + hdr_len) ||
95-
is_multicast_ether_addr(vdpasim->buffer + hdr_len))
96+
if (is_broadcast_ether_addr(net->buffer + hdr_len) ||
97+
is_multicast_ether_addr(net->buffer + hdr_len))
9698
return true;
97-
if (!strncmp(vdpasim->buffer + hdr_len, vio_config->mac, ETH_ALEN))
99+
if (!strncmp(net->buffer + hdr_len, vio_config->mac, ETH_ALEN))
98100
return true;
99101

100102
return false;
@@ -225,8 +227,7 @@ static void vdpasim_net_work(struct vdpasim *vdpasim)
225227

226228
++tx_pkts;
227229
read = vringh_iov_pull_iotlb(&txq->vring, &txq->out_iov,
228-
vdpasim->buffer,
229-
PAGE_SIZE);
230+
net->buffer, PAGE_SIZE);
230231

231232
tx_bytes += read;
232233

@@ -245,7 +246,7 @@ static void vdpasim_net_work(struct vdpasim *vdpasim)
245246
}
246247

247248
write = vringh_iov_push_iotlb(&rxq->vring, &rxq->in_iov,
248-
vdpasim->buffer, read);
249+
net->buffer, read);
249250
if (write <= 0) {
250251
++rx_errors;
251252
break;
@@ -427,6 +428,13 @@ static void vdpasim_net_setup_config(struct vdpasim *vdpasim,
427428
vio_config->mtu = cpu_to_vdpasim16(vdpasim, 1500);
428429
}
429430

431+
static void vdpasim_net_free(struct vdpasim *vdpasim)
432+
{
433+
struct vdpasim_net *net = sim_to_net(vdpasim);
434+
435+
kvfree(net->buffer);
436+
}
437+
430438
static void vdpasim_net_mgmtdev_release(struct device *dev)
431439
{
432440
}
@@ -456,7 +464,7 @@ static int vdpasim_net_dev_add(struct vdpa_mgmt_dev *mdev, const char *name,
456464
dev_attr.get_config = vdpasim_net_get_config;
457465
dev_attr.work_fn = vdpasim_net_work;
458466
dev_attr.get_stats = vdpasim_net_get_stats;
459-
dev_attr.buffer_size = PAGE_SIZE;
467+
dev_attr.free = vdpasim_net_free;
460468

461469
simdev = vdpasim_create(&dev_attr, config);
462470
if (IS_ERR(simdev))
@@ -470,6 +478,12 @@ static int vdpasim_net_dev_add(struct vdpa_mgmt_dev *mdev, const char *name,
470478
u64_stats_init(&net->rx_stats.syncp);
471479
u64_stats_init(&net->cq_stats.syncp);
472480

481+
net->buffer = kvmalloc(PAGE_SIZE, GFP_KERNEL);
482+
if (!net->buffer) {
483+
ret = -ENOMEM;
484+
goto reg_err;
485+
}
486+
473487
/*
474488
* Initialization must be completed before this call, since it can
475489
* connect the device to the vDPA bus, so requests can arrive after

0 commit comments

Comments
 (0)