Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp2504229rwd; Wed, 17 May 2023 10:34:05 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5CMp6/NZnDEdSq5lYxZ2htuP828pq+rEyv8nwQXpXg2hvuuEyVL+DHIdYpZYo++Ka7qM6O X-Received: by 2002:a17:902:c106:b0:1a9:a032:3844 with SMTP id 6-20020a170902c10600b001a9a0323844mr3165065pli.16.1684344845503; Wed, 17 May 2023 10:34:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684344845; cv=none; d=google.com; s=arc-20160816; b=JihdIGEZ13YQRe6skCoWG6LTEjR+dN4+mtkEIE1JohCWZhASiz0MCWjTuHH7g0Mjr7 gjtjQoW3etv2zRHdIlgO5E0SB7B+jHEBZhVzLNMuYK10xfYUn/2DS+WYkWYqOniM8wc0 KKzcLXk9Hnup1NuEqyX6ecnhxsi5VxQ1X3zqGLXbUdDQ0YgLeFCL/Ej6ECKr5tZK/Uhf +wOJ5Z4Ii2ukBC8kOKAwSLqDQjkogFiIQkDqYx1vWfaRZru9DVTI7s0OXZxH4vYvSsqw OrL0srYD8PhX9B4/qBm0ZIQwRkV9q6tA1BPH1uzLIPkPlgn8+pU6BE6KmtEHKN6RWLLy 27Dw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature:dkim-filter; bh=B0oIcaJ7eKE8RVMGKo2gtY9gzbix+usqvgrS1+i9Jas=; b=cnOP61sDbzja7DmQMm6ikpsQFH9hs6U/7Rr0NL84dpF1LtXZzl1L0QZzbQuhUBlQ8z loQm/sIHExkCmIGCt9NELP7Tk9Dnv9IQD8yEhjg/lh3OXDyJEc1XZrE+T6nWLRJUIOIi j4hl+aeZyjzJorOdXYVHULSODQoCtXAQWaHPTcXBCouKXX0xe2Z0o0GXNcuj0J5KsnGr O8eVYSCwSAnhSjv4G348mrlBIxrp3PIjfavFjv+hPee3OLOU5rhyQqYN6FKdFfGc+WVJ HhZX36k2/iZzj4iNV7KEWqooP36f+fKvKr97BBqEBT80qoupcbrRSPIWAdYYIMw5BGdx M1aQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.microsoft.com header.s=default header.b=g6fifcHI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.microsoft.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id li12-20020a170903294c00b001ac4c2f8e05si19608255plb.46.2023.05.17.10.33.52; Wed, 17 May 2023 10:34:05 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.microsoft.com header.s=default header.b=g6fifcHI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.microsoft.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229638AbjEQRWy (ORCPT + 99 others); Wed, 17 May 2023 13:22:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32958 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229511AbjEQRWx (ORCPT ); Wed, 17 May 2023 13:22:53 -0400 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 3F86965B9; Wed, 17 May 2023 10:22:52 -0700 (PDT) Received: from W11-BEAU-MD.localdomain (unknown [76.135.27.212]) by linux.microsoft.com (Postfix) with ESMTPSA id 3982120F2693; Wed, 17 May 2023 10:22:51 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 3982120F2693 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1684344171; bh=B0oIcaJ7eKE8RVMGKo2gtY9gzbix+usqvgrS1+i9Jas=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=g6fifcHIW6sisuL0qqYxgYpP6X1RTbYeicwpLCvLbO3XEB5Lrxs5WPRfNSPUt6J0g HICQ49ZBIKMT+Fk1oUF84zrIpFsCFMHMAqEnV2k3UeNBGcs49PcI28Xs9UUXYwpDu7 HXd+3UAhDg9ryFEFalPHO+V1L1VkaUq2mMooAF2E= Date: Wed, 17 May 2023 10:22:43 -0700 From: Beau Belgrave To: Linus Torvalds Cc: Steven Rostedt , Alexei Starovoitov , Masami Hiramatsu , LKML , linux-trace-kernel@vger.kernel.org, Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , bpf , David Vernet , dthaler@microsoft.com, brauner@kernel.org, hch@infradead.org Subject: Re: [PATCH] tracing/user_events: Run BPF program if attached Message-ID: <20230517172243.GA152@W11-BEAU-MD.localdomain> References: <20230509130111.62d587f1@rorschach.local.home> <20230509163050.127d5123@rorschach.local.home> <20230515165707.hv65ekwp2djkjj5i@MacBook-Pro-8.local> <20230515192407.GA85@W11-BEAU-MD.localdomain> <20230517003628.aqqlvmzffj7fzzoj@MacBook-Pro-8.local> <20230516222919.79bba667@rorschach.local.home> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: X-Spam-Status: No, score=-19.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_MED, SPF_HELO_PASS,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL, USER_IN_DEF_SPF_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, May 16, 2023 at 08:03:09PM -0700, Linus Torvalds wrote: > On Tue, May 16, 2023 at 7:29 PM Steven Rostedt wrote: > > > > So this code path is very much in user context (called directly by a > > write system call). The issue that Alexei had was that it's also in an > > rcu_read_lock() section. > > > > I wonder if this all goes away if we switch to SRCU? > > Yes, SRCU context would work. > > That said, how critical is this code? Because honestly, the *sanest* > thing to do is to just hold the lock that actually protects the list, > not try to walk the list in any RCU mode. > > And as far as I can tell, that's the 'event_mutex', which is already held. > > RCU walking of a list is only meaningful when the walk doesn't need > the lock that guarantees the list integrity. > > But *modification* of a RCU-protected list still requires locking, and > from a very cursory look, it really looks like 'event_mutex' is > already the lock that protects the list. > > So the whole use of RCU during the list walking there in > user_event_enabler_update() _seems_ pointless. You hold event_mutex - > user_event_enabler_write() that is called in the loop already has a > lockdep assert to that effect. > > So what is it that could even race and change the list that is the > cause of that rcu-ness? > Processes that fork() with previous user_events need to be duplicated. The fork() paths do not acquire the event_mutex. In the middle of a fork an event could become enabled/disabled, which would call this part of the code, at that time the list is actively being appended to when we try to update the bits. > Other code in that file happily just does > > mutex_lock(&event_mutex); > > list_for_each_entry_safe(enabler, next, &mm->enablers, link) > > with no RCU anywhere. Why does user_event_enabler_update() not do that? > This is due to the fork() case above without taking the event_mutex. I really tried to not cause fork() to stall if a process uses user_events. This required using RCU, maybe there is a simpler approach: One approach I can think of is that during fork() we don't add the newly created mm to the global list until we copy all the enablers. The COW pages should reflect the bits if a timing window occurs there, since I believe it's impossible for the newly forked() mm to cause a COW during that time. Then I can drop this RCU on the enablers. > Oh, and even those other loops are a bit strange. Why do they use the > "_safe" variant, even when they just traverse the list without > changing it? Look at user_event_enabler_exists(), for example. > The other places in the code that do this either will remove the event depending on the situation during the for_each, or they only hold the register lock and don't hold the event_mutex. So the disabler could get removed out from under it. IE: user_events_ioctl_reg() -> current_user_event_enabler_exists() This is a place where we could just simply change to grab the event_mutex, it's pretty isolated and we take the lock anyway further down the path. > I must really be missing something. That code is confusing. Or I am > very confused. > > Linus Thanks, -Beau