自旋锁(Spin Lock)

 转载请您注明出处:    http://www.cnblogs.com/lsh123/p/7400625.html

 

0x01 自旋锁简介

  自旋锁也是一种同步机制,它能保证某个资源只能被一个线程所拥有,这种保护被形象地称做“上锁”。它可以用于驱动程序中的同步处理。初始化自旋锁时,处理解锁状态,这时它可以被程序“获取”。“获取”后的自旋锁处理于锁定状态,不能再被“获取”。

  如果自旋锁已被锁住,这时有程序申请“获取”这个锁,程序则处于“自旋”状态。所谓自旋状态,就是不停地询问是否可以“获取”自旋锁。自旋锁不同于线程中的等待事件,在线程中如果等待某个事件(Event),操作系统会使这个线程进入休眠状态,CPU会运行其他线程;而自旋锁原理则不同,它不会切换到别的线程,而是一直让这个线程“自旋”。因此对自旋锁占用时间不宜过长,否则会导致申请自旋锁的其他线程处于自旋,会浪费CPU时间。

  驱动程序必须在低于或者等于DISPATCH_LEVEL的IRQL级别中使用自旋锁。

 

 

 

0x02 自旋锁操作函数

  自旋锁的结构:
  KSPIN_LOCK SpinLock;
  KSPIN_LOCK实际是一个操作系统相关的无符号整数,32位系统上是32位的unsigned long,64位系统则定义为unsigned __int64。typedef ULONG_PTR KSPIN_LOCK;  
(ULONG_PTR就是能够装得下指针的无符号整数,在32位被定义成unsigned long,在64位被定义成unsigned __int64)
  在初始化时,其值被设置为0,为空闲状态。
  参见WRK:

  FORCEINLINE
  VOID
  NTAPI
  KeInitializeSpinLock (
      __out PKSPIN_LOCK SpinLock
      ) 
  {
      *SpinLock = 0;
  }

  关于自旋锁的两个基本操作:获取和释放
  VOID 
  KeAcquireSpinLock(
      IN PKSPIN_LOCK  SpinLock,
      OUT PKIRQL  OldIrql
      );
  VOID 
  KeReleaseSpinLock(
      IN PKSPIN_LOCK  SpinLock,
      IN KIRQL  NewIrql
      );

 

 

 

0x03   WRK源码

  继续查阅WRK看KeAcquireSpinLock获取自旋锁的集体操作:

  


  可以看到,操作对象是第一参数SpinLock,同时也与第二参数IRQL有关。
  进一步找到KeAcquireSpinLockRaiseToDpc的定义:

  

__forceinline
KIRQL
KeAcquireSpinLockRaiseToDpc (
    __inout PKSPIN_LOCK SpinLock
    )

/*++

Routine Description:

    This function raises IRQL to DISPATCH_LEVEL and acquires the specified
    spin lock.

Arguments:

    SpinLock - Supplies a pointer to a spin lock.

Return Value:

    The previous IRQL is returned.

--*/

{

    KIRQL OldIrql;

    //
    // Raise IRQL to DISPATCH_LEVEL and acquire the specified spin lock.
    //

    OldIrql = KfRaiseIrql(DISPATCH_LEVEL);
    KxAcquireSpinLock(SpinLock);
    return OldIrql;
}

  第一步就是:

  OldIrql = KfRaiseIrql(DISPATCH_LEVEL);

  提升IRQL到DISPATCH_LEVEL,然后调用KxAcquireSpinLock()。如果当前IRQL就是DISPATCH_LEVEL,那么就直接调用KeAcquireSpinLockAtDpcLevel,省去提升IRQL一步。因为线程调度也是发生在DISPATCH_LEVEL,所以提升IRQL之后当前处理器上就不会发生线程切换。单处理器时,当前只能有一个线程被执行,而这个线程提升IRQL至DISPATCH_LEVEL之后又不会因为调度被切换出去,自然也可以实现我们想要的互斥“效果”。(所以说到了这里,如果是单核计算机的话,实际上已经完全达到了互斥上锁的目的了,但应该还需要考虑多核的情况的,所以就有之后的KxAcquireSpinLock函数。)

 

  进一步查看

  KxAcquireSpinLock函数

__forceinline
VOID
KxAcquireSpinLock (
    __inout PKSPIN_LOCK SpinLock
    )

/*++

Routine Description:

    This function acquires a spin lock at the current IRQL.

Arguments:

    SpinLock - Supplies a pointer to an spin lock.

Return Value:

    None.

--*/

{

    //
    // Acquire the specified spin lock at the current IRQL.
    //

#if !defined(NT_UP)

#if DBG
    LONG64 Thread;

    Thread = (LONG64)KeGetCurrentThread() + 1;
    if (InterlockedCompareExchange64((LONG64 *)SpinLock, Thread, 0) != 0)
#else
    if (InterlockedBitTestAndSet64((LONG64 *)SpinLock, 0))
#endif
    {

        KxWaitForSpinLockAndAcquire(SpinLock);
    }

#else

    UNREFERENCED_PARAMETER(SpinLock);

#endif // !defined(NT_UP)

    return;
}

  再看KxWaitForSpinLockAndAcquire函数,注释也都写明了这个函数是当首次尝试获取spin lock 失败后被调用,随后这个线程“自旋”,直至获取到spin lock 。

  

