gralloc: make fbdev fd a shallow copy

We never free private_module_t::framebuffer once it is initialized.
We can use a shallow copy of the fbdev dev.

Bug: 37550237
Test: boots
Change-Id: I552b40c0a59d9f4ec5442e3a284f115a88061c3e
diff --git a/gralloc/alloc_device.cpp b/gralloc/alloc_device.cpp
index 3ef099f..3c4531b 100644
--- a/gralloc/alloc_device.cpp
+++ b/gralloc/alloc_device.cpp
@@ -306,9 +306,10 @@
 		vaddr = (void *)((uintptr_t)vaddr + bufferSize);
 	}
 
+	int fbdev_fd = m->framebuffer->shallow_fbdev_fd;
 	// The entire framebuffer memory is already mapped, now create a buffer object for parts of this memory
 	private_handle_t *hnd = new private_handle_t(private_handle_t::PRIV_FLAGS_FRAMEBUFFER, usage, size, vaddr,
-	        0, dup(m->framebuffer->fd), (uintptr_t)vaddr - (uintptr_t) m->framebuffer->base);
+	        0, fbdev_fd, (uintptr_t)vaddr - (uintptr_t) m->framebuffer->base);
 #if GRALLOC_ARM_UMP_MODULE
 	hnd->ump_id = m->framebuffer->ump_id;
 
@@ -330,7 +331,7 @@
 #ifdef FBIOGET_DMABUF
 		struct fb_dmabuf_export fb_dma_buf;
 
