Received: by 10.223.185.116 with SMTP id b49csp636118wrg; Tue, 20 Feb 2018 05:27:59 -0800 (PST) X-Google-Smtp-Source: AH8x227MjYYb8WYCNftZuPN9URcVCpOvW7C/ooPknlSf7VlisyfneiF1k/jkkPKLofxEkEOXfnVL X-Received: by 2002:a17:902:c5:: with SMTP id a63-v6mr17875330pla.391.1519133279129; Tue, 20 Feb 2018 05:27:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519133279; cv=none; d=google.com; s=arc-20160816; b=vV5TZjfaArmLCV92OQ9XNgxX/joIMYZlzX1WsFOjKEE7Hi9QX7F6WFbsqI7jQwV/lO pNX0FGjIeNUS9ISPP9EoaPSuL/e4OqTZIQl4dGEUBLoR07ed8t/St9KJTZSoPSTqd/b8 SBDZtabw/bMKxk0arHMPxuLS6sN7WvkTkmd4GcHz2PNaj+8hqKHXKgmTYu0rqV5oN+1i SIDj1va39MiNiMnaie1s/zhThAnkvFeMlc+vwJDhmnzxpcmGsjFQH2+as0kpLWri3yml S2ioOVed3Iqf4zL/XhEmi6Ylh9LFfIaUSlxsHIs5OjWwb3IiT+7Aq0oYg+MjM0yKCp5M 1GDg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-language :content-transfer-encoding:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject:reply-to:dkim-signature :arc-authentication-results; bh=lb6mBm/Hawf+1LnUEr+hCq/lGMLLIyft/j9w6jx9tHA=; b=JTCoK6SdeJ8uBUB3tiKnkEP0gyVdetWFF6e7WV8QqCUkf7cAxzuGqh/7tUg4hp+64g YCvsd/sW3bccoE4S9dzOSnhLrKk7gF/SloWtIaySkl4pgXupHTf6a4X3vPUtBQyE4GRR GlHfHT56hrWqPWEUwD0o0HoRnOW4ZlVi1FMT9OB9wWFZNgktu6cfNO+Ee50Gha2Po28x xSAhiXK6XgotFjSay2AfKrQJBwQ34a2Abk2TpNifJsPUNAFyN0iNPleI1cUEGPCc07CV 3AgvYYq0l8UjlFX3E2R56E76uRi7EuXguSj8sWxFRg9R9nFWR0m3qd0rjYWNG089q3Xi LWTA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=HPbKsA4O; 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 m30-v6si6973204pli.386.2018.02.20.05.27.44; Tue, 20 Feb 2018 05:27:59 -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=HPbKsA4O; 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 S1751943AbeBTN07 (ORCPT + 99 others); Tue, 20 Feb 2018 08:26:59 -0500 Received: from mail-wr0-f181.google.com ([209.85.128.181]:45272 "EHLO mail-wr0-f181.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751786AbeBTN06 (ORCPT ); Tue, 20 Feb 2018 08:26:58 -0500 Received: by mail-wr0-f181.google.com with SMTP id p104so13012681wrc.12 for ; Tue, 20 Feb 2018 05:26:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=reply-to:subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-transfer-encoding:content-language; bh=lb6mBm/Hawf+1LnUEr+hCq/lGMLLIyft/j9w6jx9tHA=; b=HPbKsA4OXQcfyhT6X/vCszZfZD1E59f/oC8SOjNtd94xvtQN7JfLURVot+qePpVZN7 cwGMMyzc7W/4QtAhVP1FBMbtq47mAGzqFqB/pnf/QAFoE17zU+kbQjE6qZvrdDg0CXWl kkPYn/FmRfFib+xwqOVwjoYwax7XntMLFAwHIZIlhdgFB8Q9/7T61K8v9RWUx7L6cvnK eWLEO3ah8f5cQS/euR8txE6N+V33Pd202FXn7fuKXUN/3bY2ZnRKTScK0c/jCfgGFi2f CsuBAYKfy7X/0yj9a6pAdHwggnHEtCPqyXIzzSPtavQeeCrnpvHMAEQbAbv++o7Oe/CY 42aw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:reply-to:subject:to:cc:references:from :message-id:date:user-agent:mime-version:in-reply-to :content-transfer-encoding:content-language; bh=lb6mBm/Hawf+1LnUEr+hCq/lGMLLIyft/j9w6jx9tHA=; b=W1+GMSeO9JeEios9seQuY9E5f00qZPhLNLA2NLZ8gHGNexQD7cuNp+WDeIg8QcyIQa TD0QhJt7KoF+7iB1s9GrBqQ7eFTdUg+x6Osw1Tsh2BHvhcEYi8zs3bw1RltPZ8B7FVfb Pj78bDoB3pIb+Z6jLi35UaMUHZOWo2RrzeIshZsSTrIOprtMgpIsjrxc/TpQYP2WYwgn jwFaAAHYJQ4QJcyQmWn1/PcQ1zOdzEKJPctrNSplrIH+N9hc+AD9VIZziIzqblYoxvnb 0u7zX3EoxuNte74HokwusP3dBG2PNtcmWfxM3Z3h6q09fP650XrsbqipfrHJhK1GRFop R7UA== X-Gm-Message-State: APf1xPB898Nyvd8SfyluC45bFZIOaZ6aHd0JzV0n3AaRhWNNkSCksBDk Cj9b/D0CZmkJ+XeTXWUp0Aq4TTVA X-Received: by 10.28.172.70 with SMTP id v67mr250635wme.58.1519133217015; Tue, 20 Feb 2018 05:26:57 -0800 (PST) Received: from ?IPv6:2a02:908:1251:8fc0:4c6d:7233:b7e1:3b88? ([2a02:908:1251:8fc0:4c6d:7233:b7e1:3b88]) by smtp.gmail.com with ESMTPSA id c12sm2842969wmh.2.2018.02.20.05.26.56 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 20 Feb 2018 05:26:56 -0800 (PST) Reply-To: christian.koenig@amd.com Subject: Re: [PATCH 1/4] locking/ww_mutex: add ww_mutex_is_owned_by function v3 To: Peter Zijlstra Cc: amd-gfx@lists.freedesktop.org, dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org References: <20180220125829.27060-1-christian.koenig@amd.com> <20180220131253.GF25314@hirez.programming.kicks-ass.net> From: =?UTF-8?Q?Christian_K=c3=b6nig?= Message-ID: <8fd80334-4d0e-8ed0-8a09-02a7e36a0eae@gmail.com> Date: Tue, 20 Feb 2018 14:26:55 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.6.0 MIME-Version: 1.0 In-Reply-To: <20180220131253.GF25314@hirez.programming.kicks-ass.net> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Am 20.02.2018 um 14:12 schrieb Peter Zijlstra: > On Tue, Feb 20, 2018 at 01:58:26PM +0100, Christian König wrote: >> 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 > Much thanks for Cc'ing the relevant maintainers :/ Sorry for that. >> --- >> include/linux/ww_mutex.h | 17 +++++++++++++++++ >> 1 file changed, 17 insertions(+) >> >> diff --git a/include/linux/ww_mutex.h b/include/linux/ww_mutex.h >> index 39fda195bf78..14e4149d3d9d 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 >> + * @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. >> + */ >> +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); >> + else >> + return likely(__mutex_owner(&lock->base) == current); >> +} > Much better than the previous version. If you want to bike-shed, you can > leave out the 'else' and unindent the last line. Thanks for the suggestion, going to do this. > I do worry about potential users of .ctx = NULL, though. It makes it far > too easy to do recursive locking, which is something we should strongly > discourage. Well, one of the addressed use cases is indeed checking for recursive locking. But recursive locking is something rather normal for ww_mutex and we are just exercising an existing code path. E.g. the most common use case for the ww_mutex is in the graphics drivers where usespace sends us a list of buffer objects to work with. Now when userspace sends us duplicates in that buffer list the expectation is to get -EALREADY from ww_mutex_lock when we try to lock the same ww_mutex twice. Depending on the driver this then results in returning an error code to userspace or just ignoring the duplicate (because of backward compatibility). The intention behind this function is now to a) be able to extend those checks to make sure user space doesn't sends us potentially harmful nonsense and b) allow to check for recursion in TTM during buffer object eviction which uses ww_mutex_trylock instead of ww_mutex_lock. Regards, Christian.