From patchwork Wed Feb 14 08:48:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Dumazet X-Patchwork-Id: 200863 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:bc8a:b0:106:860b:bbdd with SMTP id dn10csp1075307dyb; Wed, 14 Feb 2024 00:49:23 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWl0MBZ2nGnov2Q04wartn7x6e00Cy1nd6HcLMfjYXZnMfIAB2ajH+wpT55gH+AQFqhrO+HLlstCQZqtpj7HAt4tvDrTg== X-Google-Smtp-Source: AGHT+IHVQPTtawusKZb+eANd+wrHCg6OZXK/+cUzIjMcWMkwD+5+MyCF5LMEYS4zDnMld5RzoCTI X-Received: by 2002:a17:902:a386:b0:1d9:8ac8:d79d with SMTP id x6-20020a170902a38600b001d98ac8d79dmr1974773pla.19.1707900563535; Wed, 14 Feb 2024 00:49:23 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707900563; cv=pass; d=google.com; s=arc-20160816; b=ClgrFSIyzOb56SBr7bSqzX6/TfzHszjYtjDVV4mKa8KDfEAocfcrcVJRfy2YL9C5hJ heME6+DIgwmEzXoy/tXaBUWn/Nhx1EHO3WOsA2v6YKzuVpwc4MkiVedu8UTcUbCUkBl7 Li09YYN0Q2TRzcxNnFy5XnZcOd43YFmnH06ZC0FN3W+5/ETvQF+jpXQD3DAavC2bA8Uz agE9ix1FIP609vWxxrgsNUXob0ZrdMHWLKPj17D34eVwNR6pn51okexBCB7t9PR1u3tY iUuQGEZZ1FfB+p7eLoMNtIt3CVaZ8Joowq2MsEF3FIbQu4ZerxsBjNK98tyDdOxNuYVk 8rGQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=O/JQWuyrxTUchl8YGMppPeiqbOJguP9SyeCHygUtbeE=; fh=4aIM9gyuGKNAdQ0oO1ObjkSTN8gk8iz1ENfPpNd6VSI=; b=L3fIPxgqWEUt2FPNReiIojDiN/jAJE7xTQ974ibZKoqmQQ1gCgcke4GeR09kdED8OK OAdbghAEjnbyXzgTnFxMdWumcL71Y0p/DI12gjro43IkYjoUIQq1Td9/iNuVORv9N7x6 p67f0RtuSUFXzq4oWcv+dgW5tL9ufFHOb3GspDf9LRk0ya1Ur/P4hgo23N0gBsPckabF W7YYOU2d/EWhW4B3v83ejRFlyD1i3NTQp1pxcvZPpmqrOEqi0swIK2fEH9J3N7EES1Is /nTT+jqBABo224yP5lK1Kzcfoo9qYPEVyRW4zBi9/1GiSaGj01dOs0WNa1Bgo9XlTIf1 H2nQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=1XoI+c4M; arc=pass (i=1 spf=pass spfdomain=flex--edumazet.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-64883-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-64883-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=2; AJvYcCUdkm+r1ZNMsAZvje/2CP95FCxC0vXen7iWlDWUBc+ls2ZY5tQ4gAW5WT5A2PO79mFJbaz65/8VEYpWsS9GWhhq4gp2vQ== Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id t12-20020a170902e84c00b001d749d3953asi3454067plg.162.2024.02.14.00.49.23 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Feb 2024 00:49:23 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-64883-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=@google.com header.s=20230601 header.b=1XoI+c4M; arc=pass (i=1 spf=pass spfdomain=flex--edumazet.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-64883-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-64883-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 4DF5E28D0C7 for ; Wed, 14 Feb 2024 08:49:23 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 0864312B8A; Wed, 14 Feb 2024 08:48:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="1XoI+c4M" Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (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 8D6F27475 for ; Wed, 14 Feb 2024 08:48:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707900516; cv=none; b=emLsU0QFS2lhtMQQZQBcvpDi+dTSwA8a11nEY5A/DuYPj5xZl902hnZfKkPSCTXtP7P2sbxqc4Ia6OpKXQKsafYXM1unuOUeJCmDlnfTBtyRoupH/jkXrDwskkTG442jffxOAFc5fxu9/i6VZVgPJAxeCz4O0IPQXwKuvrYg9mI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707900516; c=relaxed/simple; bh=ZU8Yz0EXsy0oVAjGfcA1t+TFkr6hmt5ICferk0jFVtI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=NEhP6w5Olj03WOh5LNiBdHuLluLNnPWuczi+ygCUBnaRRtR7A1CfsY7J0aG9O46Acn17/sP2RgKh+9WDagcYevb2ZfEoCrULOVVOQFO3LJeMv4lwrojlAsgFdXbx8q3Fi9/tH4BkdjTm9exHxq1FbGFo/vPmrXN+YO9fz3lgGac= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--edumazet.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=1XoI+c4M; arc=none smtp.client-ip=209.85.128.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--edumazet.bounces.google.com Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6077e9dd249so27909637b3.3 for ; Wed, 14 Feb 2024 00:48:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707900513; x=1708505313; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=O/JQWuyrxTUchl8YGMppPeiqbOJguP9SyeCHygUtbeE=; b=1XoI+c4M2Z/E+v2FdMwSPv3NIFk4OKKBhU/HgFK1RE46KFKyvZK9kNeD5GhfxVDW2N 1jjFzGcQPTRL1T5kuyAtL4S6NGMADWfD/G9wF5ZwubjMXAwvHTiU5YQS35joNcjPEZSU dMYofRcv4+6ku1Y/tieEzB9WeL2SX6DO2k9BTrJv90MguJ2YB7wia3iSVl08BTZD7AHC sf7Wnlp8rpFYqtPaIw8mVGUeviaCyIlPjnkNwQFi3dr2+YSh6Y6+J9gY7RPBhZSEBVPK iZ8mp+MoN+zAA0B0Mp9BHEX/bWSzJf1XACAhbW6uRYFxLNlqDb6lXsiBWqKfLWyEiyFp Cd+w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707900513; x=1708505313; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=O/JQWuyrxTUchl8YGMppPeiqbOJguP9SyeCHygUtbeE=; b=OjpTEDX4bBH9g/JwO24AFv2PsauCGrMfUDc7aQWXlph3703qGSZjxZ6zHk0Y36hBe0 PhQV5AHW5STbYj+AGtU19htNLygR+wqAXFYkCD0pzGRfuMM5I/XVwg9bnh6C9K7T0xzK cuO4I+Yuad8Ho/8NEPTp0qpfOp7NuEYIum22ciS1gdgWeMUHmXYEIEur2IO/wxIXiEZ8 MzNVPSP0XS0P6jsWSHzv86i22oFNY6kbmXN3PubZNYu8AagNFSdDSpbrkTFF/rAv1U/f ZjxV+1NiSpJ0YQIz4l5poqTQGZCEpIb9D4Xu7EyFl6CHFQSeL/GBAUAujf9IV7iVRFck y1Rg== X-Gm-Message-State: AOJu0Yy0UDWlAW6IpUjaNPL5Ut1Kb/tYXM2j9kffWedyP7q4czcq2QT+ vcilBmYIf/hys7/tVZPflfyWJNYUEaJV5MZ24Cpu5+3wuc4ntAdZXW+0AbPTGhFwr/7+cIGgxwm kavpD9Elrww== X-Received: from edumazet1.c.googlers.com ([fda3:e722:ac3:cc00:2b:7d90:c0a8:395a]) (user=edumazet job=sendgmr) by 2002:a81:8454:0:b0:607:7dee:a7fa with SMTP id u81-20020a818454000000b006077deea7famr312009ywf.2.1707900513665; Wed, 14 Feb 2024 00:48:33 -0800 (PST) Date: Wed, 14 Feb 2024 08:48:28 +0000 In-Reply-To: <20240214084829.684541-1-edumazet@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240214084829.684541-1-edumazet@google.com> X-Mailer: git-send-email 2.43.0.687.g38aa6559b0-goog Message-ID: <20240214084829.684541-2-edumazet@google.com> Subject: [PATCH 1/2] kobject: make uevent_seqnum atomic From: Eric Dumazet To: Greg Kroah-Hartman , Christian Brauner Cc: linux-kernel , "Rafael J . Wysocki" , Andrew Morton , netdev@vger.kernel.org, "David S . Miller" , Jakub Kicinski , Paolo Abeni , Eric Dumazet , Eric Dumazet X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790863541381527369 X-GMAIL-MSGID: 1790863541381527369 We will soon no longer acquire uevent_sock_mutex for most kobject_uevent_net_broadcast() calls, and also while calling uevent_net_broadcast(). Make uevent_seqnum an atomic64_t to get its own protection. This fixes a race while reading /sys/kernel/uevent_seqnum. Signed-off-by: Eric Dumazet Cc: Greg Kroah-Hartman Cc: Christian Brauner Reviewed-by: Christian Brauner --- include/linux/kobject.h | 2 +- kernel/ksysfs.c | 2 +- lib/kobject_uevent.c | 17 +++++++++-------- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/include/linux/kobject.h b/include/linux/kobject.h index c30affcc43b444cc17cb894b83b17b52e41f8ebc..c8219505a79f98bc370e52997efc8af51833cfda 100644 --- a/include/linux/kobject.h +++ b/include/linux/kobject.h @@ -38,7 +38,7 @@ extern char uevent_helper[]; #endif /* counter to tag the uevent, read only except for the kobject core */ -extern u64 uevent_seqnum; +extern atomic64_t uevent_seqnum; /* * The actions here must match the index to the string array diff --git a/kernel/ksysfs.c b/kernel/ksysfs.c index 1d4bc493b2f4b2e94133cec75e569bef3f3ead25..32ae7fa74a9c072a44f7280b950b97d25cb07baf 100644 --- a/kernel/ksysfs.c +++ b/kernel/ksysfs.c @@ -39,7 +39,7 @@ static struct kobj_attribute _name##_attr = __ATTR_RW(_name) static ssize_t uevent_seqnum_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) { - return sysfs_emit(buf, "%llu\n", (unsigned long long)uevent_seqnum); + return sysfs_emit(buf, "%llu\n", (u64)atomic64_read(&uevent_seqnum)); } KERNEL_ATTR_RO(uevent_seqnum); diff --git a/lib/kobject_uevent.c b/lib/kobject_uevent.c index fb9a2f06dd1e79db0e5db17362c88152790e2b36..9cb1a7fdaeba4fc5c698fbe84f359fb305345be1 100644 --- a/lib/kobject_uevent.c +++ b/lib/kobject_uevent.c @@ -30,7 +30,7 @@ #include -u64 uevent_seqnum; +atomic64_t uevent_seqnum; #ifdef CONFIG_UEVENT_HELPER char uevent_helper[UEVENT_HELPER_PATH_LEN] = CONFIG_UEVENT_HELPER_PATH; #endif @@ -44,7 +44,7 @@ struct uevent_sock { static LIST_HEAD(uevent_sock_list); #endif -/* This lock protects uevent_seqnum and uevent_sock_list */ +/* This lock protects uevent_sock_list */ static DEFINE_MUTEX(uevent_sock_mutex); /* the strings here must match the enum in include/linux/kobject.h */ @@ -583,13 +583,13 @@ int kobject_uevent_env(struct kobject *kobj, enum kobject_action action, break; } - mutex_lock(&uevent_sock_mutex); /* we will send an event, so request a new sequence number */ - retval = add_uevent_var(env, "SEQNUM=%llu", ++uevent_seqnum); - if (retval) { - mutex_unlock(&uevent_sock_mutex); + retval = add_uevent_var(env, "SEQNUM=%llu", + atomic64_inc_return(&uevent_seqnum)); + if (retval) goto exit; - } + + mutex_lock(&uevent_sock_mutex); retval = kobject_uevent_net_broadcast(kobj, env, action_string, devpath); mutex_unlock(&uevent_sock_mutex); @@ -688,7 +688,8 @@ static int uevent_net_broadcast(struct sock *usk, struct sk_buff *skb, int ret; /* bump and prepare sequence number */ - ret = snprintf(buf, sizeof(buf), "SEQNUM=%llu", ++uevent_seqnum); + ret = snprintf(buf, sizeof(buf), "SEQNUM=%llu", + atomic64_inc_return(&uevent_seqnum)); if (ret < 0 || (size_t)ret >= sizeof(buf)) return -ENOMEM; ret++; From patchwork Wed Feb 14 08:48:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Dumazet X-Patchwork-Id: 200864 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:bc8a:b0:106:860b:bbdd with SMTP id dn10csp1075579dyb; Wed, 14 Feb 2024 00:50:08 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWmeJMK0ioU4jlE/BxFypGfBdbUVoczPaIZR+NAYnOol4ydV1V75WxmVd7ss34adJWnQhQ/g+PfsalM1GZ+KdIZxCto2A== X-Google-Smtp-Source: AGHT+IHgF6I8Dg9+J8zOo6NimOIhHo+2ifZe/dtEPtWFzsUhH9iW8U0tS/mOZUb45LLPc5Cpucuh X-Received: by 2002:a17:902:a3c5:b0:1d9:aa2d:db6e with SMTP id q5-20020a170902a3c500b001d9aa2ddb6emr2017454plb.26.1707900608496; Wed, 14 Feb 2024 00:50:08 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707900608; cv=pass; d=google.com; s=arc-20160816; b=mCNwr/Yz9jmEo6TeqaZAVEQhW4LSZj8z09M3RVbieTDF3HZH2Fzo20pOZw369mCVSR q4Ydm5Mnlai9tRinXEm86Dkoni2MFehXxjgeKpocPEH0CoHS8DDfhHCBIOzXxs6abGBQ Bzu2qIVrJoCfyx8n7lVXweaLMzk2uaRL83/r6yZVmk94hToIjz3k38fVpoplg/9hvMWu 7Wv+vBgqpu3n0RcnNU5kMk1qwozKoXxZyHmk8Gi96rGsaMi9J90w2sMHhnRDqj1pulAo RVMerB/hvhIrufpRzoJ8mBjlDFYoU99eaIrB2S2b+z+AgI2N5dm+MPJfPzygjGEkPcdo jhMA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=vgH6jFYJlZcCO1muBwFlEgXGTqXSIAVOsixhgGigIv4=; fh=XBvEgH918qpnTG3S1DHRQhYd8mxvgeMFgGMmDpZKuIU=; b=zVHqzIXjDS2qZuJxcx1juq/Mw2xyvNBZ2bBGlMI9aLswrLFmJi3EwKQuQ/wuQOIJJi 0HUlFVTsbTMpnQgCS6qtf1yLe17G4Tdp8Bi0465hM0v05lGfSkml3IlTDPsRZX5hJDoe InH4bXGbgHyGbCGBi1BFhgDv1jIZeq4hIZ6HrLVzp4ipdcd9G33O1EgITzXH12vIba9q u2QmmyYSA7ENygJ25hp30kp3jTU1miu7CaIT7g+g+ytI6x2rmdcwHNdLt4wVtCp6NcIV WC/bdCRcEFzsoo8kYo9qTmxUbK5rf5yV0T4xPuClaGtOezGA1PQLqfTsXlrpycPi9Tgb jCwQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Z0eRummk; arc=pass (i=1 spf=pass spfdomain=flex--edumazet.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-64884-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-64884-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=2; AJvYcCV5GNuIy2Y8lulAgO7+9G8sVvfnq5axNHnKVP5EskAHLfxdSeL4Vc1U+Zp9JRxapRYC97znToq5i/DhbVKilAOemhmbaw== Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id z16-20020a170903019000b001d8f10a6a0asi3560238plg.353.2024.02.14.00.50.08 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Feb 2024 00:50:08 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-64884-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Z0eRummk; arc=pass (i=1 spf=pass spfdomain=flex--edumazet.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-64884-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-64884-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 8523428249A for ; Wed, 14 Feb 2024 08:49:32 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 81FE212E6C; Wed, 14 Feb 2024 08:48:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Z0eRummk" Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (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 46A8BB676 for ; Wed, 14 Feb 2024 08:48:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707900517; cv=none; b=fBkxAd5qOB1zO1e8TIotPg3hqafNkoBFlc/Yw3vUCglPY6i32PgioY0a9NYV6N7kwvucwJ0w0wkP5Eo+LpQuAxwOBMivlgFs5JUEjt0/NekWXmP5NchxcIEPlALoRC3Ne3EdjXuSsPTU01OzDdZ88xy11UonvQybcYVlVoz4EyU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707900517; c=relaxed/simple; bh=qi5Bs7Cf2a/XS99tp48PJPpELPUcFcpZoHc9zokTEC4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=UhPbeYRfc6JnZ12pGjsMtz0g0MZ4HqkDtUMsjQOfO2LnR76F9/E/BW9NXYKtA0QaTQwBdWhHUJaKt/B0dsy0FJHZMeDMPH1eiP91WQ34ZhWpH8rFI1OYi995qQYlesjiNxYKazWJbyZR2da0eyPJYYkUxz2p61Vh5T+5vqNZIXQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--edumazet.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Z0eRummk; arc=none smtp.client-ip=209.85.219.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--edumazet.bounces.google.com Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-dc64f63d768so9763821276.2 for ; Wed, 14 Feb 2024 00:48:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707900515; x=1708505315; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=vgH6jFYJlZcCO1muBwFlEgXGTqXSIAVOsixhgGigIv4=; b=Z0eRummkMIUk2E9fnjJZQdU8B79u2qa/JnVHPOhX4Qehn04035XMfLPxHE0hQgeGzq tTQi0UZfPFR9+kEM70ivoty+G6oh6RG1iGTzIxXOd1IPe8/KjBnvcVrSaGtNbChUr/FE 90Iud7KZX586YdIUFMmOPrv3e8NNYf26LnXBmPyGm/Lksu09nIH9JFtq2WQYlzDCAyCW 8N2x1IeU3H/igMm+hcbs02EDF314naiAo7OKSI1SV+XgUEOM1ZcCEm6hwBlUGt6Sh5UE iZB1EvB4i5H3rF/5h8y/ckxrMCO0SDVPgGfx6+04wLYlTpEIRzUkaLareE6fv6OuSoa0 Pw7w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707900515; x=1708505315; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=vgH6jFYJlZcCO1muBwFlEgXGTqXSIAVOsixhgGigIv4=; b=T+RR69J0DKxFfDRS2ClYLauK3crIVIACiNSR29o9bqnyHiwQAIuSNBxGLtBAMRyChQ RLFRdwJZX9IBCOUrHR8xXH/rFqqFvj/TvSpXPoyp2q/b8mZj+RbAteAJR9KefTlp0eok Ti5bXN7RwOlhmlV8I8QOP36hxMllUhBYc5BhO9/nGcaeAaoRNj4mOUudp4nkm3NnXu6r 82LAFQk2dnMTagDCUGCnTv1BLYQJfbkCCEHmxZPfmzasNCgK2c4LstQW+NBLlrLVgNrT 0airFi0NNWQVhlnbN9O5G8OlNBN05WakJcoCIWARjU4X75re0NUdB92CmMet4c5vweFY 5WCA== X-Gm-Message-State: AOJu0YxEdpcweONgEypYSYmN+dC8Kj/LoNLDugxmEjHFsW23qZo4Hl0m /7mLdKwC8GJ6HXuAXx0bqL7XTx2A2yXYoD0WHyOSlTbHhsHPnU+efIfE9n+l9DCXCW55f3qQgy1 5nI6B98kbkw== X-Received: from edumazet1.c.googlers.com ([fda3:e722:ac3:cc00:2b:7d90:c0a8:395a]) (user=edumazet job=sendgmr) by 2002:a05:6902:70b:b0:dc6:dfd9:d431 with SMTP id k11-20020a056902070b00b00dc6dfd9d431mr391163ybt.1.1707900515291; Wed, 14 Feb 2024 00:48:35 -0800 (PST) Date: Wed, 14 Feb 2024 08:48:29 +0000 In-Reply-To: <20240214084829.684541-1-edumazet@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240214084829.684541-1-edumazet@google.com> X-Mailer: git-send-email 2.43.0.687.g38aa6559b0-goog Message-ID: <20240214084829.684541-3-edumazet@google.com> Subject: [PATCH 2/2] kobject: reduce uevent_sock_mutex scope From: Eric Dumazet To: Greg Kroah-Hartman , Christian Brauner Cc: linux-kernel , "Rafael J . Wysocki" , Andrew Morton , netdev@vger.kernel.org, "David S . Miller" , Jakub Kicinski , Paolo Abeni , Eric Dumazet , Eric Dumazet X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790863588293539240 X-GMAIL-MSGID: 1790863588293539240 This is a followup of commit a3498436b3a0 ("netns: restrict uevents") - uevent_sock_mutex no longer protects uevent_seqnum thanks to prior patch in the series. - uevent_net_broadcast() can run without holding uevent_sock_mutex. - Instead of grabbing uevent_sock_mutex before calling kobject_uevent_net_broadcast(), we can move the mutex_lock(&uevent_sock_mutex) to the place we iterate over uevent_sock_list : uevent_net_broadcast_untagged(). After this patch, typical netdevice creations and destructions calling uevent_net_broadcast_tagged() no longer need to acquire uevent_sock_mutex. Signed-off-by: Eric Dumazet Cc: Greg Kroah-Hartman Cc: Christian Brauner Reviewed-by: Christian Brauner --- lib/kobject_uevent.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/lib/kobject_uevent.c b/lib/kobject_uevent.c index 9cb1a7fdaeba4fc5c698fbe84f359fb305345be1..03b427e2707e357ab12abeb9da234432c4bc0fb3 100644 --- a/lib/kobject_uevent.c +++ b/lib/kobject_uevent.c @@ -42,10 +42,9 @@ struct uevent_sock { #ifdef CONFIG_NET static LIST_HEAD(uevent_sock_list); -#endif - /* This lock protects uevent_sock_list */ static DEFINE_MUTEX(uevent_sock_mutex); +#endif /* the strings here must match the enum in include/linux/kobject.h */ static const char *kobject_actions[] = { @@ -315,6 +314,7 @@ static int uevent_net_broadcast_untagged(struct kobj_uevent_env *env, int retval = 0; /* send netlink message */ + mutex_lock(&uevent_sock_mutex); list_for_each_entry(ue_sk, &uevent_sock_list, list) { struct sock *uevent_sock = ue_sk->sk; @@ -334,6 +334,7 @@ static int uevent_net_broadcast_untagged(struct kobj_uevent_env *env, if (retval == -ENOBUFS || retval == -ESRCH) retval = 0; } + mutex_unlock(&uevent_sock_mutex); consume_skb(skb); return retval; @@ -589,10 +590,8 @@ int kobject_uevent_env(struct kobject *kobj, enum kobject_action action, if (retval) goto exit; - mutex_lock(&uevent_sock_mutex); retval = kobject_uevent_net_broadcast(kobj, env, action_string, devpath); - mutex_unlock(&uevent_sock_mutex); #ifdef CONFIG_UEVENT_HELPER /* call uevent_helper, usually only enabled during early boot */ @@ -743,9 +742,7 @@ static int uevent_net_rcv_skb(struct sk_buff *skb, struct nlmsghdr *nlh, return -EPERM; } - mutex_lock(&uevent_sock_mutex); ret = uevent_net_broadcast(net->uevent_sock->sk, skb, extack); - mutex_unlock(&uevent_sock_mutex); return ret; }