[BUG] Scheduler evicting task when pre-emption is disabled for that task

Đang mở
#1,376 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 3 ngày

@Xyerophyte đang làm issue này rồi.

Từ ngày 14/3/2026.

  • #1379 của @Xyerophyte — đang mở

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
15/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
c

Hướng nghiên cứu

The bug is in tasks.c: xTaskIncrementTick() leaves xYieldPendings[] set while preemption is disabled, and xTaskGenericNotifyFromISR() then treats that leftover flag as a yield. Read prvYieldForTask(), prvSelectHighestPriorityTask() and the xPreemptionDisable checks around them. Open PR #1379 already targets this, so check it before starting. Done means a task with preemption disabled is not switched out by any ISR path.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

bug

Describe the bug

Referring to FreeRTOS SMP tag: V11.2.0 with the following relevant configuration:

#define configNUMBER_OF_CORES 2
#define configRUN_MULTIPLE_PRIORITIES 1
#define configUSE_PREEMPTION 1
#define configUSE_TASK_PREEMPTION_DISABLE 1
#define configUSE_TIME_SLICING 1

A task is performing a critical operation and cannot be interrupted. It calls vTaskPreemptionDisable(NULL) before the operation and vTaskPreemptionEnable(NULL) after the operation. There is another task at the same priority, which could run via time slicing but for the pre-emption being disabled. The critical operation is lengthy and involves switching the hardware into different modes, so an attempted context switch during the operation will crash the system. However, interrupts need to remain enabled and be serviced as normal.

During the critical operation, a timer tick occurs, causing an interrupt which calls xTaskIncrementTick(). The line https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/0adc196d4bd52a2d91102b525b0aafc1e14a2386/tasks.c#L4882 sets xYieldPendings[ xCoreID ] = pdTRUE, because there is another task at the same priority that could run via time slicing, but the line https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/0adc196d4bd52a2d91102b525b0aafc1e14a2386/tasks.c#L4931 prevents this flag being acted upon immediately, because pxCurrentTCBs[ xCoreID ]->xPreemptionDisable != pdFALSE and thus it neither sets xSwitchRequired = pdTRUE nor calls prvYieldCore( xCoreID ). So far, so good.

Then, still during the critical operation, an unrelated I/O operation completes, causing an interrupt which calls xTaskGenericNotifyFromISR(). The unrelated task being notified, was blocked waiting for this notification, so it gets to line https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/0adc196d4bd52a2d91102b525b0aafc1e14a2386/tasks.c#L8154 which adds it to the ready list, and then line https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/0adc196d4bd52a2d91102b525b0aafc1e14a2386/tasks.c#L8182 which has to make a decision about yielding to the newly woken task. It first calls prvYieldForTask( pxTCB ) but this routine does not do anything because it correctly sees that pxCurrentTCBs[ xCoreID ]->xPreemptionDisable != pdFALSE and so context switching is not allowed. Still good.

The bug is that upon return from prvYieldForTask(), the next line https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/0adc196d4bd52a2d91102b525b0aafc1e14a2386/tasks.c#L8184 is checking whether xYieldPendings[ portGET_CORE_ID() ] == pdTRUE, obviously intending determine the action taken by prvYieldForTask() which is a void function so doesn't inform whether it yielded or not. Although prvYieldForTask() did not yield, there is a leftover flag in xYieldPendings[] which was set earlier by xTaskIncrementTick(), even though that flag was not acted upon at the time. So it thinks that prvYieldForTask() has yielded and goes on to line https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/0adc196d4bd52a2d91102b525b0aafc1e14a2386/tasks.c#L8188 which sets *pxHigherPriorityTaskWoken = pdTRUE.

