Received: by 2002:a05:6359:6284:b0:131:369:b2a3 with SMTP id se4csp5034057rwb; Tue, 8 Aug 2023 19:23:10 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHTlcW07jZpORnYCuoYfZ8bNoSxc6ytptyhBv2cWlmFwAl6wmAKwFgbYEJHqsvC7mRZ4Fuf X-Received: by 2002:a17:902:8609:b0:1bb:a125:f828 with SMTP id f9-20020a170902860900b001bba125f828mr1165606plo.68.1691547789966; Tue, 08 Aug 2023 19:23:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691547789; cv=none; d=google.com; s=arc-20160816; b=crOz9iALikm7Ms4pJBycZ3UqTDC2WzGdrhO4gaQ+ohYnF6mSMB3v7/vQkXQ7+gZCl7 Dek0cRcqzUlNUNVEEGwidb8hjfsXW0DFTy0zMXCuebZK++BTaBeHxsqBWYr01xZ5wPZH /xASfXxHdt1BZMlCxSfbJsMcWMi4M9Twq0HdyNOCSjRcK+oMlsaUtZ5GChyexVwUAfOn IvJTp3kciYhXuD/28u81Uchv9/lzT2pg7uHU94ZKWFltY5wAkB4qjwt/0Gxg+D6TUTiH ZPcBF+U4mbpn9tjENLgDfDnL8YauzfW2C9s/eWpvAQv1Xz2VpvvvPoAaENGyEMBB7qcV /ezg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=YE/LYcFZQN6MbPrG0CFAYL17czJIbn0IYvyYFqhferI=; fh=QIO4GCMwwU/ROC3EgXJDo83hkgl3MtP8/WcduuaRzDs=; b=oDtrW0y8zwbGwHyEl58j0MYWB5neiLwrvMH0wEVhOQCOO1GDPx8gKuhcSxqiquhqbS +xAlgNZ2X0DWd3Am+BgMkStGM/3cdLAT7NcL/trPOq6F9vIjAXWaa6NCHG5Sq8bRnrH+ JtQO0GzmBnjHgUJX8OlqdBR4i4o2NCUtkpisLWWaj9n0UnsaOD3wOeelKh3NuiM7JMZl T4JYryGb4+ozv2N4gnMZPGQTUGiBSGmSxby27fkrVEckKL9zAzmUqJeQyi++TCCx+BFE Zpb6hsXz876WihGRvF2WRI3yntR33c9mtruEFFVpYVoQisVq95w7cWoogL+WPXmzjBel FS9w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ziepe.ca header.s=google header.b=dMqr9o4M; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ki6-20020a170903068600b001b3dd62b1dbsi8196822plb.560.2023.08.08.19.22.58; Tue, 08 Aug 2023 19:23:09 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@ziepe.ca header.s=google header.b=dMqr9o4M; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230097AbjHHX33 (ORCPT + 99 others); Tue, 8 Aug 2023 19:29:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41168 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229436AbjHHX33 (ORCPT ); Tue, 8 Aug 2023 19:29:29 -0400 Received: from mail-qt1-x833.google.com (mail-qt1-x833.google.com [IPv6:2607:f8b0:4864:20::833]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EE56819BF for ; Tue, 8 Aug 2023 16:29:27 -0700 (PDT) Received: by mail-qt1-x833.google.com with SMTP id d75a77b69052e-40ffc70e740so22430131cf.0 for ; Tue, 08 Aug 2023 16:29:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ziepe.ca; s=google; t=1691537367; x=1692142167; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=YE/LYcFZQN6MbPrG0CFAYL17czJIbn0IYvyYFqhferI=; b=dMqr9o4M8WjFFzRLn3gT0X6z7XpuuP1kZX6/7LHdZZZBgoyat6Lus4tIiafgpDz0MU 2RMgaDaSko0mTB53fcS93V6hYpsQvZA/Kark6Mou+j7xe5Yk/01OXIA6qJThHTELrmh8 r0vAzUAjLjpcS009YhV7uDp6m6kyIt1Y9tYTKxiTqrT+en7vXX71HF+Witbyt758JIhu dDzYlg5+Aeg/id0tq5zhGKH4v+VMKRUWhH+bOs+2/Nn3CWJ1yjRDt5LeTeLWHxQ0aY+t NlpK/eVBUVS5jRbrwsd0efK74bHY9MAdcXbmUDPXvQR6KcimiyXj+l0gspcKbwOfwcq0 GnSg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691537367; x=1692142167; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=YE/LYcFZQN6MbPrG0CFAYL17czJIbn0IYvyYFqhferI=; b=Wsfzaeb6tKQ/RS4ZsIFzLbgIym+2gzEP3HhVj94rnS4M5+f/iZtrSMNZsP8uNEK3on D2wNAGc2VPOuaFlMf6JteFgGPgEE2kO3Y4kXbUn7DDzjCr+WxajtjEmqK+SA1gf5Dfu+ AkApHWEEqooVdXIqXB0d9lLaBG1J+dQ2hLU0D4g8TGnZj8XdcQUVu1ws43Zl351js9MN jiIgb+w1vmqAS3r6ufAnrZ73SftfFU00dzy2ML/N1H8HMhtORQPfE5sqK0+VPNomu+Nt OO8AVm+YsMxmgG6g7u6wx6LCwXmTZKWDLsw3fSUOW5nU5/My6JOZYDjkgVYp6m97Ax2V +ruA== X-Gm-Message-State: AOJu0YxaYo596vBNRhJCE1t0vDGthT/1/nFm/B7U0Xet3DXklqAvKdAl fsW5jgO6UP22usWTkH0UPTyL3LdHRwHhepISGbc= X-Received: by 2002:a05:622a:11c1:b0:40f:f058:1478 with SMTP id n1-20020a05622a11c100b0040ff0581478mr1403575qtk.30.1691537367126; Tue, 08 Aug 2023 16:29:27 -0700 (PDT) Received: from ziepe.ca (hlfxns017vw-142-68-25-194.dhcp-dynamic.fibreop.ns.bellaliant.net. [142.68.25.194]) by smtp.gmail.com with ESMTPSA id v29-20020a05622a189d00b00403b3156f18sm3683586qtc.8.2023.08.08.16.29.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Aug 2023 16:29:26 -0700 (PDT) Received: from jgg by wakko with local (Exim 4.95) (envelope-from ) id 1qTW8j-004xs5-Pa; Tue, 08 Aug 2023 20:29:25 -0300 Date: Tue, 8 Aug 2023 20:29:25 -0300 From: Jason Gunthorpe To: Alex Williamson Cc: Stefan Hajnoczi , kvm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] vfio: align capability structures Message-ID: References: <20230803144109.2331944-1-stefanha@redhat.com> <20230803151823.4e5943e6.alex.williamson@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230803151823.4e5943e6.alex.williamson@redhat.com> X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Aug 03, 2023 at 03:18:23PM -0600, Alex Williamson wrote: > diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c > index 902f06e52c48..2d074cbd371d 100644 > --- a/drivers/vfio/vfio_main.c > +++ b/drivers/vfio/vfio_main.c > @@ -1362,6 +1362,8 @@ struct vfio_info_cap_header *vfio_info_cap_add(struct vfio_info_cap *caps, > void *buf; > struct vfio_info_cap_header *header, *tmp; > > + size = ALIGN(size, sizeof(u64)); > + > buf = krealloc(caps->buf, caps->size + size, GFP_KERNEL); > if (!buf) { > kfree(caps->buf); > @@ -1395,6 +1397,8 @@ void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset) > struct vfio_info_cap_header *tmp; > void *buf = (void *)caps->buf; > > + WARN_ON(!IS_ALIGNED(offset, sizeof(u64))); > + > for (tmp = buf; tmp->next; tmp = buf + tmp->next - offset) > tmp->next += offset; > } > diff --git a/include/uapi/linux/vfio.h b/include/uapi/linux/vfio.h > index fa06e3eb4955..fd2761841ffe 100644 > --- a/include/uapi/linux/vfio.h > +++ b/include/uapi/linux/vfio.h > @@ -217,6 +217,7 @@ struct vfio_device_info { > __u32 num_regions; /* Max region index + 1 */ > __u32 num_irqs; /* Max IRQ index + 1 */ > __u32 cap_offset; /* Offset within info struct of first cap */ > + __u32 pad; /* Size must be aligned for caps */ > }; > #define VFIO_DEVICE_GET_INFO _IO(VFIO_TYPE, VFIO_BASE + 7) > > @@ -1444,6 +1445,7 @@ struct vfio_iommu_type1_info { > #define VFIO_IOMMU_INFO_CAPS (1 << 1) /* Info supports caps */ > __u64 iova_pgsizes; /* Bitmap of supported page sizes */ > __u32 cap_offset; /* Offset within info struct of first cap */ > + __u32 pad; /* Size must be aligned for caps */ > }; IMHO this is partially being caused by not using __aligned_u64 for the other __u64's in the same struct.. Both of these structs have u64s in them and many arches will automatically add the above padding. __aligned_u64 will force the reset to do it, and then making padding explicit as you have done will make it really true. This is a subtle x64/x32 compatability issue also. It is probably best just to do the change across the whole header file. Please also include the matching hunk for iommufd: --- a/drivers/iommu/iommufd/vfio_compat.c +++ b/drivers/iommu/iommufd/vfio_compat.c @@ -483,6 +483,8 @@ static int iommufd_vfio_iommu_get_info(struct iommufd_ctx *ictx, rc = cap_size; goto out_put; } + cap_size = ALIGN(cap_size, sizeof(u64)); + if (last_cap && info.argsz >= total_cap_size && put_user(total_cap_size, &last_cap->next)) { rc = -EFAULT; Thanks, Jason