qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Jean-Louis Dupond <jean-louis@dupond.be>
To: Hanna Czenczek <hreitz@redhat.com>, qemu-devel@nongnu.org
Cc: Alexander Ivanov <alexander.ivanov@virtuozzo.com>,
	Kevin Wolf <kwolf@redhat.com>,
	Andrey Drobyshev <andrey.drobyshev@virtuozzo.com>,
	qemu-block@nongnu.org
Subject: Re: [PATCH 2/2] qcow2: put discards in discard queue when discard-no-unref is enabled
Date: Tue, 13 May 2025 15:27:30 +0200	[thread overview]
Message-ID: <3143e992-b3e8-4ae4-90ec-39a8eb3e02f7@dupond.be> (raw)
In-Reply-To: <c43d6a3b-ac4e-4cee-b034-7ddcdf7dedf8@redhat.com>


On 5/12/25 14:28, Hanna Czenczek wrote:
> On 29.04.25 12:31, Jean-Louis Dupond wrote:
>> When discard-no-unref is enabled, discards are not queued like it
>> should.
>> This was broken since discard-no-unref was added.
>>
>> Add some helper function qcow2_discard_cluster which handles some common
>> checks and calls the queue_discards function if needed to add the
>> discard request to the queue.
>>
>> Signed-off-by: Jean-Louis Dupond <jean-louis@dupond.be>
>> ---
>>   block/qcow2-cluster.c  | 16 ++++++----------
>>   block/qcow2-refcount.c | 19 ++++++++++++++++++-
>>   block/qcow2.h          |  4 ++++
>>   3 files changed, 28 insertions(+), 11 deletions(-)
>>
>> diff --git a/block/qcow2-cluster.c b/block/qcow2-cluster.c
>> index ce8c0076b3..c655bf6df4 100644
>> --- a/block/qcow2-cluster.c
>> +++ b/block/qcow2-cluster.c
>> @@ -1978,12 +1978,10 @@ discard_in_l2_slice(BlockDriverState *bs, 
>> uint64_t offset, uint64_t nb_clusters,
>>           if (!keep_reference) {
>>               /* Then decrease the refcount */
>>               qcow2_free_any_cluster(bs, old_l2_entry, type);
>> -        } else if (s->discard_passthrough[type] &&
>> -                   (cluster_type == QCOW2_CLUSTER_NORMAL ||
>> -                    cluster_type == QCOW2_CLUSTER_ZERO_ALLOC)) {
>> +        } else {
>>               /* If we keep the reference, pass on the discard still */
>> -            bdrv_pdiscard(s->data_file, old_l2_entry & L2E_OFFSET_MASK,
>> -                          s->cluster_size);
>> +            qcow2_discard_cluster(bs, old_l2_entry & L2E_OFFSET_MASK,
>> +                                  s->cluster_size, cluster_type, type);
>>           }
>>       }
>>   @@ -2092,12 +2090,10 @@ zero_in_l2_slice(BlockDriverState *bs, 
>> uint64_t offset,
>>               if (!keep_reference) {
>>                   /* Then decrease the refcount */
>>                   qcow2_free_any_cluster(bs, old_l2_entry, 
>> QCOW2_DISCARD_REQUEST);
>> -            } else if (s->discard_passthrough[QCOW2_DISCARD_REQUEST] &&
>> -                       (type == QCOW2_CLUSTER_NORMAL ||
>> -                        type == QCOW2_CLUSTER_ZERO_ALLOC)) {
>> +            } else {
>>                   /* If we keep the reference, pass on the discard 
>> still */
>> -                bdrv_pdiscard(s->data_file, old_l2_entry & 
>> L2E_OFFSET_MASK,
>> -                            s->cluster_size);
>> +                qcow2_discard_cluster(bs, old_l2_entry & 
>> L2E_OFFSET_MASK,
>> +                            s->cluster_size, type, 
>> QCOW2_DISCARD_REQUEST);
>>               }
>>           }
>>       }
>> diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c
>> index d796018970..e1f830504d 100644
>> --- a/block/qcow2-refcount.c
>> +++ b/block/qcow2-refcount.c
>> @@ -1205,6 +1205,23 @@ void qcow2_free_any_cluster(BlockDriverState 
>> *bs, uint64_t l2_entry,
>>       }
>>   }
>>   +void qcow2_discard_cluster(BlockDriverState *bs, uint64_t offset,
>> +                           uint64_t length, QCow2ClusterType ctype,
>> +                           enum qcow2_discard_type dtype) {
>> +
>
> QEMU coding style requires putting the { on a separate line.
>
Fixed
>> +    BDRVQcow2State *s = bs->opaque;
>> +
>> +    if (s->discard_passthrough[dtype] &&
>> +        (ctype == QCOW2_CLUSTER_NORMAL ||
>> +         ctype == QCOW2_CLUSTER_ZERO_ALLOC)) {
>> +        if (has_data_file(bs)) {
>> +            bdrv_pdiscard(s->data_file, offset, length);
>> +        } else {
>> +            queue_discard(bs, offset, length);
>> +        }
>> +    }
>> +}
>> +
>>   int qcow2_write_caches(BlockDriverState *bs)
>>   {
>>       BDRVQcow2State *s = bs->opaque;
>> @@ -3619,7 +3636,7 @@ qcow2_discard_refcount_block(BlockDriverState 
>> *bs, uint64_t discard_block_offs)
>>           /* discard refblock from the cache if refblock is cached */
>>           qcow2_cache_discard(s->refcount_block_cache, refblock);
>>       }
>> -    update_refcount_discard(bs, discard_block_offs, s->cluster_size);
>> +    queue_discard(bs, discard_block_offs, s->cluster_size);
>>         return 0;
>>   }
>
> This hunk should go into the previous patch.
>
Fixed
>> diff --git a/block/qcow2.h b/block/qcow2.h
>> index a9e3481c6e..547bb2b814 100644
>> --- a/block/qcow2.h
>> +++ b/block/qcow2.h
>> @@ -880,6 +880,10 @@ void GRAPH_RDLOCK 
>> qcow2_free_clusters(BlockDriverState *bs,
>>   void GRAPH_RDLOCK
>>   qcow2_free_any_cluster(BlockDriverState *bs, uint64_t l2_entry,
>>                          enum qcow2_discard_type type);
>> +void GRAPH_RDLOCK
>> +qcow2_discard_cluster(BlockDriverState *bs, uint64_t offset,
>> +                      uint64_t length, QCow2ClusterType ctype,
>> +                      enum qcow2_discard_type dtype);
>>     int GRAPH_RDLOCK
>>   qcow2_update_snapshot_refcount(BlockDriverState *bs, int64_t 
>> l1_table_offset,
>
> With the above done, for both patch 1 and 2:
>
> Reviewed-by: Hanna Czenczek <hreitz@redhat.com>
>
Send v2 patch with comments addressed.

Thanks
Jean-Louis


      reply	other threads:[~2025-05-13 13:28 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-29 10:31 [PATCH 0/2] qcow2: queue discards when discard-no-unref enabled Jean-Louis Dupond
2025-04-29 10:31 ` [PATCH 1/2] qcow2: rename update_refcount_discard to queue_discard Jean-Louis Dupond
2025-04-29 10:31 ` [PATCH 2/2] qcow2: put discards in discard queue when discard-no-unref is enabled Jean-Louis Dupond
2025-05-12 12:28   ` Hanna Czenczek
2025-05-13 13:27     ` Jean-Louis Dupond [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3143e992-b3e8-4ae4-90ec-39a8eb3e02f7@dupond.be \
    --to=jean-louis@dupond.be \
    --cc=alexander.ivanov@virtuozzo.com \
    --cc=andrey.drobyshev@virtuozzo.com \
    --cc=hreitz@redhat.com \
    --cc=kwolf@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).