Skip to content

openvmm: virtiofs: inode lookup count wraps to zero at u64::MAX #1633

Open

Description

@tangruize

Summary

VirtioFsInode::lookup and VirtioFsInode::inc_lookup increment lookup_count with AtomicU64::fetch_add(1, Ordering::AcqRel), which uses wrapping arithmetic at u64::MAX. When an inode's lookup_count is u64::MAX — reachable directly via microVM saved-state restore without 2^64 live requests — one additional FUSE_LOOKUP, FUSE_READDIRPLUS, or FUSE_LINK wraps lookup_count to 0, allowing the next FUSE_FORGET to evict the still-referenced inode from InodeMap. Found by formal verification.

Root Cause

In vm/devices/virtio/virtiofs/src/inode.rs, both VirtioFsInode::lookup and VirtioFsInode::inc_lookup use fetch_add(1, Ordering::AcqRel) rather than saturating addition, even though VirtioFsInode::forget immediately below already uses AtomicU64::update with saturating_sub:

    /// Increments the lookup count.
    pub fn lookup(&self, new_path: PathBuf) {
        self.lookup_count.fetch_add(1, Ordering::AcqRel);
        self.aliases.write().insert(new_path.clone());
        let mut path = self.path.write();
        *path = new_path;
    }

    /// Increments the lookup count without updating the path.
    ///
    /// This is used when returning an existing inode in a FUSE reply (e.g., for hard links)
    /// where the kernel will track the reference and later send a forget.
    pub fn inc_lookup(&self) {
        self.lookup_count.fetch_add(1, Ordering::AcqRel);
    }

In microVM mode, validate_microvm_state accepts any SavedInode with inode.lookup_count != 0, including u64::MAX, and restores it verbatim into VirtioFsInode::lookup_count.

Consequence

Once lookup_count wraps from u64::MAX to 0, the inode remains in InodeMap with a zero reference count until any FUSE_FORGET(node_id, n) arrives. At that point, Fuse::forget computes 0u64.saturating_sub(n) == 0 and removes the node from InodeMap while the guest still holds active FUSE references to node_id. Subsequent guest operations on node_id fail with EINVAL (request for unknown inode), and in microVM mode save_microvm_state also fails if an open handle still points to the evicted inode (open handle refers to an untracked inode).

Reproduction

  1. Create a temporary directory containing a file "f", instantiate VirtioFs::new(dir.path(), None), and look up "f" once to obtain node_id.
  2. Set that inode's lookup_count to u64::MAX (simulating a restored SavedInode with lookup_count: u64::MAX or cumulative lookups).
  3. Look up "f" a second time (or call inode.inc_lookup()). lookup_count wraps from u64::MAX to 0.
  4. Call fs.forget(node_id, 1) and then fs.get_attr on node_id. forget evicts node_id from InodeMap, and get_attr fails with lx::Error::EINVAL.

Related: #1627.

