[PATCH 2/4] drm/vmwgfx: Defer fence irq processing to a tasklet

Daniel Vetter daniel at ffwll.ch
Fri Oct 30 03:23:24 PDT 2015


On Fri, Oct 30, 2015 at 02:42:44AM -0700, Thomas Hellstrom wrote:
> Reduce the time in hardware irq context and hardware irq latency.
> 
> Signed-off-by: Thomas Hellstrom <thellstrom at vmware.com>
> Reviewed-by: Sinclair Yeh <syeh at vmware.com>
> ---
>  drivers/gpu/drm/vmwgfx/vmwgfx_fence.c | 108 ++++++++++++++++++++--------------
>  drivers/gpu/drm/vmwgfx/vmwgfx_fence.h |   2 +
>  drivers/gpu/drm/vmwgfx/vmwgfx_irq.c   |   6 +-
>  3 files changed, 68 insertions(+), 48 deletions(-)
> 
> diff --git a/drivers/gpu/drm/vmwgfx/vmwgfx_fence.c b/drivers/gpu/drm/vmwgfx/vmwgfx_fence.c
> index 8e689b4..f40c36e 100644
> --- a/drivers/gpu/drm/vmwgfx/vmwgfx_fence.c
> +++ b/drivers/gpu/drm/vmwgfx/vmwgfx_fence.c
> @@ -47,6 +47,7 @@ struct vmw_fence_manager {
>  	bool seqno_valid; /* Protected by @lock, and may not be set to true
>  			     without the @goal_irq_mutex held. */
>  	unsigned ctx;
> +	struct tasklet_struct tasklet;

Bottom halves are super-deprecated except for giant existing users like
networking. I think the recommended way to do this is to either use
threaded interrupts or work-queues. The reason for that seems to be that
locking is funky around them, which is a major pain for RT. And RT is
going mainline now for real.
-Daniel

>  };
>  
>  struct vmw_user_fence {
> @@ -81,6 +82,8 @@ struct vmw_event_fence_action {
>  	uint32_t *tv_usec;
>  };
>  
> +static void vmw_fence_tasklet(unsigned long data);
> +
>  static struct vmw_fence_manager *
>  fman_from_fence(struct vmw_fence_obj *fence)
>  {
> @@ -115,12 +118,11 @@ static void vmw_fence_obj_destroy(struct fence *f)
>  		container_of(f, struct vmw_fence_obj, base);
>  
>  	struct vmw_fence_manager *fman = fman_from_fence(fence);
> -	unsigned long irq_flags;
>  
> -	spin_lock_irqsave(&fman->lock, irq_flags);
> +	spin_lock_bh(&fman->lock);
>  	list_del_init(&fence->head);
>  	--fman->num_fence_objects;
> -	spin_unlock_irqrestore(&fman->lock, irq_flags);
> +	spin_unlock_bh(&fman->lock);
>  	fence->destroy(fence);
>  }
>  
> @@ -177,7 +179,6 @@ static long vmw_fence_wait(struct fence *f, bool intr, signed long timeout)
>  	struct vmw_private *dev_priv = fman->dev_priv;
>  	struct vmwgfx_wait_cb cb;
>  	long ret = timeout;
> -	unsigned long irq_flags;
>  
>  	if (likely(vmw_fence_obj_signaled(fence)))
>  		return timeout;
> @@ -185,7 +186,7 @@ static long vmw_fence_wait(struct fence *f, bool intr, signed long timeout)
>  	vmw_fifo_ping_host(dev_priv, SVGA_SYNC_GENERIC);
>  	vmw_seqno_waiter_add(dev_priv);
>  
> -	spin_lock_irqsave(f->lock, irq_flags);
> +	spin_lock_bh(f->lock);
>  
>  	if (intr && signal_pending(current)) {
>  		ret = -ERESTARTSYS;
> @@ -205,11 +206,11 @@ static long vmw_fence_wait(struct fence *f, bool intr, signed long timeout)
>  			__set_current_state(TASK_INTERRUPTIBLE);
>  		else
>  			__set_current_state(TASK_UNINTERRUPTIBLE);
> -		spin_unlock_irqrestore(f->lock, irq_flags);
> +		spin_unlock_bh(f->lock);
>  
>  		ret = schedule_timeout(ret);
>  
> -		spin_lock_irqsave(f->lock, irq_flags);
> +		spin_lock_bh(f->lock);
>  		if (ret > 0 && intr && signal_pending(current))
>  			ret = -ERESTARTSYS;
>  	}
> @@ -219,7 +220,7 @@ static long vmw_fence_wait(struct fence *f, bool intr, signed long timeout)
>  	__set_current_state(TASK_RUNNING);
>  
>  out:
> -	spin_unlock_irqrestore(f->lock, irq_flags);
> +	spin_unlock_bh(f->lock);
>  
>  	vmw_seqno_waiter_remove(dev_priv);
>  
> @@ -300,21 +301,22 @@ struct vmw_fence_manager *vmw_fence_manager_init(struct vmw_private *dev_priv)
>  		ttm_round_pot(sizeof(struct vmw_event_fence_action));
>  	mutex_init(&fman->goal_irq_mutex);
>  	fman->ctx = fence_context_alloc(1);
> +	tasklet_init(&fman->tasklet, vmw_fence_tasklet,
> +		     (unsigned long) fman);
>  
>  	return fman;
>  }
>  
>  void vmw_fence_manager_takedown(struct vmw_fence_manager *fman)
>  {
> -	unsigned long irq_flags;
>  	bool lists_empty;
>  
>  	(void) cancel_work_sync(&fman->work);
>  
> -	spin_lock_irqsave(&fman->lock, irq_flags);
> +	spin_lock_bh(&fman->lock);
>  	lists_empty = list_empty(&fman->fence_list) &&
>  		list_empty(&fman->cleanup_list);
> -	spin_unlock_irqrestore(&fman->lock, irq_flags);
> +	spin_unlock_bh(&fman->lock);
>  
>  	BUG_ON(!lists_empty);
>  	kfree(fman);
> @@ -324,7 +326,6 @@ static int vmw_fence_obj_init(struct vmw_fence_manager *fman,
>  			      struct vmw_fence_obj *fence, u32 seqno,
>  			      void (*destroy) (struct vmw_fence_obj *fence))
>  {
> -	unsigned long irq_flags;
>  	int ret = 0;
>  
>  	fence_init(&fence->base, &vmw_fence_ops, &fman->lock,
> @@ -332,7 +333,7 @@ static int vmw_fence_obj_init(struct vmw_fence_manager *fman,
>  	INIT_LIST_HEAD(&fence->seq_passed_actions);
>  	fence->destroy = destroy;
>  
> -	spin_lock_irqsave(&fman->lock, irq_flags);
> +	spin_lock_bh(&fman->lock);
>  	if (unlikely(fman->fifo_down)) {
>  		ret = -EBUSY;
>  		goto out_unlock;
> @@ -341,7 +342,7 @@ static int vmw_fence_obj_init(struct vmw_fence_manager *fman,
>  	++fman->num_fence_objects;
>  
>  out_unlock:
> -	spin_unlock_irqrestore(&fman->lock, irq_flags);
> +	spin_unlock_bh(&fman->lock);
>  	return ret;
>  
>  }
> @@ -490,11 +491,9 @@ rerun:
>  
>  void vmw_fences_update(struct vmw_fence_manager *fman)
>  {
> -	unsigned long irq_flags;
> -
> -	spin_lock_irqsave(&fman->lock, irq_flags);
> +	spin_lock_bh(&fman->lock);
>  	__vmw_fences_update(fman);
> -	spin_unlock_irqrestore(&fman->lock, irq_flags);
> +	spin_unlock_bh(&fman->lock);
>  }
>  
>  bool vmw_fence_obj_signaled(struct vmw_fence_obj *fence)
> @@ -694,11 +693,9 @@ void vmw_fence_fifo_down(struct vmw_fence_manager *fman)
>  
>  void vmw_fence_fifo_up(struct vmw_fence_manager *fman)
>  {
> -	unsigned long irq_flags;
> -
> -	spin_lock_irqsave(&fman->lock, irq_flags);
> +	spin_lock_bh(&fman->lock);
>  	fman->fifo_down = false;
> -	spin_unlock_irqrestore(&fman->lock, irq_flags);
> +	spin_unlock_bh(&fman->lock);
>  }
>  
>  
> @@ -825,10 +822,9 @@ void vmw_event_fence_fpriv_gone(struct vmw_fence_manager *fman,
>  {
>  	struct vmw_event_fence_action *eaction;
>  	struct drm_pending_event *event;
> -	unsigned long irq_flags;
>  
>  	while (1) {
> -		spin_lock_irqsave(&fman->lock, irq_flags);
> +		spin_lock_bh(&fman->lock);
>  		if (list_empty(event_list))
>  			goto out_unlock;
>  		eaction = list_first_entry(event_list,
> @@ -837,11 +833,11 @@ void vmw_event_fence_fpriv_gone(struct vmw_fence_manager *fman,
>  		list_del_init(&eaction->fpriv_head);
>  		event = eaction->event;
>  		eaction->event = NULL;
> -		spin_unlock_irqrestore(&fman->lock, irq_flags);
> +		spin_unlock_bh(&fman->lock);
>  		event->destroy(event);
>  	}
>  out_unlock:
> -	spin_unlock_irqrestore(&fman->lock, irq_flags);
> +	spin_unlock_bh(&fman->lock);
>  }
>  
>  
> @@ -854,7 +850,7 @@ out_unlock:
>   * This function is called when the seqno of the fence where @action is
>   * attached has passed. It queues the event on the submitter's event list.
>   * This function is always called from atomic context, and may be called
> - * from irq context.
> + * from tasklet context.
>   */
>  static void vmw_event_fence_action_seq_passed(struct vmw_fence_action *action)
>  {
> @@ -863,13 +859,12 @@ static void vmw_event_fence_action_seq_passed(struct vmw_fence_action *action)
>  	struct drm_device *dev = eaction->dev;
>  	struct drm_pending_event *event = eaction->event;
>  	struct drm_file *file_priv;
> -	unsigned long irq_flags;
>  
>  	if (unlikely(event == NULL))
>  		return;
>  
>  	file_priv = event->file_priv;
> -	spin_lock_irqsave(&dev->event_lock, irq_flags);
> +	spin_lock_bh(&dev->event_lock);
>  
>  	if (likely(eaction->tv_sec != NULL)) {
>  		struct timeval tv;
> @@ -883,7 +878,7 @@ static void vmw_event_fence_action_seq_passed(struct vmw_fence_action *action)
>  	list_add_tail(&eaction->event->link, &file_priv->event_list);
>  	eaction->event = NULL;
>  	wake_up_all(&file_priv->event_wait);
> -	spin_unlock_irqrestore(&dev->event_lock, irq_flags);
> +	spin_unlock_bh(&dev->event_lock);
>  }
>  
>  /**
> @@ -900,11 +895,10 @@ static void vmw_event_fence_action_cleanup(struct vmw_fence_action *action)
>  	struct vmw_event_fence_action *eaction =
>  		container_of(action, struct vmw_event_fence_action, action);
>  	struct vmw_fence_manager *fman = fman_from_fence(eaction->fence);
> -	unsigned long irq_flags;
>  
> -	spin_lock_irqsave(&fman->lock, irq_flags);
> +	spin_lock_bh(&fman->lock);
>  	list_del(&eaction->fpriv_head);
> -	spin_unlock_irqrestore(&fman->lock, irq_flags);
> +	spin_unlock_bh(&fman->lock);
>  
>  	vmw_fence_obj_unreference(&eaction->fence);
>  	kfree(eaction);
> @@ -924,11 +918,10 @@ static void vmw_fence_obj_add_action(struct vmw_fence_obj *fence,
>  			      struct vmw_fence_action *action)
>  {
>  	struct vmw_fence_manager *fman = fman_from_fence(fence);
> -	unsigned long irq_flags;
>  	bool run_update = false;
>  
>  	mutex_lock(&fman->goal_irq_mutex);
> -	spin_lock_irqsave(&fman->lock, irq_flags);
> +	spin_lock_bh(&fman->lock);
>  
>  	fman->pending_actions[action->type]++;
>  	if (fence_is_signaled_locked(&fence->base)) {
> @@ -947,7 +940,7 @@ static void vmw_fence_obj_add_action(struct vmw_fence_obj *fence,
>  		run_update = vmw_fence_goal_check_locked(fence);
>  	}
>  
> -	spin_unlock_irqrestore(&fman->lock, irq_flags);
> +	spin_unlock_bh(&fman->lock);
>  
>  	if (run_update) {
>  		if (!fman->goal_irq_on) {
> @@ -985,7 +978,6 @@ int vmw_event_fence_action_queue(struct drm_file *file_priv,
>  	struct vmw_event_fence_action *eaction;
>  	struct vmw_fence_manager *fman = fman_from_fence(fence);
>  	struct vmw_fpriv *vmw_fp = vmw_fpriv(file_priv);
> -	unsigned long irq_flags;
>  
>  	eaction = kzalloc(sizeof(*eaction), GFP_KERNEL);
>  	if (unlikely(eaction == NULL))
> @@ -1002,9 +994,9 @@ int vmw_event_fence_action_queue(struct drm_file *file_priv,
>  	eaction->tv_sec = tv_sec;
>  	eaction->tv_usec = tv_usec;
>  
> -	spin_lock_irqsave(&fman->lock, irq_flags);
> +	spin_lock_bh(&fman->lock);
>  	list_add_tail(&eaction->fpriv_head, &vmw_fp->fence_events);
> -	spin_unlock_irqrestore(&fman->lock, irq_flags);
> +	spin_unlock_bh(&fman->lock);
>  
>  	vmw_fence_obj_add_action(fence, &eaction->action);
>  
> @@ -1025,16 +1017,15 @@ static int vmw_event_fence_action_create(struct drm_file *file_priv,
>  	struct vmw_event_fence_pending *event;
>  	struct vmw_fence_manager *fman = fman_from_fence(fence);
>  	struct drm_device *dev = fman->dev_priv->dev;
> -	unsigned long irq_flags;
>  	int ret;
>  
> -	spin_lock_irqsave(&dev->event_lock, irq_flags);
> +	spin_lock_bh(&dev->event_lock);
>  
>  	ret = (file_priv->event_space < sizeof(event->event)) ? -EBUSY : 0;
>  	if (likely(ret == 0))
>  		file_priv->event_space -= sizeof(event->event);
>  
> -	spin_unlock_irqrestore(&dev->event_lock, irq_flags);
> +	spin_unlock_bh(&dev->event_lock);
>  
>  	if (unlikely(ret != 0)) {
>  		DRM_ERROR("Failed to allocate event space for this file.\n");
> @@ -1078,9 +1069,9 @@ static int vmw_event_fence_action_create(struct drm_file *file_priv,
>  out_no_queue:
>  	event->base.destroy(&event->base);
>  out_no_event:
> -	spin_lock_irqsave(&dev->event_lock, irq_flags);
> +	spin_lock_bh(&dev->event_lock);
>  	file_priv->event_space += sizeof(*event);
> -	spin_unlock_irqrestore(&dev->event_lock, irq_flags);
> +	spin_unlock_bh(&dev->event_lock);
>  out_no_space:
>  	return ret;
>  }
> @@ -1172,3 +1163,32 @@ out_no_ref_obj:
>  	vmw_fence_obj_unreference(&fence);
>  	return ret;
>  }
> +
> +/**
> + * vmw_fence_tasklet - Fence manager tasklet entry point
> + *
> + * @data: The tasklet closure - A pointer to the fence manager cast to an
> + * unsigned long.
> + */
> +static void vmw_fence_tasklet(unsigned long data)
> +{
> +	struct vmw_fence_manager *fman = (struct vmw_fence_manager *) data;
> +
> +	spin_lock(&fman->lock);
> +	__vmw_fences_update(fman);
> +	spin_unlock(&fman->lock);
> +	wake_up_all(&fman->dev_priv->fence_queue);
> +}
> +
> +/**
> + * vmw_fence_tasklet_schedule - Schedule a fence manager tasklet run
> + *
> + * @fman: Pointer to a fence manager
> + */
> +void vmw_fence_tasklet_schedule(struct vmw_fence_manager *fman)
> +{
> +	if (!fman)
> +		return;
> +
> +	tasklet_schedule(&fman->tasklet);
> +}
> diff --git a/drivers/gpu/drm/vmwgfx/vmwgfx_fence.h b/drivers/gpu/drm/vmwgfx/vmwgfx_fence.h
> index 8be6c29..e55b2c9 100644
> --- a/drivers/gpu/drm/vmwgfx/vmwgfx_fence.h
> +++ b/drivers/gpu/drm/vmwgfx/vmwgfx_fence.h
> @@ -124,4 +124,6 @@ extern int vmw_event_fence_action_queue(struct drm_file *filee_priv,
>  					uint32_t *tv_sec,
>  					uint32_t *tv_usec,
>  					bool interruptible);
> +extern void vmw_fence_tasklet_schedule(struct vmw_fence_manager *fman);
> +
>  #endif /* _VMWGFX_FENCE_H_ */
> diff --git a/drivers/gpu/drm/vmwgfx/vmwgfx_irq.c b/drivers/gpu/drm/vmwgfx/vmwgfx_irq.c
> index ac3eccd..b0a6e65 100644
> --- a/drivers/gpu/drm/vmwgfx/vmwgfx_irq.c
> +++ b/drivers/gpu/drm/vmwgfx/vmwgfx_irq.c
> @@ -48,10 +48,8 @@ irqreturn_t vmw_irq_handler(int irq, void *arg)
>  		return IRQ_NONE;
>  
>  	if (masked_status & (SVGA_IRQFLAG_ANY_FENCE |
> -			     SVGA_IRQFLAG_FENCE_GOAL)) {
> -		vmw_fences_update(dev_priv->fman);
> -		wake_up_all(&dev_priv->fence_queue);
> -	}
> +			     SVGA_IRQFLAG_FENCE_GOAL))
> +		vmw_fence_tasklet_schedule(dev_priv->fman);
>  
>  	if (masked_status & SVGA_IRQFLAG_FIFO_PROGRESS)
>  		wake_up_all(&dev_priv->fifo_queue);
> -- 
> 2.4.3
> 
> _______________________________________________
> dri-devel mailing list
> dri-devel at lists.freedesktop.org
> http://lists.freedesktop.org/mailman/listinfo/dri-devel

-- 
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch


More information about the dri-devel mailing list