From patchwork Fri Dec 15 16:22:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lucas De Marchi X-Patchwork-Id: 13494647 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 6190BC4332F for ; Fri, 15 Dec 2023 16:22:41 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 70E5E10E23B; Fri, 15 Dec 2023 16:22:37 +0000 (UTC) Received: from mgamail.intel.com (mgamail.intel.com [134.134.136.31]) by gabe.freedesktop.org (Postfix) with ESMTPS id 2EAB610E12B; Fri, 15 Dec 2023 16:22:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1702657355; x=1734193355; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=PLd2Ls2yUvY88GQYhGlUjgqpLtm9iOYj4BxEoCsYMLA=; b=DLnXfWp9GFGUBDJ+bYXrHJNUMhygLAE57cfMdwQzSbOI1HxeM1Ff4L9x N5I0Kz6pQXcS9orRlFoDSz9izSYV6Etj8Ers86Oo0wRi/bLmv7WCrhK6h 7+cczyrTSVZzxCwrN5zmUwdMd8upsvygjeE/rFDX7apOGynp04CouTjar cFvr7k1B7hAdSkGnfMezJdRMNU3mdr9NvuZptI3+YNlefHJ7kASnt1NSe 7a+mI6vMZ0r1fS9y3M+Vt0VRqrP2Eog4bAGd2eICuQxqARQBEbPs2fBEq wag8uwYAbdPvq/JII6++5LE/7SAd1gfrkj9Que/hBvcF9iUgOBpBoaCsG w==; X-IronPort-AV: E=McAfee;i="6600,9927,10924"; a="459616054" X-IronPort-AV: E=Sophos;i="6.04,279,1695711600"; d="scan'208";a="459616054" Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 15 Dec 2023 08:22:34 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10924"; a="1021982233" X-IronPort-AV: E=Sophos;i="6.04,279,1695711600"; d="scan'208";a="1021982233" Received: from lucas-s2600cw.jf.intel.com ([10.165.21.196]) by fmsmga006-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 15 Dec 2023 08:22:34 -0800 From: Lucas De Marchi To: intel-gfx@lists.freedesktop.org Subject: [PATCH topic/core-for-CI] perf: Fix perf_event_validate_size() lockdep splat Date: Fri, 15 Dec 2023 08:22:17 -0800 Message-Id: <20231215162217.3273876-1-lucas.demarchi@intel.com> X-Mailer: git-send-email 2.40.1 MIME-Version: 1.0 X-BeenThere: intel-gfx@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Intel graphics driver community testing & development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: dri-devel@lists.freedesktop.org Errors-To: intel-gfx-bounces@lists.freedesktop.org Sender: "Intel-gfx" From: Mark Rutland When lockdep is enabled, the for_each_sibling_event(sibling, event) macro checks that event->ctx->mutex is held. When creating a new group leader event, we call perf_event_validate_size() on a partially initialized event where event->ctx is NULL, and so when for_each_sibling_event() attempts to check event->ctx->mutex, we get a splat, as reported by Lucas De Marchi: WARNING: CPU: 8 PID: 1471 at kernel/events/core.c:1950 __do_sys_perf_event_open+0xf37/0x1080 This only happens for a new event which is its own group_leader, and in this case there cannot be any sibling events. Thus it's safe to skip the check for siblings, which avoids having to make invasive and ugly changes to for_each_sibling_event(). Avoid the splat by bailing out early when the new event is its own group_leader. Fixes: 382c27f4ed28f803 ("perf: Fix perf_event_validate_size()") Closes: https://lore.kernel.org/lkml/20231214000620.3081018-1-lucas.demarchi@intel.com/ Closes: https://lore.kernel.org/lkml/ZXpm6gQ%2Fd59jGsuW@xpf.sh.intel.com/ Reported-by: Lucas De Marchi Reported-by: Pengfei Xu Signed-off-by: Mark Rutland Signed-off-by: Peter Zijlstra (Intel) Link: https://lkml.kernel.org/r/20231215112450.3972309-1-mark.rutland@arm.com [ cherry pick from tip/urgent heading to 6.7-rc6 ] Signed-off-by: Lucas De Marchi Acked-by: Rodrigo Vivi --- kernel/events/core.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/kernel/events/core.c b/kernel/events/core.c index acfc5a569818..a64165af45c1 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -1947,6 +1947,16 @@ static bool perf_event_validate_size(struct perf_event *event) group_leader->nr_siblings + 1) > 16*1024) return false; + /* + * When creating a new group leader, group_leader->ctx is initialized + * after the size has been validated, but we cannot safely use + * for_each_sibling_event() until group_leader->ctx is set. A new group + * leader cannot have any siblings yet, so we can safely skip checking + * the non-existent siblings. + */ + if (event == group_leader) + return true; + for_each_sibling_event(sibling, group_leader) { if (__perf_event_read_size(sibling->attr.read_format, group_leader->nr_siblings + 1) > 16*1024)