Received: by 2002:a05:7412:31a9:b0:e2:908c:2ebd with SMTP id et41csp5034026rdb; Fri, 15 Sep 2023 23:31:49 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHK6aQcnfkNneQ8OLQ4RbmzHLsNiUKv5Tx6vMD/+Qsh7EgTyhGL3VlcQqoEK6tenDEs1oiA X-Received: by 2002:a05:6a21:498a:b0:159:ee0f:b9e9 with SMTP id ax10-20020a056a21498a00b00159ee0fb9e9mr3787339pzc.53.1694845909594; Fri, 15 Sep 2023 23:31:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694845909; cv=none; d=google.com; s=arc-20160816; b=Fv2Fog+0Evui4PMgiWYTL6XYHd9PE8itTJfKxZL0F5OV+WB/h3iJeMFNszVZ7OPcK7 ZsOpyMlAY6Q/Y50N6uuhrualQYX+wZW86LB7efz3xgCEyEgTLf2FEk5DDnEAHmyAb3QD q6Q5+amWz1TNLoZakC1dRyD6Y1cHkOhvbi9XFOBVM1Jq0eVOE+dWuUTgBWwVKEhGX06K B9xJyUUB4s2KP8dH2l40ZhVP1Dn+3nCbcwTwZrsKFO7diIYuZ3fIdak1Luc++7Si4IFH hj4TPE29GGseT0MI08leKiQ4+glGuRnF0MFOpLm8gJTxqYbebgd0lOHgCUmYElUG57Ce WT+g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=7NfUcQ09mB7dsUpfhcIFawf5HcTW8vmdNnitXI7u8bM=; fh=fyLspjGVe9VVzdyNbY0W57HbfUPWnVXKvCZElgMBMUA=; b=ncZAM+/wmsazijWdQRt+4qBxziNgU+lsEysLMmywS6pScs9nxCCoSrcFz5nWEhrRBe WkJ/3lbjN+qhPaL/QNntoC3wdlUCozaqDHoDC4CaZHXgB+V9CxYJTVGE/XRQHbs12zSs sVUcks1pVwizPniYmH/Xf34gK7Y5iGFUMdIbIC5iDCEJ7ZXAGu9fzOyJSa1qho0EtJbZ 8MOvCH+M8/aq2MlLEwR+ivTR4CW0wlsQl9K3gNsQsdUx3xlyf8WFLnIpYBp3hTgeuz4n UBvVG0sFQpneyU/dHltmXb4PjR/WtW2tHd6SaB0PInRzZh3aC+YMzomjM2mlAeO6fCCn AurA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=ZiEZMDp3; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from groat.vger.email (groat.vger.email. [2620:137:e000::3:5]) by mx.google.com with ESMTPS id cp21-20020a056a00349500b0068895dea43csi4306642pfb.78.2023.09.15.23.31.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Sep 2023 23:31:49 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) client-ip=2620:137:e000::3:5; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=ZiEZMDp3; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id CE6CE8367225; Fri, 15 Sep 2023 13:07:16 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236771AbjIOUGc (ORCPT + 99 others); Fri, 15 Sep 2023 16:06:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54054 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237194AbjIOUGY (ORCPT ); Fri, 15 Sep 2023 16:06:24 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 543C030CA for ; Fri, 15 Sep 2023 13:05:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1694808305; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=7NfUcQ09mB7dsUpfhcIFawf5HcTW8vmdNnitXI7u8bM=; b=ZiEZMDp33p0qZtPWxOburXIX/8YeaMSf2IwfNKHBnsXOU9TP7LJsZb49F2r/R7l9zwFKYT LnwsMuUEsxSTuhTjigVqaU+o5sMuh/KwPRPnnbC5gw0AL+YJYYfgKCkFPvQFqlN5eIgzrs 1Hz0EfLDUrIlYiPvQ3YP+k/bOPY25wQ= Received: from mail-io1-f70.google.com (mail-io1-f70.google.com [209.85.166.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-220-U1wShcj9Nre_NeKJe2awBQ-1; Fri, 15 Sep 2023 16:05:01 -0400 X-MC-Unique: U1wShcj9Nre_NeKJe2awBQ-1 Received: by mail-io1-f70.google.com with SMTP id ca18e2360f4ac-7907f181babso237575039f.2 for ; Fri, 15 Sep 2023 13:05:01 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694808300; x=1695413100; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:cc:to:from:date:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=7NfUcQ09mB7dsUpfhcIFawf5HcTW8vmdNnitXI7u8bM=; b=JUcShhqUmRvTY4h8AE1RDcADnv0mHtUQjzTWmEOWlhB5U9MiNQHD8PEXNj7Bn1o4dq S0xOdNCmXoIpu51r6/ih5Udefn6lNhfSYO0ccaVMieJ98we59ASoIhvH6cKkDsKONLgh /pyrkm0hPOdo/H9frVNVkmAzFSdvFKI3L4zVXvB0oRUmAQAceEwA7JdqLOP+Z/svqdjt i6KAtsMbibNRSaiGKlcVjM3itIbFG2lyU1ZKNtp2f/7SMan/0mkfsWq3Qf6zSA+NwjYb SUJb9Oi90kCkbzY92roInH5sdZ+ltEvjaNlFgN2brPPaCEEelNHiSTp/GIF613stdNMM 7Cvg== X-Gm-Message-State: AOJu0YxfgjZhUs4fP+2ap+kirThg1nKWnrOTkGHnAuRDOCVOLXraeNWr tIrCFOQssrFkV0mJVRUxzVqMfFA71xwKDtKUy3twMBzl+PHNzMowRhsTB/ZahltLKQYxyh9vHMp VOEO7xpWMh+xFzbhBxYOx8K+K X-Received: by 2002:a5e:d60f:0:b0:786:ca40:cc88 with SMTP id w15-20020a5ed60f000000b00786ca40cc88mr2412924iom.11.1694808300565; Fri, 15 Sep 2023 13:05:00 -0700 (PDT) X-Received: by 2002:a5e:d60f:0:b0:786:ca40:cc88 with SMTP id w15-20020a5ed60f000000b00786ca40cc88mr2412906iom.11.1694808300220; Fri, 15 Sep 2023 13:05:00 -0700 (PDT) Received: from redhat.com ([38.15.60.12]) by smtp.gmail.com with ESMTPSA id j3-20020a02cc63000000b0042b320c13aasm1290128jaq.89.2023.09.15.13.04.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Sep 2023 13:04:59 -0700 (PDT) Date: Fri, 15 Sep 2023 14:04:58 -0600 From: Alex Williamson To: Stefan Hajnoczi Cc: kvm@vger.kernel.org, David Laight , linux-kernel@vger.kernel.org, "Tian, Kevin" , Jason Gunthorpe Subject: Re: [PATCH v2 2/3] vfio: use __aligned_u64 in struct vfio_device_gfx_plane_info Message-ID: <20230915140458.392e436a.alex.williamson@redhat.com> In-Reply-To: <20230829182720.331083-3-stefanha@redhat.com> References: <20230829182720.331083-1-stefanha@redhat.com> <20230829182720.331083-3-stefanha@redhat.com> X-Mailer: Claws Mail 4.1.1 (GTK 3.24.35; x86_64-redhat-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Fri, 15 Sep 2023 13:07:17 -0700 (PDT) On Tue, 29 Aug 2023 14:27:19 -0400 Stefan Hajnoczi wrote: > The memory layout of struct vfio_device_gfx_plane_info is > architecture-dependent due to a u64 field and a struct size that is not > a multiple of 8 bytes: > - On x86_64 the struct size is padded to a multiple of 8 bytes. > - On x32 the struct size is only a multiple of 4 bytes, not 8. > - Other architectures may vary. > > Use __aligned_u64 to make memory layout consistent. This reduces the > chance of 32-bit userspace on a 64-bit kernel breakage. > > This patch increases the struct size on x32 but this is safe because of > the struct's argsz field. The kernel may grow the struct as long as it > still supports smaller argsz values from userspace (e.g. applications > compiled against older kernel headers). > > Suggested-by: Jason Gunthorpe > Signed-off-by: Stefan Hajnoczi > --- > include/uapi/linux/vfio.h | 3 ++- > drivers/gpu/drm/i915/gvt/kvmgt.c | 4 +++- > samples/vfio-mdev/mbochs.c | 6 ++++-- > samples/vfio-mdev/mdpy.c | 4 +++- > 4 files changed, 12 insertions(+), 5 deletions(-) > > diff --git a/include/uapi/linux/vfio.h b/include/uapi/linux/vfio.h > index 94007ca348ed..777374dd7725 100644 > --- a/include/uapi/linux/vfio.h > +++ b/include/uapi/linux/vfio.h > @@ -816,7 +816,7 @@ struct vfio_device_gfx_plane_info { > __u32 drm_plane_type; /* type of plane: DRM_PLANE_TYPE_* */ > /* out */ > __u32 drm_format; /* drm format of plane */ > - __u64 drm_format_mod; /* tiled mode */ > + __aligned_u64 drm_format_mod; /* tiled mode */ > __u32 width; /* width of plane */ > __u32 height; /* height of plane */ > __u32 stride; /* stride of plane */ > @@ -829,6 +829,7 @@ struct vfio_device_gfx_plane_info { > __u32 region_index; /* region index */ > __u32 dmabuf_id; /* dma-buf id */ > }; > + __u32 reserved; > }; > > #define VFIO_DEVICE_QUERY_GFX_PLANE _IO(VFIO_TYPE, VFIO_BASE + 14) > diff --git a/drivers/gpu/drm/i915/gvt/kvmgt.c b/drivers/gpu/drm/i915/gvt/kvmgt.c > index 9cd9e9da60dd..813cfef23453 100644 > --- a/drivers/gpu/drm/i915/gvt/kvmgt.c > +++ b/drivers/gpu/drm/i915/gvt/kvmgt.c > @@ -1382,7 +1382,7 @@ static long intel_vgpu_ioctl(struct vfio_device *vfio_dev, unsigned int cmd, > intel_gvt_reset_vgpu(vgpu); > return 0; > } else if (cmd == VFIO_DEVICE_QUERY_GFX_PLANE) { > - struct vfio_device_gfx_plane_info dmabuf; > + struct vfio_device_gfx_plane_info dmabuf = {}; > int ret = 0; > > minsz = offsetofend(struct vfio_device_gfx_plane_info, > @@ -1392,6 +1392,8 @@ static long intel_vgpu_ioctl(struct vfio_device *vfio_dev, unsigned int cmd, > if (dmabuf.argsz < minsz) > return -EINVAL; > > + minsz = min(dmabuf.argsz, sizeof(dmabuf)); > + > ret = intel_vgpu_query_plane(vgpu, &dmabuf); > if (ret != 0) > return ret; > diff --git a/samples/vfio-mdev/mbochs.c b/samples/vfio-mdev/mbochs.c > index 3764d1911b51..78aa977ae597 100644 > --- a/samples/vfio-mdev/mbochs.c > +++ b/samples/vfio-mdev/mbochs.c > @@ -1262,7 +1262,7 @@ static long mbochs_ioctl(struct vfio_device *vdev, unsigned int cmd, > > case VFIO_DEVICE_QUERY_GFX_PLANE: > { > - struct vfio_device_gfx_plane_info plane; > + struct vfio_device_gfx_plane_info plane = {}; > > minsz = offsetofend(struct vfio_device_gfx_plane_info, > region_index); > @@ -1273,11 +1273,13 @@ static long mbochs_ioctl(struct vfio_device *vdev, unsigned int cmd, > if (plane.argsz < minsz) > return -EINVAL; > > + outsz = min_t(unsigned long, plane.argsz, sizeof(plane)); Sorry, I'm struggling with why these two sample drivers use min_t() when passed the exact same args as kvmgt above which just uses min(). But more importantly I'm also confused why we need this at all. The buffer we're copying to is provided by the user, so what's wrong with leaving the user provided reserved data? Are we just trying to return a zero'd reserved field if argsz allows for it? Any use of the reserved field other than as undefined data would need to be associated with a flags bit, so I don't think it's buying us anything to return it zero'd. What am I missing? Thanks, Alex > + > ret = mbochs_query_gfx_plane(mdev_state, &plane); > if (ret) > return ret; > > - if (copy_to_user((void __user *)arg, &plane, minsz)) > + if (copy_to_user((void __user *)arg, &plane, outsz)) > return -EFAULT; > > return 0; > diff --git a/samples/vfio-mdev/mdpy.c b/samples/vfio-mdev/mdpy.c > index 064e1c0a7aa8..f5c2effc1cec 100644 > --- a/samples/vfio-mdev/mdpy.c > +++ b/samples/vfio-mdev/mdpy.c > @@ -591,7 +591,7 @@ static long mdpy_ioctl(struct vfio_device *vdev, unsigned int cmd, > > case VFIO_DEVICE_QUERY_GFX_PLANE: > { > - struct vfio_device_gfx_plane_info plane; > + struct vfio_device_gfx_plane_info plane = {}; > > minsz = offsetofend(struct vfio_device_gfx_plane_info, > region_index); > @@ -602,6 +602,8 @@ static long mdpy_ioctl(struct vfio_device *vdev, unsigned int cmd, > if (plane.argsz < minsz) > return -EINVAL; > > + minsz = min_t(unsigned long, plane.argsz, sizeof(plane)); > + > ret = mdpy_query_gfx_plane(mdev_state, &plane); > if (ret) > return ret;