From patchwork Sun Mar 19 00:20:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Stoakes X-Patchwork-Id: 13180172 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7BC0BC74A5B for ; Sun, 19 Mar 2023 00:20:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 09517900008; Sat, 18 Mar 2023 20:20:24 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 01DD9900004; Sat, 18 Mar 2023 20:20:23 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DB2F8900008; Sat, 18 Mar 2023 20:20:23 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id CCFA9900004 for ; Sat, 18 Mar 2023 20:20:23 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id A7E58160B67 for ; Sun, 19 Mar 2023 00:20:23 +0000 (UTC) X-FDA: 80583741126.19.FF7E6DF Received: from mail-wr1-f42.google.com (mail-wr1-f42.google.com [209.85.221.42]) by imf18.hostedemail.com (Postfix) with ESMTP id BACC21C0008 for ; Sun, 19 Mar 2023 00:20:21 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b="n3/WQOG/"; spf=pass (imf18.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.221.42 as permitted sender) smtp.mailfrom=lstoakes@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1679185221; a=rsa-sha256; cv=none; b=FKzp7rBmD/khqj5jNlJHGu+PqI0IOG3XCkALp+QB4YI5WTOfM9zFw2YoCB0vTTBzXgRuVb /aLnDwrBXyUxuCaN4KtYx7j0+oztHTgASIlTBN1M1SNUApLE1H6qPZrYy8rppsluM4htPw A64zkgw7yKLqiBIhJJ+EnakbeZg+3FE= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b="n3/WQOG/"; spf=pass (imf18.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.221.42 as permitted sender) smtp.mailfrom=lstoakes@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1679185221; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=ML/fjqBQiFdniaLp2U4iIoVxIHX7dLvD1UO1EbWAfgg=; b=xk4GlJk+NMlcAcdR4B4slWOQHb5lgZzvNxFioOyB5tC19vAFUZ2U+IvXBod9fResSSKff0 gW+xnNYaYvjoTmbX+1TdCyA4Q6yc897pjdeIvf2laeMhFLj4cz2KttRsSAjqn3V6FmNFeZ VIG4B4IIIEvuZbpsKgxTiYFcu0SWnHU= Received: by mail-wr1-f42.google.com with SMTP id o7so7383520wrg.5 for ; Sat, 18 Mar 2023 17:20:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1679185220; 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=ML/fjqBQiFdniaLp2U4iIoVxIHX7dLvD1UO1EbWAfgg=; b=n3/WQOG/ih4PqFSwkZOumfiFVTaTsKhtI63BBDGI+/vv/zPkXwXwDN0ApMTMTk8SYB ZMBRckYTo15ECbqaHnrBo48DrtOZUTNYDSw8c2T1sKBr5u2pwlk3r9mfSp26dS+X7GiD hT0ZMmPNSr5XADXvConvouz1CmLldien5XgSGZJMS+0yZw8l6uxPhbwG5PXY7KrqrM5C pMxQhA41a0VwFVargL4lvGa73JGXOiX13ei56eMROD8jAJ7I7o5+7YinueUteFCHDPXM 230nGcCMkJr4sHIdUsxmxEX2nkRrEyhgq3V6csMtQPw4EcM45xMSJc3lLbFfE8xRTgJJ bZCQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679185220; 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=ML/fjqBQiFdniaLp2U4iIoVxIHX7dLvD1UO1EbWAfgg=; b=324dJhy+ISYGGem6VZv6LRlcA3g1sVKT1MTveBAkSMtdXFcJHgT2oqfeUFyblqe0UC ed1gd0aY/fqBU7wPE52plMs8aKGYNyz5j3x2x9pgA9ovNewVE4H+LlBUiuVF5iY3uigo pdBxDpnzUtQ49YLrUFRpdmZ861u3Aip/ZyTuGBOt5TcQEe+m58gx16T+Voek5Bim3Uf5 4q69YXLi1N5n8EhBy0NMb0t2CpFw7eTuVG0Vn+yBwwFIV1NJgBxqi82+QXrpfw+UMwjk II5beI+mwvsXETvB808nKIVgUVd5NGqCS6gjVAgeGyDLStQ0w6Rs+cdH6+18BPzBAa5i RsDw== X-Gm-Message-State: AO0yUKX3/L4CkAytMcLOqUQD+5DS0G6JACaYm1iz3XN/3ooPuEt99yia tEV/x5+JM98yo1U4cjGdVSm8/5tBBVQ= X-Google-Smtp-Source: AK7set95d6yiRVYGBDcNsU8nn/6kRPj65SWvFG+EDet5fLk/OpQVCKm3COqIdxAlwCybBefp9ljiDQ== X-Received: by 2002:a5d:54c1:0:b0:2d3:fba4:e61d with SMTP id x1-20020a5d54c1000000b002d3fba4e61dmr4877232wrv.12.1679185220032; Sat, 18 Mar 2023 17:20:20 -0700 (PDT) Received: from lucifer.home (host86-146-209-214.range86-146.btcentralplus.com. [86.146.209.214]) by smtp.googlemail.com with ESMTPSA id x14-20020adfdd8e000000b002cff0c57b98sm5399639wrl.18.2023.03.18.17.20.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 18 Mar 2023 17:20:19 -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 , Lorenzo Stoakes Subject: [PATCH 3/4] fs/proc/kcore: convert read_kcore() to read_kcore_iter() Date: Sun, 19 Mar 2023 00:20:11 +0000 Message-Id: <32f8fad50500d0cd0927a66638c5890533725d30.1679183626.git.lstoakes@gmail.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: References: MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Queue-Id: BACC21C0008 X-Rspamd-Server: rspam01 X-Stat-Signature: 7ei57e1mqtsfc9mamtm53yzr4s8mse4a X-HE-Tag: 1679185221-311070 X-HE-Meta: U2FsdGVkX18tdt85jSyGxDUlIQUAtnhNuZFHDNkMJKBk97oZS0ykxlT2gLOpxQ87k0hi9n0ZZ/JG8HsNUOFZLac4SCHaFlWke4bUpgTNtJ0r/bEMPXUNzQ6OPocE5NCF8dF2fX2jQNt3jZLt0bInftmu9zlQaWh8TJmcJSvZW7Sme2h7WTwwFr0nYtDDNNSYbTQVv9qhVHs5pJGBXggUnI8cJH2nZXbymWgGn/EIEIxXL7b4ieJc19ojSTGlEmmj3y2opEs68L9gEiR5Qam+J5f5Ql3EKTFja7obAdHUEBFWqYWfrIrARvJU7OBuThY8xAA5PmcuIV/LYP0znz6M4M7GHReYdyExp3EvE9v6WMxQQUbX6mTmum9mKSEa10yRw6pqPPEWb6ssnLHLUOhDEyhhPhejl4uSpk2JDZ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FLpx X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Now we have eliminated spinlocks from the vread() case, convert read_kcore() to read_kcore_iter(). 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 --- fs/proc/kcore.c | 58 ++++++++++++++++++++++++------------------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/fs/proc/kcore.c b/fs/proc/kcore.c index 556f310d6aa4..25e0eeb8d498 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 *ppos = &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; @@ -333,7 +337,7 @@ read_kcore(struct file *file, char __user *buffer, size_t buflen, loff_t *fpos) notes_offset = phdrs_offset + phdrs_len; /* ELF file header. */ - if (buflen && *fpos < sizeof(struct elfhdr)) { + if (buflen && *ppos < sizeof(struct elfhdr)) { struct elfhdr ehdr = { .e_ident = { [EI_MAG0] = ELFMAG0, @@ -355,19 +359,18 @@ read_kcore(struct file *file, char __user *buffer, size_t buflen, loff_t *fpos) .e_phnum = nphdr, }; - tsz = min_t(size_t, buflen, sizeof(struct elfhdr) - *fpos); - if (copy_to_user(buffer, (char *)&ehdr + *fpos, tsz)) { + tsz = min_t(size_t, buflen, sizeof(struct elfhdr) - *ppos); + if (copy_to_iter((char *)&ehdr + *ppos, tsz, iter) != tsz) { ret = -EFAULT; goto out; } - buffer += tsz; buflen -= tsz; - *fpos += tsz; + *ppos += tsz; } /* ELF program headers. */ - if (buflen && *fpos < phdrs_offset + phdrs_len) { + if (buflen && *ppos < phdrs_offset + phdrs_len) { struct elf_phdr *phdrs, *phdr; phdrs = kzalloc(phdrs_len, GFP_KERNEL); @@ -397,22 +400,21 @@ read_kcore(struct file *file, char __user *buffer, size_t buflen, loff_t *fpos) phdr++; } - tsz = min_t(size_t, buflen, phdrs_offset + phdrs_len - *fpos); - if (copy_to_user(buffer, (char *)phdrs + *fpos - phdrs_offset, - tsz)) { + tsz = min_t(size_t, buflen, phdrs_offset + phdrs_len - *ppos); + if (copy_to_iter((char *)phdrs + *ppos - phdrs_offset, tsz, + iter) != tsz) { kfree(phdrs); ret = -EFAULT; goto out; } kfree(phdrs); - buffer += tsz; buflen -= tsz; - *fpos += tsz; + *ppos += tsz; } /* ELF note segment. */ - if (buflen && *fpos < notes_offset + notes_len) { + if (buflen && *ppos < notes_offset + notes_len) { struct elf_prstatus prstatus = {}; struct elf_prpsinfo prpsinfo = { .pr_sname = 'R', @@ -447,24 +449,23 @@ read_kcore(struct file *file, char __user *buffer, size_t buflen, loff_t *fpos) vmcoreinfo_data, 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)) { + tsz = min_t(size_t, buflen, notes_offset + notes_len - *ppos); + if (copy_to_iter(notes + *ppos - notes_offset, tsz, iter) != tsz) { kfree(notes); ret = -EFAULT; goto out; } kfree(notes); - buffer += tsz; buflen -= tsz; - *fpos += tsz; + *ppos += tsz; } /* * Check to see if our file offset matches with any of * the addresses in the elf_phdr on our list. */ - start = kc_offset_to_vaddr(*fpos - data_offset); + start = kc_offset_to_vaddr(*ppos - data_offset); if ((tsz = (PAGE_SIZE - (start & ~PAGE_MASK))) > buflen) tsz = buflen; @@ -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,25 +542,24 @@ 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; } } skip: buflen -= tsz; - *fpos += tsz; - buffer += tsz; + *ppos += 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,