I have added a variable in the struct thread_info to count certain event.
This is done in a guest OS.
During the execution of Virtual machine I read these variables from my HOST every now and then.
I have obeserved that sometime I get the value which is expected but sometimes I read junk values.I presume that the GCC is optimizing my variable, and the memory I am reading is in garbage state.
I want to know of possible way to prevent.
turnig Off GCC optimization for the kernel is out of question because my objective is to speed up the virtual machine based on the event I have counted.
#pragma optimize("",off)
make it less efficient because then I will have to break my event counting code(which is just 2 lines) into a function. And this event I am counting occurs very often.
Is there a #pragma technique which I can use??
Will making my variable volatile help the cause??
Thanks
Making the variables
volatilewill prevent GCC from optimizing them out. You don’t need to disable optimization altogether.However, you might need to deal with the race condition that results by you trying to read from the struct while the kernel is possibly still updating it. I don’t know how you’d do that in a VM context though. Maybe there’s some special mechanism for guest-host communication provided by the hypervisor you’re using. VMware for example has VMCI.