Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp571382imm; Fri, 15 Jun 2018 02:41:27 -0700 (PDT) X-Google-Smtp-Source: ADUXVKL2EU2WVciTnBxAH4Tbx+Irl4I3U0v/ZwZ6dtHEIv9c6mPu10uKcCZmGB2mxHj2LPPFwlVB X-Received: by 2002:a17:902:2924:: with SMTP id g33-v6mr1205735plb.26.1529055687451; Fri, 15 Jun 2018 02:41:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1529055687; cv=none; d=google.com; s=arc-20160816; b=ogJnu5P1aqMupzmYgvQVAAT+hXsGsdilMRyn+IPlcJftRHiINJZNiMSGP521l9KBcR qMYygTiLL9Gdg1rnlckZ9zUJPV4FIAHJWcLFrbeJID0hcUpsNOfTYPmKVJr0Y9+L0tE2 clWkmbjYCdCNvabmZJeT5zu7Ivdjs7m0dhlRUHPtHk6lLXnoenFrOPX07420mkJC2T0I bIKLoquQUUT8sqeagZPnp4OjfXh3ivA1OkhMFgV69TzR/+q8sWvym5UChBzMN6sSVBIU lMxYzfG2gcT92J0V6FhWStbVM0VTacseCLXjdRBy7wNtxRfQC4eDbTFRYyN847K48eot i7vQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=t5apb6k5nSC0HUfE/+7Wt3NSt9FbkZt1amlbcSiyy6A=; b=E5k+lWERWYvSeYJbmPHdJWvvBY39eQYhGoy1gJp0Xsi/p7D8Y0bUVsFQI3oVbraxMW n2eIZR1SRPrFoZus4my7DQ6sRQj2HqHu10gf9STOL8+Aij1igevjj5qP5hoykiRvCAFf JuJlZdMd4w2jQ67D0eoPswrGwBXAf5YJ467Qg3PvSORiUeDrzpx90yqgmCXVOvCdozzh qbL15YkOnNP7upMZrbvcd/H8tUoWi3UE0dNSC8AVVZCyhcBRiRle8+luYb+fB7JoBnAG HwMtBfJHCKaRrhsqOOnEDKvHIia1zyY5UOgwpfmT4W02wsi7mTmHOazouQSIv/0ZDorT XvaQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=ocIfxGNU; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id u11-v6si6248179pgq.480.2018.06.15.02.41.12; Fri, 15 Jun 2018 02:41:27 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=ocIfxGNU; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756094AbeFOJkg (ORCPT + 99 others); Fri, 15 Jun 2018 05:40:36 -0400 Received: from mail-pg0-f65.google.com ([74.125.83.65]:38508 "EHLO mail-pg0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755727AbeFOJkc (ORCPT ); Fri, 15 Jun 2018 05:40:32 -0400 Received: by mail-pg0-f65.google.com with SMTP id c9-v6so4197646pgf.5; Fri, 15 Jun 2018 02:40:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=t5apb6k5nSC0HUfE/+7Wt3NSt9FbkZt1amlbcSiyy6A=; b=ocIfxGNU2wb6L+jU8/9EvSVKj9kmc043k02o4AORKIXdse4hINx0Cdwi96qMHJwjP6 VOV5nwNLeABticJWeKFKlAl+m3IgqkbtbAC9++0MTQHzRnHpPQ5UPa4EzL+bAdevOa24 gHtVg6msq3ElfclzwGZ3KzXoEGbi1xNfOeYYnSw5s1MmW3oKLhozgyVTj2DLmHhkOQqB pn72HmDh8h5FlUTlicjc3AmE3WihDVWJdcSIVeD99BPqUu0vpy0tx5rzVNo66mMzFXQ6 A+REGSeFuJcIYIFIhG3UxdVgxBk1DcFvLoF/APDrMXRvEAoXXxvJbi++ruQaEx39iMub CrEg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=t5apb6k5nSC0HUfE/+7Wt3NSt9FbkZt1amlbcSiyy6A=; b=PR9P0IvVpafTYEDCLLwAVWZBTsddUi+V8C2ZCRyeGQ3Wg7l3XN1joSOJEpm717BruS zM8RCz5me61JpFZH91ek5pcsEK6DAzWwZ37Yh1MrbETu7WgN82trUZYaFReQDjfZrv+j qXsssL+H3h7MBD+B+M02Y7PcDnmvdyf5XjaowlC+ltG5mr8q/n5fcLbopPJwGN1zJ/PY pFfECGGwZpL0S8UVGujz7CaQ4/CTa+jFc2hBSXqfbBbmvDNgauGSlwwUhCfB2zXnuWgg RO3pmw8x+MiwKmGLHlWOQUkWpbWpVDiNjKahAjtlai3c4KkEh+VAyEL2uYg2ZnI71YFb VTxQ== X-Gm-Message-State: APt69E3I0hffzxVCMS2W5zIvlUuaKwJZncm1EtUsvbMh0C53K2P8ZeBw W/nNOwcIKL4SdzjSEFMTsVA= X-Received: by 2002:a63:27c6:: with SMTP id n189-v6mr918816pgn.164.1529055632225; Fri, 15 Jun 2018 02:40:32 -0700 (PDT) Received: from localhost.localdomain ([175.223.48.179]) by smtp.gmail.com with ESMTPSA id p1-v6sm11628551pfh.58.2018.06.15.02.40.26 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 15 Jun 2018 02:40:31 -0700 (PDT) From: Sergey Senozhatsky X-Google-Original-From: Sergey Senozhatsky To: Petr Mladek , Steven Rostedt , Greg Kroah-Hartman , Jiri Slaby Cc: Linus Torvalds , Peter Zijlstra , Andrew Morton , Dmitry Vyukov , linux-kernel@vger.kernel.org, linux-serial@vger.kernel.org, Sergey Senozhatsky , Sergey Senozhatsky Subject: [RFC][PATCH 2/6] tty: add tty_port locking helpers Date: Fri, 15 Jun 2018 18:39:15 +0900 Message-Id: <20180615093919.559-3-sergey.senozhatsky@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180615093919.559-1-sergey.senozhatsky@gmail.com> References: <20180615093919.559-1-sergey.senozhatsky@gmail.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org TTY port spin_lock is one of the locks that can cause a printk-recursion deadlock, thus we need to lock it from printk_safe context. E.g. ioctl() tty_ioctl() spin_lock(&tty_port->lock) printk() call_console_drivers() foo_console_write() spin_lock(&uart_port->lock) tty_wakeup() spin_lock(&tty_port->lock) << deadlock This patch adds tty_port->lock helper macros which take care of printk_safe context and redirect potentially unsafe printk() calls to per-CPU buffers, which we flush later from safe a context. The helper macros will turn the above mentioned deadlock scenario into: ioctl() tty_ioctl() printk_safe_enter() spin_lock(&tty_port->lock) printk() printk_safe_log_store() irq_work_queue() spin_unlock(&tty_port->lock) printk_safe_exit() IRQ printk_safe_flush_buffer() vprintk_deferred() Signed-off-by: Sergey Senozhatsky --- include/linux/tty.h | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/include/linux/tty.h b/include/linux/tty.h index c56e3978b00f..828f91ab680b 100644 --- a/include/linux/tty.h +++ b/include/linux/tty.h @@ -252,6 +252,30 @@ struct tty_port { void *client_data; }; +#define tty_port_lock_irq(lock) \ + do { \ + printk_safe_enter_irq(); \ + spin_lock(lock); \ + } while (0) + +#define tty_port_unlock_irq(lock) \ + do { \ + spin_unlock(lock); \ + printk_safe_exit_irq(); \ + } while (0) + +#define tty_port_lock_irqsave(lock, flags) \ + do { \ + printk_safe_enter_irqsave(flags); \ + spin_lock(lock); \ + } while (0) + +#define tty_port_unlock_irqrestore(lock, flags) \ + do { \ + spin_unlock(lock); \ + printk_safe_exit_irqrestore(flags); \ + } while (0) + /* tty_port::iflags bits -- use atomic bit ops */ #define TTY_PORT_INITIALIZED 0 /* device is initialized */ #define TTY_PORT_SUSPENDED 1 /* device is suspended */ -- 2.17.1