Received: by 2002:a05:6358:489b:b0:bb:da1:e618 with SMTP id x27csp2270391rwn; Fri, 9 Sep 2022 10:54:35 -0700 (PDT) X-Google-Smtp-Source: AA6agR5KiGvuhMxG+XqFI+cpX+bJs5Oo4QKh4INGFZf2wf911KCp66t0ihUmqWWCxi1RGLJ1V6Xo X-Received: by 2002:a05:6402:378f:b0:43a:d3f5:79f2 with SMTP id et15-20020a056402378f00b0043ad3f579f2mr12592842edb.338.1662746075365; Fri, 09 Sep 2022 10:54:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662746075; cv=none; d=google.com; s=arc-20160816; b=O6fviR67e9x7T3CnfD/QGv0m0S7ugzpnTlXnH0HxD8Pi6+wGfir7XlkwhzjtwZ8q6W LEd8m6qlYwoF5wskkUY7dx/mPbavP3Wz+Fspxc7GqXFGuGVNGNt4hxbMu5Z3xztckm3N QbN5FV+CCApQ0hZHxAK52u2HKcLmXLG8o6TIFdUxP7sXLoXiX/o2uFsGeDCJTyAJ48vN IgqdFsZPAnoIb+Xav2m68BKOpBdKKgOpD65PdvJ3YG30vYFGPHDEUSlLOI+4UL5ob3og RVzIMcTligQhbgZZo0OttWMe2HcCJEejgJgLVr1TWuarchZU/EOPiwoHAdyD4sQREqMC TiZw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature; bh=IMbh/Kn6kRwYBjxB6sF63qVP4izNKSMNJqHkuSThfMI=; b=wNs7Cap/sSuh6gbGruEdSb2/XEPnjplfIVEZP0eoMvMf6ospuyndB+H/a26cCi2oCB RWlu1wE390+5YeHsqJlV0ImeqZSpaB1l8ikcduMYVm2ByRlpTDIv3MQejxgJiqomxqlu Wy8xNLGw0C3KxbjG/1O1bKANStYdKr78RP3JIxbE4vP7mN8kamn82YJeHLtdtGiarO5d aJQidAOe1su+m9rbStagSsDizXPp52Zg6D/Qc+4ZIJ2Ga/W7NDLPnJXfVy20qXjAOIU7 6TG9LpftjAPw8badMkGs40fxrFa7BA0nI/y7N5mMHCuEqN1CIblOHpeuxbJx7c6gw80w uk7g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=E1pvbfsh; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=collabora.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q16-20020a056402519000b00447a7af56b2si1090413edd.326.2022.09.09.10.54.10; Fri, 09 Sep 2022 10:54:35 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=E1pvbfsh; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=collabora.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231616AbiIIRhC (ORCPT + 99 others); Fri, 9 Sep 2022 13:37:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50678 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229892AbiIIRg6 (ORCPT ); Fri, 9 Sep 2022 13:36:58 -0400 Received: from madras.collabora.co.uk (madras.collabora.co.uk [IPv6:2a00:1098:0:82:1000:25:2eeb:e5ab]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6039B135D7B; Fri, 9 Sep 2022 10:36:57 -0700 (PDT) Received: from [192.168.2.145] (109-252-119-13.nat.spd-mgts.ru [109.252.119.13]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: dmitry.osipenko) by madras.collabora.co.uk (Postfix) with ESMTPSA id 0A45E6601FC6; Fri, 9 Sep 2022 18:36:51 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1662745015; bh=8xgN4e43LGiBPVdQ7GyKB5jQBCWxBspXnT50kSaZfdc=; h=Date:Subject:To:Cc:References:From:In-Reply-To:From; b=E1pvbfsh4mhx8kHsaeEx5+wdORPcsEFHoldFIWWyXseblN1dtO3m2LKuSoqgihlSa mvxWcvwfAY63jSB7DnN7yOeJIhOmHYkexHRg9C30PT7fBMeLWfUKCiYOa08B86oCUs ccnMOiQ/eemk3IQI6BMX/oXA+OGKrl0rBatDrO8+JSqXCkTd/fI+qJU/40kfFMc89g t0gQ9aaRRLRYPyQEByTuKyml5hlPtBl1GC6RbbKtZL44FqTg2qeA3KCDga5aZ0jHda scJFjauo2KMswJRJK5d6rTZTggJc5SFIdfQTgNU1j3sp7y+V1qH4Ha2hAFB1s341R4 qnfx/6JJNgQIg== Message-ID: <588ff12e-d030-0db0-e248-1afd0dee4ae1@collabora.com> Date: Fri, 9 Sep 2022 20:36:48 +0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.12.0 Subject: Re: [PATCH v4 06/21] drm/i915: Prepare to dynamic dma-buf locking specification Content-Language: en-US To: "Ruhl, Michael J" , Dmitry Osipenko , Jani Nikula , Joonas Lahtinen , "Vivi, Rodrigo" , Tvrtko Ursulin , =?UTF-8?Q?Thomas_Hellstr=c3=b6m?= , =?UTF-8?Q?Christian_K=c3=b6nig?= , Chris Wilson Cc: "dri-devel@lists.freedesktop.org" , "linux-kernel@vger.kernel.org" , "linux-media@vger.kernel.org" , "linaro-mm-sig@lists.linaro.org" , "amd-gfx@lists.freedesktop.org" , "intel-gfx@lists.freedesktop.org" , "kernel@collabora.com" , "virtualization@lists.linux-foundation.org" , "linux-rdma@vger.kernel.org" , "linux-arm-msm@vger.kernel.org" , David Airlie , Gerd Hoffmann , Gurchetan Singh , Chia-I Wu , Daniel Vetter , Daniel Almeida , Gert Wollny , Gustavo Padovan , Daniel Stone , Tomeu Vizoso , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , Rob Clark , Sumit Semwal , "Pan, Xinhui" , Thierry Reding , Tomasz Figa , Marek Szyprowski , Mauro Carvalho Chehab , Alex Deucher , Qiang Yu , Srinivas Kandagatla , Amol Maheshwari , Jason Gunthorpe , Leon Romanovsky , "Gross, Jurgen" , Stefano Stabellini , Oleksandr Tyshchenko , Tomi Valkeinen , Russell King , Lucas Stach , Christian Gmeiner References: <20220831153757.97381-1-dmitry.osipenko@collabora.com> <20220831153757.97381-7-dmitry.osipenko@collabora.com> <760b999f-b15d-102e-8bc7-c3e69f07f43f@gmail.com> From: Dmitry Osipenko In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 9/2/22 19:26, Ruhl, Michael J wrote: >> 02.09.2022 13:31, Dmitry Osipenko пишет: >>> 01.09.2022 17:02, Ruhl, Michael J пишет: >>> ... >>>>> --- a/drivers/gpu/drm/i915/gem/i915_gem_object.c >>>>> +++ b/drivers/gpu/drm/i915/gem/i915_gem_object.c >>>>> @@ -331,7 +331,19 @@ static void __i915_gem_free_objects(struct >>>>> drm_i915_private *i915, >>>>> continue; >>>>> } >>>>> >>>>> + /* >>>>> + * dma_buf_unmap_attachment() requires reservation to be >>>>> + * locked. The imported GEM shouldn't share reservation lock, >>>>> + * so it's safe to take the lock. >>>>> + */ >>>>> + if (obj->base.import_attach) >>>>> + i915_gem_object_lock(obj, NULL); >>>> >>>> There is a lot of stuff going here. Taking the lock may be premature... >>>> >>>>> __i915_gem_object_pages_fini(obj); >>>> >>>> The i915_gem_dmabuf.c:i915_gem_object_put_pages_dmabuf is where >>>> unmap_attachment is actually called, would it make more sense to make >>>> do the locking there? >>> >>> The __i915_gem_object_put_pages() is invoked with a held reservation >>> lock, while freeing object is a special time when we know that GEM is >>> unused. >>> >>> The __i915_gem_free_objects() was taking the lock two weeks ago until >>> the change made Chris Wilson [1] reached linux-next. >>> >>> [1] >>> https://git.kernel.org/pub/scm/linux/kernel/git/next/linux- >> next.git/commit/?id=2826d447fbd60e6a05e53d5f918bceb8c04e315c >>> >>> I don't think we can take the lock within >>> i915_gem_object_put_pages_dmabuf(), it may/should deadlock other code >> paths. >> >> On the other hand, we can check whether the GEM's refcount number is >> zero in i915_gem_object_put_pages_dmabuf() and then take the lock if >> it's zero. >> >> Also, seems it should be possible just to bail out from >> i915_gem_object_put_pages_dmabuf() if refcount=0. The further >> drm_prime_gem_destroy() will take care of unmapping. Perhaps this could >> be the best option, I'll give it a test. > > i915_gem_object_put_pages() is uses the SG, and the usage for > drm_prim_gem_destroy() > > from __i915_gem_free_objects() doesn't use the SG because it has been "freed" > already, I am not sure if that would work... > > Hmm.. with that in mind, maybe moving the base.import_attach check to > __i915_gem_object_put_pages with your attach check? I see you meant __i915_gem_object_pages_fini() here. > atomic_set(&obj->mm.pages_pin_count, 0); > if (obj->base.import) > i915_gem_object_lock(obj, NULL); > > __i915_gem_object_put_pages(obj); > > if (obj->base.import) > i915_gem_object_unlock(obj, NULL); > GEM_BUG_ON(i915_gem_object_has_pages(obj)); > > Pretty much one step up from the dmabuf interface, but we are guaranteed to > not have any pinned pages? Importer shouldn't hold pages outside of dma-buf API, otherwise it should be a bug. > The other caller of __i915_gem_object_pages_fini is the i915_ttm move_notify > which should not conflict (export side, not import side). > > Since it appears that not locking during the clean up is desirable, trying to make sure take the lock > is taken at the last moment might be the right path? Reducing the scope of locking usually is preferred more. Yours suggestion works okay, I couldn't spot any problems at least for a non-TTM code paths. It's indeed a bit not nice that __i915_gem_object_pages_fini() is used by TTM, but should be safe for imported objects. Will be great if anyone from i915 maintainers could ack this variant. -- Best regards, Dmitry