From patchwork Thu Mar 23 10:15:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Stoakes X-Patchwork-Id: 13185412 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 B0F8DC76195 for ; Thu, 23 Mar 2023 10:15:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BE4EE6B007B; Thu, 23 Mar 2023 06:15:31 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B43716B007D; Thu, 23 Mar 2023 06:15:31 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9215F6B007E; Thu, 23 Mar 2023 06:15:31 -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 77ACD6B007B for ; Thu, 23 Mar 2023 06:15:31 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 4F29316043C for ; Thu, 23 Mar 2023 10:15:31 +0000 (UTC) X-FDA: 80599756062.13.B40B99B Received: from mail-wm1-f42.google.com (mail-wm1-f42.google.com [209.85.128.42]) by imf19.hostedemail.com (Postfix) with ESMTP id 712101A0018 for ; Thu, 23 Mar 2023 10:15:29 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=cbMJrHwi; spf=pass (imf19.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.128.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=1679566529; 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=2AsCFAqSuw0gM0mvXGTtPyda1fJMyndkAzm+ZfOwetk=; b=VwyVtei7QT9tDf0NQSwaIvqnsw62fIvyxub4QWBfgigKGh6bJ6sEubyBZenCyu9ScOQx37 Bo2OIHkE8aiAp0NDh/WaVYeU18wuJnyRBRBqps00MA2Ae+/BznxWxicOeMCwhrpV0sE2Mq yzZtrhaArW/9ycSvn7riSf9sf1upFT4= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=cbMJrHwi; spf=pass (imf19.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.128.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=1679566529; a=rsa-sha256; cv=none; b=pgu1AZwhUkHlo1lHT2x9m3kAACnFpCQs7JIoI2Yljp52tBL9158qMhd3Ap7v1C2QM98BmB DfYa87gWCzIbDmGjJdm5QzcTJ5E0yOK+pLPIzReWxutwNzDZ+vN3mX3pVCukSb/+RomkYT MMpa104aqarVxztq6ctZ8GzXCypTdXo= Received: by mail-wm1-f42.google.com with SMTP id v20-20020a05600c471400b003ed8826253aso1676189wmo.0 for ; Thu, 23 Mar 2023 03:15:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1679566528; 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=2AsCFAqSuw0gM0mvXGTtPyda1fJMyndkAzm+ZfOwetk=; b=cbMJrHwix1y5yZ0Xw0buwAI/xzyEm8deulV4S0oYi2A/MSWlHE6FhmxwGtUM6BZ65y zs1Zcfr7/Bh5b14jhamR1yRkTF6BdGSKVhO0WAVtBXg+5x5nExAx+0rsf3NKQXcSAYQA gS1049zFR9kJzvhqZcNXSXOu6OUfXQVh2WsoMCEQQfJyptgqrw+sjmDe45N4e088vXhU AyCYpiIXPMmepZ9kSyVxWYh3hHuzRCiKpke4DGlQxW0FL1UvFxJcBCyUNLA8pSe0c9U2 dRfIKLqg5mqj0J9iPxAQhKcb04ky0jnRVgr80wu8peMa5qFXfmXFstuVnppurq0aXHa0 5qkQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679566528; 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=2AsCFAqSuw0gM0mvXGTtPyda1fJMyndkAzm+ZfOwetk=; b=xZSx0H6xGUXRga8sMQ0fn3TFLjCox2hbv0hRQrhYNh5i+D+IJG/z6BNCL+0ZKX1eOU eMqLGBomJm9li/OlhPXNb4h/vRMRwOpDmVGVMYiW5Av9jsMOJuZauzxO9p0xADV13uy8 HN9xKO+l8QFeNYSeqwLRX788d5Alv6BUaAk51RdUdF/E4xzLEISXRZoavInWwXqTZZG2 trOjrlws3Yx2jGVd1mZBvl89VHPxk5uHZjqnHBf+5m35zdbB1Pxbla/yFIzPvBaNnwP5 R9rnwibO9frMTTmFD1kyucgJUHHj1KIpLj0mUA9Iosgn6nkf8y9y3U0JVvRzbu8v2r/E h5MA== X-Gm-Message-State: AO0yUKWDk0X+97mi+McbdRThjd/Dyq/eEp+yzoq+JNiT1JmTeCrFFx2W 8c8zaCsNdXMx8LVQRPo81a8/hePAxLs= X-Google-Smtp-Source: AK7set/KXLvkspouh0TI8jR9VpZAV5euedYHcvEgky1WQvZjPi8rd3teXOJUoBJuhzHCuA4JJLYh3g== X-Received: by 2002:a05:600c:2102:b0:3ed:245f:97a with SMTP id u2-20020a05600c210200b003ed245f097amr1787883wml.19.1679566527809; Thu, 23 Mar 2023 03:15:27 -0700 (PDT) Received: from lucifer.home (host86-156-84-164.range86-156.btcentralplus.com. [86.156.84.164]) by smtp.googlemail.com with ESMTPSA id f18-20020a05600c155200b003ede2c59a54sm1416952wmg.37.2023.03.23.03.15.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Mar 2023 03:15:27 -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 v8 4/4] mm: vmalloc: convert vread() to vread_iter() Date: Thu, 23 Mar 2023 10:15:19 +0000 Message-Id: <8506cbc667c39205e65a323f750ff9c11a463798.1679566220.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-Server: rspam03 X-Stat-Signature: giahcpjmox3fojoqpyfce3zcck7hj99y X-Rspamd-Queue-Id: 712101A0018 X-HE-Tag: 1679566529-645631 X-HE-Meta: U2FsdGVkX1/Wm4yfej69QEEWAImA0aOwYpJTcjuJjT7vri7/i38mhklnfdNSmZniM7K4ds3UxAu3HtmFVunpEIOCv7SjvpJSDJgYf3/+AHMg4VG4+cSZzoYRb9anKv5ZoRObZJzzuHnRssT5zmY29lJGMWHl+0wlYqi3bJmUqqoX3jBTd309zLyNsZb3PKXG3N2KfB2WEBrr5gdbKpIcVO20awUl/MLQYe30SPELnwUD3OtKQapwQTo/VywM3oiabySB9e11uWKvuR9ti+3vQka6hZCMSW1IjlstLk0b4xbS1A+BbCB3VJFtSpYF/kp8/MLmvOMyN6i9mt85lmDAStVIRp7Xrymm6aUsfQrVl8PIo9J1arH22mgKC8yvoOywA2JjXNd/LB6I3yJJDtExFBBUNR5Whh0V7DSZXOgaeufeodx4occeCGmcgQ5bBlDJsDkZFldLOEPWDDJsr6L6vr1Yfq/G/qgO5dAjFjgZ4RFZIjDjTxOvsm+vJg/cvBLIAHvfoZ0XTDmv9MmFsNjSk5mOZsdcNdWn/0LzpvICc3TVB4TqK1qcSWX2lOXmiOhRRVakqjAQJ8b3pAACRj2fVd6hkKwQCIffTMVAvU1YFKf5DA+cVXddXTX6wO4q2rSVYLxypR9/dJho/KBPgfDLg6tzC1vRQ12eHP0kyIO55X0QRwX1qEQ7rOOV5W22gNvkz8vvmw7X77H49yR4g4dT90CT0DspdBrLVBEuwJyJwdyICJHv+7KA68/xRceFm5t5QmDSd6QOR63olgyNZqn5xZPUNRa6Q+frtpW3uxHWSuBotzcj1GQmj5m532knXvHyK0j3IG3ZLMiqsX3zbdOt1WeW/WNR1gDiCmhJZ8NFYt5eotBDK9irzY6hdkbV+loyFhchBpjzUpGFY5+VfZKfRACmghRHEFV+rTuJy/wYw0ueWdQrKsaiWh+QI+gNyWglYFMpy7pzGpGDxYTJWuc kaevrYt6 AykYbrN9VTfZ94rT5u3z6v4DGkOsDNyIvmeSUPY6l15FrX+GakcLCrmIxJXFPfn9sJy2gPK279b0NqMmJVBOz1zuKKF4BWwiRdZc7c/Wm8H59Y5WuwGlhGAXYiSqYe0PdsAfQkwHX4i5HDHpze4wz+OWOb9OAj/o5+qyJhiYYMZfgkZeAZuAw74gKjk+tFzpFgcNZY8+utR4RU6xV3fRiBkAxUYIKy2aXUE2RhG0xVfuRbcbz3Y4PdT4/WO5xVSHtByr2oBZVFZbXz9xIvesOKi0l4tI4ttUjmdqhgthKXaZgCTEwepcdwU9Smi3kdUHyj+QiWLHb5sMnYc7jlfzQ16y201d/NTV3H0Nqm/aExX44B1w5zFlIl/7ig6T/MQsbUEYJ4lxzGEoKZmcWquiqLaPE2ymxfjJVisKXBCAnEurTJigHrlWo3WLceSwBrSBdo9c5yKC19FPrCeXsopRNV3EVqX0ZhMmW9oHhSQz0kuQx0XLnMGvAbmk5IqzsPUr4AWPF 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: Having previously laid the foundation for converting vread() to an iterator function, pull the trigger and do so. This patch attempts to provide minimal refactoring and to reflect the existing logic as best we can, for example we continue to zero portions of memory not read, as before. Overall, there should be no functional difference other than a performance improvement in /proc/kcore access to vmalloc regions. Now we have eliminated the need for a bounce buffer in read_kcore_iter(), we dispense with it, and try to write to user memory optimistically but with faults disabled via copy_page_to_iter_nofault(). We already have preemption disabled by holding a spin lock. We continue faulting in until the operation is complete. Additionally, we must account for the fact that at any point a copy may fail (most likely due to a fault not being able to occur), we exit indicating fewer bytes retrieved than expected. Signed-off-by: Lorenzo Stoakes --- fs/proc/kcore.c | 44 ++++---- include/linux/vmalloc.h | 3 +- mm/nommu.c | 10 +- mm/vmalloc.c | 234 +++++++++++++++++++++++++--------------- 4 files changed, 176 insertions(+), 115 deletions(-) diff --git a/fs/proc/kcore.c b/fs/proc/kcore.c index 08b795fd80b4..25b44b303b35 100644 --- a/fs/proc/kcore.c +++ b/fs/proc/kcore.c @@ -307,13 +307,9 @@ static void append_kcore_note(char *notes, size_t *i, const char *name, *i = ALIGN(*i + descsz, 4); } -static ssize_t -read_kcore_iter(struct kiocb *iocb, struct iov_iter *iter) +static ssize_t 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; @@ -507,13 +503,30 @@ read_kcore_iter(struct kiocb *iocb, struct iov_iter *iter) switch (m->type) { case KCORE_VMALLOC: - vread(buf, (char *)start, tsz); - /* we have to zero-fill user buffer even if no read */ - if (copy_to_iter(buf, tsz, iter) != tsz) { - ret = -EFAULT; - goto out; + { + const char *src = (char *)start; + size_t read = 0, left = tsz; + + /* + * vmalloc uses spinlocks, so we optimistically try to + * read memory. If this fails, fault pages in and try + * again until we are done. + */ + while (true) { + read += vread_iter(iter, src, left); + if (read == tsz) + break; + + src += read; + left -= read; + + if (fault_in_iov_iter_writeable(iter, left)) { + ret = -EFAULT; + goto out; + } } break; + } case KCORE_USER: /* User page is handled prior to normal kernel page: */ if (copy_to_iter((char *)start, tsz, iter) != tsz) { @@ -582,10 +595,6 @@ static int open_kcore(struct inode *inode, struct file *filp) if (ret) return ret; - filp->private_data = kmalloc(PAGE_SIZE, GFP_KERNEL); - if (!filp->private_data) - return -ENOMEM; - if (kcore_need_update) kcore_update_ram(); if (i_size_read(inode) != proc_root_kcore->size) { @@ -596,16 +605,9 @@ static int open_kcore(struct inode *inode, struct file *filp) return 0; } -static int release_kcore(struct inode *inode, struct file *file) -{ - kfree(file->private_data); - return 0; -} - static const struct proc_ops kcore_proc_ops = { .proc_read_iter = read_kcore_iter, .proc_open = open_kcore, - .proc_release = release_kcore, .proc_lseek = default_llseek, }; diff --git a/include/linux/vmalloc.h b/include/linux/vmalloc.h index 69250efa03d1..461aa5637f65 100644 --- a/include/linux/vmalloc.h +++ b/include/linux/vmalloc.h @@ -9,6 +9,7 @@ #include /* pgprot_t */ #include #include +#include #include @@ -251,7 +252,7 @@ static inline void set_vm_flush_reset_perms(void *addr) #endif /* for /proc/kcore */ -extern long vread(char *buf, char *addr, unsigned long count); +extern long vread_iter(struct iov_iter *iter, const char *addr, size_t count); /* * Internals. Don't use.. diff --git a/mm/nommu.c b/mm/nommu.c index 57ba243c6a37..f670d9979a26 100644 --- a/mm/nommu.c +++ b/mm/nommu.c @@ -36,6 +36,7 @@ #include #include +#include #include #include #include @@ -198,14 +199,13 @@ unsigned long vmalloc_to_pfn(const void *addr) } EXPORT_SYMBOL(vmalloc_to_pfn); -long vread(char *buf, char *addr, unsigned long count) +long vread_iter(struct iov_iter *iter, const char *addr, size_t count) { /* Don't allow overflow */ - if ((unsigned long) buf + count < count) - count = -(unsigned long) buf; + if ((unsigned long) addr + count < count) + count = -(unsigned long) addr; - memcpy(buf, addr, count); - return count; + return copy_to_iter(addr, count, iter); } /* diff --git a/mm/vmalloc.c b/mm/vmalloc.c index 978194dc2bb8..2aaa9382605c 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -37,7 +37,6 @@ #include #include #include -#include #include #include #include @@ -3442,62 +3441,96 @@ void *vmalloc_32_user(unsigned long size) EXPORT_SYMBOL(vmalloc_32_user); /* - * small helper routine , copy contents to buf from addr. - * If the page is not present, fill zero. + * Atomically zero bytes in the iterator. + * + * Returns the number of zeroed bytes. */ +static size_t zero_iter(struct iov_iter *iter, size_t count) +{ + size_t remains = count; + + while (remains > 0) { + size_t num, copied; + + num = remains < PAGE_SIZE ? remains : PAGE_SIZE; + copied = copy_page_to_iter_nofault(ZERO_PAGE(0), 0, num, iter); + remains -= copied; + + if (copied < num) + break; + } -static int aligned_vread(char *buf, char *addr, unsigned long count) + return count - remains; +} + +/* + * small helper routine, copy contents to iter from addr. + * If the page is not present, fill zero. + * + * Returns the number of copied bytes. + */ +static size_t aligned_vread_iter(struct iov_iter *iter, + const char *addr, size_t count) { - struct page *p; - int copied = 0; + size_t remains = count; + struct page *page; - while (count) { + while (remains > 0) { unsigned long offset, length; + size_t copied = 0; offset = offset_in_page(addr); length = PAGE_SIZE - offset; - if (length > count) - length = count; - p = vmalloc_to_page(addr); + if (length > remains) + length = remains; + page = vmalloc_to_page(addr); /* - * To do safe access to this _mapped_ area, we need - * lock. But adding lock here means that we need to add - * overhead of vmalloc()/vfree() calls for this _debug_ - * interface, rarely used. Instead of that, we'll use - * kmap() and get small overhead in this access function. + * To do safe access to this _mapped_ area, we need lock. But + * adding lock here means that we need to add overhead of + * vmalloc()/vfree() calls for this _debug_ interface, rarely + * used. Instead of that, we'll use an local mapping via + * copy_page_to_iter_nofault() and accept a small overhead in + * this access function. */ - if (p) { - /* We can expect USER0 is not used -- see vread() */ - void *map = kmap_atomic(p); - memcpy(buf, map + offset, length); - kunmap_atomic(map); - } else - memset(buf, 0, length); + if (page) + copied = copy_page_to_iter_nofault(page, offset, + length, iter); + else + copied = zero_iter(iter, length); - addr += length; - buf += length; - copied += length; - count -= length; + addr += copied; + remains -= copied; + + if (copied != length) + break; } - return copied; + + return count - remains; } -static void vmap_ram_vread(char *buf, char *addr, int count, unsigned long flags) +/* + * Read from a vm_map_ram region of memory. + * + * Returns the number of copied bytes. + */ +static size_t vmap_ram_vread_iter(struct iov_iter *iter, const char *addr, + size_t count, unsigned long flags) { char *start; struct vmap_block *vb; unsigned long offset; - unsigned int rs, re, n; + unsigned int rs, re; + size_t remains, n; /* * If it's area created by vm_map_ram() interface directly, but * not further subdividing and delegating management to vmap_block, * handle it here. */ - if (!(flags & VMAP_BLOCK)) { - aligned_vread(buf, addr, count); - return; - } + if (!(flags & VMAP_BLOCK)) + return aligned_vread_iter(iter, addr, count); + + remains = count; /* * Area is split into regions and tracked with vmap_block, read out @@ -3505,50 +3538,64 @@ static void vmap_ram_vread(char *buf, char *addr, int count, unsigned long flags */ vb = xa_load(&vmap_blocks, addr_to_vb_idx((unsigned long)addr)); if (!vb) - goto finished; + goto finished_zero; spin_lock(&vb->lock); if (bitmap_empty(vb->used_map, VMAP_BBMAP_BITS)) { spin_unlock(&vb->lock); - goto finished; + goto finished_zero; } + for_each_set_bitrange(rs, re, vb->used_map, VMAP_BBMAP_BITS) { - if (!count) - break; + size_t copied; + + if (remains == 0) + goto finished; + start = vmap_block_vaddr(vb->va->va_start, rs); - while (addr < start) { - if (count == 0) - goto unlock; - *buf = '\0'; - buf++; - addr++; - count--; + + if (addr < start) { + size_t to_zero = min_t(size_t, start - addr, remains); + size_t zeroed = zero_iter(iter, to_zero); + + addr += zeroed; + remains -= zeroed; + + if (remains == 0 || zeroed != to_zero) + goto finished; } + /*it could start reading from the middle of used region*/ offset = offset_in_page(addr); n = ((re - rs + 1) << PAGE_SHIFT) - offset; - if (n > count) - n = count; - aligned_vread(buf, start+offset, n); + if (n > remains) + n = remains; + + copied = aligned_vread_iter(iter, start + offset, n); - buf += n; - addr += n; - count -= n; + addr += copied; + remains -= copied; + + if (copied != n) + goto finished; } -unlock: + spin_unlock(&vb->lock); -finished: +finished_zero: /* zero-fill the left dirty or free regions */ - if (count) - memset(buf, 0, count); + return count - remains + zero_iter(iter, remains); +finished: + /* We couldn't copy/zero everything */ + spin_unlock(&vb->lock); + return count - remains; } /** - * vread() - read vmalloc area in a safe way. - * @buf: buffer for reading data - * @addr: vm address. - * @count: number of bytes to be read. + * vread_iter() - read vmalloc area in a safe way to an iterator. + * @iter: the iterator to which data should be written. + * @addr: vm address. + * @count: number of bytes to be read. * * This function checks that addr is a valid vmalloc'ed area, and * copy data from that area to a given buffer. If the given memory range @@ -3568,13 +3615,12 @@ static void vmap_ram_vread(char *buf, char *addr, int count, unsigned long flags * (same number as @count) or %0 if [addr...addr+count) doesn't * include any intersection with valid vmalloc area */ -long vread(char *buf, char *addr, unsigned long count) +long vread_iter(struct iov_iter *iter, const char *addr, size_t count) { struct vmap_area *va; struct vm_struct *vm; - char *vaddr, *buf_start = buf; - unsigned long buflen = count; - unsigned long n, size, flags; + char *vaddr; + size_t n, size, flags, remains; addr = kasan_reset_tag(addr); @@ -3582,18 +3628,22 @@ long vread(char *buf, char *addr, unsigned long count) if ((unsigned long) addr + count < count) count = -(unsigned long) addr; + remains = count; + spin_lock(&vmap_area_lock); va = find_vmap_area_exceed_addr((unsigned long)addr); if (!va) - goto finished; + goto finished_zero; /* no intersects with alive vmap_area */ - if ((unsigned long)addr + count <= va->va_start) - goto finished; + if ((unsigned long)addr + remains <= va->va_start) + goto finished_zero; list_for_each_entry_from(va, &vmap_area_list, list) { - if (!count) - break; + size_t copied; + + if (remains == 0) + goto finished; vm = va->vm; flags = va->flags & VMAP_FLAGS_MASK; @@ -3608,6 +3658,7 @@ long vread(char *buf, char *addr, unsigned long count) if (vm && (vm->flags & VM_UNINITIALIZED)) continue; + /* Pair with smp_wmb() in clear_vm_uninitialized_flag() */ smp_rmb(); @@ -3616,38 +3667,45 @@ long vread(char *buf, char *addr, unsigned long count) if (addr >= vaddr + size) continue; - while (addr < vaddr) { - if (count == 0) + + if (addr < vaddr) { + size_t to_zero = min_t(size_t, vaddr - addr, remains); + size_t zeroed = zero_iter(iter, to_zero); + + addr += zeroed; + remains -= zeroed; + + if (remains == 0 || zeroed != to_zero) goto finished; - *buf = '\0'; - buf++; - addr++; - count--; } + n = vaddr + size - addr; - if (n > count) - n = count; + if (n > remains) + n = remains; if (flags & VMAP_RAM) - vmap_ram_vread(buf, addr, n, flags); + copied = vmap_ram_vread_iter(iter, addr, n, flags); else if (!(vm->flags & VM_IOREMAP)) - aligned_vread(buf, addr, n); + copied = aligned_vread_iter(iter, addr, n); else /* IOREMAP area is treated as memory hole */ - memset(buf, 0, n); - buf += n; - addr += n; - count -= n; + copied = zero_iter(iter, n); + + addr += copied; + remains -= copied; + + if (copied != n) + goto finished; } -finished: - spin_unlock(&vmap_area_lock); - if (buf == buf_start) - return 0; +finished_zero: + spin_unlock(&vmap_area_lock); /* zero-fill memory holes */ - if (buf != buf_start + buflen) - memset(buf, 0, buflen - (buf - buf_start)); + return count - remains + zero_iter(iter, remains); +finished: + /* Nothing remains, or We couldn't copy/zero everything. */ + spin_unlock(&vmap_area_lock); - return buflen; + return count - remains; } /**