| 1 | /* |
| 2 | * SPDX-License-Identifier: MIT |
| 3 | * |
| 4 | * Copyright (C) 2024-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 5 | */ |
| 6 | |
| 7 | #define _LGPL_SOURCE |
| 8 | #include "common/getenv.h" |
| 9 | #include "common/logging.h" |
| 10 | #include "common/populate.h" |
| 11 | |
| 12 | enum populate_policy { |
| 13 | POPULATE_UNSET, |
| 14 | |
| 15 | POPULATE_NONE, |
| 16 | POPULATE_CPU_POSSIBLE, |
| 17 | |
| 18 | POPULATE_UNKNOWN, |
| 19 | }; |
| 20 | |
| 21 | static enum populate_policy map_populate_policy = POPULATE_UNSET; |
| 22 | |
| 23 | static void init_map_populate_policy(void) |
| 24 | { |
| 25 | const char *populate_env_str; |
| 26 | |
| 27 | if (map_populate_policy != POPULATE_UNSET) |
| 28 | return; |
| 29 | |
| 30 | populate_env_str = lttng_ust_getenv("LTTNG_UST_MAP_POPULATE_POLICY"); |
| 31 | if (!populate_env_str) { |
| 32 | map_populate_policy = POPULATE_NONE; |
| 33 | return; |
| 34 | } |
| 35 | if (!strcmp(populate_env_str, "none")) { |
| 36 | map_populate_policy = POPULATE_NONE; |
| 37 | } else if (!strcmp(populate_env_str, "cpu_possible")) { |
| 38 | map_populate_policy = POPULATE_CPU_POSSIBLE; |
| 39 | } else { |
| 40 | /* |
| 41 | * populate_env_str is an untrusted environment variable |
| 42 | * input (can be provided to setuid/setgid binaries), so |
| 43 | * don't even try to print it. |
| 44 | */ |
| 45 | WARN("Unknown policy for LTTNG_UST_MAP_POPULATE_POLICY environment variable."); |
| 46 | map_populate_policy = POPULATE_UNKNOWN; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | /* |
| 51 | * Return the shared page populate policy for global pages. Returns true |
| 52 | * if shared memory pages should be pre-populated, false otherwise. |
| 53 | */ |
| 54 | bool lttng_ust_map_populate_is_enabled(void) |
| 55 | { |
| 56 | init_map_populate_policy(); |
| 57 | |
| 58 | switch (map_populate_policy) { |
| 59 | case POPULATE_UNKNOWN: /* Fall-through */ |
| 60 | case POPULATE_NONE: |
| 61 | return false; |
| 62 | case POPULATE_CPU_POSSIBLE: |
| 63 | return true; |
| 64 | default: |
| 65 | abort(); |
| 66 | } |
| 67 | return false; |
| 68 | } |
| 69 | |
| 70 | /* |
| 71 | * Return the shared page populate policy based on the @cpu number |
| 72 | * provided as input. Returns true if shared memory pages should be |
| 73 | * pre-populated, false otherwise. |
| 74 | * |
| 75 | * The @cpu argument is currently unused except for negative value |
| 76 | * validation. It is present to eventually match cpu affinity or cpu |
| 77 | * online masks if those features are added in the future. |
| 78 | */ |
| 79 | bool lttng_ust_map_populate_cpu_is_enabled(int cpu) |
| 80 | { |
| 81 | /* Reject invalid cpu number. */ |
| 82 | if (cpu < 0) |
| 83 | return false; |
| 84 | |
| 85 | return lttng_ust_map_populate_is_enabled(); |
| 86 | } |