Prevent channel buffer allocation larger than memory
authorFrancis Deslauriers <francis.deslauriers@efficios.com>
Sat, 17 Nov 2018 03:51:06 +0000 (22:51 -0500)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Sat, 16 Feb 2019 21:04:09 +0000 (16:04 -0500)
commit09b72f7aa737f46196db18bcdf3bc947a08c27a2
tree5e13bac47b629eb21adad7eb4f361e6edfa65787
parentbd3739b081f96057a93a234e6ac1c98a9a44ca39
Prevent channel buffer allocation larger than memory

Background
==========
Until recently (before lttng-modules commit 1f0ab1e) it was possible to
trigger an Out-Of-Memory crash by creating a kernel channel buffer
larger than the currently usable memory on the system. The following
commands was triggering the issue on my laptop:
  lttng create
  lttng enable-channel -k --subbuf-size=100G --num-subbuf=1 chan0

The lttng-modules commit 1f0ab1e adds a verification based on an
estimate to prevent this from happening. Since this kernel tracer sanity
check is based on an estimate, it would safer to do a similar check on
the session daemon side.

Approach
========
Verify that there is enough memory available on the system to do all the
allocations needed to enable the channel. If the available memory is
insufficient for the buffer allocation, return an error to the user
without trying to allocate the buffers.

Use the `/proc/meminfo` procfile to get an estimate of the current size
of available memory (using `MemAvailable`). The `MemAvailable` field was
added in the Linux kernel 3.14, so if it's absent, fallback to verifying
that the requested buffer is smaller than the physical memory on the
system.

Compute the size of the requested buffers using the following equation:
  requested_memory = number_subbuffer * size_subbuffer * number_cpu

The following error is returned to the command line user:
  lttng enable-channel -k --subbuf-size=100G --num-subbuf=1 chan0
  Error: Channel chan0: Not enough memory (session auto-20181121-161146)

Side effect
===========
This patch has the interesting side effect to alerting the user with an
error that buffer allocation has failed because of memory availability
in both --kernel and --userspace channel creation.

Drawback
========
The fallback check on older kernels is imperfect and is only to prevent
obvious user errors.

Note
====
In the future, there might be a need for a way to deactivate this check
(by using an environment variable) if a case arises where
`/proc/meminfo` doesn't accurately reflect the state of memory for a
particular use case.

Signed-off-by: Francis Deslauriers <francis.deslauriers@efficios.com>
src/bin/lttng/conf.c
src/common/utils.c
src/common/utils.h
src/lib/lttng-ctl/lttng-ctl.c
tests/regression/kernel/test_channel [new file with mode: 0755]
tests/root_regression
This page took 0.027108 seconds and 5 git commands to generate.