/* * Hidraw Userspace Example * * Copyright (c) 2010 Alan Ott * Copyright (c) 2010 Signal 11 Software * * The code may be used by anyone for any purpose, * and can serve as a starting point for developing * applications using hidraw. */ /* Linux */ #include #include #include /* * Ugly hack to work around failing compilation on systems that don't * yet populate new version of hidraw.h to userspace. */ #ifndef HIDIOCSFEATURE #warning Please have your distro update the userspace kernel headers #define HIDIOCSFEATURE(len) _IOC(_IOC_WRITE|_IOC_READ, 'H', 0x06, len) #define HIDIOCGFEATURE(len) _IOC(_IOC_WRITE|_IOC_READ, 'H', 0x07, len) #endif /* Unix */ #include #include #include #include #include /* C */ #include #include #include #include const char *bus_str(int bus); int main(int argc, char **argv) { int fd; int i, res, desc_size = 0; char buf[256]; struct hidraw_report_descriptor rpt_desc; struct hidraw_devinfo info; char *device = "/dev/hidraw0"; if (argc > 1) device = argv[1]; /* Open the Device with non-blocking reads. In real life, don't use a hard coded path; use libudev instead. */ fd = open(device, O_RDWR|O_NONBLOCK); if (fd < 0) { perror("Unable to open device"); return 1; } memset(&rpt_desc, 0x0, sizeof(rpt_desc)); memset(&info, 0x0, sizeof(info)); memset(buf, 0x0, sizeof(buf)); /* Get Report Descriptor Size */ res = ioctl(fd, HIDIOCGRDESCSIZE, &desc_size); if (res < 0) perror("HIDIOCGRDESCSIZE"); else printf("Report Descriptor Size: %d\n", desc_size); /* Get Report Descriptor */ rpt_desc.size = desc_size; res = ioctl(fd, HIDIOCGRDESC, &rpt_desc); if (res < 0) { perror("HIDIOCGRDESC"); } else { printf("Report Descriptor:\n"); for (i = 0; i < rpt_desc.size; i++) printf("%hhx ", rpt_desc.value[i]); puts("\n"); } /* Get Raw Name */ res = ioctl(fd, HIDIOCGRAWNAME(256), buf); if (res < 0) perror("HIDIOCGRAWNAME"); else printf("Raw Name: %s\n", buf); /* Get Physical Location */ res = ioctl(fd, HIDIOCGRAWPHYS(256), buf); if (res < 0) perror("HIDIOCGRAWPHYS"); else printf("Raw Phys: %s\n", buf); /* Get Raw Info */ res = ioctl(fd, HIDIOCGRAWINFO, &info); if (res < 0) { perror("HIDIOCGRAWINFO"); } else { printf("Raw Info:\n"); printf("\tbustype: %d (%s)\n", info.bustype, bus_str(info.bustype)); printf("\tvendor: 0x%04hx\n", info.vendor); printf("\tproduct: 0x%04hx\n", info.product); } /* Set Feature */ buf[0] = 0x9; /* Report Number */ buf[1] = 0xff; buf[2] = 0xff; buf[3] = 0xff; res = ioctl(fd, HIDIOCSFEATURE(4), buf); if (res < 0) perror("HIDIOCSFEATURE"); else printf("ioctl HIDIOCGFEATURE returned: %d\n", res); /* Get Feature */ buf[0] = 0x9; /* Report Number */ res = ioctl(fd, HIDIOCGFEATURE(256), buf); if (res < 0) { perror("HIDIOCGFEATURE"); } else { printf("ioctl HIDIOCGFEATURE returned: %d\n", res); printf("Report data (not containing the report number):\n\t"); for (i = 0; i < res; i++) printf("%hhx ", buf[i]); puts("\n"); } /* Send a Report to the Device */ buf[0] = 0x1; /* Report Number */ buf[1] = 0x77; res = write(fd, buf, 2); if (res < 0) { printf("Error: %d\n", errno); perror("write"); } else { printf("write() wrote %d bytes\n", res); } /* Get a report from the device */ res = read(fd, buf, 16); if (res < 0) { perror("read"); } else { printf("read() read %d bytes:\n\t", res); for (i = 0; i < res; i++) printf("%hhx ", buf[i]); puts("\n"); } close(fd); return 0; } const char * bus_str(int bus) { switch (bus) { case BUS_USB: return "USB"; break; case BUS_HIL: return "HIL"; break; case BUS_BLUETOOTH: return "Bluetooth"; break; case BUS_VIRTUAL: return "Virtual"; break; default: return "Other"; break; } } tion>space:mode:
authorJohannes Weiner <hannes@cmpxchg.org>2017-01-06 19:21:43 -0500
committerLinus Torvalds <torvalds@linux-foundation.org>2017-01-07 18:22:40 -0800
commitea07b862ac8ef9b8c8358517d2e39f847dda6659 (patch)
treea1db46e9dd5dcf4c26dca819fad0b69fbb37e073 /net/batman-adv
parentb0b9b3df27d100a975b4e8818f35382b64a5e35c (diff)
mm: workingset: fix use-after-free in shadow node shrinker
Several people report seeing warnings about inconsistent radix tree nodes followed by crashes in the workingset code, which all looked like use-after-free access from the shadow node shrinker. Dave Jones managed to reproduce the issue with a debug patch applied, which confirmed that the radix tree shrinking indeed frees shadow nodes while they are still linked to the shadow LRU: WARNING: CPU: 2 PID: 53 at lib/radix-tree.c:643 delete_node+0x1e4/0x200 CPU: 2 PID: 53 Comm: kswapd0 Not tainted 4.10.0-rc2-think+ #3 Call Trace: delete_node+0x1e4/0x200 __radix_tree_delete_node+0xd/0x10 shadow_lru_isolate+0xe6/0x220 __list_lru_walk_one.isra.4+0x9b/0x190 list_lru_walk_one+0x23/0x30 scan_shadow_nodes+0x2e/0x40 shrink_slab.part.44+0x23d/0x5d0 shrink_node+0x22c/0x330 kswapd+0x392/0x8f0 This is the WARN_ON_ONCE(!list_empty(&node->private_list)) placed in the inlined radix_tree_shrink(). The problem is with 14b468791fa9 ("mm: workingset: move shadow entry tracking to radix tree exceptional tracking"), which passes an update callback into the radix tree to link and unlink shadow leaf nodes when tree entries change, but forgot to pass the callback when reclaiming a shadow node. While the reclaimed shadow node itself is unlinked by the shrinker, its deletion from the tree can cause the left-most leaf node in the tree to be shrunk. If that happens to be a shadow node as well, we don't unlink it from the LRU as we should. Consider this tree, where the s are shadow entries: root->rnode | [0 n] | | [s ] [sssss] Now the shadow node shrinker reclaims the rightmost leaf node through the shadow node LRU: root->rnode | [0 ] | [s ] Because the parent of the deleted node is the first level below the root and has only one child in the left-most slot, the intermediate level is shrunk and the node containing the single shadow is put in its place: root->rnode | [s ] The shrinker again sees a single left-most slot in a first level node and thus decides to store the shadow in root->rnode directly and free the node - which is a leaf node on the shadow node LRU. root->rnode | s Without the update callback, the freed node remains on the shadow LRU, where it causes later shrinker runs to crash. Pass the node updater callback into __radix_tree_delete_node() in case the deletion causes the left-most branch in the tree to collapse too. Also add warnings when linked nodes are freed right away, rather than wait for the use-after-free when the list is scanned much later. Fixes: 14b468791fa9 ("mm: workingset: move shadow entry tracking to radix tree exceptional tracking") Reported-by: Dave Chinner <david@fromorbit.com> Reported-by: Hugh Dickins <hughd@google.com> Reported-by: Andrea Arcangeli <aarcange@redhat.com> Reported-and-tested-by: Dave Jones <davej@codemonkey.org.uk> Signed-off-by: Johannes Weiner <hannes@cmpxchg.org> Cc: Christoph Hellwig <hch@lst.de> Cc: Chris Leech <cleech@redhat.com> Cc: Lee Duncan <lduncan@suse.com> Cc: Jan Kara <jack@suse.cz> Cc: Kirill A. Shutemov <kirill.shutemov@linux.intel.com> Cc: Matthew Wilcox <mawilcox@linuxonhyperv.com> Cc: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'net/batman-adv')