mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Juergen Gross <jgross@suse.com>
To: Boris Ostrovsky <boris.ostrovsky@oracle.com>,
	Stefano Stabellini <sstabellini@kernel.org>
Cc: xen-devel@lists.xen.org, linux-kernel@vger.kernel.org,
	Stefano Stabellini <stefano@aporeto.com>
Subject: Re: [PATCH v2 11/13] xen/pvcalls: implement release command
Date: Tue, 1 Aug 2017 17:34:44 +0200	[thread overview]
Message-ID: <7ace9427-5215-6be7-907a-46dd15ea2a8f@suse.com> (raw)
In-Reply-To: <c081688e-3d88-c6c2-f53f-e2b10641e8f1@oracle.com>

On 01/08/17 17:23, Boris Ostrovsky wrote:
> On 07/31/2017 06:34 PM, Stefano Stabellini wrote:
>> On Thu, 27 Jul 2017, Boris Ostrovsky wrote:
>>>> +int pvcalls_front_release(struct socket *sock)
>>>> +{
>>>> +	struct pvcalls_bedata *bedata;
>>>> +	struct sock_mapping *map;
>>>> +	int req_id, notify;
>>>> +	struct xen_pvcalls_request *req;
>>>> +
>>>> +	if (!pvcalls_front_dev)
>>>> +		return -EIO;
>>>> +	bedata = dev_get_drvdata(&pvcalls_front_dev->dev);
>>>> +	if (!bedata)
>>>> +		return -EIO;
>>> Some (all?) other ops don't check bedata validity. Should they all do?
>> No, I don't think they should: dev_set_drvdata is called in the probe
>> function (pvcalls_front_probe). I'll remove it.
>>
>>
>>>> +
>>>> +	if (sock->sk == NULL)
>>>> +		return 0;
>>>> +
>>>> +	map = (struct sock_mapping *) READ_ONCE(sock->sk->sk_send_head);
>>>> +	if (map == NULL)
>>>> +		return 0;
>>>> +
>>>> +	spin_lock(&bedata->pvcallss_lock);
>>>> +	req_id = bedata->ring.req_prod_pvt & (RING_SIZE(&bedata->ring) - 1);
>>>> +	if (RING_FULL(&bedata->ring) ||
>>>> +	    READ_ONCE(bedata->rsp[req_id].req_id) != PVCALLS_INVALID_ID) {
>>>> +		spin_unlock(&bedata->pvcallss_lock);
>>>> +		return -EAGAIN;
>>>> +	}
>>>> +	WRITE_ONCE(sock->sk->sk_send_head, NULL);
>>>> +
>>>> +	req = RING_GET_REQUEST(&bedata->ring, req_id);
>>>> +	req->req_id = req_id;
>>>> +	req->cmd = PVCALLS_RELEASE;
>>>> +	req->u.release.id = (uint64_t)sock;
>>>> +
>>>> +	bedata->ring.req_prod_pvt++;
>>>> +	RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&bedata->ring, notify);
>>>> +	spin_unlock(&bedata->pvcallss_lock);
>>>> +	if (notify)
>>>> +		notify_remote_via_irq(bedata->irq);
>>>> +
>>>> +	wait_event(bedata->inflight_req,
>>>> +		READ_ONCE(bedata->rsp[req_id].req_id) == req_id);
>>>> +
>>>> +	if (map->active_socket) {
>>>> +		/* 
>>>> +		 * Set in_error and wake up inflight_conn_req to force
>>>> +		 * recvmsg waiters to exit.
>>>> +		 */
>>>> +		map->active.ring->in_error = -EBADF;
>>>> +		wake_up_interruptible(&map->active.inflight_conn_req);
>>>> +
>>>> +		mutex_lock(&map->active.in_mutex);
>>>> +		mutex_lock(&map->active.out_mutex);
>>>> +		pvcalls_front_free_map(bedata, map);
>>>> +		mutex_unlock(&map->active.out_mutex);
>>>> +		mutex_unlock(&map->active.in_mutex);
>>>> +		kfree(map);
>>> Since you are locking here I assume you expect that someone else might
>>> also be trying to lock the map. But you are freeing it immediately after
>>> unlocking. Wouldn't that mean that whoever is trying to grab the lock
>>> might then dereference freed memory?
>> The lock is to make sure there are no recvmsg or sendmsg in progress. We
>> are sure that no newer sendmsg or recvmsg are waiting for
>> pvcalls_front_release to release the lock because before send a message
>> to the backend we set sk_send_head to NULL.
> 
> Is there a chance that whoever is potentially calling send/rcvmsg has
> checked that sk_send_head is non-NULL but hasn't grabbed the lock yet?
> 
> Freeing a structure containing a lock right after releasing the lock
> looks weird (to me). Is there any other way to synchronize with
> sender/receiver? Any other lock?

Right. This looks fishy. Either you don't need the locks or you can't
just free the area right after releasing the lock.

> BTW, I also noticed that in rcvmsg you are calling
> wait_event_interruptible() while holding the lock. Have you tested with
> CONFIG_DEBUG_ATOMIC_SLEEP? (or maybe it's some other config  option that
> would complain about those sorts of thing)

I believe sleeping while holding a mutex is allowed. Sleeping in
spinlocked paths is bad.

BTW: You are looking for CONFIG_DEBUG_MUTEXES (see
Documentation/locking/mutex-design.txt ).


Juergen

  reply	other threads:[~2017-08-01 15:34 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-07-25 21:21 [PATCH v2 00/13] introduce the Xen PV Calls frontend Stefano Stabellini
2017-07-25 21:21 ` [PATCH v2 01/13] xen/pvcalls: introduce the pvcalls xenbus frontend Stefano Stabellini
2017-07-25 21:21   ` [PATCH v2 02/13] xen/pvcalls: connect to the backend Stefano Stabellini
2017-07-26 13:35     ` Boris Ostrovsky
2017-07-27  0:26       ` Stefano Stabellini
2017-07-27 15:07         ` Boris Ostrovsky
2017-07-31 21:55           ` Stefano Stabellini
2017-07-25 21:22   ` [PATCH v2 03/13] xen/pvcalls: implement socket command and handle events Stefano Stabellini
2017-07-26 14:27     ` Boris Ostrovsky
2017-07-26 23:19       ` Stefano Stabellini
2017-07-25 21:22   ` [PATCH v2 04/13] xen/pvcalls: implement connect command Stefano Stabellini
2017-07-26 14:51     ` Boris Ostrovsky
2017-07-26 23:22       ` Stefano Stabellini
2017-07-25 21:22   ` [PATCH v2 05/13] xen/pvcalls: implement bind command Stefano Stabellini
2017-07-26 14:56     ` Boris Ostrovsky
2017-07-26 23:59       ` Stefano Stabellini
2017-07-27 14:43         ` Boris Ostrovsky
2017-07-31 22:17           ` Stefano Stabellini
2017-07-25 21:22   ` [PATCH v2 06/13] xen/pvcalls: implement listen command Stefano Stabellini
2017-07-25 21:22   ` [PATCH v2 07/13] xen/pvcalls: implement accept command Stefano Stabellini
2017-07-26 17:52     ` Boris Ostrovsky
2017-07-26 23:24       ` Stefano Stabellini
2017-07-25 21:22   ` [PATCH v2 08/13] xen/pvcalls: implement sendmsg Stefano Stabellini
2017-07-25 21:22   ` [PATCH v2 09/13] xen/pvcalls: implement recvmsg Stefano Stabellini
2017-07-26 21:21     ` Boris Ostrovsky
2017-07-26 21:33       ` [Xen-devel] " Boris Ostrovsky
2017-07-27  0:08         ` Stefano Stabellini
2017-07-27 14:59           ` Boris Ostrovsky
2017-07-31 22:26             ` Stefano Stabellini
2017-07-25 21:22   ` [PATCH v2 10/13] xen/pvcalls: implement poll command Stefano Stabellini
2017-07-26 23:23     ` Boris Ostrovsky
2017-07-27  0:21       ` Stefano Stabellini
2017-07-25 21:22   ` [PATCH v2 11/13] xen/pvcalls: implement release command Stefano Stabellini
2017-07-27 18:39     ` Boris Ostrovsky
2017-07-31 22:34       ` Stefano Stabellini
2017-08-01 15:23         ` Boris Ostrovsky
2017-08-01 15:34           ` Juergen Gross [this message]
2017-08-01 15:57             ` Boris Ostrovsky
2017-09-11 23:56             ` Stefano Stabellini
2017-07-25 21:22   ` [PATCH v2 12/13] xen/pvcalls: implement frontend disconnect Stefano Stabellini
2017-07-25 21:22   ` [PATCH v2 13/13] xen: introduce a Kconfig option to enable the pvcalls frontend Stefano Stabellini

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=7ace9427-5215-6be7-907a-46dd15ea2a8f@suse.com \
    --to=jgross@suse.com \
    --cc=boris.ostrovsky@oracle.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=sstabellini@kernel.org \
    --cc=stefano@aporeto.com \
    --cc=xen-devel@lists.xen.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

all inboxes | Powered by JetHome®