Received: by 2002:a05:6a10:17d3:0:0:0:0 with SMTP id hz19csp315734pxb; Fri, 16 Apr 2021 06:28:01 -0700 (PDT) X-Google-Smtp-Source: ABdhPJykK115Mx7FYZkFnkjSg7RTf1pHFs9JhBil7/Is9Pnm18L5mdm3evvEE7eBVXwWY3xm9Gqz X-Received: by 2002:a62:5a83:0:b029:222:c9de:5c65 with SMTP id o125-20020a625a830000b0290222c9de5c65mr7956534pfb.23.1618579681259; Fri, 16 Apr 2021 06:28:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1618579681; cv=none; d=google.com; s=arc-20160816; b=v1vatY0c15yNJhdsncM0PWiugJFUruCAVZfdpwQjrn+GlaxZ4HN6sgT76wvOqGDrkf 5QTv63BtnXFrlu8s4EUtlStxEHkITLswhT1m7tp+zrPksIThjwr7j90voBsHzxgeQD7N JEy5cRbD1Xx5hRnKfDfvuQpA2Xs0NJnoRDgTvMhbyRzDxaCg2NdDwCze/lXgaYNGEdZ7 tH7XgKEiSh092wVGYO/c/bR3cXkjiDX3pXEMhyKrbLWYJD0PBlC2r3DBXMdZqQUeaa3r 7Vh8AYCpBmURo6xNuU598NzzhH9I49y/qqzB6zX0VARsKwji/ymCagWqxPz2BsaYPXnh /QCQ== 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-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=sb+Ugjsd0tAaDPpJs5cCihgu8wnXOCRedQVd+/pDZMc=; b=y8TrgF6OK7FyKRdUvWLkixD26lfDx6KPzQVNWPlSDkezT3HNkaRnfVsSWk3zt4HvH6 s4d7G7bGB5G1/+zRo903GyFIDoP9genSMG+4afvhWh5zr9nKmFtuLemZ7DOMSm/Cmcfw xw5bTzgH7THOVij5wu1QW92NzB5jJReN7IBLSVYxjpP56DFAVAcA9DJkoOHQfmZJuYdT ir2dJXOezlXhHMTocMixMT9heYSnlKKoyrid14G7l0vGIh5Onlmn2xDzlrg2dMCGDd9e IHdxIEiFsgXkR7tsSh0k8cHjsgvzI3DEdFlJ+aUvxCz43MznaQj4yfp544TOqiQ/RCoy dXtA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=desiato.20200630 header.b=oTQOxnF8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id o9si6293987pgu.373.2021.04.16.06.27.46; Fri, 16 Apr 2021 06:28:01 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@infradead.org header.s=desiato.20200630 header.b=oTQOxnF8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242702AbhDPLZK (ORCPT + 99 others); Fri, 16 Apr 2021 07:25:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58252 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242414AbhDPLYy (ORCPT ); Fri, 16 Apr 2021 07:24:54 -0400 Received: from desiato.infradead.org (desiato.infradead.org [IPv6:2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 812C0C061756; Fri, 16 Apr 2021 04:24:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=sb+Ugjsd0tAaDPpJs5cCihgu8wnXOCRedQVd+/pDZMc=; b=oTQOxnF8z6GepfXYmcNaITTJ8J BQKqIXFBNrrj+WsWUsizw7P/Mm/aFxwvDFsxUZBf+4IUVINWC4ikel759t8pZXuRRoEC8unOKqGEj PSG73vXSMMgdAE4lX2eWR0s4MrAOeVd0Tc5/4RxWkqyVBqAphGNu7ozOJ/4uM2gei2v2wQKlyrLAg NNjzoxj29MsAzohIPJ92Rjv+lfx4ZCpOFGcTMg8qmRKuQ5Cs5N7EuTgPtJ32/GgvpZgbS2BZHO9To +atJUR8B2KtZ3ryXu1knNaY1+Cngp9xhf2hLF8Jq3OS6cuwRB4+j0gQo5L0gtLILdKzAfQbtLoKfM PQNltwoA==; Received: from j217100.upc-j.chello.nl ([24.132.217.100] helo=noisy.programming.kicks-ass.net) by desiato.infradead.org with esmtpsa (Exim 4.94 #2 (Red Hat Linux)) id 1lXMaG-001uWR-Eu; Fri, 16 Apr 2021 11:24:25 +0000 Received: from hirez.programming.kicks-ass.net (hirez.programming.kicks-ass.net [192.168.1.225]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (Client did not present a certificate) by noisy.programming.kicks-ass.net (Postfix) with ESMTPS id 0507C300212; Fri, 16 Apr 2021 13:24:24 +0200 (CEST) Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id E762C2C30CA77; Fri, 16 Apr 2021 13:24:23 +0200 (CEST) Date: Fri, 16 Apr 2021 13:24:23 +0200 From: Peter Zijlstra To: ojeda@kernel.org Cc: Linus Torvalds , Greg Kroah-Hartman , rust-for-linux@vger.kernel.org, linux-kbuild@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH 00/13] [RFC] Rust support Message-ID: References: <20210414184604.23473-1-ojeda@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210414184604.23473-1-ojeda@kernel.org> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Apr 14, 2021 at 08:45:51PM +0200, ojeda@kernel.org wrote: > - Featureful language: sum types, pattern matching, generics, > RAII, lifetimes, shared & exclusive references, modules & > visibility, powerful hygienic and procedural macros... IMO RAII is over-valued, but just in case you care, the below seems to work just fine. No fancy new language needed, works today. Similarly you can create refcount_t guards, or with a little more work full blown smart_ptr crud. --- diff --git a/include/linux/mutex.h b/include/linux/mutex.h index e19323521f9c..f03a72dd8cea 100644 --- a/include/linux/mutex.h +++ b/include/linux/mutex.h @@ -197,4 +197,22 @@ extern void mutex_unlock(struct mutex *lock); extern int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock); +struct mutex_guard { + struct mutex *mutex; +}; + +static inline struct mutex_guard mutex_guard_lock(struct mutex *mutex) +{ + mutex_lock(mutex); + return (struct mutex_guard){ .mutex = mutex, }; +} + +static inline void mutex_guard_unlock(struct mutex_guard *guard) +{ + mutex_unlock(guard->mutex); +} + +#define DEFINE_MUTEX_GUARD(name, lock) \ + struct mutex_guard __attribute__((__cleanup__(mutex_guard_unlock))) name = mutex_guard_lock(lock) + #endif /* __LINUX_MUTEX_H */ diff --git a/kernel/events/core.c b/kernel/events/core.c index 8ee3249de2f0..603d197a83b8 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -5715,16 +5715,15 @@ static long perf_compat_ioctl(struct file *file, unsigned int cmd, int perf_event_task_enable(void) { + DEFINE_MUTEX_GUARD(event_mutex, ¤t->perf_event_mutex); struct perf_event_context *ctx; struct perf_event *event; - mutex_lock(¤t->perf_event_mutex); list_for_each_entry(event, ¤t->perf_event_list, owner_entry) { ctx = perf_event_ctx_lock(event); perf_event_for_each_child(event, _perf_event_enable); perf_event_ctx_unlock(event, ctx); } - mutex_unlock(¤t->perf_event_mutex); return 0; }