All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jeremy Fitzhardinge <jeremy@goop.org>
To: Daniel De Graaf <dgdegra@tycho.nsa.gov>
Cc: xen-devel@lists.xensource.com, Ian.Jackson@eu.citrix.com,
	konrad.wilk@oracle.com
Subject: Re: [PATCH 1/2] xen/event: Add reference counting to event channel
Date: Fri, 16 Sep 2011 21:50:11 -0700	[thread overview]
Message-ID: <4E742703.7050005@goop.org> (raw)
In-Reply-To: <1316207684-19860-2-git-send-email-dgdegra@tycho.nsa.gov>

On 09/16/2011 02:14 PM, Daniel De Graaf wrote:
> Event channels exposed to userspace by the evtchn module may be used by
> other modules in an asynchronous manner, which requires that reference
> counting be used to prevent the event channel from being closed before
> the signals are delivered.

Could you use the refcounting at the irq level?  I was quite pleased to
have removed the event channel refcounting (and the use of naked event
channels).

Oh, is it that userspace allocates an event channel with /dev/evtchn,
then passes that event channel to the gntalloc/gntdev drivers so they
can use it to pass events between the two.  That's a bit unfortunate; it
might have been better to expose those event channels as file
descriptors so you could use fd refcounting to manage the lifetimes.

What's the downside of sending the event after the event channel has closed?

That said:

