At 2026-11-13 06:50:33, "Michael S. Tsirkin" <[email protected]> wrote:
>On Mon, Nov 09, 2026 at 05:43:08PM +0800, Longjun Tang wrote:
>> From: Longjun Tang <[email protected]>
>> 
>> Export a read-only accessor for the last consumed used ring index.
>> It's useful for drivers that need to detect whether the device has
>> produced buffers that have not been consumed yet, by passing the
>> returned value to virtqueue_poll().
>> 
>> The split ring now writes the field with WRITE_ONCE() (the packed
>> ring already did) to document that the new reader can run concurrently
>> with the poll path.
>> 
>> Signed-off-by: Longjun Tang <[email protected]>
>
>This is not a good API, packed ring does not have a used ring at all.

Right, packed has no used ring. But last_used_idx is maintained for both split 
and packed
rings and can be used to get the position the driver last consumed.
>
>> ---
>>  drivers/virtio/virtio_ring.c | 18 ++++++++++++++++--
>>  include/linux/virtio.h       |  2 ++
>>  2 files changed, 18 insertions(+), 2 deletions(-)
>> 
>> diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c
>> index db678f5a80e0..6d8485c73881 100644
>> --- a/drivers/virtio/virtio_ring.c
>> +++ b/drivers/virtio/virtio_ring.c
>> @@ -1001,7 +1001,7 @@ static void *virtqueue_get_buf_ctx_split(struct 
>> vring_virtqueue *vq,
>>      /* detach_buf_split clears data, so grab it now. */
>>      ret = vq->split.desc_state[i].data;
>>      detach_buf_split(vq, i, ctx);
>> -    vq->last_used_idx++;
>> +    WRITE_ONCE(vq->last_used_idx, vq->last_used_idx + 1);
>>      /* If we expect an interrupt for the next entry, tell host
>>       * by writing event index and flush out the write before
>>       * the read in the next get_buf call. */
>> @@ -1068,7 +1068,7 @@ static void 
>> *virtqueue_get_buf_ctx_split_in_order(struct vring_virtqueue *vq,
>>      ret = vq->split.desc_state[last_used].data;
>>      detach_buf_split_in_order(vq, last_used, ctx);
>>  
>> -    vq->last_used_idx++;
>> +    WRITE_ONCE(vq->last_used_idx, vq->last_used_idx + 1);
>>      vq->last_used += (vq->vq.num_free - num_free);
>>      /* If we expect an interrupt for the next entry, tell host
>>       * by writing event index and flush out the write before
>> @@ -3227,6 +3227,20 @@ bool virtqueue_poll(struct virtqueue *_vq, unsigned 
>> int last_used_idx)
>>  }
>>  EXPORT_SYMBOL_GPL(virtqueue_poll);
>>  
>> +/**
>> + * virtqueue_get_last_used_idx - get the last consumed used ring index
>> + * @_vq: the struct virtqueue we're talking about.
>> + *
>> + * Returns the index of the most recently consumed used buffer.
>> + */
>> +unsigned int virtqueue_get_last_used_idx(const struct virtqueue *_vq)
>> +{
>> +    const struct vring_virtqueue *vq = to_vvq(_vq);
>> +
>> +    return READ_ONCE(vq->last_used_idx);
>> +}
>> +EXPORT_SYMBOL_GPL(virtqueue_get_last_used_idx);
>> +
>>  /**
>>   * virtqueue_enable_cb - restart callbacks after disable_cb.
>>   * @_vq: the struct virtqueue we're talking about.
>> diff --git a/include/linux/virtio.h b/include/linux/virtio.h
>> index f923e42cfd01..1aed4d9235c8 100644
>> --- a/include/linux/virtio.h
>> +++ b/include/linux/virtio.h
>> @@ -112,6 +112,8 @@ unsigned virtqueue_enable_cb_prepare(struct virtqueue 
>> *vq);
>>  
>>  bool virtqueue_poll(struct virtqueue *vq, unsigned);
>>  
>> +unsigned int virtqueue_get_last_used_idx(const struct virtqueue *vq);
>> +
>>  bool virtqueue_enable_cb_delayed(struct virtqueue *vq);
>>  
>>  void *virtqueue_detach_unused_buf(struct virtqueue *vq);
>> -- 
>> 2.51.0

Reply via email to