linux-perf-users.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Greg KH <gregkh@linuxfoundation.org>
To: Peter Zijlstra <peterz@infradead.org>
Cc: torvalds@linux-foundation.org, keescook@chromium.org,
	pbonzini@redhat.com, masahiroy@kernel.org, nathan@kernel.org,
	ndesaulniers@google.com, nicolas@fjasle.eu,
	catalin.marinas@arm.com, will@kernel.org, vkoul@kernel.org,
	trix@redhat.com, ojeda@kernel.org, mingo@redhat.com,
	longman@redhat.com, boqun.feng@gmail.com, dennis@kernel.org,
	tj@kernel.org, cl@linux.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, juri.lelli@redhat.com,
	vincent.guittot@linaro.org, dietmar.eggemann@arm.com,
	rostedt@goodmis.org, bsegall@google.com, mgorman@suse.de,
	bristot@redhat.com, vschneid@redhat.com, paulmck@kernel.org,
	frederic@kernel.org, quic_neeraju@quicinc.com,
	joel@joelfernandes.org, josh@joshtriplett.org,
	mathieu.desnoyers@efficios.com, jiangshanlai@gmail.com,
	rientjes@google.com, vbabka@suse.cz, roman.gushchin@linux.dev,
	42.hyeyoo@gmail.com, apw@canonical.com, joe@perches.com,
	dwaipayanray1@gmail.com, lukas.bulwahn@gmail.com,
	john.johansen@canonical.com, paul@paul-moore.com,
	jmorris@namei.org, serge@hallyn.com,
	linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org,
	dmaengine@vger.kernel.org, llvm@lists.linux.dev,
	linux-perf-users@vger.kernel.org, rcu@vger.kernel.org,
	linux-security-module@vger.kernel.org, tglx@linutronix.de,
	ravi.bangoria@amd.com, error27@gmail.com,
	luc.vanoostenryck@gmail.com
Subject: Re: [PATCH v3 46/57] perf: Simplify pmu_dev_alloc()
Date: Mon, 12 Jun 2023 17:44:59 +0200	[thread overview]
Message-ID: <2023061217-mutable-curry-c2ac@gregkh> (raw)
In-Reply-To: <20230612141322.GA83892@hirez.programming.kicks-ass.net>

On Mon, Jun 12, 2023 at 04:13:22PM +0200, Peter Zijlstra wrote:
> > Mind if I try this series to convert a more "normal" driver to see how
> > it works with that?  That's going to be the true test, see if the
> > changes make sense to someone who doesn't really know the internals of
> > the driver core like this...
> 
> Not at all, feel tree to have a go at that. I picked code I was familiar
> with, but it would ofc. be good to have others give it a spin too.

Ok, here's an attempt to clean up misc.c a bit.  It does two things,
adds a guard for the global misc_mtx mutex when used (sometimes, it's
not always paired with a release.)

Then in the last part of the file, I abuse the DEFINE_FREE() to handle a
special case of removing a proc file if things go bad (and add a
DEFINE_FREE() for class_destroy(), which should go into
include/device/class.h.

I've only test-built it, but is this the proper use of DEFINE_FREE()?
There wasn't much documentation :)

To be fair the end-result of misc_init() is much nicer and cleaner and
"obviously correct", which is good, even with the crazy proc file mess
in it.  So I like the idea overall, need to figure out when to use
DEFINE_CLASS() vs. DEFINE_FREE(), that isn't obvious to me.

Also, you can't put a DEFINE_FREE() within a function declaration, which
I guess makes sense, but the build warning is very odd when you attempt
it, mentioning an "invalid storage class".  Is that supposed to be able
to work?

thanks,

greg k-h


---
 drivers/char/misc.c | 69 +++++++++++++++++----------------------------
 1 file changed, 26 insertions(+), 43 deletions(-)

diff --git a/drivers/char/misc.c b/drivers/char/misc.c
index 1c44c29a666e..a203b17a2b82 100644
--- a/drivers/char/misc.c
+++ b/drivers/char/misc.c
@@ -123,10 +123,9 @@ static int misc_open(struct inode *inode, struct file *file)
 {
 	int minor = iminor(inode);
 	struct miscdevice *c = NULL, *iter;
-	int err = -ENODEV;
 	const struct file_operations *new_fops = NULL;
 
-	mutex_lock(&misc_mtx);
+	guard(mutex)(&misc_mtx);
 
 	list_for_each_entry(iter, &misc_list, list) {
 		if (iter->minor != minor)
@@ -149,7 +148,7 @@ static int misc_open(struct inode *inode, struct file *file)
 			break;
 		}
 		if (!new_fops)
-			goto fail;
+			return -ENODEV;
 	}
 
 	/*
@@ -159,13 +158,11 @@ static int misc_open(struct inode *inode, struct file *file)
 	 */
 	file->private_data = c;
 
-	err = 0;
 	replace_fops(file, new_fops);
 	if (file->f_op->open)
-		err = file->f_op->open(inode, file);
-fail:
-	mutex_unlock(&misc_mtx);
-	return err;
+		return file->f_op->open(inode, file);
+
+	return 0;
 }
 
 static struct class *misc_class;
