免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3197 | 回复: 5
打印 上一主题 下一主题

[硬件及驱动] 关于clk_enable()的奇怪问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-06-17 14:07 |只看该作者 |倒序浏览
本帖最后由 fjaygrfjaygr 于 2014-06-17 14:08 编辑


关于COMMON_CLOCK子系统的,下面的代码大都在 drivers/clk/clk.c中。

static struct task_struct *enable_owner;

int clk_enable(struct clk *clk)
{
        unsigned long flags;
        int ret;
        flags = clk_enable_lock();
        ret = __clk_enable(clk);
        clk_enable_unlock(flags);
        return ret;
}

static unsigned long clk_enable_lock(void)
{
        unsigned long flags;
        if (!spin_trylock_irqsave(&enable_lock, flags)) {
                if (enable_owner == current) {
                        enable_refcnt++;
                        return flags;
                        /*enable_lock、enable_owner是全局唯一的,current是类似per-cpu的。如果当前获取锁没有成功,说明了已经有一个执行体[线程或中断]之前获取了锁。如果已经有一个执行体[线程或中断]之前获取了锁,那么执行这段代码的当前执行体,就不可能和 占有 锁的那个执行体 在一个 CPU上运行,因为spin_trylock_irqsave不仅关抢占 还关 本地中断!是这样吗?  在 当前执行体 所处的CPU中就 应该不可能 出现enable_owner == current 的情况啊!?? 因为一个线程不可能同时在两个CPU上运行啊?
*/

                }
               
                spin_lock_irqsave(&enable_lock, flags);
        }
        WARN_ON_ONCE(enable_owner != NULL);
        WARN_ON_ONCE(enable_refcnt != 0);
        enable_owner = current;
        enable_refcnt = 1;
        return flags;
}
static void clk_enable_unlock(unsigned long flags)
{
        WARN_ON_ONCE(enable_owner != current);
        WARN_ON_ONCE(enable_refcnt == 0);

        if (--enable_refcnt)
                return;
        enable_owner = NULL;
        spin_unlock_irqrestore(&enable_lock, flags);
}


clk_enable_lock(void)函数中,我觉得 执行路径 永远不会到  上面的文字说明部分。。。。是不是这样,我想了一个小时,其实还是有点晕,,,

论坛徽章:
15
射手座
日期:2014-02-26 13:45:082015年迎新春徽章
日期:2015-03-04 09:54:452015年辞旧岁徽章
日期:2015-03-03 16:54:15羊年新春福章
日期:2015-02-26 08:47:552015年亚洲杯之卡塔尔
日期:2015-02-03 08:33:45射手座
日期:2014-12-31 08:36:51水瓶座
日期:2014-06-04 08:33:52天蝎座
日期:2014-05-14 14:30:41天秤座
日期:2014-04-21 08:37:08处女座
日期:2014-04-18 16:57:05戌狗
日期:2014-04-04 12:21:33技术图书徽章
日期:2014-03-25 09:00:29
2 [报告]
发表于 2014-06-17 15:02 |只看该作者
个人理解~
其它流程是否也可能获取这把锁?

论坛徽章:
17
水瓶座
日期:2013-08-29 12:09:27白羊座
日期:2014-08-07 12:36:42丑牛
日期:2014-07-24 12:44:41寅虎
日期:2014-04-16 16:15:33寅虎
日期:2014-03-12 09:28:43摩羯座
日期:2014-03-06 13:22:04技术图书徽章
日期:2014-03-06 11:34:50天蝎座
日期:2014-01-09 11:31:44寅虎
日期:2013-12-27 17:01:44双子座
日期:2013-12-27 12:32:29双子座
日期:2013-12-25 09:03:33丑牛
日期:2013-12-24 16:18:44
3 [报告]
发表于 2014-06-17 15:33 |只看该作者
回复 1# fjaygrfjaygr

git blame drivers/clk/clk.c

查下 commit 533ddeb1,解释的非常清楚!clk: allow reentrant calls into the clk framework!

