From patchwork Mon Jul 31 21:50:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Stoakes X-Patchwork-Id: 13335517 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 43B16C04A94 for ; Mon, 31 Jul 2023 21:50:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D48B52800B2; Mon, 31 Jul 2023 17:50:35 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CCF2428007A; Mon, 31 Jul 2023 17:50:35 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B48FD2800B2; Mon, 31 Jul 2023 17:50:35 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 9F77728007A for ; Mon, 31 Jul 2023 17:50:35 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 722E8160B75 for ; Mon, 31 Jul 2023 21:50:35 +0000 (UTC) X-FDA: 81073251630.22.5EAAA9C Received: from mail-wm1-f47.google.com (mail-wm1-f47.google.com [209.85.128.47]) by imf01.hostedemail.com (Postfix) with ESMTP id 8AE0C40011 for ; Mon, 31 Jul 2023 21:50:33 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=gmail.com header.s=20221208 header.b=Gh6QtEjO; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf01.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.128.47 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=1690840233; 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:references:dkim-signature; bh=LzzSGKWkmVAwS6+5gn2kW/zuOpGQRDh54S2FzorJoyA=; b=KqjsjOeleQE+nTuEqKlcDAdk8PHcb7PuLhwEpL1GQmxWZ5jajdwQtAnsbK9KkD89PODDuS 4R8mnrhFc2DGBFcCcaMEJ45z+jglbMdDKyB+hX/lzVLXV3una+m9M6hev/9L41HyQwR+5i k2ecnn5g/3chMZ5WgpHxBCo5FBhxrvw= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=gmail.com header.s=20221208 header.b=Gh6QtEjO; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf01.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.128.47 as permitted sender) smtp.mailfrom=lstoakes@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1690840233; a=rsa-sha256; cv=none; b=ce297k0pdP4F2pGcFGfag9mZG9lrq2NqBqU5dAAQ/8EVGPE2O2WyoUv60rMoBfXOZPw8A7 lMkwJ2ac7m26Mzc8BAdiw9p8hKpk3VMOzNJArT4ZMqoSoZ4Wo+3luuDcuLo7u6Wxy7FOEM OLcZNCIiKtBoNKcXyu9h8REVDMbeZcY= Received: by mail-wm1-f47.google.com with SMTP id 5b1f17b1804b1-3fbc5d5742eso54961465e9.3 for ; Mon, 31 Jul 2023 14:50:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1690840232; x=1691445032; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=LzzSGKWkmVAwS6+5gn2kW/zuOpGQRDh54S2FzorJoyA=; b=Gh6QtEjOYwj9odnsjh1f9DapsMkYw7zBcT5m9Kd1gaN0YupuuaRA1tt+K3+L1M0cAN +iRFN3msBWUF/UKb2JDD+6GJI2Gv1MpevVryLb9Bdoci+QBKe5pT1Wnv8UJK1ofVufOb xeETko2BcN4dMLmlLu7ea6ABxmGItWChPuqDD1x9cUqb5NUk/8kVlI50gFWAC4FIgthg WykjFjx6566tCwyrYocar6zjVxbZ7lNyX5ar+4lNWJQ7d9yEO/PAgOrNAibQMW6T2vPX 2q5ZT1t0X6cQnwloXqZRTBwwiJ4F+bpq6IzkWzbzTq3MGZKXR6b1Dil1dLPiW+t7xm03 L/Dg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690840232; x=1691445032; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=LzzSGKWkmVAwS6+5gn2kW/zuOpGQRDh54S2FzorJoyA=; b=HspGhkowySrL7//qlZCl6zff4vMkFH2QUyyLTLngYWhZIj04zQeziynUp2zaX44rDI nDeV1FZ3NBPBctLQaH2AtPeDRnkzJQFqywRSrruhrcMNvrVSCn+LwLPjjGqr44BGIDB4 NH4tTP97L6EF629TZMFGqhMJPpmNFhnzqZUULHx1ra2SPHrGuvuY5LejeAyLHm5hXVA1 gHNj+xhxel4e+pMFRcty3tVTqjp8X3Hgh5ndovJVlY62zqtLMaIuhF1987+J9o09a2NK dJ4mXbDxZgiu9buZl6v+sQDUbCd88nlEfHaCc89G7q+iebQ9ciV5JDv4IFQMh4NN+TB5 L8sg== X-Gm-Message-State: ABy/qLYbPK6q+YFbIhD23pV8cSeagYXN3MneRGpRg41SMWniVIODWwTh +NPC8qwhD3GqW5sbS2F6MUJlzncvwx4= X-Google-Smtp-Source: APBJJlGkZeJD/ErBNLIHuV84FQDDzVLXS/9Ndpi2HOQ7+mZlSHPebNAIlPGdzhNi1ilsx784crY0JQ== X-Received: by 2002:a7b:cd1a:0:b0:3fc:92:73d6 with SMTP id f26-20020a7bcd1a000000b003fc009273d6mr854436wmj.11.1690840231335; Mon, 31 Jul 2023 14:50:31 -0700 (PDT) Received: from lucifer.home ([2a00:23c5:dc8c:8701:1663:9a35:5a7b:1d76]) by smtp.googlemail.com with ESMTPSA id d12-20020a1c730c000000b003fa999cefc0sm12123626wmb.36.2023.07.31.14.50.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 31 Jul 2023 14:50:30 -0700 (PDT) From: Lorenzo Stoakes To: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrew Morton Cc: Baoquan He , Uladzislau Rezki , linux-fsdevel@vger.kernel.org, Jiri Olsa , Will Deacon , Mike Galbraith , Mark Rutland , wangkefeng.wang@huawei.com, catalin.marinas@arm.com, ardb@kernel.org, David Hildenbrand , Linux regression tracking , regressions@lists.linux.dev, Matthew Wilcox , Liu Shixin , Jens Axboe , Alexander Viro , Lorenzo Stoakes , stable@vger.kernel.org Subject: [PATCH] fs/proc/kcore: reinstate bounce buffer for KCORE_TEXT regions Date: Mon, 31 Jul 2023 22:50:21 +0100 Message-ID: <20230731215021.70911-1-lstoakes@gmail.com> X-Mailer: git-send-email 2.41.0 MIME-Version: 1.0 X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 8AE0C40011 X-Stat-Signature: pzf9x8qkbjqkdr966z7tp955ksba6ajg X-Rspam-User: X-HE-Tag: 1690840233-826432 X-HE-Meta: U2FsdGVkX18roj5J+el16gK7CNTpAIfEWZ32+FeDMmW8uwjwM7PI5SIULw7EM2/WxANVLghvL/pot2y/DRG7qyQkC/SEtZhabB1EH+suX2T8aJ3JUoYe138tTTREWRzTcjYaGjBZchBbMq+nnTrUdGZUefA5u6YlIMNUM6foseRcrqqSpOGyrluI2gQmLQAvIb5Ds5N43ukFVc/NGkvGgcjvkMx7xXNC803EhLD2IrhoqBL7yoawfDzPIyQ/yZFUtXManXaNi9i907DPnrm8tcliPL0MzWS8H0Gxm2ypuiM8AZ3IVKEvkA+qpIdvN13IIAHTogKkvOT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wDNcU2u0Ke1GXytup6OF9dRK7MrjwbKFPMGLLd579TPxua5+CKPBEg0znF5PqD0+dkEEkIomaQoMZZkAL18/J7mpQ9ADw7AaZIbcU0L6Wo6sdvCniL+D5DBsbCuITn+m 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: Some architectures do not populate the entire range categorised by KCORE_TEXT, so we must ensure that the kernel address we read from is valid. Unfortunately there is no solution currently available to do so with a purely iterator solution so reinstate the bounce buffer in this instance so we can use copy_from_kernel_nofault() in order to avoid page faults when regions are unmapped. This change partly reverts commit 2e1c0170771e ("fs/proc/kcore: avoid bounce buffer for ktext data"), reinstating the bounce buffer, but adapts the code to continue to use an iterator. Fixes: 2e1c0170771e ("fs/proc/kcore: avoid bounce buffer for ktext data") Reported-by: Jiri Olsa Closes: https://lore.kernel.org/all/ZHc2fm+9daF6cgCE@krava Cc: stable@vger.kernel.org Signed-off-by: Lorenzo Stoakes Tested-by: Jiri Olsa Tested-by: Will Deacon Tested-by: Baoquan He Reviewed-by: Baoquan He Signed-off-by: Lorenzo Stoakes Acked-by: David Hildenbrand --- fs/proc/kcore.c | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/fs/proc/kcore.c b/fs/proc/kcore.c index 9cb32e1a78a0..3bc689038232 100644 --- a/fs/proc/kcore.c +++ b/fs/proc/kcore.c @@ -309,6 +309,8 @@ static void append_kcore_note(char *notes, size_t *i, const char *name, 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; @@ -554,11 +556,22 @@ static ssize_t read_kcore_iter(struct kiocb *iocb, struct iov_iter *iter) fallthrough; case KCORE_VMEMMAP: case KCORE_TEXT: + /* + * Sadly we must use a bounce buffer here to be able to + * make use of copy_from_kernel_nofault(), as these + * memory regions might not always be mapped on all + * architectures. + */ + if (copy_from_kernel_nofault(buf, (void *)start, tsz)) { + if (iov_iter_zero(tsz, iter) != tsz) { + ret = -EFAULT; + goto out; + } /* * We use _copy_to_iter() to bypass usermode hardening * which would otherwise prevent this operation. */ - if (_copy_to_iter((char *)start, tsz, iter) != tsz) { + } else if (_copy_to_iter(buf, tsz, iter) != tsz) { ret = -EFAULT; goto out; } @@ -595,6 +608,10 @@ 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) { @@ -605,9 +622,16 @@ 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, };