免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: 思一克
打印 上一主题 下一主题

关于LINUX在中断(硬软)中不能睡眠的真正原因 [复制链接]

论坛徽章:
0
41 [报告]
发表于 2007-06-28 13:16 |只看该作者
LZ发帖子是讨论,不是有什么总结性的一句话能概括什么。我也是在学习中。

我觉得,LINUX设计不让中断中睡眠是经过充分考虑的。seelp on interrupt(SOI)不允许是内核设计的原因,而不是有什么绝对本质的无法更改的原因。

我同意zx_wing的说法,没有和进程关联的CONTEXT,而调度的基本单位是进程,因此KERNEL设计者无法容忍sleep on interrrupt(SOI).

下面的相同的讨论说的也很清楚:

think this should clear all confusions, apologies if this is too big
or irrelevant and doesnt make much sense, which I think is not the
case.

Note : The following explaination assumes that the isr and the
interrupted proceess share the stack, afaik isr can seperate stacks,
which is configurable, but I dont know how this explaination holds
good when the isr is having a seperate stack.

1. Why you are not allowed to sleep in an interrupt handler?Was this a
design decision or is there a fundamental reason that makes sleeping
interrupt handlers simply impossible? What about the page fault
handler - it (probably) sleeps when it has to swap in a page, why is
it possible to sleep for the page fault handler and not for an
interrupt handler?

-> Sleeping is implemented using scheduler. Scheduler only schedules
tasks (design decision to keep it simple). So you need a task context
to sleep in. Interrupt is not tied to a process (and uses stack of
whichever happens to be scheduled ) so it can't use the context
because it does not have to be in a sane state.

2. But why.....

You cannot sleep in an interrupt handler because interrupts do not have
a backing process context, and thus there is nothing to reschedule
back into. In other words, interrupt handlers are not associated with
a task, so there is nothing to "put to sleep" and (more importantly)
"nothing to wake up". They must run atomically.
The reason the page fault handler can sleep is that it is invoked only
by code that is running in process context. Because the kernel's own
memory is not pagable, only user-space memory accesses can result in a
page fault. Thus, only a few certain places (such as calls to
copy_{to,from}_user()) can cause a page fault within the kernel. Those
places must all be made by code that can sleep (i.e., process context,
no locks, etc).

3. However can't you consider the interrupt handler as running in the
context of the task A, that was incidentially running when the
interrupt occurred (the interrupt handler
uses the stack of task A, this is not always true, isr might have a
seperate stack). So wouldn't it conceptually be conceivable to put the
interrupt handler to sleep by saving the current CPU state (register
contents) with task A, putting task A asleep, and resume processing of
the interrupt once task A is rescheduled by the scheduler?
Of course this could be considered 'unfair' with respect to task A, in
that the interrupt has no relation to task A besides the fact that
they happend to be on the same CPU at the same time. But I am
interested in learning if there is any fundamental reason against
sleeping in an interrupt handler.

->There are bigger problems. The process is in an arbitrary state when
interrupt is invoked. So it might hold arbitrary spinlocks. It might
be on arbitrary wait_queues. Possibly other funny things might happen.
These either disallow sleeping or could interact badly with the
interrupt trying to sleep.

This is part of conversation happened on the same list before few
years, not verbatim though.
--

这里也说到了如果IRQ不用自己的STACK,而利用被中断任务(任务A)的STACK是否就可以SOI了的问题。
因为中断发生的CONTEXT和A无关,而是碰巧在同一个CPU上的一个任务A被中断了,如果中断睡了,A就回被殃及,系统就彻底乱了。
(和调度发生冲突了)。

至于schedule()会将任务在不同CPU之间分,而IRQ回来后如何?(假定IRQ不使用自己的STACK,可以回来的话)。
还有最后一段说的其他各种复杂的问题。

总之,如果中断不THREAD化,应该无法SOI。所以LINUX(2。6)中shedule()中有检查不允许。

如果改动LINUX,如何改动最小使得可以SOI?(虽然没有大意义,但作为讨论可以从讨论中学习其他的)。



原帖由 zx_wing 于 2007-6-28 12:38 发表于 40楼  

>>总结: 异步异常(中断)handler不是没有上下文, 而是没有固定的上下文,  如果使用被抢占的任务作为上下文, 一,自身的处理无法得到实时保障,导致系统不确定性, 二,任务受到影响.

linux是通用操作系统 ...

论坛徽章:
0
42 [报告]
发表于 2007-06-28 13:59 |只看该作者
原帖由 思一克 于 2007-6-28 13:16 发表于 41楼  
LZ发帖子是讨论,不是有什么总结性的一句话能概括什么。我也是在学习中。

我觉得,LINUX设计不让中断中睡眠是经过充分考虑的。seelp on interrupt(SOI)不允许是内核设计的原因,而不是有什么绝对本质的无法 ...

