All of lore.kernel.org
 help / color / mirror / Atom feed
From: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
To: Alan Stern <stern@rowland.harvard.edu>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	"Rafael J. Wysocki" <rafael@kernel.org>,
	Peter Zijlstra <peterz@infradead.org>,
	LKML <linux-kernel@vger.kernel.org>,
	USB list <linux-usb@vger.kernel.org>,
	Hillf Danton <hdanton@sina.com>,
	Linus Torvalds <torvalds@linux-foundation.org>
Subject: Re: Converting dev->mutex into dev->spinlock ?
Date: Mon, 6 Feb 2023 23:13:38 +0900	[thread overview]
Message-ID: <827177aa-bb64-87a9-e1af-dfe070744045@I-love.SAKURA.ne.jp> (raw)
In-Reply-To: <Y98FLlr7jkiFlV0k@rowland.harvard.edu>

On 2023/02/05 10:23, Alan Stern wrote:
> I suppose we could create separate lockdep classes for every bus_type 
> and device_type combination, as well as for the different sorts of 
> devices -- treat things like class devices separately from normal 
> devices, and so on.  But even then there would be trouble.

Sorry, since I'm not familiar with devices, I can't interpret what you
are talking about in this response. But why don't you try test5() approach
in an example module shown below (i.e. treat all dev->mutex instances
independent to each other) ?

Sharing mutex_init() (like test2() approach) causes false positives,
but allocating a key on each dev->mutex (like test5() approach) should
avoid false positives.

----------
#include <linux/module.h>
#include <linux/slab.h>

static struct mutex *alloc_mutex(void)
{
	return kzalloc(sizeof(struct mutex), GFP_KERNEL | __GFP_NOFAIL);
}

static void free_mutex(struct mutex *m)
{
	kfree(m);
}

static void test1(void)
{
	struct mutex *parent_mutex;
	struct mutex *peer_mutex[4];
	int i;

	pr_info("Running %s\n", __func__);
	parent_mutex = alloc_mutex();
	mutex_init(parent_mutex);

	peer_mutex[0] = alloc_mutex();
	mutex_init(peer_mutex[0]);
	peer_mutex[1] = alloc_mutex();
	mutex_init(peer_mutex[1]);
	peer_mutex[2] = alloc_mutex();
	mutex_init(peer_mutex[2]);
	peer_mutex[3] = alloc_mutex();
	mutex_init(peer_mutex[3]);

	mutex_lock(parent_mutex);
	for (i = 0; i < 4; i++)
		mutex_lock(peer_mutex[i]);
	for (i = 0; i < 4; i++)
		mutex_unlock(peer_mutex[i]);
	mutex_unlock(parent_mutex);
	for (i = 0; i < 4; i++)
		free_mutex(peer_mutex[i]);
	free_mutex(parent_mutex);
}

static void test2(void)
{
	struct mutex *parent_mutex;
	struct mutex *peer_mutex[4];
	int i;

	pr_info("Running %s\n", __func__);
	parent_mutex = alloc_mutex();
	mutex_init(parent_mutex);
	for (i = 0; i < 4; i++) {
		peer_mutex[i] = alloc_mutex();
		mutex_init(peer_mutex[i]);
	}
	mutex_lock(parent_mutex);
	for (i = 0; i < 4; i++)
		mutex_lock(peer_mutex[i]);
	for (i = 0; i < 4; i++)
		mutex_unlock(peer_mutex[i]);
	mutex_unlock(parent_mutex);
	for (i = 0; i < 4; i++)
		free_mutex(peer_mutex[i]);
	free_mutex(parent_mutex);
}

static void test3(void)
{
	struct mutex *parent_mutex;
	struct mutex *peer_mutex[4];
	int i;

	pr_info("Running %s\n", __func__);
	parent_mutex = alloc_mutex();
	mutex_init(parent_mutex);
	for (i = 0; i < 4; i++) {
		peer_mutex[i] = alloc_mutex();
		switch (i) {
		case 0:
			mutex_init(peer_mutex[i]);
			break;
		case 1:
			mutex_init(peer_mutex[i]);
			break;
		case 2:
			mutex_init(peer_mutex[i]);
			break;
		case 3:
			mutex_init(peer_mutex[i]);
			break;
		}
	}
	mutex_lock(parent_mutex);
	for (i = 0; i < 4; i++)
		mutex_lock(peer_mutex[i]);
	for (i = 0; i < 4; i++)
		mutex_unlock(peer_mutex[i]);
	mutex_unlock(parent_mutex);
	for (i = 0; i < 4; i++)
		free_mutex(peer_mutex[i]);
	free_mutex(parent_mutex);
}

