From patchwork Thu Feb 15 14:19:42 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: 10221363 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 2A19360467 for ; Thu, 15 Feb 2018 14:19:51 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1B2F929375 for ; Thu, 15 Feb 2018 14:19:51 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0DFE02937B; Thu, 15 Feb 2018 14:19:51 +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=-4.1 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_MED, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 7B92529375 for ; Thu, 15 Feb 2018 14:19:50 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 587666E483; Thu, 15 Feb 2018 14:19:49 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mail-wr0-x244.google.com (mail-wr0-x244.google.com [IPv6:2a00:1450:400c:c0c::244]) by gabe.freedesktop.org (Postfix) with ESMTPS id 0DEF26E483; Thu, 15 Feb 2018 14:19:47 +0000 (UTC) Received: by mail-wr0-x244.google.com with SMTP id l43so3536546wrc.2; Thu, 15 Feb 2018 06:19:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=Eim/0zR83eNQwifirz5xjHVTk75oWWjb63kaupb636I=; b=Hj6/E9Q3G1nt1D4CJ17MaQQMFQ5g4YpLJZsD3CIQOnAH6X6eoLHY5RIXVPOijvRaC7 QkDlH7yqVMWaNM1TUFIvJ6H1iovqnhNtI9XrDUTTOmzFVgltlI61MOFQDzgVOygtqcqE 8F8pb7AQ6HWz/czheO5wkozLNtBhHw64EvSppc0tsHNiUtaJNGtkPd+tk5mw3yG6Ogab PG/Ae0aAh01f3+Ys73etIJon3CyXkO68tw0mFuOFT8IVpBEy3ObzaJX3zTSHbKS7RIAf zJNU6ckJPfoCR1Op4mrWiQT7HpsaA6hqBHtsKx8ZQNuvUcExDpraT/LFC4iEq9pChoc3 FQDg== 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:mime-version :content-transfer-encoding; bh=Eim/0zR83eNQwifirz5xjHVTk75oWWjb63kaupb636I=; b=ERhPZBVp8Hm6b0+kV/d/E28E19JZka0YfULBysFdbik5anp3aGKt6+lhFnptlyDpRD wNEJKn+dXD0dLGaNfER7huBvvcsuKQJbc90qXkBbn4xgWGiQvGfziXLKWskSNLI90Mlv zbm3T7j+Y+iOFrCyUGagfD4N2aod7eozVXDJcM5NUk8mFc1inWGnsP/teASwORsUunB7 7niBknBbwdvHW5797C2od3GAPxkcZhgblnbuk+RQM6lWW8GVbBW1Hevmy1KdBUWPWHCH kqL28g7ct4yDevuDOIgSFS3H+HOxgE/sM3VEH5C5YAJxAJCzwKSzB9kXrWNg7Ivlkk1C 7HOA== X-Gm-Message-State: APf1xPAS/SGkZ8pRYVymhzERkKL+QSNcC3SQSVY0L6f7hDcjgwl/qR9i YNx91JO9mXUD/wn16MIg2GifmA== X-Google-Smtp-Source: AH8x225gay02hhkjxgQb6uxwA77YJf5RJsv8X0kppA7H0UZ0NYbplNzotZnowFx1k7Rafl+INFXmyA== X-Received: by 10.223.163.215 with SMTP id m23mr2844434wrb.90.1518704386213; Thu, 15 Feb 2018 06:19:46 -0800 (PST) Received: from baker.fritz.box ([2a02:908:1251:8fc0:3595:2adb:15ea:f8af]) by smtp.gmail.com with ESMTPSA id 19sm27382433wrv.0.2018.02.15.06.19.45 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 15 Feb 2018 06:19:45 -0800 (PST) From: "=?UTF-8?q?Christian=20K=C3=B6nig?=" X-Google-Original-From: =?UTF-8?q?Christian=20K=C3=B6nig?= To: dri-devel@lists.freedesktop.org, amd-gfx@lists.freedesktop.org, linux-kernel@vger.kernel.org Subject: [PATCH 1/3] locking/ww_mutex: cleanup lock->ctx usage in amdgpu Date: Thu, 15 Feb 2018 15:19:42 +0100 Message-Id: <20180215141944.4332-1-christian.koenig@amd.com> X-Mailer: git-send-email 2.14.1 MIME-Version: 1.0 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" X-Virus-Scanned: ClamAV using ClamSMTP amdgpu needs to verify if userspace sends us valid addresses and the simplest way of doing this is to check if the buffer object is locked with the ticket of the current submission. Clean up the access to the ww_mutex internals by providing a function for this and extend the check to the thread owning the underlying mutex. Signed-off-by: Christian König Reviewed-by: Daniel Vetter --- drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c | 3 ++- include/linux/ww_mutex.h | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c index eaa3cb0c3ad1..4c04b560e358 100644 --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c @@ -1594,7 +1594,8 @@ int amdgpu_cs_find_mapping(struct amdgpu_cs_parser *parser, *map = mapping; /* Double check that the BO is reserved by this CS */ - if (READ_ONCE((*bo)->tbo.resv->lock.ctx) != &parser->ticket) + if (!ww_mutex_is_owned_by(&(*bo)->tbo.resv->lock, current, + &parser->ticket)) return -EINVAL; if (!((*bo)->flags & AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS)) { diff --git a/include/linux/ww_mutex.h b/include/linux/ww_mutex.h index 39fda195bf78..dd580db289e8 100644 --- a/include/linux/ww_mutex.h +++ b/include/linux/ww_mutex.h @@ -358,4 +358,21 @@ static inline bool ww_mutex_is_locked(struct ww_mutex *lock) return mutex_is_locked(&lock->base); } +/** + * ww_mutex_is_owned_by - is the w/w mutex locked by this task in that context + * @lock: the mutex to be queried + * @task: the task structure to check + * @ctx: the w/w acquire context to test + * + * Returns true if the mutex is locked in the context by the given task, false + * otherwise. + */ +static inline bool ww_mutex_is_owned_by(struct ww_mutex *lock, + struct task_struct *task, + struct ww_acquire_ctx *ctx) +{ + return likely(__mutex_owner(&lock->base) == task) && + READ_ONCE(lock->ctx) == ctx; +} + #endif