Skip to content

Commit

Permalink
dm: block_if: support bypassing the Service VM's page cache
Browse files Browse the repository at this point in the history
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: projectacrn#8612

Signed-off-by: Shiqing Gao <[email protected]>
Acked-by: Wang, Yu1 <[email protected]>
  • Loading branch information
shiqingg committed May 30, 2024
1 parent c35d533 commit f907344
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion devicemodel/hw/block_if.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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)
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit f907344

Please sign in to comment.