static void test4(void)
{
	struct mutex *parent_mutex;
	struct mutex *peer_mutex[4];
	int i;

	pr_info("Running %s\n", __func__);
	parent_mutex = alloc_mutex();
	mutex_init(parent_mutex);
	for (i = 0; i < 4; i++) {
		peer_mutex[i] = alloc_mutex();
		switch (i) {
		case 0:
			mutex_init(peer_mutex[i]);
			break;
		case 1:
			mutex_init(peer_mutex[i]);
			break;
		case 2:
			mutex_init(peer_mutex[i]);
			break;
		case 3:
			mutex_init(peer_mutex[i]);
			break;
		}
	}
	mutex_lock(parent_mutex);
	for (i = 0; i < 4; i++)
		mutex_lock(peer_mutex[i]);
	for (i = 0; i < 4; i++)
		mutex_unlock(peer_mutex[i]);
	for (i = 3; i >= 0; i--)
		mutex_lock(peer_mutex[i]);
	for (i = 3; i >= 0; i--)
		mutex_unlock(peer_mutex[i]);
	mutex_unlock(parent_mutex);
	for (i = 0; i < 4; i++)
		free_mutex(peer_mutex[i]);
	free_mutex(parent_mutex);
}

struct mutex2 {
	struct mutex mutex;
	struct lock_class_key key;
};

static struct mutex2 *alloc_mutex2(void)
{
	struct mutex2 *m = kzalloc(sizeof(struct mutex2), GFP_KERNEL | __GFP_NOFAIL);

	lockdep_register_key(&m->key);
	mutex_init(&m->mutex);
	lockdep_set_class(&m->mutex, &m->key);
	return m;
}

static void free_mutex2(struct mutex2 *m)
{
	mutex_destroy(&m->mutex);
	lockdep_unregister_key(&m->key);
	kfree(m);
}

static void test5(void)
{
	struct mutex2 *parent_mutex;
	struct mutex2 *peer_mutex[4];
	int i;

	pr_info("Running %s\n", __func__);
	parent_mutex = alloc_mutex2();
	for (i = 0; i < 4; i++)
		peer_mutex[i] = alloc_mutex2();
	mutex_lock(&parent_mutex->mutex);
	for (i = 0; i < 4; i++)
		mutex_lock(&peer_mutex[i]->mutex);
	for (i = 0; i < 4; i++)
		mutex_unlock(&peer_mutex[i]->mutex);
	mutex_unlock(&parent_mutex->mutex);
	for (i = 0; i < 4; i++)
		free_mutex2(peer_mutex[i]);
	free_mutex2(parent_mutex);
}

static void test6(void)
{
	struct mutex2 *parent_mutex;
	struct mutex2 *peer_mutex[4];
	int i;

	pr_info("Running %s\n", __func__);
	parent_mutex = alloc_mutex2();
	for (i = 0; i < 4; i++)
		peer_mutex[i] = alloc_mutex2();
	mutex_lock(&parent_mutex->mutex);
	for (i = 0; i < 4; i++)
		mutex_lock(&peer_mutex[i]->mutex);
	for (i = 0; i < 4; i++)
		mutex_unlock(&peer_mutex[i]->mutex);
	for (i = 3; i >= 0; i--)
		mutex_lock(&peer_mutex[i]->mutex);
	for (i = 3; i >= 0; i--)
		mutex_unlock(&peer_mutex[i]->mutex);
	mutex_unlock(&parent_mutex->mutex);
	for (i = 0; i < 4; i++)
		free_mutex2(peer_mutex[i]);
	free_mutex2(parent_mutex);
}

static int __init lockdep_test_init(void)
{
	if (1)
		test1(); // safe and lockdep does not complain
	if (0)
		test2(); // safe but lockdep complains
	if (1)
		test3(); // safe and lockdep does not complain
	if (0)
		test4(); // unsafe and lockdep complains
	if (1)
		test5(); // safe and lockdep does not complain
	if (0)
		test6(); // unsafe and lockdep complains
        return -EINVAL;
}

module_init(lockdep_test_init);
MODULE_LICENSE("GPL");
----------


  reply	other threads:[~2023-02-06 14:14 UTC|newest]

Thread overview: 78+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-04 13:32 Converting dev->mutex into dev->spinlock ? Tetsuo Handa
2023-02-04 13:47 ` Greg Kroah-Hartman
2023-02-04 14:21   ` Tetsuo Handa
2023-02-04 14:34     ` Greg Kroah-Hartman
2023-02-04 15:16       ` Tetsuo Handa
2023-02-04 15:34     ` Alan Stern
2023-02-04 16:12       ` Tetsuo Handa
2023-02-04 16:27         ` Alan Stern
2023-02-04 17:09           ` Tetsuo Handa
2023-02-04 20:01             ` Alan Stern
2023-02-04 20:14               ` Linus Torvalds
2023-02-05  1:23                 ` Alan Stern
2023-02-06 14:13                   ` Tetsuo Handa [this message]
2023-02-06 15:45                     ` Alan Stern
2023-02-07 13:07                       ` Tetsuo Handa
2023-02-07 17:46                         ` Alan Stern
2023-02-07 22:17                           ` Tetsuo Handa
2023-02-08  0:34                             ` Alan Stern
     [not found]                             ` <20230208080739.1649-1-hdanton@sina.com>
