Received: by 2002:a05:6500:1b45:b0:1f5:f2ab:c469 with SMTP id cz5csp1202469lqb; Thu, 18 Apr 2024 03:16:49 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVikCP6NX9h6MV9OnR1U8ulMvXZamnTtTTcQH+HAl7Sml22f3dgdwxoCBA9PXMqcB61E1s28lPvedLPJRVoyPDHfRQ+wK4kIBrR5OHr+g== X-Google-Smtp-Source: AGHT+IFXSocV55qJ7PzNWal5S4/hQ3Rqm4zYSC6KixSojKXeoAfRgwATPsVCXAo557ErzLCPd2gF X-Received: by 2002:a05:6a20:dca0:b0:1a7:92dc:29f2 with SMTP id ky32-20020a056a20dca000b001a792dc29f2mr2043938pzb.49.1713435408733; Thu, 18 Apr 2024 03:16:48 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1713435408; cv=pass; d=google.com; s=arc-20160816; b=DQhjOrs8pojVM7fEy2pwsBHouVnmfGKXl+cbQpyvU0LpWfVuooixDQyqPf6Z+Mc9gt OFPIv22yCr/JNIwOpf9Es5luhwEznguNUZHao4oUeasgtJc59Rsi+AT2IOirLI8R5GmQ xdQDGvoLh/o6Og+J1owWlKv1O91sEQ0rdinEaVM8lSIRzX0gf9NJ7zYM3CcA4aMjyYqi rPrdw6WIQvpWVru2JUIIY+TKRAoKliFXQcQOyS0ZcAOyaEuscf+M02yDbr9l2XqsECuY w/XOiwL7+v18tq5m6ru7c4FLG8FRZUOBe6AacWHm/Z44CmIjcg5uPy8fPrqtQMW73hPh L/DQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature; bh=mr4THAQxcuBv8UqxKQa0kKsN0p/0SnnC1J2EkKXy4N8=; fh=ht15N20HbkwYHoOGtp8Qq7aZ7Txl5NfzxUbMQz29m/Y=; b=0Q00wQsW8CmG4mqWQNABnkfeIvYWjadXcDlGAKftKpTq5unOiQk404RnQjL2y/f3DP JmDVP0y2lUMUcH/fIHBNA3TmBvkLjRGeNYW8dBty5VqO9oZFUnGigCvXChuNw5gBLrCm 9lz09PHURCcIOKlIHcEKyzsorozdW2dLhZdC3BHGryUPKhsjnGQ7c73uMqMlLWwgpyyW f9vxRpC/3ypd1BqWUnYJWwk8ePdUXuHhZ6gv135DJmVoR5oyuXUVJUTH+5d9Za5NFG5I d8eNwU2dwNQkGbsKCjbRCFnZETLlAQHCEdnFaQIvaEnKBAr5AtgLw+I4PPD5nvOKGPRt Kj+Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=NDej8SDt; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-149861-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-149861-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id gm17-20020a056a00641100b006e558b4e179si1134703pfb.235.2024.04.18.03.16.48 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Apr 2024 03:16:48 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-149861-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=NDej8SDt; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-149861-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-149861-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org 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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 2AD3FB21E32 for ; Thu, 18 Apr 2024 10:03:13 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 85D0C15B578; Thu, 18 Apr 2024 10:03:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="NDej8SDt" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 A916715B544; Thu, 18 Apr 2024 10:03:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713434583; cv=none; b=OlcertYLWEAIRnsWmgyJAPyjXs6d1st6B46eg0qxqIcSq1V8eZ97hhLK3MVIzviT9NTCZrbqsSxTYZ8Qp+ofXJfN0SOYg8ZJs6PpmqqQeenL6ir/I5oAI298ePpU2sMmTFL8XzvMj7lZxPijPoM19UkCd3quMX4C/fNbCVn6cIQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713434583; c=relaxed/simple; bh=oJZpk3DWNO5wv+DUjDMVAC4Zkf2sFQ01Zacpn3wqjfo=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=sOL/6mm/sw4QC5HWB5QvnjlZVgcmXUhPIY3YA6Ltqp1b6886v9FDVi3IGB1/3OpTfEPLzeRVZ3YT++8UHk4MUw5QC04prKJ8JPSCuyOFg8IxSD3h8L+sALf1UyyCmyeHX9pq8hxFmw62+WCa0g8DsTrlTlD1VriQ9z7LxU7JgTo= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=NDej8SDt; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id BA97AC113CC; Thu, 18 Apr 2024 10:03:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1713434583; bh=oJZpk3DWNO5wv+DUjDMVAC4Zkf2sFQ01Zacpn3wqjfo=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=NDej8SDtUt5weJEfMTWMYDsk0C9grc5BIS0elzQp/JwJlpeUzU7OhaDdu7SGOq2Ld Siay2liwSonD5dNuVeJPfVXpQoOKp5r94o7v+p5Y0Ql8YEWjzPBKqLSPR6MPeQDKrf r3LmXvdnElMCi9OVwbHVHcQ5l/zezrgvHJ8k/8xMAzM4gRAlGF+Wrh6I3rc1QoCNnc tObkRQiikDaPAn7EKXzkzgth0Zv/G9b/Af7RbulsAA/y6S7UhELChJoNtp2dRev+62 idljm85v4sib6f4CtBBO5bJxxnKdz5ISgUyv2HCoUz9EOdu03YbBOpKEGqNcgrn7Vu SfV7qIM50+uTw== Date: Thu, 18 Apr 2024 12:03:00 +0200 From: Frederic Weisbecker To: Haifeng Xu Cc: peterz@infradead.org, mingo@redhat.com, acme@kernel.org, mark.rutland@arm.com, alexander.shishkin@linux.intel.com, jolsa@kernel.org, namhyung@kernel.org, irogers@google.com, adrian.hunter@intel.com, linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v2] perf/core: Fix missing wakeup when waiting for context reference Message-ID: References: <20240418080356.21639-1-haifeng.xu@shopee.com> 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=us-ascii Content-Disposition: inline In-Reply-To: <20240418080356.21639-1-haifeng.xu@shopee.com> On Thu, Apr 18, 2024 at 08:03:56AM +0000, Haifeng Xu wrote: > In our production environment, we found many hung tasks which are > blocked for more than 18 hours. Their call traces are like this: > > [346278.191038] __schedule+0x2d8/0x890 > [346278.191046] schedule+0x4e/0xb0 > [346278.191049] perf_event_free_task+0x220/0x270 > [346278.191056] ? init_wait_var_entry+0x50/0x50 > [346278.191060] copy_process+0x663/0x18d0 > [346278.191068] kernel_clone+0x9d/0x3d0 > [346278.191072] __do_sys_clone+0x5d/0x80 > [346278.191076] __x64_sys_clone+0x25/0x30 > [346278.191079] do_syscall_64+0x5c/0xc0 > [346278.191083] ? syscall_exit_to_user_mode+0x27/0x50 > [346278.191086] ? do_syscall_64+0x69/0xc0 > [346278.191088] ? irqentry_exit_to_user_mode+0x9/0x20 > [346278.191092] ? irqentry_exit+0x19/0x30 > [346278.191095] ? exc_page_fault+0x89/0x160 > [346278.191097] ? asm_exc_page_fault+0x8/0x30 > [346278.191102] entry_SYSCALL_64_after_hwframe+0x44/0xae > > The task was waiting for the refcount become to 1, but from the vmcore, > we found the refcount has already been 1. It seems that the task didn't > get woken up by perf_event_release_kernel() and got stuck forever. The > below scenario may cause the problem. > > Thread A Thread B > ... ... > perf_event_free_task perf_event_release_kernel > ... > acquire event->child_mutex > ... > get_ctx > ... release event->child_mutex > acquire ctx->mutex > ... > perf_free_event (acquire/release event->child_mutex) > ... > release ctx->mutex > wait_var_event > acquire ctx->mutex > acquire event->child_mutex > # move existing events to free_list > release event->child_mutex > release ctx->mutex > put_ctx > ... ... > > In this case, all events of the ctx have been freed, so we couldn't > find the ctx in free_list and Thread A will miss the wakeup. It's thus > necessary to add a wakeup after dropping the reference. > > Fixes: 1cf8dfe8a661 ("perf/core: Fix race between close() and fork()") > Signed-off-by: Haifeng Xu > Signed-off-by: Frederic Weisbecker Hint: always ask before putting someone else's Signed-off-by tag ;-) And anyway you don't need it here. Reviewed-by: Frederic Weisbecker > --- > Changes since v1 > - Add the fixed tag. > - Simplify v1's patch. (Frederic) > --- > kernel/events/core.c | 13 +++++++++++++ > 1 file changed, 13 insertions(+) > > diff --git a/kernel/events/core.c b/kernel/events/core.c > index 4f0c45ab8d7d..15c35070db6a 100644 > --- a/kernel/events/core.c > +++ b/kernel/events/core.c > @@ -5340,6 +5340,7 @@ int perf_event_release_kernel(struct perf_event *event) > again: > mutex_lock(&event->child_mutex); > list_for_each_entry(child, &event->child_list, child_list) { > + void *var = NULL; > > /* > * Cannot change, child events are not migrated, see the > @@ -5380,11 +5381,23 @@ int perf_event_release_kernel(struct perf_event *event) > * this can't be the last reference. > */ > put_event(event); > + } else { > + var = &ctx->refcount; > } > > mutex_unlock(&event->child_mutex); > mutex_unlock(&ctx->mutex); > put_ctx(ctx); > + > + if (var) { > + /* > + * If perf_event_free_task() has deleted all events from the > + * ctx while the child_mutex got released above, make sure to > + * notify about the preceding put_ctx(). > + */ > + smp_mb(); /* pairs with wait_var_event() */ > + wake_up_var(var); > + } > goto again; > } > mutex_unlock(&event->child_mutex); > -- > 2.25.1 >