@@ -55,14 +55,30 @@ OPTIONAL_CMA_CONFIGS="CONFIG_CMA_SIZE_MBYTES CONFIG_CMA_SIZE_SEL_MBYTES CONFIG_C
5555
5656log_info " Checking optional CMA configurations..."
5757for cfg in $OPTIONAL_CMA_CONFIGS ; do
58- if check_kernel_config " $cfg " 2> /dev/null; then
59- value=$( grep " ^$cfg =" /proc/config.gz 2> /dev/null | cut -d' =' -f2 || echo " enabled" )
60- log_info " $cfg : $value "
58+ if check_optional_config " $cfg " 2> /dev/null; then
59+ log_pass " $cfg : enabled"
6160 else
62- log_info " $cfg : not set (optional)"
61+ log_warn " $cfg : not enabled (optional)"
6362 fi
6463done
6564
65+ if check_kernel_config " CONFIG_CMA_DEBUGFS" 2> /dev/null; then
66+ log_pass " CONFIG_CMA_DEBUGFS: enabled"
67+ if [ ! -d " /sys/kernel/debug/cma" ]; then
68+ if [ ! -d " /sys/kernel/debug" ]; then
69+ log_info " CONFIG_CMA_DEBUGFS is enabled but debugfs is not mounted"
70+ log_info " Mounting debugfs"
71+ if mount -t debugfs debugfs /sys/kernel/debug 2> /dev/null; then
72+ log_pass " debugfs mounted successfully"
73+ else
74+ log_warn " Could not mount debugfs — CMA debugfs checks will be skipped"
75+ fi
76+ else
77+ log_warn " CONFIG_CMA_DEBUGFS is enabled but /sys/kernel/debug/cma not found"
78+ fi
79+ fi
80+ fi
81+
6682log_info " === CMA Memory Statistics ==="
6783
6884if [ -f " /proc/meminfo" ]; then
114130
115131log_info " === CMA Initialization and Runtime ==="
116132
117- # Check dmesg for CMA initialization
118- if dmesg | grep -i -q " cma.*reserved" ; then
133+ # Capture dmesg once to a temp file to avoid subshell issues (pass=false inside
134+ # a pipe subshell would not propagate to the outer shell) and to avoid running
135+ # dmesg multiple times.
136+ dmesg_tmp=$( mktemp /tmp/cma_dmesg.XXXXXX 2> /dev/null || echo " /tmp/cma_dmesg_$$ .tmp" )
137+ dmesg > " $dmesg_tmp " 2> /dev/null
138+
139+ # Broaden CMA reservation patterns to cover multiple kernel variants:
140+ # cma: Reserved N MiB at ... (common upstream)
141+ # CMA: Reserved N MiB at ... (some arch variants)
142+ # Reserved memory: created CMA memory pool at ...
143+ # OF: reserved mem: initialized node linux,cma ...
144+ # reserved mem.*CMA (generic DT path)
145+ CMA_INIT_PATTERN=" cma:.*reserved|reserved memory:.*cma|linux,cma|of:.*reserved mem.*cma"
146+
147+ if grep -i -q -E " $CMA_INIT_PATTERN " " $dmesg_tmp " 2> /dev/null; then
119148 log_pass " CMA initialization messages found in dmesg:"
120-
121- dmesg | grep -i " cma.*reserved" | tail -n 5 | while IFS= read -r line; do
149+ grep -i -E " $CMA_INIT_PATTERN " " $dmesg_tmp " 2> /dev/null | tail -n 5 | while IFS= read -r line; do
122150 log_info " $line "
123151 done
124152else
125153 log_fail " No CMA initialization messages found in dmesg"
126- pass=false
154+ pass=false
127155fi
128156
129- if dmesg | grep -i -q " cma.*alloc\|cma.*release" ; then
157+ # CMA allocation/release activity (informational only)
158+ if grep -i -q -E " cma.*alloc|cma.*release" " $dmesg_tmp " 2> /dev/null; then
130159 log_info " CMA allocation/release activity detected"
131- alloc_count=$( dmesg | grep -i -c " cma.*alloc" || echo 0)
132- release_count=$( dmesg | grep -i -c " cma.*release" || echo 0)
160+ alloc_count=$( grep -i -c " cma.*alloc" " $dmesg_tmp " 2> /dev/null || echo 0)
161+ release_count=$( grep -i -c " cma.*release" " $dmesg_tmp " 2> /dev/null || echo 0)
133162 log_info " Allocations: $alloc_count "
134163 log_info " Releases: $release_count "
135164fi
136165
137- if dmesg | grep -i " cma" | grep -i -q " error\|fail\|warn" ; then
166+ # Check for CMA errors/warnings — capture to variable (not a pipe) so pass=false works
167+ cma_errors=$( grep -i " cma" " $dmesg_tmp " 2> /dev/null | grep -i " error\|fail\|warn" || true)
168+ if [ -n " $cma_errors " ]; then
138169 log_fail " CMA warnings/errors found in dmesg:"
139- pass=false
140- dmesg | grep -i " cma " | grep -i " error\|fail\|warn " | tail -n 3 | while IFS= read -r line; do
170+ pass=false
171+ printf ' %s\n ' " $cma_errors " | tail -n 3 | while IFS= read -r line; do
141172 log_warn " $line "
142173 done
143174fi
144175
176+ rm -f " $dmesg_tmp "
177+
145178log_info " === CMA Sysfs/Debugfs Interface ==="
146179
147180if [ -d " /sys/kernel/debug/cma" ]; then
148181 log_info " Found CMA debugfs: /sys/kernel/debug/cma"
149-
150- # List CMA areas
151- if [ -d " /sys/kernel/debug/cma" ]; then
152- cma_area_count=0
153- for area in /sys/kernel/debug/cma/* ; do
154- if [ -d " $area " ]; then
155- area_name=$( basename " $area " )
156- log_info " CMA area: $area_name "
157- cma_area_count=$(( cma_area_count + 1 ))
158- fi
159- done
160- log_info " Total CMA areas: $cma_area_count "
161- fi
182+
183+ cma_area_count=0
184+ for area in /sys/kernel/debug/cma/* ; do
185+ if [ -d " $area " ]; then
186+ area_name=$( basename " $area " )
187+ log_info " CMA area: $area_name "
188+ cma_area_count=$(( cma_area_count + 1 ))
189+ fi
190+ done
191+ log_info " Total CMA areas: $cma_area_count "
162192else
163- log_warn " CMA debugfs not found (may need debugfs mounted)"
193+ log_warn " CMA debugfs not found (may need debugfs mounted or CONFIG_CMA_DEBUGFS enabled )"
164194fi
165195
166196if [ -f " /proc/vmstat" ]; then
@@ -169,20 +199,18 @@ if [ -f "/proc/vmstat" ]; then
169199 grep " cma" /proc/vmstat | while IFS= read -r line; do
170200 log_info " $line "
171201 done
172- else
173- log_fail " CMA statistics not found in /proc/vmstat: "
174- pass=false
202+ else
203+ log_fail " CMA statistics not found in /proc/vmstat"
204+ pass=false
175205 fi
176206fi
177207
178- log_info " ================================================================================"
179-
180208if $pass ; then
181209 log_pass " $TESTNAME : Test Passed"
182210 echo " $TESTNAME PASS" > " $res_file "
183- exit 0
184211else
185212 log_fail " $TESTNAME : Test Failed"
186213 echo " $TESTNAME FAIL" > " $res_file "
187- exit 1
188214fi
215+ log_info " ================================================================================"
216+ exit 0
0 commit comments