From patchwork Thu Jan 10 20:47:48 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Vetter X-Patchwork-Id: 1962421 Return-Path: X-Original-To: patchwork-dri-devel@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork2.kernel.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) by patchwork2.kernel.org (Postfix) with ESMTP id EB931DF264 for ; Thu, 10 Jan 2013 20:56:15 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id C5681E64F9 for ; Thu, 10 Jan 2013 12:56:15 -0800 (PST) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mail-wg0-f43.google.com (mail-wg0-f43.google.com [74.125.82.43]) by gabe.freedesktop.org (Postfix) with ESMTP id C4539E63A8 for ; Thu, 10 Jan 2013 12:48:33 -0800 (PST) Received: by mail-wg0-f43.google.com with SMTP id e12so491997wge.22 for ; Thu, 10 Jan 2013 12:48:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ffwll.ch; s=google; h=x-received:from:to:cc:subject:date:message-id:x-mailer:in-reply-to :references; bh=r4hoO2uDB3giBbUPNaI3h6Ak4u9STCw6ftIf2ewt+vs=; b=MDjbj5fo7hjcRV+9GWftPf6zlfILM6pM3KZBOzY9dKZ37ofAem07JB+LYyjQyFBWOQ NN6IoJBJ2aV8Hi/vyN9fU5AyVn5r5lxTpOoKnisvToIrGKVJRci+AwBZ6o17ev8f7i+E Alf/akw+SPEVbV2YXWBWV3fBLvretUuCoaXnw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=x-received:from:to:cc:subject:date:message-id:x-mailer:in-reply-to :references:x-gm-message-state; bh=r4hoO2uDB3giBbUPNaI3h6Ak4u9STCw6ftIf2ewt+vs=; b=ouiawSFRQdmTZRYGo45Mvbz7Rd32Pe7ORKpD0d8DQzuwYAky5J8sZWhjOiSwSDl1Zg alhV6H6V1fSFuwFD6w4+haHlF3vGGAzHNreDGfCkhLTKq2KFY1DWxYMkj5vKvg2OfA+n r/abQ/sJePsMFY1DJzCGGiDG6q54+D0vUK6kKcIzvB3UEM626LtjwUGAlBYAwiyhZ6fM XK5QJokX1irrm5zdAG04W/yZWWjTG3QARqMAXa7wRhLF4mk7hycLuKHlka+bvf1KiJd+ GtzZk7h7P1ZLn6bCWAGVmtz/N8roJQie3LGfSa1t6O9mL1ktAM0rh9gS9vL98h8AB8+2 2Weg== X-Received: by 10.180.14.2 with SMTP id l2mr11606842wic.2.1357850913067; Thu, 10 Jan 2013 12:48:33 -0800 (PST) Received: from biers.ffwll.local (178-83-130-250.dynamic.hispeed.ch. [178.83.130.250]) by mx.google.com with ESMTPS id hu8sm9975655wib.6.2013.01.10.12.48.32 (version=TLSv1.2 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 10 Jan 2013 12:48:32 -0800 (PST) From: Daniel Vetter To: DRI Development Subject: [PATCH 07/35] drm/nouveau: try to protect nbo->pin_refcount Date: Thu, 10 Jan 2013 21:47:48 +0100 Message-Id: <1357850897-27102-8-git-send-email-daniel.vetter@ffwll.ch> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1357850897-27102-1-git-send-email-daniel.vetter@ffwll.ch> References: <1357850897-27102-1-git-send-email-daniel.vetter@ffwll.ch> X-Gm-Message-State: ALoCoQn8zILWQN925EvYFcbHy/tZB0igcbG04VPKtXUS5GPG4yfKj4Z9+iwFRutk7zemCiUxmbgQ Cc: Daniel Vetter X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: dri-devel-bounces+patchwork-dri-devel=patchwork.kernel.org@lists.freedesktop.org Errors-To: dri-devel-bounces+patchwork-dri-devel=patchwork.kernel.org@lists.freedesktop.org ... by moving the bo_pin/bo_unpin manipulation of the pin_refcount under the protection of the ttm reservation lock. pin/unpin seems to get called from all over the place, so atm this is completely racy. After this patch there are only a few places in cleanup functions left which access ->pin_refcount without locking. But I'm hoping that those are safe and some other code invariant guarantees that this won't blow up. In any case, I only need to fix up pin/unpin to make ->pageflip work safely, so let's keep it at that. Add a comment to the header to explain the new locking rule. Signed-off-by: Daniel Vetter --- drivers/gpu/drm/nouveau/nouveau_bo.c | 22 +++++++++++----------- drivers/gpu/drm/nouveau/nouveau_bo.h | 2 ++ 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/drivers/gpu/drm/nouveau/nouveau_bo.c b/drivers/gpu/drm/nouveau/nouveau_bo.c index 69d7b1d..64d6e304 100644 --- a/drivers/gpu/drm/nouveau/nouveau_bo.c +++ b/drivers/gpu/drm/nouveau/nouveau_bo.c @@ -300,17 +300,18 @@ nouveau_bo_pin(struct nouveau_bo *nvbo, uint32_t memtype) struct ttm_buffer_object *bo = &nvbo->bo; int ret; + ret = ttm_bo_reserve(bo, false, false, false, 0); + if (ret) + goto out; + if (nvbo->pin_refcnt && !(memtype & (1 << bo->mem.mem_type))) { NV_ERROR(drm, "bo %p pinned elsewhere: 0x%08x vs 0x%08x\n", bo, 1 << bo->mem.mem_type, memtype); - return -EINVAL; + ret = -EINVAL; + goto out; } if (nvbo->pin_refcnt++) - return 0; - - ret = ttm_bo_reserve(bo, false, false, false, 0); - if (ret) goto out; nouveau_bo_placement_set(nvbo, memtype, 0); @@ -328,10 +329,8 @@ nouveau_bo_pin(struct nouveau_bo *nvbo, uint32_t memtype) break; } } - ttm_bo_unreserve(bo); out: - if (unlikely(ret)) - nvbo->pin_refcnt--; + ttm_bo_unreserve(bo); return ret; } @@ -342,13 +341,13 @@ nouveau_bo_unpin(struct nouveau_bo *nvbo) struct ttm_buffer_object *bo = &nvbo->bo; int ret; - if (--nvbo->pin_refcnt) - return 0; - ret = ttm_bo_reserve(bo, false, false, false, 0); if (ret) return ret; + if (--nvbo->pin_refcnt) + goto out; + nouveau_bo_placement_set(nvbo, bo->mem.placement, 0); ret = nouveau_bo_validate(nvbo, false, false); @@ -365,6 +364,7 @@ nouveau_bo_unpin(struct nouveau_bo *nvbo) } } +out: ttm_bo_unreserve(bo); return ret; } diff --git a/drivers/gpu/drm/nouveau/nouveau_bo.h b/drivers/gpu/drm/nouveau/nouveau_bo.h index 25ca379..81d00fe 100644 --- a/drivers/gpu/drm/nouveau/nouveau_bo.h +++ b/drivers/gpu/drm/nouveau/nouveau_bo.h @@ -28,6 +28,8 @@ struct nouveau_bo { struct nouveau_drm_tile *tile; struct drm_gem_object *gem; + + /* protect by the ttm reservation lock */ int pin_refcnt; struct ttm_bo_kmap_obj dma_buf_vmap;