From patchwork Sat Aug 9 20:15:16 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ben Widawsky X-Patchwork-Id: 4703311 Return-Path: X-Original-To: patchwork-intel-gfx@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 3BC839F377 for ; Sat, 9 Aug 2014 20:13:15 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 56DC92013D for ; Sat, 9 Aug 2014 20:13:14 +0000 (UTC) Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) by mail.kernel.org (Postfix) with ESMTP id 7ED6520138 for ; Sat, 9 Aug 2014 20:13:12 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 2439D893C0; Sat, 9 Aug 2014 13:13:12 -0700 (PDT) X-Original-To: intel-gfx@lists.freedesktop.org Delivered-To: intel-gfx@lists.freedesktop.org Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by gabe.freedesktop.org (Postfix) with ESMTP id 6CF9F893C0 for ; Sat, 9 Aug 2014 13:13:11 -0700 (PDT) Received: from orsmga002.jf.intel.com ([10.7.209.21]) by orsmga101.jf.intel.com with ESMTP; 09 Aug 2014 13:13:11 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.01,833,1400050800"; d="scan'208";a="585869144" Received: from statham.jf.intel.com ([10.7.197.60]) by orsmga002.jf.intel.com with ESMTP; 09 Aug 2014 13:13:10 -0700 From: Ben Widawsky To: Intel GFX Date: Sat, 9 Aug 2014 13:15:16 -0700 Message-Id: <1407615316-30645-1-git-send-email-benjamin.widawsky@intel.com> X-Mailer: git-send-email 2.0.4 In-Reply-To: <1404238671-18760-5-git-send-email-benjamin.widawsky@intel.com> References: <1404238671-18760-5-git-send-email-benjamin.widawsky@intel.com> Cc: Ben Widawsky , Ben Widawsky Subject: [Intel-gfx] [PATCH] [v2] drm/i915: Fix another another use-after-free in do_switch X-BeenThere: intel-gfx@lists.freedesktop.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: Intel graphics driver community testing & development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: intel-gfx-bounces@lists.freedesktop.org Sender: "Intel-gfx" X-Spam-Status: No, score=-4.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_MED, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP See the following for many more details. commit acc240d41ea1ab9c488a79219fb313b5b46265ae Author: Daniel Vetter Date: Thu Dec 5 15:42:34 2013 +0100 drm/i915: Fix use-after-free in do_switch In this case, the issue is only for full PPGTT: do_switch context_unref ppgtt_release i915_gpu_idle switch_to_default from changes to default context This could be backported to the pre do_switch cleanup I did in this series. However, it's much cleaner and more obvious as a patch on top, so I'd really like to do this as a post cleanup patch. v2: There was a bug in the original patch where the ring->last_context was set too early. I am not sure how this wasn't being hit when I sent this previously. Perhaps I tested the wrong patch previously. Signed-off-by: Ben Widawsky Reviewed-by: Chris Wilson --- drivers/gpu/drm/i915/i915_gem_context.c | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/drivers/gpu/drm/i915/i915_gem_context.c b/drivers/gpu/drm/i915/i915_gem_context.c index c9aa3e6..0ce8fc9 100644 --- a/drivers/gpu/drm/i915/i915_gem_context.c +++ b/drivers/gpu/drm/i915/i915_gem_context.c @@ -609,14 +609,18 @@ mi_set_context(struct intel_engine_cs *ring, return ret; } -static void do_switch_fini_common(struct intel_engine_cs *ring, - struct intel_context *from, - struct intel_context *to) +static struct intel_context *do_switch_fini_common(struct intel_engine_cs *ring, + struct intel_context *from, + struct intel_context *to) { + struct intel_context *ret; if (likely(from)) i915_gem_context_unreference(from); i915_gem_context_reference(to); + ret = ring->last_context; ring->last_context = to; + + return ret; } static int do_switch_xcs(struct intel_engine_cs *ring, @@ -762,14 +766,20 @@ static int do_switch_rcs(struct intel_engine_cs *ring, */ from->legacy_hw_ctx.rcs_state->dirty = 1; BUG_ON(from->legacy_hw_ctx.rcs_state->ring != ring); - - /* obj is kept alive until the next request by its active ref */ - i915_gem_object_ggtt_unpin(from->legacy_hw_ctx.rcs_state); } uninitialized = !to->legacy_hw_ctx.initialized && from == NULL; to->legacy_hw_ctx.initialized = true; - do_switch_fini_common(ring, from, to); + /* From may have disappeared again after the context unref */ + from = do_switch_fini_common(ring, from, to); + if (from != NULL) { + /* obj is kept alive until the next request by its active ref. + * XXX: The context needs to be unpinned last, or else we risk + * hitting evict/idle on the ppgtt free, which will call back + * into this, and we'll get a double unpin on this context + */ + i915_gem_object_ggtt_unpin(from->legacy_hw_ctx.rcs_state); + } if (uninitialized) { ret = i915_gem_render_state_init(ring);