From patchwork Tue Feb 20 14:49:46 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: 10230177 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 199D8602B1 for ; Tue, 20 Feb 2018 14:49:53 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 09606286B9 for ; Tue, 20 Feb 2018 14:49:53 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id EF89B286BE; Tue, 20 Feb 2018 14:49:52 +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 85C66286B9 for ; Tue, 20 Feb 2018 14:49:51 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id C98CB6E45C; Tue, 20 Feb 2018 14:49:49 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mail-wr0-x242.google.com (mail-wr0-x242.google.com [IPv6:2a00:1450:400c:c0c::242]) by gabe.freedesktop.org (Postfix) with ESMTPS id 1112E6E452; Tue, 20 Feb 2018 14:49:48 +0000 (UTC) Received: by mail-wr0-x242.google.com with SMTP id w77so15132384wrc.6; Tue, 20 Feb 2018 06:49:48 -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=rAJuejiWasqwqIRiJBhqDR/cT58g2fYMlrVgdfC+UgY=; b=kOkCUXvD+wQQ8NYd6p93hPjTvJ6BCbnIlcLpuP1stj1se4BW17oLN9IiHG7m0pAS7a dHB/piawFBScbErg5GvqAcpNwpUzv9AJ3KoRIWx99HUZDWnJ/isv1lK/FC/p3apoLBwg 5vK2UJyfZki9x2UfKznLP3ihgwLGGF7odR0RKSuEz3N6+ykkHoUVf/KtmjL8Yb5EQ0Bn 8aMyyosa8c1Vqu+f0l9R8XFmoK8ArkPTKQJI4fwdf+O3RWAVbP/6J7vb3U6L14nGljAJ xYEeGjW7/8eVlio3daXpSLVSXGtHKrsGscQOL5tIsgPuDkpQwxjKtWNZ8Gck4aTSEeP1 yWXw== 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=rAJuejiWasqwqIRiJBhqDR/cT58g2fYMlrVgdfC+UgY=; b=XVc8slpDcnfV3/qc8/XSLScoL09dFzCh/iQX+sTBrxbzBkah35dYfn0YVOEJ2A3MGd uIh+WcX9/5OKVwIuYXamwKN+Dw6iJvuHYYxLBs8ufWRs1Hee7MlJzvEya7elIRtWs330 ZrNVHqTDaAMwp4DOecXsmVCxuiyOIGdyy3kogtExlg9acY81PYqf3q4U50o+Oq69Ller hqClcU0NVnqGNKifGTIjNu70JqbR6MsBLZ0V4ZAJiItneOR1AikFEh2UZj3XMAv+tdi7 UHjq5EJdfy1Do337/ucSWTRmB6mFUym1R1VKXp4OC+o47c/hg8S9M6VMdGtk5b3C89tV jnoA== X-Gm-Message-State: APf1xPBtdyyBXQ2s2/Y0moNqUo0C+ukltrhNWHpb8ucPM9YtnTCaoSoy cxvEPbnL/Ykq62v2AF7FyJqv5Q== X-Google-Smtp-Source: AH8x226N/LjOjxYOIxDI579XApXWihExP5HcAuV/O5t77wOdXxvDkKeDchzBILY3I8SFg85AaOeQeQ== X-Received: by 10.223.172.167 with SMTP id o36mr4257259wrc.34.1519138187648; Tue, 20 Feb 2018 06:49:47 -0800 (PST) Received: from baker.fritz.box ([2a02:908:1251:8fc0:ed7f:cda1:8dc7:d450]) by smtp.gmail.com with ESMTPSA id l199sm9336883wmd.18.2018.02.20.06.49.46 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 20 Feb 2018 06:49:47 -0800 (PST) From: "=?UTF-8?q?Christian=20K=C3=B6nig?=" X-Google-Original-From: =?UTF-8?q?Christian=20K=C3=B6nig?= To: peterz@infradead.org, amd-gfx@lists.freedesktop.org, dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org Subject: [PATCH] locking/ww_mutex: add ww_mutex_is_owned_by function v4 Date: Tue, 20 Feb 2018 15:49:46 +0100 Message-Id: <20180220144946.2510-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. v2: split amdgpu changes into separate patch as suggested by Alex v3: change logic as suggested by Daniel v4: fix test in case ctx is NULL Signed-off-by: Christian König Reviewed-by: Daniel Vetter --- include/linux/ww_mutex.h | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/include/linux/ww_mutex.h b/include/linux/ww_mutex.h index 39fda195bf78..fea4acc0bcbc 100644 --- a/include/linux/ww_mutex.h +++ b/include/linux/ww_mutex.h @@ -358,4 +358,23 @@ 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 + * @ctx: the w/w acquire context to test + * + * If @ctx is not NULL test if the mutex is owned by this context. + * If @ctx is NULL test if the mutex is owned by the current thread and not + * locked in any context. + */ +static inline bool ww_mutex_is_owned_by(struct ww_mutex *lock, + struct ww_acquire_ctx *ctx) +{ + if (ctx) + return likely(READ_ONCE(lock->ctx) == ctx); + + return likely(__mutex_owner(&lock->base) == current) && + likely(READ_ONCE(lock->ctx) == NULL; +} + #endif