This helper function provides a way to determine if a ram block
is backed by memfd by checking the SEALS on the associated fd.
This is useful in scenarios where we need to quickly verify if
a given memory region is associated with a memfd or not.

Cc: Marc-André Lureau <[email protected]>
Cc: Alex Bennée <[email protected]>
Cc: Akihiko Odaki <[email protected]>
Cc: Dmitry Osipenko <[email protected]>
Cc: Alex Williamson <[email protected]>
Cc: Cédric Le Goater <[email protected]>
Signed-off-by: Vivek Kasireddy <[email protected]>
---
 hw/display/virtio-gpu-dmabuf.c | 23 +++++++++++++++--------
 1 file changed, 15 insertions(+), 8 deletions(-)

diff --git a/hw/display/virtio-gpu-dmabuf.c b/hw/display/virtio-gpu-dmabuf.c
index c34d4c85bc..258c48d31b 100644
--- a/hw/display/virtio-gpu-dmabuf.c
+++ b/hw/display/virtio-gpu-dmabuf.c
@@ -27,6 +27,19 @@
 #include "standard-headers/linux/udmabuf.h"
 #include "standard-headers/drm/drm_fourcc.h"
 
+static bool ram_block_is_memfd_backed(RAMBlock *rb)
+{
+    int ret;
+
+    if (rb && rb->fd > 0) {
+       ret = fcntl(rb->fd, F_GET_SEALS);
+       if (ret > 0) {
+           return true;
+       }
+    }
+    return false;
+}
+
 static void virtio_gpu_create_udmabuf(struct virtio_gpu_simple_resource *res)
 {
     struct udmabuf_create_list *list;
@@ -94,20 +107,14 @@ static void virtio_gpu_destroy_dmabuf(struct 
virtio_gpu_simple_resource *res)
 static int find_memory_backend_type(Object *obj, void *opaque)
 {
     bool *memfd_backend = opaque;
-    int ret;
 
     if (object_dynamic_cast(obj, TYPE_MEMORY_BACKEND)) {
         HostMemoryBackend *backend = MEMORY_BACKEND(obj);
-        RAMBlock *rb = backend->mr.ram_block;
 
-        if (rb && rb->fd > 0) {
-            ret = fcntl(rb->fd, F_GET_SEALS);
-            if (ret > 0) {
-                *memfd_backend = true;
-            }
+        if (ram_block_is_memfd_backed(backend->mr.ram_block)) {
+            *memfd_backend = true;
         }
     }
-
     return 0;
 }
 
-- 
2.50.1


Reply via email to