论坛徽章:
15
射手座
日期:2014-02-26 13:45:082015年迎新春徽章
日期:2015-03-04 09:54:452015年辞旧岁徽章
日期:2015-03-03 16:54:15羊年新春福章
日期:2015-02-26 08:47:552015年亚洲杯之卡塔尔
日期:2015-02-03 08:33:45射手座
日期:2014-12-31 08:36:51水瓶座
日期:2014-06-04 08:33:52天蝎座
日期:2014-05-14 14:30:41天秤座
日期:2014-04-21 08:37:08处女座
日期:2014-04-18 16:57:05戌狗
日期:2014-04-04 12:21:33技术图书徽章
日期:2014-03-25 09:00:29
4 [报告]
发表于 2014-06-17 18:25 |只看该作者
asuka2001 发表于 2014-06-17 15:33
回复 1# fjaygrfjaygr

git blame drivers/clk/clk.c

看似
spin_lock_irqsave(&enable_lock, flags);
关中断之后,就没有重入的可能了吧?

论坛徽章:
17
水瓶座
日期:2013-08-29 12:09:27白羊座
日期:2014-08-07 12:36:42丑牛
日期:2014-07-24 12:44:41寅虎
日期:2014-04-16 16:15:33寅虎
日期:2014-03-12 09:28:43摩羯座
日期:2014-03-06 13:22:04技术图书徽章
日期:2014-03-06 11:34:50天蝎座
日期:2014-01-09 11:31:44寅虎
日期:2013-12-27 17:01:44双子座
日期:2013-12-27 12:32:29双子座
日期:2013-12-25 09:03:33丑牛
日期:2013-12-24 16:18:44
5 [报告]
发表于 2014-06-17 20:56 |只看该作者
回复 4# humjb_1983

commit 533ddeb1e86f506129ee388a6cc13796dcf31311
Author: Mike Turquette <mturquette@linaro.org>
Date:   Thu Mar 28 13:59:02 2013 -0700

    clk: allow reentrant calls into the clk framework

    Reentrancy into the clock framework is necessary for clock operations
    that result in nested calls to the clk api.  A common example is a clock
    that is prepared via an i2c transaction, such as a clock inside of a
    discrete audio chip or a power management IC.  The i2c subsystem itself
    will use the clk api resulting in a deadlock:

    clk_prepare(audio_clk)
        i2c_transfer(..)
                clk_prepare(i2c_controller_clk)

    The ability to reenter the clock framework prevents this deadlock.

    Other use cases exist such as allowing .set_rate callbacks to call
    clk_set_parent to achieve the best rate, or to save power in certain
    configurations.  Yet another example is performing pinctrl operations
    from a clk_ops callback.  Calls into the pinctrl subsystem may call
    clk_{un}prepare on an unrelated clock.  Allowing for nested calls to
    reenter the clock framework enables both of these use cases.

    Reentrancy is implemented by two global pointers that track the owner
    currently holding a global lock.  One pointer tracks the owner during
    sleepable, mutex-protected operations and the other one tracks the owner
    during non-interruptible, spinlock-protected operations.

    When the clk framework is entered we try to hold the global lock.  If it
    is held we compare the current task against the current owner; a match
    implies a nested call and we reenter.  If the values do not match then
    we block on the lock until it is released.

    Signed-off-by: Mike Turquette <mturquette@linaro.org>
    Cc: Rajagopal Venkat <rajagopal.venkat@linaro.org>
    Cc: David Brown <davidb@codeaurora.org>
    Tested-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
    Reviewed-by: Thomas Gleixner <tglx@linutronix.de>
    Reviewed-by: Ulf Hansson <ulf.hansson@linaro.org>

论坛徽章:
15
射手座
日期:2014-02-26 13:45:082015年迎新春徽章
日期:2015-03-04 09:54:452015年辞旧岁徽章
日期:2015-03-03 16:54:15羊年新春福章
日期:2015-02-26 08:47:552015年亚洲杯之卡塔尔
日期:2015-02-03 08:33:45射手座
日期:2014-12-31 08:36:51水瓶座
日期:2014-06-04 08:33:52天蝎座
日期:2014-05-14 14:30:41天秤座
日期:2014-04-21 08:37:08处女座
日期:2014-04-18 16:57:05戌狗
日期:2014-04-04 12:21:33技术图书徽章
日期:2014-03-25 09:00:29
6 [报告]
发表于 2014-06-18 12:19 |只看该作者
asuka2001 发表于 2014-06-17 20:56
回复 4# humjb_1983

commit 533ddeb1e86f506129ee388a6cc13796dcf31311

呵呵,感谢,看起来保证framework的可重入,并不限于这一个函数~~
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP