Received: by 2002:ad5:4acb:0:0:0:0:0 with SMTP id n11csp2084816imw; Tue, 5 Jul 2022 22:59:51 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vubnUCRHzUs5Pw6bsDqZtlCLvtKi+8T0EL2+HSCXJIkUH9uBWsDjGHmaOwzEreIc2QCvJC X-Received: by 2002:a17:90b:4b90:b0:1ed:64a1:9765 with SMTP id lr16-20020a17090b4b9000b001ed64a19765mr46832677pjb.121.1657087191419; Tue, 05 Jul 2022 22:59:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1657087191; cv=none; d=google.com; s=arc-20160816; b=AKdrxx9XrkA1FNBj5GFzzoll63VDWSJl83qmzZoblpfQzdvz8yV3mLVYmxWX7aB20I fUsj6cnU2J/Rpjfsye42jFZi2jkIybNpNKgeODLqGKCho25lDqcaao6cMZ1G3HkRx0Yq ebeXzcVayvEwENsX8nLGPVgstjVpc0OD0oWcc5cJD48VqaYlud6jIaqCgLQlInh5r32D NZJHZ39jbMySo06mEQ/XH43C5iuoahiamScbhxQAHZt0jWgW/KAHuHPbjMLVCbKWDdMy BVSgRhEA25YJHDFVc+PFdJ1EPlA7hV8nYxeOx2RTroqJcs8tW61r7Gjx4j4Nw30jRepD YXiQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=jh0a+absz9pAb5bIlORUQ/Q1+4cClz2C63wKgA8hG5I=; b=hA0WwQC3o5Ke8zc79xPTiV2LGYbMLyM0DtnkxcSPVOxXNAQDeIuRoM/nVnjqKWkJG9 juxaovaoy49DDwsO94Sf3L4xp21/4jkfAO8japm4oxek2nAtaGtxkdYZsuVmwiBHAd/R STlOTX03oXxqsMlMn3QChngFiTtAMneppd4uMGCRJ/BTp6LGbWaC5T0u1gEBF5SriMkU h1ShEd1GWhVKnOVcpYS7JfZkN78gCDEYouI2L9c1VMrA5RIqjhkXci7wbDeK2NzSXLXi R/CaK8DwA+CPkqAb47+fI0XqDxqcP2lR+C3gBwfpH9H8P6vH/o2ZO9PTIKTyXPSR3mmB Gz/w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=orhJLYQc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id e190-20020a6369c7000000b00411c101fb71si18207976pgc.87.2022.07.05.22.59.39; Tue, 05 Jul 2022 22:59:51 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=orhJLYQc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231368AbiGFFXj (ORCPT + 99 others); Wed, 6 Jul 2022 01:23:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46638 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231349AbiGFFX0 (ORCPT ); Wed, 6 Jul 2022 01:23:26 -0400 Received: from smtp-fw-2101.amazon.com (smtp-fw-2101.amazon.com [72.21.196.25]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B4396DE6; Tue, 5 Jul 2022 22:23:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1657085004; x=1688621004; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=jh0a+absz9pAb5bIlORUQ/Q1+4cClz2C63wKgA8hG5I=; b=orhJLYQcJiu/kREOpdVNW9a8I2XTwrjwMVg7EZmQu+6wdjdUfAtKcIdF bK12O0bJBf47CLArOqMGig8yFS1mBNeqElC9ryjy7cRd5ta+LaqKiLjrL bPRc4/qvbqjP6H/RN8Z8hQC+EsIz2FI1VPB0Fgs3uq4vcE27KXFMO2fvE Y=; X-IronPort-AV: E=Sophos;i="5.92,248,1650931200"; d="scan'208";a="215167206" Received: from iad12-co-svc-p1-lb1-vlan2.amazon.com (HELO email-inbound-relay-pdx-2c-90419278.us-west-2.amazon.com) ([10.43.8.2]) by smtp-border-fw-2101.iad2.amazon.com with ESMTP; 06 Jul 2022 05:23:11 +0000 Received: from EX13MTAUWB001.ant.amazon.com (pdx1-ws-svc-p6-lb9-vlan2.pdx.amazon.com [10.236.137.194]) by email-inbound-relay-pdx-2c-90419278.us-west-2.amazon.com (Postfix) with ESMTPS id 4BA3644103; Wed, 6 Jul 2022 05:23:11 +0000 (UTC) Received: from EX19D004ANA001.ant.amazon.com (10.37.240.138) by EX13MTAUWB001.ant.amazon.com (10.43.161.207) with Microsoft SMTP Server (TLS) id 15.0.1497.36; Wed, 6 Jul 2022 05:23:10 +0000 Received: from 88665a182662.ant.amazon.com (10.43.162.55) by EX19D004ANA001.ant.amazon.com (10.37.240.138) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1118.9; Wed, 6 Jul 2022 05:23:08 +0000 From: Kuniyuki Iwashima To: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Luis Chamberlain , Kees Cook , Iurii Zaikin CC: Kuniyuki Iwashima , Kuniyuki Iwashima , , Subject: [PATCH v1 net 06/16] sysctl: Add proc_douintvec_minmax_lockless(). Date: Tue, 5 Jul 2022 22:21:20 -0700 Message-ID: <20220706052130.16368-7-kuniyu@amazon.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220706052130.16368-1-kuniyu@amazon.com> References: <20220706052130.16368-1-kuniyu@amazon.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-Originating-IP: [10.43.162.55] X-ClientProxiedBy: EX13D18UWA004.ant.amazon.com (10.43.160.45) To EX19D004ANA001.ant.amazon.com (10.37.240.138) X-Spam-Status: No, score=-4.8 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_MED,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org A sysctl variable is accessed concurrently, and there is always a chance of data-race. So, all readers and writers need some basic protection to avoid load/store-tearing. This patch changes proc_douintvec_minmax() to use READ_ONCE()/WRITE_ONCE() internally to fix a data-race on the sysctl side. For now, proc_douintvec_minmax() itself is tolerant to a data-race, but we still need to add annotations on the other subsystem's side. In case we miss such fixes, this patch converts proc_douintvec_minmax() to a wrapper of proc_douintvec_minmax_lockless(). When we fix a data-race in the other subsystem, we can explicitly set it as a handler. Also, this patch removes proc_douintvec_minmax()'s document and adds proc_douintvec_minmax_lockless()'s one so that no one will use proc_douintvec_minmax() anymore. Fixes: 61d9b56a8920 ("sysctl: add unsigned int range support") Signed-off-by: Kuniyuki Iwashima --- CC: Luis R. Rodriguez --- include/linux/sysctl.h | 1 + kernel/sysctl.c | 17 +++++++++++++---- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/include/linux/sysctl.h b/include/linux/sysctl.h index 7f91cc625d56..830d1a8f21d4 100644 --- a/include/linux/sysctl.h +++ b/include/linux/sysctl.h @@ -87,6 +87,7 @@ PROC_HANDLER(proc_dobool_lockless); PROC_HANDLER(proc_dointvec_lockless); PROC_HANDLER(proc_douintvec_lockless); PROC_HANDLER(proc_dointvec_minmax_lockless); +PROC_HANDLER(proc_douintvec_minmax_lockless); /* * Register a set of sysctl names by calling register_sysctl_table diff --git a/kernel/sysctl.c b/kernel/sysctl.c index aead731ae74b..8ff57b8d1212 100644 --- a/kernel/sysctl.c +++ b/kernel/sysctl.c @@ -954,14 +954,15 @@ static int do_proc_douintvec_minmax_conv(unsigned long *lvalp, (param->max && *param->max < tmp)) return -ERANGE; - *valp = tmp; + WRITE_ONCE(*valp, tmp); } return 0; } /** - * proc_douintvec_minmax - read a vector of unsigned ints with min/max values + * proc_douintvec_minmax_lockless - read/write a vector of unsigned ints + * with min/max values locklessly * @table: the sysctl table * @write: %TRUE if this is a write to the sysctl file * @buffer: the user buffer @@ -979,8 +980,8 @@ static int do_proc_douintvec_minmax_conv(unsigned long *lvalp, * * Returns 0 on success or -ERANGE on write when the range check fails. */ -int proc_douintvec_minmax(struct ctl_table *table, int write, - void *buffer, size_t *lenp, loff_t *ppos) +int proc_douintvec_minmax_lockless(struct ctl_table *table, int write, + void *buffer, size_t *lenp, loff_t *ppos) { struct do_proc_douintvec_minmax_conv_param param = { .min = (unsigned int *) table->extra1, @@ -990,6 +991,12 @@ int proc_douintvec_minmax(struct ctl_table *table, int write, do_proc_douintvec_minmax_conv, ¶m); } +int proc_douintvec_minmax(struct ctl_table *table, int write, + void *buffer, size_t *lenp, loff_t *ppos) +{ + return proc_douintvec_minmax_lockless(table, write, buffer, lenp, ppos); +} + /** * proc_dou8vec_minmax - read a vector of unsigned chars with min/max values * @table: the sysctl table @@ -1532,6 +1539,7 @@ PROC_HANDLER_ENOSYS(proc_dobool_lockless); PROC_HANDLER_ENOSYS(proc_dointvec_lockless); PROC_HANDLER_ENOSYS(proc_douintvec_lockless); PROC_HANDLER_ENOSYS(proc_dointvec_minmax_lockless); +PROC_HANDLER_ENOSYS(proc_douintvec_minmax_lockless); #endif /* CONFIG_PROC_SYSCTL */ @@ -2446,3 +2454,4 @@ EXPORT_SYMBOL(proc_dobool_lockless); EXPORT_SYMBOL(proc_dointvec_lockless); EXPORT_SYMBOL(proc_douintvec_lockless); EXPORT_SYMBOL(proc_dointvec_minmax_lockless); +EXPORT_SYMBOL_GPL(proc_douintvec_minmax_lockless); -- 2.30.2