# Memfd Preservation via LUO

_Peios Kernel / Internals / Memory management_

> /core-api/liveupdate

<a id="rst-memfd-preservation-via-luo"></a>

<!-- SPDX-License-Identifier: GPL-2.0-or-later -->


**Overview**

Memory file descriptors (memfd) can be preserved over a kexec using the Live
Update Orchestrator (LUO) file preservation. This allows userspace to
transfer its memory contents to the next kernel after a kexec.

The preservation is not intended to be transparent. Only select properties of
the file are preserved. All others are reset to default. The preserved
properties are described below.

> [!NOTE]
> The LUO API is not stabilized yet, so the preserved properties of a memfd
> are also not stable and are subject to backwards incompatible changes.

> [!NOTE]
> Currently a memfd backed by Hugetlb is not supported. Memfds created
> with `MFD_HUGETLB` will be rejected.

**Preserved Properties**

The following properties of the memfd are preserved across kexec:

- File Contents

  All data stored in the file is preserved.

- File Size

  The size of the file is preserved. Holes in the file are filled by
  allocating pages for them during preservation.

- File Position

  The current file position is preserved, allowing applications to continue
  reading/writing from their last position.

- File Status Flags

  memfds are always opened with `O_RDWR` and `O_LARGEFILE`. This property
  is maintained.

**Non-Preserved Properties**

All properties which are not preserved must be assumed to be reset to
default. This section describes some of those properties which may be more of
note.

- `FD_CLOEXEC` flag

  A memfd can be created with the `MFD_CLOEXEC` flag that sets the
  `FD_CLOEXEC` on the file. This flag is not preserved and must be set
  again after restore via `fcntl()`.

- Seals:
  File seals are not preserved. The file is unsealed on restore and if
  needed, must be sealed again via `fcntl()`.


<a id="rst-memfd-preservation-abi"></a>

## Memfd Preservation ABI





#### <a id="c-MEMFD_LUO_FOLIO_DIRTY"></a>`MEMFD_LUO_FOLIO_DIRTY`

```c
MEMFD_LUO_FOLIO_DIRTY
```

The folio is dirty.

**Description**

This flag indicates the folio contains data from user. A non-dirty folio is
one that was allocated (say using fallocate(2)) but not written to.

#### <a id="c-MEMFD_LUO_FOLIO_UPTODATE"></a>`MEMFD_LUO_FOLIO_UPTODATE`

```c
MEMFD_LUO_FOLIO_UPTODATE
```

The folio is up-to-date.

**Description**

An up-to-date folio has been zeroed out. shmem zeroes out folios on first
use. This flag tracks which folios need zeroing.

#### <a id="c-struct-memfd_luo_folio_ser"></a>`struct memfd_luo_folio_ser`

Serialized state of a single folio.

**Definition**

```c
struct memfd_luo_folio_ser {
    u64 pfn:52;
    u64 flags:12;
    u64 index;
};
```

**Members**

- `pfn`: The page frame number of the folio.
- `flags`: Flags to describe the state of the folio.
- `index`: The page offset (pgoff_t) of the folio within the original file.

#### <a id="c-struct-memfd_luo_ser"></a>`struct memfd_luo_ser`

Main serialization structure for a memfd.

**Definition**

```c
struct memfd_luo_ser {
    u64 pos;
    u64 size;
    u64 nr_folios;
    struct kho_vmalloc folios;
};
```

**Members**

- `pos`: The file's current position (f_pos).
- `size`: The total size of the file in bytes (i_size).
- `nr_folios`: Number of folios in the folios array.
- `folios`: KHO vmalloc descriptor pointing to the array of
  struct memfd_luo_folio_ser.


<a id="rst-see-also"></a>

## See Also

- [Live Update Orchestrator](</kernel/internals/core-api/liveupdate.md>)

- [Kexec Handover Subsystem](</kernel/internals/core-api/kho/index.md>)
