From patchwork Thu Mar 6 11:39:22 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: K Prateek Nayak X-Patchwork-Id: 14004334 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2056.outbound.protection.outlook.com [40.107.236.56]) (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 4777220D4F6; Thu, 6 Mar 2025 11:40:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.236.56 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741261217; cv=fail; b=bAU+69GKwnz4v+I9uvsWLFuocmj5iI80eHfB8wSLDzqECTc71XGEMObzWLishCXLDFYiOYoVIgj04bdM2pWTdIafuzegBZaT3vhHJDIUFIcCwu5yB1y1l5uBFzkkOu3d7Fz1bqGYY0xecjPziiZH13RNJwFZ1gHCV8Sy+DC3r1g= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741261217; c=relaxed/simple; bh=kfuj6Tuj1un3hWjLcspO4RtSms0pB9xrMceArVLjjhY=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=JmtY+nuz0S92nwGTiocoEUjvgomNR2nULbaIilOLFNbgZgYqVGML7B95pe2rgBm/CtkaHrr48goWgeDkbUU6PJQ62lsgYis9BQRSpPzFva3mcMvVZ5QFy7AQSgX1o80TfGNrD+2OUuhqgkq26EWtnNF1JoXn3w+94Qr5Vt7XtIM= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=us2hlPIc; arc=fail smtp.client-ip=40.107.236.56 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="us2hlPIc" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=YNFjfC4ziE8ui5LOWhxAIyOiB0Xcv+dsDBC9pc8T+MWhc87V44OPO8D69vwa1fJQxDyQFnSQnYuEJRDVKo1/7JBa3sD/MjscCGkRPrPbR+JCj7Ysi8rzx3bIucVqPHXQJTpb6yjhiOZy9vZHSgiU944/UXy2++FLIn7sPRi9t1FP6pr1gYMtZ50xXBSkQNFX/Bno5BVTOSnapooTfAbt3LKD8RIDsMcQg/uHY2Ei3SnyxHubdxIXjHdIEHBRQqhXPAPrKKSmNwvivachDoFh8q7R37WHUrgXfExfZWzKeiAiHHB2GIdNv5AZdMQmPOQsrnjvuWRWy+Kf9FgUe/0ATw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=hOpN0zEc8cInHConMJIJjTGl0oV1Z8GIpP/kTvr5DRw=; b=qT9xm26XZ9rAadinxBhq0GZNDAFW12ZiDIVc2RmH1eWOBKMYhosbha+hacZEK5tJ8FFqRQhccYWvWD7tdspvjxkBBuADMlURwWJwsQtdz06JGQxCFkr8w4x+fNJz8KwHRxE+tNX+mMR7ynlC5Zgdz8PG4olZ6WcTmLminP5Z/GKWxYKuHibb1NIB5g9oiVL3PEzxeG5ARdb7vE0U0mHCUxJmDE0XR+qt6mm+j3mIpVnvErzx1+OMcLh5ElhFrbxI0Gq3lO7ERpxZZpdJchORtLJzHd5+97KttJDNrSOWGEmmMDtTOcU011OxuFvyT685mc1FPXfCbqXDw++bcyE+Jg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=linux-foundation.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=hOpN0zEc8cInHConMJIJjTGl0oV1Z8GIpP/kTvr5DRw=; b=us2hlPIcWDjpgaU8mKJHi7JjlfFgXVqZo8QOfNHQDrjZdF3cs3LjzSj14AWVCxom8Ux+dUdaZ0BAewS3i+h0fVlY25RArbMWwxeLaYzCweof/WWDYSvsnTDTQk6pauwaL8oANqQsASf2JE9BLOEixBHjO0seOHxIyN+uxg+MkN4= Received: from DS0PR17CA0017.namprd17.prod.outlook.com (2603:10b6:8:191::28) by MW6PR12MB8707.namprd12.prod.outlook.com (2603:10b6:303:241::5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8511.16; Thu, 6 Mar 2025 11:40:06 +0000 Received: from CY4PEPF0000E9DC.namprd05.prod.outlook.com (2603:10b6:8:191:cafe::5) by DS0PR17CA0017.outlook.office365.com (2603:10b6:8:191::28) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.20.8511.17 via Frontend Transport; Thu, 6 Mar 2025 11:40:06 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by CY4PEPF0000E9DC.mail.protection.outlook.com (10.167.241.75) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.8511.15 via Frontend Transport; Thu, 6 Mar 2025 11:40:06 +0000 Received: from BLRKPRNAYAK.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.39; Thu, 6 Mar 2025 05:39:59 -0600 From: K Prateek Nayak To: Linus Torvalds , Oleg Nesterov , Miklos Szeredi , Alexander Viro , Christian Brauner , "Andrew Morton" , Hugh Dickins , , , CC: Jan Kara , "Matthew Wilcox (Oracle)" , Mateusz Guzik , "Gautham R. Shenoy" , Rasmus Villemoes , , , Swapnil Sapkal , K Prateek Nayak Subject: [RFC PATCH 1/3] fs/pipe: Limit the slots in pipe_resize_ring() Date: Thu, 6 Mar 2025 11:39:22 +0000 Message-ID: <20250306113924.20004-2-kprateek.nayak@amd.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250306113924.20004-1-kprateek.nayak@amd.com> References: <20250306113924.20004-1-kprateek.nayak@amd.com> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY4PEPF0000E9DC:EE_|MW6PR12MB8707:EE_ X-MS-Office365-Filtering-Correlation-Id: a0e95e4b-797a-41b0-d0f3-08dd5ca3a449 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|36860700013|7416014|376014|82310400026|921020; X-Microsoft-Antispam-Message-Info: 0yM9AQMpbAR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IkopN5B8XvrMPvb/JU2fGk2bvCL/Wx0Ivkn+GEa0+/Hy3FgCQRLCUSXeyoNp4bDHddxtOrM1X7t3nMmCjdcp6sI8fquGt3idtUjs0RyXMkZHOGSi6hMogt/kD2Lw3+aT42ODGi7qFOPLRvC9wghAJIGFDwNhJ8LBpm3LzI7VJd75iFL6N6EV6JLwx1+j4EfJp7WQ8AtMOWrs5lt5KMczo4xSdIsLWrqSo+7+TuDtKaTPgm0kvYJNGoJwgA20EaSKvrJdjr+ei1fvIrtcKP7q+HrhKmTv2X7uZbPCRXRLCVvfnZcgGjvDg4eXawIRLYhOGt0tjWFCUXoY0AbJnURZd0JWV9A6T3clhIL0lF4EUwaxiXatOqP7QJh6wDt4jPkqd/qO04ifQpBVne5CG6w8iyqbIRrtN7m1+nk1ZV79f1DJUn5aHcogEncfVAuiM X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230040)(1800799024)(36860700013)(7416014)(376014)(82310400026)(921020);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 06 Mar 2025 11:40:06.0329 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: a0e95e4b-797a-41b0-d0f3-08dd5ca3a449 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: CY4PEPF0000E9DC.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW6PR12MB8707 Limit the number of slots in pipe_resize_ring() to the maximum value representable by pipe->{head,tail}. Values beyond the max limit can lead to incorrect pipe_occupancy() calculations where the pipe will never appear full. Since nr_slots is always a power of 2 and the maximum size of pipe_index_t is 32 bits, BIT() is sufficient to represent the maximum value possible for nr_slots. Signed-off-by: K Prateek Nayak --- fs/pipe.c | 4 ++++ 1 file changed, 4 insertions(+) base-commit: 848e076317446f9c663771ddec142d7c2eb4cb43 diff --git a/fs/pipe.c b/fs/pipe.c index e8e6698f3698..3ca3103e1de7 100644 --- a/fs/pipe.c +++ b/fs/pipe.c @@ -1272,6 +1272,10 @@ int pipe_resize_ring(struct pipe_inode_info *pipe, unsigned int nr_slots) struct pipe_buffer *bufs; unsigned int head, tail, mask, n; + /* nr_slots larger than limits of pipe->{head,tail} */ + if (unlikely(nr_slots > BIT(BITS_PER_TYPE(pipe_index_t) - 1))) + return -EINVAL; + bufs = kcalloc(nr_slots, sizeof(*bufs), GFP_KERNEL_ACCOUNT | __GFP_NOWARN); if (unlikely(!bufs)) From patchwork Thu Mar 6 11:39:23 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: K Prateek Nayak X-Patchwork-Id: 14004335 Received: from NAM04-DM6-obe.outbound.protection.outlook.com (mail-dm6nam04on2070.outbound.protection.outlook.com [40.107.102.70]) (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 CE0A920D4F6; Thu, 6 Mar 2025 11:40:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.102.70 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741261230; cv=fail; b=Gden5vRtogpBOFoXcACHSltu7+8aTmyAebKl9k6ACXpEvKhb1hp3QIXBEooKv5B8IvAoWTLNm04FJzs++4HRyHwdq9WVlCLl4OMB/76ZxU6axjgMZFAgUn2YSdiRNhjexl5o+zP0QJ2tc/QwwPezJubLwR/Xi+ILKSevFEJY05Y= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741261230; c=relaxed/simple; bh=Vw19LwwoFcvg9YQ6KNQx0U78TJUsOCxfFBkQlH9goao=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=K/Hm0cchABV5fY2RIKedct+hrG4zFNyeFszqCef5Ou73/mlhTgsY4ZjkMGmMm2KoXfJTFrDJZVBEaHoSVe7HMWPqUUWJYfa+rdI4UhcZitHw8WNPTFHdIMRpPbwFWK0gMxn2JBeMaabbVjTG5pGyRVsiHewQ2trzW4nwLvqDdSU= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=r8RrzXk1; arc=fail smtp.client-ip=40.107.102.70 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="r8RrzXk1" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=OFP5sfrZOGG4SK5k1Hk72lDx/LlbGyGHtyqwxj0eE7r/6yKyC2gqIulROF3Y0JD/WxqGF2/Y2GaYSGLsYpI26ucAFqRKzvr8xqY0ClKYp5/25igVJklDOrOiVgKNPubyRN+5pf2dnl+cpe+xx5X8/1qUR5C6zlhTAs9T4i8ddCVMzbpUJ+LDMAdCQW8mPon7ICIaphcAGGvolJtWESdVKyLUtCRxUKFxR/vtO5Ggk2JD01P6qDY9u5I8XFDNOkqpSQHagRYn5LwxTDzmePW+YxuCVEsA6O8wmutWxSEFQ7MAJw/l2/Onmvr39RXB2P0sIQCTAGGizOXDXsacIoizOg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=Qb6yW67yojK6bb6VWOs3X8iThjJoUbpShCGMeGQd/Qg=; b=KyhC6Vcc2agVsxsYbS6UKNy5huMSYzYJ5FkHDPT5JwuhQEsMVOr71Ts88+5Glbm7UV4+yHJOd9326PkJKOStRv82IngY8XITJk6OhJBX+sD3kK/7yKyG4oD11Ovsym1Oe7vhm0QKXlFt1gyIn7fik/P8mzQx2BdSc/fUCkxAXHP9jhqMIS58pTCiq5M+NU+WESygOfaPMWXbkLQsGJ5eW/wVUJJfs5KoAbcIJCyM7pXOwwa9z+k2GQQX+sqqnN2pVS4a7dneEPV5KT2dPqwZ+CkKpRDkl2KPwjyFy8m9rKux/MQCLGnQ25MU9jjSjh+326Ba4mKxj5Kw5+JFBjoukg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=linux-foundation.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=Qb6yW67yojK6bb6VWOs3X8iThjJoUbpShCGMeGQd/Qg=; b=r8RrzXk1t07TCv9KG5xroR5gzpk1YWZGb+AbEHJuhszrYnlwOHSasra+lYU36rNaJc8SA4wTSSXxYWHouUcpC35oyVoqaaVGWxb2cJzsmiMEHP2VAan2p6A+YY9ucfClWVVIvLeb//5qb7AFPKL1orZ+LD+G9fg55vyF3zXz7JM= Received: from PH1PEPF000132F0.NAMP220.PROD.OUTLOOK.COM (2603:10b6:518:1::33) by CY8PR12MB8066.namprd12.prod.outlook.com (2603:10b6:930:70::5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8511.18; Thu, 6 Mar 2025 11:40:25 +0000 Received: from CY4PEPF0000E9D6.namprd05.prod.outlook.com (2a01:111:f403:f912::5) by PH1PEPF000132F0.outlook.office365.com (2603:1036:903:47::3) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.20.8511.18 via Frontend Transport; Thu, 6 Mar 2025 11:40:25 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by CY4PEPF0000E9D6.mail.protection.outlook.com (10.167.241.69) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.8511.15 via Frontend Transport; Thu, 6 Mar 2025 11:40:24 +0000 Received: from BLRKPRNAYAK.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.39; Thu, 6 Mar 2025 05:40:18 -0600 From: K Prateek Nayak To: Linus Torvalds , Oleg Nesterov , Miklos Szeredi , Alexander Viro , Christian Brauner , "Andrew Morton" , Hugh Dickins , , , CC: Jan Kara , "Matthew Wilcox (Oracle)" , Mateusz Guzik , "Gautham R. Shenoy" , Rasmus Villemoes , , , Swapnil Sapkal , K Prateek Nayak Subject: [RFC PATCH 2/3] fs/splice: Atomically read pipe->{head,tail} in opipe_prep() Date: Thu, 6 Mar 2025 11:39:23 +0000 Message-ID: <20250306113924.20004-3-kprateek.nayak@amd.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250306113924.20004-1-kprateek.nayak@amd.com> References: <20250306113924.20004-1-kprateek.nayak@amd.com> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY4PEPF0000E9D6:EE_|CY8PR12MB8066:EE_ X-MS-Office365-Filtering-Correlation-Id: a6b719a1-e0be-4a11-f2ac-08dd5ca3af93 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|376014|7416014|82310400026|1800799024|36860700013|921020; X-Microsoft-Antispam-Message-Info: LmZ0tLhsi5yIhjYuciVW1JA6qxzTsqSiJE9RY9SbYuzrWnPCAzGD9wdkkwCQs8ztuX6LpIgZWeIGgRHLrzARu/Ua6t8La4EDth3U/gzz9n1MEcxLZVE2jKpbTvrXQpiN04tGhoiNyzVi9cxJj3o3Kn5yiWmI4dKjO5p7MLq41JzVWu71BBazcOMGC8Xw9yFq/pkA8h8wrBghheFe0lY8mI/GLur7t8h0v5UrJ/yoPQuqND35KbFbkkgtSOK0sIlLpQ+UsgVaTYOri0kKnqiLpmSw0TWfJActPoREyP1+OGvdo2HlbhuCFCf8PC8/+8Qmj09mQgtovFvaWX+VqleeZC49WUkA7JC/Jxdzs9e+eWJsDehYDGBccDrGf5RTa6IA8jc8nAX7+Wtn6KLUu2scwowXU2UniHNXleBS5NpI9Yj0KLaMSaR2KwJIi9wJCFXOx7BWNQtuJ/C+bDKslUenICbHBOiOEmnGtnhlbb37BaX3CVQ19Y5rRJjxbsjsp2Pb28rAs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L:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230040)(376014)(7416014)(82310400026)(1800799024)(36860700013)(921020);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 06 Mar 2025 11:40:24.9893 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: a6b719a1-e0be-4a11-f2ac-08dd5ca3af93 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: CY4PEPF0000E9D6.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY8PR12MB8066 opipe_prep() checks pipe_full() before taking the "pipe->mutex". Use the newly introduced "pipe->head_tail" member to read the head and the tail atomically and not miss any updates between the reads. Signed-off-by: K Prateek Nayak --- fs/splice.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/fs/splice.c b/fs/splice.c index 28cfa63aa236..e51f33aca032 100644 --- a/fs/splice.c +++ b/fs/splice.c @@ -1682,13 +1682,14 @@ static int ipipe_prep(struct pipe_inode_info *pipe, unsigned int flags) */ static int opipe_prep(struct pipe_inode_info *pipe, unsigned int flags) { + union pipe_index idx = { .head_tail = READ_ONCE(pipe->head_tail) }; int ret; /* * Check pipe occupancy without the inode lock first. This function * is speculative anyways, so missing one is ok. */ - if (!pipe_full(pipe->head, pipe->tail, pipe->max_usage)) + if (!pipe_full(idx.head, idx.tail, READ_ONCE(pipe->max_usage))) return 0; ret = 0; From patchwork Thu Mar 6 11:39:24 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: K Prateek Nayak X-Patchwork-Id: 14004346 Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2077.outbound.protection.outlook.com [40.107.94.77]) (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 23EFD20AF6C; Thu, 6 Mar 2025 11:40:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.94.77 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741261257; cv=fail; b=QwUSNjCEAqJ5I7etknrIwrN2lK4KEjPb9kMyJZTVNNveWC8dVZMCTlfE9cl/m2sFsVuG8h0h2il8uhW/8jdqjQfJyzDwkI7j/yY77bEV6BEyhYGTBcZke8fKMnc2Ny6jdapQ/z1P48de+VJIXD77VdP0FssSpa3qL2v8Zw5WhJg= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741261257; c=relaxed/simple; bh=34f3ZiI5aiOkGW3ltTWKv+ScOS+x/r5HLbnWTisKowE=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=J4XsLVGvTNN7V7l2ErwolPrrpfmCyQ6pumLJjUQ96rU8kiaQeCEiaJhJfgZUA7stjoL5EhgZMAZU0BaGNRzh4NIkk5imQLgVdp9NMGdaQXR8FVTaY2Ax3ti5nADjLvshSZUsES++5mVhvce0RjBDvaR7UOGN8QyhOUXHuhY5ulk= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=ko7NujTm; arc=fail smtp.client-ip=40.107.94.77 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="ko7NujTm" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=LPlKUVx2PqfrKDuZSeuZc07kdXCrzZgWorQUjndWeXZlj9NO2/euLdx/KUmXDh0/bE+tSRvAiFGEdWqDKo7xOH8chO+JI6/eAqz4kRhcA/jrKf7cMID8JGtezr9T5+isZhUcKM9roUe5bRXZIko1joFvU4AWcyu0l/4GU32pMz9/T7BYEP0Zo1fIPUjlL4nkej0dfSzdoNsbaoO4kAISV2UDwGMVB90LLQGP7/wj9Tez5IcPlBKIs+NVDMfOIwAqktvUtVeH7tAntTg+v5Ozh/m4E6cGddkg8uc8+VDLot99pnan/GYfM0Dlgs4YuJuuvJqjNmYNBIP9MYocntr4fA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=yWzYkgs+iTmA7ljf3vmtYL2QU3Kx0wKwUlGM4TzjI+o=; b=FI41KlssoLRAt53HHz6lAphV3UPgBX6YS28unQDwC6l5JFPP1JTLQGsmFIqU29eXWAeezEfk00Ud/Z/08HWjq/8P0RD7nDmykgUGZXMGVaU4jaf3+Lqb4S5J+46tnfJMSBThdvxWRkhmjavIcg5JnxmX4hXY0E1FTedhLXEO5R2kAXLbSArsRawpU/LDUH9ITxuVzvInyiQA8fOIWLAh2fq3hHU3dTRpcYxrF6bwLptizrjjCS5TGYCJRAduUcpQCuzyY49mi4Kge5FqKPGcYkqvWBFYv4tJrLS+P/Eh5MEey0ouCpmBIiEWrTBI187ZruoqFDM4MdrDrMEfc2LJMA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=linux-foundation.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=yWzYkgs+iTmA7ljf3vmtYL2QU3Kx0wKwUlGM4TzjI+o=; b=ko7NujTmsOe02u4tXvX60KO0azKxrGmPcoyF+EbuPmL408RwkLEvk4zvZPx9UpD8QhGAuEuxkf4XFg3lR/dCbhtEktVzD1s1A32K20dbbRUBZcfpkIzNcNfgo+A8cKRSmZlnkBGxZo4KoTTOg4lFBkq2BUY1VTWenXcJmEawvyk= Received: from DS7PR03CA0114.namprd03.prod.outlook.com (2603:10b6:5:3b7::29) by PH8PR12MB7135.namprd12.prod.outlook.com (2603:10b6:510:22c::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8489.25; Thu, 6 Mar 2025 11:40:45 +0000 Received: from CY4PEPF0000E9D8.namprd05.prod.outlook.com (2603:10b6:5:3b7:cafe::e3) by DS7PR03CA0114.outlook.office365.com (2603:10b6:5:3b7::29) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.20.8511.16 via Frontend Transport; Thu, 6 Mar 2025 11:40:44 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by CY4PEPF0000E9D8.mail.protection.outlook.com (10.167.241.71) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.8511.15 via Frontend Transport; Thu, 6 Mar 2025 11:40:44 +0000 Received: from BLRKPRNAYAK.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.39; Thu, 6 Mar 2025 05:40:37 -0600 From: K Prateek Nayak To: Linus Torvalds , Oleg Nesterov , Miklos Szeredi , Alexander Viro , Christian Brauner , "Andrew Morton" , Hugh Dickins , , , CC: Jan Kara , "Matthew Wilcox (Oracle)" , Mateusz Guzik , "Gautham R. Shenoy" , Rasmus Villemoes , , , Swapnil Sapkal , K Prateek Nayak Subject: [RFC PATCH 3/3] treewide: pipe: Convert all references to pipe->{head,tail,max_usage,ring_size} to unsigned short Date: Thu, 6 Mar 2025 11:39:24 +0000 Message-ID: <20250306113924.20004-4-kprateek.nayak@amd.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250306113924.20004-1-kprateek.nayak@amd.com> References: <20250306113924.20004-1-kprateek.nayak@amd.com> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY4PEPF0000E9D8:EE_|PH8PR12MB7135:EE_ X-MS-Office365-Filtering-Correlation-Id: e2bb570c-9619-45f3-0126-08dd5ca3baeb X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|82310400026|7416014|376014|36860700013|1800799024|921020; X-Microsoft-Antispam-Message-Info: JBfInG+4LwF+sCA8FfcKj1Ta9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XulzXM90Dt9H8U72LDkf9InVXrRI/EvrqKgc5wLs+sbl7ce+WWHRG1v/lCNIY0pyKEwM1TJe4HioK6HgBuBDVewAxLc44iWipAPCe1phhbw+Ybc4bSU09maFQMec9dIhQau5tP8L86I8v9hlitjPLZlMRmVCiTNta5dsY1+WSpAzsdp8s4DtW/zFMNJpHMOZ0XRzVkcmcpKGdFmnlzZbD9p+UrntekgIHjcvTZVIKfx3xrw+wkuKhJCame6xoQvITJkjnrblq4AsW0oz/Qr0SV+/A1safuV5DAUtnHzVmFwN629aFjaxabEYUzFsIQ5NTLrBhxRZIWYbJPvd6lsK5E4HrXArdnBH6nMNhqBV5Gx+UPewb1agRhR12ozm2/b8JP49Fkoi3KWhOFrZzV3ygrPe5uJoQAo4IIa6mmbMC2dM0XoqDW4S2a0CcCnB X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230040)(82310400026)(7416014)(376014)(36860700013)(1800799024)(921020);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 06 Mar 2025 11:40:44.0147 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: e2bb570c-9619-45f3-0126-08dd5ca3baeb X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: CY4PEPF0000E9D8.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH8PR12MB7135 Use 16-bit head and tail to track the pipe buffer production and consumption. Since "pipe->max_usage" and "pipe->ring_size" must fall between the head and the tail limits, convert them to unsigned short as well to catch any cases of unsigned arithmetic going wrong. Part of fs/fuse/dev.c, fs/splice.c, mm/filemap.c, and mm/shmem.c were touched to accommodate the "unsigned short" based calculations of pipe_occupancy(). pipe->tail is incremented always with both "pipe->mutex" and "pipe->rd_wait.lock" held for pipes with watch queue. pipe_write() exits early if pipe has a watch queue but otherwise takes the "pipe->muxtex" before updating pipe->head. post_one_notification() holds the "pipe->rd_wait.lock" when updating pipe->head. Updates to "pipe->head" and "pipe->tail" are always mutually exclusive, either guarded by "pipe->mutex" or by "pipe->rd_wait.lock". Even a RMW updates to the 16-bits fields should be safe because of those synchronization primitives on architectures that cannot do an atomic 16-bit store. Signed-off-by: K Prateek Nayak --- fs/fuse/dev.c | 7 +++--- fs/pipe.c | 31 +++++++++++++------------- fs/splice.c | 47 ++++++++++++++++++++------------------- include/linux/pipe_fs_i.h | 39 +++++++++++--------------------- kernel/watch_queue.c | 3 ++- mm/filemap.c | 5 +++-- mm/shmem.c | 5 +++-- 7 files changed, 65 insertions(+), 72 deletions(-) diff --git a/fs/fuse/dev.c b/fs/fuse/dev.c index 2b2d1b755544..993e6dc24de1 100644 --- a/fs/fuse/dev.c +++ b/fs/fuse/dev.c @@ -1440,6 +1440,7 @@ static ssize_t fuse_dev_splice_read(struct file *in, loff_t *ppos, int page_nr = 0; struct pipe_buffer *bufs; struct fuse_copy_state cs; + unsigned short free_slots; struct fuse_dev *fud = fuse_get_dev(in); if (!fud) @@ -1457,7 +1458,8 @@ static ssize_t fuse_dev_splice_read(struct file *in, loff_t *ppos, if (ret < 0) goto out; - if (pipe_occupancy(pipe->head, pipe->tail) + cs.nr_segs > pipe->max_usage) { + free_slots = pipe->max_usage - pipe_occupancy(pipe->head, pipe->tail); + if (free_slots < cs.nr_segs) { ret = -EIO; goto out; } @@ -2107,9 +2109,8 @@ static ssize_t fuse_dev_splice_write(struct pipe_inode_info *pipe, struct file *out, loff_t *ppos, size_t len, unsigned int flags) { - unsigned int head, tail, mask, count; + unsigned short head, tail, mask, count, idx; unsigned nbuf; - unsigned idx; struct pipe_buffer *bufs; struct fuse_copy_state cs; struct fuse_dev *fud; diff --git a/fs/pipe.c b/fs/pipe.c index 3ca3103e1de7..b8d87eabff79 100644 --- a/fs/pipe.c +++ b/fs/pipe.c @@ -216,9 +216,9 @@ static inline bool pipe_readable(const struct pipe_inode_info *pipe) return !pipe_empty(idx.head, idx.tail) || !writers; } -static inline unsigned int pipe_update_tail(struct pipe_inode_info *pipe, - struct pipe_buffer *buf, - unsigned int tail) +static inline unsigned short pipe_update_tail(struct pipe_inode_info *pipe, + struct pipe_buffer *buf, + unsigned short tail) { pipe_buf_release(pipe, buf); @@ -272,9 +272,9 @@ pipe_read(struct kiocb *iocb, struct iov_iter *to) */ for (;;) { /* Read ->head with a barrier vs post_one_notification() */ - unsigned int head = smp_load_acquire(&pipe->head); - unsigned int tail = pipe->tail; - unsigned int mask = pipe->ring_size - 1; + unsigned short head = smp_load_acquire(&pipe->head); + unsigned short tail = pipe->tail; + unsigned short mask = pipe->ring_size - 1; #ifdef CONFIG_WATCH_QUEUE if (pipe->note_loss) { @@ -417,7 +417,7 @@ static inline int is_packetized(struct file *file) static inline bool pipe_writable(const struct pipe_inode_info *pipe) { union pipe_index idx = { .head_tail = READ_ONCE(pipe->head_tail) }; - unsigned int max_usage = READ_ONCE(pipe->max_usage); + unsigned short max_usage = READ_ONCE(pipe->max_usage); return !pipe_full(idx.head, idx.tail, max_usage) || !READ_ONCE(pipe->readers); @@ -428,7 +428,7 @@ pipe_write(struct kiocb *iocb, struct iov_iter *from) { struct file *filp = iocb->ki_filp; struct pipe_inode_info *pipe = filp->private_data; - unsigned int head; + unsigned short head; ssize_t ret = 0; size_t total_len = iov_iter_count(from); ssize_t chars; @@ -471,7 +471,7 @@ pipe_write(struct kiocb *iocb, struct iov_iter *from) was_empty = pipe_empty(head, pipe->tail); chars = total_len & (PAGE_SIZE-1); if (chars && !was_empty) { - unsigned int mask = pipe->ring_size - 1; + unsigned short mask = pipe->ring_size - 1; struct pipe_buffer *buf = &pipe->bufs[(head - 1) & mask]; int offset = buf->offset + buf->len; @@ -614,7 +614,8 @@ pipe_write(struct kiocb *iocb, struct iov_iter *from) static long pipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) { struct pipe_inode_info *pipe = filp->private_data; - unsigned int count, head, tail, mask; + unsigned short head, tail, mask; + unsigned int count; switch (cmd) { case FIONREAD: @@ -1270,10 +1271,10 @@ unsigned int round_pipe_size(unsigned int size) int pipe_resize_ring(struct pipe_inode_info *pipe, unsigned int nr_slots) { struct pipe_buffer *bufs; - unsigned int head, tail, mask, n; + unsigned short head, tail, mask, n; /* nr_slots larger than limits of pipe->{head,tail} */ - if (unlikely(nr_slots > BIT(BITS_PER_TYPE(pipe_index_t) - 1))) + if (unlikely(nr_slots > USHRT_MAX)) return -EINVAL; bufs = kcalloc(nr_slots, sizeof(*bufs), @@ -1298,13 +1299,13 @@ int pipe_resize_ring(struct pipe_inode_info *pipe, unsigned int nr_slots) * and adjust the indices. */ if (n > 0) { - unsigned int h = head & mask; - unsigned int t = tail & mask; + unsigned short h = head & mask; + unsigned short t = tail & mask; if (h > t) { memcpy(bufs, pipe->bufs + t, n * sizeof(struct pipe_buffer)); } else { - unsigned int tsize = pipe->ring_size - t; + unsigned short tsize = pipe->ring_size - t; if (h > 0) memcpy(bufs + tsize, pipe->bufs, h * sizeof(struct pipe_buffer)); diff --git a/fs/splice.c b/fs/splice.c index e51f33aca032..891a7cf9fb55 100644 --- a/fs/splice.c +++ b/fs/splice.c @@ -198,9 +198,9 @@ ssize_t splice_to_pipe(struct pipe_inode_info *pipe, struct splice_pipe_desc *spd) { unsigned int spd_pages = spd->nr_pages; - unsigned int tail = pipe->tail; - unsigned int head = pipe->head; - unsigned int mask = pipe->ring_size - 1; + unsigned short tail = pipe->tail; + unsigned short head = pipe->head; + unsigned short mask = pipe->ring_size - 1; ssize_t ret = 0; int page_nr = 0; @@ -245,9 +245,9 @@ EXPORT_SYMBOL_GPL(splice_to_pipe); ssize_t add_to_pipe(struct pipe_inode_info *pipe, struct pipe_buffer *buf) { - unsigned int head = pipe->head; - unsigned int tail = pipe->tail; - unsigned int mask = pipe->ring_size - 1; + unsigned short head = pipe->head; + unsigned short tail = pipe->tail; + unsigned short mask = pipe->ring_size - 1; int ret; if (unlikely(!pipe->readers)) { @@ -271,7 +271,7 @@ EXPORT_SYMBOL(add_to_pipe); */ int splice_grow_spd(const struct pipe_inode_info *pipe, struct splice_pipe_desc *spd) { - unsigned int max_usage = READ_ONCE(pipe->max_usage); + unsigned short max_usage = READ_ONCE(pipe->max_usage); spd->nr_pages_max = max_usage; if (max_usage <= PIPE_DEF_BUFFERS) @@ -327,12 +327,13 @@ ssize_t copy_splice_read(struct file *in, loff_t *ppos, struct kiocb kiocb; struct page **pages; ssize_t ret; - size_t used, npages, chunk, remain, keep = 0; + size_t npages, chunk, remain, keep = 0; + unsigned short used; int i; /* Work out how much data we can actually add into the pipe */ used = pipe_occupancy(pipe->head, pipe->tail); - npages = max_t(ssize_t, pipe->max_usage - used, 0); + npages = max_t(unsigned short, pipe->max_usage - used, 0); len = min_t(size_t, len, npages * PAGE_SIZE); npages = DIV_ROUND_UP(len, PAGE_SIZE); @@ -445,9 +446,9 @@ static void wakeup_pipe_writers(struct pipe_inode_info *pipe) static int splice_from_pipe_feed(struct pipe_inode_info *pipe, struct splice_desc *sd, splice_actor *actor) { - unsigned int head = pipe->head; - unsigned int tail = pipe->tail; - unsigned int mask = pipe->ring_size - 1; + unsigned short head = pipe->head; + unsigned short tail = pipe->tail; + unsigned short mask = pipe->ring_size - 1; int ret; while (!pipe_empty(head, tail)) { @@ -494,8 +495,8 @@ static int splice_from_pipe_feed(struct pipe_inode_info *pipe, struct splice_des /* We know we have a pipe buffer, but maybe it's empty? */ static inline bool eat_empty_buffer(struct pipe_inode_info *pipe) { - unsigned int tail = pipe->tail; - unsigned int mask = pipe->ring_size - 1; + unsigned short tail = pipe->tail; + unsigned short mask = pipe->ring_size - 1; struct pipe_buffer *buf = &pipe->bufs[tail & mask]; if (unlikely(!buf->len)) { @@ -690,7 +691,7 @@ iter_file_splice_write(struct pipe_inode_info *pipe, struct file *out, while (sd.total_len) { struct kiocb kiocb; struct iov_iter from; - unsigned int head, tail, mask; + unsigned short head, tail, mask; size_t left; int n; @@ -809,7 +810,7 @@ ssize_t splice_to_socket(struct pipe_inode_info *pipe, struct file *out, pipe_lock(pipe); while (len > 0) { - unsigned int head, tail, mask, bc = 0; + unsigned short head, tail, mask, bc = 0; size_t remain = len; /* @@ -960,7 +961,7 @@ static ssize_t do_splice_read(struct file *in, loff_t *ppos, struct pipe_inode_info *pipe, size_t len, unsigned int flags) { - unsigned int p_space; + unsigned short p_space; if (unlikely(!(in->f_mode & FMODE_READ))) return -EBADF; @@ -1724,9 +1725,9 @@ static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe, size_t len, unsigned int flags) { struct pipe_buffer *ibuf, *obuf; - unsigned int i_head, o_head; - unsigned int i_tail, o_tail; - unsigned int i_mask, o_mask; + unsigned short i_head, o_head; + unsigned short i_tail, o_tail; + unsigned short i_mask, o_mask; int ret = 0; bool input_wakeup = false; @@ -1861,9 +1862,9 @@ static ssize_t link_pipe(struct pipe_inode_info *ipipe, size_t len, unsigned int flags) { struct pipe_buffer *ibuf, *obuf; - unsigned int i_head, o_head; - unsigned int i_tail, o_tail; - unsigned int i_mask, o_mask; + unsigned short i_head, o_head; + unsigned short i_tail, o_tail; + unsigned short i_mask, o_mask; ssize_t ret = 0; /* diff --git a/include/linux/pipe_fs_i.h b/include/linux/pipe_fs_i.h index e572e6fc4f81..0997c028548c 100644 --- a/include/linux/pipe_fs_i.h +++ b/include/linux/pipe_fs_i.h @@ -31,19 +31,6 @@ struct pipe_buffer { unsigned long private; }; -/* - * Really only alpha needs 32-bit fields, but - * might as well do it for 64-bit architectures - * since that's what we've historically done, - * and it makes 'head_tail' always be a simple - * 'unsigned long'. - */ -#ifdef CONFIG_64BIT -typedef unsigned int pipe_index_t; -#else -typedef unsigned short pipe_index_t; -#endif - /* * We have to declare this outside 'struct pipe_inode_info', * but then we can't use 'union pipe_index' for an anonymous @@ -51,10 +38,10 @@ typedef unsigned short pipe_index_t; * below. Annoying. */ union pipe_index { - unsigned long head_tail; + unsigned int head_tail; struct { - pipe_index_t head; - pipe_index_t tail; + unsigned short head; + unsigned short tail; }; }; @@ -89,15 +76,15 @@ struct pipe_inode_info { /* This has to match the 'union pipe_index' above */ union { - unsigned long head_tail; + unsigned int head_tail; struct { - pipe_index_t head; - pipe_index_t tail; + unsigned short head; + unsigned short tail; }; }; - unsigned int max_usage; - unsigned int ring_size; + unsigned short max_usage; + unsigned short ring_size; unsigned int nr_accounted; unsigned int readers; unsigned int writers; @@ -181,7 +168,7 @@ static inline bool pipe_has_watch_queue(const struct pipe_inode_info *pipe) * @head: The pipe ring head pointer * @tail: The pipe ring tail pointer */ -static inline bool pipe_empty(unsigned int head, unsigned int tail) +static inline bool pipe_empty(unsigned short head, unsigned short tail) { return head == tail; } @@ -191,9 +178,9 @@ static inline bool pipe_empty(unsigned int head, unsigned int tail) * @head: The pipe ring head pointer * @tail: The pipe ring tail pointer */ -static inline unsigned int pipe_occupancy(unsigned int head, unsigned int tail) +static inline unsigned short pipe_occupancy(unsigned short head, unsigned short tail) { - return (pipe_index_t)(head - tail); + return head - tail; } /** @@ -202,8 +189,8 @@ static inline unsigned int pipe_occupancy(unsigned int head, unsigned int tail) * @tail: The pipe ring tail pointer * @limit: The maximum amount of slots available. */ -static inline bool pipe_full(unsigned int head, unsigned int tail, - unsigned int limit) +static inline bool pipe_full(unsigned short head, unsigned short tail, + unsigned short limit) { return pipe_occupancy(head, tail) >= limit; } diff --git a/kernel/watch_queue.c b/kernel/watch_queue.c index 5267adeaa403..c76cfebf46c8 100644 --- a/kernel/watch_queue.c +++ b/kernel/watch_queue.c @@ -101,7 +101,8 @@ static bool post_one_notification(struct watch_queue *wqueue, struct pipe_inode_info *pipe = wqueue->pipe; struct pipe_buffer *buf; struct page *page; - unsigned int head, tail, mask, note, offset, len; + unsigned short head, tail, mask; + unsigned int note, offset, len; bool done = false; spin_lock_irq(&pipe->rd_wait.lock); diff --git a/mm/filemap.c b/mm/filemap.c index d4564a79eb35..6007b2403471 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -2943,9 +2943,10 @@ ssize_t filemap_splice_read(struct file *in, loff_t *ppos, { struct folio_batch fbatch; struct kiocb iocb; - size_t total_spliced = 0, used, npages; + size_t total_spliced = 0, npages; loff_t isize, end_offset; bool writably_mapped; + unsigned short used; int i, error = 0; if (unlikely(*ppos >= in->f_mapping->host->i_sb->s_maxbytes)) @@ -2956,7 +2957,7 @@ ssize_t filemap_splice_read(struct file *in, loff_t *ppos, /* Work out how much data we can actually add into the pipe */ used = pipe_occupancy(pipe->head, pipe->tail); - npages = max_t(ssize_t, pipe->max_usage - used, 0); + npages = max_t(unsigned short, pipe->max_usage - used, 0); len = min_t(size_t, len, npages * PAGE_SIZE); folio_batch_init(&fbatch); diff --git a/mm/shmem.c b/mm/shmem.c index 4ea6109a8043..339084e5a8a1 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -3509,13 +3509,14 @@ static ssize_t shmem_file_splice_read(struct file *in, loff_t *ppos, struct inode *inode = file_inode(in); struct address_space *mapping = inode->i_mapping; struct folio *folio = NULL; - size_t total_spliced = 0, used, npages, n, part; + size_t total_spliced = 0, npages, n, part; + unsigned short used; loff_t isize; int error = 0; /* Work out how much data we can actually add into the pipe */ used = pipe_occupancy(pipe->head, pipe->tail); - npages = max_t(ssize_t, pipe->max_usage - used, 0); + npages = max_t(unsigned short, pipe->max_usage - used, 0); len = min_t(size_t, len, npages * PAGE_SIZE); do {