Re: [PATCH 4/7] KVM: selftests: Add helpers to consolidate open coded list operations

From: Peter Xu
Date: Fri Mar 20 2020 - 18:47:58 EST


On Fri, Mar 20, 2020 at 01:55:43PM -0700, Sean Christopherson wrote:
> Add helpers for the KVM sefltests' variant of a linked list to replace a
> variety of open coded adds, deletes and iterators.
>
> Signed-off-by: Sean Christopherson <sean.j.christopherson@xxxxxxxxx>
> ---
> tools/testing/selftests/kvm/lib/kvm_util.c | 68 ++++++++++++----------
> 1 file changed, 37 insertions(+), 31 deletions(-)
>
> diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c
> index 9a783c20dd26..d7b74f465570 100644
> --- a/tools/testing/selftests/kvm/lib/kvm_util.c
> +++ b/tools/testing/selftests/kvm/lib/kvm_util.c
> @@ -19,6 +19,27 @@
> #define KVM_UTIL_PGS_PER_HUGEPG 512
> #define KVM_UTIL_MIN_PFN 2
>
> +#define kvm_list_add(head, new) \
> +do { \
> + if (head) \
> + head->prev = new; \
> + new->next = head; \
> + head = new; \
> +} while (0)
> +
> +#define kvm_list_del(head, del) \
> +do { \
> + if (del->next) \
> + del->next->prev = del->prev; \
> + if (del->prev) \
> + del->prev->next = del->next; \
> + else \
> + head = del->next; \
> +} while (0)
> +
> +#define kvm_list_for_each(head, iter) \
> + for (iter = head; iter; iter = iter->next)
> +

I'm not sure whether we should start to use a common list, e.g.,
tools/include/linux/list.h, if we're going to rework them after all...
Even if this is preferred, maybe move to a header so kvm selftests can
use it in the future outside "vcpu" struct too and this file only?

Thanks,

--
Peter Xu