提交 dad6d5e7 编写于 作者: G Greg Kurz 提交者: Michael Roth

9p: Lock directory streams with a CoMutex

Locking was introduced in QEMU 2.7 to address the deprecation of
readdir_r(3) in glibc 2.24. It turns out that the frontend code is
the worst place to handle a critical section with a pthread mutex:
the code runs in a coroutine on behalf of the QEMU mainloop and then
yields control, waiting for the fsdev backend to process the request
in a worker thread. If the client resends another readdir request for
the same fid before the previous one finally unlocked the mutex, we're
deadlocked.

This never bit us because the linux client serializes readdir requests
for the same fid, but it is quite easy to demonstrate with a custom
client.

A good solution could be to narrow the critical section in the worker
thread code and to return a copy of the dirent to the frontend, but
this causes quite some changes in both 9p.c and codir.c. So, instead
of that, in order for people to easily backport the fix to older QEMU
versions, let's simply use a CoMutex since all the users for this
sit in coroutines.

Fixes: 7cde47d4 ("9p: add locking to V9fsDir")
Reviewed-by: NChristian Schoenebeck <qemu_oss@crudebyte.com>
Message-Id: <158981894794.109297.3530035833368944254.stgit@bahia.lan>
Signed-off-by: NGreg Kurz <groug@kaod.org>
(cherry picked from commit ed463454)
Signed-off-by: NMichael Roth <mdroth@linux.vnet.ibm.com>
上级 ad56aecb
......@@ -186,22 +186,22 @@ typedef struct V9fsXattr
typedef struct V9fsDir {
DIR *stream;
QemuMutex readdir_mutex;
CoMutex readdir_mutex;
} V9fsDir;
static inline void v9fs_readdir_lock(V9fsDir *dir)
{
qemu_mutex_lock(&dir->readdir_mutex);
qemu_co_mutex_lock(&dir->readdir_mutex);
}
static inline void v9fs_readdir_unlock(V9fsDir *dir)
{
qemu_mutex_unlock(&dir->readdir_mutex);
qemu_co_mutex_unlock(&dir->readdir_mutex);
}
static inline void v9fs_readdir_init(V9fsDir *dir)
{
qemu_mutex_init(&dir->readdir_mutex);
qemu_co_mutex_init(&dir->readdir_mutex);
}
/*
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册