{"action":"created","issue":{"url":"https://api.github.com/repos/zephyrproject-rtos/zephyr/issues/17869","repository_url":"https://api.github.com/repos/zephyrproject-rtos/zephyr","labels_url":"https://api.github.com/repos/zephyrproject-rtos/zephyr/issues/17869/labels{/name}","comments_url":"https://api.github.com/repos/zephyrproject-rtos/zephyr/issues/17869/comments","events_url":"https://api.github.com/repos/zephyrproject-rtos/zephyr/issues/17869/events","html_url":"https://github.com/zephyrproject-rtos/zephyr/issues/17869","id":474578310,"node_id":"MDU6SXNzdWU0NzQ1NzgzMTA=","number":17869,"title":"Unlocking nested k_sched_lock() cause forced preemption","user":{"login":"yashi","id":10152,"node_id":"MDQ6VXNlcjEwMTUy","avatar_url":"https://avatars2.githubusercontent.com/u/10152?v=4","gravatar_id":"","url":"https://api.github.com/users/yashi","html_url":"https://github.com/yashi","followers_url":"https://api.github.com/users/yashi/followers","following_url":"https://api.github.com/users/yashi/following{/other_user}","gists_url":"https://api.github.com/users/yashi/gists{/gist_id}","starred_url":"https://api.github.com/users/yashi/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/yashi/subscriptions","organizations_url":"https://api.github.com/users/yashi/orgs","repos_url":"https://api.github.com/users/yashi/repos","events_url":"https://api.github.com/users/yashi/events{/privacy}","received_events_url":"https://api.github.com/users/yashi/received_events","type":"User","site_admin":false},"labels":[{"id":700751250,"node_id":"MDU6TGFiZWw3MDA3NTEyNTA=","url":"https://api.github.com/repos/zephyrproject-rtos/zephyr/labels/area:%20Kernel","name":"area: Kernel","color":"ededed","default":false},{"id":383053976,"node_id":"MDU6TGFiZWwzODMwNTM5NzY=","url":"https://api.github.com/repos/zephyrproject-rtos/zephyr/labels/bug","name":"bug","color":"e99695","default":true},{"id":636351494,"node_id":"MDU6TGFiZWw2MzYzNTE0OTQ=","url":"https://api.github.com/repos/zephyrproject-rtos/zephyr/labels/priority:%20medium","name":"priority: medium","color":"d93f0b","default":false}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2019-07-30T13:15:13Z","updated_at":"2019-07-30T17:21:56Z","closed_at":null,"author_association":"CONTRIBUTOR","body":"**Describe the bug**\r\n`k_sched_unlock()` calls `update_cache()` with `preempt_ok` == 1.  But when `k_sched_unlock()` is to unlock nested lock, it will preempt the current thread even though the caller still holds the scheduler lock.\r\n\r\n```\r\nk_sched_lock();\r\nk_sched_lock();\r\nk_sched_unlock();  /* <--- this shouldn't be a scheduling point */\r\nk_sched_unlock();  /* <--- this is a scheduling point */\r\n```\r\n\r\n**To Reproduce**\r\nSteps to reproduce the behavior:\r\n1. Get main.c from https://gist.github.com/yashi/517aab09ce16cda537341f5c5d958fac\r\n1. mkdir build; cd build\r\n1. cmake -DBOARD=qemu_cortex_m3\r\n1. make\r\n1. See error\r\n\r\n**Expected behavior**\r\nNested lock should be taken care.\r\n\r\n**Impact**\r\nA preemptive thread with the scheduler lock is unexpectedly preempted.\r\n\r\n**Screenshots or console output**\r\n```\r\n$ ninja run\r\n[3/8] Linking C executable zephyr/zephyr_prebuilt.elf\r\nMemory region         Used Size  Region Size  %age Used\r\n           FLASH:       11580 B       256 KB      4.42%\r\n            SRAM:       14364 B        64 KB     21.92%\r\n        IDT_LIST:         120 B         2 KB      5.86%\r\n[7/8] To exit from QEMU enter: 'CTRL+a, x'[QEMU] CPU: cortex-m3\r\nqemu-system-arm: warning: nic stellaris_enet.0 has no peer\r\n***** Booting Zephyr OS build zephyr-v1.14.0-2821-g92dd53a55cee *****\r\n*    main: 0x20000180 w/ -2\r\n* preempt: 0x2000006c w/ 1\r\n*    coop: 0x20000008 w/ -1\r\n##### hello from main\r\n##### hello from coop\r\n##### hello from preempt\r\n##### about to unlock\r\n##### This shouldn't be printed\r\nQEMU: Terminated\r\n```\r\n\r\n**Environment (please complete the following information):**\r\nDoesn't matter.\r\n\r\n**Source code:**\r\n```C\r\n#include <zephyr.h>\r\n\r\n#define MY_STACK_SIZE 5000\r\n\r\nK_THREAD_STACK_DEFINE(preempt_stack, MY_STACK_SIZE);\r\nstruct k_thread preempt_thread;\r\n\r\nK_THREAD_STACK_DEFINE(coop_stack, MY_STACK_SIZE);\r\nstruct k_thread coop_thread;\r\n\r\nstatic void preempt(void *arg1, void *arg2, void *arg3)\r\n{\r\n\tARG_UNUSED(arg1);\r\n\tARG_UNUSED(arg2);\r\n\tARG_UNUSED(arg3);\r\n\r\n\tk_sched_lock();\r\n\tk_sched_lock();\r\n\r\n\tprintk(\"##### hello from preempt\\n\");\r\n\r\n\tk_busy_wait(20000);\r\n\r\n\tprintk(\"##### about to unlock\\n\");\r\n\tk_sched_unlock();\r\n\r\n\twhile (true) {\r\n\t}\r\n\r\n\tk_sched_unlock();\r\n}\r\n\r\nstatic void coop(void *arg1, void *arg2, void *arg3)\r\n{\r\n\tARG_UNUSED(arg1);\r\n\tARG_UNUSED(arg2);\r\n\tARG_UNUSED(arg3);\r\n\r\n\tprintk(\"##### hello from coop\\n\");\r\n\tk_usleep(1);\r\n\tprintk(\"##### This shouldn't be printed\\n\");\r\n\r\n\twhile (true) {\r\n\t}\r\n}\r\n\r\nvoid main(void)\r\n{\r\n\tk_thread_priority_set(k_current_get(), -2);\r\n\r\n\tk_thread_create(&preempt_thread, preempt_stack,\r\n\t\t\tK_THREAD_STACK_SIZEOF(preempt_stack),\r\n\t\t\tpreempt,\r\n\t\t\tNULL, NULL, NULL,\r\n\t\t\t1, 0, K_NO_WAIT);\r\n\r\n\tk_thread_create(&coop_thread, coop_stack,\r\n\t\t\tK_THREAD_STACK_SIZEOF(coop_stack),\r\n\t\t\tcoop,\r\n\t\t\tNULL, NULL, NULL,\r\n\t\t\t-1, 0, K_NO_WAIT);\r\n\r\n\tprintk(\"*    main: %p w/ %d\\n\", k_current_get(), k_thread_priority_get(k_current_get()));\r\n\tprintk(\"* preempt: %p w/ %d\\n\", &preempt_thread, k_thread_priority_get(&preempt_thread));\r\n\tprintk(\"*    coop: %p w/ %d\\n\", &coop_thread, k_thread_priority_get(&coop_thread));\r\n\r\n\twhile (true) {\r\n\t\tprintk(\"##### hello from main\\n\");\r\n\t\tk_sleep(100);\r\n\t}\r\n\r\n\treturn;\r\n}\r\n```"},"comment":{"url":"https://api.github.com/repos/zephyrproject-rtos/zephyr/issues/comments/516513519","html_url":"https://github.com/zephyrproject-rtos/zephyr/issues/17869#issuecomment-516513519","issue_url":"https://api.github.com/repos/zephyrproject-rtos/zephyr/issues/17869","id":516513519,"node_id":"MDEyOklzc3VlQ29tbWVudDUxNjUxMzUxOQ==","user":{"login":"aescolar","id":32480799,"node_id":"MDQ6VXNlcjMyNDgwNzk5","avatar_url":"https://avatars0.githubusercontent.com/u/32480799?v=4","gravatar_id":"","url":"https://api.github.com/users/aescolar","html_url":"https://github.com/aescolar","followers_url":"https://api.github.com/users/aescolar/followers","following_url":"https://api.github.com/users/aescolar/following{/other_user}","gists_url":"https://api.github.com/users/aescolar/gists{/gist_id}","starred_url":"https://api.github.com/users/aescolar/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aescolar/subscriptions","organizations_url":"https://api.github.com/users/aescolar/orgs","repos_url":"https://api.github.com/users/aescolar/repos","events_url":"https://api.github.com/users/aescolar/events{/privacy}","received_events_url":"https://api.github.com/users/aescolar/received_events","type":"User","site_admin":false},"created_at":"2019-07-30T17:21:56Z","updated_at":"2019-07-30T17:21:56Z","author_association":"CONTRIBUTOR","body":"CC @andyross "}}
{"actor":{"display_login":"aescolar"}}