From patchwork Sun Mar 19 07:09:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Stoakes X-Patchwork-Id: 13180242 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 12F64C7619A for ; Sun, 19 Mar 2023 07:09:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 43EDC900006; Sun, 19 Mar 2023 03:09:47 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3C849900002; Sun, 19 Mar 2023 03:09:47 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1F328900006; Sun, 19 Mar 2023 03:09:47 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 0F039900002 for ; Sun, 19 Mar 2023 03:09:47 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id DAD5EC0C69 for ; Sun, 19 Mar 2023 07:09:46 +0000 (UTC) X-FDA: 80584772772.07.DE78D27 Received: from mail-ed1-f50.google.com (mail-ed1-f50.google.com [209.85.208.50]) by imf22.hostedemail.com (Postfix) with ESMTP id 059E5C0017 for ; Sun, 19 Mar 2023 07:09:44 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=Z5A4SYAM; spf=pass (imf22.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.208.50 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=1679209785; 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=oc2hEUT1Mq56uGMxgJWFa8j7P+EXF0Lnqv/90BLNrOkMI3fdtsNa0J1egh1mPUsLrklWU6 p4BZr1isLvj4GzTVgsMlZEbVWL7SclQYDpxsg2QJHOMJ6tv1QgJq7D2v+BxiT4oLx4Zqbr KUU7YWb8BrLuyt1jaSbue4ACpQdd6oQ= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=Z5A4SYAM; spf=pass (imf22.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.208.50 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=1679209785; a=rsa-sha256; cv=none; b=Xf4nk2CCNcolnnXYUQvYfMF8yzD/6znZQVp/RuPMhD2+xAYMsYNzPuzK9Eb88DkO7V8Oep 7/z3W3hr7uSxaw0SOvmA7oozcWaJiiFboX8qOYOqEyvLlZpY5YEB9GUTu1BCwqy0X+M24r 8yg9NuiD3oVFs5QIHFG4euLeikz0EfE= Received: by mail-ed1-f50.google.com with SMTP id z21so35483747edb.4 for ; Sun, 19 Mar 2023 00:09:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1679209783; 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=Z5A4SYAMeoS3RR8Z4VWJcLUiygTK9MPz2hpEcGzak+h9lDhV9fLONDhT9zbE2C+yNg 8rv7BvOpHO0Sx3zejcitLQz6k0a4MVQIxsp9pkabAiX3qbecdxCSYy1n/OTf5RrL2ibo imKjO+t9688AnjvhOoFAdoaDlUwgG7oddRgRcNPVlJEtwMAZHJUFCIouxn9LGFRg5sPp 3specuIgov4Tk2AoSaONSPqii/LRvwchK3NxsaP4ID8Jpud36J7LBJY7/MyOYZ+Yryl/ eLLC8ao/emzfoxD23mCr58ZoewzZOJcncj0ZpZrlVsnuTdhu6B12vmmKBw5twY17tH8n 24xg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679209783; 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=5eNosSEf137Pzyt+BTimLWQNM7eNsYJHs/9oa+YsbHpdw1Ngk+V7LlTS58TWz35afW kFL5t1pZrQn///CuzPFtTiuKFycSWPQ3jDVO57pQ0zc3Tr1nb4bORFeGft6SNIBzd6aB BSKYU/abG0kxRYu7d9EYkQQSr6/TNAPU7ZjN2pXH6RLKxFyT/bWPyY6ArfaSubnwpONT tOTVrgSMirlb8EFal6g5ha7swKv5/GZO1aLo6A/D3qODbDr0mYC5mJNVQUokhyJibvgj 09JnXmlxZJJG9tHBqyt3dTP51IuLV4e6vDiA4T1xonk8TFbnXGe9M+B3CMVdYBsA07NY CNqg== X-Gm-Message-State: AO0yUKXWBkKqLB5FS/d7t3RsQFVVlnjEgEh8Sjc7bu5HGOYW0e/lPCww SoE7McZ/DH+9q77SRUFF/Hx095tcjpA= X-Google-Smtp-Source: AK7set+MDLQIHRjSLb5brGnPCVCIAh7+aXOeit8OMTHh4Z0ogtj8/iZA2kquj6wEYzeRrXr2kOJBAQ== X-Received: by 2002:a17:907:3e0e:b0:933:3da2:436 with SMTP id hp14-20020a1709073e0e00b009333da20436mr4510936ejc.54.1679209783409; Sun, 19 Mar 2023 00:09:43 -0700 (PDT) Received: from localhost.localdomain ([2a00:23ee:1938:1bcd:c6e1:42ba:ae87:772e]) by smtp.googlemail.com with ESMTPSA id u8-20020a170906b10800b008c9b44b7851sm2943920ejy.182.2023.03.19.00.09.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 19 Mar 2023 00:09:42 -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 v2 3/4] fs/proc/kcore: convert read_kcore() to read_kcore_iter() Date: Sun, 19 Mar 2023 07:09:32 +0000 Message-Id: <32f8fad50500d0cd0927a66638c5890533725d30.1679209395.git.lstoakes@gmail.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Queue-Id: 059E5C0017 X-Stat-Signature: y1dek81ckgo8tuda4ddaeetixtojbona X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1679209784-129211 X-HE-Meta: U2FsdGVkX18AyVmgGHD007OMvyvK8d7fm1sdqgY/2cL+hsLkQjtmkctXmaE2vPakFoQ9R17vY/GhCS0/2ryQxEkFcCoDBpLPao8W0qLI29RfuyJmefptONipP92gCkvjA9t2MU6yME+I8GgSwbR553qpVRob3hdDwKqQdE7hHrwFKQde6OdWQKHWA2ZtSU9ZmZw2CfMOcdU8o3KDHk6t8+ZvkNAZBwUMIse+yDIeLG1uMWOYz8+AtNRDBm/8U23nfqRgeE8J+Q4+C/o6tFGsFac4jSEaDyjFQ/6veO+utO5+SF3dR+Ydo6eKasuei4u+Ubi4m/YgsMzyCbwvvBdmH8EnLZgJzBBAY/Y8t7BQdC5Vb4HpVBHiaKYmWCnhiuQDMzaT0WlvqSbiedk+JYQsYe0swhXera3yBC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=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,