// INFO: task hung in rwsem_down_read_failed
// https://syzkaller.appspot.com/bug?id=2c3e370f4b05d93d4267f52b320348f6111220f9
// status:open
// autogenerated by syzkaller (http://github.com/google/syzkaller)

#define _GNU_SOURCE
#include <endian.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdio.h>
#include <string.h>
#include <sys/prctl.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>

__attribute__((noreturn)) static void doexit(int status)
{
  volatile unsigned i;
  syscall(__NR_exit_group, status);
  for (i = 0;; i++) {
  }
}
#include <stdint.h>
#include <string.h>

const int kFailStatus = 67;
const int kRetryStatus = 69;

static void fail(const char* msg, ...)
{
  int e = errno;
  va_list args;
  va_start(args, msg);
  vfprintf(stderr, msg, args);
  va_end(args);
  fprintf(stderr, " (errno %d)\n", e);
  doexit((e == ENOMEM || e == EAGAIN) ? kRetryStatus : kFailStatus);
}

static uint64_t current_time_ms()
{
  struct timespec ts;

  if (clock_gettime(CLOCK_MONOTONIC, &ts))
    fail("clock_gettime failed");
  return (uint64_t)ts.tv_sec * 1000 + (uint64_t)ts.tv_nsec / 1000000;
}

static uintptr_t syz_open_dev(uintptr_t a0, uintptr_t a1, uintptr_t a2)
{
  if (a0 == 0xc || a0 == 0xb) {
    char buf[128];
    sprintf(buf, "/dev/%s/%d:%d", a0 == 0xc ? "char" : "block", (uint8_t)a1,
            (uint8_t)a2);
    return open(buf, O_RDWR, 0);
  } else {
    char buf[1024];
    char* hash;
    strncpy(buf, (char*)a0, sizeof(buf));
    buf[sizeof(buf) - 1] = 0;
    while ((hash = strchr(buf, '#'))) {
      *hash = '0' + (char)(a1 % 10);
      a1 /= 10;
    }
    return open(buf, a2, 0);
  }
}

static void test();

void loop()
{
  int iter;
  for (iter = 0;; iter++) {
    int pid = fork();
    if (pid < 0)
      fail("loop fork failed");
    if (pid == 0) {
      prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0);
      setpgrp();
      test();
      doexit(0);
    }
    int status = 0;
    uint64_t start = current_time_ms();
    for (;;) {
      int res = waitpid(-1, &status, __WALL | WNOHANG);
      if (res == pid)
        break;
      usleep(1000);
      if (current_time_ms() - start > 5 * 1000) {
        kill(-pid, SIGKILL);
        kill(pid, SIGKILL);
        while (waitpid(-1, &status, __WALL) != pid) {
        }
        break;
      }
    }
  }
}

long r[1];
uint64_t procid;
void test()
{
  memset(r, -1, sizeof(r));
  syscall(__NR_mmap, 0x20000000, 0xfff000, 3, 0x32, -1, 0);
  memcpy((void*)0x2091c000, "/dev/sequencer2", 16);
  syscall(__NR_openat, 0xffffffffffffff9c, 0x2091c000, 0x80003, 0);
  memcpy((void*)0x20539000, "/dev/snd/seq", 13);
  r[0] = syz_open_dev(0x20539000, 0, 0);
  *(uint8_t*)0x20a10000 = 0xe;
  *(uint8_t*)0x20a10001 = 0;
  *(uint8_t*)0x20a10002 = 0xe;
  *(uint8_t*)0x20a10003 = 0;
  *(uint32_t*)0x20a10004 = 0x100;
  *(uint32_t*)0x20a10008 = 0x4000;
  *(uint8_t*)0x20a1000c = 0;
  *(uint8_t*)0x20a1000d = 0;
  *(uint8_t*)0x20a1000e = 0;
  *(uint8_t*)0x20a1000f = 0;
  *(uint8_t*)0x20a10010 = 0;
  *(uint8_t*)0x20a10011 = 0;
  *(uint8_t*)0x20a10012 = 0;
  *(uint8_t*)0x20a10013 = 0;
  *(uint8_t*)0x20a10014 = 0;
  *(uint8_t*)0x20a10015 = 0;
  *(uint8_t*)0x20a10016 = 0;
  *(uint8_t*)0x20a10017 = 0;
  *(uint8_t*)0x20a10018 = 0;
  *(uint8_t*)0x20a10019 = 0;
  *(uint8_t*)0x20a1001a = 0;
  *(uint8_t*)0x20a1001b = 0;
  *(uint8_t*)0x20a1001c = 0;
  *(uint8_t*)0x20a1001d = 0;
  *(uint8_t*)0x20a1001e = 0;
  *(uint8_t*)0x20a1001f = 0;
  *(uint8_t*)0x20a10020 = 0;
  *(uint8_t*)0x20a10021 = 0;
  *(uint8_t*)0x20a10022 = 0;
  *(uint8_t*)0x20a10023 = 0;
  *(uint8_t*)0x20a10024 = 0;
  *(uint8_t*)0x20a10025 = 0;
  *(uint8_t*)0x20a10026 = 0;
  *(uint8_t*)0x20a10027 = 0;
  *(uint8_t*)0x20a10028 = 0;
  *(uint8_t*)0x20a10029 = 0;
  *(uint8_t*)0x20a1002a = 0;
  *(uint8_t*)0x20a1002b = 0;
  *(uint8_t*)0x20a1002c = 0;
  *(uint8_t*)0x20a1002d = 0;
  *(uint8_t*)0x20a1002e = 0;
  *(uint8_t*)0x20a1002f = 0;
  *(uint8_t*)0x20a10030 = 0;
  *(uint8_t*)0x20a10031 = 0;
  *(uint8_t*)0x20a10032 = 0;
  *(uint8_t*)0x20a10033 = 0;
  *(uint8_t*)0x20a10034 = 0;
  *(uint8_t*)0x20a10035 = 0;
  *(uint8_t*)0x20a10036 = 0;
  *(uint8_t*)0x20a10037 = 0;
  *(uint8_t*)0x20a10038 = 0;
  *(uint8_t*)0x20a10039 = 0;
  *(uint8_t*)0x20a1003a = 0;
  *(uint8_t*)0x20a1003b = 0;
  *(uint8_t*)0x20a1003c = 0;
  *(uint8_t*)0x20a1003d = 0;
  *(uint8_t*)0x20a1003e = 0;
  *(uint8_t*)0x20a1003f = 0;
  *(uint8_t*)0x20a10040 = 0;
  *(uint8_t*)0x20a10041 = 0;
  *(uint8_t*)0x20a10042 = 0;
  *(uint8_t*)0x20a10043 = 0;
  *(uint8_t*)0x20a10044 = 0;
  *(uint8_t*)0x20a10045 = 0;
  *(uint8_t*)0x20a10046 = 0;
  *(uint8_t*)0x20a10047 = 0;
  *(uint8_t*)0x20a10048 = 0;
  *(uint8_t*)0x20a10049 = 0;
  *(uint8_t*)0x20a1004a = 0;
  *(uint8_t*)0x20a1004b = 0;
  *(uint8_t*)0x20a1004c = 0;
  *(uint8_t*)0x20a1004d = 0;
  *(uint8_t*)0x20a1004e = 0;
  *(uint8_t*)0x20a1004f = 0;
  syscall(__NR_ioctl, r[0], 0x40505330, 0x20a10000);
}

int main()
{
  for (procid = 0; procid < 8; procid++) {
    if (fork() == 0) {
      for (;;) {
        loop();
      }
    }
  }
  sleep(1000000);
  return 0;
}
