From patchwork Fri Jan 12 09:47:28 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Christian_K=C3=B6nig?= X-Patchwork-Id: 10160147 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id CC100602D8 for ; Fri, 12 Jan 2018 09:47:41 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C9B802897D for ; Fri, 12 Jan 2018 09:47:41 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id BE5C4289A1; Fri, 12 Jan 2018 09:47:41 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4B4B92897D for ; Fri, 12 Jan 2018 09:47:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932414AbeALJrj (ORCPT ); Fri, 12 Jan 2018 04:47:39 -0500 Received: from mail-wm0-f66.google.com ([74.125.82.66]:41671 "EHLO mail-wm0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932489AbeALJrd (ORCPT ); Fri, 12 Jan 2018 04:47:33 -0500 Received: by mail-wm0-f66.google.com with SMTP id g75so10894249wme.0 for ; Fri, 12 Jan 2018 01:47:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=jqB73uovpTqW4R30FILSdPYvCy6XE6r79/TcepHEd9w=; b=T2iJPfT2BxSOZQwggcaMwax/I8t+jYZAfB/1ALohL+IAPJTqVnqZ6m/NWSbf7XD97V NlFoRS5VqGtvtIbaXcUS0LRdSS/Zfux5TBxIxMVeMY/SXHLQbxSsMl1X2/hEnC5EgdI0 7/zxpObS3++l2WZiDBrDfTM86Q/SIA4hx49WNzGSV9R6ar7Y4MCNL1jrBFZJ/2h72+/p tcfYjndNQlTChA2aAJCZahbreHe2n5LUbwmOzrEnpBL0IEeymb/i0t4q984aNuTJMqbs s09irCigJb4FfmjBdwhvZfmiIuv+7o0OzLxx7AHTNXL1O6AoneZaFCiHfnvZN2LSEZIY szzA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=jqB73uovpTqW4R30FILSdPYvCy6XE6r79/TcepHEd9w=; b=FQzTOjSP2hP4zvzrzJjjhXfImHmhftGL9eGZcpcS84PkICMSKemvMicmRvYVJhYaBG OJLQ8tYPjuDbLe6fRr7Who6W2mhVQDC2nwzy6lZOlKla0H/TkxznwNCWW2TAyQws938j TfDCnUG/4Qt474nB6AA5yyu4dxijZzBkbSRxLdzRnHt9A9bWBJ6cWc6/yMfLwIdi3seL iCd27dY/o3I2QaQq7og7rZPU/R+QG/NO7etn64g5/+2E1MRmRrTCT9PWffUXHn+CMz1R Rc5cdUAKj890oli9l3T7E0H3qFRyirMRKth0Ebyvw5akekl22i41gAl61eTkoq5IyFsG 7Muw== X-Gm-Message-State: AKwxytcXFeFVwXCFsMg0PMH0FCk/3q8kT1fUOkdFBnlCcgkEZtshtRsf GXgnGP6sEagmWH7RjZngshe1XA== X-Google-Smtp-Source: ACJfBouZV9tkx5V7/uT3uBy+Aoaaghd0tXB3sOXvBkB65D+fCBCRJmwhNaNT574FLLN+SSE9rGxd1g== X-Received: by 10.28.234.200 with SMTP id g69mr3288828wmi.137.1515750452495; Fri, 12 Jan 2018 01:47:32 -0800 (PST) Received: from baker.fritz.box ([2a02:908:1251:8fc0:6d3c:530f:8e94:9257]) by smtp.gmail.com with ESMTPSA id p10sm16616345wrh.61.2018.01.12.01.47.31 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 12 Jan 2018 01:47:31 -0800 (PST) From: "=?UTF-8?q?Christian=20K=C3=B6nig?=" X-Google-Original-From: =?UTF-8?q?Christian=20K=C3=B6nig?= To: daniel@ffwll.ch, sumit.semwal@linaro.org, gustavo@padovan.org, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org Subject: [PATCH 2/3] drm/amdgpu: add amdgpu_pasid_free_delayed v2 Date: Fri, 12 Jan 2018 10:47:28 +0100 Message-Id: <20180112094729.17491-2-christian.koenig@amd.com> X-Mailer: git-send-email 2.14.1 In-Reply-To: <20180112094729.17491-1-christian.koenig@amd.com> References: <20180112094729.17491-1-christian.koenig@amd.com> MIME-Version: 1.0 Sender: linux-media-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Free up a pasid after all fences signaled. v2: also handle the case when we can't allocate a fence array. Signed-off-by: Christian König --- drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c | 82 +++++++++++++++++++++++++++++++++ drivers/gpu/drm/amd/amdgpu/amdgpu_ids.h | 2 + 2 files changed, 84 insertions(+) diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c index 5248a3232aff..842caa5ed73b 100644 --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c @@ -40,6 +40,12 @@ */ static DEFINE_IDA(amdgpu_pasid_ida); +/* Helper to free pasid from a fence callback */ +struct amdgpu_pasid_cb { + struct dma_fence_cb cb; + unsigned int pasid; +}; + /** * amdgpu_pasid_alloc - Allocate a PASID * @bits: Maximum width of the PASID in bits, must be at least 1 @@ -75,6 +81,82 @@ void amdgpu_pasid_free(unsigned int pasid) ida_simple_remove(&amdgpu_pasid_ida, pasid); } +static void amdgpu_pasid_free_cb(struct dma_fence *fence, + struct dma_fence_cb *_cb) +{ + struct amdgpu_pasid_cb *cb = + container_of(_cb, struct amdgpu_pasid_cb, cb); + + amdgpu_pasid_free(cb->pasid); + dma_fence_put(fence); + kfree(cb); +} + +/** + * amdgpu_pasid_free_delayed - free pasid when fences signal + * + * @resv: reservation object with the fences to wait for + * @pasid: pasid to free + * + * Free the pasid only after all the fences in resv are signaled. + */ +void amdgpu_pasid_free_delayed(struct reservation_object *resv, + unsigned int pasid) +{ + struct dma_fence *fence, **fences; + struct amdgpu_pasid_cb *cb; + unsigned count; + int r; + + r = reservation_object_get_fences_rcu(resv, NULL, &count, &fences); + if (r) + goto fallback; + + if (count == 0) { + amdgpu_pasid_free(pasid); + return; + } + + if (count == 1) { + fence = fences[0]; + kfree(fences); + } else { + uint64_t context = dma_fence_context_alloc(1); + struct dma_fence_array *array; + + array = dma_fence_array_create(count, fences, context, + 1, false); + if (!array) { + kfree(fences); + goto fallback; + } + fence = &array->base; + } + + cb = kmalloc(sizeof(*cb), GFP_KERNEL); + if (!cb) { + /* Last resort when we are OOM */ + dma_fence_wait(fence, false); + dma_fence_put(fence); + amdgpu_pasid_free(pasid); + } else { + cb->pasid = pasid; + if (dma_fence_add_callback(fence, &cb->cb, + amdgpu_pasid_free_cb)) + amdgpu_pasid_free_cb(fence, &cb->cb); + } + + return; + +fallback: + /* Not enough memory for the delayed delete, as last resort + * block for all the fences to complete. + */ + reservation_object_wait_timeout_rcu(resv, true, false, + MAX_SCHEDULE_TIMEOUT); + amdgpu_pasid_free(pasid); +} + /* * VMID manager * diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ids.h b/drivers/gpu/drm/amd/amdgpu/amdgpu_ids.h index ad931fa570b3..38f37c16fc5e 100644 --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ids.h +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ids.h @@ -69,6 +69,8 @@ struct amdgpu_vmid_mgr { int amdgpu_pasid_alloc(unsigned int bits); void amdgpu_pasid_free(unsigned int pasid); +void amdgpu_pasid_free_delayed(struct reservation_object *resv, + unsigned int pasid); bool amdgpu_vmid_had_gpu_reset(struct amdgpu_device *adev, struct amdgpu_vmid *id);