From patchwork Tue Dec 18 21:25:10 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Vetter X-Patchwork-Id: 1893691 Return-Path: X-Original-To: patchwork-dri-devel@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork1.kernel.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) by patchwork1.kernel.org (Postfix) with ESMTP id B51793FCD4 for ; Tue, 18 Dec 2012 21:32:30 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id A1A39E6266 for ; Tue, 18 Dec 2012 13:32:30 -0800 (PST) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mail-ea0-f170.google.com (mail-ea0-f170.google.com [209.85.215.170]) by gabe.freedesktop.org (Postfix) with ESMTP id 3FA49E5ED7 for ; Tue, 18 Dec 2012 13:25:31 -0800 (PST) Received: by mail-ea0-f170.google.com with SMTP id d11so485626eaa.15 for ; Tue, 18 Dec 2012 13:25:30 -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=IhYacoiy2kS3aueTuKx4vVKTHe2SxDRalEeZwU1819A=; b=TAGXDX3SrAkHSJXTeS2P/lS6y2VvOqVcNoFH+hHYgIE8aoCI5yahWQ2su82L15Y7L7 cY79gimsCLBD7C5H8pwJipmSmRrOWTpqs47Gj0ZM+DrnjBQYRxSwiIHdAjrcHi7v3M/k kfInPjTyAlHS2iF2HNlw8HbWteG0rsA39Kbjg= 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=IhYacoiy2kS3aueTuKx4vVKTHe2SxDRalEeZwU1819A=; b=QUncf6fL0S6M7BRxOmU6DGc6wsPfLkVsR0ooNZ2201/xkdQ/+qAogtznnEHgHQakjj CVCUiNXVvosILxZMZ1GTCphO4tvaRZqgTJ+V35ceI8IRMONyhrTSgo6rYnPxNPTGdMtr 7EAQVrNyyNf1L6ZqMkI8H8MVIQgM0QBj8N8aJgqA/G2AbOKy5ACqeFt19wlNlhEhfGMM DVEYxZVUDj2aYbsDCqikA3FEiDew2gkWcmtx+dPDN+lbakjh14hb6nEI4OzKF2W8agDb ToBfF9y1K8aeXOYE9yWU557uIHQWTR0TwXRqDziiZX1xxUeeREskIvayJvitZwWnS/8Y AUMw== X-Received: by 10.14.215.194 with SMTP id e42mr8873184eep.32.1355865930241; Tue, 18 Dec 2012 13:25:30 -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 w3sm5549337eel.17.2012.12.18.13.25.29 (version=TLSv1/SSLv3 cipher=OTHER); Tue, 18 Dec 2012 13:25:29 -0800 (PST) From: Daniel Vetter To: DRI Development Subject: [PATCH 07/10] drm/nouveau: try to protect nbo->pin_refcount Date: Tue, 18 Dec 2012 22:25:10 +0100 Message-Id: <1355865913-14858-8-git-send-email-daniel.vetter@ffwll.ch> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1355865913-14858-1-git-send-email-daniel.vetter@ffwll.ch> References: <1355865913-14858-1-git-send-email-daniel.vetter@ffwll.ch> X-Gm-Message-State: ALoCoQmyLYf6eg5T96sZo8kPnux1EMRJSOK0Mi/hpqZBwxeqVE1ANFDqf7sA+hO/9GtvDUw47VXU 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 5614c89..1eb5c77 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;