From patchwork Wed Feb 21 09:22:39 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: 10231609 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 DA2726056F for ; Wed, 21 Feb 2018 09:28:09 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E25D22886F for ; Wed, 21 Feb 2018 09:28:09 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D769B2894F; Wed, 21 Feb 2018 09:28:09 +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 D92AE2894E for ; Wed, 21 Feb 2018 09:28:08 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 812A56E5A2; Wed, 21 Feb 2018 09:28:07 +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 B49F16E596; Wed, 21 Feb 2018 09:28:05 +0000 (UTC) Received: by mail-wr0-x244.google.com with SMTP id m5so2469219wrg.1; Wed, 21 Feb 2018 01:28:05 -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=rZ2AdHySDQaoy+KKivymc5HID2BJ6XBw9SJj/4FmyA8=; b=ArvPD8PZwBcRu0tNCwv3Ae6/dcc3GF/iylKbvYifroJWbEuetp4yA7uKiPwSLIK8qk sdiK3hN3ur1D8T3y5W+X7mVJiMvdX9wWQxTInx4LMdvK+GgW5EnreaxlKyNn0Yx8Rkwt WPyr/qi7Ri+WpmeAaiaCjjpTnZ1lhGrhY17zD3HW63a9wgUW5gN43BQklYw1UUtaaNjV Crd3zevn13k12cUnqWTZxraa6+gU+VDyomhbpTXMg09M1q+fouh0wJK2o7qglxoMVCF4 EMmHhYQNdm2TRUPjWZI2IDiMJLgbUU20/FxmCGgGjzGkz5BrM+slN9+49/Va44NQFXi6 ApTg== 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=rZ2AdHySDQaoy+KKivymc5HID2BJ6XBw9SJj/4FmyA8=; b=dehjZrEZ0t7cFk+IMte+ax1LxnqgLughMYBADY3j8iS3duelEbbuJuQ7T3d/4Rdn3a zpOkOOIEQCY0xnkOnnMVsobTGQEgC5LjUvm+jTT7rZpgyk2S/eQMdkaBuIh2MQecwRUq LzO73N3YTEKvGkLiV5dFf0LRKq3zj+Wy6TKjjN4pxN1OAIVsrvQUcYeEyHzdFEP6/ehR Zq9jehqfNqlRm+NrgN2FNXHGuA6txYCKhnTVzoc8r6S91/mlRzqjgiAccnlCc3TdqmA+ PRMMId34Mxce5+ITHD73eA3+79iAUFMFhFygI7uNOsY/tra9dEK1V7CCQMU+9jX7k872 pX9A== X-Gm-Message-State: APf1xPBOZIK2wxCpPvM3w/8X+l54nWHAnqoD5M8xTRrJcANgGVL3Uie7 uLDRIzIhKq5lJabtXHsdP3w= X-Google-Smtp-Source: AH8x227jMGczSSOuCiK8+7wfdluixQhR7JLBcDrNMe1kGt5gLXs0shfRwzQ0q2s8bXI47FPCB9W8dw== X-Received: by 10.28.11.70 with SMTP id 67mr1388527wml.158.1519204960958; Wed, 21 Feb 2018 01:22:40 -0800 (PST) Received: from baker.fritz.box ([2a02:908:1251:8fc0:d050:cb11:74d7:d8f6]) by smtp.gmail.com with ESMTPSA id o12sm21194862wrb.15.2018.02.21.01.22.40 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 21 Feb 2018 01:22:40 -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, daniel@ffwll.ch, 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 v5 Date: Wed, 21 Feb 2018 10:22:39 +0100 Message-Id: <20180221092239.5184-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 v5: fix stupid typo and improve kerneldoc Signed-off-by: Christian König Reviewed-by: Daniel Vetter --- include/linux/ww_mutex.h | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/include/linux/ww_mutex.h b/include/linux/ww_mutex.h index 39fda195bf78..6355de00239d 100644 --- a/include/linux/ww_mutex.h +++ b/include/linux/ww_mutex.h @@ -358,4 +358,24 @@ 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, i.e. acquired using either ww_mutex_trylock() or + * ww_mutex_lock() without supplying a &ww_acquire_ctx. + */ +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