DECLSPEC_NOINLINE
ULONG64
KxWaitForSpinLockAndAcquire (
    __inout PKSPIN_LOCK SpinLock
    )

/*++

Routine Description:

    This function is called when the first attempt to acquire a spin lock
    fails. A spin loop is executed until the spin lock is free and another
    attempt to acquire is made. If the attempt fails, then another wait
    for the spin lock to become free is initiated.

Arguments:

    SpinLock - Supplies the address of a spin lock.

Return Value:

    The number of wait loops that were executed.

--*/

{

    ULONG64 SpinCount = 0;

#if DBG

    LONG64 Thread = (LONG64)KeGetCurrentThread() + 1;

#endif

    //
    // Wait for spin lock to become free.
    //

    do {
        do {
            KeYieldProcessor();
        } while (*(volatile LONG64 *)SpinLock != 0);

#if DBG

    } while (InterlockedCompareExchange64((LONG64 *)SpinLock, Thread, 0) != 0);

#else

    } while(InterlockedBitTestAndSet64((LONG64 *)SpinLock, 0));

#endif

    return SpinCount;
}

  注意到KxAcquireSpinLock函数中还有一个InterlockedBitTestAndSet64函数,这应该是一个64位的函数,我在WRK中没能找到它的定义,但是找到了它的32位版本:

BOOLEAN
FORCEINLINE
InterlockedBitTestAndSet (
    IN LONG *Base,
    IN LONG Bit
    )
{
    __asm {
           mov eax, Bit
           mov ecx, Base
           lock bts [ecx], eax
           setc al
    };
}

  主要的操作是lock bts [ecx], eax这一条指令,这是一条进行位测试并置位的指令。,这里在进行关键的操作时有lock前缀,保证了多处理器安全。InterLockedXXX函数都有这个特点。显然,KxAcquireSpinLock()函数先测试锁的状态。若锁空闲,则*SpinLock为0,那么InterlockedBitTestAndSet()将返回0,并使*SpinLock置位,不再为0。这样KxAcquireSpinLock()就成功得到了锁,并设置锁为占用状态(*SpinLock不为0),函数返回。若锁已被占用,InterlockedBitTestAndSet()将返回1,此时将调用KxWaitForSpinLockAndAcquire()等待并获取这个锁。这也呼应了初始化时SpinLock置0的操作——SPIN_LOCK为0则锁空闲,非0则已被占有

 

  现在KeAcquireSpinLock函数获取 spin lock的流程就清晰了,总结一下:

  1.KfRaiseIrql函数将IRQL提升DISPATCH_LEVEL级别

  2.KxAcquireSpinLock函数申请获取一个spin lock,它先调用了InterlockedBitTestAndSet函数,测试锁的状态。若锁空闲,即spin lock为0,则使spin lock置位,不再为0。,InterlockedBitTestAndSet()返回0,这样KxAcquireSpinLock()就成功得到了锁若锁已被占用,InterlockedBitTestAndSet()将返回1,此时将调用KxWaitForSpinLockAndAcquire()等待,持续“自旋”知道获取到了这个锁。

 

 

//bp KAtomLock!DriverEntry

KSPIN_LOCK __SpinLock;
KIRQL      __OldIrql;
NTSTATUS DriverEntry(PDRIVER_OBJECT DriverObject, PUNICODE_STRING RegisterPath)
{
    NTSTATUS Status = STATUS_SUCCESS;
    PDEVICE_OBJECT  DeviceObject = NULL;

    DriverObject->DriverUnload = DriverUnload;
    SeCreateSpinLock();
    return Status;
}
VOID SeCreateSpinLock()
{
    KeInitializeSpinLock(&__SpinLock);
    HANDLE  ThreadHandle[2] = { 0 };
    ULONG   i = 0;
    PVOID   ThreadObject[2] = { 0 };
    for (i=0;i<2;i++)
    {
        PsCreateSystemThread(&ThreadHandle[i], 0, NULL, NULL, NULL, ThreadProcedure, (PVOID)(i+1));
    }
    for (i = 0; i < 2; i++)
    {
        ObReferenceObjectByHandle(ThreadHandle[i], 0, NULL, KernelMode, &ThreadObject[i], NULL);
    }
    KeWaitForMultipleObjects(2, ThreadObject, WaitAll, Executive, KernelMode, FALSE, NULL, NULL);
    for (i = 0; i < 2; i++)
    {
        ObDereferenceObject(ThreadObject[i]);
        ZwClose(ThreadHandle[i]);
        ThreadHandle[i] = NULL;
    }
}
VOID DriverUnload(PDRIVER_OBJECT DriverObject)
{
    DbgPrint("DriverUnload()\r\n");
}

VOID ThreadProcedure(PVOID ParameterData)
{
    PITEM Item;
    int Count = 0;

    KeAcquireSpinLock(&__SpinLock, &__OldIrql);  //提升DISPATCH_LEVEL 
    for (Count = 1; Count <= 20; Count += 1)
    {
        DbgPrint("ThreadID%d:%d\r\n",(int)ParameterData,Count);    
    }
    KeReleaseSpinLock(&__SpinLock, &__OldIrql);
    PsTerminateSystemThread(STATUS_SUCCESS);
}

 

 

 

 

 

 

 

 

 

 

 

 

 

  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值