| 1 |
# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 |
# SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 |
|
| 4 |
import contextlib |
| 5 |
import ctypes |
| 6 |
from fcntl import ioctl |
| 7 |
import mmap |
| 8 |
import os |
| 9 |
import os.path |
| 10 |
from pathlib import Path |
| 11 |
|
| 12 |
from drgn import Object |
| 13 |
from drgn.helpers.linux.block import ( |
| 14 |
bdev_partno, |
| 15 |
blk_mq_rq_from_pdu, |
| 16 |
blk_mq_rq_to_pdu, |
| 17 |
blk_rq_bytes, |
| 18 |
blk_rq_pos, |
| 19 |
disk_devt, |
| 20 |
disk_name, |
| 21 |
for_each_disk, |
| 22 |
for_each_partition, |
| 23 |
nr_blockdev_pages, |
| 24 |
op_is_write, |
| 25 |
part_devt, |
| 26 |
part_name, |
| 27 |
req_op, |
| 28 |
request_queue_busy_iter, |
| 29 |
rq_data_dir, |
| 30 |
) |
| 31 |
from drgn.helpers.linux.device import MAJOR, MINOR |
| 32 |
from drgn.helpers.linux.list import list_for_each |
| 33 |
from tests.linux_kernel import ( |
| 34 |
IOCB_CMD_PREAD, |
| 35 |
IOCB_CMD_PWRITE, |
| 36 |
LinuxKernelTestCase, |
| 37 |
io_destroy, |
| 38 |
io_setup, |
| 39 |
io_submit, |
| 40 |
iocb, |
| 41 |
meminfo_field_in_pages, |
| 42 |
skip_unless_have_test_kmod, |
| 43 |
wait_until, |
| 44 |
) |
| 45 |
|
| 46 |
|
| 47 |
class TestBlock(LinuxKernelTestCase): |
| 48 |
def test_disk_devt(self): |
| 49 |
for disk in for_each_disk(self.prog): |
| 50 |
path = os.path.join(b"/sys/block", disk_name(disk), b"dev") |
| 51 |
with open(path, "r") as f: |
| 52 |
expected = f.read().strip() |
| 53 |
devt = disk_devt(disk).value_() |
| 54 |
self.assertEqual(f"{MAJOR(devt)}:{MINOR(devt)}", expected) |
| 55 |
|
| 56 |
def test_for_each_disk(self): |
| 57 |
self.assertEqual( |
| 58 |
{disk_name(disk).decode() for disk in for_each_disk(self.prog)}, |
| 59 |
set(os.listdir("/sys/block")), |
| 60 |
) |
| 61 |
|
| 62 |
def test_part_devt(self): |
| 63 |
for part in for_each_partition(self.prog): |
| 64 |
path = os.path.join(b"/sys/class/block", part_name(part), b"dev") |
| 65 |
with open(path, "r") as f: |
| 66 |
expected = f.read().strip() |
| 67 |
devt = part_devt(part).value_() |
| 68 |
self.assertEqual(f"{MAJOR(devt)}:{MINOR(devt)}", expected) |
| 69 |
|
| 70 |
def test_for_each_partition(self): |
| 71 |
self.assertEqual( |
| 72 |
{part_name(part).decode() for part in for_each_partition(self.prog)}, |
| 73 |
set(os.listdir("/sys/class/block")), |
| 74 |
) |
| 75 |
|
| 76 |
def test_bdev_partno(self): |
| 77 |
for part in for_each_partition(self.prog): |
| 78 |
try: |
| 79 |
with open( |
| 80 |
os.path.join(b"/sys/class/block", part_name(part), b"partition"), |
| 81 |
"r", |
| 82 |
) as f: |
| 83 |
partition = int(f.read()) |
| 84 |
except FileNotFoundError: |
| 85 |
partition = 0 |
| 86 |
if part.type_.type.tag == "hd_struct": |
| 87 |
self.skipTest("can't get bdev easily on old kernels") |
| 88 |
self.assertIdentical(bdev_partno(part), Object(self.prog, "u8", partition)) |
| 89 |
|
| 90 |
def test_nr_blockdev_pages(self): |
| 91 |
self.assertAlmostEqual( |
| 92 |
nr_blockdev_pages(self.prog), |
| 93 |
meminfo_field_in_pages("Buffers"), |
| 94 |
delta=1024 * 1024 * 1024, |
| 95 |
) |
| 96 |
|
| 97 |
@skip_unless_have_test_kmod |
| 98 |
def test_rq_helpers(self): |
| 99 |
with contextlib.ExitStack() as exit_stack: |
| 100 |
fd = os.open("/dev/drgntestb0", os.O_RDWR | os.O_DIRECT) |
| 101 |
exit_stack.callback(os.close, fd) |
| 102 |
|
| 103 |
map = exit_stack.enter_context( |
| 104 |
mmap.mmap(-1, mmap.PAGESIZE, mmap.MAP_PRIVATE) |
| 105 |
) |
| 106 |
|
| 107 |
iocbs = (iocb * 1)() |
| 108 |
iocbs[0].aio_lio_opcode = IOCB_CMD_PWRITE |
| 109 |
iocbs[0].aio_fildes = fd |
| 110 |
iocbs[0].aio_buf = ctypes.addressof(ctypes.c_char.from_buffer(map)) |
| 111 |
iocbs[0].aio_nbytes = mmap.PAGESIZE |
| 112 |
iocbs[0].aio_offset = 6 * mmap.PAGESIZE |
| 113 |
|
| 114 |
ctx_id = io_setup(len(iocbs)) |
| 115 |
exit_stack.callback(io_destroy, ctx_id) |
| 116 |
|
| 117 |
truant = Path("/sys/block/drgntestb0/truant") |
| 118 |
truant.write_text("1\n") |
| 119 |
exit_stack.callback(truant.write_text, "0\n") |
| 120 |
|
| 121 |
io_submit(ctx_id, iocbs) |
| 122 |
|
| 123 |
num_loafing = Path("/sys/block/drgntestb0/num_loafing") |
| 124 |
wait_until(lambda: int(num_loafing.read_text()) > 0) |
| 125 |
|
| 126 |
loafing_requests = self.prog["drgn_test_blkdevs"][0].loafing_requests |
| 127 |
pos = loafing_requests.next.read_() |
| 128 |
self.assertNotEqual(pos, loafing_requests.address_of_()) |
| 129 |
|
| 130 |
rq = blk_mq_rq_from_pdu(pos) |
| 131 |
self.assertEqual(req_op(rq), self.prog["REQ_OP_WRITE"]) |
| 132 |
self.assertEqual(rq_data_dir(rq), 1) |
| 133 |
self.assertEqual(blk_rq_pos(rq), (6 * mmap.PAGESIZE) >> 9) |
| 134 |
self.assertEqual(blk_rq_bytes(rq), mmap.PAGESIZE) |
| 135 |
|
| 136 |
def test_op_is_write(self): |
| 137 |
self.assertFalse(op_is_write(self.prog["REQ_OP_READ"])) |
| 138 |
self.assertTrue(op_is_write(self.prog["REQ_OP_WRITE"])) |
| 139 |
self.assertTrue(op_is_write(self.prog["REQ_OP_DISCARD"])) |
| 140 |
|
| 141 |
# drgntestb0 has 2 hardware queues with 2 tags each. drgntestb1 has 2 |
| 142 |
# hardware queues which share 4 tags. |
| 143 |
@skip_unless_have_test_kmod |
| 144 |
def test_request_queue_busy_iter(self): |
| 145 |
for index in (0, 1): |
| 146 |
with self.subTest( |
| 147 |
"shared tags" if index else None |
| 148 |
), contextlib.ExitStack() as exit_stack: |
| 149 |
name = f"drgntestb{index}" |
| 150 |
|
| 151 |
try: |
| 152 |
fd = os.open("/dev/" + name, os.O_RDWR | os.O_DIRECT) |
| 153 |
except FileNotFoundError: |
| 154 |
if index == 1: |
| 155 |
self.skipTest("kernel does not have BLK_MQ_F_TAG_HCTX_SHARED") |
| 156 |
else: |
| 157 |
raise |
| 158 |
exit_stack.callback(os.close, fd) |
| 159 |
|
| 160 |
sysfs_path = Path("/sys/block") / name |
| 161 |
(sysfs_path / "queue/scheduler").write_text("none\n") |
| 162 |
|
| 163 |
map = exit_stack.enter_context( |
| 164 |
mmap.mmap(-1, mmap.PAGESIZE, mmap.MAP_PRIVATE) |
| 165 |
) |
| 166 |
|
| 167 |
# Submit fewer requests than the total number of driver tags. |
| 168 |
iocbs = (iocb * 1)() |
| 169 |
iocbs[0].aio_lio_opcode = IOCB_CMD_PREAD |
| 170 |
iocbs[0].aio_fildes = fd |
| 171 |
iocbs[0].aio_buf = ctypes.addressof(ctypes.c_char.from_buffer(map)) |
| 172 |
iocbs[0].aio_nbytes = mmap.PAGESIZE |
| 173 |
iocbs[0].aio_offset = 0 |
| 174 |
|
| 175 |
ctx_id = io_setup(len(iocbs)) |
| 176 |
exit_stack.callback(io_destroy, ctx_id) |
| 177 |
|
| 178 |
truant = sysfs_path / "truant" |
| 179 |
truant.write_text("1\n") |
| 180 |
exit_stack.callback(truant.write_text, "0\n") |
| 181 |
|
| 182 |
ioctl(fd, 0x4C00) |
| 183 |
io_submit(ctx_id, iocbs) |
| 184 |
|
| 185 |
# This also tests blk_mq_rq_from_pdu(). |
| 186 |
self.assertCountEqual( |
| 187 |
list( |
| 188 |
request_queue_busy_iter( |
| 189 |
self.prog["drgn_test_blkdevs"][index].disk.queue |
| 190 |
) |
| 191 |
), |
| 192 |
[ |
| 193 |
blk_mq_rq_from_pdu(node) |
| 194 |
for node in list_for_each( |
| 195 |
self.prog["drgn_test_blkdevs"][ |
| 196 |
index |
| 197 |
].loafing_requests.address_of_() |
| 198 |
) |
| 199 |
], |
| 200 |
) |
| 201 |
|
| 202 |
# This is the same as the previous assertion, but it passes |
| 203 |
# tags explicitly and tests blk_mq_rq_to_pdu(). |
| 204 |
self.assertCountEqual( |
| 205 |
[ |
| 206 |
blk_mq_rq_to_pdu(rq) |
| 207 |
for rq in request_queue_busy_iter( |
| 208 |
self.prog["drgn_test_blkdevs"][index].disk.queue, "driver" |
| 209 |
) |
| 210 |
], |
| 211 |
list( |
| 212 |
list_for_each( |
| 213 |
self.prog["drgn_test_blkdevs"][ |
| 214 |
index |
| 215 |
].loafing_requests.address_of_() |
| 216 |
) |
| 217 |
), |
| 218 |
) |
| 219 |
|
| 220 |
self.assertEqual( |
| 221 |
sum( |
| 222 |
1 |
| 223 |
for _ in request_queue_busy_iter( |
| 224 |
self.prog["drgn_test_blkdevs"][index].disk.queue, "sched" |
| 225 |
) |
| 226 |
), |
| 227 |
0, |
| 228 |
) |
| 229 |
|
| 230 |
@skip_unless_have_test_kmod |
| 231 |
def test_request_queue_busy_iter_with_scheduler(self): |
| 232 |
if ( |
| 233 |
"mq-deadline" |
| 234 |
not in Path("/sys/block/drgntestb0/queue/scheduler").read_text() |
| 235 |
): |
| 236 |
self.skipTest("kernel does not support mq-deadline I/O scheduler") |
| 237 |
for index in (0, 1): |
| 238 |
with self.subTest( |
| 239 |
"shared tags" if index else None |
| 240 |
), contextlib.ExitStack() as exit_stack: |
| 241 |
name = f"drgntestb{index}" |
| 242 |
|
| 243 |
try: |
| 244 |
fd = os.open("/dev/" + name, os.O_RDWR | os.O_DIRECT) |
| 245 |
except FileNotFoundError: |
| 246 |
if index == 1: |
| 247 |
self.skipTest("kernel does not have BLK_MQ_F_TAG_HCTX_SHARED") |
| 248 |
else: |
| 249 |
raise |
| 250 |
exit_stack.callback(os.close, fd) |
| 251 |
|
| 252 |
sysfs_path = Path("/sys/block") / name |
| 253 |
(sysfs_path / "queue/scheduler").write_text("mq-deadline\n") |
| 254 |
(sysfs_path / "queue/nr_requests").write_text("128\n") |
| 255 |
|
| 256 |
iocbs = (iocb * 5)() |
| 257 |
|
| 258 |
map = exit_stack.enter_context( |
| 259 |
mmap.mmap(-1, len(iocbs) * mmap.PAGESIZE, mmap.MAP_PRIVATE) |
| 260 |
) |
| 261 |
|
| 262 |
# Submit more requests than the total number of driver tags. |
| 263 |
for i in range(len(iocbs)): |
| 264 |
iocbs[i].aio_lio_opcode = IOCB_CMD_PREAD |
| 265 |
iocbs[i].aio_fildes = fd |
| 266 |
iocbs[i].aio_buf = ctypes.addressof( |
| 267 |
ctypes.c_char.from_buffer(map, i * mmap.PAGESIZE) |
| 268 |
) |
| 269 |
iocbs[i].aio_nbytes = mmap.PAGESIZE |
| 270 |
# Leave gaps between requests to avoid merging. |
| 271 |
iocbs[i].aio_offset = 2 * (i + 1) * mmap.PAGESIZE |
| 272 |
|
| 273 |
ctx_id = io_setup(len(iocbs)) |
| 274 |
exit_stack.callback(io_destroy, ctx_id) |
| 275 |
|
| 276 |
truant = sysfs_path / "truant" |
| 277 |
truant.write_text("1\n") |
| 278 |
exit_stack.callback(truant.write_text, "0\n") |
| 279 |
|
| 280 |
ioctl(fd, 0x4C00) |
| 281 |
io_submit(ctx_id, iocbs) |
| 282 |
|
| 283 |
self.assertCountEqual( |
| 284 |
[ |
| 285 |
blk_rq_pos(rq).value_() << 9 |
| 286 |
for rq in request_queue_busy_iter( |
| 287 |
self.prog["drgn_test_blkdevs"][index].disk.queue |
| 288 |
) |
| 289 |
], |
| 290 |
range(0, 2 * (len(iocbs) + 1) * mmap.PAGESIZE, 2 * mmap.PAGESIZE), |
| 291 |
) |
| 292 |
|
| 293 |
self.assertEqual( |
| 294 |
sum( |
| 295 |
1 |
| 296 |
for _ in request_queue_busy_iter( |
| 297 |
self.prog["drgn_test_blkdevs"][index].disk.queue, "sched" |
| 298 |
) |
| 299 |
), |
| 300 |
len(iocbs) + 1, |
| 301 |
) |
| 302 |
|
| 303 |
self.assertCountEqual( |
| 304 |
list( |
| 305 |
request_queue_busy_iter( |
| 306 |
self.prog["drgn_test_blkdevs"][index].disk.queue, "driver" |
| 307 |
) |
| 308 |
), |
| 309 |
[ |
| 310 |
blk_mq_rq_from_pdu(node) |
| 311 |
for node in list_for_each( |
| 312 |
self.prog["drgn_test_blkdevs"][ |
| 313 |
index |
| 314 |
].loafing_requests.address_of_() |
| 315 |
) |
| 316 |
], |
| 317 |
) |