Activity

  1. tangruize commented on Nov 15, 2026

    @tangruize
    ContributorAuthor

    Suggested Fix

    Update VirtioFsInode::lookup and VirtioFsInode::inc_lookup to use self.lookup_count.update(Ordering::AcqRel, Ordering::Acquire, |count| count.saturating_add(1)), matching the atomic update pattern already used in VirtioFsInode::forget. Saturating at u64::MAX ensures an inode at the maximum representable reference count stays pinned rather than wrapping to zero and being prematurely evicted.

    Patch against OpenVMM 9d64daad3cbe14fb0591081b73c6c9648e18d0fb. Save the contents below as fix.patch and run git apply fix.patch.

    fix.patch
    diff --git a/vm/devices/virtio/virtiofs/src/inode.rs b/vm/devices/virtio/virtiofs/src/inode.rs
    index 6ec061ce3..771b6048e 100644
    --- a/vm/devices/virtio/virtiofs/src/inode.rs
    +++ b/vm/devices/virtio/virtiofs/src/inode.rs
    @@ -232,20 +232,27 @@ pub(crate) fn statx_from(&self, statx: &lx::StatEx) -> fuse_statx {
             sx
         }
     
    -    /// Increments the lookup count.
    +    /// Increments the lookup count, saturating at `u64::MAX`.
         pub fn lookup(&self, new_path: PathBuf) {
    -        self.lookup_count.fetch_add(1, Ordering::AcqRel);
    +        self.lookup_count
    +            .update(Ordering::AcqRel, Ordering::Acquire, |count| {
    +                count.saturating_add(1)
    +            });
             self.aliases.write().insert(new_path.clone());
             let mut path = self.path.write();
             *path = new_path;
         }
     
    -    /// Increments the lookup count without updating the path.
    +    /// Increments the lookup count without updating the path, saturating at
    +    /// `u64::MAX`.
         ///
         /// This is used when returning an existing inode in a FUSE reply (e.g., for hard links)
         /// where the kernel will track the reference and later send a forget.
         pub fn inc_lookup(&self) {
    -        self.lookup_count.fetch_add(1, Ordering::AcqRel);
    +        self.lookup_count
    +            .update(Ordering::AcqRel, Ordering::Acquire, |count| {
    +                count.saturating_add(1)
    +            });
         }
     
         /// Decrements the lookup count, and returns the new count.
    @@ -945,4 +952,76 @@ fn path_keyed_reserved_insert_file_to_directory() {
                 assert_eq!(inodes.insert(same).unwrap().1, current_id);
             }
         }
    +
    +    /// A lookup of an existing node whose `lookup_count` is already `u64::MAX`
    +    /// must saturate rather than wrapping `lookup_count` to zero and allowing a
    +    /// single `forget` to evict the still-referenced node.
    +    #[test]
    +    fn lookup_at_the_largest_count_keeps_the_node() {
    +        use fuse::Fuse;
    +        use fuse::Request;
    +        use fuse::protocol::*;
    +        use zerocopy::FromZeros;
    +        use zerocopy::IntoBytes;
    +
    +        fn request(node_id: u64, opcode: u32) -> Request {
    +            let header = fuse_in_header {
    +                len: (size_of::<fuse_in_header>() + 2) as u32,
    +                opcode,
    +                nodeid: node_id,
    +                ..fuse_in_header::new_zeroed()
    +            };
    +            let mut bytes = header.as_bytes().to_vec();
    +            bytes.extend_from_slice(b".\0");
    +            Request::new(bytes.as_slice()).unwrap()
    +        }
    +
    +        let dir = tempfile::tempdir().unwrap();
    +        std::fs::write(dir.path().join("f"), b"hello").unwrap();
    +        let fs = crate::VirtioFs::new(dir.path(), None).unwrap();
    +
    +        let first = fs
    +            .lookup(&request(FUSE_ROOT_ID, FUSE_LOOKUP), "f".as_ref())
    +            .unwrap();
    +        let node_id = first.nodeid;
    +
    +        fs.inner
    +            .inodes
    +            .read()
    +            .get(node_id)
    +            .unwrap()
    +            .lookup_count
    +            .store(u64::MAX, Ordering::Release);
    +
    +        let again = fs
    +            .lookup(&request(FUSE_ROOT_ID, FUSE_LOOKUP), "f".as_ref())
    +            .unwrap();
    +        assert_eq!(again.nodeid, node_id);
    +
    +        fs.forget(node_id, 1);
    +
    +        fs.get_attr(
    +            &request(node_id, FUSE_GETATTR),
    +            fuse_getattr_in::new_zeroed().getattr_flags,
    +            0,
    +        )
    +        .expect("node with saturated lookup count was evicted after one forget");
    +    }
    +
    +    #[test]
    +    fn inc_lookup_at_the_largest_count_keeps_the_node() {
    +        let tmp = tempfile::tempdir().unwrap();
    +        let volume = Arc::new(VirtioFsVolume::new(
    +            LxVolume::new(tmp.path()).unwrap(),
    +            0,
    +            false,
    +        ));
    +        let (inode, _) = VirtioFsInode::new(volume, PathBuf::new()).unwrap();
    +        inode.lookup_count.store(u64::MAX, Ordering::Release);
    +
    +        inode.inc_lookup();
    +
    +        assert_eq!(inode.lookup_count(), u64::MAX);
    +        assert_ne!(inode.forget(2, 1), 0);
    +    }
     }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: openvmmInitial title-keyword triage suggestion

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions