From nobody Wed Dec 24 10:00:51 2025 Received: from out-180.mta1.migadu.com (out-180.mta1.migadu.com [95.215.58.180]) (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 C7648947E for ; Sat, 27 Jan 2024 02:01:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706320886; cv=none; b=ZlwIAv0s0IolExsI8vTEvigddFOfZjWFphJz50e40LEz9VQkC3Jh5I9fRTsevpXYfnaGUd+sFt64v5AqRirk7ahefv+5AgSr1jl/gdL9hRidPkx36Bw87viGzB3Jen6YYWRKNHTS087dmyWnhE7izpbtMAqaxkNYzUGh6/oj7Sc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706320886; c=relaxed/simple; bh=NB0BzrAcaq2ykZlOjZmEopevoBwZpfcanoIfs3aB2/c=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=d2ukqSeCmFhwCoXmvoPyAni5Z8MdPIuvacXqfk1+pdP80DpvRath4qdf9qZjBYc6l3hN5LIXiV7Hl8DcS2ael2lCLbpfsFBif7L6wtc/4qSgGaUhm/oA04fQAWIi8toK+hL4gGa27dy7Ggv8GYyxJfMjDfedWKQBhCQxpSR+Ha8= 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=IRrflCGA; arc=none smtp.client-ip=95.215.58.180 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="IRrflCGA" 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=1706320880; 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=IRrflCGAwRzIR7s1CoAZdadZOYN2Va1YUr0Gvb4s4xwTzRC8NvfuSFq4qoakppnpgpy5Tx sEvp4RfNIFm9zt2bC7Vb9WPyd9z2SsFYCQ1OHL5L6cXS7H4siqJsmlXFBAIyB1X2T0CyVJ 2R6qMpIha7uW1W5I0tISdrb6xWAOunY= From: Kent Overstreet To: linux-kernel@vger.kernel.org, netdev@vger.kernel.org, linux-fsdevel@vgerkernel.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:01:05 -0500 Message-ID: <20240127020111.487218-2-kent.overstreet@linux.dev> In-Reply-To: <20240127020111.487218-1-kent.overstreet@linux.dev> References: <20240127020111.487218-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 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" *_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 =3D PIPE_DEF_= BUFFERS * INR_OPEN_CUR; * -- Manfred Spraul 2002-05-09 */ =20 -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 =20 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); =20 @@ -98,28 +100,16 @@ void pipe_unlock(struct pipe_inode_info *pipe) } EXPORT_SYMBOL(pipe_unlock); =20 -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 =3D=3D pipe2); =20 - 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); } =20 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; =20 ret =3D 0; - __pipe_lock(pipe); + mutex_lock(&pipe->mutex); =20 /* * 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 =3D -EAGAIN; break; } - __pipe_unlock(pipe); + mutex_unlock(&pipe->mutex); =20 /* * 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; =20 - __pipe_lock(pipe); + mutex_lock(&pipe->mutex); was_full =3D pipe_full(pipe->head, pipe->tail, pipe->max_usage); wake_next_reader =3D true; } if (pipe_empty(pipe->head, pipe->tail)) wake_next_reader =3D false; - __pipe_unlock(pipe); + mutex_unlock(&pipe->mutex); =20 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 =3D=3D 0)) return 0; =20 - __pipe_lock(pipe); + mutex_lock(&pipe->mutex); =20 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 =3D pipe_empty(pipe->head, pipe->tail); wake_next_writer =3D true; } out: if (pipe_full(pipe->head, pipe->tail, pipe->max_usage)) wake_next_writer =3D false; - __pipe_unlock(pipe); + mutex_unlock(&pipe->mutex); =20 /* * 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) =20 switch (cmd) { case FIONREAD: - __pipe_lock(pipe); + mutex_lock(&pipe->mutex); count =3D 0; head =3D pipe->head; tail =3D pipe->tail; @@ -639,16 +629,16 @@ static long pipe_ioctl(struct file *filp, unsigned in= t cmd, unsigned long arg) count +=3D pipe->bufs[tail & mask].len; tail++; } - __pipe_unlock(pipe); + mutex_unlock(&pipe->mutex); =20 return put_user(count, (int __user *)arg); =20 #ifdef CONFIG_WATCH_QUEUE case IOC_WATCH_QUEUE_SET_SIZE: { int ret; - __pipe_lock(pipe); + mutex_lock(&pipe->mutex); ret =3D watch_queue_set_size(pipe, arg); - __pipe_unlock(pipe); + mutex_unlock(&pipe->mutex); return ret; } =20 @@ -734,7 +724,7 @@ pipe_release(struct inode *inode, struct file *file) { struct pipe_inode_info *pipe =3D file->private_data; =20 - __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); =20 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 =3D filp->private_data; int retval =3D 0; =20 - __pipe_lock(pipe); + mutex_lock(&pipe->mutex); if (filp->f_mode & FMODE_READ) retval =3D fasync_helper(fd, filp, on, &pipe->fasync_readers); if ((filp->f_mode & FMODE_WRITE) && retval >=3D 0) { @@ -768,7 +758,7 @@ pipe_fasync(int fd, struct file *filp, int on) /* this can happen only if on =3D=3D T */ fasync_helper(-1, filp, 0, &pipe->fasync_readers); } - __pipe_unlock(pipe); + mutex_unlock(&pipe->mutex); return retval; } =20 @@ -834,6 +824,7 @@ struct pipe_inode_info *alloc_pipe_info(void) pipe->nr_accounted =3D pipe_bufs; pipe->user =3D user; mutex_init(&pipe->mutex); + lock_set_cmp_fn(&pipe->mutex, pipe_lock_cmp_fn, NULL); return pipe; } =20 @@ -1144,7 +1135,7 @@ static int fifo_open(struct inode *inode, struct file= *filp) filp->private_data =3D pipe; /* OK, we have a pipe and it's pinned down */ =20 - __pipe_lock(pipe); + mutex_lock(&pipe->mutex); =20 /* 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) } =20 /* Ok! */ - __pipe_unlock(pipe); + mutex_unlock(&pipe->mutex); return 0; =20 err_rd: @@ -1230,7 +1221,7 @@ static int fifo_open(struct inode *inode, struct file= *filp) goto err; =20 err: - __pipe_unlock(pipe); + mutex_unlock(&pipe->mutex); =20 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; =20 - __pipe_lock(pipe); + mutex_lock(&pipe->mutex); =20 switch (cmd) { case F_SETPIPE_SZ: @@ -1425,7 +1416,7 @@ long pipe_fcntl(struct file *file, unsigned int cmd, = unsigned int arg) break; } =20 - __pipe_unlock(pipe); + mutex_unlock(&pipe->mutex); return ret; } =20 --=20 2.43.0 From nobody Wed Dec 24 10:00:51 2025 Received: from out-172.mta1.migadu.com (out-172.mta1.migadu.com [95.215.58.172]) (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 96B4AB66B for ; Sat, 27 Jan 2024 02:01:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706320886; cv=none; b=d3wcLpv2W3ixbCA0gwLhkUQcw6Clri+qBafqFetPC7lT0LYuCuHr4IqHQnmlfz1ujhemqiwVeCIYI7oMZ9yVSxwgfyl8dC0pW35jfUjVvSRCs0IF3i8MrDIK4UsMdaRFmCSrRpj5w/wyyPpQje2TRKrKcegw94RiNrzK1LWWmRU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706320886; c=relaxed/simple; bh=I17n2kJwfLP73JxpOT/4Z+XY1RSDVWFd3kwZEjsB2hg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=M+Cr9ORewhsSHXuENRgU3kjxLLRcqEPyWFBAB/ajI5ambAfUNafYkeuoFsHrj9L9k5LGUyxfWEBq4Uls/Y2bsv2HzWYnnV32S83xHYky+AlCvSGao3cXZtHkuL3lYUIwBLIzBJPXhVoaXt0r/pU74V2ckHujJeCBCT2c2m+cnno= 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=e4TNW933; arc=none smtp.client-ip=95.215.58.172 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="e4TNW933" 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=1706320882; 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=e4TNW933G9qVxjcf4lFEpCbSzgbDfoyYa5dvenGHsJ1R4Ve4VK4GP6sJbrTurD0dHHvbJ3 /9Yexwkkli1xL4W2Gy9bLSBa9o+ftgDxjUS8PB2K4ORVChldBDvjSZtGznD6UEgMkAPXjP NrCoucg4nB873RTeBqqvhqP5E4bLUdg= From: Kent Overstreet To: linux-kernel@vger.kernel.org, netdev@vger.kernel.org, linux-fsdevel@vgerkernel.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:01:06 -0500 Message-ID: <20240127020111.487218-3-kent.overstreet@linux.dev> In-Reply-To: <20240127020111.487218-1-kent.overstreet@linux.dev> References: <20240127020111.487218-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 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" 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, c= onst struct class_attribu { int n =3D 0; int idx; - mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING); + mutex_lock(&ctl_mutex); for (idx =3D 0; idx < MAX_WRITERS; idx++) { struct pktcdvd_device *pd =3D 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; =20 - mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING); + mutex_lock(&ctl_mutex); =20 for (idx =3D 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 =3D 0; =20 - mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING); + mutex_lock(&ctl_mutex); =20 for (idx =3D 0; idx < MAX_WRITERS; idx++) { pd =3D pkt_devs[idx]; @@ -2780,7 +2780,7 @@ static void pkt_get_status(struct pkt_ctrl_command *c= trl_cmd) { struct pktcdvd_device *pd; =20 - mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING); + mutex_lock(&ctl_mutex); =20 pd =3D 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 =3D PIPE_DEF_B= UFFERS * INR_OPEN_CUR; =20 #define cmp_int(l, r) ((l > r) - (l < r)) =20 -#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 =3D pipe_bufs; pipe->user =3D 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; } =20 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 */ =20 #ifdef CONFIG_PROVE_LOCKING +int lockdep_ptr_order_cmp_fn(const struct lockdep_map *, const struct lock= dep_map *); void lockdep_set_lock_cmp_fn(struct lockdep_map *, lock_cmp_fn, lock_print= _fn); =20 #define lock_set_cmp_fn(lock, ...) lockdep_set_lock_cmp_fn(&(lock)->dep_ma= p, __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 =20 +#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__); =20 #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) { --=20 2.43.0 From nobody Wed Dec 24 10:00:51 2025 Received: from out-172.mta1.migadu.com (out-172.mta1.migadu.com [95.215.58.172]) (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 98EBEB66C for ; Sat, 27 Jan 2024 02:01:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706320886; cv=none; b=Hdx0T3LsdcngWewZSCtr1etObgJKJFD8jvB36ReiuEtgDHFknVVnOjV6Y/oMhAookpCcRysGJU4XrAYkVuqcXGjZ4yhCTuvgw29zYhc8djyn6Gb4PpjXpaBFYvgdogNsP3LXs9U6kx+55PzNVdOISFxG2L+Srl48QFmL89JtD8E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706320886; c=relaxed/simple; bh=pn08eJXs7OnPxSN3c37dEpPPa01CX6MaTgxVEqfxj/E=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=kPol6E0KcYMMqSExB+AlN3Fh+rFj+yThAOWKHz7iMyKizYrGi9+o2dsZLt1e7oceWuET/LZAmqWSTf/RIuXRKxGkwkkHeQD2zt5VSbPQ6o1u6xexGH9+ZmDZqH2X5V2myhYDf632z5HX4U+FKUBzphnWoXoXAIleGlW08lQfXCw= 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=F4KkgIgm; arc=none smtp.client-ip=95.215.58.172 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="F4KkgIgm" 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=1706320882; 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=F4KkgIgmy9OcwE8R7133eu10UjSiwQ1vtF3lEAuLstprPg8mmQNw459i07XrtJKuoEInSd KGODWGEyybcTIMH1FA+OsjO/ieWDxnXaJhcyoe+XjVAVJ0I3CNUQzvkdYv50vlcGnx4t4B m87eQSBMwzmxcmk73I7VnSpOFATmdnc= From: Kent Overstreet To: linux-kernel@vger.kernel.org, netdev@vger.kernel.org, linux-fsdevel@vgerkernel.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:01:07 -0500 Message-ID: <20240127020111.487218-4-kent.overstreet@linux.dev> In-Reply-To: <20240127020111.487218-1-kent.overstreet@linux.dev> References: <20240127020111.487218-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 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" 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 s= ock *sk, kuid_t uid) sk->sk_peer_pid =3D NULL; sk->sk_peer_cred =3D NULL; spin_lock_init(&sk->sk_peer_lock); + lock_set_cmp_fn_ptr_order(&sk->sk_peer_lock); =20 sk->sk_write_pending =3D 0; sk->sk_rcvlowat =3D 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 soc= k *peersk) =20 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 =3D sk->sk_peer_pid; old_cred =3D sk->sk_peer_cred; --=20 2.43.0 From nobody Wed Dec 24 10:00:51 2025 Received: from out-178.mta1.migadu.com (out-178.mta1.migadu.com [95.215.58.178]) (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 794FABA45 for ; Sat, 27 Jan 2024 02:01:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706320887; cv=none; b=FP9ZkjyCNN5tojYs6TP1oqvOvMVgB9jn9vsdRI6/zNBjcAYeCcB8IKwL6AhA5PcPUcyO91LOfuA+xZzrR7/4i2JGGwnTrKKYIPCoIRArsI7PfkyvwV9lTOmb1URz+6Z5rIjZ43wDGU6AdVTSYpwf1daCyZMXiZMImSuemIA2izU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706320887; c=relaxed/simple; bh=WEGpc0xxaxj2AY+Xn8ROwEZelU+zXk/EivX8ekFZmeA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=O7h7d7wuo2dlOMnukS5b+eCSV5YDbmJsRGQneniTBkHw9LnJv04VFDPdzAkA9G8t5iMwpi3H+NkKg8bQXPY3Sk7utp5lDM6T7+/VBOokQIQao03h0gGeqw9ugyIRbqcMUelXAeYx8SaznBozT1JjXlue4+gY/TH+KFSJuyrb4EE= 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=cJsdlIWY; arc=none smtp.client-ip=95.215.58.178 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="cJsdlIWY" 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=1706320883; 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=cJsdlIWYIbH3uvx9fpJeoELDRif4ZO6HQhkaqt/fjfF51RPc2TxcvlavZ2oegJqg0odAmV PB5Gpc3EYMzJCvBjElu/YDmgzpSL/D4M7eNj1ZL+ocQSL2k5AZf4AWyjdDoShDumlUzx5J TRyWFsQ76zG0xGqPFwUYks0nwemHdTk= From: Kent Overstreet To: linux-kernel@vger.kernel.org, netdev@vger.kernel.org, linux-fsdevel@vgerkernel.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:01:08 -0500 Message-ID: <20240127020111.487218-5-kent.overstreet@linux.dev> In-Reply-To: <20240127020111.487218-1-kent.overstreet@linux.dev> References: <20240127020111.487218-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 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" 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 { =20 #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) =20 /* 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); =20 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]); } =20 static void unix_table_double_unlock(struct net *net, @@ -997,6 +997,7 @@ static struct sock *unix_create1(struct net *net, struc= t socket *sock, int kern, u->path.dentry =3D NULL; u->path.mnt =3D 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 so= ckaddr *uaddr, int addr_len) =20 static void unix_state_double_lock(struct sock *sk1, struct sock *sk2) { - if (unlikely(sk1 =3D=3D sk2) || !sk2) { - unix_state_lock(sk1); - return; - } - if (sk1 < sk2) { + if (sk1 > sk2) + swap(sk1, sk2); + if (sk1 && sk1 !=3D sk2) unix_state_lock(sk1); - unix_state_lock_nested(sk2); - } else { - unix_state_lock(sk2); - unix_state_lock_nested(sk1); - } + unix_state_lock(sk2); } =20 static void unix_state_double_unlock(struct sock *sk1, struct sock *sk2) @@ -1591,7 +1586,7 @@ static int unix_stream_connect(struct socket *sock, s= truct sockaddr *uaddr, goto out_unlock; } =20 - unix_state_lock_nested(sk); + unix_state_lock(sk); =20 if (sk->sk_state !=3D st) { unix_state_unlock(sk); @@ -3575,6 +3570,7 @@ static int __net_init unix_net_init(struct net *net) =20 for (i =3D 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]); } =20 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 =3D unix_sk(req)->peer; buf[i++] =3D (peer ? sock_i_ino(peer) : 0); unix_state_unlock(req); --=20 2.43.0