Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9D697C4338F for ; Tue, 10 Aug 2021 10:13:22 +0000 (UTC) Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 279C460F25 for ; Tue, 10 Aug 2021 10:13:22 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 279C460F25 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=ffwll.ch Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=lists.freedesktop.org Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 5A87689FBC; Tue, 10 Aug 2021 10:13:21 +0000 (UTC) Received: from mail-wm1-x32e.google.com (mail-wm1-x32e.google.com [IPv6:2a00:1450:4864:20::32e]) by gabe.freedesktop.org (Postfix) with ESMTPS id 49F35896EB for ; Tue, 10 Aug 2021 10:13:20 +0000 (UTC) Received: by mail-wm1-x32e.google.com with SMTP id l34-20020a05600c1d22b02902573c214807so1552580wms.2 for ; Tue, 10 Aug 2021 03:13:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ffwll.ch; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:content-transfer-encoding:in-reply-to; bh=O1MLfh0EJ0XIFN1iBr0ImL0ctyq/++4YsTEh2RRbnRw=; b=D/Hs021oHkM9eZC3zomAZqXWZZFaXZt4M7OJTSM2t9I8bjjNsNBPlAjaNZuE/A1M/6 Sr9Nj6Hb18xqTvQ7zZiZthj9JWINbBuLc65eIyAg8WQGDdXJxZQtxWFaEKlomDpyrK5s BHQy+NFspqTawJmvOlBiP2HWZOFoWndmvt5m8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:content-transfer-encoding :in-reply-to; bh=O1MLfh0EJ0XIFN1iBr0ImL0ctyq/++4YsTEh2RRbnRw=; b=VzVg+yGQiwmVcfbMeykPBBtCs9sf+H/USQ6Unw2miTUd2eP68Ped2ZpajaBW0igkEm lmGQ2ihPSi9QIRvZCtgaavkfv2QXvDGrT43z/rNY+f/sDCH4uZ3sJ7JlAmj+mCxefb/P +OqlJhWIA7A+DAKBKQVKlkw3+YbNskxJ9i5bv9UJc3DIhLLSU4sZb6f2g6yoVh02vJdV 2JuWapvqkIhCNvsMlDxQGSPL5yqh6k8On3XYwi+4+VRFufnTXjbTRQWh13LN8DAEAu6u 3USjNRT+09yFE4CrY+Cefkd+PXoM2txLZxA5+k2sGqub0/Fq3REc0b/zpwweIDWcPkTR HIKA== X-Gm-Message-State: AOAM5321Eb/CzStJi2fRF9syO+E/c4AWWmtzPG7blreIkrQ/GLPwgHcn Q9KdX8IJzg02gYlWCW8CkvitVrjudEtU4Q== X-Google-Smtp-Source: ABdhPJxGUmx78Md/8qtPrEerlZ00v/dk7myfhVZBcE0CM2bglCRB1ao0nIHbgqDtmiRmjsuX4FGzTA== X-Received: by 2002:a1c:f203:: with SMTP id s3mr3725493wmc.138.1628590398852; Tue, 10 Aug 2021 03:13:18 -0700 (PDT) Received: from phenom.ffwll.local ([2a02:168:57f4:0:efd0:b9e5:5ae6:c2fa]) by smtp.gmail.com with ESMTPSA id a11sm8546197wrw.67.2021.08.10.03.13.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Aug 2021 03:13:18 -0700 (PDT) Date: Tue, 10 Aug 2021 12:13:16 +0200 From: Daniel Vetter To: Jason Ekstrand Cc: Intel Graphics Development , DRI Development , Daniel Vetter , Chris Wilson , Tvrtko Ursulin , Joonas Lahtinen , Matthew Brost , Matthew Auld , Maarten Lankhorst , Thomas =?iso-8859-1?Q?Hellstr=F6m?= , Lionel Landwerlin , Dave Airlie Message-ID: References: <20210806201852.1345184-1-daniel.vetter@ffwll.ch> <17b2342e218.2817.c6988b7ea6112e3e892765a0d4287e0c@jlekstrand.net> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: X-Operating-System: Linux phenom 5.10.0-7-amd64 Subject: Re: [Intel-gfx] [PATCH] drm/i915: Release ctx->syncobj on final put, not on ctx close X-BeenThere: intel-gfx@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Intel graphics driver community testing & development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: intel-gfx-bounces@lists.freedesktop.org Sender: "Intel-gfx" On Mon, Aug 09, 2021 at 08:47:14PM +0200, Daniel Vetter wrote: > On Sun, Aug 8, 2021 at 2:56 AM Jason Ekstrand wrote: > > > > On August 6, 2021 15:18:59 Daniel Vetter wrote: > > > >> gem context refcounting is another exercise in least locking design it > >> seems, where most things get destroyed upon context closure (which can > >> race with anything really). Only the actual memory allocation and the > >> locks survive while holding a reference. > >> > >> This tripped up Jason when reimplementing the single timeline feature > >> in > >> > >> commit 00dae4d3d35d4f526929633b76e00b0ab4d3970d > >> Author: Jason Ekstrand > >> Date: Thu Jul 8 10:48:12 2021 -0500 > >> > >> drm/i915: Implement SINGLE_TIMELINE with a syncobj (v4) > >> > >> We could fix the bug by holding ctx->mutex, but it's cleaner to just > > > > > > What bug is this fixing, exactly? > > Oh lol I was all busy ranting and not explaining :-) I found it while > auditing other context stuff, so that other patch has the longer > commit message with more history, but that patch is also now tied into > the vm-dercuify, so short summary: You put the syncobj in context > close (i.e. CTX_DESTRY ioctl or close(drmfd)), not in the final > kref_put. Which means you're open to a use-after-free if you race > against an execbuf. ctx->vm is equally broken (but for other ioctl), > once this fix here is merged I send out the ctx->vm fix because that's > tied into the vm-dercuify now due to conflicts. CI caught more, so just explaining what I'm fixing here isn't going to be enough. The troubel is that drm_syncobj_put is now called from very awkward places, and I need to see whether we can fix that. Or whether we need more work_struct (like we have already for i915_address_space) for the final release. -Daniel > -Daniel > > > > > --Jason > > > >> > >> make the context object actually invariant over its _entire_ lifetime. > >> > >> Signed-off-by: Daniel Vetter > >> Fixes: 00dae4d3d35d ("drm/i915: Implement SINGLE_TIMELINE with a syncobj (v4)") > >> Cc: Jason Ekstrand > >> Cc: Chris Wilson > >> Cc: Tvrtko Ursulin > >> Cc: Joonas Lahtinen > >> Cc: Matthew Brost > >> Cc: Matthew Auld > >> Cc: Maarten Lankhorst > >> Cc: "Thomas Hellström" > >> Cc: Lionel Landwerlin > >> Cc: Dave Airlie > >> --- > >> drivers/gpu/drm/i915/gem/i915_gem_context.c | 6 +++--- > >> 1 file changed, 3 insertions(+), 3 deletions(-) > >> > >> diff --git a/drivers/gpu/drm/i915/gem/i915_gem_context.c b/drivers/gpu/drm/i915/gem/i915_gem_context.c > >> index 754b9b8d4981..93ba0197d70a 100644 > >> --- a/drivers/gpu/drm/i915/gem/i915_gem_context.c > >> +++ b/drivers/gpu/drm/i915/gem/i915_gem_context.c > >> @@ -940,6 +940,9 @@ void i915_gem_context_release(struct kref *ref) > >> trace_i915_context_free(ctx); > >> GEM_BUG_ON(!i915_gem_context_is_closed(ctx)); > >> > >> + if (ctx->syncobj) > >> + drm_syncobj_put(ctx->syncobj); > >> + > >> mutex_destroy(&ctx->engines_mutex); > >> mutex_destroy(&ctx->lut_mutex); > >> > >> @@ -1159,9 +1162,6 @@ static void context_close(struct i915_gem_context *ctx) > >> if (vm) > >> i915_vm_close(vm); > >> > >> - if (ctx->syncobj) > >> - drm_syncobj_put(ctx->syncobj); > >> - > >> ctx->file_priv = ERR_PTR(-EBADF); > >> > >> /* > >> -- > >> 2.32.0 > > > > > > > -- > Daniel Vetter > Software Engineer, Intel Corporation > http://blog.ffwll.ch -- Daniel Vetter Software Engineer, Intel Corporation http://blog.ffwll.ch