All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Michael S. Tsirkin" <mst@redhat.com>
To: Alex Williamson <alex.williamson@hp.com>
Cc: kvm@vger.kernel.org, sheng@linux.intel.com
Subject: Re: [PATCH v2] kvm: Use a bitmap for tracking used GSIs
Date: Tue, 12 May 2009 22:39:14 +0300	[thread overview]
Message-ID: <20090512193914.GB28935@redhat.com> (raw)
In-Reply-To: <20090508222925.5119.94814.stgit@dl380g6-3.ned.telco.ned.telco>


On Fri, May 08, 2009 at 04:31:21PM -0600, Alex Williamson wrote:
> +#ifdef KVM_CAP_IRQ_ROUTING

We don't need these anymore.

> +static inline void set_bit(unsigned int *buf, int bit)
> +{
> +	buf[bit >> 5] |= (1U << (bit & 0x1f));
> +}

external () not needed here. bit >> 5 might be clearer as bit / 32 IMO.

> +
> +static inline void clear_bit(unsigned int *buf, int bit)
> +{
> +	buf[bit >> 5] &= ~(1U << (bit & 0x1f));
> +}

Make bit unsigned. And then bit & 0x1f can be written as bit % 32.
IMO that's easier to parse.

> +
> +static int kvm_find_free_gsi(kvm_context_t kvm)
> +{
> +	int i, bit, gsi;
> +	unsigned int *buf = kvm->used_gsi_bitmap;
> +
> +	for (i = 0; i < (kvm->max_gsi >> 5); i++) {

may be clearer as kvm->max_gsi / 32

> +		if (buf[i] != ~0U)
> +			break;
> +	}

{} around single statement isn't needed

> +
> +	if (i == kvm->max_gsi >> 5)
> +		return -ENOSPC;

May be clearer as kvm->max_gsi / 32
By the way, this math means we can't use all gsi's
if the number is not a multiple of 32.
Round up instead? It's not hard: (kvm->max_gsi + 31) / 32

> +
> +	bit = ffs(~buf[i]);
> +	if (!bit)
> +		return -EAGAIN;

We know it won't be 0, right?
Instead of checking twice, move the ffs call within the loop above?
E.g. like this:
	for (i = 0; i < kvm->max_gsi / 32; ++i)
		if ((bit = ffs(~buf[i])) {
		    gsi = bit - 1 + i * 32;
		    set_bit(buf, gsi);
		    return gsi;
		}

	return -ENOSPC;

> +
> +	gsi = (bit - 1) | (i << 5);

clearer as bit - 1 + i * 32

> +	set_bit(buf, gsi);
> +	return gsi;
> +}
> +#endif
> +
>  int kvm_get_irq_route_gsi(kvm_context_t kvm)
>  {
>  #ifdef KVM_CAP_IRQ_ROUTING
> -	if (kvm->max_used_gsi >= KVM_IOAPIC_NUM_PINS)  {
> -	    if (kvm->max_used_gsi + 1 < kvm_get_gsi_count(kvm))
> -                return kvm->max_used_gsi + 1;
> -            else
> -                return -ENOSPC;
> -        } else
> -            return KVM_IOAPIC_NUM_PINS;
> +	int gsi;
> +
> +	pthread_mutex_lock(&kvm->gsi_mutex);
> +
> +	if (!kvm->max_gsi) {

Why is this lazy allocation required?
Let's do the below in some init function,
and keep code simple?

> +		int i;
> +
> +		/* Round the number of GSIs supported to a 4 byte
> +		 * value so we can search it using ints and ffs */
> +		i = kvm_get_gsi_count(kvm) & ~0x1f;

Should not we round up? Why not?
Again, we can count = kvm_get_gsi_count(kvm) / 32, and use count * 4 below.

> +		kvm->used_gsi_bitmap = malloc(i >> 3);

why not qemu_malloc?

> +		if (!kvm->used_gsi_bitmap) {
> +			pthread_mutex_unlock(&kvm->gsi_mutex);
> +			return -ENOMEM;
> +		}
> +		memset(kvm->used_gsi_bitmap, 0, i >> 3);
> +		kvm->max_gsi = i;
> +
> +		/* Mark all the IOAPIC pin GSIs as already used */
> +		for (i = 0; i <= KVM_IOAPIC_NUM_PINS; i++)

Is this really <=? Not <?

> +			set_bit(kvm->used_gsi_bitmap, i);
> +	}
> +
> +	gsi = kvm_find_free_gsi(kvm);
> +	pthread_mutex_unlock(&kvm->gsi_mutex);
> +	return gsi;
>  #else
>  	return -ENOSYS;
>  #endif
>  }
> + 
> +void kvm_free_irq_route_gsi(kvm_context_t kvm, int gsi)
> +{
> +#ifdef KVM_CAP_IRQ_ROUTING
> +	pthread_mutex_lock(&kvm->gsi_mutex);
> +	clear_bit(kvm->used_gsi_bitmap, gsi);
> +	pthread_mutex_unlock(&kvm->gsi_mutex);
> +#endif
> +}
>  
>  #ifdef KVM_CAP_DEVICE_MSIX
>  int kvm_assign_set_msix_nr(kvm_context_t kvm,
> diff --git a/kvm/libkvm/libkvm.h b/kvm/libkvm/libkvm.h
> index c23d37b..4e9344c 100644
> --- a/kvm/libkvm/libkvm.h
> +++ b/kvm/libkvm/libkvm.h
> @@ -856,6 +856,16 @@ int kvm_commit_irq_routes(kvm_context_t kvm);
>   */
>  int kvm_get_irq_route_gsi(kvm_context_t kvm);
>  
> +/*!
> + * \brief Free used GSI number
> + *
> + * Free used GSI number acquired from kvm_get_irq_route_gsi()
> + *
> + * \param kvm Pointer to the current kvm_context
> + * \param gsi GSI number to free
> + */
> +void kvm_free_irq_route_gsi(kvm_context_t kvm, int gsi);
> +
>  #ifdef KVM_CAP_DEVICE_MSIX
>  int kvm_assign_set_msix_nr(kvm_context_t kvm,
>  			   struct kvm_assigned_msix_nr *msix_nr);
> 

-- 
MST

  reply	other threads:[~2009-05-12 19:40 UTC|newest]

Thread overview: 56+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-05-07 22:22 [PATCH] kvm: Use a bitmap for tracking used GSIs Alex Williamson
2009-05-08 22:31 ` [PATCH v2] " Alex Williamson
2009-05-12 19:39   ` Michael S. Tsirkin [this message]
2009-05-12 21:56     ` Alex Williamson
2009-05-12 22:07   ` [PATCH v3] " Alex Williamson
2009-05-13  3:30     ` Yang, Sheng
2009-05-13  3:42       ` Alex Williamson
2009-05-13  4:10         ` Alex Williamson
2009-05-13  4:15           ` Yang, Sheng
2009-05-13  4:41     ` [PATCH v4] " Alex Williamson
2009-05-13  4:58       ` Yang, Sheng
2009-05-13  9:47       ` Avi Kivity
2009-05-13 12:28         ` Alex Williamson
2009-05-13 12:35           ` Avi Kivity
2009-05-13 12:55             ` Alex Williamson
2009-05-13 13:00               ` Avi Kivity
2009-05-13 13:11                 ` Alex Williamson
2009-05-13 13:55                   ` Michael S. Tsirkin
2009-05-13 14:15                     ` Alex Williamson
2009-05-13 14:30                       ` Michael S. Tsirkin
2009-05-13 14:33                       ` Alex Williamson
2009-05-13 23:07                         ` Alex Williamson
2009-05-17 20:47                           ` Avi Kivity
2009-05-18 11:12                             ` Michael S. Tsirkin
2009-05-18 11:36                               ` Avi Kivity
2009-05-18 12:19                                 ` Michael S. Tsirkin
2009-05-18 12:33                                   ` Avi Kivity
2009-05-18 13:45                                     ` Michael S. Tsirkin
2009-05-18 13:55                                       ` Avi Kivity
2009-05-18 14:40                                         ` Michael S. Tsirkin
2009-05-18 14:46                                           ` Avi Kivity
2009-05-18 15:01                                             ` Michael S. Tsirkin
2009-05-18 15:08                                               ` Avi Kivity
2009-05-13 14:32       ` Michael S. Tsirkin
2009-05-13 15:13       ` [PATCH v5] " Alex Williamson
2009-05-13 16:05         ` Michael S. Tsirkin
2009-05-13 17:13           ` Alex Williamson
2009-05-17 20:51           ` Avi Kivity
2009-05-13 17:28         ` [PATCH v6] " Alex Williamson
2009-05-13 18:46           ` Michael S. Tsirkin
2009-05-17 20:54           ` Avi Kivity
2009-05-18 22:32             ` Alex Williamson
2009-05-19  8:01               ` Avi Kivity
2009-05-19 20:48           ` [PATCH v7] " Alex Williamson
2009-05-20 11:55             ` Avi Kivity
2009-05-13  7:03     ` [PATCH v3] " Michael S. Tsirkin
2009-05-13 12:15       ` Alex Williamson
2009-05-13  7:04     ` Michael S. Tsirkin
2009-05-13 12:19       ` Alex Williamson
2009-05-13 14:25         ` Michael S. Tsirkin
2009-05-17 20:49         ` Avi Kivity
2009-05-11 12:00 ` [PATCH] " Yang, Sheng
2009-05-12 18:45   ` Alex Williamson
2009-05-12 19:06     ` Alex Williamson
2009-05-12 19:10       ` Michael S. Tsirkin
2009-05-12 19:23         ` Alex Williamson

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=20090512193914.GB28935@redhat.com \
    --to=mst@redhat.com \
    --cc=alex.williamson@hp.com \
    --cc=kvm@vger.kernel.org \
    --cc=sheng@linux.intel.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.