我说的lz是xiaozhaoz兄,因为他概括的观点有点多,我看的不是很明白,所以想让他总结一下。
呵呵,版主以为我在叫你啦

论坛徽章:
0
43 [报告]
发表于 2007-06-28 14:04 |只看该作者
LZ我认为是说发帖子的人呀?

难到我理解错了。

原帖由 zx_wing 于 2007-6-28 13:59 发表于 42楼  

我说的lz是xiaozhaoz兄,因为他概括的观点有点多,我看的不是很明白,所以想让他总结一下。
呵呵,版主以为我在叫你啦

论坛徽章:
0
44 [报告]
发表于 2007-06-28 14:25 |只看该作者
原帖由 zx_wing 于 2007-6-28 13:59 发表于 42楼  

我说的lz是xiaozhaoz兄,因为他概括的观点有点多,我看的不是很明白,所以想让他总结一下。
呵呵,版主以为我在叫你啦


我个人的看法, 相信大家会有更多的想法.

一句话的总结:
中断sleep会导致系统工作异常.

三句话总结:
    1. 中断sleep会增加 任务, 系统的不确定性
    2. 和中断共享中断号的中断会受到影响.
    3. 中断的处理受到被抢占任务上下文属性的限制.

解释和解决的方法看19楼.

总之, 我们都是在玩文字游戏, 中断没有固定上下文(context), 而不是没有上下文.

PS, 正是因为Linux是一个通用的操作系统, 所以必须让用户可以配置成嵌入式实时模式, 所以必须考虑实时性因素!

论坛徽章:
0
45 [报告]
发表于 2007-06-28 14:30 |只看该作者
我怎么觉得你们3位彼此都懂对方说什么,只不过主张不同?

论坛徽章:
0
46 [报告]
发表于 2007-06-28 14:51 |只看该作者
我看了19楼的帖子,

觉得
中断和软中断的线程化和spin_lock的可sleep化这两个并不能提高系统的实时性。

比如spinlock, 就是为了短暂需要lock的时候让CPU空等待。这时比用可以sleep的锁要节省CPU而不是浪费。因为调度的耗费可能要比SPIN的耗费多的多。

linux的中断是半THREAD化的。你可以增加工作在THREAD(softirqd)中的比重,增加后,系统反映更慢了。比如你打一个字,一个网络包的处理,如果都用THREAD做,响应应该是慢一些。因为调度的原因 。

如果不对,请批驳。

[ 本帖最后由 思一克 于 2007-6-28 14:53 编辑 ]

论坛徽章:
0
47 [报告]
发表于 2007-06-28 15:55 |只看该作者
原帖由 思一克 于 2007-6-28 14:51 发表于 46楼  
我看了19楼的帖子,

觉得
中断和软中断的线程化和spin_lock的可sleep化这两个并不能提高系统的实时性。

比如spinlock, 就是为了短暂需要lock的时候让CPU空等待。这时比用可以sleep的锁要节省CPU而不是浪 ...


好几个同事问过同样的问题.

这个问题已经脱离了本贴的原意, 如果有兴趣, 可以另外开贴讨论.

实时性的必须保证, 系统在任何情况下, 可以在一定的时间内执行到某个任务.

1. 你的比较尺度有问题, 你是拿现有系统的最好情况和realtime patch系统的最坏情况比较.
2. 现有系统的最坏情况, 延时可能有几十毫秒, 察看19楼的描述. 而realtime patch的系统只有几十us(不要低估了我们CPU的处理能力, 而且上限制是可以计算的.

具体你可以在网上找到大量的测试报告.

[ 本帖最后由 xiaozhaoz 于 2007-6-28 16:53 编辑 ]

论坛徽章:
0
48 [报告]
发表于 2007-06-28 17:11 |只看该作者
LINUX中到是有中断还没有完全返回就调用schedule()而睡眠过去的例子。

可以猜是哪里。

论坛徽章:
0
49 [报告]
发表于 2007-06-28 19:58 |只看该作者
我觉得,中断和异常不同,中断是异步的,异常和系统调用是同步的。
异常比如缺页异常发生时,当前任务在异常处理完成之前不能继续运行,该异常处理过程和当前任务天然相联系,运行在当前进程的上下文中。
中断的发生很可能是与当前任务无关的,如果把中断处理实现为强行与当前任务相关联,就会造成当前任务由于和他毫无关系的过程的执行而被剥夺了运行的权利。这样的调度实现是不符合公平合理的原则的,是错误的。

论坛徽章:
0
50 [报告]
发表于 2007-06-28 21:53 |只看该作者
原帖由 albcamus 于 2007-6-28 14:30 发表于 45楼  
我怎么觉得你们3位彼此都懂对方说什么,只不过主张不同?

恩,我也觉得其实大家心里都比较明白了,但表述不同而已。
我就此打住了。讨论让我收获不少,谢谢大家。特别是solaris中那个IST让我开眼界了。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP