Received: by 2002:ac8:5349:0:b0:427:7d78:cd45 with SMTP id d9csp282803qto; Tue, 19 Dec 2023 08:06:34 -0800 (PST) X-Google-Smtp-Source: AGHT+IHw1VkawBoDNqIalGLhtdx6Uym3kqdI2OWgejD4Equ+/z0YJFiVromeVwVhhhK1pR79Cc+M X-Received: by 2002:a05:622a:1393:b0:427:7e53:1605 with SMTP id o19-20020a05622a139300b004277e531605mr679013qtk.84.1703001994552; Tue, 19 Dec 2023 08:06:34 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1703001994; cv=none; d=google.com; s=arc-20160816; b=k9K6/tSzRhLHyXV9YW8qsw300GA7qZxZvmS17YSCsU/0CLUkUBOpCTQZQrqNEU1px6 ItM31e+Su3ohri4Vol2bxmqM1C+aJj3058ABKkC/W0MN+ZCZPZOjeAFFlI5WMDUksKcs JKPzRfwWbhw2XzlOm8aI9FnNoS5HPTYv6AskvioGPTI+Nap6R03g4Oya1Y5xzUzkcPtJ 2UyPgW4oMXwJwM6gtIxwLa2bPbsk/wD61TY5vfDhXrUVC3sFkVIlEmvsvQh4quajbAQ+ bgM4oJnkbRS/R4C3VjcQaTmbjw4Z4MaGZQKT3y+6FYcGdx1RjVGhxnstBhh8aEAK8sIe JuLQ== 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-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-5535-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id ew5-20020a05622a514500b004239cba1ce8si28239192qtb.571.2023.12.19.08.06.34 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Dec 2023 08:06:34 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-5535-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; 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-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-5535-linux.lists.archive=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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 536561C24704 for ; Tue, 19 Dec 2023 16:06:34 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 0DE1A20335; Tue, 19 Dec 2023 16:05:50 +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=20Hellstr=C3=B6m?= To: intel-xe@lists.freedesktop.org Cc: =?UTF-8?q?Thomas=20Hellstr=C3=B6m?= , Peter Zijlstra , Ingo Molnar , Will Deacon , Waiman Long , Boqun Feng , Maarten Lankhorst , =?UTF-8?q?Christian=20K=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 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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", -- 2.42.0