>
> Signed-off-by: Daniel De Graaf <dgdegra@tycho.nsa.gov>
> ---
>  drivers/xen/events.c |   38 ++++++++++++++++++++++++++++++++++++++
>  include/xen/events.h |    6 ++++++
>  2 files changed, 44 insertions(+), 0 deletions(-)
>
> diff --git a/drivers/xen/events.c b/drivers/xen/events.c
> index da70f5c..c9343b9 100644
> --- a/drivers/xen/events.c
> +++ b/drivers/xen/events.c
> @@ -89,6 +89,7 @@ struct irq_info
>  {
>  	struct list_head list;
>  	enum xen_irq_type type;	/* type */
> +	unsigned short refcount;

Is short large enough?  Is this something that untrusted userspace could
end up wrapping?  If short is sufficient, you should pack it next to the
other short fields to avoid a gap.

>  	unsigned irq;
>  	unsigned short evtchn;	/* event channel */
>  	unsigned short cpu;	/* cpu bound */
> @@ -407,6 +408,7 @@ static void xen_irq_init(unsigned irq)
>  		panic("Unable to allocate metadata for IRQ%d\n", irq);
>  
>  	info->type = IRQT_UNBOUND;
> +	info->refcount = 1;
>  
>  	irq_set_handler_data(irq, info);
>  
> @@ -469,6 +471,8 @@ static void xen_free_irq(unsigned irq)
>  
>  	irq_set_handler_data(irq, NULL);
>  
> +	BUG_ON(info->refcount > 1);
> +
>  	kfree(info);
>  
>  	/* Legacy IRQ descriptors are managed by the arch. */
> @@ -912,9 +916,14 @@ static void unbind_from_irq(unsigned int irq)
>  {
>  	struct evtchn_close close;
>  	int evtchn = evtchn_from_irq(irq);
> +	struct irq_info *info = irq_get_handler_data(irq);
>  
>  	spin_lock(&irq_mapping_update_lock);
>  
> +	info->refcount--;
> +	if (info->refcount > 0)
> +		goto out_unlock;
> +
>  	if (VALID_EVTCHN(evtchn)) {
>  		close.port = evtchn;
>  		if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
> @@ -943,6 +952,7 @@ static void unbind_from_irq(unsigned int irq)
>  
>  	xen_free_irq(irq);
>  
> + out_unlock:
>  	spin_unlock(&irq_mapping_update_lock);
>  }
>  
> @@ -1038,6 +1048,34 @@ void unbind_from_irqhandler(unsigned int irq, void *dev_id)
>  }
>  EXPORT_SYMBOL_GPL(unbind_from_irqhandler);
>  
> +int get_evtchn_reservation(unsigned int evtchn)
"reservation"?  I think just evtchn_get/put would be more consistent
with kernel naming conventions.

> +{
> +	int irq = evtchn_to_irq[evtchn];
> +	struct irq_info *info;
> +
> +	if (irq == -1)
> +		return -ENOENT;
> +
> +	info = irq_get_handler_data(irq);
> +
> +	if (!info)
> +		return -ENOENT;
> +
> +	spin_lock(&irq_mapping_update_lock);
> +	info->refcount++;
> +	spin_unlock(&irq_mapping_update_lock);

What is this spinlock protecting against?  The non-atomicity of ++, or
something larger scale?  If its just an atomicity thing, should it be an
atomic_t?

> +
> +	return 0;
> +}
> +EXPORT_SYMBOL_GPL(get_evtchn_reservation);
> +
> +void put_evtchn_reservation(unsigned int evtchn)
> +{
> +	int irq = evtchn_to_irq[evtchn];
> +	unbind_from_irq(irq);

Hm.

> +}
> +EXPORT_SYMBOL_GPL(put_evtchn_reservation);
> +
>  void xen_send_IPI_one(unsigned int cpu, enum ipi_vector vector)
>  {
>  	int irq = per_cpu(ipi_to_irq, cpu)[vector];
> diff --git a/include/xen/events.h b/include/xen/events.h
> index d287997..23bd5fd 100644
> --- a/include/xen/events.h
> +++ b/include/xen/events.h
> @@ -37,6 +37,12 @@ int bind_interdomain_evtchn_to_irqhandler(unsigned int remote_domain,
>   */
>  void unbind_from_irqhandler(unsigned int irq, void *dev_id);
>  
> +/*
> + * Allow extra references to event channels exposed to userspace by evtchn
> + */
> +int get_evtchn_reservation(unsigned int evtchn);
> +void put_evtchn_reservation(unsigned int evtchn);
> +
>  void xen_send_IPI_one(unsigned int cpu, enum ipi_vector vector);
>  int resend_irq_on_evtchn(unsigned int irq);
>  void rebind_evtchn_irq(int evtchn, int irq);

  reply	other threads:[~2011-09-17  4:50 UTC|newest]

Thread overview: 44+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-09-16 21:14 [PATCH 0/2] Add reference counting to grant notify ioctls Daniel De Graaf
2011-09-16 21:14 ` [PATCH 1/2] xen/event: Add reference counting to event channel Daniel De Graaf
2011-09-17  4:50   ` Jeremy Fitzhardinge [this message]
2011-09-17  6:11     ` Keir Fraser
2011-09-19 15:50     ` Daniel De Graaf
2011-09-19 16:22       ` Jeremy Fitzhardinge
2011-09-19 17:53         ` Daniel De Graaf
2011-09-16 21:14 ` [PATCH 2/2] xen/gnt{dev, alloc}: reserve event channels for notify Daniel De Graaf
2011-10-18 21:04 ` [PATCH v2 0/2] Add reference counting to grant notify ioctls Daniel De Graaf
2011-10-18 21:04   ` [PATCH 1/2] xen/event: Add reference counting to event channel Daniel De Graaf
2011-10-19  9:20     ` Ian Campbell
2011-10-24 20:22     ` Konrad Rzeszutek Wilk
2011-10-24 22:20       ` Daniel De Graaf
2011-10-25 19:05         ` Konrad Rzeszutek Wilk
2011-10-25  8:17       ` Ian Campbell
2011-10-25 19:09         ` Konrad Rzeszutek Wilk
2011-10-25 20:44           ` Ian Campbell
2011-10-18 21:04   ` [PATCH 2/2] xen/gnt{dev, alloc}: reserve event channels for notify Daniel De Graaf
2011-10-19  9:24     ` Ian Campbell
2011-10-19 14:23       ` Daniel De Graaf
2011-10-19 14:45         ` [PATCH 2/2] xen/gnt{dev,alloc}: " Ian Campbell
2011-10-24 20:57     ` [PATCH 2/2] xen/gnt{dev, alloc}: " Konrad Rzeszutek Wilk
2011-10-24 22:01       ` Daniel De Graaf
2011-10-25 19:02         ` Konrad Rzeszutek Wilk
2011-10-25 19:41           ` Daniel De Graaf
2011-10-25 19:51             ` Konrad Rzeszutek Wilk
2011-10-25 20:27             ` Ian Campbell
2011-10-25 20:42               ` Daniel De Graaf
2011-10-25 20:50                 ` Ian Campbell
2011-10-25 21:07                   ` Daniel De Graaf
2011-10-26 15:47 ` [PATCH v3 0/3] Add reference counting to grant notify ioctls Daniel De Graaf
2011-10-26 15:47   ` [PATCH 1/3] xen/event: Add reference counting to event channels Daniel De Graaf
2011-10-26 16:51     ` Ian Campbell
2011-10-26 16:57       ` Ian Campbell
2011-10-26 17:28       ` Daniel De Graaf
2011-10-27  9:03         ` Ian Campbell
2011-10-26 18:42     ` [PATCH 1/3 v3.2] " Daniel De Graaf
2011-10-26 15:47   ` [PATCH 2/3] xen/gntalloc: Change gref_lock to a mutex Daniel De Graaf
2011-10-26 15:47   ` [PATCH 3/3] xen/gnt{dev, alloc}: reserve event channels for notify Daniel De Graaf
2011-10-27 21:58 ` [PATCH v4 0/3] Add reference counting to grant notify ioctls Daniel De Graaf
2011-10-27 21:58   ` [PATCH 1/3] xen/event: Add reference counting to event channels Daniel De Graaf
2011-10-27 21:58   ` [PATCH 2/3] xen/gntalloc: Change gref_lock to a mutex Daniel De Graaf
2011-10-27 21:58   ` [PATCH 3/3] xen/gnt{dev, alloc}: reserve event channels for notify Daniel De Graaf
2011-11-11 20:33   ` [PATCH v4 0/3] Add reference counting to grant notify ioctls Konrad Rzeszutek Wilk

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=4E742703.7050005@goop.org \
    --to=jeremy@goop.org \
    --cc=Ian.Jackson@eu.citrix.com \
    --cc=dgdegra@tycho.nsa.gov \
    --cc=konrad.wilk@oracle.com \
    --cc=xen-devel@lists.xensource.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.