Freeing physical pages mapped in the MDL

Hi All,

I have question regarding freeing of physical pages mapped to MDL using this function MmFreePagesFromMdl.
I have a code flow like this.

  1. User mode allocates a memory and invokes IOCTL (Direct_IN)
  2. My driver finds the physical pages passed in the MDL structure from IRP
  3. Programs the device using the physical pages
  4. Then try to deallocate the memory occupied by the physical pages

Does this cause any memory corruption or access violation since the I feel the system also will try to free the physical pages when completing the IRP request.

Any pointer will be appreciated…

Thanks,

Yeah, you didn’t allocate those pages so don’t free them.

1 Like

You don’t own that memory, so of course you are not allowed to free the pages. They will be freed by the owner.

And please remember that even with METHOD_IN_DIRECT, the first buffer in the DeviceIoControl call is COPIED into kernel memory. It is only the second buffer that is mapped.

1 Like