@@ -197,29 +194,24 @@ static const struct file_operations misc_fops = {
 int misc_register(struct miscdevice *misc)
 {
 	dev_t dev;
-	int err = 0;
 	bool is_dynamic = (misc->minor == MISC_DYNAMIC_MINOR);
 
 	INIT_LIST_HEAD(&misc->list);
 
-	mutex_lock(&misc_mtx);
+	guard(mutex)(&misc_mtx);
 
 	if (is_dynamic) {
 		int i = misc_minor_alloc();
 
-		if (i < 0) {
-			err = -EBUSY;
-			goto out;
-		}
+		if (i < 0)
+			return -EBUSY;
 		misc->minor = i;
 	} else {
 		struct miscdevice *c;
 
 		list_for_each_entry(c, &misc_list, list) {
-			if (c->minor == misc->minor) {
-				err = -EBUSY;
-				goto out;
-			}
+			if (c->minor == misc->minor)
+				return -EBUSY;
 		}
 	}
 
@@ -233,8 +225,7 @@ int misc_register(struct miscdevice *misc)
 			misc_minor_free(misc->minor);
 			misc->minor = MISC_DYNAMIC_MINOR;
 		}
-		err = PTR_ERR(misc->this_device);
-		goto out;
+		return PTR_ERR(misc->this_device);
 	}
 
 	/*
@@ -242,9 +233,7 @@ int misc_register(struct miscdevice *misc)
 	 * earlier defaults
 	 */
 	list_add(&misc->list, &misc_list);
- out:
-	mutex_unlock(&misc_mtx);
-	return err;
+	return 0;
 }
 EXPORT_SYMBOL(misc_register);
 
@@ -261,11 +250,10 @@ void misc_deregister(struct miscdevice *misc)
 	if (WARN_ON(list_empty(&misc->list)))
 		return;
 
-	mutex_lock(&misc_mtx);
+	guard(mutex)(&misc_mtx);
 	list_del(&misc->list);
 	device_destroy(misc_class, MKDEV(MISC_MAJOR, misc->minor));
 	misc_minor_free(misc->minor);
-	mutex_unlock(&misc_mtx);
 }
 EXPORT_SYMBOL(misc_deregister);
 
@@ -280,29 +268,24 @@ static char *misc_devnode(const struct device *dev, umode_t *mode)
 	return NULL;
 }
 
+DEFINE_FREE(class_destroy, struct class *, if (_T) class_destroy(_T));
+DEFINE_FREE(remove_proc, struct proc_dir_entry *, if (_T) remove_proc_entry("misc", NULL));
 static int __init misc_init(void)
 {
-	int err;
-	struct proc_dir_entry *ret;
+	struct proc_dir_entry *ret __free(remove_proc) = proc_create_seq("misc", 0, NULL, &misc_seq_ops);
+	struct class *c __free(class_destroy) = class_create("misc");
 
-	ret = proc_create_seq("misc", 0, NULL, &misc_seq_ops);
-	misc_class = class_create("misc");
-	err = PTR_ERR(misc_class);
-	if (IS_ERR(misc_class))
-		goto fail_remove;
+	if (IS_ERR(c))
+		return PTR_ERR(c);
 
-	err = -EIO;
 	if (register_chrdev(MISC_MAJOR, "misc", &misc_fops))
-		goto fail_printk;
-	misc_class->devnode = misc_devnode;
-	return 0;
+		return -EIO;
 
-fail_printk:
-	pr_err("unable to get major %d for misc devices\n", MISC_MAJOR);
-	class_destroy(misc_class);
-fail_remove:
-	if (ret)
-		remove_proc_entry("misc", NULL);
-	return err;
+	c->devnode = misc_devnode;
+
+	misc_class = no_free_ptr(c);
+	no_free_ptr(ret);
+
+	return 0;
 }
 subsys_initcall(misc_init);
