From patchwork Sat Jan 27 02:08:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Overstreet X-Patchwork-Id: 192948 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2395:b0:106:343:edcb with SMTP id gw21csp273064dyb; Fri, 26 Jan 2024 18:22:43 -0800 (PST) X-Google-Smtp-Source: AGHT+IGOeNgejnRKmO8wKRLdTbs7iASPyeJ/6yjPs+CMXeurZ+3zdxl9I2whUKuweK8WpnxDqV2F X-Received: by 2002:a17:903:230c:b0:1d7:5c44:aff2 with SMTP id d12-20020a170903230c00b001d75c44aff2mr720714plh.31.1706322163572; Fri, 26 Jan 2024 18:22:43 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706322163; cv=pass; d=google.com; s=arc-20160816; b=gApv2fYEHyway2HJe2BBC5NMVoRXeOxAHFAfU1LPNis1LcQYlis/bD1Wlv3wT32rGb z9m1nTH6TeQyYJGf/x0VDRW0EEt+Jw3p8BHfzgpQfA8254f7+/vIJa/7uWkYTZXve2H0 wl/aXYqth/LgljcSHOp+1smUW+Lz+A6vQPK6uAFAiLybf2N3qdC+hAd7HTJvnf0BPsGt W61fnF80+4pOLlWSBS9NNiWqy2ESjjaUkRYakN3IlYNHh05fcVOE+AN2CZ3td2bZqDxl ax7KYAczENsOJsJsUKfZpPzKvP15R6okZ6kT26JaOozV7v2oz7v4svMUhMJtZO/ygNW6 Iguw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=v89WId/z8nf+HOLrRAkJwMVVVvufG9aM/NTrDQ9Z3LE=; fh=nNM3CdhXCw1yYk5OhI6fdtGO03xDc+FuD93jXSGRCbc=; b=uc36Opr8yEYELp/pKfzyMnylc9idEbzZaVitFd+iAo55GlDB45HrW8u7hBwAh8cWLF R8u5hgMk1DwvS10Q9l4mkUXHx3GMMyoLEP+QmWceTaZlvz0xC83jG/epf+5sS6acQFzV iToe45lBhJsIp/WfIsMtjD85eeECW0zDVd/vd8Cj8id0tPM2rUMnKYKo6dKdF4I/nxep tCB6TX5GPRehvrKMcMgO6LxvNJLw3KuAnSbdPR44/osiNV2bfvKxg3y/VB8QuK2eUKPI m9Qku/E3AWOBUEdAzH6IEnFl69uHCUEkbnuup05MO6UlkGtLcLafwucTSArYfRypdJMD XJDA== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=aJfiHuzw; arc=pass (i=1 spf=pass spfdomain=linux.dev dkim=pass dkdomain=linux.dev dmarc=pass fromdomain=linux.dev); spf=pass (google.com: domain of linux-kernel+bounces-41010-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-41010-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id n18-20020a170902d0d200b001d7852b9acdsi1996261pln.386.2024.01.26.18.22.43 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Jan 2024 18:22:43 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-41010-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=aJfiHuzw; arc=pass (i=1 spf=pass spfdomain=linux.dev dkim=pass dkdomain=linux.dev dmarc=pass fromdomain=linux.dev); spf=pass (google.com: domain of linux-kernel+bounces-41010-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-41010-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev 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 sv.mirrors.kernel.org (Postfix) with ESMTPS id B7A4B28DC17 for ; Sat, 27 Jan 2024 02:16:48 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 63A3D200AD; Sat, 27 Jan 2024 02:08:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="aJfiHuzw" Received: from out-183.mta1.migadu.com (out-183.mta1.migadu.com [95.215.58.183]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 45ACE125B1 for ; Sat, 27 Jan 2024 02:08:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.183 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706321326; cv=none; b=IG5iWLfUS/RygSZGdk3Y3p4NDefZ/aDCGjaSstrk7EIAX5sT35JcHKRFpAUgWiXv9NG/G6dgzZdedvX960c2LdIEWEH7ZPfHvuLlTiFXIq1CaFt2G5+G3xXb1ekSgzGDGsLt1DLdapkKARn7aJdXRF1TEc1PR+z9mckR9++K7KQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706321326; c=relaxed/simple; bh=NB0BzrAcaq2ykZlOjZmEopevoBwZpfcanoIfs3aB2/c=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=elcJBoaDqX0Rvo/jmfcIpv1AQQ/V8I44EVGs9xDSnaEthzD6VaBtExNYiOSnQSRO+cIqfwOqinbh8uTyhpRu+PUlZr5+eqhPxxs7+/23LZkXxnKjSafmFG7XQZ1h+DDx8mn2uOFTJ+K0RgbiT4plOxpvvUFxy54HJ83ikWeCd2Y= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=aJfiHuzw; arc=none smtp.client-ip=95.215.58.183 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1706321321; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=v89WId/z8nf+HOLrRAkJwMVVVvufG9aM/NTrDQ9Z3LE=; b=aJfiHuzw/yGaPvnLXQoSJd1AsdnzZh26zTD0SLbAqo014SIZzF1k4FJAMYGEZ8npYeJGV1 gSfm3vA02aI82CIFXujYe7N6U7jz42etZmXog81znbsO2lnYrEbAYjhU2ywaWIWdN4Cz2q 1pvCLAjfpFMWmtDr9lqkKMO4RbJI5Xg= From: Kent Overstreet To: linux-kernel@vger.kernel.org, netdev@vger.kernel.org, linux-fsdevel@vger.kernel.org Cc: Kent Overstreet , peterz@infradead.org, boqun.feng@gmail.com, Alexander Viro , Christian Brauner , Jan Kara Subject: [PATCH 1/4] fs/pipe: Convert to lockdep_cmp_fn Date: Fri, 26 Jan 2024 21:08:28 -0500 Message-ID: <20240127020833.487907-2-kent.overstreet@linux.dev> In-Reply-To: <20240127020833.487907-1-kent.overstreet@linux.dev> References: <20240127020833.487907-1-kent.overstreet@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789208469050315895 X-GMAIL-MSGID: 1789208469050315895 *_lock_nested() is fundamentally broken; lockdep needs to check lock ordering, but we cannot device a total ordering on an unbounded number of elements with only a few subclasses. the replacement is to define lock ordering with a proper comparison function. fs/pipe.c was already doing everything correctly otherwise, nothing much changes here. Cc: Alexander Viro Cc: Christian Brauner Cc: Jan Kara Signed-off-by: Kent Overstreet Reviewed-by: Jan Kara --- fs/pipe.c | 81 +++++++++++++++++++++++++------------------------------ 1 file changed, 36 insertions(+), 45 deletions(-) diff --git a/fs/pipe.c b/fs/pipe.c index f1adbfe743d4..50c8a8596b52 100644 --- a/fs/pipe.c +++ b/fs/pipe.c @@ -76,18 +76,20 @@ static unsigned long pipe_user_pages_soft = PIPE_DEF_BUFFERS * INR_OPEN_CUR; * -- Manfred Spraul 2002-05-09 */ -static void pipe_lock_nested(struct pipe_inode_info *pipe, int subclass) +#define cmp_int(l, r) ((l > r) - (l < r)) + +#ifdef CONFIG_PROVE_LOCKING +static int pipe_lock_cmp_fn(const struct lockdep_map *a, + const struct lockdep_map *b) { - if (pipe->files) - mutex_lock_nested(&pipe->mutex, subclass); + return cmp_int((unsigned long) a, (unsigned long) b); } +#endif void pipe_lock(struct pipe_inode_info *pipe) { - /* - * pipe_lock() nests non-pipe inode locks (for writing to a file) - */ - pipe_lock_nested(pipe, I_MUTEX_PARENT); + if (pipe->files) + mutex_lock(&pipe->mutex); } EXPORT_SYMBOL(pipe_lock); @@ -98,28 +100,16 @@ void pipe_unlock(struct pipe_inode_info *pipe) } EXPORT_SYMBOL(pipe_unlock); -static inline void __pipe_lock(struct pipe_inode_info *pipe) -{ - mutex_lock_nested(&pipe->mutex, I_MUTEX_PARENT); -} - -static inline void __pipe_unlock(struct pipe_inode_info *pipe) -{ - mutex_unlock(&pipe->mutex); -} - void pipe_double_lock(struct pipe_inode_info *pipe1, struct pipe_inode_info *pipe2) { BUG_ON(pipe1 == pipe2); - if (pipe1 < pipe2) { - pipe_lock_nested(pipe1, I_MUTEX_PARENT); - pipe_lock_nested(pipe2, I_MUTEX_CHILD); - } else { - pipe_lock_nested(pipe2, I_MUTEX_PARENT); - pipe_lock_nested(pipe1, I_MUTEX_CHILD); - } + if (pipe1 > pipe2) + swap(pipe1, pipe2); + + pipe_lock(pipe1); + pipe_lock(pipe2); } static void anon_pipe_buf_release(struct pipe_inode_info *pipe, @@ -271,7 +261,7 @@ pipe_read(struct kiocb *iocb, struct iov_iter *to) return 0; ret = 0; - __pipe_lock(pipe); + mutex_lock(&pipe->mutex); /* * We only wake up writers if the pipe was full when we started @@ -368,7 +358,7 @@ pipe_read(struct kiocb *iocb, struct iov_iter *to) ret = -EAGAIN; break; } - __pipe_unlock(pipe); + mutex_unlock(&pipe->mutex); /* * We only get here if we didn't actually read anything. @@ -400,13 +390,13 @@ pipe_read(struct kiocb *iocb, struct iov_iter *to) if (wait_event_interruptible_exclusive(pipe->rd_wait, pipe_readable(pipe)) < 0) return -ERESTARTSYS; - __pipe_lock(pipe); + mutex_lock(&pipe->mutex); was_full = pipe_full(pipe->head, pipe->tail, pipe->max_usage); wake_next_reader = true; } if (pipe_empty(pipe->head, pipe->tail)) wake_next_reader = false; - __pipe_unlock(pipe); + mutex_unlock(&pipe->mutex); if (was_full) wake_up_interruptible_sync_poll(&pipe->wr_wait, EPOLLOUT | EPOLLWRNORM); @@ -462,7 +452,7 @@ pipe_write(struct kiocb *iocb, struct iov_iter *from) if (unlikely(total_len == 0)) return 0; - __pipe_lock(pipe); + mutex_lock(&pipe->mutex); if (!pipe->readers) { send_sig(SIGPIPE, current, 0); @@ -582,19 +572,19 @@ pipe_write(struct kiocb *iocb, struct iov_iter *from) * after waiting we need to re-check whether the pipe * become empty while we dropped the lock. */ - __pipe_unlock(pipe); + mutex_unlock(&pipe->mutex); if (was_empty) wake_up_interruptible_sync_poll(&pipe->rd_wait, EPOLLIN | EPOLLRDNORM); kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN); wait_event_interruptible_exclusive(pipe->wr_wait, pipe_writable(pipe)); - __pipe_lock(pipe); + mutex_lock(&pipe->mutex); was_empty = pipe_empty(pipe->head, pipe->tail); wake_next_writer = true; } out: if (pipe_full(pipe->head, pipe->tail, pipe->max_usage)) wake_next_writer = false; - __pipe_unlock(pipe); + mutex_unlock(&pipe->mutex); /* * If we do do a wakeup event, we do a 'sync' wakeup, because we @@ -629,7 +619,7 @@ static long pipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) switch (cmd) { case FIONREAD: - __pipe_lock(pipe); + mutex_lock(&pipe->mutex); count = 0; head = pipe->head; tail = pipe->tail; @@ -639,16 +629,16 @@ static long pipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) count += pipe->bufs[tail & mask].len; tail++; } - __pipe_unlock(pipe); + mutex_unlock(&pipe->mutex); return put_user(count, (int __user *)arg); #ifdef CONFIG_WATCH_QUEUE case IOC_WATCH_QUEUE_SET_SIZE: { int ret; - __pipe_lock(pipe); + mutex_lock(&pipe->mutex); ret = watch_queue_set_size(pipe, arg); - __pipe_unlock(pipe); + mutex_unlock(&pipe->mutex); return ret; } @@ -734,7 +724,7 @@ pipe_release(struct inode *inode, struct file *file) { struct pipe_inode_info *pipe = file->private_data; - __pipe_lock(pipe); + mutex_lock(&pipe->mutex); if (file->f_mode & FMODE_READ) pipe->readers--; if (file->f_mode & FMODE_WRITE) @@ -747,7 +737,7 @@ pipe_release(struct inode *inode, struct file *file) kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN); kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT); } - __pipe_unlock(pipe); + mutex_unlock(&pipe->mutex); put_pipe_info(inode, pipe); return 0; @@ -759,7 +749,7 @@ pipe_fasync(int fd, struct file *filp, int on) struct pipe_inode_info *pipe = filp->private_data; int retval = 0; - __pipe_lock(pipe); + mutex_lock(&pipe->mutex); if (filp->f_mode & FMODE_READ) retval = fasync_helper(fd, filp, on, &pipe->fasync_readers); if ((filp->f_mode & FMODE_WRITE) && retval >= 0) { @@ -768,7 +758,7 @@ pipe_fasync(int fd, struct file *filp, int on) /* this can happen only if on == T */ fasync_helper(-1, filp, 0, &pipe->fasync_readers); } - __pipe_unlock(pipe); + mutex_unlock(&pipe->mutex); return retval; } @@ -834,6 +824,7 @@ struct pipe_inode_info *alloc_pipe_info(void) pipe->nr_accounted = pipe_bufs; pipe->user = user; mutex_init(&pipe->mutex); + lock_set_cmp_fn(&pipe->mutex, pipe_lock_cmp_fn, NULL); return pipe; } @@ -1144,7 +1135,7 @@ static int fifo_open(struct inode *inode, struct file *filp) filp->private_data = pipe; /* OK, we have a pipe and it's pinned down */ - __pipe_lock(pipe); + mutex_lock(&pipe->mutex); /* We can only do regular read/write on fifos */ stream_open(inode, filp); @@ -1214,7 +1205,7 @@ static int fifo_open(struct inode *inode, struct file *filp) } /* Ok! */ - __pipe_unlock(pipe); + mutex_unlock(&pipe->mutex); return 0; err_rd: @@ -1230,7 +1221,7 @@ static int fifo_open(struct inode *inode, struct file *filp) goto err; err: - __pipe_unlock(pipe); + mutex_unlock(&pipe->mutex); put_pipe_info(inode, pipe); return ret; @@ -1411,7 +1402,7 @@ long pipe_fcntl(struct file *file, unsigned int cmd, unsigned int arg) if (!pipe) return -EBADF; - __pipe_lock(pipe); + mutex_lock(&pipe->mutex); switch (cmd) { case F_SETPIPE_SZ: @@ -1425,7 +1416,7 @@ long pipe_fcntl(struct file *file, unsigned int cmd, unsigned int arg) break; } - __pipe_unlock(pipe); + mutex_unlock(&pipe->mutex); return ret; } From patchwork Sat Jan 27 02:08:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Overstreet X-Patchwork-Id: 192949 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2395:b0:106:343:edcb with SMTP id gw21csp274170dyb; Fri, 26 Jan 2024 18:26:23 -0800 (PST) X-Google-Smtp-Source: AGHT+IFDJ5YRyTcHC0gn0qsC9wpZEJom83fmZaB/AtFHquuVNMMfDzP474tIlUtzPB/eJKTi9SPI X-Received: by 2002:aa7:c411:0:b0:55d:3a60:c0b4 with SMTP id j17-20020aa7c411000000b0055d3a60c0b4mr308966edq.42.1706322382797; Fri, 26 Jan 2024 18:26:22 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706322382; cv=pass; d=google.com; s=arc-20160816; b=P9Bkko08RFRvjhO6vc+vp4rin1yre7s10AY/u8CPW8T7NdLToceQpSPcegMdoqi7g5 rwqu6dxm6Njo87BSS1T/3HZKowTsNml6D7lheEmVz4BOL13VL/XHUZZ6PLV3shgQbai3 tOz5VEKm36vWUHlyj6hV2jpWEqRjLTDedVzc2681VwJLIelZrANYENCEz3PTMXm/kfuo JC32Y/+YO/smIvhBAhDhtynsXmEYP5TG//nGO1s3oeWD87CbFMBkPbqFUqVkRp1RGlgU 0s9J8UP4F9srb5eku00toh2ChQFl20e8j0516LkGvL8pchLMvV3EZtH6t+vuDed03beB Jf2Q== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=dhcmLJc/KPpam9Xa9GT9+fmxCdezLGqepV5bOxUHdOE=; fh=2sjOP59XGR4r99n5itLmxyAfoh46FGs/2pzyLP9EdQ4=; b=avP5HL93+cC2xdeul8LDjQlunhSbMJ62WQNQzjvw8OMmKY+iykHHmGlY5Wxf7i02Oj okX8UY039mA5I7QB+JjNMPURX0vUEyi8j4JZD1DhqyBV00ESpW/bZZ/ShV2XiIcf1FU+ YRkHU2lWFGEAM+eeJ4vTPfe+Al/gq+oSzJucivLMrxFqnNMbuUX1+H1Hd7HgtjFtxaK2 CNU9+A92tcA+J0E8VhZdRXPYKOL0C5Qi8IRxQdE9YSCXc13nYDf59J0BJTchad4MZ/Yi 8/LtrMtKIy/pnVO9lk9eFbkqV39L1zVduWZ242ljzCOxjIgmwhE4/6b0CtX/OpRAT+Rz NJqw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=K6HuP2cy; arc=pass (i=1 spf=pass spfdomain=linux.dev dkim=pass dkdomain=linux.dev dmarc=pass fromdomain=linux.dev); spf=pass (google.com: domain of linux-kernel+bounces-41011-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-41011-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id g9-20020a056402428900b0055c87f6d257si1256609edc.541.2024.01.26.18.26.22 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Jan 2024 18:26:22 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-41011-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=K6HuP2cy; arc=pass (i=1 spf=pass spfdomain=linux.dev dkim=pass dkdomain=linux.dev dmarc=pass fromdomain=linux.dev); spf=pass (google.com: domain of linux-kernel+bounces-41011-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-41011-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev 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 am.mirrors.kernel.org (Postfix) with ESMTPS id 7CDB51F2B9E7 for ; Sat, 27 Jan 2024 02:16:41 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E81EA1F94D; Sat, 27 Jan 2024 02:08:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="K6HuP2cy" Received: from out-185.mta1.migadu.com (out-185.mta1.migadu.com [95.215.58.185]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9BAEE13ADC for ; Sat, 27 Jan 2024 02:08:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.185 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706321326; cv=none; b=XcSjleWUU/si9Dyey71WGYeLKg1Oc2Q7CRAdbmR9Afv2tzpFMc4rtRQg5tYhyB/k5MKEZFuVtEeQAkhCUi4MUgGcp4S/uwinUOnNyH4o110tIKR+gD83lT5/zD+zoW1HeeYkfvROkvV/MmdPWT0OOLcqYr/2xX73LYZbeU8tKyQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706321326; c=relaxed/simple; bh=I17n2kJwfLP73JxpOT/4Z+XY1RSDVWFd3kwZEjsB2hg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=D6NLatV6e0ZAsbPYPlZRklQrRnJYRCdtcSipoeW34mpsixkPuiPTm5ypqb56t3/UGWPkvufIUHfDaCzorgx99XXBMiiubuD2KNEAo+e+eJsHI0Fvonubive2Al8/41i4XD/0uXjIQhTQUChyV/6KVb2DuUIRMrb1UAqkENW0hgQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=K6HuP2cy; arc=none smtp.client-ip=95.215.58.185 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1706321322; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=dhcmLJc/KPpam9Xa9GT9+fmxCdezLGqepV5bOxUHdOE=; b=K6HuP2cynzzRdMj+8Oi7h2/kxJYJxkq+MpSDzZ9Wlyk3AJZIDiYVsNznm9zyJjYntOEdOD WZaqIJ/l6rfJKTb14+fL+Z6YOd8/Rcj6w3hIM54Na/Ou0oOiLNjzLp+8w3nL1IDQL+tM5W roD4OH/1l/734jceNVxlAMT2r36o/iE= From: Kent Overstreet To: linux-kernel@vger.kernel.org, netdev@vger.kernel.org, linux-fsdevel@vger.kernel.org Cc: Kent Overstreet , peterz@infradead.org, boqun.feng@gmail.com, linux-block@vger.kernel.org, Jens Axboe Subject: [PATCH 2/4] pktcdvd: kill mutex_lock_nested() usage Date: Fri, 26 Jan 2024 21:08:29 -0500 Message-ID: <20240127020833.487907-3-kent.overstreet@linux.dev> In-Reply-To: <20240127020833.487907-1-kent.overstreet@linux.dev> References: <20240127020833.487907-1-kent.overstreet@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789208698821460443 X-GMAIL-MSGID: 1789208698821460443 Unecessary, we're not actually taking nested locks of the same type. Cc: linux-block@vger.kernel.org Cc: Jens Axboe Signed-off-by: Kent Overstreet --- drivers/block/pktcdvd.c | 8 ++++---- fs/pipe.c | 10 +--------- include/linux/lockdep.h | 3 +++ kernel/locking/lockdep.c | 6 ++++++ 4 files changed, 14 insertions(+), 13 deletions(-) diff --git a/drivers/block/pktcdvd.c b/drivers/block/pktcdvd.c index d56d972aadb3..2eb68a624fda 100644 --- a/drivers/block/pktcdvd.c +++ b/drivers/block/pktcdvd.c @@ -332,7 +332,7 @@ static ssize_t device_map_show(const struct class *c, const struct class_attribu { int n = 0; int idx; - mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING); + mutex_lock(&ctl_mutex); for (idx = 0; idx < MAX_WRITERS; idx++) { struct pktcdvd_device *pd = pkt_devs[idx]; if (!pd) @@ -2639,7 +2639,7 @@ static int pkt_setup_dev(dev_t dev, dev_t* pkt_dev) struct pktcdvd_device *pd; struct gendisk *disk; - mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING); + mutex_lock(&ctl_mutex); for (idx = 0; idx < MAX_WRITERS; idx++) if (!pkt_devs[idx]) @@ -2729,7 +2729,7 @@ static int pkt_remove_dev(dev_t pkt_dev) int idx; int ret = 0; - mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING); + mutex_lock(&ctl_mutex); for (idx = 0; idx < MAX_WRITERS; idx++) { pd = pkt_devs[idx]; @@ -2780,7 +2780,7 @@ static void pkt_get_status(struct pkt_ctrl_command *ctrl_cmd) { struct pktcdvd_device *pd; - mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING); + mutex_lock(&ctl_mutex); pd = pkt_find_dev_from_minor(ctrl_cmd->dev_index); if (pd) { diff --git a/fs/pipe.c b/fs/pipe.c index 50c8a8596b52..abe171566015 100644 --- a/fs/pipe.c +++ b/fs/pipe.c @@ -78,14 +78,6 @@ static unsigned long pipe_user_pages_soft = PIPE_DEF_BUFFERS * INR_OPEN_CUR; #define cmp_int(l, r) ((l > r) - (l < r)) -#ifdef CONFIG_PROVE_LOCKING -static int pipe_lock_cmp_fn(const struct lockdep_map *a, - const struct lockdep_map *b) -{ - return cmp_int((unsigned long) a, (unsigned long) b); -} -#endif - void pipe_lock(struct pipe_inode_info *pipe) { if (pipe->files) @@ -824,7 +816,7 @@ struct pipe_inode_info *alloc_pipe_info(void) pipe->nr_accounted = pipe_bufs; pipe->user = user; mutex_init(&pipe->mutex); - lock_set_cmp_fn(&pipe->mutex, pipe_lock_cmp_fn, NULL); + lock_set_cmp_fn_ptr_order(&pipe->mutex); return pipe; } diff --git a/include/linux/lockdep.h b/include/linux/lockdep.h index 08b0d1d9d78b..e0b121f96c80 100644 --- a/include/linux/lockdep.h +++ b/include/linux/lockdep.h @@ -391,6 +391,7 @@ extern int lockdep_is_held(const void *); #endif /* !LOCKDEP */ #ifdef CONFIG_PROVE_LOCKING +int lockdep_ptr_order_cmp_fn(const struct lockdep_map *, const struct lockdep_map *); void lockdep_set_lock_cmp_fn(struct lockdep_map *, lock_cmp_fn, lock_print_fn); #define lock_set_cmp_fn(lock, ...) lockdep_set_lock_cmp_fn(&(lock)->dep_map, __VA_ARGS__) @@ -398,6 +399,8 @@ void lockdep_set_lock_cmp_fn(struct lockdep_map *, lock_cmp_fn, lock_print_fn); #define lock_set_cmp_fn(lock, ...) do { } while (0) #endif +#define lock_set_cmp_fn_ptr_order(lock) lock_set_cmp_fn(lock, lockdep_ptr_order_cmp_fn); + enum xhlock_context_t { XHLOCK_HARD, XHLOCK_SOFT, diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c index 151bd3de5936..5630be7f5cb2 100644 --- a/kernel/locking/lockdep.c +++ b/kernel/locking/lockdep.c @@ -4919,6 +4919,12 @@ struct lock_class_key __lockdep_no_validate__; EXPORT_SYMBOL_GPL(__lockdep_no_validate__); #ifdef CONFIG_PROVE_LOCKING +int lockdep_ptr_order_cmp_fn(const struct lockdep_map *a, + const struct lockdep_map *b) +{ + return cmp_int((unsigned long) a, (unsigned long) b); +} + void lockdep_set_lock_cmp_fn(struct lockdep_map *lock, lock_cmp_fn cmp_fn, lock_print_fn print_fn) { From patchwork Sat Jan 27 02:08:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Overstreet X-Patchwork-Id: 192951 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2395:b0:106:343:edcb with SMTP id gw21csp276026dyb; Fri, 26 Jan 2024 18:32:31 -0800 (PST) X-Google-Smtp-Source: AGHT+IEIQm1VhSAd1qJA5LJp/4qEWCK7TfNkZ8HoJA062EYrVjUukyG4Xim7Baca0A9yQ9a4sqlZ X-Received: by 2002:a81:6f57:0:b0:5f5:9898:ca3a with SMTP id k84-20020a816f57000000b005f59898ca3amr889563ywc.87.1706322751209; Fri, 26 Jan 2024 18:32:31 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706322751; cv=pass; d=google.com; s=arc-20160816; b=cm1cTJpJX0uGsMI4vI09sY6myeC84d7pVIfVcaLImImmkDFi9lvCbAwrQjM34cUcPv Eb4iR4umhNSs/rIR4XIH13p/G6zkEU0U4LzB/Jqjtrg0sRxmeyXzL79hWgE7kyDjfZ1o GUMbLhFUT2+ByxO6CBGj8n82J+xKWYErNyDi/1c6NUuPUvNknjTdm5TlO5UJH8cvRgbN jvNlJ2tdL1xcq871G/ed+pqmYVSVMmYP59ZB+Jd/Yzh5jcKM5Jbs/PjnOt68WElp+2fj 4X82uBkhjMjzoGh8E0rmENomn6VC/IvQGAvCAr7FTTH0L07bXHqQy2YCWNTiWyyl58o3 2bDw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=n3Eqaf0JpcfE9VphQ2k45PLVkc2qGy56bLUTBAo+DzE=; fh=G5DaSdor7MropyFk0CA4E4srFTXsRWsLAIC8xcZLlO4=; b=rfcCIl5BSZJyvC+KtW6ZU0z2dIJUk7lgJ7tTetA+92WKy5c/VB7v8EB+xjhbsYmWU5 +0l8+mLM/Jw9+x1/23q2OoPp8+RuqA3BiMwUin7igCLSElfWEahJrWDGvhJjDeOkyVlg vkDbOwyiMRpRa5dEhVwE/MUTlurKc1mMcX/52NiFE+pA3GduduK1vSiI+s8z0fJp73hB HBAqeB53QoFENR55BQLL3NjBX6qk3h8gwHFfxx8xz2FgvinlIxnhjwuE3Tv19P2ACSlk KMVDzCKwMZMp9x+F4gnZoRENSB79ogxl5Ei/3wHUYdE2xyTeJxDVnK4Bc4WfWz6Y1/Au W6Mg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=vPx2lXWr; arc=pass (i=1 spf=pass spfdomain=linux.dev dkim=pass dkdomain=linux.dev dmarc=pass fromdomain=linux.dev); spf=pass (google.com: domain of linux-kernel+bounces-41012-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-41012-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id q10-20020a170902c74a00b001d6ff17e58esi2033424plq.187.2024.01.26.18.32.30 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Jan 2024 18:32:31 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-41012-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=vPx2lXWr; arc=pass (i=1 spf=pass spfdomain=linux.dev dkim=pass dkdomain=linux.dev dmarc=pass fromdomain=linux.dev); spf=pass (google.com: domain of linux-kernel+bounces-41012-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-41012-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev 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 BBAE5B26F31 for ; Sat, 27 Jan 2024 02:16:38 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D22621F945; Sat, 27 Jan 2024 02:08:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="vPx2lXWr" Received: from out-177.mta1.migadu.com (out-177.mta1.migadu.com [95.215.58.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7AF4B14274 for ; Sat, 27 Jan 2024 02:08:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706321327; cv=none; b=d8OdhFmZRnGkDVuo/l88NgOGJnb9qM9pyxuhKaMo/nBLtJEmV13Na4+PCt4MTjSOm9JXnmUAjdqglZyuKKyGCLflwrMorWXq2yNLso6I5q8rmjB8puH8ESlYMsXr6Sl0py1bbxHvJDtNF5z1KwZdPPmBwPLQ7D89URUpzHLElZI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706321327; c=relaxed/simple; bh=pn08eJXs7OnPxSN3c37dEpPPa01CX6MaTgxVEqfxj/E=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=dIOkSiLQ8hWuenpWvG3ziEkrnQSl40wndbhOBlIT8K1Hc/iFN6WscgDJjJG9E31o1+TzvWA7lD+OgDU3QrJ42+54OLiR6DDcWiZJUPbK+flbnm0F6i+WSyZdNwUWmQNHA2ow4hrNfBIZadYdrNXwcqMi50epDprgfsXan0+Ep3E= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=vPx2lXWr; arc=none smtp.client-ip=95.215.58.177 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1706321323; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=n3Eqaf0JpcfE9VphQ2k45PLVkc2qGy56bLUTBAo+DzE=; b=vPx2lXWr9tcHGNC+54266m4I5ZFB0bMBCYBcm95hK18A/i6lbfbV2osBlEQRdcUNMpVu20 6Ssh5ZGyhngGcYn72bo2I8oqgsP7m6dfu5Q4oHlUu7g87bYiWSLBO4/21mPcbIrciEuI6e dpK5TGsVN1S6s2l5Jb0EARayllJw9LY= From: Kent Overstreet To: linux-kernel@vger.kernel.org, netdev@vger.kernel.org, linux-fsdevel@vger.kernel.org Cc: Kent Overstreet , peterz@infradead.org, boqun.feng@gmail.com Subject: [PATCH 3/4] net: Convert sk->sk_peer_lock to lock_set_cmp_fn_ptr_order() Date: Fri, 26 Jan 2024 21:08:30 -0500 Message-ID: <20240127020833.487907-4-kent.overstreet@linux.dev> In-Reply-To: <20240127020833.487907-1-kent.overstreet@linux.dev> References: <20240127020833.487907-1-kent.overstreet@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789209085331228813 X-GMAIL-MSGID: 1789209085331228813 Cc: netdev@vger.kernel.org Signed-off-by: Kent Overstreet --- net/core/sock.c | 1 + net/unix/af_unix.c | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/net/core/sock.c b/net/core/sock.c index 158dbdebce6a..da7360c0f454 100644 --- a/net/core/sock.c +++ b/net/core/sock.c @@ -3474,6 +3474,7 @@ void sock_init_data_uid(struct socket *sock, struct sock *sk, kuid_t uid) sk->sk_peer_pid = NULL; sk->sk_peer_cred = NULL; spin_lock_init(&sk->sk_peer_lock); + lock_set_cmp_fn_ptr_order(&sk->sk_peer_lock); sk->sk_write_pending = 0; sk->sk_rcvlowat = 1; diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c index ac1f2bc18fc9..d013de3c5490 100644 --- a/net/unix/af_unix.c +++ b/net/unix/af_unix.c @@ -706,10 +706,10 @@ static void copy_peercred(struct sock *sk, struct sock *peersk) if (sk < peersk) { spin_lock(&sk->sk_peer_lock); - spin_lock_nested(&peersk->sk_peer_lock, SINGLE_DEPTH_NESTING); + spin_lock(&peersk->sk_peer_lock); } else { spin_lock(&peersk->sk_peer_lock); - spin_lock_nested(&sk->sk_peer_lock, SINGLE_DEPTH_NESTING); + spin_lock(&sk->sk_peer_lock); } old_pid = sk->sk_peer_pid; old_cred = sk->sk_peer_cred; From patchwork Sat Jan 27 02:08:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Overstreet X-Patchwork-Id: 192950 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2395:b0:106:343:edcb with SMTP id gw21csp274267dyb; Fri, 26 Jan 2024 18:26:40 -0800 (PST) X-Google-Smtp-Source: AGHT+IHzCwj7NlJzP+SWIIgxjjhznPNMMgSHRCSf4UT/0Vn4qwBth3RoU4+7UiI3VIAMGJaMoiCn X-Received: by 2002:a17:906:ae8e:b0:a35:44fd:c0e2 with SMTP id md14-20020a170906ae8e00b00a3544fdc0e2mr77859ejb.8.1706322400191; Fri, 26 Jan 2024 18:26:40 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706322400; cv=pass; d=google.com; s=arc-20160816; b=kwaDpNZfCC4aPYs3+BUN+fPUosGcU/z0uGKZzYCVTres5e90pFll/N9NIxg8dBW3kl Mc2wcpfMXHOBcNqC0FwUdJiYHMp3evNpY8LCCt6xujT7sUSWUssOLH/x2xXbNGTZJG4P NiA/S2VubeF9FEvTNS5SN+5lZdSdtw9MOyz6kXCK/yLRZVVymJnocTHA77U7hVTxtbo4 AsVBqqnMtDrplaX36Y5qqOgFZ4rcwoDVXgUakUhiBLltZLH6HMg7BZqgmMCIp9VnBtay 7wY3DpwuLsin11L9Mb+7rvpYHBf6xV3SSsvEFdYvzLUoR6ZI9LlIwyu7i7gDHKe2ttQY MdIg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=uLB5skfy3BEv0JbsXyQzZFD+57i+sZEpeM1MJI3SUiw=; fh=G5DaSdor7MropyFk0CA4E4srFTXsRWsLAIC8xcZLlO4=; b=ISx35ilBVVDdvRQ/VmUXJ1VyWsm+E7NnQErGzhui5BeWRb+N83BKkF4DruiDpW2fFA 0niuvR5/LTS5p3+0o4GtebISPyXgCObher3LwV3HWuQsumRcXzT2ktvKRkfX1sk7kJKS xCEtlFvoepiac4Rmtzbh7TxEawlh6yWYXx4d8ZYZWZR7b37tihK5PMD1Z7/S1V8CVSJk wtPDr0Q+YL/j1Mw98cHMURyXogJk+pAITU/kyrojGnHl5bscg2LfIGBGZzaynuQtA9EL 1L/sJjin3S5qz2JayVKdvwIRiT2dKBU3Fh4eMUBLTOPv4UbWg9QUCX27KrYItxYR0RO/ B2yg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=qJ+Rs5He; arc=pass (i=1 spf=pass spfdomain=linux.dev dkim=pass dkdomain=linux.dev dmarc=pass fromdomain=linux.dev); spf=pass (google.com: domain of linux-kernel+bounces-41013-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-41013-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id f19-20020a17090624d300b00a317e4aad11si1227203ejb.375.2024.01.26.18.26.40 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Jan 2024 18:26:40 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-41013-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=qJ+Rs5He; arc=pass (i=1 spf=pass spfdomain=linux.dev dkim=pass dkdomain=linux.dev dmarc=pass fromdomain=linux.dev); spf=pass (google.com: domain of linux-kernel+bounces-41013-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-41013-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev 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 am.mirrors.kernel.org (Postfix) with ESMTPS id 21EB31F2BD74 for ; Sat, 27 Jan 2024 02:16:56 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id EFDBA20339; Sat, 27 Jan 2024 02:08:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="qJ+Rs5He" Received: from out-187.mta1.migadu.com (out-187.mta1.migadu.com [95.215.58.187]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 53CD514AB0 for ; Sat, 27 Jan 2024 02:08:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.187 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706321328; cv=none; b=MLHCtg/PJbpai6nYelccDCmcVCxcGpU0bM/kkzL4nr2fWugqtyt6FR/kwqQQ4DZPyKr6ISacto4bDaNW0gEPb90knUDFGRfWPGfNJ8xtrGj/7KP3Ej2x4Ol8SEqwG37EfyZmXMPw9IMVBgff6OBtnXSxsOkqi4ixzrBdlrQciSE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706321328; c=relaxed/simple; bh=WEGpc0xxaxj2AY+Xn8ROwEZelU+zXk/EivX8ekFZmeA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=WM3HiLZvkWcgYXV48scmJOUGI2kjehlbu8GM/i+QfkUUOgm9+qBahJwvu22OC3d7yhRr8QEVsnwo3GfG4d0oQ1M/e4/lHI6xLYe7I6X7ooFS3EJ25v0QD0OXuIWy3waNIiHlwHo9YF8a72sGKPFXr+YTPDRZe4CPXYkV3QhigNk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=qJ+Rs5He; arc=none smtp.client-ip=95.215.58.187 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1706321324; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=uLB5skfy3BEv0JbsXyQzZFD+57i+sZEpeM1MJI3SUiw=; b=qJ+Rs5Hetw76AhWzqkBqPEZ/rwuaoaVg6aS11PxBaqriW5WPXxSENcSnZNZXM1c6eVbqmM HjfsYmTkTsHI6SIAhoLt7RahUYWkpTQ2q28spAAEQ3SMoy/8FpcbppN6VBQqYXblzzV86c YEZMI0JmVQa5R1JwUH/xLTa6AQ/1YSA= From: Kent Overstreet To: linux-kernel@vger.kernel.org, netdev@vger.kernel.org, linux-fsdevel@vger.kernel.org Cc: Kent Overstreet , peterz@infradead.org, boqun.feng@gmail.com Subject: [PATCH 4/4] af_unix: convert to lock_cmp_fn Date: Fri, 26 Jan 2024 21:08:31 -0500 Message-ID: <20240127020833.487907-5-kent.overstreet@linux.dev> In-Reply-To: <20240127020833.487907-1-kent.overstreet@linux.dev> References: <20240127020833.487907-1-kent.overstreet@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789208716809595483 X-GMAIL-MSGID: 1789208716809595483 Kill - unix_state_lock_nested - _nested usage for net->unx.table.locks[]. replace both with lock_set_cmp_fn_ptr_order(&u->lock). The lock ordering in sk_diag_dump_icons() looks suspicious; this may turn up a real issue. Cc: netdev@vger.kernel.org Signed-off-by: Kent Overstreet --- include/net/af_unix.h | 3 --- net/unix/af_unix.c | 20 ++++++++------------ net/unix/diag.c | 2 +- 3 files changed, 9 insertions(+), 16 deletions(-) diff --git a/include/net/af_unix.h b/include/net/af_unix.h index 49c4640027d8..4eff0a089640 100644 --- a/include/net/af_unix.h +++ b/include/net/af_unix.h @@ -48,9 +48,6 @@ struct scm_stat { #define unix_state_lock(s) spin_lock(&unix_sk(s)->lock) #define unix_state_unlock(s) spin_unlock(&unix_sk(s)->lock) -#define unix_state_lock_nested(s) \ - spin_lock_nested(&unix_sk(s)->lock, \ - SINGLE_DEPTH_NESTING) /* The AF_UNIX socket */ struct unix_sock { diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c index d013de3c5490..1a0d273799c1 100644 --- a/net/unix/af_unix.c +++ b/net/unix/af_unix.c @@ -170,7 +170,7 @@ static void unix_table_double_lock(struct net *net, swap(hash1, hash2); spin_lock(&net->unx.table.locks[hash1]); - spin_lock_nested(&net->unx.table.locks[hash2], SINGLE_DEPTH_NESTING); + spin_lock(&net->unx.table.locks[hash2]); } static void unix_table_double_unlock(struct net *net, @@ -997,6 +997,7 @@ static struct sock *unix_create1(struct net *net, struct socket *sock, int kern, u->path.dentry = NULL; u->path.mnt = NULL; spin_lock_init(&u->lock); + lock_set_cmp_fn_ptr_order(&u->lock); atomic_long_set(&u->inflight, 0); INIT_LIST_HEAD(&u->link); mutex_init(&u->iolock); /* single task reading lock */ @@ -1340,17 +1341,11 @@ static int unix_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len) static void unix_state_double_lock(struct sock *sk1, struct sock *sk2) { - if (unlikely(sk1 == sk2) || !sk2) { - unix_state_lock(sk1); - return; - } - if (sk1 < sk2) { + if (sk1 > sk2) + swap(sk1, sk2); + if (sk1 && sk1 != sk2) unix_state_lock(sk1); - unix_state_lock_nested(sk2); - } else { - unix_state_lock(sk2); - unix_state_lock_nested(sk1); - } + unix_state_lock(sk2); } static void unix_state_double_unlock(struct sock *sk1, struct sock *sk2) @@ -1591,7 +1586,7 @@ static int unix_stream_connect(struct socket *sock, struct sockaddr *uaddr, goto out_unlock; } - unix_state_lock_nested(sk); + unix_state_lock(sk); if (sk->sk_state != st) { unix_state_unlock(sk); @@ -3575,6 +3570,7 @@ static int __net_init unix_net_init(struct net *net) for (i = 0; i < UNIX_HASH_SIZE; i++) { spin_lock_init(&net->unx.table.locks[i]); + lock_set_cmp_fn_ptr_order(&net->unx.table.locks[i]); INIT_HLIST_HEAD(&net->unx.table.buckets[i]); } diff --git a/net/unix/diag.c b/net/unix/diag.c index bec09a3a1d44..8ab5e2217e4c 100644 --- a/net/unix/diag.c +++ b/net/unix/diag.c @@ -84,7 +84,7 @@ static int sk_diag_dump_icons(struct sock *sk, struct sk_buff *nlskb) * queue lock. With the other's queue locked it's * OK to lock the state. */ - unix_state_lock_nested(req); + unix_state_lock(req); peer = unix_sk(req)->peer; buf[i++] = (peer ? sock_i_ino(peer) : 0); unix_state_unlock(req);