blob: dc3f74fecd19579038aa5aacdee43abd4a44eaa3 [file] [log] [blame]
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001From 0066b862bb3a09f39295abd5d972a53ac8dc1555 Mon Sep 17 00:00:00 2001
2From: Peter Bergin <peter@berginkonsult.se>
3Date: Wed, 19 Sep 2018 15:12:31 +0200
4Subject: [PATCH] rpm/rpmio.c: restrict virtual memory usage if limit set
5
6A solution to avoid OOM situation when the virtual memory is restricted
7for a user (ulimit -v). As the lzopen_internal function is run in parallel
8one instance per CPU thread the available virtual memory is limited per
9CPU thread.
10
11Upstream-Status: Pending [merge of multithreading patches to upstream]
12
13Signed-off-by: Peter Bergin <peter@berginkonsult.se>
Andrew Geissler5199d832021-09-24 16:47:35 -050014Signed-off-by: Ranjitsinh Rathod <ranjitsinh.rathod@kpit.com>
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080015---
Andrew Geissler5199d832021-09-24 16:47:35 -050016 rpmio/rpmio.c | 36 ++++++++++++++++++++++++++++++++++++
17 1 file changed, 36 insertions(+)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080018
19diff --git a/rpmio/rpmio.c b/rpmio/rpmio.c
20index e051c98..b3c56b6 100644
21--- a/rpmio/rpmio.c
22+++ b/rpmio/rpmio.c
Andrew Geissler5199d832021-09-24 16:47:35 -050023@@ -845,6 +845,42 @@ static LZFILE *lzopen_internal(const char *mode, int fd, int xz)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080024 }
25 #endif
26
Andrew Geissler5199d832021-09-24 16:47:35 -050027+ struct rlimit virtual_memory = {RLIM_INFINITY , RLIM_INFINITY};
28+ int status = getrlimit(RLIMIT_AS, &virtual_memory);
29+ if ((status != -1) && (virtual_memory.rlim_cur != RLIM_INFINITY)) {
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080030+ const uint64_t virtual_memlimit = virtual_memory.rlim_cur;
Andrew Geissler5199d832021-09-24 16:47:35 -050031+ uint32_t threads_max = lzma_cputhreads();
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080032+ const uint64_t virtual_memlimit_per_cpu_thread =
Andrew Geissler5199d832021-09-24 16:47:35 -050033+ virtual_memlimit / ((threads_max == 0) ? 1 : threads_max);
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080034+ rpmlog(RPMLOG_NOTICE, "XZ: virtual memory restricted to %lu and "
35+ "per CPU thread %lu\n", virtual_memlimit, virtual_memlimit_per_cpu_thread);
Andrew Geissler5199d832021-09-24 16:47:35 -050036+ uint64_t memory_usage_virt;
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080037+ /* keep reducing the number of compression threads until memory
38+ usage falls below the limit per CPU thread*/
39+ while ((memory_usage_virt = lzma_stream_encoder_mt_memusage(&mt_options)) >
40+ virtual_memlimit_per_cpu_thread) {
Andrew Geissler5199d832021-09-24 16:47:35 -050041+ /* If number of threads goes down to zero or in case of any other error
42+ * lzma_stream_encoder_mt_memusage will return UINT64_MAX. We must check
43+ * for both the cases here to avoid an infinite loop.
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080044+ * If we get into situation that one thread requires more virtual memory
45+ * than available we set one thread, print error message and try anyway. */
Andrew Geissler5199d832021-09-24 16:47:35 -050046+ if ((--mt_options.threads == 0) || (memory_usage_virt == UINT64_MAX)) {
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080047+ mt_options.threads = 1;
48+ rpmlog(RPMLOG_WARNING,
49+ "XZ: Could not adjust number of threads to get below "
50+ "virtual memory limit %lu. usage %lu\n",
51+ virtual_memlimit_per_cpu_thread, memory_usage_virt);
52+ break;
53+ }
54+ }
55+ if (threads != (int)mt_options.threads)
56+ rpmlog(RPMLOG_NOTICE,
57+ "XZ: Adjusted the number of threads from %d to %d to not "
58+ "exceed the memory usage limit of %lu bytes\n",
59+ threads, mt_options.threads, virtual_memlimit);
60+
61+ }
62+
63 ret = lzma_stream_encoder_mt(&lzfile->strm, &mt_options);
64 }
65 #endif
66--
672.7.4
68