1 /*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <array>
18 #include <cstdio>
19 #include <fstream>
20 #include <string>
21
22 #include <android-base/parseint.h>
23 #include <android-base/properties.h>
24 #include <android/api-level.h>
25 #include <gmock/gmock.h>
26 #include <gtest/gtest.h>
27 #include <vintf/VintfObject.h>
28
29 using android::vintf::KernelVersion;
30 using android::vintf::VintfObject;
31 using android::base::ParseInt;
32
33 namespace android {
34 namespace kernel {
35
36 class KernelLoopConfigTest : public ::testing::Test {
37 protected:
38 const int first_api_level_;
KernelLoopConfigTest()39 KernelLoopConfigTest()
40 : first_api_level_(std::stoi(
41 android::base::GetProperty("ro.product.first_api_level", "0"))) {}
should_run() const42 bool should_run() const {
43 // TODO check for APEX support (for upgrading devices)
44 return first_api_level_ >= __ANDROID_API_Q__;
45 }
46 };
47
TEST_F(KernelLoopConfigTest,ValidLoopCountConfig)48 TEST_F(KernelLoopConfigTest, ValidLoopCountConfig) {
49 if (!should_run()) return;
50
51 static constexpr const char* kCmd =
52 "zcat /proc/config.gz | grep CONFIG_BLK_DEV_LOOP_MIN_COUNT";
53 std::array<char, 256> line;
54
55 std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(kCmd, "r"), pclose);
56 ASSERT_NE(pipe, nullptr);
57
58 auto read = fgets(line.data(), line.size(), pipe.get());
59 ASSERT_NE(read, nullptr);
60
61 auto minCountStr = std::string(read);
62
63 auto pos = minCountStr.find("=");
64 ASSERT_NE(pos, std::string::npos);
65 ASSERT_GE(minCountStr.length(), pos + 1);
66
67 int minCountValue = std::stoi(minCountStr.substr(pos + 1));
68 ASSERT_GE(minCountValue, 16);
69
70 std::ifstream max_loop("/sys/module/loop/parameters/max_loop");
71
72 std::string max_loop_str;
73
74 std::getline(max_loop, max_loop_str);
75
76 int max_loop_value;
77
78 ParseInt(max_loop_str, &max_loop_value);
79
80 auto runtime_info = VintfObject::GetRuntimeInfo();
81 ASSERT_NE(nullptr, runtime_info);
82
83 /*
84 * Upstream commit 85c50197716c ("loop: Fix the max_loop commandline argument
85 * treatment when it is set to 0") aligned max_loop to the kernel
86 * documentation, which states that when it is not set, it should be
87 * CONFIG_BLK_DEV_LOOP_MIN_COUNT instead of 0. This commit was applied to
88 * kernels 5.15.86+.
89 *
90 * For kernels older than 5.15.86, ensure that max_loop is not set by ensuring
91 * that it is 0. This ensures that CONFIG_BLK_DEV_LOOP_MIN_COUNT are being
92 * pre-allocated.
93 *
94 * For kernels 5.15.86+ ensure that max_loop is either not set (i.e. it is
95 * CONFIG_BLK_DEV_LOOP_MIN_COUNT), or if it is set, it s greater than
96 * CONFIG_BLK_DEV_LOOP_MIN_COUNT to ensure that at least that many loop
97 * devices are pre-allocated.
98 */
99 if (runtime_info->kernelVersion() < KernelVersion(5, 15, 86)) {
100 EXPECT_EQ(0, max_loop_value);
101 } else {
102 EXPECT_GE(max_loop_value, minCountValue);
103 }
104 }
105
TEST_F(KernelLoopConfigTest,ValidLoopPartParameter)106 TEST_F(KernelLoopConfigTest, ValidLoopPartParameter) {
107 if (!should_run()) return;
108
109 std::ifstream max_part("/sys/module/loop/parameters/max_part");
110
111 std::string max_part_str;
112
113 std::getline(max_part, max_part_str);
114
115 int max_part_value = std::stoi(max_part_str);
116 EXPECT_LE(max_part_value, 7);
117 }
118
119 } // namespace kernel
120 } // namespace android
121