Received: by 2002:a05:7412:3784:b0:e2:908c:2ebd with SMTP id jk4csp2789501rdb; Wed, 4 Oct 2023 11:25:18 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFdyjIiMdkY4UgOwxqtbU64iFTDtHSNXRZInVdsxv2kTDylYHFsVwA/HjYln0GBHsuzcINN X-Received: by 2002:a05:6358:880e:b0:143:8e40:917b with SMTP id hv14-20020a056358880e00b001438e40917bmr3191120rwb.9.1696443918484; Wed, 04 Oct 2023 11:25:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696443918; cv=none; d=google.com; s=arc-20160816; b=Uc4ukopAwX24EZ+GkjdvtQMzEbh1ITTMnUV0J4ctOx0YeN4AJLIndUGmb3CnkD9OxL MZ7yVDL2di4csdMH58YLHI+tdOoN9EYB7eYZp2qYthf0KM0qBU84tWPv514U+O25CZOf 2L3bkqFSeeQtCdRf65sSif2YKkJxoKE2OUAsIIAKXCTFHW6O8zFNb8jhmu3XwxUwOH/x mvDoBwUXCawC5EP+gReBXGcouqVchw5QnXwjdY22A3oGdqBunUINUMz4lrSpmJPf29WL tR2SqDUVUvp5QRrXVuDQL3g71Ie+cXnwM/yNA5fiAXqGibDgsRdjepQdZyGmfp+EZyKP P2cw== 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 :organization:from:references:cc:to:content-language:subject :user-agent:mime-version:date:message-id:dkim-signature; bh=9SOmUjy4ewgil/vdpgmMKA0zu4u1oLN4xpKp5BP8Qhk=; fh=kbzq0hg1nz0esQ52K+EoyMNXxPlSgTxzcWb25PEjIuY=; b=OfOpeaiysyfueNdWMSNVRDo5FMG7KSkAD/QLEpTc4ftCfsGICmUSv9oKUgNWWzqOp1 YhEhHT20diD2722EXoDoVVPdCKE82xAQJcoIj5zSQzLwAAz1I0OfIpZjPD23C2pz/EHf 6bSU8MANytKY66YzG57wWuu3fOMUNDCSh/60aTDlk1zBlvkGGdyXQYrHECOq+YAUNBSQ zTYR5OMIoINMLHWw7Py+URyAJ43WZEnOG9QyRr6aNdNeSmo7Kp0E7Qwx1eMkmsZBe+7S gcUJjEzy45PSdsG8/yyQ39aB4ZZlGY4vSOHAyTXq9QD0XjTxUgP+DuoYOLa91KV8wXa7 oXJA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="Mmc+/XH8"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id b26-20020a6567da000000b005859a90c58csi4145238pgs.772.2023.10.04.11.25.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Oct 2023 11:25:18 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="Mmc+/XH8"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 51C448043910; Wed, 4 Oct 2023 11:25:17 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244172AbjJDSZR (ORCPT + 99 others); Wed, 4 Oct 2023 14:25:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42364 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244168AbjJDSZQ (ORCPT ); Wed, 4 Oct 2023 14:25:16 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F1A2C98 for ; Wed, 4 Oct 2023 11:24:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1696443865; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=9SOmUjy4ewgil/vdpgmMKA0zu4u1oLN4xpKp5BP8Qhk=; b=Mmc+/XH8AJn3mn7Sz4kbCtOibFQyPy9TUgw/i/fmhZfgubyeXMjxjRKyeNFAOEnBaf6V3+ FhbF3m3XzIsreKkzOGnpmXE3WQC9CKi/vsUUrIA/EmHsZlXGrMejLhhAKVDjgGBjt3pM8P v5Db/tDUlVVxZZtDYLXGYswHbChA5Rg= Received: from mail-ej1-f70.google.com (mail-ej1-f70.google.com [209.85.218.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-336-WeAlo18QOtqYGyCsBPJvQw-1; Wed, 04 Oct 2023 14:24:23 -0400 X-MC-Unique: WeAlo18QOtqYGyCsBPJvQw-1 Received: by mail-ej1-f70.google.com with SMTP id a640c23a62f3a-9b95fa56bd5so11453766b.0 for ; Wed, 04 Oct 2023 11:24:23 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696443862; x=1697048662; h=content-transfer-encoding:in-reply-to:organization:from:references :cc:to:content-language:subject:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=9SOmUjy4ewgil/vdpgmMKA0zu4u1oLN4xpKp5BP8Qhk=; b=n7oGu5wnitoidsnRX9N5fq7iuiPtOvl7ThSFamvL+M7y8jliUY/M5YORVWNiJlQUWT 0V+CQyU44B4j11II5anrpPpzaLzURE/L59cHXO09L1yZpMh4LnAGMOF9JPohIMY1XDmM 44HKgaEZuRrmY4XA4JrOXywwmDFx1MX9lilYxEOmCAzqKtIY8Y54pUFzRgal5dBHgbLt Ov/sDV33DRXQr3tnYouoM9DuIr7AmuzfabVjUKpijWgNZoVeaP0K6+N7032w0NuWGmoj 3fGdZKguq2onL98qaZMGzQSdx4euhpwtj5F2LysUO8L/vFg3YXZsPn6eWjPdP7blXfDs kf8g== X-Gm-Message-State: AOJu0YzJn7pvd0H6eQEha6gSd6EXuAIoiheWPUGIhNmijyhtXVEzFOyL XUPcd3mAfAJwjPCBNQUX7UpQpH7t7gRQd6+jKkVbF4CQnUBgglacyOWRlb4k4RQ0/pA1DTGAU2S qRviqoQwAYlv0mfWWDJ3/7lD5 X-Received: by 2002:a17:907:75f4:b0:9ae:699d:8a2f with SMTP id jz20-20020a17090775f400b009ae699d8a2fmr2872321ejc.2.1696443862638; Wed, 04 Oct 2023 11:24:22 -0700 (PDT) X-Received: by 2002:a17:907:75f4:b0:9ae:699d:8a2f with SMTP id jz20-20020a17090775f400b009ae699d8a2fmr2872308ejc.2.1696443862329; Wed, 04 Oct 2023 11:24:22 -0700 (PDT) Received: from ?IPV6:2a02:810d:4b3f:de9c:642:1aff:fe31:a15c? ([2a02:810d:4b3f:de9c:642:1aff:fe31:a15c]) by smtp.gmail.com with ESMTPSA id jw21-20020a17090776b500b009786c8249d6sm3256405ejc.175.2023.10.04.11.24.21 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 04 Oct 2023 11:24:21 -0700 (PDT) Message-ID: Date: Wed, 4 Oct 2023 20:24:20 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.13.0 Subject: Re: [PATCH drm-misc-next v5 4/6] drm/gpuvm: track/lock/validate external/evicted objects Content-Language: en-US To: =?UTF-8?Q?Thomas_Hellstr=c3=b6m?= , airlied@gmail.com, daniel@ffwll.ch, matthew.brost@intel.com, sarah.walker@imgtec.com, donald.robson@imgtec.com, boris.brezillon@collabora.com, christian.koenig@amd.com, faith@gfxstrand.net Cc: dri-devel@lists.freedesktop.org, nouveau@lists.freedesktop.org, linux-kernel@vger.kernel.org References: <20230928191624.13703-1-dakr@redhat.com> <20230928191624.13703-5-dakr@redhat.com> <6b16ab6f-b1a2-efdb-04bf-5af4c3de381b@linux.intel.com> <6489f31f-8929-3e59-fbef-a22049cccbe3@redhat.com> <36233651a7675ab894134e41fc711fdcc71eefec.camel@linux.intel.com> <74d79ced-e811-bed9-6fb0-db694428c10f@redhat.com> From: Danilo Krummrich Organization: RedHat In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE, SPF_NONE 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 X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Wed, 04 Oct 2023 11:25:17 -0700 (PDT) On 10/4/23 19:57, Thomas Hellström wrote: > On Wed, 2023-10-04 at 19:17 +0200, Danilo Krummrich wrote: >> On 10/4/23 17:29, Thomas Hellström wrote: >>> >>> On Wed, 2023-10-04 at 14:57 +0200, Danilo Krummrich wrote: >>>> On 10/3/23 11:11, Thomas Hellström wrote: >>>> >>>> >>>> >>>>>>> + >>>>>>> +/** >>>>>>> + * drm_gpuvm_bo_evict() - add / remove a &drm_gpuvm_bo to >>>>>>> / >>>>>>> from the &drm_gpuvms >>>>>>> + * evicted list >>>>>>> + * @vm_bo: the &drm_gpuvm_bo to add or remove >>>>>>> + * @evict: indicates whether the object is evicted >>>>>>> + * >>>>>>> + * Adds a &drm_gpuvm_bo to or removes it from the >>>>>>> &drm_gpuvms >>>>>>> evicted list. >>>>>>> + */ >>>>>>> +void >>>>>>> +drm_gpuvm_bo_evict(struct drm_gpuvm_bo *vm_bo, bool evict) >>>>>>> +{ >>>>>>> +    struct drm_gem_object *obj = vm_bo->obj; >>>>>>> + >>>>>>> +    dma_resv_assert_held(obj->resv); >>>>>>> + >>>>>>> +    /* Always lock list transactions, even if >>>>>>> DRM_GPUVM_RESV_PROTECTED is >>>>>>> +     * set. This is required to protect multiple >>>>>>> concurrent >>>>>>> calls to >>>>>>> +     * drm_gpuvm_bo_evict() with BOs with different >>>>>>> dma_resv. >>>>>>> +     */ >>>>>> >>>>>> This doesn't work. The RESV_PROTECTED case requires the >>>>>> evicted >>>>>> flag we discussed before. The list is either protected by the >>>>>> spinlock or the resv. Otherwise a list add could race with a >>>>>> list >>>>>> removal elsewhere. >>>> >>>> I think it does unless I miss something, but it might be a bit >>>> subtle >>>> though. >>>> >>>> Concurrent drm_gpuvm_bo_evict() are protected by the spinlock. >>>> Additionally, when >>>> drm_gpuvm_bo_evict() is called we hold the dma-resv of the >>>> corresponding GEM object. >>>> >>>> In drm_gpuvm_validate() I assert that we hold *all* dma-resv, >>>> which >>>> implies that no >>>> one can call drm_gpuvm_bo_evict() on any of the VM's objects and >>>> no >>>> one can add a new >>>> one and directly call drm_gpuvm_bo_evict() on it either. >>> >>> But translated into how the data (the list in this case) is >>> protected >>> it becomes >>> >>> "Either the spinlock and the bo resv of a single list item OR the >>> bo >>> resvs of all bos that can potentially be on the list", >>> >>> while this is certainly possible to assert, any new / future code >>> that >>> manipulates the evict list will probably get this wrong and as a >>> result >>> the code becomes pretty fragile. I think drm_gpuvm_bo_destroy() >>> already >>> gets it wrong in that it, while holding a single resv, doesn't take >>> the >>> spinlock. >> >> That's true and I don't like it either. Unfortunately, with the dma- >> resv >> locking scheme we can't really protect the evict list without the >> drm_gpuvm_bo::evicted trick properly. >> >> But as pointed out in my other reply, I'm a bit worried about the >> drm_gpuvm_bo::evicted trick being too restrictive, but maybe it's >> fine >> doing it in the RESV_PROTECTED case. > > Ah, indeed. I misread that as discussing the current code rather than > the drm_gpuvm_bo::evicted trick. If validating only a subset, or a > range, then with the drm_gpuvm_bo::evicted trick would be valid only > for that subset. > > But the current code would break because the condition of locking "the > resvs of all bos that can potentially be on the list" doesn't hold > anymore, and you'd get list corruption. > > What *would* work, though, is the solution currently in xe, The > original evict list, and a staging evict list whose items are copied > over on validation. The staging evict list being protected by the > spinlock, the original evict list by the resv, and they'd use separate > list heads in the drm_gpuvm_bo, but that is yet another complication. > > But I think if this becomes an issue, those VMs (perhaps OpenGL UMD > VMs) only wanting to validate a subset, would simply initially rely on > the current non-RESV solution. It looks like it's only a matter of > flipping the flag on a per-vm basis. If such a driver locks a range it can also just validate all locked objects I guess. And for everything else, we still have the spinlock protected variant, where drivers can freely move things around by just taking the spinlock. I think I will go ahead and add drm_gpuvm_bo::evicted, plus the helpers I mentioned. > > /Thomas > > >> >>> >>> So I think that needs fixing, and if keeping that protection I >>> think it >>> needs to be documented with the list member and ideally an assert. >>> But >>> also note that lockdep_assert_held will typically give false true >>> for >>> dma_resv locks; as long as the first dma_resv lock locked in a >>> drm_exec >>> sequence  remains locked, lockdep thinks *all* dma_resv locks are >>> held. >>> (or something along those lines), so the resv lockdep asserts are >>> currently pretty useless. >>> >>> /Thomas >>> >>> >>> >>>> >>>>>> >>>>>> Thanks, >>>>>> >>>>>> Thomas >>>>>> >>>>>> >>>>> >>>> >>> >> >