Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve pool memory management #50

Merged
merged 2 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion PropertySheet.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<PropertyGroup Label="UserMacros">
<OVPN_DCO_VERSION_MAJOR>0</OVPN_DCO_VERSION_MAJOR>
<OVPN_DCO_VERSION_MINOR>9</OVPN_DCO_VERSION_MINOR>
<OVPN_DCO_VERSION_PATCH>3</OVPN_DCO_VERSION_PATCH>
<OVPN_DCO_VERSION_PATCH>4</OVPN_DCO_VERSION_PATCH>
</PropertyGroup>
<PropertyGroup />
<ItemDefinitionGroup>
Expand Down
32 changes: 26 additions & 6 deletions bufferpool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@

#define OVPN_BUFFER_HEADROOM 26 // we prepend TCP packet size (2 bytes) and crypto overhead (24 bytes)

// good enough limit for in-flight packets
constexpr auto MAX_POOL_SIZE = 100'000;

struct OVPN_BUFFER_POOL_IMPL
{
LIST_ENTRY ListHead;
Expand Down Expand Up @@ -145,10 +148,19 @@ OvpnBufferPoolGet(OVPN_BUFFER_POOL handle, POOL_ENTRY** entry) {
if (slist_entry) {
*entry = CONTAINING_RECORD(slist_entry, POOL_ENTRY, PoolListEntry);
} else {
if (pool->PoolSize > MAX_POOL_SIZE)
{
*entry = NULL;
LOG_ERROR("Pool size is too large", TraceLoggingValue(pool->Tag, "tag"), TraceLoggingValue(pool->PoolSize, "size"));
return;
}
*entry = (POOL_ENTRY*)ExAllocatePool2(POOL_FLAG_NON_PAGED, pool->ItemSize, 'ovpn');
InterlockedIncrement(&pool->PoolSize);
if ((pool->PoolSize % 64) == 0) {
LOG_INFO("Pool size", TraceLoggingValue(pool->Tag, "tag"), TraceLoggingValue(pool->PoolSize, "size"));
if (*entry)
{
InterlockedIncrement(&pool->PoolSize);
if ((pool->PoolSize % 256) == 0) {
LOG_INFO("Pool size", TraceLoggingValue(pool->Tag, "tag"), TraceLoggingValue(pool->PoolSize, "size"));
}
}
}
}
Expand All @@ -161,10 +173,17 @@ OvpnTxBufferPoolGet(OVPN_TX_BUFFER_POOL handle, OVPN_TX_BUFFER** buffer)
if (*buffer == NULL)
return STATUS_INSUFFICIENT_RESOURCES;

(*buffer)->Pool = handle;

(*buffer)->Mdl = IoAllocateMdl(*buffer, ((OVPN_BUFFER_POOL_IMPL*)handle)->ItemSize, FALSE, FALSE, NULL);
MmBuildMdlForNonPagedPool((*buffer)->Mdl);
if (((*buffer)->Mdl) == NULL)
{
OvpnTxBufferPoolPut(*buffer);
*buffer = NULL;
return STATUS_INSUFFICIENT_RESOURCES;
}

(*buffer)->Pool = handle;
MmBuildMdlForNonPagedPool((*buffer)->Mdl);

(*buffer)->Data = (*buffer)->Head + OVPN_BUFFER_HEADROOM;
(*buffer)->Tail = (*buffer)->Data;
Expand Down Expand Up @@ -206,7 +225,8 @@ _Use_decl_annotations_
VOID
OvpnTxBufferPoolPut(OVPN_TX_BUFFER* buffer)
{
IoFreeMdl(buffer->Mdl);
if (buffer->Mdl)
IoFreeMdl(buffer->Mdl);

OvpnBufferPoolPut(buffer);
}
Expand Down
2 changes: 1 addition & 1 deletion msm/installer.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{fda93664-ca76-44ef-89aa-625fbe9d64f6}</ProjectGuid>
<RootNamespace>installer</RootNamespace>
<WindowsTargetPlatformVersion>10.0.22000.0</WindowsTargetPlatformVersion>
<WindowsTargetPlatformVersion>10.0.20348.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
Expand Down
2 changes: 1 addition & 1 deletion ovpn-cli/ovpn-cli.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
<ProjectGuid>{ba23900a-7fac-49cf-9ac7-c20cd52f42af}</ProjectGuid>
<RootNamespace>ovpncli</RootNamespace>
<ProjectName>ovpn-dco-cli</ProjectName>
<WindowsTargetPlatformVersion>10.0.22000.0</WindowsTargetPlatformVersion>
<WindowsTargetPlatformVersion>10.0.20348.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
Expand Down
Loading