{"action":"created","comment":{"url":"https://api.github.com/repos/lkl/linux/pulls/comments/80741016","pull_request_review_id":1775671,"id":80741016,"diff_hunk":"@@ -33,400 +32,136 @@ syscall_handler_t syscall_table[__NR_syscalls] = {\n #endif\n };\n \n-struct syscall {\n-\tlong no, *params, ret;\n-};\n-\n-static struct syscall_thread_data {\n-\tstruct syscall *s;\n-\tvoid *mutex, *completion;\n-\tint irq;\n-\t/* to be accessed from Linux context only */\n-\twait_queue_head_t wqh;\n-\tstruct list_head list;\n-\tbool stop;\n-\tstruct completion stopped;\n-} default_syscall_thread_data;\n-\n-static LIST_HEAD(syscall_threads);\n-\n-static struct syscall *dequeue_syscall(struct syscall_thread_data *data)\n-{\n-\n-\treturn (struct syscall *)__sync_fetch_and_and((long *)&data->s, 0);\n-}\n-\n-static long run_syscall(struct syscall *s)\n+static long run_syscall(long no, long *params)\n {\n \tlong ret;\n \n-\tif (s->no < 0 || s->no >= __NR_syscalls)\n-\t\tret = -ENOSYS;\n-\telse {\n-\t\tret = syscall_table[s->no](s->params[0], s->params[1],\n-\t\t\t\t\t   s->params[2], s->params[3],\n-\t\t\t\t\t   s->params[4], s->params[5]);\n-\t}\n-\ts->ret = ret;\n+\tif (no < 0 || no >= __NR_syscalls)\n+\t\treturn -ENOSYS;\n+\n+\tret = syscall_table[no](params[0], params[1], params[2], params[3],\n+\t\t\t\tparams[4], params[5]);\n \n \ttask_work_run();\n \n \treturn ret;\n }\n \n-static irqreturn_t syscall_irq_handler(int irq, void *dev_id)\n-{\n-\tstruct syscall_thread_data *data = (struct syscall_thread_data *)dev_id;\n \n-\twake_up(&data->wqh);\n+#define CLONE_FLAGS (CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_THREAD |\t\\\n+\t\t     CLONE_SIGHAND | SIGCHLD)\n \n-\treturn IRQ_HANDLED;\n-}\n+static int host_task_id;\n+static struct task_struct *host0;\n \n-static void cleanup_syscall_threads(void);\n-\n-int syscall_thread(void *_data)\n+static int new_host_task(struct task_struct **task)\n {\n-\tstruct syscall_thread_data *data;\n-\tstruct syscall *s;\n-\tint ret;\n-\tstatic int count;\n-\n-\tdata = (struct syscall_thread_data *)_data;\n-\tinit_waitqueue_head(&data->wqh);\n-\tlist_add(&data->list, &syscall_threads);\n-\tinit_completion(&data->stopped);\n-\n-\tsnprintf(current->comm, sizeof(current->comm), \"ksyscalld%d\", count++);\n-\n-\tdata->irq = lkl_get_free_irq(\"syscall\");\n-\tif (data->irq < 0) {\n-\t\tpr_err(\"lkl: %s: failed to allocate irq: %d\\n\", __func__,\n-\t\t       data->irq);\n-\t\treturn data->irq;\n-\t}\n-\n-\tret = request_irq(data->irq, syscall_irq_handler, 0, current->comm,\n-\t\t\t  data);\n-\tif (ret) {\n-\t\tpr_err(\"lkl: %s: failed to request irq %d: %d\\n\", __func__,\n-\t\t       data->irq, ret);\n-\t\tlkl_put_irq(data->irq, \"syscall\");\n-\t\tdata->irq = -1;\n-\t\treturn ret;\n-\t}\n-\n-\tpr_info(\"lkl: syscall thread %s initialized (irq%d)\\n\", current->comm,\n-\t\tdata->irq);\n-\n-\t/* system call thread is ready */\n-\tlkl_ops->sem_up(data->completion);\n-\n-\twhile (1) {\n-\t\twait_event(data->wqh,\n-\t\t\t   (s = dequeue_syscall(data)) != NULL || data->stop);\n-\n-\t\tif (data->stop || s->no == __NR_reboot)\n-\t\t\tbreak;\n-\n-\t\trun_syscall(s);\n+\tpid_t pid;\n \n-\t\tlkl_ops->sem_up(data->completion);\n-\t}\n+\tswitch_to_host_task(host0);\n \n-\tif (data == &default_syscall_thread_data)\n-\t\tcleanup_syscall_threads();\n+\tpid = kernel_thread(NULL, NULL, CLONE_FLAGS);\n+\tif (pid < 0)\n+\t\treturn pid;\n \n-\tpr_info(\"lkl: exiting syscall thread %s\\n\", current->comm);\n+\trcu_read_lock();\n+\t*task = find_task_by_pid_ns(pid, &init_pid_ns);\n+\trcu_read_unlock();\n \n-\tlist_del(&data->list);\n+\tget_task_struct(*task);","path":"arch/lkl/kernel/syscalls.c","position":141,"original_position":141,"commit_id":"94822bd5f566dc676c4d57f46b2795dbd1ca36f1","original_commit_id":"94822bd5f566dc676c4d57f46b2795dbd1ca36f1","user":{"login":"tavip","id":213300,"avatar_url":"https://avatars.githubusercontent.com/u/213300?v=3","gravatar_id":"","url":"https://api.github.com/users/tavip","html_url":"https://github.com/tavip","followers_url":"https://api.github.com/users/tavip/followers","following_url":"https://api.github.com/users/tavip/following{/other_user}","gists_url":"https://api.github.com/users/tavip/gists{/gist_id}","starred_url":"https://api.github.com/users/tavip/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tavip/subscriptions","organizations_url":"https://api.github.com/users/tavip/orgs","repos_url":"https://api.github.com/users/tavip/repos","events_url":"https://api.github.com/users/tavip/events{/privacy}","received_events_url":"https://api.github.com/users/tavip/received_events","type":"User","site_admin":false},"body":" To avoid races when terminating a task, in __switch_to. Say we end a host task with del_host_task. We will call do_exit which eventually calls schedule and __switch_to. The first thing we do is to release the semaphore which will run finish_task_switch which in turn will issue put_task_struct which will free the thread_info structure. This can happen before we have a chance to test the flags in the thread that shutsdown.\r\n\r\nYes, we need a put_struct before thread_exit() in __switch_to but it got lost in the iterations :) Or perhaps it is simpler to make a copy of the _prev flags on stack.\r\n\r\nGood catch, thanks !","created_at":"2016-09-27T16:56:19Z","updated_at":"2016-09-27T16:56:19Z","html_url":"https://github.com/lkl/linux/pull/246#discussion_r80741016","pull_request_url":"https://api.github.com/repos/lkl/linux/pulls/246","_links":{"self":{"href":"https://api.github.com/repos/lkl/linux/pulls/comments/80741016"},"html":{"href":"https://github.com/lkl/linux/pull/246#discussion_r80741016"},"pull_request":{"href":"https://api.github.com/repos/lkl/linux/pulls/246"}}},"pull_request":{"url":"https://api.github.com/repos/lkl/linux/pulls/246","id":86676510,"html_url":"https://github.com/lkl/linux/pull/246","diff_url":"https://github.com/lkl/linux/pull/246.diff","patch_url":"https://github.com/lkl/linux/pull/246.patch","issue_url":"https://api.github.com/repos/lkl/linux/issues/246","number":246,"state":"open","locked":false,"title":"Direct host calls - v2","user":{"login":"tavip","id":213300,"avatar_url":"https://avatars.githubusercontent.com/u/213300?v=3","gravatar_id":"","url":"https://api.github.com/users/tavip","html_url":"https://github.com/tavip","followers_url":"https://api.github.com/users/tavip/followers","following_url":"https://api.github.com/users/tavip/following{/other_user}","gists_url":"https://api.github.com/users/tavip/gists{/gist_id}","starred_url":"https://api.github.com/users/tavip/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tavip/subscriptions","organizations_url":"https://api.github.com/users/tavip/orgs","repos_url":"https://api.github.com/users/tavip/repos","events_url":"https://api.github.com/users/tavip/events{/privacy}","received_events_url":"https://api.github.com/users/tavip/received_events","type":"User","site_admin":false},"body":"This is another try to implement direct host calls. This time I took a slightly different approach where we don't  switch back to the idle thread when finish a syscall, we keep the current context (unless others syscalls from other threads are in progress, in which case we schedule to give them a chance to run). This means that for most next syscalls we have the proper context and we can execute code directly.\r\n\r\nThis approach is very similar to user/kernel context where, when exiting the syscall, the current kernel thread context is preserved. In our case, when exiting the syscall, the host thread will continue executing host code, like the regular kernel would execute user code.\r\n\r\nOne side-effect of this is that the kernel threads will not get a chance to run until the next system call. Note that since we allow for direct irqs, irqs and softirqs will run.\r\n\r\nThis approach brings the syscall latency to about 100-150ns on my machine (from ~6000ns). It also increases the TCP_RR/TCP_STREAM/TCP_MAERTS performance by ~10%.\n\n<!-- Reviewable:start -->\n---\nThis change is [<img src=\"https://reviewable.io/review_button.svg\" height=\"34\" align=\"absmiddle\" alt=\"Reviewable\"/>](https://reviewable.io/reviews/lkl/linux/246)\n<!-- Reviewable:end -->\n","created_at":"2016-09-26T03:24:44Z","updated_at":"2016-09-27T16:56:19Z","closed_at":null,"merged_at":null,"merge_commit_sha":"142eba25027cd83dfc76df9c9e2ece506e8a23ec","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/lkl/linux/pulls/246/commits","review_comments_url":"https://api.github.com/repos/lkl/linux/pulls/246/comments","review_comment_url":"https://api.github.com/repos/lkl/linux/pulls/comments{/number}","comments_url":"https://api.github.com/repos/lkl/linux/issues/246/comments","statuses_url":"https://api.github.com/repos/lkl/linux/statuses/94822bd5f566dc676c4d57f46b2795dbd1ca36f1","head":{"label":"tavip:no-syscall-threads","ref":"no-syscall-threads","sha":"94822bd5f566dc676c4d57f46b2795dbd1ca36f1","user":{"login":"tavip","id":213300,"avatar_url":"https://avatars.githubusercontent.com/u/213300?v=3","gravatar_id":"","url":"https://api.github.com/users/tavip","html_url":"https://github.com/tavip","followers_url":"https://api.github.com/users/tavip/followers","following_url":"https://api.github.com/users/tavip/following{/other_user}","gists_url":"https://api.github.com/users/tavip/gists{/gist_id}","starred_url":"https://api.github.com/users/tavip/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tavip/subscriptions","organizations_url":"https://api.github.com/users/tavip/orgs","repos_url":"https://api.github.com/users/tavip/repos","events_url":"https://api.github.com/users/tavip/events{/privacy}","received_events_url":"https://api.github.com/users/tavip/received_events","type":"User","site_admin":false},"repo":{"id":67880482,"name":"linux","full_name":"tavip/linux","owner":{"login":"tavip","id":213300,"avatar_url":"https://avatars.githubusercontent.com/u/213300?v=3","gravatar_id":"","url":"https://api.github.com/users/tavip","html_url":"https://github.com/tavip","followers_url":"https://api.github.com/users/tavip/followers","following_url":"https://api.github.com/users/tavip/following{/other_user}","gists_url":"https://api.github.com/users/tavip/gists{/gist_id}","starred_url":"https://api.github.com/users/tavip/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tavip/subscriptions","organizations_url":"https://api.github.com/users/tavip/orgs","repos_url":"https://api.github.com/users/tavip/repos","events_url":"https://api.github.com/users/tavip/events{/privacy}","received_events_url":"https://api.github.com/users/tavip/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/tavip/linux","description":"Linux kernel source tree","fork":true,"url":"https://api.github.com/repos/tavip/linux","forks_url":"https://api.github.com/repos/tavip/linux/forks","keys_url":"https://api.github.com/repos/tavip/linux/keys{/key_id}","collaborators_url":"https://api.github.com/repos/tavip/linux/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/tavip/linux/teams","hooks_url":"https://api.github.com/repos/tavip/linux/hooks","issue_events_url":"https://api.github.com/repos/tavip/linux/issues/events{/number}","events_url":"https://api.github.com/repos/tavip/linux/events","assignees_url":"https://api.github.com/repos/tavip/linux/assignees{/user}","branches_url":"https://api.github.com/repos/tavip/linux/branches{/branch}","tags_url":"https://api.github.com/repos/tavip/linux/tags","blobs_url":"https://api.github.com/repos/tavip/linux/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/tavip/linux/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/tavip/linux/git/refs{/sha}","trees_url":"https://api.github.com/repos/tavip/linux/git/trees{/sha}","statuses_url":"https://api.github.com/repos/tavip/linux/statuses/{sha}","languages_url":"https://api.github.com/repos/tavip/linux/languages","stargazers_url":"https://api.github.com/repos/tavip/linux/stargazers","contributors_url":"https://api.github.com/repos/tavip/linux/contributors","subscribers_url":"https://api.github.com/repos/tavip/linux/subscribers","subscription_url":"https://api.github.com/repos/tavip/linux/subscription","commits_url":"https://api.github.com/repos/tavip/linux/commits{/sha}","git_commits_url":"https://api.github.com/repos/tavip/linux/git/commits{/sha}","comments_url":"https://api.github.com/repos/tavip/linux/comments{/number}","issue_comment_url":"https://api.github.com/repos/tavip/linux/issues/comments{/number}","contents_url":"https://api.github.com/repos/tavip/linux/contents/{+path}","compare_url":"https://api.github.com/repos/tavip/linux/compare/{base}...{head}","merges_url":"https://api.github.com/repos/tavip/linux/merges","archive_url":"https://api.github.com/repos/tavip/linux/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/tavip/linux/downloads","issues_url":"https://api.github.com/repos/tavip/linux/issues{/number}","pulls_url":"https://api.github.com/repos/tavip/linux/pulls{/number}","milestones_url":"https://api.github.com/repos/tavip/linux/milestones{/number}","notifications_url":"https://api.github.com/repos/tavip/linux/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/tavip/linux/labels{/name}","releases_url":"https://api.github.com/repos/tavip/linux/releases{/id}","deployments_url":"https://api.github.com/repos/tavip/linux/deployments","created_at":"2016-09-10T16:01:05Z","updated_at":"2016-09-10T16:06:37Z","pushed_at":"2016-09-26T02:47:13Z","git_url":"git://github.com/tavip/linux.git","ssh_url":"git@github.com:tavip/linux.git","clone_url":"https://github.com/tavip/linux.git","svn_url":"https://github.com/tavip/linux","homepage":"","size":1630359,"stargazers_count":0,"watchers_count":0,"language":"C","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"lkl:master","ref":"master","sha":"75def1f9393eaf3bdfbc2b13e894ac69299ab142","user":{"login":"lkl","id":247191,"avatar_url":"https://avatars.githubusercontent.com/u/247191?v=3","gravatar_id":"","url":"https://api.github.com/users/lkl","html_url":"https://github.com/lkl","followers_url":"https://api.github.com/users/lkl/followers","following_url":"https://api.github.com/users/lkl/following{/other_user}","gists_url":"https://api.github.com/users/lkl/gists{/gist_id}","starred_url":"https://api.github.com/users/lkl/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lkl/subscriptions","organizations_url":"https://api.github.com/users/lkl/orgs","repos_url":"https://api.github.com/users/lkl/repos","events_url":"https://api.github.com/users/lkl/events{/privacy}","received_events_url":"https://api.github.com/users/lkl/received_events","type":"Organization","site_admin":false},"repo":{"id":45491718,"name":"linux","full_name":"lkl/linux","owner":{"login":"lkl","id":247191,"avatar_url":"https://avatars.githubusercontent.com/u/247191?v=3","gravatar_id":"","url":"https://api.github.com/users/lkl","html_url":"https://github.com/lkl","followers_url":"https://api.github.com/users/lkl/followers","following_url":"https://api.github.com/users/lkl/following{/other_user}","gists_url":"https://api.github.com/users/lkl/gists{/gist_id}","starred_url":"https://api.github.com/users/lkl/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lkl/subscriptions","organizations_url":"https://api.github.com/users/lkl/orgs","repos_url":"https://api.github.com/users/lkl/repos","events_url":"https://api.github.com/users/lkl/events{/privacy}","received_events_url":"https://api.github.com/users/lkl/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/lkl/linux","description":"Linux kernel source tree","fork":true,"url":"https://api.github.com/repos/lkl/linux","forks_url":"https://api.github.com/repos/lkl/linux/forks","keys_url":"https://api.github.com/repos/lkl/linux/keys{/key_id}","collaborators_url":"https://api.github.com/repos/lkl/linux/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/lkl/linux/teams","hooks_url":"https://api.github.com/repos/lkl/linux/hooks","issue_events_url":"https://api.github.com/repos/lkl/linux/issues/events{/number}","events_url":"https://api.github.com/repos/lkl/linux/events","assignees_url":"https://api.github.com/repos/lkl/linux/assignees{/user}","branches_url":"https://api.github.com/repos/lkl/linux/branches{/branch}","tags_url":"https://api.github.com/repos/lkl/linux/tags","blobs_url":"https://api.github.com/repos/lkl/linux/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/lkl/linux/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/lkl/linux/git/refs{/sha}","trees_url":"https://api.github.com/repos/lkl/linux/git/trees{/sha}","statuses_url":"https://api.github.com/repos/lkl/linux/statuses/{sha}","languages_url":"https://api.github.com/repos/lkl/linux/languages","stargazers_url":"https://api.github.com/repos/lkl/linux/stargazers","contributors_url":"https://api.github.com/repos/lkl/linux/contributors","subscribers_url":"https://api.github.com/repos/lkl/linux/subscribers","subscription_url":"https://api.github.com/repos/lkl/linux/subscription","commits_url":"https://api.github.com/repos/lkl/linux/commits{/sha}","git_commits_url":"https://api.github.com/repos/lkl/linux/git/commits{/sha}","comments_url":"https://api.github.com/repos/lkl/linux/comments{/number}","issue_comment_url":"https://api.github.com/repos/lkl/linux/issues/comments{/number}","contents_url":"https://api.github.com/repos/lkl/linux/contents/{+path}","compare_url":"https://api.github.com/repos/lkl/linux/compare/{base}...{head}","merges_url":"https://api.github.com/repos/lkl/linux/merges","archive_url":"https://api.github.com/repos/lkl/linux/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/lkl/linux/downloads","issues_url":"https://api.github.com/repos/lkl/linux/issues{/number}","pulls_url":"https://api.github.com/repos/lkl/linux/pulls{/number}","milestones_url":"https://api.github.com/repos/lkl/linux/milestones{/number}","notifications_url":"https://api.github.com/repos/lkl/linux/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/lkl/linux/labels{/name}","releases_url":"https://api.github.com/repos/lkl/linux/releases{/id}","deployments_url":"https://api.github.com/repos/lkl/linux/deployments","created_at":"2015-11-03T19:53:35Z","updated_at":"2016-09-15T19:40:19Z","pushed_at":"2016-09-26T03:25:07Z","git_url":"git://github.com/lkl/linux.git","ssh_url":"git@github.com:lkl/linux.git","clone_url":"https://github.com/lkl/linux.git","svn_url":"https://github.com/lkl/linux","homepage":"","size":1630716,"stargazers_count":151,"watchers_count":151,"language":"C","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":26,"mirror_url":null,"open_issues_count":26,"forks":26,"open_issues":26,"watchers":151,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/lkl/linux/pulls/246"},"html":{"href":"https://github.com/lkl/linux/pull/246"},"issue":{"href":"https://api.github.com/repos/lkl/linux/issues/246"},"comments":{"href":"https://api.github.com/repos/lkl/linux/issues/246/comments"},"review_comments":{"href":"https://api.github.com/repos/lkl/linux/pulls/246/comments"},"review_comment":{"href":"https://api.github.com/repos/lkl/linux/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/lkl/linux/pulls/246/commits"},"statuses":{"href":"https://api.github.com/repos/lkl/linux/statuses/94822bd5f566dc676c4d57f46b2795dbd1ca36f1"}}}}
{"actor":{"display_login":"tavip"}}