From patchwork Tue Dec 19 16:05:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Thomas_Hellstr=C3=B6m?= X-Patchwork-Id: 181068 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:24d3:b0:fb:cd0c:d3e with SMTP id r19csp2043338dyi; Tue, 19 Dec 2023 08:06:45 -0800 (PST) X-Google-Smtp-Source: AGHT+IEOLtRWbpya243aS0CD1DTlt1BOdB0p2bMkNqhDI9kt44dNoxWoMOwcVLsArjb5zNEtx8jc X-Received: by 2002:a17:906:f749:b0:a23:7467:243c with SMTP id jp9-20020a170906f74900b00a237467243cmr1278951ejb.60.1703002005060; Tue, 19 Dec 2023 08:06:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1703002005; cv=none; d=google.com; s=arc-20160816; b=Ie6rCJd5TtpJAWmtWINZD/I1a0Ce7dr/sHUFqKZA57/jEtt8WlsGmglUIDcPmYZcIQ maZ45gPKQ91XyooA79VmqB7ZF7ayisr5+neP5fFhcCwIcB6e1V2zVZ6NK0oT1/7ulq94 SlAQBmZjp+z3jXhWF3oMK3RLr3vB+If1LY7BG5XApgGLCBMp7dPP2+oPkeV/SxpTxaZ5 Fovnhp2fK019LB4LEiaWV93yQAtYfWQUs/8WaYsvdL8Z1NePU/MTHEDhfGNzX5t/pIB5 NX5riL8vb/l5vwk24YoWnE/ZT7Lv/V6Y4Ae9Hz9z+Mc3YaCPJO0TST13l1EjUkfcME/Y bJJw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from:dkim-signature; bh=7Rcu9UNweXroFCxLDA10ksx+tU90YswEarS8Wg6J+gQ=; fh=k0Pb5r8K1vuwCzwvb+wFuh1+yi3vCSA96JZUqNCUwbs=; b=gi7VazqGUGbG1gsSSKfVWkOI+hC238ZyIQHCKjfiFWDJVR0/aA3j2Jd8Oiu2bKRT3v i/eR7FA/o6aWynkrgZaWYKpv/pG72eM+OTp0KFt77J/FHZSgTNE/SFhbRHpy4+8rbPXp oISIIMvQHtCtVYnMnG8ULAV2m4HFdfzuTfhMj6fV4mMfj1mCrsi+kHLOLzYAqrSSmjDJ Iea7j2QXxfSRi43Uhgtag8LyF6mbpe8Z3c61EVZ1zxplR2TR6Dvir1qrbyNtZyGfYa/2 +XJweqOwKsjC8o+gHFd1eDUetCivF7xs5QS63LsCFOH1BaVAGujpJZdDcgnAY+xwNyuy a+4g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=V+kLbLv2; spf=pass (google.com: domain of linux-kernel+bounces-5535-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-5535-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id i15-20020a170906a28f00b00a2686be0a26si233438ejz.580.2023.12.19.08.06.44 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Dec 2023 08:06:45 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-5535-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=V+kLbLv2; spf=pass (google.com: domain of linux-kernel+bounces-5535-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-5535-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 2BEFB1F24A99 for ; Tue, 19 Dec 2023 16:06:44 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 0AA1320B1B; Tue, 19 Dec 2023 16:05:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="V+kLbLv2" X-Original-To: linux-kernel@vger.kernel.org Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4F689200C8 for ; Tue, 19 Dec 2023 16:05:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1703001947; x=1734537947; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=E/4G/640BLyW6ewtFRM5tFKJC3TzPRtX941yRQny8NE=; b=V+kLbLv2RO972TuPWkCEc/Cd1ZzYk+TarAeOAWDDyTS4ObKeD8vJL/n+ iQt3uk2QVL3LVbgcANSwRfYcp37PeTsx0fJDmHtylZk+5l2YwzezjLY0D aLB9SNhuQ7SUkERh0WMSAvTvMLzAVGN8yTYDaxkEXfr/f0ACRHuVXUlWr /+VRurTtJoOWLNY0M+mDq+JPg2XKh/ugg96PXEkFYkXruAOwOEWdHuEiw cuyy6h0mcEpONCV/Sv0gtOdsJKK3INhBfQYhleT6y+OUGeuEEWmaPLTRI pyxpI09Mnsc7TKzoCyEEr9mk31tYkbR6VET9qDLVlyQPzL8RouKzKntMP w==; X-IronPort-AV: E=McAfee;i="6600,9927,10929"; a="394561370" X-IronPort-AV: E=Sophos;i="6.04,288,1695711600"; d="scan'208";a="394561370" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmsmga104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 19 Dec 2023 08:05:46 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10929"; a="919658780" X-IronPort-AV: E=Sophos;i="6.04,288,1695711600"; d="scan'208";a="919658780" Received: from hasifnax-mobl1.gar.corp.intel.com (HELO fedora..) ([10.249.254.69]) by fmsmga001-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 19 Dec 2023 08:05:43 -0800 From: =?utf-8?q?Thomas_Hellstr=C3=B6m?= To: intel-xe@lists.freedesktop.org Cc: =?utf-8?q?Thomas_Hellstr=C3=B6m?= , Peter Zijlstra , Ingo Molnar , Will Deacon , Waiman Long , Boqun Feng , Maarten Lankhorst , =?utf-8?q?Christian_K=C3=B6nig?= , dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org Subject: [PATCH] locking/ww_mutex: Adjust to lockdep nest_lock requirements Date: Tue, 19 Dec 2023 17:05:24 +0100 Message-ID: <20231219160524.3646-1-thomas.hellstrom@linux.intel.com> X-Mailer: git-send-email 2.42.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785727030882756828 X-GMAIL-MSGID: 1785727030882756828 When using mutex_acquire_nest() with a nest_lock, lockdep refcounts the number of acquired lockdep_maps of mutexes of the same class, and also keeps a pointer to the first acquired lockdep_map of a class. That pointer is then used for various comparison-, printing- and checking purposes, but there is no mechanism to actively ensure that lockdep_map stays in memory. Instead, a warning is printed if the lockdep_map is freed and there are still held locks of the same lock class, even if the lockdep_map itself has been released. In the context of WW/WD transactions that means that if a user unlocks and frees a ww_mutex from within an ongoing ww transaction, and that mutex happens to be the first ww_mutex grabbed in the transaction, such a warning is printed and there might be a risk of a UAF. Note that this is only problem when lockdep is enabled and affects only dereferences of struct lockdep_map. Adjust to this by adding a fake lockdep_map to the acquired context and make sure it is the first acquired lockdep map of the associated ww_mutex class. Then hold it for the duration of the WW/WD transaction. This has the side effect that trying to lock a ww mutex *without* a ww_acquire_context but where a such context has been acquire, we'd see a lockdep splat. The test-ww_mutex.c selftest attempts to do that, so modify that particular test to not acquire a ww_acquire_context if it is not going to be used. Cc: Peter Zijlstra Cc: Ingo Molnar Cc: Will Deacon Cc: Waiman Long Cc: Boqun Feng Cc: Maarten Lankhorst Cc: Christian König Cc: dri-devel@lists.freedesktop.org Cc: linux-kernel@vger.kernel.org Signed-off-by: Thomas Hellström --- include/linux/ww_mutex.h | 14 ++++++++++++++ kernel/locking/test-ww_mutex.c | 6 ++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/include/linux/ww_mutex.h b/include/linux/ww_mutex.h index bb763085479a..a401a2f31a77 100644 --- a/include/linux/ww_mutex.h +++ b/include/linux/ww_mutex.h @@ -65,6 +65,16 @@ struct ww_acquire_ctx { #endif #ifdef CONFIG_DEBUG_LOCK_ALLOC struct lockdep_map dep_map; + /** + * @first_lock_dep_map: fake lockdep_map for first locked ww_mutex. + * + * lockdep requires the lockdep_map for the first locked ww_mutex + * in a ww transaction to remain in memory until all ww_mutexes of + * the transaction have been unlocked. Ensure this by keeping a + * fake locked ww_mutex lockdep map between ww_acquire_init() and + * ww_acquire_fini(). + */ + struct lockdep_map first_lock_dep_map; #endif #ifdef CONFIG_DEBUG_WW_MUTEX_SLOWPATH unsigned int deadlock_inject_interval; @@ -146,7 +156,10 @@ static inline void ww_acquire_init(struct ww_acquire_ctx *ctx, debug_check_no_locks_freed((void *)ctx, sizeof(*ctx)); lockdep_init_map(&ctx->dep_map, ww_class->acquire_name, &ww_class->acquire_key, 0); + lockdep_init_map(&ctx->first_lock_dep_map, ww_class->mutex_name, + &ww_class->mutex_key, 0); mutex_acquire(&ctx->dep_map, 0, 0, _RET_IP_); + mutex_acquire_nest(&ctx->first_lock_dep_map, 0, 0, &ctx->dep_map, _RET_IP_); #endif #ifdef CONFIG_DEBUG_WW_MUTEX_SLOWPATH ctx->deadlock_inject_interval = 1; @@ -185,6 +198,7 @@ static inline void ww_acquire_done(struct ww_acquire_ctx *ctx) static inline void ww_acquire_fini(struct ww_acquire_ctx *ctx) { #ifdef CONFIG_DEBUG_LOCK_ALLOC + mutex_release(&ctx->first_lock_dep_map, _THIS_IP_); mutex_release(&ctx->dep_map, _THIS_IP_); #endif #ifdef DEBUG_WW_MUTEXES diff --git a/kernel/locking/test-ww_mutex.c b/kernel/locking/test-ww_mutex.c index 78719e1ef1b1..c688131ed8f4 100644 --- a/kernel/locking/test-ww_mutex.c +++ b/kernel/locking/test-ww_mutex.c @@ -62,7 +62,8 @@ static int __test_mutex(unsigned int flags) int ret; ww_mutex_init(&mtx.mutex, &ww_class); - ww_acquire_init(&ctx, &ww_class); + if (flags & TEST_MTX_CTX) + ww_acquire_init(&ctx, &ww_class); INIT_WORK_ONSTACK(&mtx.work, test_mutex_work); init_completion(&mtx.ready); @@ -90,7 +91,8 @@ static int __test_mutex(unsigned int flags) ret = wait_for_completion_timeout(&mtx.done, TIMEOUT); } ww_mutex_unlock(&mtx.mutex); - ww_acquire_fini(&ctx); + if (flags & TEST_MTX_CTX) + ww_acquire_fini(&ctx); if (ret) { pr_err("%s(flags=%x): mutual exclusion failure\n",