From e2da306755a39f5e5e2563a4b41bc1c317f7d3df Mon Sep 17 00:00:00 2001 From: Shiqing Gao Date: Wed, 18 Oct 2023 01:20:00 +0800 Subject: [PATCH] dm: block_if: support bypassing the Service VM's page cache This patch adds an acrn-dm option `nocache` to bypass the Service VM's page cache. - By default, the Service VM's page cache is utilized. - If `nocache` is specified in acrn-dm parameters, the Service VM's page cache would be bypassed (opening the file/block with O_DIRECT flag). Example to bypass the Service VM's page cache: `add_virtual_device 5 virtio-blk iothread,mq=2,/dev/nvme2n1,writeback,nocache` Tracked-On: #8612 Signed-off-by: Shiqing Gao Acked-by: Wang, Yu1 --- devicemodel/hw/block_if.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/devicemodel/hw/block_if.c b/devicemodel/hw/block_if.c index a3c9841d3..3cb6f6cad 100644 --- a/devicemodel/hw/block_if.c +++ b/devicemodel/hw/block_if.c @@ -829,6 +829,7 @@ blockif_open(const char *optstr, const char *ident, int queue_num, struct iothre int max_discard_sectors, max_discard_seg, discard_sector_alignment; off_t probe_arg[] = {0, 0}; int aio_mode; + int bypass_host_cache, open_flag; pthread_once(&blockif_once, blockif_init); @@ -850,6 +851,9 @@ blockif_open(const char *optstr, const char *ident, int queue_num, struct iothre /* writethru is on by default */ writeback = 0; + /* By default, do NOT bypass Service VM's page cache. */ + bypass_host_cache = 0; + candiscard = 0; if (queue_num <= 0) @@ -874,6 +878,8 @@ blockif_open(const char *optstr, const char *ident, int queue_num, struct iothre writeback = 0; else if (!strcmp(cp, "ro")) ro = 1; + else if (!strcmp(cp, "nocache")) + bypass_host_cache = 1; else if (!strncmp(cp, "discard", strlen("discard"))) { strsep(&cp, "="); if (cp != NULL) { @@ -933,8 +939,12 @@ blockif_open(const char *optstr, const char *ident, int queue_num, struct iothre * after file is opened. Instead, we call fsync() after each write * operation to emulate it. */ + open_flag = (ro ? O_RDONLY : O_RDWR); + if (bypass_host_cache == 1) { + open_flag |= O_DIRECT; + } + fd = open(nopt, open_flag); - fd = open(nopt, ro ? O_RDONLY : O_RDWR); if (fd < 0 && !ro) { /* Attempt a r/w fail with a r/o open */ fd = open(nopt, O_RDONLY);