From patchwork Sat Dec 7 18:26:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ilya Dryomov X-Patchwork-Id: 13898361 Received: from mail-wr1-f42.google.com (mail-wr1-f42.google.com [209.85.221.42]) (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 1B8B714B96E for ; Sat, 7 Dec 2024 18:26:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733596002; cv=none; b=WO5sWdwq3iuUSGPYjQM31tQCU+2/c89RwM7V1P6fNduuHTTHs2kh3ZYn8GHTGUDeo430+2Z8RyZAEt6w45OqjllC9yMtE7P1F+c+VK7UgQQ5ayB5F6xpAPC4OljeEtczOBeEdJNtJZRi2d0IdpGfiLmU76XD9t8LK9n6oOjEt1w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733596002; c=relaxed/simple; bh=zpN7mCvmWgsGNMdbYkI6N+LLIU3D8E1lJmaRuOWBR6s=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=rp/E/J2Ccj/vDjSzlinLeHL8JZQxgompp75PX5Bjhqr/RWokNOYUMQSmbw/ohfvk422am7dconEreRaAHWdS8Tuwh2cX5+GqmKMI5lr93mOxXysrDz/Axlax1+3ZjFxFsGAbzQ8I027r+mwptNx9HxCX6Gyzkpjmyh/MXA+bjJQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=jSyeuReG; arc=none smtp.client-ip=209.85.221.42 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="jSyeuReG" Received: by mail-wr1-f42.google.com with SMTP id ffacd0b85a97d-3863494591bso335518f8f.1 for ; Sat, 07 Dec 2024 10:26:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1733595999; x=1734200799; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=hjVTmTY3qHMLl0cJaFxi4Fr2X/jFuu79M0U9ds0fjic=; b=jSyeuReGO7byDHIl7Mes1x8XY1wVd53oAqKpf4bP50YBCiZKp5FMd3v8OiFmzvbMuf zC1IF3vDkXYgkikE1x3YvH8t1lOZxqM9KjHadZtO9ZDiEbUbwZP+j1+lOX6VzkibXI+5 3QHtBSRtkjrb3t/Y3OhlYAyiPAzLv8ZhKBCw0LlXmT8g0sHGiqjupjtymXGvaBf/1ia9 KAB+7BaemDIgNwLv+HlZNCEpUgndXR8eoqEBa64CG39J8aFqBAM2mdb54g3XZTXGOcJw 9SOkeia6TSoPWEo7IuNV45slxeT1xAU0FW7w2SGqYmZ8Dq+O7tuUZccSAfzuEhM9eiRd f3+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733595999; x=1734200799; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=hjVTmTY3qHMLl0cJaFxi4Fr2X/jFuu79M0U9ds0fjic=; b=roLlKwHn/Wp2F6otkhRbdDso3wz5XixiM6QT090CPM4+cw8jECwrnaTgcrqcAhgHhD A9QX5YcTyUEkWIMsQppYw2I/E0IlWSydkU+DRMh6K0A/9B+5DGmSdRMzdBkfSd9+9nIy DUeVs5htsQ75PEayXVodIFK3BmPJp9kbontgVYsGhWCMPbcAGS7G4f5fVX9KPuoyfPJR suDcsADeKR2XF/dKkZhWw5Ci9ORqfOsGQR7qdB8ebJwiK+ZKLbQuOMuLNztELy5OPQHr SrnKBnlTx3OVGWERNIddxyoA4qwCDOEF0UhDT68XPpundy+TZ5B4KdCxrIJYXvsOt/q6 f/Zw== X-Gm-Message-State: AOJu0Yw8sGLYHtSqMiRhH3hjSd/bWQ6GgRT1oiy9ucdnqF9KTV1PFQRf T0AttWfl5TYPpGnKkLTZ7WvdLx7cLIHBh0Ldpwe7o74nHA/Y5jtYIDQV4Q== X-Gm-Gg: ASbGncvDmbJ5qwmp6jofkDwpPWcn/4dV2n4B5W4casNddmPNOZSujO0JTP1Cn3AMafD XLE1JZG+oLnJT3rZFYwV1DwXGydGdmwkjF2GUIug4lQyyy03MI8fe9MdhNpdQLEx8Y7P3QT8NYP RY8nzni1JakCMEO0ly78tEQxK2OmIAzNlGpH4r1Z/4ev5fsHlxZkiqyH4BUIjkJOJCSD3KmipCu QLmAIiInzJqpdBkj2BOqgxHbLpQFMETtyG0FRICRmXw8MEHE/QqekTPy4NAKqQSxlo4EmElmnfn WLzlRalJkdtcCDAX9g== X-Google-Smtp-Source: AGHT+IFRBge/GNp/1k+3bgmSeOpECqTWgNj1gwtHzB9r84DWNn2zCac7G06+B9EYjb2uWPvC3RRBdg== X-Received: by 2002:a05:6000:1fa4:b0:385:e38f:8cc with SMTP id ffacd0b85a97d-3862b3d5eacmr5936872f8f.38.1733595999151; Sat, 07 Dec 2024 10:26:39 -0800 (PST) Received: from localhost.localdomain (ip-94-112-167-15.bb.vodafone.cz. [94.112.167.15]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3862190989fsm7957386f8f.73.2024.12.07.10.26.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 07 Dec 2024 10:26:37 -0800 (PST) From: Ilya Dryomov To: ceph-devel@vger.kernel.org Cc: Alex Markuze , Max Kellermann , Jeff Layton Subject: [PATCH 1/2] ceph: fix memory leak in ceph_direct_read_write() Date: Sat, 7 Dec 2024 19:26:18 +0100 Message-ID: <20241207182622.97113-2-idryomov@gmail.com> X-Mailer: git-send-email 2.46.1 In-Reply-To: <20241207182622.97113-1-idryomov@gmail.com> References: <20241207182622.97113-1-idryomov@gmail.com> Precedence: bulk X-Mailing-List: ceph-devel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 The bvecs array which is allocated in iter_get_bvecs_alloc() is leaked and pages remain pinned if ceph_alloc_sparse_ext_map() fails. There is no need to delay the allocation of sparse_ext map until after the bvecs array is set up, so fix this by moving sparse_ext allocation a bit earlier. Also, make a similar adjustment in __ceph_sync_read() for consistency (a leak of the same kind in __ceph_sync_read() has been addressed differently). Cc: stable@vger.kernel.org Fixes: 03bc06c7b0bd ("ceph: add new mount option to enable sparse reads") Signed-off-by: Ilya Dryomov --- fs/ceph/file.c | 43 ++++++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/fs/ceph/file.c b/fs/ceph/file.c index f9bb9e5493ce..0df2ffc69e92 100644 --- a/fs/ceph/file.c +++ b/fs/ceph/file.c @@ -1116,6 +1116,16 @@ ssize_t __ceph_sync_read(struct inode *inode, loff_t *ki_pos, len = read_off + read_len - off; more = len < iov_iter_count(to); + op = &req->r_ops[0]; + if (sparse) { + extent_cnt = __ceph_sparse_read_ext_count(inode, read_len); + ret = ceph_alloc_sparse_ext_map(op, extent_cnt); + if (ret) { + ceph_osdc_put_request(req); + break; + } + } + num_pages = calc_pages_for(read_off, read_len); page_off = offset_in_page(off); pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL); @@ -1129,16 +1139,6 @@ ssize_t __ceph_sync_read(struct inode *inode, loff_t *ki_pos, offset_in_page(read_off), false, true); - op = &req->r_ops[0]; - if (sparse) { - extent_cnt = __ceph_sparse_read_ext_count(inode, read_len); - ret = ceph_alloc_sparse_ext_map(op, extent_cnt); - if (ret) { - ceph_osdc_put_request(req); - break; - } - } - ceph_osdc_start_request(osdc, req); ret = ceph_osdc_wait_request(osdc, req); @@ -1557,6 +1557,16 @@ ceph_direct_read_write(struct kiocb *iocb, struct iov_iter *iter, break; } + op = &req->r_ops[0]; + if (sparse) { + extent_cnt = __ceph_sparse_read_ext_count(inode, size); + ret = ceph_alloc_sparse_ext_map(op, extent_cnt); + if (ret) { + ceph_osdc_put_request(req); + break; + } + } + len = iter_get_bvecs_alloc(iter, size, &bvecs, &num_pages); if (len < 0) { ceph_osdc_put_request(req); @@ -1566,6 +1576,8 @@ ceph_direct_read_write(struct kiocb *iocb, struct iov_iter *iter, if (len != size) osd_req_op_extent_update(req, 0, len); + osd_req_op_extent_osd_data_bvecs(req, 0, bvecs, num_pages, len); + /* * To simplify error handling, allow AIO when IO within i_size * or IO can be satisfied by single OSD request. @@ -1597,17 +1609,6 @@ ceph_direct_read_write(struct kiocb *iocb, struct iov_iter *iter, req->r_mtime = mtime; } - osd_req_op_extent_osd_data_bvecs(req, 0, bvecs, num_pages, len); - op = &req->r_ops[0]; - if (sparse) { - extent_cnt = __ceph_sparse_read_ext_count(inode, size); - ret = ceph_alloc_sparse_ext_map(op, extent_cnt); - if (ret) { - ceph_osdc_put_request(req); - break; - } - } - if (aio_req) { aio_req->total_len += len; aio_req->num_reqs++; From patchwork Sat Dec 7 18:26:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ilya Dryomov X-Patchwork-Id: 13898362 Received: from mail-wm1-f50.google.com (mail-wm1-f50.google.com [209.85.128.50]) (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 3601114B96E for ; Sat, 7 Dec 2024 18:26:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.50 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733596005; cv=none; b=sDv3ldyOoBKr/eUbXFJBSGT47XZ6y94fnvYTcRe9bkz727+X0QN8J8sGyHkzePtjRDptw89Goc8mr12C/mCLc6EAAtCjWYXrR3Q6QqOqZwuxJpPoinr45P6cNMWGsi6lP8nD5KZkXpL8CR6zMq4JbyOeV8ewiZor28B0fzciHm8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733596005; c=relaxed/simple; bh=d8Bgfej8kxiAELXWymuZBPbUK4Is217AmBNTsdd0e2Y=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=IHLkTWwSCUP2yOYscV5mPgSon/aUi+4qj4CEKEbQfK5m6JtQ8ET1Pm8IiHYHUjSZrh0+dEkXFZpFLC1W8w3xniWXu8aD5W+zvYSWPpDSIc1g92JVqB/o8EJvtH5NSDRuAvstFH+rnRh15gpJbccAoKnVlw5WQnGl6LCt2N7rd4E= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=kv1O0E7g; arc=none smtp.client-ip=209.85.128.50 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="kv1O0E7g" Received: by mail-wm1-f50.google.com with SMTP id 5b1f17b1804b1-434f094c636so1879255e9.2 for ; Sat, 07 Dec 2024 10:26:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1733596002; x=1734200802; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=hbECu90BnaSPKem32qyeN1V+u7434lzbvNHUSLLCNuc=; b=kv1O0E7gEHukJDLy8HkiZPmZWEyxpeeIrW92OxYSg0nSm7FS30edtrtpDP1B/2mgeI m23QWUuu/tqbRJPr2yV8CNmftghZVxFpLBWPXXlycZx6HEWHEB8VVDIMPmiYv5fKxt5z VeJulA/blt/OTnRA4+otUNl0DyZeJyqRz1ATGb4ePR+xZ1SHPzCPDS5yviqgpkxoV0QT XXjjhMxQaGnU8FfJf8idclG2nbYuompRnQCc7gvd7KdBxgJ7vCdK1fR+spYF8DJh0Igq avUje/5WJeEeE81brqXk0v+kBVUskwLRdY6J15Ia9gdNFMmBWJlwBxZbPQzeNgbAGDJF 0PGw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733596002; x=1734200802; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=hbECu90BnaSPKem32qyeN1V+u7434lzbvNHUSLLCNuc=; b=Jj5ZshtOTxMSuc7/ESZv4xvo+0w3VY4lp3Jfqa9//DSvjCpH0ybkSRJtxAHkGKeTyM qTl/MbhYSr5PmChHbAxmAkYBlN2yRXI2PLmjsMR1c1BAEnnmFIOqfl+1UbU6U8DSxZgQ wCeOa+RHaHeH8kLgGA6ZNqR+p8ijqzKlIpzzaeBh/sRKdvKkXayh927M9euvFGV3I6Go GeuV5dbby69VNuQ7dM10dD/Wa7/fz2ezGFihNt3QQGBuzAMkDMqGBrwVKDuJIyy7c5ej c0k7DDc94vtRxeDu9Wq88JAXZ/tNc5NHQL/YVJmXvWePUQKjPAZ6fygUgl1Sa4GWWiSW mIGw== X-Gm-Message-State: AOJu0Yzfc3VI/US3YjW3t3UDVT5sU1oFdQD4SfL/urPheNWrc37Bvwp6 V5xPC5Bwase21Ia5E6DeG+TSz6zmr+xVpcNN0BXu4wg8FqEu5+XYKomCXg== X-Gm-Gg: ASbGncuiBYPXDxCGOXATKi7oFga86Q9F8a59nz9nBXr+3SgB7lpYDdAZSkEbOqOIz/D Uve1Go2InVk6/j+4CvKvK+/P5CEaAq0N+Sac0FiD6/Rd91Rdo/N5pIFpp72KocTc7TKI5RxI+5o /0tb5DsTcVf+Siff5rBNESkwymxDsZjv14i5KB3yeopmQBgVYeveqmEhN3C5Scwjo1E2cDax1rK +ktntIvtIH0bdSYJJ/WFjV/TNfMrgpJZn9fePG1MgEpt8v4R3WxwKvDE20GFgtSDVJRqbxxuUmk WvQnBE4Xl4f0Dhz0vg== X-Google-Smtp-Source: AGHT+IE+kVYm4YqrfNN08PYYJ5RK/Hg/4bc4AMsc9WZ9Y684RzV8TFLciMYqAOIH6tZkKbdiYg7lwQ== X-Received: by 2002:a5d:64c3:0:b0:385:e1e5:fff3 with SMTP id ffacd0b85a97d-3862b3e8852mr6225341f8f.57.1733596002429; Sat, 07 Dec 2024 10:26:42 -0800 (PST) Received: from localhost.localdomain (ip-94-112-167-15.bb.vodafone.cz. [94.112.167.15]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3862190989fsm7957386f8f.73.2024.12.07.10.26.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 07 Dec 2024 10:26:40 -0800 (PST) From: Ilya Dryomov To: ceph-devel@vger.kernel.org Cc: Alex Markuze , Max Kellermann , Jeff Layton Subject: [PATCH 2/2] ceph: allocate sparse_ext map only for sparse reads Date: Sat, 7 Dec 2024 19:26:19 +0100 Message-ID: <20241207182622.97113-3-idryomov@gmail.com> X-Mailer: git-send-email 2.46.1 In-Reply-To: <20241207182622.97113-1-idryomov@gmail.com> References: <20241207182622.97113-1-idryomov@gmail.com> Precedence: bulk X-Mailing-List: ceph-devel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 If mounted with sparseread option, ceph_direct_read_write() ends up making an unnecessarily allocation for O_DIRECT writes. Fixes: 03bc06c7b0bd ("ceph: add new mount option to enable sparse reads") Signed-off-by: Ilya Dryomov --- fs/ceph/file.c | 2 +- net/ceph/osd_client.c | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/fs/ceph/file.c b/fs/ceph/file.c index 0df2ffc69e92..f17bc4dc629c 100644 --- a/fs/ceph/file.c +++ b/fs/ceph/file.c @@ -1558,7 +1558,7 @@ ceph_direct_read_write(struct kiocb *iocb, struct iov_iter *iter, } op = &req->r_ops[0]; - if (sparse) { + if (!write && sparse) { extent_cnt = __ceph_sparse_read_ext_count(inode, size); ret = ceph_alloc_sparse_ext_map(op, extent_cnt); if (ret) { diff --git a/net/ceph/osd_client.c b/net/ceph/osd_client.c index 9b1168eb77ab..b24afec24138 100644 --- a/net/ceph/osd_client.c +++ b/net/ceph/osd_client.c @@ -1173,6 +1173,8 @@ EXPORT_SYMBOL(ceph_osdc_new_request); int __ceph_alloc_sparse_ext_map(struct ceph_osd_req_op *op, int cnt) { + WARN_ON(op->op != CEPH_OSD_OP_SPARSE_READ); + op->extent.sparse_ext_cnt = cnt; op->extent.sparse_ext = kmalloc_array(cnt, sizeof(*op->extent.sparse_ext),