-		if (ioctl(m->framebuffer->fd, FBIOGET_DMABUF, &fb_dma_buf) == 0)
+		if (ioctl(fbdev_fd, FBIOGET_DMABUF, &fb_dma_buf) == 0)
 		{
 			AINF("framebuffer accessed with dma buf (fd 0x%x)\n", (int)fb_dma_buf.fd);
 			hnd->share_fd = fb_dma_buf.fd;
@@ -518,7 +519,6 @@
 		const size_t bufferSize = m->finfo.line_length * m->info.yres;
 		int index = ((uintptr_t)hnd->base - (uintptr_t)m->framebuffer->base) / bufferSize;
 		m->bufferMask &= ~(1 << index);
-		close(hnd->fd);
 
 #if GRALLOC_ARM_UMP_MODULE
 
diff --git a/gralloc/framebuffer_device.cpp b/gralloc/framebuffer_device.cpp
index 3004778..bacefee 100644
--- a/gralloc/framebuffer_device.cpp
+++ b/gralloc/framebuffer_device.cpp
@@ -94,9 +94,11 @@
 #define FBIO_WAITFORVSYNC       _IOW('F', 0x20, __u32)
 #define S3CFB_SET_VSYNC_INT _IOW('F', 206, unsigned int)
 
-		if (ioctl(m->framebuffer->fd, FBIOPAN_DISPLAY, &m->info) == -1)
+		int fbdev_fd = m->framebuffer->shallow_fbdev_fd;
+
+		if (ioctl(fbdev_fd, FBIOPAN_DISPLAY, &m->info) == -1)
 		{
-			AERR("FBIOPAN_DISPLAY failed for fd: %d", m->framebuffer->fd);
+			AERR("FBIOPAN_DISPLAY failed for fd: %d", fbdev_fd);
 			m->base.unlock(&m->base, buffer);
 			return 0;
 		}
@@ -106,9 +108,9 @@
 			// enable VSYNC
 			interrupt = 1;
 
-			if (ioctl(m->framebuffer->fd, S3CFB_SET_VSYNC_INT, &interrupt) < 0)
+			if (ioctl(fbdev_fd, S3CFB_SET_VSYNC_INT, &interrupt) < 0)
 			{
-				//      AERR("S3CFB_SET_VSYNC_INT enable failed for fd: %d", m->framebuffer->fd);
+				//      AERR("S3CFB_SET_VSYNC_INT enable failed for fd: %d", fbdev_fd);
 				return 0;
 			}
 
@@ -118,9 +120,9 @@
 #endif
 			int crtc = 0;
 
-			if (ioctl(m->framebuffer->fd, FBIO_WAITFORVSYNC, &crtc) < 0)
+			if (ioctl(fbdev_fd, FBIO_WAITFORVSYNC, &crtc) < 0)
 			{
-				AERR("FBIO_WAITFORVSYNC failed for fd: %d", m->framebuffer->fd);
+				AERR("FBIO_WAITFORVSYNC failed for fd: %d", fbdev_fd);
 #ifdef MALI_VSYNC_EVENT_REPORT_ENABLE
 				gralloc_mali_vsync_report(MALI_VSYNC_EVENT_END_WAIT);
 #endif
@@ -133,9 +135,9 @@
 			// disable VSYNC
 			interrupt = 0;
 
-			if (ioctl(m->framebuffer->fd, S3CFB_SET_VSYNC_INT, &interrupt) < 0)
+			if (ioctl(fbdev_fd, S3CFB_SET_VSYNC_INT, &interrupt) < 0)
 			{
-				AERR("S3CFB_SET_VSYNC_INT disable failed for fd: %d", m->framebuffer->fd);
+				AERR("S3CFB_SET_VSYNC_INT disable failed for fd: %d", fbdev_fd);
 				return 0;
 			}
 		}
@@ -146,9 +148,9 @@
 		gralloc_mali_vsync_report(MALI_VSYNC_EVENT_BEGIN_WAIT);
 #endif
 
-		if (ioctl(m->framebuffer->fd, FBIOPUT_VSCREENINFO, &m->info) == -1)
+		if (ioctl(fbdev_fd, FBIOPUT_VSCREENINFO, &m->info) == -1)
 		{
-			AERR("FBIOPUT_VSCREENINFO failed for fd: %d", m->framebuffer->fd);
+			AERR("FBIOPUT_VSCREENINFO failed for fd: %d", fbdev_fd);
 #ifdef MALI_VSYNC_EVENT_REPORT_ENABLE
 			gralloc_mali_vsync_report(MALI_VSYNC_EVENT_END_WAIT);
 #endif
@@ -383,7 +385,7 @@
 
 	// Create a "fake" buffer object for the entire frame buffer memory, and store it in the module
 	module->framebuffer = new private_handle_t(private_handle_t::PRIV_FLAGS_FRAMEBUFFER, 0, fbSize, vaddr,
-	        0, dup(fd), 0);
+	        0, fd, 0);
 
 	module->numBuffers = info.yres_virtual / info.yres;
 	module->bufferMask = 0;
diff --git a/gralloc/gralloc_priv.h b/gralloc/gralloc_priv.h
index 5797725..b931ac2 100644
--- a/gralloc/gralloc_priv.h
+++ b/gralloc/gralloc_priv.h
@@ -182,7 +182,7 @@
 #endif
 
 	// Following members is for framebuffer only
-	int     fd;
+	int     shallow_fbdev_fd; // shallow copy, not dup'ed
 	int     offset;
 
 #if GRALLOC_ARM_DMA_BUF_MODULE
@@ -219,7 +219,7 @@
 		yuv_info(MALI_YUV_NO_INFO),
 		ump_id((int)secure_id),
 		ump_mem_handle((int)handle),
-		fd(0),
+		shallow_fbdev_fd(0),
 		offset(0)
 #if GRALLOC_ARM_DMA_BUF_MODULE
 		,
@@ -253,7 +253,7 @@
 		ump_id((int)UMP_INVALID_SECURE_ID),
 		ump_mem_handle((int)UMP_INVALID_MEMORY_HANDLE),
 #endif
-		fd(0),
+		shallow_fbdev_fd(0),
 		offset(0),
 		ion_hnd(ION_INVALID_HANDLE)
 
@@ -286,7 +286,7 @@
 		ump_id((int)UMP_INVALID_SECURE_ID),
 		ump_mem_handle((int)UMP_INVALID_MEMORY_HANDLE),
 #endif
-		fd(fb_file),
+		shallow_fbdev_fd(fb_file),
 		offset(fb_offset)
 #if GRALLOC_ARM_DMA_BUF_MODULE
 		,