My I/O completion interrupt handler ends with a line like portYIELD_FROM_ISR(task_woken), and on the Xtensa port that I'm using, it ends up calling vTaskSwitchContext() from the assembly ISR wrapper, instead of returning to the user code that is performing my critical operation. The actual context switching is done in prvSelectHighestPriorityTask() and the if-block at https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/0adc196d4bd52a2d91102b525b0aafc1e14a2386/tasks.c#L1086 appears to switch in a new ready task (that is not running) without checking pxCurrentTCBs[ xCoreID ]->xPreemptionDisable. It does have such a check further down, at line https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/0adc196d4bd52a2d91102b525b0aafc1e14a2386/tasks.c#L1250, but this is only in regard to re-scheduling the evicted task on another core. The task with pre-emption disabled has already been evicted.

This bug has been verified on real hardware. I had to reconstruct the above sequence of events through tracing, so it is possible that the exact failure sequence could vary slightly from what I described. Indeed it fails in several different ways depending on the exact timing of the timer ticks and unrelated I/O operations (of which several are going on simultaneously). I can see two other possible failure paths through xTaskResumeAll() or xTaskResumeFromISR(), and these are called from basically everywhere -- I think another of my failure cases is caused by my calling xSemaphoreGiveFromISR() -> xQueueGenericSend() -> xTaskResumeAll(), but I haven't definitively verified this. What I can say, is that it is definitely dangerous to leave a true value in xYieldPendings[] because there are a number of ways that it could be acted upon later.

My diagnosis is that the incorrect logic is in xTaskIncrementTick(), and that the bug was introduced with commit https://github.com/FreeRTOS/FreeRTOS-Kernel/tree/294569e495515e238dc890a8b4613d01260d1f06 which was intended to optimize the logic for configNUMBER_OF_CORES > 1. See https://github.com/FreeRTOS/FreeRTOS-Kernel/pull/1118 for the discussion. The original logic was correct, as far as I can see. Backing out this commit fixes the issue for me on real hardware.

Incidentally, the discussion of that PR is claiming that the original code line

    BaseType_t xYieldRequiredForCore[ configNUMBER_OF_CORES ] = { pdFALSE };

was leaving some array members uninitialized. That's not the case. It has always been legal C to initialize just the first array member and rely on the compiler to fill the rest with zeros, even for an auto array as here. MISRA may have clouded the issue -- I believe MISRA accepts an initializer of {0} to initialize the entire array.

Target

  • Development board: ESP32-S3-MINI-1-N4R2
  • Instruction Set Architecture: Xtensa LX
  • IDE and version: command line
  • Toolchain and version: xtensa-esp-elf-gcc (crosstool-NG esp-13.2.0_20240530) 13.2.0

Host

  • Host OS: Devuan
  • Version: Excalibur

To Reproduce
Suggest to reproduce using ESP-IDF with its OTA update feature, which needs to disable pre-emption in order to write the flash chip. A non-trivial test program would have to be written with the following features:

  • Use menuconfig to set the FreeRTOS type to FreeRTOS-SMP rather than Espressif's custom FreeRTOS.
  • Manually upgrade the FreeRTOS-SMP version to the appropriate tag -- Espressif uses an older one without the bug, when they eventually upgrade to a newer version they are likely to encounter this bug eventually.
  • Have either semaphores or direct-to-task notifications happening during the OTA update. Have this happen from interrupts marked as IRAM-safe.
  • Have multiple tasks of the same priority as the one writing the flash during the OTA update, that could execute but for the preemption being disabled.
  • Suggest to have task(s) blocked waiting on the semaphores or direct-to-task notifications. Suggest these additional tasks should have higher priority.

Expected behavior
A task which has disabled pre-emption should never be pre-empted, regardless of what interrupts happen.

Screenshots
Screenshots of the failure mode and the diagnostic tracing can be provided upon request. Additional tests can be performed upon request if there is a suggestion that the current logic should have worked or that the failure mode is different than described.

Additional context
None

Ngôn ngữ chính
C
Star
4.6k
Fork
1.6k
Merge trung bình
2 ngày 22 giờ
Pull request đã merge (30 ngày)
4

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của FreeRTOS/FreeRTOS-Kernel

Tất cả issue của FreeRTOS/FreeRTOS-Kernel

Issue tương tự

Thêm issue về C

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.