From patchwork Sun Dec 8 17:32:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Alex Markuze X-Patchwork-Id: 848296 Received: from mx0b-001b2d01.pphosted.com (mx0b-001b2d01.pphosted.com [148.163.158.5]) (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 DACD822EE4 for ; Sun, 8 Dec 2024 17:32:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=148.163.158.5 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733679144; cv=fail; b=i8JQGdUPlsulAByuXK4S4/2l4e9qbKdwf4By3sCY29qPijH1xXC80jjvtO/Ffxl27nsQmhj04cM22uFfqHi2R/iV2S/EOY8hTlp3XHKwyp+vMOVGxMRtapO/WkPr1vHlO+CRvVyE64iHx0EKPZVBJzo5WDbP7gbnLEvF1l3kIGQ= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733679144; c=relaxed/simple; bh=BS+wFGWzUObQUru6If5tkC7KWiecX7t0173BV0SzP+o=; h=From:To:Subject:Date:Message-ID:References:In-Reply-To: Content-Type:MIME-Version; b=a+MeAjkbfaSykmL3HOm22Nw/5L+oGslKSnWolvpn0lGD0Nzo82PXeB5grqLlykNfkhzXYho5JTk4G6Y0tZ+GX/WJ5EwoyavyiNcfJIRX2oyXMmTysDO6VbW4vZn1ZfSjn/1sV1zfD3ndJghSBmiVSlQUVQu9jd6byH2m6dvHLCg= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=ibm.com; spf=pass smtp.mailfrom=ibm.com; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b=GIZyOlbo; arc=fail smtp.client-ip=148.163.158.5 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=ibm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=ibm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b="GIZyOlbo" Received: from pps.filterd (m0360072.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 4B86vFnw012588 for ; Sun, 8 Dec 2024 17:32:21 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h= content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s=pp1; bh=BS+wFG WzUObQUru6If5tkC7KWiecX7t0173BV0SzP+o=; b=GIZyOlboam8d51CCqMrIQ0 YzZhFz9troxNR3d3mvV9uYDsxCg1JdWeErZSZUfrDzXWWiCvWGJjq39TBQr2n1AJ arrzdF8V9npsT0Hj7umzpMa1goPDWwVlCWRilHFAf8YBLAlqUgnZl173LtkdPB4n oMMx2MxCQjujnNGl8cB0M1LdjycnTYmFvYk/1Mb1HXBdePUxDycFSJ1e6gzCAJnP 4bos2rXr+Ej7RYDgduHdZWb5WWDXdkd1rdxJ1du+bG9uqDHWtgHLR3wnWfKK8gdR Fp0Goe3XdaueVN47RHjYEZKNrBBlUYX7kC+6oyj/QMR9CheCZOUsA7I51wzkW7aQ == Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 43ce0x4vm8-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Sun, 08 Dec 2024 17:32:21 +0000 (GMT) Received: from m0360072.ppops.net (m0360072.ppops.net [127.0.0.1]) by pps.reinject (8.18.0.8/8.18.0.8) with ESMTP id 4B8HWKko022824 for ; Sun, 8 Dec 2024 17:32:20 GMT Received: from nam10-dm6-obe.outbound.protection.outlook.com (mail-dm6nam10lp2045.outbound.protection.outlook.com [104.47.58.45]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 43ce0x4vm5-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sun, 08 Dec 2024 17:32:20 +0000 (GMT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=X1k0Zhb1IfzGrdZBBOV52bgfXkh6aoJ+6F+zeG3vsei/l6fKAfndB7VJHbqqfrGApc4tB8YmWLx8TzEAu3eepWV0GVVwsQRLLenHUn9huFp1bTtT/kWLWO/eMf92//l+HpjErveqm1Qw7MUcDOCs07+yRAZ8SL1E05ywkavfNuXeJ5Eyq2hu2ST11pfcuCd4qUehAevgTNTnkHOjDu4cbCHou2CIGCSHmqUo4Y/dto7lIOOkLhBx5wCrFxPAW6CO4muZ1q6hr4NIH7ofQ2cl9QtFiMMgxMfwB1+7X76MrhUfAHKlMlLkauhiNvOhKHwCXI6BmyG/WKUFPE7lCpcNyg== 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=HDnmdNl+lHcdGxeujlfzm7Noe4yDjatFlrG4nxk1vy4=; b=MP7YuvmKxwpH+VswlOVemNyfGL3q8y9yYndleHAN4xHPpN3NYlC5ECGgSMGYH9lKyysP8LMEPWQ1+LbdJ8jL2EhkTKrSd9/hAxIMiiD+8k8rJl1EC3RmkvFdtWX3o5uBAo7uBVoryddVuK4oeF90VGND8WllqnVU3BDudbCXzGbNWD8h/QaP/UYsMIASquULbqUtt8+ZJKNj5lZMasjGTADf/ZxGHY7VrjJMMCx83Jhwj2jIM3d9T4lcpjDwTfX/uQnaSM/NOE1yAosG0eoQXmrnPw+iiVyzIVsi+ykS3+G/trJ2OLSM7iWfDYAhe/IIIvjand9x7THBDZ98Y+A/jA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=ibm.com; dmarc=pass action=none header.from=ibm.com; dkim=pass header.d=ibm.com; arc=none Received: from CO1PR15MB4876.namprd15.prod.outlook.com (2603:10b6:303:e3::18) by SA1PR15MB4321.namprd15.prod.outlook.com (2603:10b6:806:1ac::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8230.12; Sun, 8 Dec 2024 17:32:18 +0000 Received: from CO1PR15MB4876.namprd15.prod.outlook.com ([fe80::9887:d635:6513:10f]) by CO1PR15MB4876.namprd15.prod.outlook.com ([fe80::9887:d635:6513:10f%7]) with mapi id 15.20.8230.016; Sun, 8 Dec 2024 17:32:18 +0000 From: Alex Markuze To: "ceph-devel@vger.kernel.org" , "idryomov@gmail.com" Subject: [PATCH] ceph: improve error handling and short/overflow-read logic in __ceph_sync_read() Thread-Topic: [PATCH] ceph: improve error handling and short/overflow-read logic in __ceph_sync_read() Thread-Index: AQHbSZchpaDtxBRPtEiqVZCjH9oDng== Date: Sun, 8 Dec 2024 17:32:18 +0000 Message-ID: References: <20241208172930.3975558-1-amarkuze@redhat.com> In-Reply-To: <20241208172930.3975558-1-amarkuze@redhat.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: msip_labels: x-ms-publictraffictype: Email x-ms-traffictypediagnostic: CO1PR15MB4876:EE_|SA1PR15MB4321:EE_ x-ms-office365-filtering-correlation-id: adb7c2ef-6dc0-4527-d1a4-08dd17ae43a6 x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam: BCL:0; ARA:13230040|10070799003|1800799024|376014|366016|38070700018; x-microsoft-antispam-message-info: =?iso-8859-1?q?jjiSgKn3K2ONGcfw82o0A+bOyv?= =?iso-8859-1?q?6KSRJ0un4e/Dm6HrKuDc6hL3tXfTdTQF3nN5GgjqBiu97rpzNeeTt1HaJ7SP?= =?iso-8859-1?q?HUuY6c1t6xfMK+ChMro1NovTc91Ko1OqSDkvGi+ljcZn33Vctg6WbZYpVRTR?= =?iso-8859-1?q?fvHwV6tepFIXqitmCreKjybR8VMIMBBI4VOnDqlEeRTWnKXyYLZg4T4jVO1w?= =?iso-8859-1?q?lr/lVOvHtnvih5sW4BQhGdZeKMBhcR6VgH7SxeZQIKVgpMbx/X8lgdH94xeP?= =?iso-8859-1?q?BHuFHOhg31NFEfjZE6cHLYwviVhgTiM0FO49aGdU16dxg3cjkYsSqhHWWsll?= =?iso-8859-1?q?2ZRvoyroVTj3GjIgZQgMPfXR5qQXHGvVVuh7eAAGwt/u3m6a8CFKTqY8LniA?= =?iso-8859-1?q?XW7N9hcp7OQQ/AWi0jpmH5Xnd1RZk+TiMiO/B/uUd+JoRGJ+NRHMd6npjCD/?= =?iso-8859-1?q?CcWUt7UbZjoJrUusXMpvVHDRJyVlf+3sTemMgQdc/TOPmyMaswBABEuCOP6C?= =?iso-8859-1?q?8hwVDQVOTD/vYFL5LoSolShYqWGZteU/a6hFkr1xn2gxAKXB2luMld0PdIn9?= =?iso-8859-1?q?r8v6cOZZH+ddcqH8hfPs2eWyt0NovB9f0j2rQzWysRfPl6uQHopmL4KIh5s1?= =?iso-8859-1?q?drhYnMlRZeGIbHc4GJDqQ3kx7gl79FMq+2mREsVdeSdzulUGwqRT60WgZjQu?= =?iso-8859-1?q?8iudwr52KfsfoyT5lrLNqDEKER2Tu8fm9pqxvI40tCHVMi1jTZl/EyiFVnlb?= =?iso-8859-1?q?l0lkz9prMRywo+aSKNwadTO/7TVkjsag/r25fCvtoGDZi10q+yNYBpLIyy/n?= =?iso-8859-1?q?P3p1O58qlLQKQricM5ErFIWAaqW0C0F1aZJkttpX+Uhai7x6eIV2DvcRlA2r?= =?iso-8859-1?q?+TuU8Uy6DV43Ug9h62OdXQ6qsb/uMRQugGFZdfuEBRSJT0ymxC+oY7REGVl8?= =?iso-8859-1?q?IbAxUSAidoCGNoCz4qdnZPNydSVo3rfQdcJQFh6iP1DVnVeZzbcaZfQUW6lM?= =?iso-8859-1?q?m4/bXmwoG1gHixve/NBGi4avHD6xH4hRAni5nJEcqThz9QR15wrscF2VnpX9?= =?iso-8859-1?q?dD05iPjexAMa179nlC8/sLPsU2/Nm+qKzU79+QWDVzQSKYm4dS8c2XVySBSd?= =?iso-8859-1?q?BBUADdsCFAHZFtzaokQCgnGQfG6WX4jCdxQn6B9ka0pkT4Pn6QieIB4rYXQU?= =?iso-8859-1?q?92KTuoxaADHTqWJUxcXL41kmCuraeQ19gNt/DpECCT8K+cusGUk0jTiueBmG?= =?iso-8859-1?q?0h/VDEUAcFfrGJ9AyleVhWLy//dEca2k4JAuYo8mn/aEJxPZf7zqK3gkzJ5f?= =?iso-8859-1?q?fHnIxbkIW5lUj0214zMKrJw0xc+zK3+eACjp2FJk9+EkyslsbiMttfKtnLhy?= =?iso-8859-1?q?cusCpU9h5Af31ytfdOfKfZpmM8xW8UksO64zHDXzE=3D?= x-forefront-antispam-report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:CO1PR15MB4876.namprd15.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230040)(10070799003)(1800799024)(376014)(366016)(38070700018); DIR:OUT; SFP:1101; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?iso-8859-1?q?g2nOvAxOQ1UjPC5kOlOws5a?= =?iso-8859-1?q?QVPnALy3VopKx1VutWn105DHQ2m6rpzl95f6n2Q0z4NJ8sBfP7+ExL2I4pba?= =?iso-8859-1?q?R4Px56uASRkvXfsloN3OPQSOS5wGAJY+cylBudh2cvZ+4j9w8j+2vp4dMc8l?= =?iso-8859-1?q?dKbTtE5s7C362I0QIoBgGKD8FShrNbxu0YOpfH7DtUNun9PeTHGIY7p+ub+N?= =?iso-8859-1?q?L2f3L/mppwNb1tWSROBBE8E41c9opYOgF+h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wcwT?= =?iso-8859-1?q?3wjhcVd0VToL6aE4IEFRRLRinQu0uJEMTtiK4A60VTU2wB2/QSlU348koLe0?= =?iso-8859-1?q?XJR56oD75iOfeSiQe9kVNMIrTEeOeCtRuVQJ3xz41eRBsfXYoydPdqPsg8Hq?= =?iso-8859-1?q?OLB7zfrfGGZDa0WI0RufAgIAz6nk0i2masSx6zKgrVi417OeZVIfEDm5RumM?= =?iso-8859-1?q?BOX39kDvKtp8sH1UjHEYJvo6ypXs/aD7009k0AgDSDLegCBD1Q6e1Gl3gVx9?= =?iso-8859-1?q?iio5jPCvixfbIianoa4xqrnNyU+rmSy94rO358pXbIYE004iBQ9ZAaH8xnIX?= =?iso-8859-1?q?KnwvQQN+jRoK0HeW4tbK4XN/v79WBQ/LimnOT98/P3tA/T/WHNs+PFpGJ8xN?= =?iso-8859-1?q?8OSeDhcR5Lt37BsSJLsBSO+MsQjgVhhLautWrubK7giDayHZN9qnzAQaPKTv?= =?iso-8859-1?q?/tPDyqsEeRlGVk5ZV7q7OWGF+9SEZ1nOpdQZcx6/BinDd+dIzi/FKTkHtGCU?= =?iso-8859-1?q?qFGyW3p/r0VgdypxiRgwpespv+nh+XPx5De04dw=3D=3D?= X-OriginatorOrg: ibm.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: CO1PR15MB4876.namprd15.prod.outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: adb7c2ef-6dc0-4527-d1a4-08dd17ae43a6 X-MS-Exchange-CrossTenant-originalarrivaltime: 08 Dec 2024 17:32:18.3141 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: fcf67057-50c9-4ad4-98f3-ffca64add9e9 X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: BOtOZ+1MNJoVuyx1COkNrkugFjfh2k0zX7tsbImr9RgVT5cGgCm446pyXzLrC1QfOxsJtJf1aAOU4LpKKgWcXg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA1PR15MB4321 X-Proofpoint-GUID: fhhKva3Yg8tA2RUkZLeCe0J1GCv8pkyt X-Proofpoint-ORIG-GUID: ZLadhcCe9QzGJU87_VAUDk7q5HvaGY2v Precedence: bulk X-Mailing-List: ceph-devel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1051,Hydra:6.0.680,FMLib:17.12.62.30 definitions=2024-10-15_01,2024-10-11_01,2024-09-30_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 lowpriorityscore=0 spamscore=0 clxscore=1011 impostorscore=0 mlxscore=0 mlxlogscore=999 priorityscore=1501 malwarescore=0 adultscore=0 bulkscore=0 phishscore=0 suspectscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2411120000 definitions=main-2412080145 This patch refines the read logic in __ceph_sync_read() to ensure more predictable and efficient behavior in various edge cases. - Return early if the requested read length is zero or if the file size   (`i_size`) is zero. - Initialize the index variable (`idx`) where needed and reorder some   code to ensure it is always set before use. - Improve error handling by checking for negative return values earlier. - Remove redundant encrypted file checks after failures. Only attempt   filesystem-level decryption if the read succeeded. - Simplify leftover calculations to correctly handle cases where the read   extends beyond the end of the file or stops short. - This resolves multiple issues caused by integer overflow   - https://tracker.ceph.com/issues/67524   - https://tracker.ceph.com/issues/68981   - https://tracker.ceph.com/issues/68980 Signed-off-by: Alex Markuze ---  fs/ceph/file.c | 29 ++++++++++++++---------------  1 file changed, 14 insertions(+), 15 deletions(-) -- 2.34.1 diff --git a/fs/ceph/file.c b/fs/ceph/file.c index ce342a5d4b8b..8e0400d461a2 100644 --- a/fs/ceph/file.c +++ b/fs/ceph/file.c @@ -1066,7 +1066,7 @@ ssize_t __ceph_sync_read(struct inode *inode, loff_t *ki_pos,          if (ceph_inode_is_shutdown(inode))                  return -EIO;   -       if (!len) +       if (!len || !i_size)                  return 0;          /*           * flush any page cache pages in this range.  this @@ -1086,7 +1086,7 @@ ssize_t __ceph_sync_read(struct inode *inode, loff_t *ki_pos,                  int num_pages;                  size_t page_off;                  bool more; -               int idx; +               int idx = 0;                  size_t left;                  struct ceph_osd_req_op *op;                  u64 read_off = off; @@ -1160,7 +1160,14 @@ ssize_t __ceph_sync_read(struct inode *inode, loff_t *ki_pos,                  else if (ret == -ENOENT)                          ret = 0;   -               if (ret > 0 && IS_ENCRYPTED(inode)) { +               if (ret < 0) { +                       ceph_osdc_put_request(req); +                       if (ret == -EBLOCKLISTED) +                               fsc->blocklisted = true; +                       break; +               } + +               if (IS_ENCRYPTED(inode)) {                          int fret;                            fret = ceph_fscrypt_decrypt_extents(inode, pages, @@ -1187,7 +1194,7 @@ ssize_t __ceph_sync_read(struct inode *inode, loff_t *ki_pos,                  }                    /* Short read but not EOF? Zero out the remainder. */ -               if (ret >= 0 && ret < len && (off + ret < i_size)) { +               if (ret < len && (off + ret < i_size)) {                          int zlen = min(len - ret, i_size - off - ret);                          int zoff = page_off + ret;   @@ -1197,13 +1204,11 @@ ssize_t __ceph_sync_read(struct inode *inode, loff_t *ki_pos,                          ret += zlen;                  }   -               idx = 0; -               if (ret <= 0) -                       left = 0; -               else if (off + ret > i_size) -                       left = i_size - off; +               if (off + ret > i_size) +                       left = (i_size > off) ? i_size - off : 0;                  else                          left = ret; +                  while (left > 0) {                          size_t plen, copied;   @@ -1222,12 +1227,6 @@ ssize_t __ceph_sync_read(struct inode *inode, loff_t *ki_pos,                    ceph_osdc_put_request(req);   -               if (ret < 0) { -                       if (ret == -EBLOCKLISTED) -                               fsc->blocklisted = true; -                       break; -               } -                  if (off >= i_size || !more)                          break;          }