Received: by 2002:ab2:6991:0:b0:1f7:f6c3:9cb1 with SMTP id v17csp1209505lqo; Thu, 9 May 2024 07:41:11 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUGCRFmYfKIxYswQOGl8E4n/InsktnZArSfEQhSUzGwSDX6bihjHPFI3/dAMzSxGPz5LZYuYuTh67s2YLPO72Thlt60FVr4Sau+nNdVNA== X-Google-Smtp-Source: AGHT+IHSwjCNg6q8p5vHOL6m3oT1FFY/XWmG0+YkyOZuaFywU3bAFcFJLsdqduALuhj5wlRxAxEj X-Received: by 2002:a17:902:c40d:b0:1e8:418b:7640 with SMTP id d9443c01a7336-1eeb0998a3amr65753845ad.48.1715265670967; Thu, 09 May 2024 07:41:10 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715265670; cv=pass; d=google.com; s=arc-20160816; b=GENAqfn3upGJw2l0mxPypwSZr7RK64FuzR1SxrzSOo51wi9V0TGXjrZsVeqEJmiYWu igYQJKqn+6RuhMyrnOT1fgYarUzWEmJEj9DJV8VKzKqKcOnydEzgsjwmd6nHK9jCMJl4 FJYJID4X3L3MPLuR1DCqxbtB0ETj7ajQrhSUfbUeRLl6RY+sg9X7bKbxRJDCtR/AXiOo +myGj+oNjawsKvU6LHAsl0FQPxfc6Trb0VUUTVQdn2Jjtzu7MqOMqPSO+5xpUYEnWTwe pzAg+aR/pQduRbLc6Ic/eJRmto57hBO2i6JvWP8F7psoJVWu/FmQOCGnECB2aujLdOYm CDBA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=to:subject:message-id:date:from:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:dkim-signature; bh=Ve3gsjSEOvuMwQiH+NbsNTGdRZOJIKD3aizKaJe9iMY=; fh=oehhlNM1CR2C4ohURdiINtOeTv99DGDQEVfw8gIONkI=; b=pWfTnb/rYeq/SH9bUSBp0U9/RrfzoIzF2k8ym7XQPn4BOzwEdMK0L0I+p7X1IFyEbz 075Pvle3/WTorK6Rz/NmVnt7snaDf5byIzX6uMzKbtQkhArsFT47g+KyQ47jBVcfBP9g 4/ddUSSpicxv+5h73FXJFWcI9SHlR9UT0Lk5ug73LtY6SVZrKysfjoI75PYCUj4pDrpD hJN6x+9DDjL2pO17aRD+wu/IShHo/R6AKludYx1ocVmDfreQaO8Gsagg5c+aCi5K3pXU gfnhkRhWc8AogArgKIiYCSmcph6DMx8bxXEFghW9f3SO06O8vSDrfNlg1Ah1UMsTz3tZ 1ZfQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=AhUt2G8P; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-174598-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-174598-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id d9443c01a7336-1ef0c16096fsi15254645ad.546.2024.05.09.07.41.10 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 May 2024 07:41:10 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-174598-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=AhUt2G8P; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-174598-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-174598-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 1BF59B2189A for ; Thu, 9 May 2024 14:39:35 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D88DC3BBE3; Thu, 9 May 2024 14:39:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="AhUt2G8P" Received: from mail-yb1-f174.google.com (mail-yb1-f174.google.com [209.85.219.174]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 799CE2C683 for ; Thu, 9 May 2024 14:39:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.174 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715265567; cv=none; b=BebMzfrU5aCRRhz7L9OXUX+77mygMqFH5t7NTMEAwTCl6NT2R13CRNbQps/magQl8er6D91AT1FOO3XBNk7dpOjxqk3DwFnrFJqfhXMxxKrFra3glNewQ4VAcHJOIT2gL1mfcx5lFK4qcv/RceT5flHBAbcjhg8Myg60cMopHfE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715265567; c=relaxed/simple; bh=e4tPfjYMgnYgzdvbv0igdz0nugwTuBFyfkXUhHnWO+Q=; h=MIME-Version:From:Date:Message-ID:Subject:To:Content-Type; b=EqhUPOq5DqI/HU/+jS40y0+0TTL4Hv/PhCmrScjgkUtcpUqqd+JcRUN2ptfAox9whXXDXCGvEgpPyIiJXwr0NKPxDZSUNdSvJFIQcPjXAnMejvDbUXwx2N5cifGwb3yTTcpw2t/fO57qiotKOWh59BNbCNGxymIGeu1MCj2w8MA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=AhUt2G8P; arc=none smtp.client-ip=209.85.219.174 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-yb1-f174.google.com with SMTP id 3f1490d57ef6-de462f3d992so997149276.2 for ; Thu, 09 May 2024 07:39:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1715265564; x=1715870364; darn=vger.kernel.org; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=Ve3gsjSEOvuMwQiH+NbsNTGdRZOJIKD3aizKaJe9iMY=; b=AhUt2G8P8msXJyetbTrdCz7Hfo7QLIpmzfSi9IHwQa9+R2nN75MH+6ulp77rE3qsgV PCkayFvVep7B1CA/dNx0Z/+T72Zj4imuGVu6JxOcm+l8TlncpRGcbtBMepDFEBYYnIwi YkAWd45zO4jvaKsbnbI4XpKSy1eZitR7HEPaMxwqIjXr0081XtzhFydcP/kMbrAJ9NoJ T1EAQO0DRK8UQQsipsZ7O6OdRfO7ciTH+AbzisOro1uzWpzZL1yJwiUIs/rrK5McCUY9 jtS/fVo2DlKbz6ktTilFUCi0heaQISgFwYA/U3M82nTBslto69CmyAUn8OWCwHDJs2oq a8Zw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715265564; x=1715870364; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=Ve3gsjSEOvuMwQiH+NbsNTGdRZOJIKD3aizKaJe9iMY=; b=ATfLT6BBCHLdXubkvFOYkIhSNUB64rmfX/6LCGVDQWzjLkvHdZvRiWq5NpibYBdWWM Lr+0iT01vxnXtjZYGBK6f2jXG8HNa3mha2IWIDfS7C3PlcmQTmpn3frLftGbDQ2Msugc /TQ6EnFYm394PEWGQvpYBFQWRRSLWyURCkKXVAO7JHHZ41woZ8RR8eV0Fwk7Y5I/tXRd TrcRYIkPV3tRzqQxso1sGK/lwB2JoYwyxZZj6Ys+f4LjCacfVfW8syFZ4j5qxZpPlRB8 P/I6xqsOz1561ERP60P7HPFk8t2yWLgmdBcYdlEepi50b7TXm8uO85RPuo3mVoy81pLG 7rwQ== X-Forwarded-Encrypted: i=1; AJvYcCX5Baiw9dZRO0KGXynOfAeAN3KoPKWgFLtVdXu2aW0Sw9mOZqyrtNAWDZHRJ7J2bXMQ7jbdrNtyaja80B4bqC9cwFhtd6QOjWt7NG89 X-Gm-Message-State: AOJu0YwS3FUK/JucrqOSH/Dg3gDqSNxqWcYlrh5o8+U6Iyarl95nBYUF qRTLfG+t6mkJgsmlU2dx9qmb7lHp1cVK5Rb7FTHXqTa2mVLSTcuy88C/zKx/ybRZ12h//vifepf b8LMk0M1VHG1NdiFmBIuBH2Mr5uXVc9Oe X-Received: by 2002:a25:e206:0:b0:de7:61db:9fa0 with SMTP id 3f1490d57ef6-debb9d036e2mr6003130276.22.1715265564433; Thu, 09 May 2024 07:39:24 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Muni Sekhar Date: Thu, 9 May 2024 20:09:13 +0530 Message-ID: Subject: Seeking Assistance with Spin Lock Usage and Resolving Hard LOCKUP Error To: kernelnewbies , LKML Content-Type: text/plain; charset="UTF-8" Dear Linux Kernel Community, I am reaching out to seek assistance regarding the usage of spin locks in the Linux kernel and to address a recurring issue related to hard LOCKUP errors that I have encountered during testing. Recently, I developed a small kernel module that involves ISR handling and utilizes the spinlock_t primitive. In my module, I have employed spin locks both in process context using spin_lock() and spin_unlock() APIs, as well as in ISR context using spin_lock_irqsave() and spin_unlock_irqrestore() APIs. Here is a brief overview of how I have implemented spin locks in my module: spinlock_t my_spinlock; // Declare a spin lock // In ISR context (interrupt handler): spin_lock_irqsave(&my_spinlock, flags); // ... Critical section ... spin_unlock_irqrestore(&my_spinlock, flags); // In process context: (struct file_operations.read) spin_lock(&my_spinlock); // ... Critical section ... spin_unlock(&my_spinlock); However, during testing, I have encountered a scenario where a hard LOCKUP (NMI watchdog: Watchdog detected hard LOCKUP on cpu 2) error occurs, specifically when a process context code execution triggers the spin_lock() function and is preempted by an interrupt that enters the ISR context and encounters the spin_lock_irqsave() function. This situation leads to the CPU being stuck indefinitely. My primary concern is to understand the appropriate usage of spin locks in both process and ISR contexts to avoid such hard LOCKUP errors. I am seeking clarification on the following points: Is it safe to use spin_lock_irqsave() and spin_unlock_irqrestore() APIs in ISR context and spin_lock() and spin_unlock() APIs in process context simultaneously? In scenarios where a process context code execution is preempted by an interrupt and enters ISR context, how should spin locks be used to prevent hard LOCKUP errors? Are there any specific guidelines or best practices for using spin locks in scenarios involving both process and ISR contexts? I would greatly appreciate any insights, guidance, or suggestions from the experienced members of the Linux kernel community to help address this issue and ensure the correct and efficient usage of spin locks in my kernel module. Thank you very much for your time and assistance. -- Thanks, Sekhar