Received: by 2002:a05:7412:9c07:b0:fa:6e18:a558 with SMTP id lr7csp50184rdb; Fri, 26 Jan 2024 18:27:03 -0800 (PST) X-Google-Smtp-Source: AGHT+IGPUU3ijG2iwufJ9CU9K+FGhH3/upHjcEeXjeCAvypIUpwVlgyR0rbBPx6kQwyXHGfDnNXC X-Received: by 2002:a19:914a:0:b0:50f:ffc5:5cda with SMTP id y10-20020a19914a000000b0050fffc55cdamr904286lfj.57.1706322423179; Fri, 26 Jan 2024 18:27:03 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706322423; cv=pass; d=google.com; s=arc-20160816; b=x/gzQ6dTtgn7MXxGa92gM1LNu3MWwVwmNeP9ipXZwmQpb4ZFdKWh138GhGMZlM6+2D VHrYcY03zskUjHAen2XkXg0xr6UA5F2iJaPSkeIwDaBvDMqhB0ralJM3uZajhv8weIY6 Jq0+gg0KrJAPLbMm+Th70ip/0ul3C18wEr8Sf5sPJorwXAQNGy+k5yV/HR75q+yM3ncB ihFO368pxBoWjiu17AKZsRsJ5+c2lOdZZAQMk/P9OPmWW6xQbstWqXq/Mm1zOF3O7EvH 0fU+z0Gxpmhp2QHF8OeuNhJZbWZwEL2psXippr7l7HxNph5irgJTnyXywtEzwQLO0amp L3CQ== 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-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-41013-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id a12-20020a1709064a4c00b00a352afafddbsi363915ejv.124.2024.01.26.18.27.03 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Jan 2024 18:27:03 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-41013-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; 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-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-41013-linux.lists.archive=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 3310E1F2B726 for ; Sat, 27 Jan 2024 02:16:16 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1249D1E899; 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="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 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT 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); -- 2.43.0