Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

selftests: memcg: Add tests for IN_DELETE_SELF and IN_IGNORED

Add two new tests that verify inotify events are sent when memcg files
or directories are removed with rmdir.

Signed-off-by: T.J. Mercier <tjmercier@google.com>
Acked-by: Tejun Heo <tj@kernel.org>
Acked-by: Amir Goldstein <amir73il@gmail.com>
Tested-by: syzbot@syzkaller.appspotmail.com
Link: https://patch.msgid.link/20260225223404.783173-4-tjmercier@google.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

T.J. Mercier and committed by
Greg Kroah-Hartman
2de27980 eea5d2bb

+112
+112
tools/testing/selftests/cgroup/test_memcontrol.c
··· 10 10 #include <sys/stat.h> 11 11 #include <sys/types.h> 12 12 #include <unistd.h> 13 + #include <sys/inotify.h> 13 14 #include <sys/socket.h> 14 15 #include <sys/wait.h> 15 16 #include <arpa/inet.h> ··· 1644 1643 return ret; 1645 1644 } 1646 1645 1646 + static int read_event(int inotify_fd, int expected_event, int expected_wd) 1647 + { 1648 + struct inotify_event event; 1649 + ssize_t len = 0; 1650 + 1651 + len = read(inotify_fd, &event, sizeof(event)); 1652 + if (len < (ssize_t)sizeof(event)) 1653 + return -1; 1654 + 1655 + if (event.mask != expected_event || event.wd != expected_wd) { 1656 + fprintf(stderr, 1657 + "event does not match expected values: mask %d (expected %d) wd %d (expected %d)\n", 1658 + event.mask, expected_event, event.wd, expected_wd); 1659 + return -1; 1660 + } 1661 + 1662 + return 0; 1663 + } 1664 + 1665 + static int test_memcg_inotify_delete_file(const char *root) 1666 + { 1667 + int ret = KSFT_FAIL; 1668 + char *memcg = NULL; 1669 + int fd, wd; 1670 + 1671 + memcg = cg_name(root, "memcg_test_0"); 1672 + 1673 + if (!memcg) 1674 + goto cleanup; 1675 + 1676 + if (cg_create(memcg)) 1677 + goto cleanup; 1678 + 1679 + fd = inotify_init1(0); 1680 + if (fd == -1) 1681 + goto cleanup; 1682 + 1683 + wd = inotify_add_watch(fd, cg_control(memcg, "memory.events"), IN_DELETE_SELF); 1684 + if (wd == -1) 1685 + goto cleanup; 1686 + 1687 + if (cg_destroy(memcg)) 1688 + goto cleanup; 1689 + free(memcg); 1690 + memcg = NULL; 1691 + 1692 + if (read_event(fd, IN_DELETE_SELF, wd)) 1693 + goto cleanup; 1694 + 1695 + if (read_event(fd, IN_IGNORED, wd)) 1696 + goto cleanup; 1697 + 1698 + ret = KSFT_PASS; 1699 + 1700 + cleanup: 1701 + if (fd >= 0) 1702 + close(fd); 1703 + if (memcg) 1704 + cg_destroy(memcg); 1705 + free(memcg); 1706 + 1707 + return ret; 1708 + } 1709 + 1710 + static int test_memcg_inotify_delete_dir(const char *root) 1711 + { 1712 + int ret = KSFT_FAIL; 1713 + char *memcg = NULL; 1714 + int fd, wd; 1715 + 1716 + memcg = cg_name(root, "memcg_test_0"); 1717 + 1718 + if (!memcg) 1719 + goto cleanup; 1720 + 1721 + if (cg_create(memcg)) 1722 + goto cleanup; 1723 + 1724 + fd = inotify_init1(0); 1725 + if (fd == -1) 1726 + goto cleanup; 1727 + 1728 + wd = inotify_add_watch(fd, memcg, IN_DELETE_SELF); 1729 + if (wd == -1) 1730 + goto cleanup; 1731 + 1732 + if (cg_destroy(memcg)) 1733 + goto cleanup; 1734 + free(memcg); 1735 + memcg = NULL; 1736 + 1737 + if (read_event(fd, IN_DELETE_SELF, wd)) 1738 + goto cleanup; 1739 + 1740 + if (read_event(fd, IN_IGNORED, wd)) 1741 + goto cleanup; 1742 + 1743 + ret = KSFT_PASS; 1744 + 1745 + cleanup: 1746 + if (fd >= 0) 1747 + close(fd); 1748 + if (memcg) 1749 + cg_destroy(memcg); 1750 + free(memcg); 1751 + 1752 + return ret; 1753 + } 1754 + 1647 1755 #define T(x) { x, #x } 1648 1756 struct memcg_test { 1649 1757 int (*fn)(const char *root); ··· 1772 1662 T(test_memcg_oom_group_leaf_events), 1773 1663 T(test_memcg_oom_group_parent_events), 1774 1664 T(test_memcg_oom_group_score_events), 1665 + T(test_memcg_inotify_delete_file), 1666 + T(test_memcg_inotify_delete_dir), 1775 1667 }; 1776 1668 #undef T 1777 1669