Received: from eggs.gnu.org ([2001:4830:134:3::10]:42331) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eZxZf-0008HU-Dq for qemu-devel@nongnu.org; Fri, 12 Jan 2018 06:32:41 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eZxZa-0006LM-Nh for qemu-devel@nongnu.org; Fri, 12 Jan 2018 06:32:39 -0500 Received: from lhrrgout.huawei.com ([194.213.3.17]:38566 helo=huawei.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1eZxZa-0006H9-EB for qemu-devel@nongnu.org; Fri, 12 Jan 2018 06:32:34 -0500 From: Antonios Motakis Message-ID: <081955e1-84ec-4877-72d4-f4e8b46be350@huawei.com> Date: Fri, 12 Jan 2018 19:32:10 +0800 MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="------------303602ACDA40D510D798D283" Subject: [Qemu-devel] [RFC] qid path collision issues in 9pfs List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: "qemu-devel@nongnu.org" , Greg Kurz Cc: Veaceslav Falico , Jani Kokkonen , Eduard Shishkin , "vfalico@gmail.com" , "Wangguoli (Andy)" , Jiangyiwen , "zhangwei (CR)" --------------303602ACDA40D510D798D283 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Hello all, We have found an issue in the 9p implementation of QEMU, with how qid pat= hs are generated, which can cause qid path collisions and several issues = caused by them. In our use case (running containers under VMs) these have= proven to be critical. In particular, stat_to_qid in hw/9pfs/9p.c generates a qid path using the= inode number of the file as input. According to the 9p spec the path sho= uld be able to uniquely identify a file, distinct files should not share = a path value. The current implementation that defines qid.path =3D inode nr works fine = as long as there are not files from multiple partitions visible under the= 9p share. In that case, distinct files from different devices are allowe= d to have the same inode number. So with multiple partitions, we have a v= ery high probability of qid path collisions. How to demonstrate the issue: 1) Prepare a problematic share: - mount one partition under share/p1/ with some files inside - mount another one *with identical contents* under share/p2/ - confirm that both partitions have files with same inode nr, size, etc 2) Demonstrate breakage: - start a VM with a virtio-9p pointing to the share - mount 9p share with FSCACHE on - keep open share/p1/file - open and write to share/p2/file What should happen is, the guest will consider share/p1/file and share/p2= /file to be the same file, and since we are using the cache it will not r= eopen it. We intended to write to partition 2, but we just wrote to parti= tion 1. This is just one example on how the guest may rely on qid paths b= eing unique. In the use case of containers where we commonly have a few containers per= VM, all based on similar images, these kind of qid path collisions are v= ery common and they seem to cause all kinds of funny behavior (sometimes = very subtle). To avoid this situation, the device id of a file needs to be also taken a= s input for generating a qid path. Unfortunately, the size of both inode = nr + device id together would be 96 bits, while we have only 64 bits for = the qid path, so we can't just append them and call it a day :( We have thought of a few approaches, but we would definitely like to hear= what the upstream maintainers and community think: * Full fix: Change the 9p protocol We would need to support a longer qid path, based on a virtio feature fla= g. This would take reworking of host and guest parts of virtio-9p, so bot= h QEMU and Linux for most users. * Fallback and/or interim solutions A virtio feature flag may be refused by the guest, so we think we still n= eed to make collisions less likely even with 64 bit paths. E.g. 1. XOR the device id with inode nr to produce the qid path (we attach a p= roof of concept patch) 2. 64 bit hash of device id and inode nr 3. other ideas, such as allocating new qid paths and keep a look up table= of some sorts (possibly too expensive) With our proof of concept patch, the issues caused by qid path collisions= go away, so it can be seen as an interim solution of sorts. However, the= chance of collisions is not eliminated, we are just replacing the curren= t strategy, which is almost guaranteed to cause collisions in certain use= cases, with one that makes them more rare. We think that a virtio featur= e flag for longer qid paths is the only way to eliminate these issues com= pletely. This is the extent that we were able to analyze the issue from our side, = we would like to hear if there are some better ideas, or which approach w= ould be more appropriate for upstream. Best regards, --=20 Antonios Motakis Virtualization Engineer Huawei Technologies Duesseldorf GmbH European Research Center Riesstrasse 25, 80992 M=C3=BCnchen --------------303602ACDA40D510D798D283 Content-Type: text/plain; charset="UTF-8"; name="0001-9pfs-stat_to_qid-use-device-id-as-input-to-qid.path.patch" Content-Transfer-Encoding: base64 Content-Dis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