cgroup_regression_test.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. #!/bin/sh
  2. # SPDX-License-Identifier: GPL-2.0-or-later
  3. # Copyright (c) 2019 Petr Vorel <pvorel@suse.cz>
  4. # Copyright (c) 2009 FUJITSU LIMITED
  5. # Author: Li Zefan <lizf@cn.fujitsu.com>
  6. TST_TESTFUNC=test
  7. TST_SETUP=do_setup
  8. TST_CLEANUP=do_cleanup
  9. TST_CNT=9
  10. TST_NEEDS_ROOT=1
  11. TST_NEEDS_TMPDIR=1
  12. TST_NEEDS_CMDS="awk dmesg find mountpoint rmdir"
  13. . cgroup_lib.sh
  14. do_setup()
  15. {
  16. mkdir cgroup/
  17. if tst_kvcmp -lt "2.6.29"; then
  18. tst_brk TCONF ignored "test must be run with kernel 2.6.29 or newer"
  19. fi
  20. if [ ! -f /proc/cgroups ]; then
  21. tst_brk TCONF ignored "Kernel does not support for control groups; skipping testcases";
  22. fi
  23. dmesg -c > /dev/null
  24. NR_BUG=`dmesg | grep -c "kernel BUG"`
  25. NR_NULL=`dmesg | grep -c "kernel NULL pointer dereference"`
  26. NR_WARNING=`dmesg | grep -c "^WARNING"`
  27. NR_LOCKDEP=`dmesg | grep -c "possible recursive locking detected"`
  28. }
  29. do_cleanup()
  30. {
  31. if mountpoint -q cgroup/; then
  32. find cgroup/ -maxdepth 1 -depth -exec rmdir {} +
  33. umount cgroup/
  34. rmdir cgroup
  35. fi
  36. }
  37. check_kernel_bug()
  38. {
  39. local id="$1"
  40. local ok_msg="no kernel bug was found"
  41. local new_bug=`dmesg | grep -c "kernel BUG"`
  42. local new_null=`dmesg | grep -c "kernel NULL pointer dereference"`
  43. local new_warning=`dmesg | grep -c "^WARNING"`
  44. local new_lockdep=`dmesg | grep -c "possible recursive locking detected"`
  45. [ "$id" ] && ok_msg="$ok_msg for test $i"
  46. # no kernel bug is detected
  47. if [ $new_bug -eq $NR_BUG -a $new_warning -eq $NR_WARNING -a \
  48. $new_null -eq $NR_NULL -a $new_lockdep -eq $NR_LOCKDEP ]; then
  49. tst_res TPASS $ok_msg
  50. return 0
  51. fi
  52. # some kernel bug is detected
  53. if [ $new_bug -gt $NR_BUG ]; then
  54. tst_res TFAIL "kernel BUG was detected!"
  55. fi
  56. if [ $new_warning -gt $NR_WARNING ]; then
  57. tst_res TFAIL "kernel WARNING was detected!"
  58. fi
  59. if [ $new_null -gt $NR_NULL ]; then
  60. tst_res TFAIL "kernel NULL pointer dereference!"
  61. fi
  62. if [ $new_lockdep -gt $NR_LOCKDEP ]; then
  63. tst_res TFAIL "kernel lockdep warning was detected!"
  64. fi
  65. NR_BUG=$new_bug
  66. NR_NULL=$new_null
  67. NR_WARNING=$new_warning
  68. NR_LOCKDEP=$new_lockdep
  69. tst_res TWARN "BUG FOUND!"
  70. dmesg
  71. return 1
  72. }
  73. #---------------------------------------------------------------------------
  74. # Bug: There was a race when keeping forking processes and at the same
  75. # time cat /cgroup/tasks (should be the very first time to read
  76. # /cgroup/tasks, otherwise this bug won't be triggered)
  77. # Kernel: 2.6.24, 2.6.25-rcX
  78. # Links: http://lkml.org/lkml/2007/10/17/224
  79. # http://lkml.org/lkml/2008/3/5/332
  80. # http://lkml.org/lkml/2008/4/16/493
  81. # Fix: commit 0e04388f0189fa1f6812a8e1cb6172136eada87e
  82. #---------------------------------------------------------------------------
  83. test1()
  84. {
  85. cgroup_regression_fork_processes &
  86. sleep 1
  87. mount -t cgroup -o none,name=foo cgroup cgroup/
  88. if [ $? -ne 0 ]; then
  89. tst_res TFAIL "failed to mount cgroup filesystem"
  90. kill -TERM $!
  91. return
  92. fi
  93. cat cgroup/tasks > /dev/null
  94. kill -TERM $!
  95. wait $! 2>/dev/null
  96. umount cgroup/
  97. check_kernel_bug
  98. }
  99. #---------------------------------------------------------------------------
  100. # Bug: a cgroup's notify_on_release flag did not inherit from its parent.
  101. # Kernel: 2.6.24-rcX
  102. # Links: http://lkml.org/lkml/2008/2/25/12
  103. # Fix: commit bc231d2a048010d5e0b49ac7fddbfa822fc41109
  104. #---------------------------------------------------------------------------
  105. test2()
  106. {
  107. local val1
  108. local val2
  109. mount -t cgroup -o none,name=foo cgroup cgroup/
  110. if [ $? -ne 0 ]; then
  111. tst_res TFAIL "Failed to mount cgroup filesystem"
  112. return
  113. fi
  114. echo 0 > cgroup/notify_on_release
  115. mkdir cgroup/0
  116. val1=`cat cgroup/0/notify_on_release`
  117. echo 1 > cgroup/notify_on_release
  118. mkdir cgroup/1
  119. val2=`cat cgroup/1/notify_on_release`
  120. if [ $val1 -ne 0 -o $val2 -ne 1 ]; then
  121. tst_res TFAIL "wrong notify_on_release value"
  122. else
  123. tst_res TPASS "notify_on_release is inherited"
  124. fi
  125. rmdir cgroup/0 cgroup/1
  126. umount cgroup/
  127. }
  128. #---------------------------------------------------------------------------
  129. # Bug: Accessing NULL cgrp->dentry when reading /proc/sched_debug
  130. # Kernel: 2.6.26-2.6.28
  131. # Links: http://lkml.org/lkml/2008/10/30/44
  132. # http://lkml.org/lkml/2008/12/12/107
  133. # http://lkml.org/lkml/2008/12/16/481
  134. # Fix: commit a47295e6bc42ad35f9c15ac66f598aa24debd4e2
  135. #---------------------------------------------------------------------------
  136. test3()
  137. {
  138. local cpu_subsys_path
  139. if [ ! -e /proc/sched_debug ]; then
  140. tst_res TCONF "CONFIG_SCHED_DEBUG is not enabled"
  141. return
  142. fi
  143. if ! grep -q -w "cpu" /proc/cgroups; then
  144. tst_res TCONF "CONFIG_CGROUP_SCHED is not enabled"
  145. return
  146. fi
  147. cpu_subsys_path=$(get_cgroup_mountpoint "cpu")
  148. # Run the test for 30 secs
  149. if [ -z "$cpu_subsys_path" ]; then
  150. mount -t cgroup -o cpu xxx cgroup/
  151. if [ $? -ne 0 ]; then
  152. tst_res TFAIL "Failed to mount cpu subsys"
  153. return
  154. fi
  155. cpu_subsys_path=cgroup
  156. fi
  157. cgroup_regression_3_1.sh $cpu_subsys_path &
  158. pid1=$!
  159. cgroup_regression_3_2.sh &
  160. pid2=$!
  161. sleep 30
  162. kill -USR1 $pid1 $pid2
  163. wait $pid1 2>/dev/null
  164. wait $pid2 2>/dev/null
  165. rmdir $cpu_subsys_path/0 2> /dev/null
  166. umount cgroup/ 2> /dev/null
  167. check_kernel_bug
  168. }
  169. #---------------------------------------------------------------------------
  170. # Bug: cgroup hierarchy lock's lockdep subclass may overflow
  171. # Kernel: 2.6.29-rcX
  172. # Link: http://lkml.org/lkml/2009/2/4/67
  173. # Fix:
  174. #---------------------------------------------------------------------------
  175. test4()
  176. {
  177. local lines
  178. if [ ! -e /proc/lockdep ]; then
  179. tst_res TCONF "CONFIG_LOCKDEP is not enabled"
  180. return
  181. fi
  182. # MAX_LOCKDEP_SUBCLASSES is 8, so number of subsys should be > 8
  183. lines=`cat /proc/cgroups | wc -l`
  184. if [ $lines -le 9 ]; then
  185. tst_res TCONF "require more than 8 cgroup subsystems"
  186. return
  187. fi
  188. mount -t cgroup -o none,name=foo cgroup cgroup/
  189. mkdir cgroup/0
  190. rmdir cgroup/0
  191. umount cgroup/
  192. if dmesg | grep -q "MAX_LOCKDEP_SUBCLASSES too low"; then
  193. tst_res TFAIL "lockdep BUG was found"
  194. return
  195. fi
  196. tst_res TPASS "no lockdep BUG was found"
  197. }
  198. #---------------------------------------------------------------------------
  199. # Bug: When running 2 concurrent mount/umount threads, kernel WARNING
  200. # may be triggered, but it's VFS' issue but not cgroup.
  201. # Kernel: 2.6.24 - 2.6.29-rcX
  202. # Links: http://lkml.org/lkml/2009/1/4/354
  203. # Fix: commit 1a88b5364b535edaa321d70a566e358390ff0872
  204. #---------------------------------------------------------------------------
  205. test5()
  206. {
  207. cgroup_regression_5_1.sh &
  208. local pid1=$!
  209. cgroup_regression_5_2.sh &
  210. local pid2=$!
  211. sleep 30
  212. kill -USR1 $pid1 $pid2
  213. wait $pid1 2>/dev/null
  214. wait $pid2 2>/dev/null
  215. mount -t cgroup none cgroup 2> /dev/null
  216. mkdir cgroup/0
  217. rmdir cgroup/0
  218. umount cgroup/ 2> /dev/null
  219. check_kernel_bug
  220. }
  221. #---------------------------------------------------------------------------
  222. # Bug: There was a race between cgroup_clone and umount
  223. # Kernel: 2.6.24 - 2.6.28, 2.6.29-rcX
  224. # Links: http://lkml.org/lkml/2008/12/24/124
  225. # Fix: commit 7b574b7b0124ed344911f5d581e9bc2d83bbeb19
  226. #---------------------------------------------------------------------------
  227. test6()
  228. {
  229. if tst_kvcmp -ge "3.0"; then
  230. tst_res TCONF "CONFIG_CGROUP_NS is NOT supported in Kernels >= 3.0"
  231. return
  232. fi
  233. if ! grep -q -w "ns" /proc/cgroups; then
  234. tst_res TCONF "CONFIG_CGROUP_NS is NOT enabled"
  235. return
  236. fi
  237. cgroup_regression_6_1.sh &
  238. local pid1=$!
  239. cgroup_regression_6_2 &
  240. local pid2=$!
  241. tst_res TINFO "run test for 30 sec"
  242. sleep 30
  243. kill -USR1 $pid1
  244. kill -TERM $pid2
  245. wait $pid1 2>/dev/null
  246. wait $pid2 2>/dev/null
  247. mount -t cgroup -o ns xxx cgroup/ > /dev/null 2>&1
  248. rmdir cgroup/[1-9]* > /dev/null 2>&1
  249. umount cgroup/
  250. check_kernel_bug
  251. }
  252. #---------------------------------------------------------------------------
  253. # Bug: There was a bug when remount cgroup fs with some dead subdirs in
  254. # it (rmdir()ed but still has some refcnts on it). It caused memory
  255. # leak, and may cause oops when cat /proc/sched_debug.
  256. # Kernel: 2.6.24 - 2.6.27, 2.6.28-rcX
  257. # Links: http://lkml.org/lkml/2008/12/10/369
  258. # Fix: commit 307257cf475aac25db30b669987f13d90c934e3a
  259. #---------------------------------------------------------------------------
  260. test_7_1()
  261. {
  262. local subsys=$1
  263. # we should be careful to select a $subsys_path which is related to
  264. # cgroup only: if cgroup debugging is enabled a 'debug' $subsys
  265. # could be passed here as params and this will lead to ambiguity and
  266. # errors when grepping simply for 'debug' in /proc/mounts since we'll
  267. # find also /sys/kernel/debug. Helper takes care of this.
  268. local subsys_path=$(get_cgroup_mountpoint $subsys)
  269. if [ -z "$subsys_path" ]; then
  270. mount -t cgroup -o $subsys xxx cgroup/
  271. if [ $? -ne 0 ]; then
  272. tst_res TFAIL "failed to mount $subsys"
  273. return
  274. fi
  275. subsys_path=cgroup
  276. fi
  277. mkdir $subsys_path/0
  278. sleep 100 < $subsys_path/0 & # add refcnt to this dir
  279. rmdir $subsys_path/0
  280. # remount with new subsystems added
  281. # since 2.6.28, this remount will fail
  282. if [ "$subsys_path" = "cgroup" ]; then
  283. mount -t cgroup -o remount xxx cgroup/ 2> /dev/null
  284. kill -TERM $!
  285. wait $! 2>/dev/null
  286. umount cgroup/
  287. fi
  288. }
  289. test_7_2()
  290. {
  291. local subsys=$1
  292. mount -t cgroup -o none,name=foo cgroup cgroup/
  293. if [ $? -ne 0 ]; then
  294. tst_res TFAIL "failed to mount cgroup"
  295. return
  296. fi
  297. mkdir cgroup/0
  298. sleep 100 < cgroup/0 & # add refcnt to this dir
  299. rmdir cgroup/0
  300. # remount with some subsystems removed
  301. # since 2.6.28, this remount will fail
  302. mount -t cgroup -o remount,$subsys xxx cgroup/ 2> /dev/null
  303. kill -TERM $!
  304. wait $! 2>/dev/null
  305. umount cgroup/
  306. grep -q -w "cpu" /proc/cgroups
  307. if [ $? -ne 0 -o ! -e /proc/sched_debug ]; then
  308. tst_res TCONF "skip rest of testing due possible oops triggered by reading /proc/sched_debug"
  309. return
  310. fi
  311. tmp=0
  312. while [ $tmp -lt 50 ]; do
  313. echo 3 > /proc/sys/vm/drop_caches
  314. cat /proc/sched_debug > /dev/null
  315. tmp=$((tmp+1))
  316. done
  317. }
  318. test7()
  319. {
  320. local lines=`cat /proc/cgroups | wc -l`
  321. local subsys
  322. local i=1
  323. if [ $lines -le 2 ]; then
  324. tst_res TCONF "require at least 2 cgroup subsystems"
  325. slt_result $SLT_Untested
  326. return
  327. fi
  328. subsys=`tail -n 1 /proc/cgroups | awk '{ print $1 }'`
  329. # remount to add new subsystems to the hierarchy
  330. while [ $i -le 2 ]; do
  331. test_7_$i $subsys || return
  332. check_kernel_bug $i || return
  333. i=$((i+1))
  334. done
  335. }
  336. #---------------------------------------------------------------------------
  337. # Bug: oops when get cgroupstat of a cgroup control file
  338. # Kernel: 2.6.24 - 2.6.27, 2.6.28-rcX
  339. # Links: http://lkml.org/lkml/2008/11/19/53
  340. # Fix: commit 33d283bef23132c48195eafc21449f8ba88fce6b
  341. #---------------------------------------------------------------------------
  342. test8()
  343. {
  344. mount -t cgroup -o none,name=foo cgroup cgroup/
  345. if [ $? -ne 0 ]; then
  346. tst_res TFAIL "failed to mount cgroup filesystem"
  347. return
  348. fi
  349. if cgroup_regression_getdelays -C cgroup/tasks > /dev/null 2>&1; then
  350. tst_res TFAIL "should have failed to get cgroupstat of tasks file"
  351. fi
  352. umount cgroup/
  353. check_kernel_bug
  354. }
  355. #---------------------------------------------------------------------------
  356. # Bug: When running 2 concurrent mount/umount threads, lockdep warning
  357. # may be triggered, it's a false positive, and it's VFS' issue but
  358. # not cgroup.
  359. # Kernel: 2.6.24 - 2.6.29-rcX
  360. # Links: http://lkml.org/lkml/2009/1/4/352
  361. # Fix: commit ada723dcd681e2dffd7d73345cc8fda0eb0df9bd
  362. #---------------------------------------------------------------------------
  363. test9()
  364. {
  365. cgroup_regression_9_1.sh &
  366. local pid1=$!
  367. cgroup_regression_9_2.sh &
  368. local pid2=$!
  369. sleep 30
  370. kill -USR1 $pid1 $pid2
  371. wait $pid1 2>/dev/null
  372. wait $pid2 2>/dev/null
  373. umount cgroup/ 2> /dev/null
  374. check_kernel_bug
  375. }
  376. tst_run