2023-02-08 10:30                               ` [PATCH] drivers/core: Replace lockdep_set_novalidate_class() with unique class keys Tetsuo Handa
2023-02-08 15:07                                 ` Alan Stern
2023-02-09  0:22                                   ` Tetsuo Handa
2023-02-09  0:46                                     ` Linus Torvalds
2023-02-09  1:50                                       ` Tetsuo Handa
2023-02-09  2:26                                     ` Alan Stern
2023-02-11  2:04                                       ` Tetsuo Handa
2023-02-11 21:41                                         ` [PATCH RFC] " Alan Stern
2023-02-11 21:51                                           ` Linus Torvalds
2023-02-11 23:06                                             ` Kent Overstreet
2023-02-11 23:08                                               ` Kent Overstreet
2023-02-11 23:24                                               ` Kent Overstreet
2023-02-12  2:40                                                 ` Alan Stern
2023-02-12  2:46                                                   ` Kent Overstreet
2023-02-12  3:03                                                     ` Alan Stern
2023-02-12  3:10                                                       ` Kent Overstreet
2023-02-12 15:23                                                         ` Alan Stern
2023-02-12 19:14                                                           ` Kent Overstreet
2023-02-12 20:19                                                             ` Alan Stern
2023-02-12 20:51                                                               ` Kent Overstreet
2023-02-13  1:23                                                                 ` Alan Stern
2023-02-13  2:21                                                                   ` Kent Overstreet
2023-02-13 15:25                                                                     ` Alan Stern
2023-02-13  9:29                                                                 ` Peter Zijlstra
2023-02-13  9:27                                                               ` Peter Zijlstra
2023-02-13 15:28                                                                 ` Alan Stern
2023-02-13 16:36                                                                   ` Peter Zijlstra
2023-02-13  9:24                                                           ` Peter Zijlstra
2023-02-13 15:25                                                             ` Alan Stern
2023-02-13 16:29                                                               ` Peter Zijlstra
2023-02-14  1:51                                                                 ` Boqun Feng
2023-02-14  1:53                                                                   ` Boqun Feng
2023-02-14  2:03                                                                   ` Alan Stern
2023-02-14  2:09                                                                     ` Boqun Feng
     [not found]                                                                     ` <20230214052733.3354-1-hdanton@sina.com>
2023-02-14  5:55                                                                       ` Boqun Feng
2023-02-14 10:48                                                                   ` Peter Zijlstra
2023-02-14 16:22                                                                     ` Boqun Feng
2023-02-15 10:45                                                                       ` Peter Zijlstra
2023-02-20 17:32                                                                         ` Boqun Feng
2023-02-13 18:46                                                             ` Kent Overstreet
2023-02-14 11:05                                                               ` Peter Zijlstra
2023-02-14 20:05                                                                 ` Alan Stern
2023-02-15 10:33                                                                   ` Peter Zijlstra
2023-02-14 20:16                                                                 ` Kent Overstreet
     [not found]                                               ` <20230212013220.2678-1-hdanton@sina.com>
2023-02-12  1:52                                                 ` Kent Overstreet
2023-02-13  9:49                                           ` Peter Zijlstra
2023-02-13 16:18                                             ` Alan Stern
2023-02-13 17:51                                               ` Greg Kroah-Hartman
2023-02-13 18:05                                                 ` Alan Stern
2023-02-05  1:31               ` Converting dev->mutex into dev->spinlock ? Tetsuo Handa
2023-02-05 16:46                 ` Alan Stern
2023-02-06  2:56                   ` Hillf Danton
2023-02-06  4:44                     ` Matthew Wilcox
2023-02-06  5:17                     ` Greg Kroah-Hartman
2023-02-06  6:43                       ` Hillf Danton
2023-02-06  6:48                         ` Greg Kroah-Hartman
2023-02-04 15:12 ` Alan Stern
2023-02-04 15:30   ` Tetsuo Handa
2023-02-04 15:40     ` Alan Stern
2023-02-05  0:21       ` Hillf Danton

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=827177aa-bb64-87a9-e1af-dfe070744045@I-love.SAKURA.ne.jp \
    --to=penguin-kernel@i-love.sakura.ne.jp \
    --cc=gregkh@linuxfoundation.org \
    --cc=hdanton@sina.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=peterz@infradead.org \
    --cc=rafael@kernel.org \
    --cc=stern@rowland.harvard.edu \
    --cc=torvalds@linux-foundation.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.