From patchwork Wed Mar 22 18:57:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Stoakes X-Patchwork-Id: 73642 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:604a:0:0:0:0:0 with SMTP id j10csp2529157wrt; Wed, 22 Mar 2023 12:34:39 -0700 (PDT) X-Google-Smtp-Source: AK7set87gI3olWYbx8CJPr19FS3H1Y1Vb2X4YFIrKFKxnlCrtWTSEQAw5brSTc6BCZ3Bj0WUfJdw X-Received: by 2002:a17:903:d3:b0:1a0:549d:39a1 with SMTP id x19-20020a17090300d300b001a0549d39a1mr3495498plc.32.1679513679004; Wed, 22 Mar 2023 12:34:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679513678; cv=none; d=google.com; s=arc-20160816; b=aHvG0eHZb5zBr1/xf72Px/2vTw1TFqV8FUaC6L+F+LoiS85sW1D/DJo4o/dLydT3w/ /D2w2rEoxXmVIpXz9ODbpvY21AG1BK76v5PlicbmKvyuJ7bD76rXRpeDJt+CUEPQbJnE yad72eDORQu/zYtWsESNOHEDik1wyXFbI9hcy/GUrQr6T7r2wC0OfNd/fAW3puOk6N7N TQRllXyNkIKf5qmRl8bCsXQuGRMbNYQ+D46zo8NwSVIqVJaJ1pCnuRsesjGGhxQ22KIz 8YAYbvPnwslLrL7Sa4p4xEZTOg4qKpdv50G2ZjrRWTkpDzUmTCJbstNAi7UcaGCFhCq9 VREw== 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=CnqbASwlTwT+hpjobkOVo9MU5RGxoqqRsLFO5z2uP7A=; b=zUe0bKB3cg64lvUt1COTSfvhNZzgvsIJK75OwjMKkrUomInT4iTk0UOl32J4CVnOV/ 6XLSaN/uqZ9Hw4mOkVjvThobsbS4yyE2COzphnMhHP/3Etc8YqQqrn0x0ngJxNkoLmhC 4uaVJ/eKvcAbRTFzqlrFzcTK8/XMylRz7KOeaAClmzQTe3ndGMtoWG5SO2LCchq0g4l+ of75U7QF9eSAojghSS+Axwmcgo/+4Uk1dg5RfsKVdJtAjsoTMZQYb55iGoO8s1mnSoHs m4t0ngAauzt6h8/pXXzE/npBN0Q/tzymUvH5x4afbi8Te45crH1i45sdB5Bc74Ozp3B9 cAqQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=X5rLPjH2; 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=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id e5-20020a17090301c500b0019e4154578esi17872753plh.76.2023.03.22.12.33.56; Wed, 22 Mar 2023 12:34:38 -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=@gmail.com header.s=20210112 header.b=X5rLPjH2; 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=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231362AbjCVS62 (ORCPT + 99 others); Wed, 22 Mar 2023 14:58:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51896 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230429AbjCVS54 (ORCPT ); Wed, 22 Mar 2023 14:57:56 -0400 Received: from mail-wm1-x32b.google.com (mail-wm1-x32b.google.com [IPv6:2a00:1450:4864:20::32b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 679576A400; Wed, 22 Mar 2023 11:57:12 -0700 (PDT) Received: by mail-wm1-x32b.google.com with SMTP id p16so12145300wmq.5; Wed, 22 Mar 2023 11:57:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1679511430; 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=CnqbASwlTwT+hpjobkOVo9MU5RGxoqqRsLFO5z2uP7A=; b=X5rLPjH26RWLUUeLBu1hISlyzQSMr+PgXho7dojo9U9tdfKws5wILXd6kKzml0qaYE 6UAxUEgaYTZl7w1y+ma2wJl1xqHyAhy4CMtQZOoqAinLm47gNH20SbSdZCtQ9/e8LXpI IZpPPGJ/wFlT7ktyodLSZ+9szWbYnaNZGwrfpWXKGs/Yd0ER4CyIbUEIC1MFgZ/FH329 8lxyNsOYVpzfl98WTWbwg8hqf/SfSzxR7KUkKxPLRMDUXJOhoyMS0qicmwa+6FxqC5Nv zTGnpbotv7uW1d+2Bf/OaXkcjJ6C+DuhSpDxHTCoxb87dqZ+BIBtPz9c2sUD/T+P3eD0 6BMg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679511430; 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=CnqbASwlTwT+hpjobkOVo9MU5RGxoqqRsLFO5z2uP7A=; b=qvT9wY2y1W2wh5yb4BtMf2nVYhl8K7ENqCyzcRKlzEJ7YjjbdtVYhPd+X5tThwOLCq 7pldfmHQzb64l5DeoxXzFQfU+Xo7DHeZefFjAFKd7/rqHXiJ0uCRUbW07EoKJdvZs1nr IL98M+CqX61fjlN//AF029jm7VSKlNVfipmP+3z33ohTy9l8r5CAAMPAFK4l42r1Nuu6 ilx1AeAhLuDSjVDOalfwH8HF4jIOx6j4RYczjGF40jz9xPxwTExFLT1FCRdzs066p1oW i13aCb8aRvllno8l/ysOgUIzuX0w7TXDlhNk+s9cdSvlAfdobus8cpFL1QzSrXT/FupP uDqA== X-Gm-Message-State: AO0yUKU5c6ddSdHhgJeoNq505EhSUj4qy4NwUsTXMkxQJt1bs4Ek5zPh 8zjiUl18pLYdANNSR60eNHE= X-Received: by 2002:a05:600c:218d:b0:3eb:a4e:a2b2 with SMTP id e13-20020a05600c218d00b003eb0a4ea2b2mr463420wme.4.1679511430687; Wed, 22 Mar 2023 11:57:10 -0700 (PDT) Received: from lucifer.home ([2a00:23c5:dc8c:8701:1663:9a35:5a7b:1d76]) by smtp.googlemail.com with ESMTPSA id j21-20020a05600c42d500b003ee581f37a9sm3181241wme.10.2023.03.22.11.57.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Mar 2023 11:57:10 -0700 (PDT) From: Lorenzo Stoakes To: linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, Andrew Morton Cc: Baoquan He , Uladzislau Rezki , Matthew Wilcox , David Hildenbrand , Liu Shixin , Jiri Olsa , Jens Axboe , Alexander Viro , Lorenzo Stoakes Subject: [PATCH v7 2/4] fs/proc/kcore: convert read_kcore() to read_kcore_iter() Date: Wed, 22 Mar 2023 18:57:02 +0000 Message-Id: X-Mailer: git-send-email 2.39.2 In-Reply-To: References: MIME-Version: 1.0 X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS autolearn=unavailable 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1761097735456124345?= X-GMAIL-MSGID: =?utf-8?q?1761097735456124345?= For the time being we still use a bounce buffer for vread(), however in the next patch we will convert this to interact directly with the iterator and eliminate the bounce buffer altogether. Signed-off-by: Lorenzo Stoakes Reviewed-by: David Hildenbrand --- fs/proc/kcore.c | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/fs/proc/kcore.c b/fs/proc/kcore.c index 556f310d6aa4..08b795fd80b4 100644 --- a/fs/proc/kcore.c +++ b/fs/proc/kcore.c @@ -24,7 +24,7 @@ #include #include #include -#include +#include #include #include #include @@ -308,9 +308,12 @@ static void append_kcore_note(char *notes, size_t *i, const char *name, } static ssize_t -read_kcore(struct file *file, char __user *buffer, size_t buflen, loff_t *fpos) +read_kcore_iter(struct kiocb *iocb, struct iov_iter *iter) { + struct file *file = iocb->ki_filp; char *buf = file->private_data; + loff_t *fpos = &iocb->ki_pos; + size_t phdrs_offset, notes_offset, data_offset; size_t page_offline_frozen = 1; size_t phdrs_len, notes_len; @@ -318,6 +321,7 @@ read_kcore(struct file *file, char __user *buffer, size_t buflen, loff_t *fpos) size_t tsz; int nphdr; unsigned long start; + size_t buflen = iov_iter_count(iter); size_t orig_buflen = buflen; int ret = 0; @@ -356,12 +360,11 @@ read_kcore(struct file *file, char __user *buffer, size_t buflen, loff_t *fpos) }; tsz = min_t(size_t, buflen, sizeof(struct elfhdr) - *fpos); - if (copy_to_user(buffer, (char *)&ehdr + *fpos, tsz)) { + if (copy_to_iter((char *)&ehdr + *fpos, tsz, iter) != tsz) { ret = -EFAULT; goto out; } - buffer += tsz; buflen -= tsz; *fpos += tsz; } @@ -398,15 +401,14 @@ read_kcore(struct file *file, char __user *buffer, size_t buflen, loff_t *fpos) } tsz = min_t(size_t, buflen, phdrs_offset + phdrs_len - *fpos); - if (copy_to_user(buffer, (char *)phdrs + *fpos - phdrs_offset, - tsz)) { + if (copy_to_iter((char *)phdrs + *fpos - phdrs_offset, tsz, + iter) != tsz) { kfree(phdrs); ret = -EFAULT; goto out; } kfree(phdrs); - buffer += tsz; buflen -= tsz; *fpos += tsz; } @@ -448,14 +450,13 @@ read_kcore(struct file *file, char __user *buffer, size_t buflen, loff_t *fpos) min(vmcoreinfo_size, notes_len - i)); tsz = min_t(size_t, buflen, notes_offset + notes_len - *fpos); - if (copy_to_user(buffer, notes + *fpos - notes_offset, tsz)) { + if (copy_to_iter(notes + *fpos - notes_offset, tsz, iter) != tsz) { kfree(notes); ret = -EFAULT; goto out; } kfree(notes); - buffer += tsz; buflen -= tsz; *fpos += tsz; } @@ -497,7 +498,7 @@ read_kcore(struct file *file, char __user *buffer, size_t buflen, loff_t *fpos) } if (!m) { - if (clear_user(buffer, tsz)) { + if (iov_iter_zero(tsz, iter) != tsz) { ret = -EFAULT; goto out; } @@ -508,14 +509,14 @@ read_kcore(struct file *file, char __user *buffer, size_t buflen, loff_t *fpos) case KCORE_VMALLOC: vread(buf, (char *)start, tsz); /* we have to zero-fill user buffer even if no read */ - if (copy_to_user(buffer, buf, tsz)) { + if (copy_to_iter(buf, tsz, iter) != tsz) { ret = -EFAULT; goto out; } break; case KCORE_USER: /* User page is handled prior to normal kernel page: */ - if (copy_to_user(buffer, (char *)start, tsz)) { + if (copy_to_iter((char *)start, tsz, iter) != tsz) { ret = -EFAULT; goto out; } @@ -531,7 +532,7 @@ read_kcore(struct file *file, char __user *buffer, size_t buflen, loff_t *fpos) */ if (!page || PageOffline(page) || is_page_hwpoison(page) || !pfn_is_ram(pfn)) { - if (clear_user(buffer, tsz)) { + if (iov_iter_zero(tsz, iter) != tsz) { ret = -EFAULT; goto out; } @@ -541,17 +542,17 @@ read_kcore(struct file *file, char __user *buffer, size_t buflen, loff_t *fpos) case KCORE_VMEMMAP: case KCORE_TEXT: /* - * We use _copy_to_user() to bypass usermode hardening + * We use _copy_to_iter() to bypass usermode hardening * which would otherwise prevent this operation. */ - if (_copy_to_user(buffer, (char *)start, tsz)) { + if (_copy_to_iter((char *)start, tsz, iter) != tsz) { ret = -EFAULT; goto out; } break; default: pr_warn_once("Unhandled KCORE type: %d\n", m->type); - if (clear_user(buffer, tsz)) { + if (iov_iter_zero(tsz, iter) != tsz) { ret = -EFAULT; goto out; } @@ -559,7 +560,6 @@ read_kcore(struct file *file, char __user *buffer, size_t buflen, loff_t *fpos) skip: buflen -= tsz; *fpos += tsz; - buffer += tsz; start += tsz; tsz = (buflen > PAGE_SIZE ? PAGE_SIZE : buflen); } @@ -603,7 +603,7 @@ static int release_kcore(struct inode *inode, struct file *file) } static const struct proc_ops kcore_proc_ops = { - .proc_read = read_kcore, + .proc_read_iter = read_kcore_iter, .proc_open = open_kcore, .proc_release = release_kcore, .proc_lseek = default_llseek,