• 指针返回Linux kernel 中的per_cpu宏


    时间紧张,先记一笔,后续优化与完善。

        本博文为原创,遵守CC3.0协议,转载请注明出处:http://blog.csdn.net/lux_veritas/article/details/8977510

        --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

        

        per_cpu宏在linux kernel中很常见,此处结合代码简析一下per_cpu宏实现的功能,基于linux2.6.36kernel版本:

        

        在开启CONFIG_SMP情况下per_cpu宏的实现如下,其功能是根据cpu的值计算其内部数据偏移(per_cpu_offset(cpu)),返回var指针增加该偏移后的地址:

    #define per_cpu(var, cpu) \
    	(*SHIFT_PERCPU_PTR(&(var), per_cpu_offset(cpu)))

        其中,SHIFT_PERCPU_PTR

        宏的定义如下,首先验证

        __p

        是不是为一个

        percpu

        变量指针,然后使

        __p

        的值加

        __offset

        。分析如下:

    /* Weird cast keeps both GCC and sparse happy. */
    #define SHIFT_PERCPU_PTR(__p, __offset)	({				\
    	__verify_pcpu_ptr((__p));					\        //1
    	RELOC_HIDE((typeof(*(__p)) __kernel __force *)(__p), (__offset)); \      //2
    })

        //1验证指针的宏:

    /*
     * Macro which verifies @ptr is a percpu pointer without evaluating
     * @ptr.  This is to be used in percpu accessors to verify that the
     * input parameter is a percpu pointer.
     */
    #define __verify_pcpu_ptr(ptr)	do {					\
    	const void __percpu *__vpp_verify = (typeof(ptr))NULL;		\
    	(void)__vpp_verify;						\
    } while (0)

        //2使指针增量的宏:

        每日一道理
    坚持的昨天叫立足,坚持的今天叫进取,坚持的明天叫成功。
    /*
     * This macro obfuscates arithmetic on a variable address so that gcc
     * shouldn't recognize the original var, and make assumptions about it.
     *
     * This is needed because the C standard makes it undefined to do
     * pointer arithmetic on "objects" outside their boundaries and the
     * gcc optimizers assume this is the case. In particular they
     * assume such arithmetic does not wrap.
     *
     * A miscompilation has been observed because of this on PPC.
     * To work around it we hide the relationship of the pointer and the object
     * using this macro.
     *
     * Versions of the ppc64 compiler before 4.1 had a bug where use of
     * RELOC_HIDE could trash r30. The bug can be worked around by changing
     * the inline assembly constraint from =g to =r, in this particular
     * case either is valid.
     */
    #define RELOC_HIDE(ptr, off)					\
      ({ unsigned long __ptr;					\
    __asm__ ("" : "=r"(__ptr) : "0"(ptr));		\
    //__ptr存放在寄存器中,ptr存放在__ptr地点寄存器中,即实现ptr到__ptr的赋值,最后将__ptr的值返回
    (typeof(ptr)) (__ptr + (off)); })						
    //全部实现将ptr的值加上off,返回给ptr

        

        关于per_cpu_offset的宏定义如下,per_cpu_offset(x)的值实际上是trap_block数组以x作为数组下标的数组项中结构体trap_per_cpu__per_cpu_base的值:

    /*
     * per_cpu_offset() is the offset that has to be added to a
     * percpu variable to get to the instance for a certain processor.
     *
     * Most arches use the __per_cpu_offset array for those offsets but
     * some arches have their own ways of determining the offset (x86_64, s390).
     */
    #define per_cpu_offset(x) (__per_cpu_offset(x))
    
    #define __per_cpu_offset(__cpu) \
    	(trap_block[(__cpu)].__per_cpu_base)
    
    struct trap_per_cpu trap_block[NR_CPUS];

        

        

        Reference

        

    [1]http://www.ibiblio.org/gferg/ldp/GCC-Inline-Assembly-HOWTO.html#s6

        

    [2]http://www.yubo.org/xen/code.php?file=linux-2.6.18-xen.hg/include/linux/compiler-gcc.h&begin=20&end=23&lang=CPP&num=1&version=3.4&title=RELOC_HIDE()

        

    文章结束给大家分享下程序员的一些笑话语录: IT业众生相
    第一级:神人,天资过人而又是技术狂热者同时还拥有过人的商业头脑,高瞻远瞩,技术过人,大器也。如丁磊,求伯君。
    第二级:高人,有天赋,技术过人但没有过人的商业头脑,通常此类人不是顶尖黑客就是技术总监之流。
    第三级:牛人,技术精湛,熟悉行业知识,敢于创新,有自己的公司和软件产品。
    第四级:工头,技术精湛,有领导团队的能力,此类人大公司项目经理居多。
    第五级:技术工人,技术精湛,熟悉行业知识但领导能力欠加,此类人大多为系分人员或资深程序员,基本上桀骜不逊,自视清高,不愿于一般技术人员为伍,在论坛上基本以高手面目出现。
    第六级:熟练工人,技术有广度无深度,喜欢钻研但浅尝辄止。此类人大多为老程序员,其中一部分喜欢利用工具去查找网上有漏洞的服务器,干点坏事以获取成绩感。如果心情好,在论坛上他们会回答菜鸟的大部分问题。此级别为软件业苦力的重要组成部分。
    第七级:工人,某些技术较熟练但缺乏深度和广度,此类人大多为程序员级别,经常在论坛上提问偶尔也回答菜鸟的问题。为软件产业苦力的主要组成部分。
    第八级:菜鸟,入门时间不长,在论坛上会反复提问很初级的问题,有一种唐僧的精神。虽然招人烦但基本很可爱。只要认真钻研,一两年后就能升级到上一层。
    第九级:大忽悠,利用中国教育的弊病,顶着一顶高学历的帽子,在小公司里混个软件部经理,设计不行,代码不行,只会胡乱支配下属,拍领导马屁,在领导面前胡吹海侃,把自己打扮成技术高手的模样。把勾心斗角的办公室文化引入技术部门,实在龌龊!
    第十级:驴或傻X,会写SELECT语句就说自己精通ORALCE,连寄存器有几种都不知道就说自己懂汇编,建议全部送到日本当IT产业工人,挣了日本人的钱还严重打击日本的软件业!

    --------------------------------- 原创文章 By
    指针和返回
    ---------------------------------

  • 相关阅读:
    一. js高级(1)-面向对象编程
    tips01- 定位
    h5c3 part6 flex
    h5c3 part5 background and transform
    template and pagination
    h5c3 part4
    h5c3 part3
    h5c3 part2
    h5c3 part1
    学习博客
  • 原文地址:https://www.cnblogs.com/jiangu66/p/3102217.html
Copyright © 2020-2023  润新知