try_module_get和module_put

try_module_get

注解:

        1> 位置: /linux/kernel/module.c

        2>声明:static inline int  try_module_get(structmodule *module)

        3> 功能:判断 module 模块是否处于活动状态,然后通过 local_inc() 宏将该模块的引用计数加 1

        4>返回值:

linux-2.6中返回值是一个整数,如果该模块处于活动状态且对它引用计数加1操作正确则返回1,否则返回0

linux-3.7.5中返回值是一个bool量,正确返回true,错误返回false!

实现方式Linux-2.6

static inline int try_module_get(struct module *module)
{
    int ret = 1;

    if (module) {
        unsigned int cpu = get_cpu();
        if (likely(module_is_live(module))) {
            local_inc(__module_ref_addr(module, cpu));
            trace_module_get(module, _THIS_IP_,
                local_read(__module_ref_addr(module, cpu)));
        }   
        else
            ret = 0;
        put_cpu();
    }   
    return ret;
}

实现方式Linux-3.7.5中

bool try_module_get(struct module *module)
{
    bool ret = true;

    if (module) {
        preempt_disable();

        if (likely(module_is_live(module))) {
            __this_cpu_inc(module->refptr->incs);
            trace_module_get(module, _RET_IP_);
        } else 
            ret = false;

        preempt_enable();
    }    
    return ret; 
}
EXPORT_SYMBOL(try_module_get);

module_put

注解:

     1>声明:

Linux-3.7.5中void module_put(struct module *module)

Linux-2.6中static inline void module_put(struct module *module) 

2>功能:使指定的模块使用量减一

实现方式Linux-2.6中,是空的   我很不解,求高手解释!

static inline void module_put(struct module *module)  ///不解!!
{
}


Linux-3.7.5中

void module_put(struct module *module)
{
    if (module) {
        preempt_disable();
        smp_wmb(); /* see comment in module_refcount */
        __this_cpu_inc(module->refptr->decs);

        trace_module_put(module, _RET_IP_);
        /* Maybe they're waiting for us to drop reference? */
        if (unlikely(!module_is_live(module)))
            wake_up_process(module->waiter);
        preempt_enable();
    }    
}
EXPORT_SYMBOL(module_put);

这两个函数的使用实例,hello模块init函数

/*模块初始化函数*/
static int __init hello_init(void)
{
        printk("<0>module_refcount(module):%d\n",module_refcount(THIS_MODULE));
        try_module_get(THIS_MODULE);

        printk("<0>module_refcount(module):%d\n",module_refcount(THIS_MODULE));
        module_put(THIS_MODULE);
        printk("<0>module_refcount(module):%d\n",module_refcount(THIS_MODULE));
        return 0;
}

打印的结果

[root@root hello模块]# 
Message from syslogd@localhost at Feb  2 09:07:45 ...
 kernel:module_refcount(module):1

Message from syslogd@localhost at Feb  2 09:07:45 ...
 kernel:module_refcount(module):2
 
Message from syslogd@localhost at Feb  2 09:07:45 ...
 kernel:module_refcount(module):1

由上面的程序可以看出来在模块加载的过程中,模块的使用量就是1了,然后用try_module_get把使用量变为2,再使用module_put把使用量变为1,加载完成后使用量为0
开始写程序的时候把module_put写在了__eixt中,导致加载了模块使用量一直为1,无法卸载只能重启!后来才知道rmmod是在调用module_exit之前检查模块的引用计数的,所以在exit之前就应该要module_put释放引用计数,这样一来把module_put写在init里面就可以解决了!

  • 6
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
A Practical Guide to TPM 2.0: Using the Trusted Platform Module in the New Age of Security is a straight-forward primer for developers. It shows security and TPM concepts, demonstrating their use in real applications that the reader can try out. Simply put, this book is designed to empower and excite the programming community to go out and do cool things with the TPM. The approach is to ramp the reader up quickly and keep their interest.A Practical Guide to TPM 2.0: Using the Trusted Platform Module in the New Age of Security explains security concepts, describes the TPM 2.0 architecture, and provides code and pseudo-code examples in parallel, from very simple concepts and code to highly complex concepts and pseudo-code. The book includes instructions for the available execution environments and real code examples to get readers up and talking to the TPM quickly. The authors then help the users expand on that with pseudo-code descriptions of useful applications using the TPM. What you’ll learn TPM 2.0 architecture fundamentals, including changes from TPM 1.2 TPM 2.0 security concepts Essential application development techniques A deep dive into the features of TPM 2.0 A primer on the execution environments available for application development. Learn as you go! Who this book is for Application software developers, OS developers, device-driver developers, and embedded-device specialists, who will benefit from mastering TPM 2.0 capabilities and building their own applications quickly. This book will give them the tools they need to experiment with and understand the technology. Software architects who need to understand the security guarantees provided by TPMs Managers who fund the projects that use TPMs. Non-technical users who may want to know why TPMs are on their computers and how to make use of them. Table of Contents Chapter 1: History of the TPM Chapter 2: Basic Security Concepts Chapter 3: Quick Tutorial on TPM 2.0 Chapter 4: Existing Applications That Use TPMs Chapter 5: Navigating the Specification Chapter 6: Execution Environment Chapter 7: TPM Software Stack Chapter 8: TPM Entities Chapter 9: Hierarchies Chapter 10: Keys Chapter 11: NV Indexes Chapter 12: Platform Configuration Registers Chapter 13: Authorizations and Sessions Chapter 14: Extended Authorization (EA) Policies Chapter 15: Key Management Chapter 16: Auditing TPM Commands Chapter 17: Decrypt/Encrypt Sessions Chapter 18: Context Management Chapter 19: Startup, Shutdown, and Provisioning Chapter 20: Debugging Chapter 21: Solving Bigger Problems with the TPM 2.0 Chapter 22: Platform Security Technologies That Use TPM 2.0
A Practical Guide to TPM 2.0: Using the Trusted Platform Module in the New Age of Security is a straight-forward primer for developers. It shows security and TPM concepts, demonstrating their use in real applications that the reader can try out. Simply put, this book is designed to empower and excite the programming community to go out and do cool things with the TPM. The approach is to ramp the reader up quickly and keep their interest.A Practical Guide to TPM 2.0: Using the Trusted Platform Module in the New Age of Security explains security concepts, describes the TPM 2.0 architecture, and provides code and pseudo-code examples in parallel, from very simple concepts and code to highly complex concepts and pseudo-code. The book includes instructions for the available execution environments and real code examples to get readers up and talking to the TPM quickly. The authors then help the users expand on that with pseudo-code descriptions of useful applications using the TPM. What you’ll learn TPM 2.0 architecture fundamentals, including changes from TPM 1.2 TPM 2.0 security concepts Essential application development techniques A deep dive into the features of TPM 2.0 A primer on the execution environments available for application development. Learn as you go! Who this book is for Application software developers, OS developers, device-driver developers, and embedded-device specialists, who will benefit from mastering TPM 2.0 capabilities and building their own applications quickly. This book will give them the tools they need to experiment with and understand the technology. Software architects who need to understand the security guarantees provided by TPMs Managers who fund the projects that use TPMs. Non-technical users who may want to know why TPMs are on their computers and how to make use of them.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值