-- 
2.41.0


  reply	other threads:[~2023-06-12 15:45 UTC|newest]

Thread overview: 103+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-12  9:07 [PATCH v3 00/57] Scope-based Resource Management Peter Zijlstra
2023-06-12  9:07 ` [PATCH v3 01/57] dmaengine: ioat: Free up __cleanup() name Peter Zijlstra
2023-06-16 22:41   ` Dave Jiang
2023-06-12  9:07 ` [PATCH v3 02/57] apparmor: " Peter Zijlstra
2023-06-13  8:38   ` John Johansen
2023-06-13 10:28   ` John Johansen
2024-01-16 18:10   ` John Johansen
2023-06-12  9:07 ` [PATCH v3 03/57] locking: Introduce __cleanup() based infrastructure Peter Zijlstra
2023-06-13 10:55   ` Peter Zijlstra
2023-06-13 19:46     ` Kees Cook
2023-06-13 17:16   ` Miguel Ojeda
2023-08-14  7:24   ` Bartosz Golaszewski
2024-01-14  6:49   ` Dmitry Torokhov
2023-06-12  9:07 ` [PATCH v3 04/57] kbuild: Drop -Wdeclaration-after-statement Peter Zijlstra
2023-06-14  6:13   ` Masahiro Yamada
2023-06-14  8:25     ` Peter Zijlstra
2023-06-12  9:07 ` [PATCH v3 05/57] sched: Simplify get_nohz_timer_target() Peter Zijlstra
2023-06-12  9:07 ` [PATCH v3 06/57] sched: Simplify sysctl_sched_uclamp_handler() Peter Zijlstra
2023-06-12 13:47   ` Dan Carpenter
2023-06-12  9:07 ` [PATCH v3 07/57] sched: Simplify: migrate_swap_stop() Peter Zijlstra
2023-06-12  9:07 ` [PATCH v3 08/57] sched: Simplify wake_up_if_idle() Peter Zijlstra
2023-06-12 18:02   ` Boqun Feng
2023-06-12 20:02     ` Peter Zijlstra
2023-06-12  9:07 ` [PATCH v3 09/57] sched: Simplify ttwu() Peter Zijlstra
2023-06-12 13:51   ` Dan Carpenter
2023-06-12 14:08     ` Peter Zijlstra
2023-06-12  9:07 ` [PATCH v3 10/57] sched: Simplify sched_exec() Peter Zijlstra
2023-06-12  9:07 ` [PATCH v3 11/57] sched: Simplify sched_tick_remote() Peter Zijlstra
2023-06-12  9:07 ` [PATCH v3 12/57] sched: Simplify try_steal_cookie() Peter Zijlstra
2023-06-12  9:07 ` [PATCH v3 13/57] sched: Simplify sched_core_cpu_{starting,deactivate}() Peter Zijlstra
2023-06-12  9:07 ` [PATCH v3 14/57] sched: Simplify set_user_nice() Peter Zijlstra
2023-06-12  9:07 ` [PATCH v3 15/57] sched: Simplify syscalls Peter Zijlstra
2023-06-12  9:07 ` [PATCH v3 16/57] sched: Simplify sched_{set,get}affinity() Peter Zijlstra
2023-06-12  9:07 ` [PATCH v3 17/57] sched: Simplify yield_to() Peter Zijlstra
2023-06-12  9:07 ` [PATCH v3 18/57] sched: Simplify sched_rr_get_interval() Peter Zijlstra
2023-06-12  9:07 ` [PATCH v3 19/57] sched: Simplify sched_move_task() Peter Zijlstra
2023-06-12  9:07 ` [PATCH v3 20/57] sched: Simplify tg_set_cfs_bandwidth() Peter Zijlstra
2023-06-12  9:07 ` [PATCH v3 21/57] sched: Misc cleanups Peter Zijlstra
2023-06-12  9:07 ` [PATCH v3 22/57] perf: Fix cpuctx refcounting Peter Zijlstra
2023-06-12  9:07 ` [PATCH v3 23/57] perf: Simplify perf_event_alloc() error path Peter Zijlstra
2023-06-12  9:07 ` [PATCH v3 24/57] perf: Simplify perf_pmu_register() " Peter Zijlstra
2023-06-12  9:07 ` [PATCH v3 25/57] perf: Simplify perf_fget_light() Peter Zijlstra
2023-06-12  9:07 ` [PATCH v3 26/57] perf: Simplify event_function*() Peter Zijlstra
2023-06-12 14:46   ` Dan Carpenter
2023-06-12 14:58     ` Peter Zijlstra
2023-06-13  5:56   ` Namhyung Kim
2023-06-13  7:39     ` Peter Zijlstra
2023-06-12  9:07 ` [PATCH v3 27/57] perf: Simplify perf_cgroup_connect() Peter Zijlstra
2023-06-12  9:07 ` [PATCH v3 28/57] perf; Simplify event_sched_in() Peter Zijlstra
2023-06-13 19:43   ` Kees Cook
2023-06-12  9:07 ` [PATCH v3 29/57] perf: Simplify: __perf_install_in_context() Peter Zijlstra
2023-06-12  9:07 ` [PATCH v3 30/57] perf: Simplify: *perf_event_{dis,en}able*() Peter Zijlstra
2023-06-12  9:07 ` [PATCH v3 31/57] perf: Simplify perf_event_modify_attr() Peter Zijlstra
2023-06-12  9:07 ` [PATCH v3 32/57] perf: Simplify perf_event_context_sched_in() Peter Zijlstra
2023-06-12  9:07 ` [PATCH v3 33/57] perf: Simplify perf_adjust_freq_unthr_context() Peter Zijlstra
2023-06-12 16:27   ` Linus Torvalds
2023-06-12 18:44     ` Peter Zijlstra
2023-06-12 18:55       ` Linus Torvalds
2023-06-12 20:05         ` Peter Zijlstra
2023-06-13 12:05         ` Peter Zijlstra
2023-06-12  9:07 ` [PATCH v3 34/57] perf: Simplify perf_event_*_on_exec() Peter Zijlstra
2023-06-12  9:07 ` [PATCH v3 35/57] perf: Simplify *perf_event_read*() Peter Zijlstra
2023-06-12  9:07 ` [PATCH v3 36/57] perf: Simplify find_get_pmu_context() Peter Zijlstra
2023-06-12  9:07 ` [PATCH v3 37/57] perf: Simplify perf_read_group() Peter Zijlstra
2023-06-12  9:07 ` [PATCH v3 38/57] perf: Simplify IOC_SET_OUTPUT Peter Zijlstra
2023-06-12  9:07 ` [PATCH v3 39/57] perf: Simplify perf_event_*_userpage() Peter Zijlstra
2023-06-12  9:07 ` [PATCH v3 40/57] perf: Simplify perf_mmap_close()/perf_aux_sample_output() Peter Zijlstra
2023-06-12  9:07 ` [PATCH v3 41/57] perf: Simplify __perf_event_output() Peter Zijlstra
2023-06-12  9:07 ` [PATCH v3 42/57] perf: Simplify perf_iterate_sb() Peter Zijlstra
2023-06-12  9:07 ` [PATCH v3 43/57] perf: Simplify perf_sw_event() Peter Zijlstra
2023-06-12  9:07 ` [PATCH v3 44/57] perf: Simplify bpf_overflow_handler() Peter Zijlstra
2023-06-12  9:07 ` [PATCH v3 45/57] perf: Simplify perf_event_parse_addr_filter() Peter Zijlstra
2023-06-13 19:47   ` Kees Cook
2023-06-12  9:07 ` [PATCH v3 46/57] perf: Simplify pmu_dev_alloc() Peter Zijlstra
2023-06-12  9:44   ` Peter Zijlstra
2023-06-12  9:55     ` Greg KH
2023-06-12 12:18     ` Greg KH
2023-06-12 12:29       ` Paolo Bonzini
2023-06-12 12:58         ` Greg KH
2023-06-12 13:09       ` Greg KH
2023-06-12 13:35     ` Greg KH
2023-06-12 14:13       ` Peter Zijlstra
2023-06-12 15:44         ` Greg KH [this message]
2023-06-13  7:34           ` Peter Zijlstra
2023-06-13  7:50             ` Greg KH
2023-06-13 10:50               ` Peter Zijlstra
2023-06-12  9:08 ` [PATCH v3 47/57] perf: Simplify perf_pmu_register() Peter Zijlstra
2023-06-12  9:08 ` [PATCH v3 48/57] perf: Simplify perf_init_event() Peter Zijlstra
2023-06-12  9:08 ` [PATCH v3 49/57] perf: Simplify perf_event_alloc() Peter Zijlstra
2023-06-12  9:08 ` [PATCH v3 50/57] perf: Simplify sys_perf_event_open() Peter Zijlstra
2023-06-12  9:08 ` [PATCH v3 51/57] perf: Simplify perf_event_create_kernel_counter() Peter Zijlstra
2023-06-12  9:08 ` [PATCH v3 52/57] perf: Simplify perf_event_init_context() Peter Zijlstra
2023-06-12  9:08 ` [PATCH v3 53/57] perf: Simplify perf_event_sysfs_init() Peter Zijlstra
2023-06-12  9:08 ` [PATCH v3 54/57] perf: Misc cleanups Peter Zijlstra
2023-06-12  9:08 ` [PATCH v3 55/57] perf: Simplify find_get_context() Peter Zijlstra
2023-06-12  9:08 ` [PATCH v3 56/57] perf: Simplify perf_pmu_output_stop() Peter Zijlstra
2023-06-12 16:19   ` Linus Torvalds
2023-06-12 17:11     ` Sean Christopherson
2023-06-12 17:43       ` Linus Torvalds
2023-06-12 18:55     ` Peter Zijlstra
2023-06-12  9:08 ` [PATCH v3 57/57] perf: Simplify perf_install_in_context() Peter Zijlstra
2023-06-12  9:51 ` [PATCH v3 00/57] Scope-based Resource Management Peter Zijlstra
2023-06-12 16:37 ` Linus Torvalds

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=2023061217-mutable-curry-c2ac@gregkh \
    --to=gregkh@linuxfoundation.org \
    --cc=42.hyeyoo@gmail.com \
    --cc=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=apw@canonical.com \
    --cc=boqun.feng@gmail.com \
    --cc=bristot@redhat.com \
    --cc=bsegall@google.com \
    --cc=catalin.marinas@arm.com \
    --cc=cl@linux.com \
    --cc=dennis@kernel.org \
    --cc=dietmar.eggemann@arm.com \
    --cc=dmaengine@vger.kernel.org \
    --cc=dwaipayanray1@gmail.com \
    --cc=error27@gmail.com \
    --cc=frederic@kernel.org \
    --cc=irogers@google.com \
    --cc=jiangshanlai@gmail.com \
    --cc=jmorris@namei.org \
    --cc=joe@perches.com \
    --cc=joel@joelfernandes.org \
    --cc=john.johansen@canonical.com \
    --cc=jolsa@kernel.org \
    --cc=josh@joshtriplett.org \
    --cc=juri.lelli@redhat.com \
    --cc=keescook@chromium.org \
    --cc=linux-kbuild@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=linux-security-module@vger.kernel.org \
    --cc=llvm@lists.linux.dev \
    --cc=longman@redhat.com \
    --cc=luc.vanoostenryck@gmail.com \
    --cc=lukas.bulwahn@gmail.com \
    --cc=mark.rutland@arm.com \
    --cc=masahiroy@kernel.org \
    --cc=mathieu.desnoyers@efficios.com \
    --cc=mgorman@suse.de \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=nathan@kernel.org \
    --cc=ndesaulniers@google.com \
    --cc=nicolas@fjasle.eu \
    --cc=ojeda@kernel.org \
    --cc=paul@paul-moore.com \
    --cc=paulmck@kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=peterz@infradead.org \
    --cc=quic_neeraju@quicinc.com \
    --cc=ravi.bangoria@amd.com \
    --cc=rcu@vger.kernel.org \
    --cc=rientjes@google.com \
    --cc=roman.gushchin@linux.dev \
    --cc=rostedt@goodmis.org \
    --cc=serge@hallyn.com \
    --cc=tglx@linutronix.de \
    --cc=tj@kernel.org \
    --cc=torvalds@linux-foundation.org \
    --cc=trix@redhat.com \
    --cc=vbabka@suse.cz \
    --cc=vincent.guittot@linaro.org \
    --cc=vkoul@kernel.org \
    --cc=vschneid@redhat.com \
    --cc=will@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).