From patchwork Thu Mar 23 10:15:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Stoakes X-Patchwork-Id: 13185409 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 3FE59C76195 for ; Thu, 23 Mar 2023 10:15:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C2EA56B0074; Thu, 23 Mar 2023 06:15:27 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B90ED6B0075; Thu, 23 Mar 2023 06:15:27 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A33616B0078; Thu, 23 Mar 2023 06:15:27 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 972BA6B0074 for ; Thu, 23 Mar 2023 06:15:27 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 6E74C1C4F92 for ; Thu, 23 Mar 2023 10:15:27 +0000 (UTC) X-FDA: 80599755894.29.15EBD46 Received: from mail-wm1-f46.google.com (mail-wm1-f46.google.com [209.85.128.46]) by imf15.hostedemail.com (Postfix) with ESMTP id 8D23BA0015 for ; Thu, 23 Mar 2023 10:15:25 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=qX0NvMt0; spf=pass (imf15.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.128.46 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=1679566525; 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=cY5q38znnygXEk7MQGfhMNT6fWhXSJdRMwyHM9vnLSQ=; b=wcy7d3EbgYxIOpqHZVzC78L7PmbhgtmmbY556xqHFiCzjLKIEHHumyI67GMmItFgyjS/Jv 7ZCRON03f7Jru5RUa2sT0ZTW4/iiUH2bwTH4H2+2HSKUBwWFt1aAdYbSX4XP6flA8/aYb1 gdr2NSH3nieRHIsscgnhlDuWrpmAsvg= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=qX0NvMt0; spf=pass (imf15.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.128.46 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=1679566525; a=rsa-sha256; cv=none; b=NV2L8ppL9lkm2woOKHX6lDDbxMZGQTtFW02NMGv9fsykApLm+dD+B6WomKAVkuDUUkzXij bqe/Q+hoUhEZpyVvT68R9OwHkfsGQRM9oQbr5fHYP2iZr0fCeA4OcZ5FqnfTblsUVVnkDI VeBjk/v/z0A0q24rszX1VsuVGcFBXl0= Received: by mail-wm1-f46.google.com with SMTP id o32so6725173wms.1 for ; Thu, 23 Mar 2023 03:15:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1679566524; 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=cY5q38znnygXEk7MQGfhMNT6fWhXSJdRMwyHM9vnLSQ=; b=qX0NvMt0CqzoY0Lr7UQkxPq2uVe4Zy3EMu6s5/qeG3l29H6nnTeBqXkDfUGl+sMJDi To1Uc1nPNJQYHElIFq5iO8fS+lk/SakYHnFC0dSfIt0VpQY8sLqg09eimJ/7YEqbP1XR VT8JBPbHX06JTtbWOk93ztG/5KD1q6ob99V4FihMHwv2MByqX1Ai3Z6znLATtnv+ksm2 c5FSk9J5/L8z0Co66rkYe1lq/fW2DO7o+YDkp/zAgdPysPTuJd1SALva7Z+nuzGuib3o K2iz/+LuOqaihmm/nmaxKKkG3rhp33yiI0LpXtvNIi6guzXD+7ckLO8ucQMHb28Rp07t X6fw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679566524; 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=cY5q38znnygXEk7MQGfhMNT6fWhXSJdRMwyHM9vnLSQ=; b=0np/hvfD+eWCU5HiXZCbNX7Vw1TLmPQhG2DQGRAWmMHz2fN/YwmvlmSTjCiGzQJEN/ SL6ZZRuSrDSXHy4OaiLaBvYH67uJP/8IDbTzA+b0FCPKh3oWUPXY5XKX4ZjMg0iHuEv6 c4chnosSVZoF2aHRH3q9LXgTBmgfvOgNEPcgy+R2wF2T/kFwTz9jkNySDtmfi8Jb6Wke k6v42Ujoc9JgZ6RqBITDbBl0sSI8ClyBhIItMU8LNnZWPtb1CJ/NhHmI83073b/modoj hOhljzOvFCEHvA+9EinvzgNvLhtPHe4fq/jEBxKpzdPMtZfKmWq7HOdLpX1BgHpCruMd g1PQ== X-Gm-Message-State: AO0yUKXxd/nL3TIAczofb+JHuW95F5tqedT7rV8xu1QZkySIIBDQXBTB 5Wf2V6ebrGZMBiy2Cqw9ll9islli7go= X-Google-Smtp-Source: AK7set8l2mw02Hi+yEM7NEqPCCxbfo5h/cpPyKk3/TXqnMetKEQVjkCEfcASensglEf7x+j3LK9Iiw== X-Received: by 2002:a05:600c:b43:b0:3ed:2eb5:c2e8 with SMTP id k3-20020a05600c0b4300b003ed2eb5c2e8mr1890205wmr.10.1679566523681; Thu, 23 Mar 2023 03:15:23 -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.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Mar 2023 03:15:22 -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 1/4] fs/proc/kcore: avoid bounce buffer for ktext data Date: Thu, 23 Mar 2023 10:15:16 +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: jw3meghe8jz6azdosko5ytuccoxujj3b X-Rspamd-Queue-Id: 8D23BA0015 X-HE-Tag: 1679566525-473851 X-HE-Meta: U2FsdGVkX196WUU95keuphxkRSHlNW8sz+J3obxZ+t3UxaGiYvqSdJgr/X4rcKDM5wk20qRTqpzRt46ajQCAHFAuZXbZYH8POwExMhwcfA2vEk8KfwAfO7Sf37tWZCLO3I6fRO85/5gnIqXfi9nZJkKhMD985U/ZvqZzInddPIeM9dLSwSCX7MVYsGWs7VC2a9UHhp40dc0zNPUUpLqUNWzD7jjmnpgQPKaetErx/P/QBRbP+xQi4VuB5nHRaM5zeVTMfVZMYH+hvcU6ab0dNzPb6AlC4RsUI7D8uxRFYC8Z4e8k8PDQboPvAZssviKMak2pPLqS4BU6VtjLzMtsGNfi9GNEG6aAVcQ5ydF0jxIHfPr3dF7RAf6fXi4JCFZb0dw2s8QLVDincnEEdIQam6KaodBDcZM6vvysZNpkZOuIm7HiHZRA0UZPecaA1ioHSUHuSibQ9CF7ixnQzvL5RsXthL0twYsWWeKxDjoTd+1icYT9d+rfrw6C9fAg3MLlo5qjof0n46iF1dPDkPgk0r0a8ti3NV39gl3G6YZes94pG1YOzkBZdOIu7ccLGlTl7hyTn2+zd/yqCNkBFg40RqUreNfwhC2wcS+B5ZTym2m6XKqS9rsYSKa4e5Kad2OHFRiz9+sSaVF8MD1DHogd7n1VZKqJ6l8GLGqUSFafavqqliOyFkEM3MBY4XLwqJEsyg1SYEA5s/aKFSawygVO/s//cCZuNPaf1bLkgIT8mYmWn1N0Dsi/slIPnWflQpRL1eWlsjl2/EgX7yOVm51RlZngNeVIVWimBdQ/usg2OwaDw+PHPsTqD/0sPA4lFeA7lIV0nfGnCmpa09wEuT7TL5VL3Xpo/YjLaAzJng0kEAYriSV5IPFUd63N3N+k4v5dvFRY8uzxJQ/hwmXmr50VSAP5c1Xy/CfOTMkgqkOfvaK4+nBUY3zIHdk5cZ4UtvFQaXOYrPMTbVZxInq+z9b qUnI15WC 0UDZv2sQ3l+bTm8xbxB05lhstgKDJ9gXrbFJbjNQ8Bs8wgAZ7nyw9lDRiT1KvUUp8YOtbAeiqq4rHtCMg9YL+wNCkb2VnEU+J78X2FotnIOkpGekxli5M5fBOlUucPkQU5k4KK0x5FJL8jEVN4Cx53FkKBmu3DA3R4XB8my38kt1RwVqP82PZjy8+LvSjmmyluDEa5L9WSVv0g1ZM3dXh2KPqh5Y+abGAllZXf8OeB6cJ+cjbpariF7Aypk6yoWJyzXZZJBnzuo8ZElKeQun5S3nGULOeH0B3mm4c9wrAbzqbaZmVMgVPMlHjDBJZxYkTpDLy0UnSOXmZ6R98ENQFdNWK3jvg+e0g1uSfY3+D8+QS7gXb/9qv/jdSmRft/GMqChxfjk18ruR7fAl0XmcgljKpPAbm5rZ2Eryi8z1XKAsihUI1Xm5776gXzCQIOIB0mtj0blN9DAvTX9ODJKD6VdB2apq0iS4OHAgFSz59AvtnxviXf+DSeKMu89Pc1z/BoAG7utuK6KXiDr6kxspicv9sSjsk8oHI9WhfUR6CDyWolMNIZ06b06NjOnXuFAXy5G1Dy7ZOCEpibAk= 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: Commit df04abfd181a ("fs/proc/kcore.c: Add bounce buffer for ktext data") introduced the use of a bounce buffer to retrieve kernel text data for /proc/kcore in order to avoid failures arising from hardened user copies enabled by CONFIG_HARDENED_USERCOPY in check_kernel_text_object(). We can avoid doing this if instead of copy_to_user() we use _copy_to_user() which bypasses the hardening check. This is more efficient than using a bounce buffer and simplifies the code. We do so as part an overall effort to eliminate bounce buffer usage in the function with an eye to converting it an iterator read. Signed-off-by: Lorenzo Stoakes Reviewed-by: David Hildenbrand --- fs/proc/kcore.c | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/fs/proc/kcore.c b/fs/proc/kcore.c index 71157ee35c1a..556f310d6aa4 100644 --- a/fs/proc/kcore.c +++ b/fs/proc/kcore.c @@ -541,19 +541,12 @@ read_kcore(struct file *file, char __user *buffer, size_t buflen, loff_t *fpos) case KCORE_VMEMMAP: case KCORE_TEXT: /* - * Using bounce buffer to bypass the - * hardened user copy kernel text checks. + * We use _copy_to_user() to bypass usermode hardening + * which would otherwise prevent this operation. */ - if (copy_from_kernel_nofault(buf, (void *)start, tsz)) { - if (clear_user(buffer, tsz)) { - ret = -EFAULT; - goto out; - } - } else { - if (copy_to_user(buffer, buf, tsz)) { - ret = -EFAULT; - goto out; - } + if (_copy_to_user(buffer, (char *)start, tsz)) { + ret = -EFAULT; + goto out; } break; default: From patchwork Thu Mar 23 10:15:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Stoakes X-Patchwork-Id: 13185410 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 B554DC7619A for ; Thu, 23 Mar 2023 10:15:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 06F596B0075; Thu, 23 Mar 2023 06:15:29 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id F3BF56B0078; Thu, 23 Mar 2023 06:15:28 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D41BD6B007B; Thu, 23 Mar 2023 06:15:28 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id C541D6B0075 for ; Thu, 23 Mar 2023 06:15:28 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 9382DAB0F4 for ; Thu, 23 Mar 2023 10:15:28 +0000 (UTC) X-FDA: 80599755936.18.54911D2 Received: from mail-wr1-f54.google.com (mail-wr1-f54.google.com [209.85.221.54]) by imf18.hostedemail.com (Postfix) with ESMTP id B09291C0002 for ; Thu, 23 Mar 2023 10:15:26 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=ay5smV8H; spf=pass (imf18.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.221.54 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=1679566526; 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=CnqbASwlTwT+hpjobkOVo9MU5RGxoqqRsLFO5z2uP7A=; b=FXioTtXvIHs3bLl9qmGDD2pMUU7Bi9gT4PmyT5pxcQsNovvNEBMDhZypnoo4ETs/xxfD1i FBgP79XnfUIKDd1/5w2UAQm8919tX4F0ESXpOpE+2VDfQD/FTH2G0hkANUV83zhdxOR8LF MIz7noJnMfZkpH5Ut+71TosWoRO+qIU= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=ay5smV8H; spf=pass (imf18.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.221.54 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=1679566526; a=rsa-sha256; cv=none; b=l91O2PC0KNmPCctfMkVfDtXxwOhfwlNWb5PAQc5QFEJ5PlRGaGhGWv9tnzcAPNWSfBq90+ glPJtWuYpy4Zoasb0oZM0TrueuXLxbAg/Lrim559vvlNsu4AZ9Dg9abn8yxn/JGpppApCf eZQEAVtFWo3rRjWVlFlEsKTtQZmxZNo= Received: by mail-wr1-f54.google.com with SMTP id i9so19829774wrp.3 for ; Thu, 23 Mar 2023 03:15:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1679566525; 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=ay5smV8H77AUMTTLy9JbPLC9H0FvAA6po1YwW88plRseeVZNzrO/yWXbWoeqBe73BV EPgoT4A9/R/wlPu2/DDT/sXT4SvPJX01j/7S2NXxmlxFXfMomXZaZHpKhswlSSHQAlwn YGU9oMUiADoHyd7ZKlL/C+RRgIp5YyBRmuDMkS8V6pWCvANVchSSxW2H3WGP45Ou1g4F 5LowAeuegB7b2wwRVSLyP6b5V+tJKxRo43A9f1YG5Pig2CU0CsXh/kbSLJ1arlLoPMAR UlnBALoZjxX2Emv0VI1hYbblmrbcykLwIR8Hj8xPwSWirkoP5Xg6oe7ivaw2MaP++1RV gW9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679566525; 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=PTnFyLjx6mZj+SBfKO0NTW4ZpiYtf2VjJSdkZIyxOUlcJ8ekvitkVTGJ7RND66mHqN HDrp90+MyTZkQ5gkpsTbFP8gVIfeq4sCQmG8RXx/8D1ty6I5oZ+DqrjoHDCD27qlRlm8 fTE1AJdLT3KVRyP9oeMGET2+s6WUWmGN96zfRNsJLG4GCs0j6nCrWcDnpO5nqlm6vMdy tacqeICDhvIO0uCHD8r9ZtV4/8P/1yRZjMT0X8n0P9P94QzpLO5rIemikE1tO5JYciRg B54OUCKWX+/EsMyvFFl3/iNdAVhkXTJcJonk8YMk+hHZj0RCvlGh2NEQtI5VxNaz03aD GRsA== X-Gm-Message-State: AAQBX9dyxes3wBM7gD+ZuyVOrky8LCl9FlvTWJIH5DA+DtYh5Hw/IDfK jOv429+invgnSbsjt2xCozC6qAbhj8Q= X-Google-Smtp-Source: AKy350YAiKXXkl0iQoEpKbs8mnEe9sJULjjf91EDkzy28H0cXCyjz2M7MyL7zCDYF1/QYu3rmhjIKA== X-Received: by 2002:adf:fac2:0:b0:2cf:f061:8134 with SMTP id a2-20020adffac2000000b002cff0618134mr2124557wrs.26.1679566525016; Thu, 23 Mar 2023 03:15:25 -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.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Mar 2023 03:15:24 -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 2/4] fs/proc/kcore: convert read_kcore() to read_kcore_iter() Date: Thu, 23 Mar 2023 10:15:17 +0000 Message-Id: X-Mailer: git-send-email 2.39.2 In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: B09291C0002 X-Rspam-User: X-Stat-Signature: 581r9zc6pob6qyag4a1r94utokh5xmhs X-HE-Tag: 1679566526-722556 X-HE-Meta: U2FsdGVkX1+A+8DjCtz97n9hxjiwKhfEF0MFr9wwrL07gJQmJN9GoEnjp1v0deqcujKyuugcH5nVzmTtsz56fi/1bXX1Wi4eskczeA4SuvM7eVcn7Gosy8Ku1FwC99b0TGyzfw3G3Sl+ZB/CPRnTtCKzW4zHfggUUkfSpxoXW+LH16VRVpn+i2DX1Dd/1H0fqX8EfQ8DkLpkW8O/Y+YyMzsrEQzXplqWFQ6u64ls7kVP805aN9MvTFAUStzmBK16C9RThveQbRXn1srH1G4GYRSHz7qxix+nDVw8YXuqrZWY6bcCExbEgXsXMZJYisYSQ17hCK/hd4QfFy+wZBj8AgCmJ+Y4DzKHMdDIWqggcoYZVDrhi2gd86dYei2gktsIClIJ1G4l12lLw2VbPOIiQL9Bb51TO/+XqsM+wxVcewrLPF5TLpR3RFLy1yLaVVZg40ctC3YjLrTuMX1OfcRNllfAmGNkNQV3ZJMuTKjfMx/h5uAObEL57homRWmNNKUSpQg/TeCEBBxTbjaYI9Oel4xbRbYBnc2mT1TPSsz1TbXtOn8+SHYITQI07cNejtpzaQFQ1jb5KrDTUxg1m32f7+D1SOoccc61JjS8QIU4qe+HuyXwqUdHFGOSdOmqk/u4Md4fWdCFaM/YvR9ZWLIQlXUUnEyJK4qk3gG5j4gs1jssgBmu1uAaMQxAMA1aLpoRimwHOFQ5iMUQh7fCnKbblykLnWCiKFwvJIpg1v3FiMAsOhz3qKlxXWn8ryORaJIWseKnjP83fA4W89NMqT6sqdJlmtYuuiAyI6GCRpkKuB7ppGrRnPkD7rKAmqmoNOJ04UaK6KBdmnPyb5eEjWZM8gRx83cS5ufa69OsXEYzciEG74WA6grX5a3LX067wyP9uOYHCP16ez/903/6dQwfeE3G4N+jxyO9zgsK92x6hCWlIe4uNumBkT0iLW0p2wJN9p+MiRUiH6pBG834G7u eq1itgQL XyH+h6eTDcGvFPgx6ufWn4XZQIuq0VBDByLI9NADoUefb8EHISAd8Tljrgk0kl10Jbj/BOj75Xlu8O0GUSFHLA2K/VugM6Cqa+lLYg7X8wA/hELmZRC6a5CNn3Jw/55YngOLhewd1Dxe00sk+IKqny6WDJYmXgiOgCvrvlSJ67lKdN3eiGAPoPFrxyh08i8RKj74XIBp50qSQGcjkRzGejd3unu4nCK3GaMkOK4L2eoaWp9bt7c9opCB7SUuBO2thazl6ymQcg3dT8dWVHgupYD8tSdezpED4SmwDMN+ZQVI17FO+dXPedNuTLnkFEcHl2PkpUO59wj+7lNaNpfS0FO8yctPv5juxne/bR4Z8Bg/YL5tpLPxRKjpFJBWtsg3RKfs6b/NsxyYBU3U/juq0RlD5lwpIzfTI3BDV2l7l6yZCWRq7/S7QetSvDYcTJbVNqEJMiA/ndeWiKC4xE1CnNknwz7DB4594pkg7x1q+PVqs5Wc0jenkZTYJjSe+8HBQ8J8Jrlf8lpil8MnPqzLVWVh0191VnRxxp8YH8foIJK5qsRDTgr+F/hF9vBfm48WJnUBtFFegUvhnY4CGYeRe/6V4Qz8I+Qove6WbmVd27l2soT33IU/3/jg0FpPoATVEsF0U 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: 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, From patchwork Thu Mar 23 10:15:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Stoakes X-Patchwork-Id: 13185411 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 36B05C6FD1D for ; Thu, 23 Mar 2023 10:15:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A0D256B0078; Thu, 23 Mar 2023 06:15:30 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 995B16B007B; Thu, 23 Mar 2023 06:15:30 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 69A8B6B007D; Thu, 23 Mar 2023 06:15:30 -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 51A896B0078 for ; Thu, 23 Mar 2023 06:15:30 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 2AB928059A for ; Thu, 23 Mar 2023 10:15:30 +0000 (UTC) X-FDA: 80599756020.29.6A002F3 Received: from mail-wr1-f44.google.com (mail-wr1-f44.google.com [209.85.221.44]) by imf21.hostedemail.com (Postfix) with ESMTP id 3A6B21C002D for ; Thu, 23 Mar 2023 10:15:27 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=q71TRgMa; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf21.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.221.44 as permitted sender) smtp.mailfrom=lstoakes@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1679566528; 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=ziv2ovcgc/3NOuBG0+YWYfMhKDq2ZBtAkLCMMX3QtWE=; b=dPRJQEEZr46QMn1ISyVD+BPXsrYMU5lw3kcIzlaMLuspc3q8MFZTtVL+dYMnmtu5VlCcXQ GBDnI3ZjLQw3fqosrFWE5A3aAQjRcveSbBj2BZDmosDVrJrI9T8NvQqdBnnA9ew2lUquhh 4rV/AhTFvuh47hFsX97svFPKKBlvMY8= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=q71TRgMa; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf21.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.221.44 as permitted sender) smtp.mailfrom=lstoakes@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1679566528; a=rsa-sha256; cv=none; b=rV8+1OLJx692xsEfWtnZSNhNnGdR69lZdicEph96DjsTrGh7362Z/hQdOsMYAP7pep/le3 fKSR+p8eK5g6AOaUtwFIA3EVk7L3ciU+ZyyhESVkWsbVcpCJzVsJ+cGSjvh/5McijqoUAO GIVJ/hSf6xEypq40515MdgK/Jt+gxCU= Received: by mail-wr1-f44.google.com with SMTP id v1so13728452wrv.1 for ; Thu, 23 Mar 2023 03:15:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1679566526; 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=ziv2ovcgc/3NOuBG0+YWYfMhKDq2ZBtAkLCMMX3QtWE=; b=q71TRgMaiJhexb4rt9K6K/OJXB+xp2xMvihLfEQSyx92J+vcfBnKnPXLHd1HxY+Jkh fylGKmF703YaOMD430/wzE2YVyB/toaRNiNVbr9xNt1ushLgX7U8X6EWfC7giBMjdd3A QXbJXhtqmS/cNRUAe5OtLLDmT/jhP5iNMIcVmfToX2nFjnziIra8o0oqX0SPd5mdlBvo s7byAuwRvXRdFSvpzjpYzFYWDE8ZDnUc/S4DsfwvwNsHx4H9uXjgvTyptRBwa0AgmqkG 4txxsONgFqFC4qRe5uOA2fEgbppYZRZHznEeqrg73PhMqjSuCGV61AMbGtfc84SQPvMv sobA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679566526; 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=ziv2ovcgc/3NOuBG0+YWYfMhKDq2ZBtAkLCMMX3QtWE=; b=eOHmZxNUKVtvEqc2Uh+QyOjNl09HKBjuMqphJKa+MhXE/n2BUCHqstvcX5epqRAl5r +BtyJnZWHPlNkom1brmUQdTnqa86KaGqA1CiYKNA8mM79jDs62fqvdb5lknSvZUDVRFf oNVbwC/e+U9rr4ocxHb1hBroAzKCpDWe9l//6Pj9n7BEcCPtKZWZivXSxmW5BXbHsJ6m sQyk9Qv+cJk/YcFVkQXMfkBaTrQLZBgol8ocYnBEhGXeT4NOUPabpj8j7kbkybgtzllL 0H2vCqn1yLlqSy8JyirPC2ttqCItT0ekQeZEht1/XGkUEgH/CW9FfZsCWfs2JIkn7Yl3 zJ9g== X-Gm-Message-State: AAQBX9coz4P+IEbYwdZu3iISkDdgWO4zNBb8lhcuTDKn6B+55y+6uYOv pOisQDpziLhbqkY0lqcx2KaQopmyZes= X-Google-Smtp-Source: AKy350ZF32A8g3SpKgBLxf2F/F30nUWdXSvk0BtRiQ25SfSbpeVUfcurgkUToqQZN9wTmKBO9ILEMQ== X-Received: by 2002:adf:f30a:0:b0:2dc:c45:faf6 with SMTP id i10-20020adff30a000000b002dc0c45faf6mr794519wro.51.1679566526457; Thu, 23 Mar 2023 03:15:26 -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.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Mar 2023 03:15:25 -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 3/4] iov_iter: add copy_page_to_iter_nofault() Date: Thu, 23 Mar 2023 10:15:18 +0000 Message-Id: <19734729defb0f498a76bdec1bef3ac48a3af3e8.1679566220.git.lstoakes@gmail.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Queue-Id: 3A6B21C002D X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: frqp5yhj4mufpmtjqy66gf8r9c1xi9yw X-HE-Tag: 1679566527-604881 X-HE-Meta: U2FsdGVkX18g4G0zaJSy2tc3cwU79k+zAl4KCGJTNaZ0JmXhw2kurI1H4/xIhTopZOFaLCu2DNvAxbIcjb5emA5l+fNyxXsKzUh699UyatScOXcrG1IfXWI69ljeYCkgFCPnhj8J63SIQ3Ltv8DahogEynvwoKGnJqMu1/JScCoUtlX4tdBH54xFGs0BWMGizjcOw9d5Dh1ZmsHpacC1bqQoo/YzCj5+so9oYEfRH0llrhHbYPJ2TRP8miIr76NSqr/LumO14v/1wmr467He3jTbVLg3wpKvwsx4qh1LkwtYQiGDtDycteRxF+5UsGfCmM42+zwulBhLowMljT9EDvpbEuUnFc4AVYtKAk2jWFhNWjdNx+xS3eyvFEIleSAkVujXYu9PJkXI8ZHQn2kcmaqQeexIaEnpd0GoFcN5bj9xcx3dY+nMT+I087X1Z+i+5JP+/zyhZBBOfX5JfFVo+M1gd82O+GO4m1DyK6poUR2waoqTbt6hm92Ui6w2EhIYlJtJ/lgo5bTdC0dK5tQz0UvDejlsQRZKntuX3JevCH4sfXsnZcZ/XrVQYo6xKk/aeySvit/sfVKcgDEo+XkB88VPMttxHUlHP/U/sHTHvxKME5+KKDJ8Bgoygg1Rd70cu7Vj0Y4PCkiqUNwRYSME9QOrj64sWlCvyslkK2Ny3IwKuj/WYqev6RgJi4W1aeKLUiS2huJSzlLwYzt5J5YSU3YpqlRvM+lZR5EzbueZWrMyrRjWYhdmBkw0EGqdgEOrKvxS7Kd+qeFFM7GaOvc5W32T6Pq70VeAR6YIwFF6Eetofl4XrPaNvRTBGmHH7WK1Rim0E8RiUrI+oYxUB5NAorbiBi4mCBOJvxyu/off6rjZX3xxM7vdT8ZoQAVjTN5SapExypX5x49IEEHXSxA2B1l+OF3TzCNHewqE8tA1pDiV/ORI8cRGgIBeNxRRAq3KmB+1P+LHDQ2ifw4KPI9 Y0FvYdMP sFzDGfn66TEfwqI+m6UpOVS91613wXEh6Kh9sMkeAeHKZu+H63abFZ+1oU8t6I289FVaTQLmmupvCuU5XxSCprATR2Kf9HrStNzXoxAh+gNnzA0QQd0I5e9ra96q0/RDh1uZznO2ft7ZFuSq4nMbXmw01n/fJwC4yA5hdSCeGb5JFM7Lzb2/Ybw0/89Jl9LcZnFvBHm5Koh547So9OUjmNpNoYQPscoJMrmVgvgNq6WIHU8mlNjyXwAL5uA8j2Xb0kz4Z3t74Arki1bhbGeXQK45Isy7qpq0lNEGRrfaU2kC1/CKVf9RZjQ5/3n35FHf5T1R057MVtkW7g26yrKPEVcpbK1Za+oWR81mH0QpyEoWHifcYVasZAAbyqsR9Jt6BToNXOxv1+OHWBrWaua8YBWZ5VRDVP12BXCiXNF6dGd3fliX9LGX/my0xhbw5u+IYtfd2AfQeN9L8mzPda5U2233NlOjMXXDN1icj0pkP7jyVNvrU+Vg1QrPyQZkK4ObpHctlmk7JmADKh/fq4B3OMYMGMbYYRmZPgbmtlqwGSsuhhZqDCoseDaPkne6kNadxp4BzYKqMbEDMuxc= 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: Provide a means to copy a page to user space from an iterator, aborting if a page fault would occur. This supports compound pages, but may be passed a tail page with an offset extending further into the compound page, so we cannot pass a folio. This allows for this function to be called from atomic context and _try_ to user pages if they are faulted in, aborting if not. The function does not use _copy_to_iter() in order to not specify might_fault(), this is similar to copy_page_from_iter_atomic(). This is being added in order that an iteratable form of vread() can be implemented while holding spinlocks. Signed-off-by: Lorenzo Stoakes --- include/linux/uio.h | 2 ++ lib/iov_iter.c | 48 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/include/linux/uio.h b/include/linux/uio.h index 27e3fd942960..29eb18bb6feb 100644 --- a/include/linux/uio.h +++ b/include/linux/uio.h @@ -173,6 +173,8 @@ static inline size_t copy_folio_to_iter(struct folio *folio, size_t offset, { return copy_page_to_iter(&folio->page, offset, bytes, i); } +size_t copy_page_to_iter_nofault(struct page *page, unsigned offset, + size_t bytes, struct iov_iter *i); static __always_inline __must_check size_t copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i) diff --git a/lib/iov_iter.c b/lib/iov_iter.c index 274014e4eafe..34dd6bdf2fba 100644 --- a/lib/iov_iter.c +++ b/lib/iov_iter.c @@ -172,6 +172,18 @@ static int copyout(void __user *to, const void *from, size_t n) return n; } +static int copyout_nofault(void __user *to, const void *from, size_t n) +{ + long res; + + if (should_fail_usercopy()) + return n; + + res = copy_to_user_nofault(to, from, n); + + return res < 0 ? n : res; +} + static int copyin(void *to, const void __user *from, size_t n) { size_t res = n; @@ -734,6 +746,42 @@ size_t copy_page_to_iter(struct page *page, size_t offset, size_t bytes, } EXPORT_SYMBOL(copy_page_to_iter); +size_t copy_page_to_iter_nofault(struct page *page, unsigned offset, size_t bytes, + struct iov_iter *i) +{ + size_t res = 0; + + if (!page_copy_sane(page, offset, bytes)) + return 0; + if (WARN_ON_ONCE(i->data_source)) + return 0; + if (unlikely(iov_iter_is_pipe(i))) + return copy_page_to_iter_pipe(page, offset, bytes, i); + page += offset / PAGE_SIZE; // first subpage + offset %= PAGE_SIZE; + while (1) { + void *kaddr = kmap_local_page(page); + size_t n = min(bytes, (size_t)PAGE_SIZE - offset); + + iterate_and_advance(i, n, base, len, off, + copyout_nofault(base, kaddr + offset + off, len), + memcpy(base, kaddr + offset + off, len) + ) + kunmap_local(kaddr); + res += n; + bytes -= n; + if (!bytes || !n) + break; + offset += n; + if (offset == PAGE_SIZE) { + page++; + offset = 0; + } + } + return res; +} +EXPORT_SYMBOL(copy_page_to_iter_nofault); + size_t copy_page_from_iter(struct page *page, size_t offset, size_t bytes, struct iov_iter *i) { 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; } /**