mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Guilherme Giacomo Simoes <trintaeoitogc@gmail.com>
To: trintaeoitogc@gmail.com
Cc: axboe@kernel.dk, bvanassche@acm.org, josef@toxicpanda.com,
	linux-block@vger.kernel.org, linux-kernel@vger.kernel.org,
	nbd@other.debian.org,
	syzbot+d7d72f3d467710442af0@syzkaller.appspotmail.com
Subject: Re: [PATCH] nbd: fix circular lock dependency
Date: Wed, 11 Nov 2026 09:24:26 -0300	[thread overview]
Message-ID: <20261111122426.225742-1-trintaeoitogc@gmail.com> (raw)
In-Reply-To: <20261111101919.215767-1-trintaeoitogc@gmail.com>

Guilherme Giacomo Simoes <trintaeoitogc@gmail.com> wrote:
> > Additionally, reclassifying the socket in nbd_get_socket() happens
> > before it is known whether NBD will use the socket. If
> > nbd_genl_reconnect_sock_cb() does not find a dead connection, or if
> > nbd_add_socket() fails, the socket is returned to user space with the
> > NBD lock class. While this only affects lockdep and while the
> > nbd_add_socket() error paths already behave this way, it is another
> > reason to only reclassify a socket once it is really being used.
> current the nbd_reclassify_socket() is called just below nbd_get_socket() call
> inside nbd_add_socket():
> ```
>  	sock = nbd_get_socket(nbd, arg, &err);
>  	if (!sock)
>  		return err;
> 	nbd_reclassify_socket(sock);
> ```
> 
> what have exactly same effect that call at end of nbd_get_socket(). I guess
> that "reclassify a socket once it is really being used" must be done putting
> nbd_reclassify_socket at end of nbd_add_socket() (before put_socket tag).
> Maybe something like:
> ```
> static int nbd_add_socket(struct nbd_device *nbd, unsigned long arg,
> 			  bool netlink)
> {
> 	struct nbd_config *config = nbd->config;
> 	struct socket *sock;
> 	struct nbd_sock **socks;
> 	struct nbd_sock *nsock;
> 	int err;
> 
> 	/* Arg will be cast to int, check it to avoid overflow */
> 	if (arg > INT_MAX)
> 		return -EINVAL;
> 
> 	if (nbd->pid) {
> 		dev_err(disk_to_dev(nbd->disk),
> 			"Cannot add socket to a running device\n");
> 		return -EBUSY;
> 	}
> 
> 	sock = nbd_get_socket(nbd, arg, &err);
> 	if (!sock)
> 		return err;
> 
> 	if (!netlink && !nbd->task_setup &&
> 	    !test_bit(NBD_RT_BOUND, &config->runtime_flags))
> 		nbd->task_setup = current;
> 
> 	if (!netlink &&
> 	    (nbd->task_setup != current ||
> 	     test_bit(NBD_RT_BOUND, &config->runtime_flags))) {
> 		dev_err(disk_to_dev(nbd->disk),
> 			"Device being setup by another task");
> 		err = -EBUSY;
> 		goto put_socket;
> 	}
> 
> 	nsock = kzalloc_obj(*nsock);
> 	if (!nsock) {
> 		err = -ENOMEM;
> 		goto put_socket;
> 	}
> 
> 	socks = krealloc(config->socks, (config->num_connections + 1) *
> 			 sizeof(struct nbd_sock *), GFP_KERNEL);
> 	if (!socks) {
> 		kfree(nsock);
> 		err = -ENOMEM;
> 		goto put_socket;
> 	}
> 
> +   nbd_reclassify_socket(sock);
> 
> 	config->socks = socks;
> 
> 	nsock->fallback_index = -1;
> 	nsock->dead = false;
> 	mutex_init(&nsock->tx_lock);
> 	nsock->sock = sock;
> 	nsock->pending = NULL;
> 	nsock->sent = 0;
> 	nsock->cookie = 0;
> 	INIT_WORK(&nsock->work, nbd_pending_cmd_work);
> 	socks[config->num_connections++] = nsock;
> 	atomic_inc(&config->live_connections);
> 
> 	return 0;
> 
> put_socket:
> 	sockfd_put(sock);
> 	return err;
> }
> ```
> 
> I don't know if this is a really good thing once that until this be executed
> the user space can got a generic lock (the same generic lock of sock here)
> before nbd_reclassify_socket() call leading to the same problem.
> Thoughts? 
> 
I back to read the nbd_genl_reconnect_sock_cb(), and now I understand what you say.
YES, nbd_get_socket() also runs for sockets that are never installed.  When
nbd_genl_reconnect_sock_cb() finds no dead connection it does:
```
	sockfd_put(sock);
	kfree(args);
	return 1; 
```

So the add line inside nbd_add_socket() that I sended previous don't make
sense.

What do you think now? We will call nbd_reclassify_socket() inside
nbd_get_socket() and call this only one time? Or is a better way call the
nbd_reclassify_socket() inside nbd_genl_reconnect_sock_cb() when we have sure
that we will use the socket?

thanks,
guilherme

      parent reply	other threads:[~2026-11-11 12:24 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-10 21:57 Guilherme Giacomo Simoes
2026-11-11  0:40 ` Bart Van Assche
2026-11-11 10:19   ` Guilherme Giacomo Simoes
2026-11-11 11:29     ` Timo Joannin
2026-11-11 12:25       ` Guilherme Giacomo Simoes
2026-11-11 11:38     ` Guilherme Giacomo Simoes
2026-11-11 12:24     ` Guilherme Giacomo Simoes [this message]

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=20261111122426.225742-1-trintaeoitogc@gmail.com \
    --to=trintaeoitogc@gmail.com \
    --cc=axboe@kernel.dk \
    --cc=bvanassche@acm.org \
    --cc=josef@toxicpanda.com \
    --cc=linux-block@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nbd@other.debian.org \
    --cc=syzbot+d7d72f3d467710442af0@syzkaller.appspotmail.com \
    /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

all inboxes | Powered by JetHome®