Received: by 10.223.185.116 with SMTP id b49csp475075wrg; Wed, 21 Feb 2018 01:24:12 -0800 (PST) X-Google-Smtp-Source: AH8x224+ulKfZd/jtKl007fbGoZY4t2zseqJtbkgxXGxH1lDr8Wkkp5LjhlT6CoLDB1rSdqWffq3 X-Received: by 10.99.9.195 with SMTP id 186mr2199244pgj.152.1519205051876; Wed, 21 Feb 2018 01:24:11 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519205051; cv=none; d=google.com; s=arc-20160816; b=m3MfmoxlLQCbJl6x3TfClxd1mpHXPvKnu/hlbAZmG516E8XUe6T2QVPl+E8HIlBb3b sVodpol4qupRjqIHXFnfWw1CJ4labpxKhQM9qDbm+dqgmxKvhWGVRZmLIO7WpV9mdWkc SK5uggFb6OGUnw9nl476k3VGiTz8rTbUNlQcVx65UaT4szv8jrViWH7jxJnZuauhwUQl wf0A8fmVArlsh7YS9T78HKFmdmDyax43H9/QPskMsSiedPS/c8lWlugjglTSH7NgJnwA MeztC4X1bvKSeYKMtrvSaSvcHaSdcEcH6iAFxvPRFcHhM5UoNeiDItSPt4nuL+2ja0Fj 4acA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:date:subject:to:from:dkim-signature :arc-authentication-results; bh=rZ2AdHySDQaoy+KKivymc5HID2BJ6XBw9SJj/4FmyA8=; b=QTefDXy/UydDflgOtx29ZaYLgUi0YyGauWjfPrXhojmTFPYjZ/qbm0SrtOUjb8owjV uVigxGl2/g+uyz2e/GaLYCYxP//czqljV8TeXB0ChlXa944ly27H1t2pYm2ZaQJ750Vt Y4RTGWA1Ix7ZjJgLX3MkFpCiq39MjqF71feNC+SMzZolPUHmgdVsxISm2xke1nXqjwL7 2ounp58czUcjYST2qvE/J43TmYtA9qzA8Z/rm49lLZreWWIVA3StzqEQw1sYW276v18N 9RqKuM9QtVDzJ/k7Gx5Lh2ITqRTxfhHlJLnXUuNlb6bnAfHednstRu5ZKnxBHbf0LHsw 6Lew== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=ArvPD8PZ; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id y38-v6si1043683plh.448.2018.02.21.01.23.57; Wed, 21 Feb 2018 01:24:11 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=ArvPD8PZ; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932249AbeBUJWo (ORCPT + 99 others); Wed, 21 Feb 2018 04:22:44 -0500 Received: from mail-wr0-f195.google.com ([209.85.128.195]:34010 "EHLO mail-wr0-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932069AbeBUJWm (ORCPT ); Wed, 21 Feb 2018 04:22:42 -0500 Received: by mail-wr0-f195.google.com with SMTP id m5so2425317wrg.1 for ; Wed, 21 Feb 2018 01:22:41 -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=mJVTZ10LDnG8mLXKl6lKhx4+XC3GkBAFFssEjNorLPAKKakWcE9uuvLgn7rxrieeRx 3gwGvdsBoVLr/3ogHfHgVNriiNCD/KSHY1l1xSeMRCWxXcCG/0+7pLYHjeOORJrWlJFR 6BS7aQL30WQHjfPInj32jm0iyHl+HhsFE8tVNdOQua8J/N4+Rmow4etcnToV8Cd5IJ9V OM0V4gZMF2ejgfjQVbArj8jlaqtaNITa5BvTb8P/YCCVkqBm3UQPnyZ9bnY4oztLcF3Q v2iIdWtQQIC3qbt10Vom+Sh3QtfiX2PIzYWCcEo5wmDZ/fr+nz9m83diThqw5qEEXNQS 6hfg== X-Gm-Message-State: APf1xPB+My1D2BtRyPWVTFd2ELE5pUkpFGo3Xo6+0Ms27HR7n/5jnDR6 ghHLeJVK+tDIMzI7qMDcRoo= 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 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 -- 2.14.1