From patchwork Mon Mar 20 23:42:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Stoakes X-Patchwork-Id: 13182034 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 1F77AC6FD1D for ; Mon, 20 Mar 2023 23:43:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 76FA66B007B; Mon, 20 Mar 2023 19:43:05 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 721B66B007D; Mon, 20 Mar 2023 19:43:05 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5C14B6B007E; Mon, 20 Mar 2023 19:43:05 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 4BA0F6B007B for ; Mon, 20 Mar 2023 19:43:05 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 11780121036 for ; Mon, 20 Mar 2023 23:43:05 +0000 (UTC) X-FDA: 80590904730.09.631001E Received: from mail-wm1-f52.google.com (mail-wm1-f52.google.com [209.85.128.52]) by imf06.hostedemail.com (Postfix) with ESMTP id 1B3FE180008 for ; Mon, 20 Mar 2023 23:43:02 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b="M6jtlC5/"; spf=pass (imf06.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.128.52 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=1679355783; 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=z0civEybDSa1dY1/a3U05vJNHnRsFY7UbdA6Q9UbhdXRI/eYyClnkInoxMbVh09ap5lLop WObxz3Pvp+ImxU6WwaSzs4DpCj/0wmflyAem0emiNVY2ruyntWZ/zQxJUdo2ploDlhfxOA FnRH/G93jkLaXYDBb22/eFXD8Sa64Y4= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b="M6jtlC5/"; spf=pass (imf06.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.128.52 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=1679355783; a=rsa-sha256; cv=none; b=dAkUrEmdvL2L73ipZqJEL3MmF+Q2PT43Olwr5kTGa4EAjZIz0r1tz87S2DTKh7hX5rHwXW XFuRzAOoU9sNGKpbOAR2SFQWl0oZY4csIk1WSDWclqdPlEg0+roIjIdlTDvzeIsvQqyrO5 7Lm3JV+q6NFFMCxqRXmqr92XykL9fg8= Received: by mail-wm1-f52.google.com with SMTP id w11so7489227wmo.2 for ; Mon, 20 Mar 2023 16:43:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1679355781; 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=M6jtlC5/vJmiXbdHsI9oOM2KucMVCzV6haLZB1PopMBrkSD5WieTnBqyr6OrwmIp3g hD9mBRvc4sPWDH6ghBxN3wZkjaCwNBhgB56XKpSt7hbB3SAtbX9Hno/GhOXUp0uSkoJP Zses6gYNWsSR9Qp9Sm+la0vQRrPLiMqZbHMlr8Uo9P1kHOlxXK65TDCYtYMZeQN6VqFg UOQ+I6EP2qTaW1ass6+vTSSOIfhcZ4TlCSPy0ZcunqLgfPbsIedbxmNJtxSB2bj3xtTV oNW+2XBSwS2vwp573I6bwP5NR8wkD7jVfOCYSF5jc4rdofqO4baUH4Jm/tACIHOz3WMs S77g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679355781; 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=mtUKj/xZvYRQVIw9+n4hPNt19CTJl2A5xH/jyhcH3gJd2IBlrTl68jAHWXL6E4NWvT VXgkE9n/eJFyQAKJ+Izmjrx+aDi4ghUp1ms9I9kQXDnnbAJffbgLKiofnEnpgKroPw81 z4BBh8hRDhoDF/qwNuV2JgsRByeG3ee9jNShgyBn6J0Hnmck7mKDHH+4Y2HTQ+bISOsY MxvMpRLs5M2oEgCPswsAd0EFLVp2UVyDBn0DVh0sjsAA0oVw7rKBfHJMOjQGDO9AEXRg gjflpZ88LrdjvRmUNDD9M4FqfQZWWjpxajyDZSmLJ/+BOolQspn1j2WhMD8DYel5Bkjt XvdA== X-Gm-Message-State: AO0yUKXNAWZn01reVRmn5nnj1sAw69fOisZ07c/TFLccIYk4kaf9MFpD +xE3XrcU54l3NFxfaJCCIANBvlGMw40= X-Google-Smtp-Source: AK7set+5IspP0M+LW3yIKnbie5GYRusdTlTtejDJT01Lj/5lgx0jT6pFil+M358oALjvNSCJU/jdmQ== X-Received: by 2002:a7b:c3ce:0:b0:3ed:355c:4610 with SMTP id t14-20020a7bc3ce000000b003ed355c4610mr832789wmj.35.1679355780849; Mon, 20 Mar 2023 16:43:00 -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 u1-20020a05600c440100b003e209186c07sm17504541wmn.19.2023.03.20.16.42.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Mar 2023 16:42:58 -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 v3 2/4] fs/proc/kcore: convert read_kcore() to read_kcore_iter() Date: Mon, 20 Mar 2023 23:42:43 +0000 Message-Id: X-Mailer: git-send-email 2.39.2 In-Reply-To: References: MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam03 X-Stat-Signature: 6qejotpwadjuy4udroupbaza9afsrt31 X-Rspamd-Queue-Id: 1B3FE180008 X-HE-Tag: 1679355782-462550 X-HE-Meta: U2FsdGVkX1/s+LvyqI4nqgiIbq/S/Bnan+7V6ZXZySs5jjL+aRHiImEjkPCeoxQvjYeOcyOxUCOs+yDs5x6Qia45tyhWnaaCaEWSFNdle77z1Lh2YHq+NWSLUB/g55Qk6ast81rI9PsmOr56lglICuOKYswXs0AjuJc8kg+Rs8pPFScSaifZOPFUSq/fgvWC1xWaXBnnUkJsMeZZ8K0zHlHTviV+v5H/vuHLxijK2hynfaN9o6Q06S0LFYiT4vfulqnKeT6z1k/yF4r+9mxCSQps172J63WGAvKhBtYhr+rBX0kReDRRSB9bcSRQ0rCFyRro2HI2BI/ey9VSSv31Kp9vsz2HOmcIDdrLf0CXAoBz50KajkXupEhem+hfbHZdOAi1wplyLXEIwUDlsWxewvZ+ZZhzhqE4faWPrRtYYfI8IEmhbM4YHinELNhrCyCktekNZ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spHkOKLzaIV7OnIc7vHBVCLaOu3cR5Pr2UIO7Y4l1kocHEhzw= 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,