From mboxrd@z Thu Jan 1 00:00:00 1970 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 01C00C7EE23 for ; Tue, 23 May 2023 19:55:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238294AbjEWTzg (ORCPT ); Tue, 23 May 2023 15:55:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53474 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229614AbjEWTzd (ORCPT ); Tue, 23 May 2023 15:55:33 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 22042E5 for ; Tue, 23 May 2023 12:54:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1684871691; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=IUxmQ5P+MH9SvOjqUd5l2+YQ2XDFMIly2tHjSWnRkr0=; b=JkAGHIDyuP8mRGOIu37kVJXBN6ruA6E4p+Nk1lJ9MiLNCpfruNb1ryMZnabSJxtANmnUsr p2FZVAhIChI8gbjYp9p+CzZoK9qTj8RIGtmknoetJHk+brq/vYAqBHBI/uKVaTxrTtjnh0 788mBWHFohUHw5C3wbL325udH5yxfy0= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-541-gNeZnkYYM_a-OP0OnxG1Tw-1; Tue, 23 May 2023 15:54:47 -0400 X-MC-Unique: gNeZnkYYM_a-OP0OnxG1Tw-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 06664185A78B; Tue, 23 May 2023 19:54:47 +0000 (UTC) Received: from work.redhat.com (unknown [10.39.193.140]) by smtp.corp.redhat.com (Postfix) with ESMTP id 794E4492B00; Tue, 23 May 2023 19:54:45 +0000 (UTC) From: Tim Wiederhake To: "Borislav Petkov" , "Dave Hansen" , "H. Peter Anvin" , "Ingo Molnar" , "Paolo Bonzini" , "Thomas Gleixner" , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, x86@kernel.org Cc: Tim Wiederhake Subject: [PATCH 1/2] x86/msr: Read MSRs individually Date: Tue, 23 May 2023 21:49:48 +0200 Message-Id: <20230523194949.96149-1-twiederh@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Reading from /dev/cpu/*/msr with buffer size > 8 would read the data of the same msr repeatedly instead of the data for consecutive msrs, as one might expect. Solve by restricting MSR reads to one per call. Signed-off-by: Tim Wiederhake --- arch/x86/kernel/msr.c | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/arch/x86/kernel/msr.c b/arch/x86/kernel/msr.c index 7bb17d37db01..058f2b67d0c7 100644 --- a/arch/x86/kernel/msr.c +++ b/arch/x86/kernel/msr.c @@ -58,24 +58,17 @@ static ssize_t msr_read(struct file *file, char __user *buf, u32 reg = *ppos; int cpu = iminor(file_inode(file)); int err = 0; - ssize_t bytes = 0; - if (count % 8) + if (count < 8) return -EINVAL; /* Invalid chunk size */ - for (; count; count -= 8) { - err = rdmsr_safe_on_cpu(cpu, reg, &data[0], &data[1]); - if (err) - break; - if (copy_to_user(tmp, &data, 8)) { - err = -EFAULT; - break; - } - tmp += 2; - bytes += 8; - } + err = rdmsr_safe_on_cpu(cpu, reg, &data[0], &data[1]); + if (err) + return err; + if (copy_to_user(tmp, &data, 8)) + return -EFAULT; - return bytes ? bytes : err; + return 8; } static int filter_write(u32 reg) -- 2.39.2