From patchwork Fri Mar 24 15:35:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefano Garzarella X-Patchwork-Id: 74600 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp719779vqo; Fri, 24 Mar 2023 08:40:27 -0700 (PDT) X-Google-Smtp-Source: AKy350asSlURm9Apmef2TTKHcgg+ruOl2L38h2Mtf0GlB/TwUx49epPyhnrqgrn03DilYi6wEUwA X-Received: by 2002:a17:906:4ac4:b0:921:412b:d8c1 with SMTP id u4-20020a1709064ac400b00921412bd8c1mr3145695ejt.71.1679672427606; Fri, 24 Mar 2023 08:40:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679672427; cv=none; d=google.com; s=arc-20160816; b=BXhLS83iaMOt78yD9LtlRusu981lTkEAjDdm7gJ2gLH1+TPtrSwDcNjqIBLeJsnH3g +faZahTOmCVdo0BViYcKccq8n3lcrIY1JBRXuj6/whBKmPUKeGH/fIAsKFPuRKqezqKA /n7FmCCo6CITDEerJ+96qarp2hFLuF/ekBHK3uai+OKjuci9fXISg3hOFBxKJPWoRIXX Owqj3OBA25h2I/T0Va4Jsq12kfvhYkmVhtgPI2nDbl2IyfKoZiD4Na28QEMo5Zvj2krd X/kQjeAx5Cem8K0tspcJuM7nUJBUSlydR1zgqrzM1okcsemHFVNd/XYT5Yfeob/cpjP8 N74A== 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:date:subject:cc:to:from :dkim-signature; bh=MmE27wy0MmMLRHR3IF88SSg1j96JW0EiZjlHTqsz8dM=; b=khUawfUMpLSWa5XXPZL5ig+4bKq1iNpkrOjRg7F/rtV8BTD4K3CHBiDMTk3HLZjD2D OGy1cLYsOPKvlUiqmNUf8WK3MQUQghZHvKNvt06QCdWD1t4yqpeWhfffREbziGP0y/sf WJuqE1445WrUgROrcDuHCWvJkO2PeHTKjWVV2xcOet4riraHgLZsugVkOBSGTQL8g/Yc t7GPJ9eeZRTVoTIDYWZpYN1ZUdpJg9TYgUX9sgAdAjrGBLncuvdWYscBdyFFUf1zrr7X T7TJeZ2yXaUBtm++Np6atVdgXvAZ1a2vDpWweMhZ8Tk+uvGu/Qbng8eIUAz+ClQezKSJ /fFg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=TLjxFI4T; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id n22-20020a170906119600b008d0743df869si18943579eja.704.2023.03.24.08.40.03; Fri, 24 Mar 2023 08:40:27 -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=@redhat.com header.s=mimecast20190719 header.b=TLjxFI4T; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232270AbjCXPhm (ORCPT + 99 others); Fri, 24 Mar 2023 11:37:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41644 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231681AbjCXPhf (ORCPT ); Fri, 24 Mar 2023 11:37:35 -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 4EE4120544 for ; Fri, 24 Mar 2023 08:36:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1679672182; 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=MmE27wy0MmMLRHR3IF88SSg1j96JW0EiZjlHTqsz8dM=; b=TLjxFI4Tkbt3AKgLy9sSXJaM7NSDmP3THQEkk8UB/wlLfkv5Ut2xns5T6KGp6m5ORgKTi8 dGMZ/18W5Ys6OEIbHpDAoGp98QodtlmxOKkPKJsEPI6wWtUD5F0iL0iHU+GvoU+XxxNBs1 n70GY/kw+/ZlQQAVCimQjbAfjN8mm5s= Received: from mail-ed1-f70.google.com (mail-ed1-f70.google.com [209.85.208.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-583-HjuYM8ZsPBy0-lbEDR4-Ag-1; Fri, 24 Mar 2023 11:36:20 -0400 X-MC-Unique: HjuYM8ZsPBy0-lbEDR4-Ag-1 Received: by mail-ed1-f70.google.com with SMTP id ev6-20020a056402540600b004bc2358ac04so3751412edb.21 for ; Fri, 24 Mar 2023 08:36:20 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679672179; 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=MmE27wy0MmMLRHR3IF88SSg1j96JW0EiZjlHTqsz8dM=; b=TBFXm+beAsFaMFrUUY1rBNK5uREZdC5QyQmnipJ9EXwgxEMELQig20ND6L472xOT2I IdecQU9u7EM+bZEUHC5WvUXx7nvoJUzroHA8e5UX25E2h48Ml55WE9GHQz8Sf9OFIcdZ Tuh+IbNcVgpTbb6bwjcgHEhHKmz28SQ3N1u+/Fh7wv5bP1OS1wW4BL5DDWXw5AWhJFos t1WhUCB/qCMemXCt884gEWHqE7Wd3hcNVCK6FAwWQdwNrl36vrGeZJ9Q/aLrz509o1AN tj/FlrDe8bmY+hVSaas8bhBWb7CAwcXcEqsE2vSRuiR76cz6VISxb0IL+YBVklydxseP RTjw== X-Gm-Message-State: AAQBX9d4GCTmcfn1rU/CvNsjG04/PKQsZcDW2yj2aGmssC7sOgoVMQKk XfyJuW8HWOjIeeyLol5w2SOZvhmQdJW5pFjruzXR921MQuJt0Agj6P9tSETlsnz6Zgu+oZJA2jn O3GxF+MJjf5nKDi9IxRa7au/5 X-Received: by 2002:a17:906:fa0b:b0:8b8:c06e:52d8 with SMTP id lo11-20020a170906fa0b00b008b8c06e52d8mr2946642ejb.36.1679672179809; Fri, 24 Mar 2023 08:36:19 -0700 (PDT) X-Received: by 2002:a17:906:fa0b:b0:8b8:c06e:52d8 with SMTP id lo11-20020a170906fa0b00b008b8c06e52d8mr2946615ejb.36.1679672179513; Fri, 24 Mar 2023 08:36:19 -0700 (PDT) Received: from localhost.localdomain (host-82-53-134-98.retail.telecomitalia.it. [82.53.134.98]) by smtp.gmail.com with ESMTPSA id a27-20020a509b5b000000b00501dd53dbfbsm5468613edj.75.2023.03.24.08.36.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Mar 2023 08:36:18 -0700 (PDT) From: Stefano Garzarella To: virtualization@lists.linux-foundation.org Cc: stefanha@redhat.com, Jason Wang , linux-kernel@vger.kernel.org, Andrey Zhadchenko , "Michael S. Tsirkin" , kvm@vger.kernel.org, netdev@vger.kernel.org, eperezma@redhat.com, Stefano Garzarella Subject: [PATCH v4 1/9] vdpa: add bind_mm/unbind_mm callbacks Date: Fri, 24 Mar 2023 16:35:59 +0100 Message-Id: <20230324153607.46836-2-sgarzare@redhat.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230324153607.46836-1-sgarzare@redhat.com> References: <20230324153607.46836-1-sgarzare@redhat.com> MIME-Version: 1.0 Content-type: text/plain X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE autolearn=unavailable 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1761264195529597957?= X-GMAIL-MSGID: =?utf-8?q?1761264195529597957?= These new optional callbacks is used to bind/unbind the device to a specific address space so the vDPA framework can use VA when these callbacks are implemented. Suggested-by: Jason Wang Acked-by: Jason Wang Signed-off-by: Stefano Garzarella --- Notes: v2: - removed `struct task_struct *owner` param (unused for now, maybe useful to support cgroups) [Jason] - add unbind_mm callback [Jason] include/linux/vdpa.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/include/linux/vdpa.h b/include/linux/vdpa.h index 43f59ef10cc9..369c21394284 100644 --- a/include/linux/vdpa.h +++ b/include/linux/vdpa.h @@ -290,6 +290,14 @@ struct vdpa_map_file { * @vdev: vdpa device * @idx: virtqueue index * Returns pointer to structure device or error (NULL) + * @bind_mm: Bind the device to a specific address space + * so the vDPA framework can use VA when this + * callback is implemented. (optional) + * @vdev: vdpa device + * @mm: address space to bind + * @unbind_mm: Unbind the device from the address space + * bound using the bind_mm callback. (optional) + * @vdev: vdpa device * @free: Free resources that belongs to vDPA (optional) * @vdev: vdpa device */ @@ -351,6 +359,8 @@ struct vdpa_config_ops { int (*set_group_asid)(struct vdpa_device *vdev, unsigned int group, unsigned int asid); struct device *(*get_vq_dma_dev)(struct vdpa_device *vdev, u16 idx); + int (*bind_mm)(struct vdpa_device *vdev, struct mm_struct *mm); + void (*unbind_mm)(struct vdpa_device *vdev); /* Free device resources */ void (*free)(struct vdpa_device *vdev); From patchwork Fri Mar 24 15:36:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefano Garzarella X-Patchwork-Id: 74614 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp728364vqo; Fri, 24 Mar 2023 08:55:00 -0700 (PDT) X-Google-Smtp-Source: AKy350b7EC4AqhgNd2zuPKWaUft4VEqHwxYfMQ8U1g+TrhRB59AHGnXD6rLe14N3BWUyaWuH3B/K X-Received: by 2002:a17:903:2347:b0:1a0:50bd:31c0 with SMTP id c7-20020a170903234700b001a050bd31c0mr3651567plh.24.1679673300536; Fri, 24 Mar 2023 08:55:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679673300; cv=none; d=google.com; s=arc-20160816; b=JV3iHpJGOmLTn0HhKdgBf4RhTDYKaZFTi6F12yI0BA1VqEK2hrDjxjkM2q8qpLgEz2 198KF56FLa7pDsOdlg4ZctTmamwTPpzbU5i4i8U9SybwFtqQEPthV0TMXFw9dkN1R0Sk WW37uciqvnsERnnWebygqTu3vHhVlTCmJQXsCCaI0naFhlRvlDgWuTl+P6ZnunQq1bN0 dRhn/D1Y9epQYoszol7uJUj7hpdScE9lJuEI0cGwYg2jfGG8iH157v/2Cq3RBGUiIahu 4yMDXCwwOFMm59l9MShuvZAXPLOZPyXoAXK+sw/TtkoxZiRwQUkMaJdrZXGTtpgzfxkZ nGKw== 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:date:subject:cc:to:from :dkim-signature; bh=4MlJ8boSBAN33TyWlK9MdNtuYbz66i2Pl/D7dBKo3iE=; b=MqtQnh304pzcXhQUJ5PieebK+ypK8siCQxKrjSZRVRwyKqOQkc6Fu1upPceHuvMPZV FmOGaJ2tXi8fh35Tgf2mW4+1JmqDXTH5Rr8SmaidJNnXP9CM9TT41r03GUVFuiroqUfu 0zbszWNB0fYFuFNKFyrwyqWOBBNv5eRE/enqaVkLs1+PYFOnI+od4dc7o1cx3WhRUmu4 MLuM81ogvzNW7uDU9/XGkNDSGYSMxqDuzBMtGNnwoQBicsCVRcig9ypGHbdMxRzPtXXc dtOC26Ef1HwQtu828nK13TVNSGMzSB5PlxSGYdOFWLyoPwk+eRv8iivhJtm2EVqKYARj ht7w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=cUboR778; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g12-20020a170902e38c00b0019d038765f6si20001111ple.449.2023.03.24.08.54.48; Fri, 24 Mar 2023 08:55:00 -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=@redhat.com header.s=mimecast20190719 header.b=cUboR778; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232335AbjCXPhr (ORCPT + 99 others); Fri, 24 Mar 2023 11:37:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41802 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231937AbjCXPhk (ORCPT ); Fri, 24 Mar 2023 11:37:40 -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 ESMTPS id 8FD0D20A3F for ; Fri, 24 Mar 2023 08:36:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1679672184; 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=4MlJ8boSBAN33TyWlK9MdNtuYbz66i2Pl/D7dBKo3iE=; b=cUboR778KpKd1GmvNtCHbLGJpDevVFxJ3Ly+6aC1tYwc/ia9GiBPZ8J8JJkWj469kCd5QH L3z8MYU9oSZD3jLRxIqz5L2pm2qaK0Gds1dCGHE8flP140/InX358G0KfTMwVWMRLj3oVV t9ZJKzetnYRNIhtCXBQw8PoLPlZieCA= Received: from mail-ed1-f72.google.com (mail-ed1-f72.google.com [209.85.208.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-94-ghBU36WOO1SKQsST29Z3ng-1; Fri, 24 Mar 2023 11:36:23 -0400 X-MC-Unique: ghBU36WOO1SKQsST29Z3ng-1 Received: by mail-ed1-f72.google.com with SMTP id es16-20020a056402381000b004fa3e04c882so3735219edb.10 for ; Fri, 24 Mar 2023 08:36:22 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679672182; 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=4MlJ8boSBAN33TyWlK9MdNtuYbz66i2Pl/D7dBKo3iE=; b=NwxcSNfZUY00UswhNDPc47g/zbX8cWQwiwHeudIwlxpAkcLSuf+R9DbW5WEy3kUDOw zEPW2QyD0q3GKeoGA56zOMDi+8bivL8P+TFr3z3elxl5N/2/MLqF2r0QL/SEeQ92AwAn W+YeQ64quF0aGTWT1IVskZTrcJt18lZTGJEVDZ3bXRDtTPaESl3g7Cd56ewFOkI5nfWA rRRg2Q11CgHO4euFx87DfsknVw/mA1H1Q/iBpY5gY66BlJ7p1TDdl9oLXmwN9fLb2qE9 ZpYy4T5b7RxJHSWdGBfnPihHK33cjjKdbU+0P0uquaazoAO/h6zeq7UXOGX4Yi+d6eDW DtbA== X-Gm-Message-State: AAQBX9eeMFBLaY3Pq7Q+2OYzl49P0ZBllrw0OPX/xImLROGoxtdgAEib atic6ZV9lhNLbvoDV2QKXLjEiXs5xn3ZFdkgiLlkeZc+hQC4dSULDiCzgRoLkoQzp/QgpX3NWOT dun+YJaS542OIMTAS1W8swpzp X-Received: by 2002:aa7:c790:0:b0:4fc:d837:2c44 with SMTP id n16-20020aa7c790000000b004fcd8372c44mr3252026eds.35.1679672181884; Fri, 24 Mar 2023 08:36:21 -0700 (PDT) X-Received: by 2002:aa7:c790:0:b0:4fc:d837:2c44 with SMTP id n16-20020aa7c790000000b004fcd8372c44mr3252001eds.35.1679672181598; Fri, 24 Mar 2023 08:36:21 -0700 (PDT) Received: from localhost.localdomain (host-82-53-134-98.retail.telecomitalia.it. [82.53.134.98]) by smtp.gmail.com with ESMTPSA id a27-20020a509b5b000000b00501dd53dbfbsm5468613edj.75.2023.03.24.08.36.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Mar 2023 08:36:20 -0700 (PDT) From: Stefano Garzarella To: virtualization@lists.linux-foundation.org Cc: stefanha@redhat.com, Jason Wang , linux-kernel@vger.kernel.org, Andrey Zhadchenko , "Michael S. Tsirkin" , kvm@vger.kernel.org, netdev@vger.kernel.org, eperezma@redhat.com, Stefano Garzarella Subject: [PATCH v4 2/9] vhost-vdpa: use bind_mm/unbind_mm device callbacks Date: Fri, 24 Mar 2023 16:36:00 +0100 Message-Id: <20230324153607.46836-3-sgarzare@redhat.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230324153607.46836-1-sgarzare@redhat.com> References: <20230324153607.46836-1-sgarzare@redhat.com> MIME-Version: 1.0 Content-type: text/plain X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE autolearn=unavailable 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1761265111260589064?= X-GMAIL-MSGID: =?utf-8?q?1761265111260589064?= When the user call VHOST_SET_OWNER ioctl and the vDPA device has `use_va` set to true, let's call the bind_mm callback. In this way we can bind the device to the user address space and directly use the user VA. The unbind_mm callback is called during the release after stopping the device. Signed-off-by: Stefano Garzarella --- Notes: v4: - added new switch after vhost_dev_ioctl() [Jason] v3: - added `case VHOST_SET_OWNER` in vhost_vdpa_unlocked_ioctl() [Jason] v2: - call the new unbind_mm callback during the release [Jason] - avoid to call bind_mm callback after the reset, since the device is not detaching it now during the reset drivers/vhost/vdpa.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c index 7be9d9d8f01c..3824c249612f 100644 --- a/drivers/vhost/vdpa.c +++ b/drivers/vhost/vdpa.c @@ -219,6 +219,28 @@ static int vhost_vdpa_reset(struct vhost_vdpa *v) return vdpa_reset(vdpa); } +static long vhost_vdpa_bind_mm(struct vhost_vdpa *v) +{ + struct vdpa_device *vdpa = v->vdpa; + const struct vdpa_config_ops *ops = vdpa->config; + + if (!vdpa->use_va || !ops->bind_mm) + return 0; + + return ops->bind_mm(vdpa, v->vdev.mm); +} + +static void vhost_vdpa_unbind_mm(struct vhost_vdpa *v) +{ + struct vdpa_device *vdpa = v->vdpa; + const struct vdpa_config_ops *ops = vdpa->config; + + if (!vdpa->use_va || !ops->unbind_mm) + return; + + ops->unbind_mm(vdpa); +} + static long vhost_vdpa_get_device_id(struct vhost_vdpa *v, u8 __user *argp) { struct vdpa_device *vdpa = v->vdpa; @@ -716,6 +738,17 @@ static long vhost_vdpa_unlocked_ioctl(struct file *filep, break; } + if (r) + goto out; + + switch (cmd) { + case VHOST_SET_OWNER: + r = vhost_vdpa_bind_mm(v); + if (r) + vhost_dev_reset_owner(d, NULL); + break; + } +out: mutex_unlock(&d->mutex); return r; } @@ -1287,6 +1320,7 @@ static int vhost_vdpa_release(struct inode *inode, struct file *filep) vhost_vdpa_clean_irq(v); vhost_vdpa_reset(v); vhost_dev_stop(&v->vdev); + vhost_vdpa_unbind_mm(v); vhost_vdpa_config_put(v); vhost_vdpa_cleanup(v); mutex_unlock(&d->mutex); From patchwork Fri Mar 24 15:36:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefano Garzarella X-Patchwork-Id: 74602 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp720010vqo; Fri, 24 Mar 2023 08:40:50 -0700 (PDT) X-Google-Smtp-Source: AKy350ZFwla/RUmmddgsj4JhlKHSgHWOJ0NSvHm9/Y2OW5MBpPNo9qY0DX7TUUg5DxrCQMV8veBX X-Received: by 2002:aa7:c305:0:b0:502:246e:6739 with SMTP id l5-20020aa7c305000000b00502246e6739mr1638453edq.27.1679672449973; Fri, 24 Mar 2023 08:40:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679672449; cv=none; d=google.com; s=arc-20160816; b=DjFu7dnwvOJYHWzTr+7q0nmBJVHWnlGhcM88y6uBZz1pVQ7zFt7sEEHqihuwJVfKoX 9Zo8AnVkxQbOmbwWHZkAZ6Io5g3FW2saE8lQYomv1qDiefDR9qJDBKjfx1R8v29rDQw2 Xb/KUTwUUb1Ss8+VoAxYGteGb4CRZOkfJYp9OdKSwlNq2L8ODSeS3+cyIXE6ytj1ywwx D/iUEtp/b0X5WuigQQRheTNRUEX7mqDQmfn/fJh7GsmH6D1d21IDD6MhbpaTBGg+AvCl CmYriiZtxKwnQqAuLmOMyr6w+k32SedQHsU1/S7mcV1Wm1dXsmd/glitP1mz6KgtOn0x ys5g== 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:date:subject:cc:to:from :dkim-signature; bh=Joco4lLfDzF0rb3l626/1qV/yRiN74V9xc1/q5OsmGA=; b=ovYWxC0JWDJyH4Ii+ShmJSW/cUE2P5WaavVl+ID5TZBkSKVJLn//6RifbyhwkyK1ze ua/qw+sMaMxMPV/P0X5YXyJGlCrq8BarDiUqIK2fWU/+6fJ5Neg2SDtgZ2M4Iy4HA2PK 2OFLhN7jmMMfUBV0+CLXLG9wYvdvGD/FEALSyF74hDPq1k/ofHt2kVwBPQpl5cvVCqOG Vliat/jnJkuYh+6zh6WbJwy9Hf+bc8QRRBrti4TJoT8BhF1E6QqDfja7iifAWHuIlTdH 6Jnv5JDBZ7WfSmFa83L/yIuef7/59NP6MmLaaV6TqRdB4Oayszjy+chKMGtci+SdmEEi +WjQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=CioaTP5m; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id b19-20020aa7d493000000b004fc0c645ab1si23336427edr.234.2023.03.24.08.40.26; Fri, 24 Mar 2023 08:40:49 -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=@redhat.com header.s=mimecast20190719 header.b=CioaTP5m; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231616AbjCXPh4 (ORCPT + 99 others); Fri, 24 Mar 2023 11:37:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41844 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232242AbjCXPhl (ORCPT ); Fri, 24 Mar 2023 11:37:41 -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 ESMTPS id 3A32D1F909 for ; Fri, 24 Mar 2023 08:36:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1679672186; 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=Joco4lLfDzF0rb3l626/1qV/yRiN74V9xc1/q5OsmGA=; b=CioaTP5m806hVsHe9I7fmZKirgQLITqFqFbq9dkg7J2SQ9KuLBjGfsYw5TRftB9TGsGarG 9xgWAoMZfBV6fEhKBbQfZNAro5xrp1Fvh6qtBcptPSPqmyjyNZfm4VGeEOs5FDlVyuYapo 6pzTPlTqSkNzOjBsJG08kGFCX4zmlEo= Received: from mail-ed1-f69.google.com (mail-ed1-f69.google.com [209.85.208.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-672-HnOqjBa1NjeSLY5g26URMg-1; Fri, 24 Mar 2023 11:36:25 -0400 X-MC-Unique: HnOqjBa1NjeSLY5g26URMg-1 Received: by mail-ed1-f69.google.com with SMTP id es16-20020a056402381000b004fa3e04c882so3735379edb.10 for ; Fri, 24 Mar 2023 08:36:24 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679672184; 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=Joco4lLfDzF0rb3l626/1qV/yRiN74V9xc1/q5OsmGA=; b=pChWeiFKZUsEStiYkj4mnBVF9vxHiVlt0j1aojfQtWiSbRqq7s98pRSCbvjIbbH5go QWJ2rsOB5Pkrt7QcEEqh4cuKATmVeeXBzW1bHjgcEjjTu0+u0mbyAjhcNeRXKJTkHhaw nfrMZ+4FYyj0KHRx4jGfkdHPcUcXrC60Rz5+Nwm9Ap3Wbx8g8K+si7u9Dwd9rZ4PxkZ/ 0moByCe7fDM5Ux7yVPEofRHfrfyMkI/ZT6/4x0hUhpNSoi4aqeeWmqcyWHJBeF4aQ4Jy /JvCly1w4ivP5Eaym09jjvlUEJMVQxB8HjWsc1yQMo3QoSS5l8eoXIaYMbz7W0r2vnq3 Tqvw== X-Gm-Message-State: AAQBX9cxBYtQegaZkExObrfP5+b2qAEmVvZkLdP02KOxdfi4Qb6jI+Gw CeNVidspEEiFNs2VT3KMD6fn7dMk+6eKrQ59kREXvohBLBtoHD4rdAOMMzmI2i76oIVuXjCr1Ff Y1BRG+mlaqLl0xD68KkCyKbhH X-Received: by 2002:aa7:db59:0:b0:4fa:7fd8:8f6a with SMTP id n25-20020aa7db59000000b004fa7fd88f6amr2931935edt.38.1679672184008; Fri, 24 Mar 2023 08:36:24 -0700 (PDT) X-Received: by 2002:aa7:db59:0:b0:4fa:7fd8:8f6a with SMTP id n25-20020aa7db59000000b004fa7fd88f6amr2931918edt.38.1679672183746; Fri, 24 Mar 2023 08:36:23 -0700 (PDT) Received: from localhost.localdomain (host-82-53-134-98.retail.telecomitalia.it. [82.53.134.98]) by smtp.gmail.com with ESMTPSA id a27-20020a509b5b000000b00501dd53dbfbsm5468613edj.75.2023.03.24.08.36.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Mar 2023 08:36:22 -0700 (PDT) From: Stefano Garzarella To: virtualization@lists.linux-foundation.org Cc: stefanha@redhat.com, Jason Wang , linux-kernel@vger.kernel.org, Andrey Zhadchenko , "Michael S. Tsirkin" , kvm@vger.kernel.org, netdev@vger.kernel.org, eperezma@redhat.com, Stefano Garzarella , "Fabio M. De Francesco" Subject: [PATCH v4 3/9] vringh: replace kmap_atomic() with kmap_local_page() Date: Fri, 24 Mar 2023 16:36:01 +0100 Message-Id: <20230324153607.46836-4-sgarzare@redhat.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230324153607.46836-1-sgarzare@redhat.com> References: <20230324153607.46836-1-sgarzare@redhat.com> MIME-Version: 1.0 Content-type: text/plain X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE autolearn=unavailable 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1761264218968805926?= X-GMAIL-MSGID: =?utf-8?q?1761264218968805926?= kmap_atomic() is deprecated in favor of kmap_local_page() since commit f3ba3c710ac5 ("mm/highmem: Provide kmap_local*"). With kmap_local_page() the mappings are per thread, CPU local, can take page-faults, and can be called from any context (including interrupts). Furthermore, the tasks can be preempted and, when they are scheduled to run again, the kernel virtual addresses are restored and still valid. kmap_atomic() is implemented like a kmap_local_page() which also disables page-faults and preemption (the latter only for !PREEMPT_RT kernels, otherwise it only disables migration). The code within the mappings/un-mappings in getu16_iotlb() and putu16_iotlb() don't depend on the above-mentioned side effects of kmap_atomic(), so that mere replacements of the old API with the new one is all that is required (i.e., there is no need to explicitly add calls to pagefault_disable() and/or preempt_disable()). This commit reuses a "boiler plate" commit message from Fabio, who has already did this change in several places. Cc: "Fabio M. De Francesco" Reviewed-by: Fabio M. De Francesco Acked-by: Jason Wang Signed-off-by: Stefano Garzarella --- Notes: v3: - credited Fabio for the commit message - added reference to the commit that deprecated kmap_atomic() [Jason] v2: - added this patch since checkpatch.pl complained about deprecation of kmap_atomic() touched by next patch drivers/vhost/vringh.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/drivers/vhost/vringh.c b/drivers/vhost/vringh.c index a1e27da54481..0ba3ef809e48 100644 --- a/drivers/vhost/vringh.c +++ b/drivers/vhost/vringh.c @@ -1220,10 +1220,10 @@ static inline int getu16_iotlb(const struct vringh *vrh, if (ret < 0) return ret; - kaddr = kmap_atomic(iov.bv_page); + kaddr = kmap_local_page(iov.bv_page); from = kaddr + iov.bv_offset; *val = vringh16_to_cpu(vrh, READ_ONCE(*(__virtio16 *)from)); - kunmap_atomic(kaddr); + kunmap_local(kaddr); return 0; } @@ -1241,10 +1241,10 @@ static inline int putu16_iotlb(const struct vringh *vrh, if (ret < 0) return ret; - kaddr = kmap_atomic(iov.bv_page); + kaddr = kmap_local_page(iov.bv_page); to = kaddr + iov.bv_offset; WRITE_ONCE(*(__virtio16 *)to, cpu_to_vringh16(vrh, val)); - kunmap_atomic(kaddr); + kunmap_local(kaddr); return 0; } From patchwork Fri Mar 24 15:36:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefano Garzarella X-Patchwork-Id: 74603 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp720451vqo; Fri, 24 Mar 2023 08:41:34 -0700 (PDT) X-Google-Smtp-Source: AKy350ZwEHE6lOOvKxOpiIUqUHIFe8NFxCtjXvn4Rp6CldJgtp8HcHjJkHdk+YnT3W3xxzRCEa5j X-Received: by 2002:a17:906:3117:b0:8ab:4c4:d0f6 with SMTP id 23-20020a170906311700b008ab04c4d0f6mr3317881ejx.56.1679672494604; Fri, 24 Mar 2023 08:41:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679672494; cv=none; d=google.com; s=arc-20160816; b=xw4tKX+Xo943s9iLBwaZMADTxh0u3agg+wVFZrLbU3fqEoJh0m396yy82u/UPXGJUd igbZq7qLAtTQYO6gICjTo+jWu2pO90Lfnps78S6d419T8pU2zZ8OLukB1Bz7f6qHshtW KEY/cxDP0g9IKseR816NSweU7ejIukU/P+kI9AmwjUfOVwQB9jkPeZUR9/nTXV0q0QxS NiNbvYZ/qjCIzMdyE82qjY0kuqmjjHB+n0/bmHSsJJAzOrhkfPemqIvAX2P6ownOpxS8 W0Hiia+iC6K0UGwqeupD/y7CJuE+IF8YyU0Qyp4cEfGZIivF2yIyFUw9tL3IrHjk9782 ir8w== 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:date:subject:cc:to:from :dkim-signature; bh=reoAM7f+Ql0t7teTIED97nCtpsbVHLsQxA9mgF0KDac=; b=cIT8BgsGz+jodZG/HJN43y5v+TjN9nD+cyOn4IlyclpWSByIhA6PbvBERZovB5W4b5 63MMUx7pefVflgh1ZTDaCffpIAbn5n51rESlXDc878/AAVETAAD+tqEUMZo2KqTUYmTE itcDDoQNjaestO+NQbCTQUIRGpZIg+Nmb03Sotc0O1Z4SdyP31iTT/6KLlErA0c1d4hY 4fzhFA8eG8SrNYU3iYPX5NLREAR6qB/5hoHsxxEajGfEWMn+TmPYT2vtKSb888TTU49C 4h1wBDBD5BynTk0S8vTi7yEaI2uqI45B6zgynPuz9eszLx0JPN76MIDrTPsryYInwVzO 4Gxg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=bFYuR7hF; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id fy28-20020a1709069f1c00b009320a6dc4ddsi20016774ejc.316.2023.03.24.08.41.11; Fri, 24 Mar 2023 08:41:34 -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=@redhat.com header.s=mimecast20190719 header.b=bFYuR7hF; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231976AbjCXPiM (ORCPT + 99 others); Fri, 24 Mar 2023 11:38:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42328 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232504AbjCXPhx (ORCPT ); Fri, 24 Mar 2023 11:37:53 -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 4F8481E9E3 for ; Fri, 24 Mar 2023 08:36:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1679672209; 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=reoAM7f+Ql0t7teTIED97nCtpsbVHLsQxA9mgF0KDac=; b=bFYuR7hF1KJNT3DhgJkC8CQMwaMD8LkztP4INrcOpyTXyPiTG1cfr75DfJCpRhjHLIt3h8 2MVsV9aYJJ0WftNfqs6nUWEvfTdRLqlWgR8iLlMokfeGV95vuGpVgj9IXNzTOAIQd6XupQ JViWEHp4KwlAVahOh1MHlsLG3RthFKo= Received: from mail-ed1-f69.google.com (mail-ed1-f69.google.com [209.85.208.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-437-lo5G6hGrPCCJEZMeDvjQqA-1; Fri, 24 Mar 2023 11:36:48 -0400 X-MC-Unique: lo5G6hGrPCCJEZMeDvjQqA-1 Received: by mail-ed1-f69.google.com with SMTP id k12-20020a50c8cc000000b004accf30f6d3so3744808edh.14 for ; Fri, 24 Mar 2023 08:36:48 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679672206; 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=reoAM7f+Ql0t7teTIED97nCtpsbVHLsQxA9mgF0KDac=; b=f27QGOZHL0hJmJQbitVX1HJp9Ajv+KOVvFoPDmOO81dpX9i+j3FOFDlQPhH6cBqXq8 LxLeY7+fhLllAoI3IBYFJG8gTSTAuoL0fjCoq7Y230iQKEzB/NwNgcJK0jXJjg3Ddn6c as6Jgb7BM9AMksQI55G/Xf0HMRvJmnRG1jayrwUiZLd7z7wL899yQ5/UBAl4v3C8AKIR wvaTCVs3tnqZKZPmvZX6mCE8o6TFMhITr6zWYBtOHJ5IHCBnGQGp8FaXMx34NxUJ745F wKwAf9R8bICoEYPmYLfmgI373O+hwisCvAbdze82tbyJ1m9Ur9J9mvzSyWZhafH53zt7 NE3g== X-Gm-Message-State: AAQBX9c4lvIbnUici6kfVcULCYpZc20yHvq+/C5SNl2QOhDfBE9XX759 Ev/A8MY1jJfKeYaxxpcdXKz3rvIPwxUceVLeSL5vwkyHxYXwdE6+F1Ur/+msiQ8wtSY7e0znjVA Up97ENYVTLHXKEhTjludBSxyw X-Received: by 2002:a05:6402:1002:b0:501:c547:2135 with SMTP id c2-20020a056402100200b00501c5472135mr3055023edu.36.1679672206666; Fri, 24 Mar 2023 08:36:46 -0700 (PDT) X-Received: by 2002:a05:6402:1002:b0:501:c547:2135 with SMTP id c2-20020a056402100200b00501c5472135mr3055009edu.36.1679672206395; Fri, 24 Mar 2023 08:36:46 -0700 (PDT) Received: from localhost.localdomain (host-82-53-134-98.retail.telecomitalia.it. [82.53.134.98]) by smtp.gmail.com with ESMTPSA id g25-20020a50d0d9000000b00501c2a9e16dsm7987307edf.74.2023.03.24.08.36.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Mar 2023 08:36:45 -0700 (PDT) From: Stefano Garzarella To: virtualization@lists.linux-foundation.org Cc: stefanha@redhat.com, Jason Wang , linux-kernel@vger.kernel.org, Andrey Zhadchenko , "Michael S. Tsirkin" , kvm@vger.kernel.org, netdev@vger.kernel.org, eperezma@redhat.com, Stefano Garzarella Subject: [PATCH v4 4/9] vringh: define the stride used for translation Date: Fri, 24 Mar 2023 16:36:02 +0100 Message-Id: <20230324153607.46836-5-sgarzare@redhat.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230324153607.46836-1-sgarzare@redhat.com> References: <20230324153607.46836-1-sgarzare@redhat.com> MIME-Version: 1.0 Content-type: text/plain X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE autolearn=unavailable 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1761264265855261820?= X-GMAIL-MSGID: =?utf-8?q?1761264265855261820?= Define a macro to be reused in the different parts of the code. Useful for the next patches where we add more arrays to manage also translations with user VA. Suggested-by: Eugenio Perez Martin Signed-off-by: Stefano Garzarella --- Notes: v4: - added this patch with the changes extracted from the next patch [Eugenio] - used _STRIDE instead of _SIZE [Eugenio] drivers/vhost/vringh.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/drivers/vhost/vringh.c b/drivers/vhost/vringh.c index 0ba3ef809e48..4aee230f7622 100644 --- a/drivers/vhost/vringh.c +++ b/drivers/vhost/vringh.c @@ -1141,13 +1141,15 @@ static int iotlb_translate(const struct vringh *vrh, return ret; } +#define IOTLB_IOV_STRIDE 16 + static inline int copy_from_iotlb(const struct vringh *vrh, void *dst, void *src, size_t len) { u64 total_translated = 0; while (total_translated < len) { - struct bio_vec iov[16]; + struct bio_vec iov[IOTLB_IOV_STRIDE]; struct iov_iter iter; u64 translated; int ret; @@ -1180,7 +1182,7 @@ static inline int copy_to_iotlb(const struct vringh *vrh, void *dst, u64 total_translated = 0; while (total_translated < len) { - struct bio_vec iov[16]; + struct bio_vec iov[IOTLB_IOV_STRIDE]; struct iov_iter iter; u64 translated; int ret; From patchwork Fri Mar 24 15:39:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Stefano Garzarella X-Patchwork-Id: 74617 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp729401vqo; Fri, 24 Mar 2023 08:56:49 -0700 (PDT) X-Google-Smtp-Source: AKy350aMb4G18z+1bxI4E/nCWjLgXDOOgi1GXEFOYNv2yZa7sPNbczVv151dZnJcYIwL23HQ24/K X-Received: by 2002:a17:903:110e:b0:1a1:bfd6:f890 with SMTP id n14-20020a170903110e00b001a1bfd6f890mr3694977plh.9.1679673409532; Fri, 24 Mar 2023 08:56:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679673409; cv=none; d=google.com; s=arc-20160816; b=TWLW0MNxy7UuPwv9jqIiYmkg5tcVhgAwr4lvjZJDdWMlbxDja6dK4jFiyFHMKHR6PX Bh604pbPtTZ5z7a1gY8kV2LgykD3+LH+KlEI7csM2E1COrcJrYfgOu7fPep8tQ5XxDGG 4hDw8qNXYu7/EJ7J+Fojbvi/Oixst+dkNn/ixG2fAzqonPMRC7L+RfZwsnXYte8uvQMY JWskR/PAz/vyGOncFziJgxpL8nEbPzZCf9lIcc8GYLpOSydFrlN9ooYnyhdtOnr2n1rV S+yMzgt4V7cEwMnqfSq5ilGYGGzGfJrCEQXZ7EUmQRh0JaRlmy5ZC/PthFY8jZ6mbO5a OCmw== 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:date:subject:cc:to:from :dkim-signature; bh=+04OpsvyUrUsuT6XcLeeN9Ze/5Klc/NacWCLWIQp650=; b=D3ytiQt8SYHP/Z1Wzi8FMV/e6q4kZCOgb7YpE3POKLdZekKQc3Ua75rW01onhx9sRP BeeLCa7vy8lXor2RdjTz5wLZxQOZwEnUpGGKp+AxLUq8B2D9nmTE3PN3a5xEWJsjZeNW QB8W4MIB403wvh7MwzjWQDZG+lviBwl2Fh+KhThUfS9OX7xOLtTXo51FWCDPdDLplg4d EWCPW/i20ylcUaoYKxDMKFJEoWMnLu2tEd9bVF2ROm8FSqh+lDfhVDVHeYrPobMjynpA +PBNIbTWSIZYOOUa9o4ptvG6o7UXJpjUEYe1+xC4QWkiaeszB2GBePc0jldeDLBnVyQ4 s7DA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=DFk1pdQ0; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id e17-20020a17090301d100b001a05a44093csi23119156plh.58.2023.03.24.08.56.36; Fri, 24 Mar 2023 08:56:49 -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=@redhat.com header.s=mimecast20190719 header.b=DFk1pdQ0; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231580AbjCXPkb (ORCPT + 99 others); Fri, 24 Mar 2023 11:40:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48804 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231508AbjCXPk3 (ORCPT ); Fri, 24 Mar 2023 11:40:29 -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 ESMTPS id A7EB31F933 for ; Fri, 24 Mar 2023 08:39:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1679672372; 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=+04OpsvyUrUsuT6XcLeeN9Ze/5Klc/NacWCLWIQp650=; b=DFk1pdQ07teVn7ZY1Wd+9E0p9T9egSGBCqYCgLZkJDltdPVGKpYAyj24zNx6+EiHvRFpvx 9lz7IFXTP0+X8BU+mZ5YaDVJLrzYSl3s4t8hNvjiD3BE08kTwwQKi77Rw2I8mYI2DMCyhA yf4RIkTErbsQs6RvdUf8a8xvZzMe11s= Received: from mail-ed1-f71.google.com (mail-ed1-f71.google.com [209.85.208.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-232-BhoK-CvqM_ummrG53THxaw-1; Fri, 24 Mar 2023 11:39:31 -0400 X-MC-Unique: BhoK-CvqM_ummrG53THxaw-1 Received: by mail-ed1-f71.google.com with SMTP id s30-20020a508d1e000000b005005cf48a93so3750888eds.8 for ; Fri, 24 Mar 2023 08:39:30 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679672370; 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=+04OpsvyUrUsuT6XcLeeN9Ze/5Klc/NacWCLWIQp650=; b=iMfKv7Qlb+BreUBBTN4zHMYN56Jwz8i1jdnkfqiAlcq7/cc7Uq0QPbFVooR4EkQ+6W Mf+87i3PgoElblgE+JwcJeYF8LQFKcGc+rOXLVhXZOzGdwgYmZIesyfvpctNOFpByIng tDPncgH+joZ4kara3Dy3iorObcJtPmupZOUapPqjoGJqptcDHIkaXa4mXeDTAzhyXP8C M1Yf0aitXAnkVwZWjbRHlVL+SZMSBMGOSEhZnw0rKGpYvnND0qaJh+sCuEwAhBSDSwLI 2TOXZ5150Wce+li7hKP6d0PT7clt9KDfiSqE/Laf+Cp/+hbg/ojAoIu20uJb2nljIF+t Xwlw== X-Gm-Message-State: AAQBX9eb9z25n2LvApuBJtgTc0ne/2HMlLdDmDFnMZRmKZ4neOS/2xfO GKxcY/W+9M1p7kCRP+M2MIa6gx3Aidp9OQR6bHP/Bp53L2OBCCCbGQ9tAk9VZtYG1jRERZXXS5W 1AQ+GewBIHmzCSlbO/Rehs//M X-Received: by 2002:a05:6402:3445:b0:4fd:2b05:1722 with SMTP id l5-20020a056402344500b004fd2b051722mr3182554edc.7.1679672369857; Fri, 24 Mar 2023 08:39:29 -0700 (PDT) X-Received: by 2002:a05:6402:3445:b0:4fd:2b05:1722 with SMTP id l5-20020a056402344500b004fd2b051722mr3182539edc.7.1679672369577; Fri, 24 Mar 2023 08:39:29 -0700 (PDT) Received: from localhost.localdomain (host-82-53-134-98.retail.telecomitalia.it. [82.53.134.98]) by smtp.gmail.com with ESMTPSA id u5-20020a50d505000000b004c09f0ba24dsm10904587edi.48.2023.03.24.08.39.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Mar 2023 08:39:28 -0700 (PDT) From: Stefano Garzarella To: virtualization@lists.linux-foundation.org Cc: Andrey Zhadchenko , Jason Wang , eperezma@redhat.com, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, kvm@vger.kernel.org, stefanha@redhat.com, "Michael S. Tsirkin" , Stefano Garzarella Subject: [PATCH v4 5/9] vringh: support VA with iotlb Date: Fri, 24 Mar 2023 16:39:19 +0100 Message-Id: <20230324153919.47633-1-sgarzare@redhat.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230324153607.46836-1-sgarzare@redhat.com> References: <20230324153607.46836-1-sgarzare@redhat.com> MIME-Version: 1.0 Content-type: text/plain X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE autolearn=unavailable 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1761265224689916928?= X-GMAIL-MSGID: =?utf-8?q?1761265224689916928?= vDPA supports the possibility to use user VA in the iotlb messages. So, let's add support for user VA in vringh to use it in the vDPA simulators. Signed-off-by: Stefano Garzarella Acked-by: Eugenio Pérez --- Notes: v4: - used uintptr_t for `io_addr` [Eugenio] - added `io_addr` and `io_len` variables in iotlb_translate - avoided overflow doing `map->addr - map->start + addr` [Jason] - removed `is_iovec` field from struct iotlb_vec [Jason] - added vringh_init_iotlb_va() [Jason] v3: - refactored avoiding code duplication [Eugenio] v2: - replace kmap_atomic() with kmap_local_page() [see previous patch] - fix cast warnings when build with W=1 C=1 include/linux/vringh.h | 9 +++ drivers/vhost/vringh.c | 171 +++++++++++++++++++++++++++++++++-------- 2 files changed, 148 insertions(+), 32 deletions(-) diff --git a/include/linux/vringh.h b/include/linux/vringh.h index 1991a02c6431..b4edfadf5479 100644 --- a/include/linux/vringh.h +++ b/include/linux/vringh.h @@ -32,6 +32,9 @@ struct vringh { /* Can we get away with weak barriers? */ bool weak_barriers; + /* Use user's VA */ + bool use_va; + /* Last available index we saw (ie. where we're up to). */ u16 last_avail_idx; @@ -284,6 +287,12 @@ int vringh_init_iotlb(struct vringh *vrh, u64 features, struct vring_avail *avail, struct vring_used *used); +int vringh_init_iotlb_va(struct vringh *vrh, u64 features, + unsigned int num, bool weak_barriers, + struct vring_desc *desc, + struct vring_avail *avail, + struct vring_used *used); + int vringh_getdesc_iotlb(struct vringh *vrh, struct vringh_kiov *riov, struct vringh_kiov *wiov, diff --git a/drivers/vhost/vringh.c b/drivers/vhost/vringh.c index 4aee230f7622..771c2aba8aac 100644 --- a/drivers/vhost/vringh.c +++ b/drivers/vhost/vringh.c @@ -1094,10 +1094,17 @@ EXPORT_SYMBOL(vringh_need_notify_kern); #if IS_REACHABLE(CONFIG_VHOST_IOTLB) +struct iotlb_vec { + union { + struct iovec *iovec; + struct bio_vec *bvec; + } iov; + size_t count; +}; + static int iotlb_translate(const struct vringh *vrh, u64 addr, u64 len, u64 *translated, - struct bio_vec iov[], - int iov_size, u32 perm) + struct iotlb_vec *ivec, u32 perm) { struct vhost_iotlb_map *map; struct vhost_iotlb *iotlb = vrh->iotlb; @@ -1107,9 +1114,11 @@ static int iotlb_translate(const struct vringh *vrh, spin_lock(vrh->iotlb_lock); while (len > s) { - u64 size, pa, pfn; + uintptr_t io_addr; + size_t io_len; + u64 size; - if (unlikely(ret >= iov_size)) { + if (unlikely(ret >= ivec->count)) { ret = -ENOBUFS; break; } @@ -1124,10 +1133,22 @@ static int iotlb_translate(const struct vringh *vrh, } size = map->size - addr + map->start; - pa = map->addr + addr - map->start; - pfn = pa >> PAGE_SHIFT; - bvec_set_page(&iov[ret], pfn_to_page(pfn), min(len - s, size), - pa & (PAGE_SIZE - 1)); + io_len = min(len - s, size); + io_addr = map->addr - map->start + addr; + + if (vrh->use_va) { + struct iovec *iovec = ivec->iov.iovec; + + iovec[ret].iov_len = io_len; + iovec[ret].iov_base = (void __user *)io_addr; + } else { + u64 pfn = io_addr >> PAGE_SHIFT; + struct bio_vec *bvec = ivec->iov.bvec; + + bvec_set_page(&bvec[ret], pfn_to_page(pfn), io_len, + io_addr & (PAGE_SIZE - 1)); + } + s += size; addr += size; ++ret; @@ -1146,23 +1167,36 @@ static int iotlb_translate(const struct vringh *vrh, static inline int copy_from_iotlb(const struct vringh *vrh, void *dst, void *src, size_t len) { + struct iotlb_vec ivec; + union { + struct iovec iovec[IOTLB_IOV_STRIDE]; + struct bio_vec bvec[IOTLB_IOV_STRIDE]; + } iov; u64 total_translated = 0; + ivec.iov.iovec = iov.iovec; + ivec.count = IOTLB_IOV_STRIDE; + while (total_translated < len) { - struct bio_vec iov[IOTLB_IOV_STRIDE]; struct iov_iter iter; u64 translated; int ret; ret = iotlb_translate(vrh, (u64)(uintptr_t)src, len - total_translated, &translated, - iov, ARRAY_SIZE(iov), VHOST_MAP_RO); + &ivec, VHOST_MAP_RO); if (ret == -ENOBUFS) - ret = ARRAY_SIZE(iov); + ret = IOTLB_IOV_STRIDE; else if (ret < 0) return ret; - iov_iter_bvec(&iter, ITER_SOURCE, iov, ret, translated); + if (vrh->use_va) { + iov_iter_init(&iter, ITER_SOURCE, ivec.iov.iovec, ret, + translated); + } else { + iov_iter_bvec(&iter, ITER_SOURCE, ivec.iov.bvec, ret, + translated); + } ret = copy_from_iter(dst, translated, &iter); if (ret < 0) @@ -1179,23 +1213,36 @@ static inline int copy_from_iotlb(const struct vringh *vrh, void *dst, static inline int copy_to_iotlb(const struct vringh *vrh, void *dst, void *src, size_t len) { + struct iotlb_vec ivec; + union { + struct iovec iovec[IOTLB_IOV_STRIDE]; + struct bio_vec bvec[IOTLB_IOV_STRIDE]; + } iov; u64 total_translated = 0; + ivec.iov.iovec = iov.iovec; + ivec.count = IOTLB_IOV_STRIDE; + while (total_translated < len) { - struct bio_vec iov[IOTLB_IOV_STRIDE]; struct iov_iter iter; u64 translated; int ret; ret = iotlb_translate(vrh, (u64)(uintptr_t)dst, len - total_translated, &translated, - iov, ARRAY_SIZE(iov), VHOST_MAP_WO); + &ivec, VHOST_MAP_WO); if (ret == -ENOBUFS) - ret = ARRAY_SIZE(iov); + ret = IOTLB_IOV_STRIDE; else if (ret < 0) return ret; - iov_iter_bvec(&iter, ITER_DEST, iov, ret, translated); + if (vrh->use_va) { + iov_iter_init(&iter, ITER_DEST, ivec.iov.iovec, ret, + translated); + } else { + iov_iter_bvec(&iter, ITER_DEST, ivec.iov.bvec, ret, + translated); + } ret = copy_to_iter(src, translated, &iter); if (ret < 0) @@ -1212,20 +1259,36 @@ static inline int copy_to_iotlb(const struct vringh *vrh, void *dst, static inline int getu16_iotlb(const struct vringh *vrh, u16 *val, const __virtio16 *p) { - struct bio_vec iov; - void *kaddr, *from; + struct iotlb_vec ivec; + union { + struct iovec iovec[1]; + struct bio_vec bvec[1]; + } iov; + __virtio16 tmp; int ret; + ivec.iov.iovec = iov.iovec; + ivec.count = 1; + /* Atomic read is needed for getu16 */ - ret = iotlb_translate(vrh, (u64)(uintptr_t)p, sizeof(*p), NULL, - &iov, 1, VHOST_MAP_RO); + ret = iotlb_translate(vrh, (u64)(uintptr_t)p, sizeof(*p), + NULL, &ivec, VHOST_MAP_RO); if (ret < 0) return ret; - kaddr = kmap_local_page(iov.bv_page); - from = kaddr + iov.bv_offset; - *val = vringh16_to_cpu(vrh, READ_ONCE(*(__virtio16 *)from)); - kunmap_local(kaddr); + if (vrh->use_va) { + ret = __get_user(tmp, (__virtio16 __user *)ivec.iov.iovec[0].iov_base); + if (ret) + return ret; + } else { + void *kaddr = kmap_local_page(ivec.iov.bvec[0].bv_page); + void *from = kaddr + ivec.iov.bvec[0].bv_offset; + + tmp = READ_ONCE(*(__virtio16 *)from); + kunmap_local(kaddr); + } + + *val = vringh16_to_cpu(vrh, tmp); return 0; } @@ -1233,20 +1296,36 @@ static inline int getu16_iotlb(const struct vringh *vrh, static inline int putu16_iotlb(const struct vringh *vrh, __virtio16 *p, u16 val) { - struct bio_vec iov; - void *kaddr, *to; + struct iotlb_vec ivec; + union { + struct iovec iovec; + struct bio_vec bvec; + } iov; + __virtio16 tmp; int ret; + ivec.iov.iovec = &iov.iovec; + ivec.count = 1; + /* Atomic write is needed for putu16 */ - ret = iotlb_translate(vrh, (u64)(uintptr_t)p, sizeof(*p), NULL, - &iov, 1, VHOST_MAP_WO); + ret = iotlb_translate(vrh, (u64)(uintptr_t)p, sizeof(*p), + NULL, &ivec, VHOST_MAP_RO); if (ret < 0) return ret; - kaddr = kmap_local_page(iov.bv_page); - to = kaddr + iov.bv_offset; - WRITE_ONCE(*(__virtio16 *)to, cpu_to_vringh16(vrh, val)); - kunmap_local(kaddr); + tmp = cpu_to_vringh16(vrh, val); + + if (vrh->use_va) { + ret = __put_user(tmp, (__virtio16 __user *)ivec.iov.iovec[0].iov_base); + if (ret) + return ret; + } else { + void *kaddr = kmap_local_page(ivec.iov.bvec[0].bv_page); + void *to = kaddr + ivec.iov.bvec[0].bv_offset; + + WRITE_ONCE(*(__virtio16 *)to, tmp); + kunmap_local(kaddr); + } return 0; } @@ -1320,11 +1399,39 @@ int vringh_init_iotlb(struct vringh *vrh, u64 features, struct vring_avail *avail, struct vring_used *used) { + vrh->use_va = false; + return vringh_init_kern(vrh, features, num, weak_barriers, desc, avail, used); } EXPORT_SYMBOL(vringh_init_iotlb); +/** + * vringh_init_iotlb_va - initialize a vringh for a ring with IOTLB containing + * user VA. + * @vrh: the vringh to initialize. + * @features: the feature bits for this ring. + * @num: the number of elements. + * @weak_barriers: true if we only need memory barriers, not I/O. + * @desc: the userpace descriptor pointer. + * @avail: the userpace avail pointer. + * @used: the userpace used pointer. + * + * Returns an error if num is invalid. + */ +int vringh_init_iotlb_va(struct vringh *vrh, u64 features, + unsigned int num, bool weak_barriers, + struct vring_desc *desc, + struct vring_avail *avail, + struct vring_used *used) +{ + vrh->use_va = true; + + return vringh_init_kern(vrh, features, num, weak_barriers, + desc, avail, used); +} +EXPORT_SYMBOL(vringh_init_iotlb_va); + /** * vringh_set_iotlb - initialize a vringh for a ring with IOTLB. * @vrh: the vring From patchwork Fri Mar 24 15:39:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Stefano Garzarella X-Patchwork-Id: 74606 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp720901vqo; Fri, 24 Mar 2023 08:42:22 -0700 (PDT) X-Google-Smtp-Source: AKy350ZcDe6+yCCQymppN2j5O9pncZro9pBJlzTdMik3IJfX418p4sOmuIR51zPyW+jhX3zVg/Pn X-Received: by 2002:a17:906:b1ce:b0:933:3fda:94fb with SMTP id bv14-20020a170906b1ce00b009333fda94fbmr3741811ejb.10.1679672542024; Fri, 24 Mar 2023 08:42:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679672542; cv=none; d=google.com; s=arc-20160816; b=VbdKveAFQW/7FIpqSsxl4fNpFtFoywWNGH2m+eyfu/ApVkGGzDE9hLzQ1h708hSmxL Db/OWEF4SbG37MhV24gHyr/xhG8hXkoSMtAwsmli1FFRVMPYNSq4R/EEf1IRerJNi9M8 yDuZV3JPdjkgG85HTizqW6VWS0xxwD2wIZAR5XUYD4KwSHw2mbtsACOSqyJCBE8n26tX RmURznhMMBNGw2tsxsE/LUv13rV/PMW4PfKaN6Crl5OY9qnCjr22LmF+LCxitox8awBJ 7j4AkgdKSStsbrC876gBGZCnkXPgJFtxeAchWXSKk2n/vCKCV/4Kg+oIqAsBU8fp/Rtq Nb3A== 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:date:subject:cc:to:from :dkim-signature; bh=dJgV3Uny0vAQYBeBkJItoY2HRKhsGCmVf+cCCim+XIk=; b=cPXwheVKre/2WXnFHuF4fsRaxK84mZ7YVfZA5FPowA3bfzY5pSVmIqDJayVn3h1Yt5 U/QwjdepQXUVbmT8iiaoVaRZzMMkM2yyIIe0VmzXZucwwqlNduEroUckj2BbHVd/BUfd Yds5tS4905fyk6cAdgFMy7KNMffWT2HycCV6M4GKd8eP/rmHz6t7c/5mXL/CxR91U0Ju RWl1f28F3EQktDrX+/6IxSJjvKfPuxWdIx6zgDBWfLMQNxFgwxZz1InD5Lwk1xfhVjt9 my9gPZEli985+AtHz6BgZLGvV2cYrdnqeM3FU8QfeWrrt2X40S89yF/xNkDvkAQrHvzW 7X1Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=en6VYq2d; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id a5-20020a170906368500b008e0bd541c5asi20079244ejc.449.2023.03.24.08.41.58; Fri, 24 Mar 2023 08:42:22 -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=@redhat.com header.s=mimecast20190719 header.b=en6VYq2d; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232435AbjCXPkk (ORCPT + 99 others); Fri, 24 Mar 2023 11:40:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49142 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232187AbjCXPkd (ORCPT ); Fri, 24 Mar 2023 11:40:33 -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 ESMTPS id 6EB1F2007A for ; Fri, 24 Mar 2023 08:39:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1679672382; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=dJgV3Uny0vAQYBeBkJItoY2HRKhsGCmVf+cCCim+XIk=; b=en6VYq2dfWkn4AQkw2R92tyllByGZwHNzsiG68QdJJ+d8ZBPyvnc6+s7kJt27xYxO5cdGi nPCbruM3z7Ji6Aujt3JGPm9MpDyjbVeSqodKpfoawfx4TWpvouHQp7B3zki9IHfK7lCXNF jEBNCv1rVLYhjT0F3Cgb865I6195vf8= Received: from mail-ed1-f70.google.com (mail-ed1-f70.google.com [209.85.208.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-180-iXfXSNIHMCivI2CBQXZktg-1; Fri, 24 Mar 2023 11:39:41 -0400 X-MC-Unique: iXfXSNIHMCivI2CBQXZktg-1 Received: by mail-ed1-f70.google.com with SMTP id j21-20020a508a95000000b004fd82403c91so3760579edj.3 for ; Fri, 24 Mar 2023 08:39:40 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679672380; 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=dJgV3Uny0vAQYBeBkJItoY2HRKhsGCmVf+cCCim+XIk=; b=Q1Iq1ypjwYINKcaKn5OpF3+SuRMHwYdwx8qB5lG9HIuu9b5Mel+S+GJrni1CqM9OAI oimMuGsXMO35FvHoFvKjme0fMvHsmNgPZgmsngSf7vUj2S/3ihvlUjIDBYYzDo0nBD/5 teR+E2TD+2L+4nJ/FQ32hbTFLY1lE1wVTZInPshz+gPFrw7PNLrlsQA1tYJF29l31a/H AN4g3FGNAu8XzL2phFn6nlVO2BmBqCIYfr/FimRghbs6hIRjIN4vkh+yXSMw/CZJa52u Lgz6RPBS7crEYld/1Wc/7BJAqYSpxN0+ZsEyx0+DedZVIXlXPC4UKDgozvoSSirTG7dm dYow== X-Gm-Message-State: AAQBX9fjfKPCV2ZKD0ap6ZkxXnUeFcgHIhZFdCrAKA9z+EQifmRJZsik 5HHtncy16USY4zk18jbEThzwGM23iZ+rF/IYjsAKK/OXjVGMoxN2C5m02VJUl1po2+WG4UHcUIv NmJ8RUSP9E1UtJEcdE7sDEeKO X-Received: by 2002:a17:906:3607:b0:92d:44ca:1137 with SMTP id q7-20020a170906360700b0092d44ca1137mr3426156ejb.43.1679672380057; Fri, 24 Mar 2023 08:39:40 -0700 (PDT) X-Received: by 2002:a17:906:3607:b0:92d:44ca:1137 with SMTP id q7-20020a170906360700b0092d44ca1137mr3426143ejb.43.1679672379807; Fri, 24 Mar 2023 08:39:39 -0700 (PDT) Received: from localhost.localdomain (host-82-53-134-98.retail.telecomitalia.it. [82.53.134.98]) by smtp.gmail.com with ESMTPSA id ot17-20020a170906ccd100b008e51a1fd7bfsm10581350ejb.172.2023.03.24.08.39.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Mar 2023 08:39:38 -0700 (PDT) From: Stefano Garzarella To: virtualization@lists.linux-foundation.org Cc: Andrey Zhadchenko , Jason Wang , eperezma@redhat.com, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, kvm@vger.kernel.org, stefanha@redhat.com, "Michael S. Tsirkin" , Stefano Garzarella Subject: [PATCH v4 6/9] vdpa_sim: make devices agnostic for work management Date: Fri, 24 Mar 2023 16:39:29 +0100 Message-Id: <20230324153929.47670-1-sgarzare@redhat.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230324153607.46836-1-sgarzare@redhat.com> References: <20230324153607.46836-1-sgarzare@redhat.com> MIME-Version: 1.0 Content-type: text/plain X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,PP_MIME_FAKE_ASCII_TEXT, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE autolearn=unavailable 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1761264315142187047?= X-GMAIL-MSGID: =?utf-8?q?1761264315142187047?= Let's move work management inside the vdpa_sim core. This way we can easily change how we manage the works, without having to change the devices each time. Acked-by: Eugenio Pérez Martin Acked-by: Jason Wang Signed-off-by: Stefano Garzarella --- drivers/vdpa/vdpa_sim/vdpa_sim.h | 3 ++- drivers/vdpa/vdpa_sim/vdpa_sim.c | 17 +++++++++++++++-- drivers/vdpa/vdpa_sim/vdpa_sim_blk.c | 6 ++---- drivers/vdpa/vdpa_sim/vdpa_sim_net.c | 6 ++---- 4 files changed, 21 insertions(+), 11 deletions(-) diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.h b/drivers/vdpa/vdpa_sim/vdpa_sim.h index 144858636c10..acee20faaf6a 100644 --- a/drivers/vdpa/vdpa_sim/vdpa_sim.h +++ b/drivers/vdpa/vdpa_sim/vdpa_sim.h @@ -45,7 +45,7 @@ struct vdpasim_dev_attr { u32 ngroups; u32 nas; - work_func_t work_fn; + void (*work_fn)(struct vdpasim *vdpasim); void (*get_config)(struct vdpasim *vdpasim, void *config); void (*set_config)(struct vdpasim *vdpasim, const void *config); int (*get_stats)(struct vdpasim *vdpasim, u16 idx, @@ -78,6 +78,7 @@ struct vdpasim { struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *attr, const struct vdpa_dev_set_config *config); +void vdpasim_schedule_work(struct vdpasim *vdpasim); /* TODO: cross-endian support */ static inline bool vdpasim_is_little_endian(struct vdpasim *vdpasim) diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.c b/drivers/vdpa/vdpa_sim/vdpa_sim.c index eea23c630f7c..2df5227e0b62 100644 --- a/drivers/vdpa/vdpa_sim/vdpa_sim.c +++ b/drivers/vdpa/vdpa_sim/vdpa_sim.c @@ -127,6 +127,13 @@ static void vdpasim_do_reset(struct vdpasim *vdpasim) static const struct vdpa_config_ops vdpasim_config_ops; static const struct vdpa_config_ops vdpasim_batch_config_ops; +static void vdpasim_work_fn(struct work_struct *work) +{ + struct vdpasim *vdpasim = container_of(work, struct vdpasim, work); + + vdpasim->dev_attr.work_fn(vdpasim); +} + struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *dev_attr, const struct vdpa_dev_set_config *config) { @@ -163,7 +170,7 @@ struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *dev_attr, vdpasim = vdpa_to_sim(vdpa); vdpasim->dev_attr = *dev_attr; - INIT_WORK(&vdpasim->work, dev_attr->work_fn); + INIT_WORK(&vdpasim->work, vdpasim_work_fn); spin_lock_init(&vdpasim->lock); spin_lock_init(&vdpasim->iommu_lock); @@ -214,6 +221,12 @@ struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *dev_attr, } EXPORT_SYMBOL_GPL(vdpasim_create); +void vdpasim_schedule_work(struct vdpasim *vdpasim) +{ + schedule_work(&vdpasim->work); +} +EXPORT_SYMBOL_GPL(vdpasim_schedule_work); + static int vdpasim_set_vq_address(struct vdpa_device *vdpa, u16 idx, u64 desc_area, u64 driver_area, u64 device_area) @@ -248,7 +261,7 @@ static void vdpasim_kick_vq(struct vdpa_device *vdpa, u16 idx) } if (vq->ready) - schedule_work(&vdpasim->work); + vdpasim_schedule_work(vdpasim); } static void vdpasim_set_vq_cb(struct vdpa_device *vdpa, u16 idx, diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim_blk.c b/drivers/vdpa/vdpa_sim/vdpa_sim_blk.c index 5117959bed8a..eb4897c8541e 100644 --- a/drivers/vdpa/vdpa_sim/vdpa_sim_blk.c +++ b/drivers/vdpa/vdpa_sim/vdpa_sim_blk.c @@ -11,7 +11,6 @@ #include #include #include -#include #include #include #include @@ -286,9 +285,8 @@ static bool vdpasim_blk_handle_req(struct vdpasim *vdpasim, return handled; } -static void vdpasim_blk_work(struct work_struct *work) +static void vdpasim_blk_work(struct vdpasim *vdpasim) { - struct vdpasim *vdpasim = container_of(work, struct vdpasim, work); bool reschedule = false; int i; @@ -326,7 +324,7 @@ static void vdpasim_blk_work(struct work_struct *work) spin_unlock(&vdpasim->lock); if (reschedule) - schedule_work(&vdpasim->work); + vdpasim_schedule_work(vdpasim); } static void vdpasim_blk_get_config(struct vdpasim *vdpasim, void *config) diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim_net.c b/drivers/vdpa/vdpa_sim/vdpa_sim_net.c index 862f405362de..e61a9ecbfafe 100644 --- a/drivers/vdpa/vdpa_sim/vdpa_sim_net.c +++ b/drivers/vdpa/vdpa_sim/vdpa_sim_net.c @@ -11,7 +11,6 @@ #include #include #include -#include #include #include #include @@ -192,9 +191,8 @@ static void vdpasim_handle_cvq(struct vdpasim *vdpasim) u64_stats_update_end(&net->cq_stats.syncp); } -static void vdpasim_net_work(struct work_struct *work) +static void vdpasim_net_work(struct vdpasim *vdpasim) { - struct vdpasim *vdpasim = container_of(work, struct vdpasim, work); struct vdpasim_virtqueue *txq = &vdpasim->vqs[1]; struct vdpasim_virtqueue *rxq = &vdpasim->vqs[0]; struct vdpasim_net *net = sim_to_net(vdpasim); @@ -260,7 +258,7 @@ static void vdpasim_net_work(struct work_struct *work) vdpasim_net_complete(rxq, write); if (tx_pkts > 4) { - schedule_work(&vdpasim->work); + vdpasim_schedule_work(vdpasim); goto out; } } From patchwork Fri Mar 24 15:39:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefano Garzarella X-Patchwork-Id: 74613 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp728310vqo; Fri, 24 Mar 2023 08:54:54 -0700 (PDT) X-Google-Smtp-Source: AKy350bBr+SVVcDJZr/fnWlyNAf4l0fwxwt21Qt5qc46Z0ScUAMWlIe+c1AP4kd1tv0qbhfiw3Bn X-Received: by 2002:a17:90b:4b43:b0:23b:4bf6:bbed with SMTP id mi3-20020a17090b4b4300b0023b4bf6bbedmr3694009pjb.24.1679673294610; Fri, 24 Mar 2023 08:54:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679673294; cv=none; d=google.com; s=arc-20160816; b=nfUID4qIgzov2hxfracHm0DxMTf6BQo7o9PmIU7FyMmx6ubkv1rakISiR5I8NyVyVv WQqKb6Tchz4JNXZmp6UmoJqGe31jB9C3wknChCBo8HUQR+ogVgp2HXLcGs2P9Qgg5G3h zAXmoqpIknaWh36ynwVWdyC/8Xwu7Yp+by9bs13OH0IyzfPVagpiRUdPCHX4yTYsi/hV X3zZg9K8CKQQVDDBDXVfPd6ccTcIGKTsrXag4SYvOo8LZlHLc/oC9g7vIZBRSt54xntz fEqEGUGDrUbZZ74qHkgZiflipnn+hsIev3dFYnYnHpu7wtvylNfJZw4s6N4DSUUtUPiL T0Tw== 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:date:subject:cc:to:from :dkim-signature; bh=VVAueuORiKGKOw5iPuuD3Ux7+pQ6EAyahLBg2nxd2sw=; b=hLwXWWgc3vrryOBtMZ/CkMvkLmwmGTJXiOHbEl4JgjYnKWJMXvYi9XEu0lSCOgCuSB kgO++bsRhlWJWXqf1v73xTAucTOtAvMPZ9rvr7AP7r+N2sM4oMbXaUTDXkQc5Ra6G3bo jvaRUIxw8qvv6DTN3+nIO06j0Bb7Lg6syYYyyFcTwoGOLqrGMPP5vI5lbYkTsMU4lYVE ypwDGDtQsltCMfLX5M4uOW40O9YYmjI42dlJTBLqoF2E47NJzPAwPRdc3MYKVN3OiCRJ SsZLfA/73EcGcXKUwUkL74vmyuVLrs2NjLjazMGA70GUCgsghJsUx7S71CjzLwcxM0hL GBtQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=YWo9+qSW; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id cs16-20020a17090af51000b0023b3da3ffc2si80173pjb.138.2023.03.24.08.54.39; Fri, 24 Mar 2023 08:54:54 -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=@redhat.com header.s=mimecast20190719 header.b=YWo9+qSW; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231508AbjCXPkw (ORCPT + 99 others); Fri, 24 Mar 2023 11:40:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48894 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232197AbjCXPkl (ORCPT ); Fri, 24 Mar 2023 11:40:41 -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 2AFD62129A for ; Fri, 24 Mar 2023 08:39:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1679672392; 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=VVAueuORiKGKOw5iPuuD3Ux7+pQ6EAyahLBg2nxd2sw=; b=YWo9+qSWC35TJKScyq4XNA3nwz4tA3j0eBwXR9yRaBIs9mIV8QKoGfB6dmACMeqZUsbd80 itYYNY34xA9HTguN4jnMDyc4+sznBX2HGSAAEv+EFW32DlVwNts+SOBHNynMdmacjRhSlr onApVaCIYGStztqhDwXBCWGp6dj4urw= Received: from mail-ed1-f72.google.com (mail-ed1-f72.google.com [209.85.208.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-83-V9q7yx27NRm6NqALrbIlxQ-1; Fri, 24 Mar 2023 11:39:51 -0400 X-MC-Unique: V9q7yx27NRm6NqALrbIlxQ-1 Received: by mail-ed1-f72.google.com with SMTP id es16-20020a056402381000b004fa3e04c882so3747410edb.10 for ; Fri, 24 Mar 2023 08:39:50 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679672390; 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=VVAueuORiKGKOw5iPuuD3Ux7+pQ6EAyahLBg2nxd2sw=; b=chOujcUm/QGzDHyadtCBglYLWuEo4Zx2tT70nwuSpk2yjOYjHfFDQBiDpGUBN+y1dT 7Ru7HAJogcKS0o1dLb1W2YhhiO8r6Zm5R9J1kbhgP0HXlNnM61Cu4b6tVNhLyXSrUSv0 44Yx1lvnzUY5bPNlpzk6IoL9JspLTt5yL1/1A2EE4V6vTNb975f1LiOliDW/PACAtnTD q/UUwA7vVz0UmTmG5UxWZ26Z6QpurnSX7nr3RaSZQoAxmDOyQBrXLf4OBAPJvjTrI2DH 1aPCU6lURROUn3lWEWO2LRekb7Q4yGFS2NA8UbMjgXEqefF+eh+kNCk9IIEJhHgiZchD 8m1Q== X-Gm-Message-State: AAQBX9eaSjWSPw3Qj2cIpOo4zF/M/cERndd0FciThBpRRZN/H9MKfk3c 63LtAYNbJ2rooJ9OQ8jWmtBxw6SRvcgF+Ei9Dzhi0JgfkstD0LULNDicWsP3suAWowUJjxz6Kch fCwq0X8+KY9KE33SDt8y5abPh X-Received: by 2002:a17:906:3850:b0:92f:13b9:d498 with SMTP id w16-20020a170906385000b0092f13b9d498mr3253853ejc.36.1679672389913; Fri, 24 Mar 2023 08:39:49 -0700 (PDT) X-Received: by 2002:a17:906:3850:b0:92f:13b9:d498 with SMTP id w16-20020a170906385000b0092f13b9d498mr3253829ejc.36.1679672389708; Fri, 24 Mar 2023 08:39:49 -0700 (PDT) Received: from localhost.localdomain (host-82-53-134-98.retail.telecomitalia.it. [82.53.134.98]) by smtp.gmail.com with ESMTPSA id sd24-20020a170906ce3800b00931024e96c5sm10571246ejb.99.2023.03.24.08.39.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Mar 2023 08:39:48 -0700 (PDT) From: Stefano Garzarella To: virtualization@lists.linux-foundation.org Cc: Andrey Zhadchenko , Jason Wang , eperezma@redhat.com, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, kvm@vger.kernel.org, stefanha@redhat.com, "Michael S. Tsirkin" , Stefano Garzarella Subject: [PATCH v4 7/9] vdpa_sim: use kthread worker Date: Fri, 24 Mar 2023 16:39:40 +0100 Message-Id: <20230324153940.47710-1-sgarzare@redhat.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230324153607.46836-1-sgarzare@redhat.com> References: <20230324153607.46836-1-sgarzare@redhat.com> MIME-Version: 1.0 Content-type: text/plain X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE autolearn=unavailable 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1761265104677341085?= X-GMAIL-MSGID: =?utf-8?q?1761265104677341085?= Let's use our own kthread to run device jobs. This allows us more flexibility, especially we can attach the kthread to the user address space when vDPA uses user's VA. Acked-by: Jason Wang Signed-off-by: Stefano Garzarella --- Notes: v3: - fix `dev` not initialized in the error path [Simon Horman] drivers/vdpa/vdpa_sim/vdpa_sim.h | 3 ++- drivers/vdpa/vdpa_sim/vdpa_sim.c | 19 +++++++++++++------ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.h b/drivers/vdpa/vdpa_sim/vdpa_sim.h index acee20faaf6a..ce83f9130a5d 100644 --- a/drivers/vdpa/vdpa_sim/vdpa_sim.h +++ b/drivers/vdpa/vdpa_sim/vdpa_sim.h @@ -57,7 +57,8 @@ struct vdpasim_dev_attr { struct vdpasim { struct vdpa_device vdpa; struct vdpasim_virtqueue *vqs; - struct work_struct work; + struct kthread_worker *worker; + struct kthread_work work; struct vdpasim_dev_attr dev_attr; /* spinlock to synchronize virtqueue state */ spinlock_t lock; diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.c b/drivers/vdpa/vdpa_sim/vdpa_sim.c index 2df5227e0b62..bd9f9054de94 100644 --- a/drivers/vdpa/vdpa_sim/vdpa_sim.c +++ b/drivers/vdpa/vdpa_sim/vdpa_sim.c @@ -11,8 +11,8 @@ #include #include #include +#include #include -#include #include #include #include @@ -127,7 +127,7 @@ static void vdpasim_do_reset(struct vdpasim *vdpasim) static const struct vdpa_config_ops vdpasim_config_ops; static const struct vdpa_config_ops vdpasim_batch_config_ops; -static void vdpasim_work_fn(struct work_struct *work) +static void vdpasim_work_fn(struct kthread_work *work) { struct vdpasim *vdpasim = container_of(work, struct vdpasim, work); @@ -170,11 +170,17 @@ struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *dev_attr, vdpasim = vdpa_to_sim(vdpa); vdpasim->dev_attr = *dev_attr; - INIT_WORK(&vdpasim->work, vdpasim_work_fn); + dev = &vdpasim->vdpa.dev; + + kthread_init_work(&vdpasim->work, vdpasim_work_fn); + vdpasim->worker = kthread_create_worker(0, "vDPA sim worker: %s", + dev_attr->name); + if (IS_ERR(vdpasim->worker)) + goto err_iommu; + spin_lock_init(&vdpasim->lock); spin_lock_init(&vdpasim->iommu_lock); - dev = &vdpasim->vdpa.dev; dev->dma_mask = &dev->coherent_dma_mask; if (dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64))) goto err_iommu; @@ -223,7 +229,7 @@ EXPORT_SYMBOL_GPL(vdpasim_create); void vdpasim_schedule_work(struct vdpasim *vdpasim) { - schedule_work(&vdpasim->work); + kthread_queue_work(vdpasim->worker, &vdpasim->work); } EXPORT_SYMBOL_GPL(vdpasim_schedule_work); @@ -623,7 +629,8 @@ static void vdpasim_free(struct vdpa_device *vdpa) struct vdpasim *vdpasim = vdpa_to_sim(vdpa); int i; - cancel_work_sync(&vdpasim->work); + kthread_cancel_work_sync(&vdpasim->work); + kthread_destroy_worker(vdpasim->worker); for (i = 0; i < vdpasim->dev_attr.nvqs; i++) { vringh_kiov_cleanup(&vdpasim->vqs[i].out_iov); From patchwork Fri Mar 24 15:39:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefano Garzarella X-Patchwork-Id: 74607 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp721305vqo; Fri, 24 Mar 2023 08:43:00 -0700 (PDT) X-Google-Smtp-Source: AKy350Zt+CNCtOftRP3PkdXSNZzUEFF9ylB7j5Ek7FFgk2AoCamV7tedyG+40idTXhDY+FrTa4z4 X-Received: by 2002:aa7:cd71:0:b0:4fa:c7c9:62ea with SMTP id ca17-20020aa7cd71000000b004fac7c962eamr3629227edb.11.1679672580029; Fri, 24 Mar 2023 08:43:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679672580; cv=none; d=google.com; s=arc-20160816; b=pYCf+0n/FCD5uIPLIEFP7paRRv8EJBtBd6w+7vxGth1ZsfGL3Bd5IDhZnt7kfDQbBp OBNPpY7Zo5TBTZrFT7/gQ/EzYDI7I6BxsNrDaexMzcnBMJ08F9Gs7Pi9HbjVVUnxRpM/ bmGga32YgzJ0iGfsIzWDXD4BnQU9abmG/kSJxuOTx69rI15rKe+4NOz04EN3AhHFFa0u o1wK4q1EPd6iQ3pywABdHHyg2w6EtfUWUF8N+lNStckIE5dQDkxoC/9cMmj4+Y++sfwN bpCMHJHMdNqCBAqCZ7EBZkoNTTH+OzIg9SQep+wKdLda+h/1pfUePWJHOUQGHFh+x7Xu Wlmw== 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:date:subject:cc:to:from :dkim-signature; bh=tQ8fDN2h9p4iTg0iFPY/+DuAnJcVPz4edknSv7QjGbU=; b=c4R37WqxcmkHuSVDTi7s0L0hOuroa9nJx/l1sRZvKIFFigHUIN9+skyBdCX4wTAiRw YR6yt/Scm43iXkd6X37FCEugB1FOitDP/ElWC+SwdW+h7e8PjyV1lqeOghQ4zLZK7oDs 5P8KJQ/Ko/gZ2enkQpQo+rNWThAqRk2bd95NNs8QYiVYLtyFTpmcE1C7OkP72QCjBPTy YA7zWoeN+cBnh8MfNxqoTVcfAnH1gD5PpSfwy2UZ/HVb+wh3XQPca4V+4lEV7ginE7Dv msY73C/i5hezqyzujODw7LM32SRwI8HkiALns0KqgTkyHlKOn25kDBNfFDh2pGojmHV8 tgkg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="c/3C3c9o"; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id i18-20020aa7c712000000b004accb68cc93si20672142edq.387.2023.03.24.08.42.36; Fri, 24 Mar 2023 08:43:00 -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=@redhat.com header.s=mimecast20190719 header.b="c/3C3c9o"; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231968AbjCXPlG (ORCPT + 99 others); Fri, 24 Mar 2023 11:41:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49666 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232514AbjCXPky (ORCPT ); Fri, 24 Mar 2023 11:40:54 -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 ESMTPS id 5A15820A2B for ; Fri, 24 Mar 2023 08:40:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1679672402; 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=tQ8fDN2h9p4iTg0iFPY/+DuAnJcVPz4edknSv7QjGbU=; b=c/3C3c9oNcvkRX7lVi0LUG6CyYMvs7xMFKL/W4J1YTHtEoTVE9Yjn8zxWwJhdGTVq0RjsL 7StRkFDRN61vpLBhG8cQM33qJDkhdnsw0FxaEg8T9Yp14e1U7HPrQaZmuwFEJBr84Skzuk sRD7qT0ydgZUFnQVdNWa5nFBn9c93jo= Received: from mail-ed1-f70.google.com (mail-ed1-f70.google.com [209.85.208.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-62-SMV2-AMUNp2uZSpnqgTjtA-1; Fri, 24 Mar 2023 11:40:01 -0400 X-MC-Unique: SMV2-AMUNp2uZSpnqgTjtA-1 Received: by mail-ed1-f70.google.com with SMTP id c1-20020a0564021f8100b004acbe232c03so3703582edc.9 for ; Fri, 24 Mar 2023 08:40:01 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679672400; 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=tQ8fDN2h9p4iTg0iFPY/+DuAnJcVPz4edknSv7QjGbU=; b=tjJn/8WGtuH+J4uTIxYJkge2nPY7gX1cZ+2Zb34G/PqL1g54ctCBDuDtp9a1mkMxr9 It/EecHNNr3om7U2lM3GfNysBDbEfNU9cjSG3U7H+3pzZx5rHv+gr92s60caywsUzQSk KFU8LyAH2Wu+UkAnp69yL2fBpv/NHRwgo0x58gVUCEjmhLXISCEuUPgWrhQojb7LHghj H10iF72tenJfnlokX/p99cu9vJILmBrJCPyo5YHfvBvMhLOLQGL+pskrH3ZH1+5i85cO lJordJmJFkAA4lduyWmBhBMFlnipJrwTmI/acDBi3WtXRyQpqHBsjywNZGZn2UKZisll enPg== X-Gm-Message-State: AAQBX9fY1oHW3y2L23AqyFT/C5XqvS2Bioh6Ja26QkEchimbIMiQXTIm U6cOdjVEe+xcCofycExUIobG9cK49zHZlFiBgX0GH16qt69BQ+t5DnI0SiB0hxQ8Yf6ENE9DWO7 2i3KE19eS1A0+jI7hTDLnYrUN X-Received: by 2002:a17:906:f143:b0:933:4c93:69ee with SMTP id gw3-20020a170906f14300b009334c9369eemr3200127ejb.45.1679672400242; Fri, 24 Mar 2023 08:40:00 -0700 (PDT) X-Received: by 2002:a17:906:f143:b0:933:4c93:69ee with SMTP id gw3-20020a170906f14300b009334c9369eemr3200117ejb.45.1679672400073; Fri, 24 Mar 2023 08:40:00 -0700 (PDT) Received: from localhost.localdomain (host-82-53-134-98.retail.telecomitalia.it. [82.53.134.98]) by smtp.gmail.com with ESMTPSA id wy8-20020a170906fe0800b0093e261cc8bcsm1113313ejb.58.2023.03.24.08.39.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Mar 2023 08:39:59 -0700 (PDT) From: Stefano Garzarella To: virtualization@lists.linux-foundation.org Cc: Andrey Zhadchenko , Jason Wang , eperezma@redhat.com, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, kvm@vger.kernel.org, stefanha@redhat.com, "Michael S. Tsirkin" , Stefano Garzarella Subject: [PATCH v4 8/9] vdpa_sim: replace the spinlock with a mutex to protect the state Date: Fri, 24 Mar 2023 16:39:49 +0100 Message-Id: <20230324153949.47778-1-sgarzare@redhat.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230324153607.46836-1-sgarzare@redhat.com> References: <20230324153607.46836-1-sgarzare@redhat.com> MIME-Version: 1.0 Content-type: text/plain X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE autolearn=unavailable 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1761264355282054590?= X-GMAIL-MSGID: =?utf-8?q?1761264355282054590?= The spinlock we use to protect the state of the simulator is sometimes held for a long time (for example, when devices handle requests). This also prevents us from calling functions that might sleep (such as kthread_flush_work() in the next patch), and thus having to release and retake the lock. For these reasons, let's replace the spinlock with a mutex that gives us more flexibility. Suggested-by: Jason Wang Acked-by: Jason Wang Signed-off-by: Stefano Garzarella --- drivers/vdpa/vdpa_sim/vdpa_sim.h | 4 ++-- drivers/vdpa/vdpa_sim/vdpa_sim.c | 34 ++++++++++++++-------------- drivers/vdpa/vdpa_sim/vdpa_sim_blk.c | 4 ++-- drivers/vdpa/vdpa_sim/vdpa_sim_net.c | 4 ++-- 4 files changed, 23 insertions(+), 23 deletions(-) diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.h b/drivers/vdpa/vdpa_sim/vdpa_sim.h index ce83f9130a5d..4774292fba8c 100644 --- a/drivers/vdpa/vdpa_sim/vdpa_sim.h +++ b/drivers/vdpa/vdpa_sim/vdpa_sim.h @@ -60,8 +60,8 @@ struct vdpasim { struct kthread_worker *worker; struct kthread_work work; struct vdpasim_dev_attr dev_attr; - /* spinlock to synchronize virtqueue state */ - spinlock_t lock; + /* mutex to synchronize virtqueue state */ + struct mutex mutex; /* virtio config according to device type */ void *config; struct vhost_iotlb *iommu; diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.c b/drivers/vdpa/vdpa_sim/vdpa_sim.c index bd9f9054de94..2b2e439a66f7 100644 --- a/drivers/vdpa/vdpa_sim/vdpa_sim.c +++ b/drivers/vdpa/vdpa_sim/vdpa_sim.c @@ -178,7 +178,7 @@ struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *dev_attr, if (IS_ERR(vdpasim->worker)) goto err_iommu; - spin_lock_init(&vdpasim->lock); + mutex_init(&vdpasim->mutex); spin_lock_init(&vdpasim->iommu_lock); dev->dma_mask = &dev->coherent_dma_mask; @@ -286,13 +286,13 @@ static void vdpasim_set_vq_ready(struct vdpa_device *vdpa, u16 idx, bool ready) struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx]; bool old_ready; - spin_lock(&vdpasim->lock); + mutex_lock(&vdpasim->mutex); old_ready = vq->ready; vq->ready = ready; if (vq->ready && !old_ready) { vdpasim_queue_ready(vdpasim, idx); } - spin_unlock(&vdpasim->lock); + mutex_unlock(&vdpasim->mutex); } static bool vdpasim_get_vq_ready(struct vdpa_device *vdpa, u16 idx) @@ -310,9 +310,9 @@ static int vdpasim_set_vq_state(struct vdpa_device *vdpa, u16 idx, struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx]; struct vringh *vrh = &vq->vring; - spin_lock(&vdpasim->lock); + mutex_lock(&vdpasim->mutex); vrh->last_avail_idx = state->split.avail_index; - spin_unlock(&vdpasim->lock); + mutex_unlock(&vdpasim->mutex); return 0; } @@ -409,9 +409,9 @@ static u8 vdpasim_get_status(struct vdpa_device *vdpa) struct vdpasim *vdpasim = vdpa_to_sim(vdpa); u8 status; - spin_lock(&vdpasim->lock); + mutex_lock(&vdpasim->mutex); status = vdpasim->status; - spin_unlock(&vdpasim->lock); + mutex_unlock(&vdpasim->mutex); return status; } @@ -420,19 +420,19 @@ static void vdpasim_set_status(struct vdpa_device *vdpa, u8 status) { struct vdpasim *vdpasim = vdpa_to_sim(vdpa); - spin_lock(&vdpasim->lock); + mutex_lock(&vdpasim->mutex); vdpasim->status = status; - spin_unlock(&vdpasim->lock); + mutex_unlock(&vdpasim->mutex); } static int vdpasim_reset(struct vdpa_device *vdpa) { struct vdpasim *vdpasim = vdpa_to_sim(vdpa); - spin_lock(&vdpasim->lock); + mutex_lock(&vdpasim->mutex); vdpasim->status = 0; vdpasim_do_reset(vdpasim); - spin_unlock(&vdpasim->lock); + mutex_unlock(&vdpasim->mutex); return 0; } @@ -441,9 +441,9 @@ static int vdpasim_suspend(struct vdpa_device *vdpa) { struct vdpasim *vdpasim = vdpa_to_sim(vdpa); - spin_lock(&vdpasim->lock); + mutex_lock(&vdpasim->mutex); vdpasim->running = false; - spin_unlock(&vdpasim->lock); + mutex_unlock(&vdpasim->mutex); return 0; } @@ -453,7 +453,7 @@ static int vdpasim_resume(struct vdpa_device *vdpa) struct vdpasim *vdpasim = vdpa_to_sim(vdpa); int i; - spin_lock(&vdpasim->lock); + mutex_lock(&vdpasim->mutex); vdpasim->running = true; if (vdpasim->pending_kick) { @@ -464,7 +464,7 @@ static int vdpasim_resume(struct vdpa_device *vdpa) vdpasim->pending_kick = false; } - spin_unlock(&vdpasim->lock); + mutex_unlock(&vdpasim->mutex); return 0; } @@ -536,14 +536,14 @@ static int vdpasim_set_group_asid(struct vdpa_device *vdpa, unsigned int group, iommu = &vdpasim->iommu[asid]; - spin_lock(&vdpasim->lock); + mutex_lock(&vdpasim->mutex); for (i = 0; i < vdpasim->dev_attr.nvqs; i++) if (vdpasim_get_vq_group(vdpa, i) == group) vringh_set_iotlb(&vdpasim->vqs[i].vring, iommu, &vdpasim->iommu_lock); - spin_unlock(&vdpasim->lock); + mutex_unlock(&vdpasim->mutex); return 0; } diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim_blk.c b/drivers/vdpa/vdpa_sim/vdpa_sim_blk.c index eb4897c8541e..568119e1553f 100644 --- a/drivers/vdpa/vdpa_sim/vdpa_sim_blk.c +++ b/drivers/vdpa/vdpa_sim/vdpa_sim_blk.c @@ -290,7 +290,7 @@ static void vdpasim_blk_work(struct vdpasim *vdpasim) bool reschedule = false; int i; - spin_lock(&vdpasim->lock); + mutex_lock(&vdpasim->mutex); if (!(vdpasim->status & VIRTIO_CONFIG_S_DRIVER_OK)) goto out; @@ -321,7 +321,7 @@ static void vdpasim_blk_work(struct vdpasim *vdpasim) } } out: - spin_unlock(&vdpasim->lock); + mutex_unlock(&vdpasim->mutex); if (reschedule) vdpasim_schedule_work(vdpasim); diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim_net.c b/drivers/vdpa/vdpa_sim/vdpa_sim_net.c index e61a9ecbfafe..7ab434592bfe 100644 --- a/drivers/vdpa/vdpa_sim/vdpa_sim_net.c +++ b/drivers/vdpa/vdpa_sim/vdpa_sim_net.c @@ -201,7 +201,7 @@ static void vdpasim_net_work(struct vdpasim *vdpasim) u64 rx_drops = 0, rx_overruns = 0, rx_errors = 0, tx_errors = 0; int err; - spin_lock(&vdpasim->lock); + mutex_lock(&vdpasim->mutex); if (!vdpasim->running) goto out; @@ -264,7 +264,7 @@ static void vdpasim_net_work(struct vdpasim *vdpasim) } out: - spin_unlock(&vdpasim->lock); + mutex_unlock(&vdpasim->mutex); u64_stats_update_begin(&net->tx_stats.syncp); net->tx_stats.pkts += tx_pkts; From patchwork Fri Mar 24 15:40:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefano Garzarella X-Patchwork-Id: 74608 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp721371vqo; Fri, 24 Mar 2023 08:43:08 -0700 (PDT) X-Google-Smtp-Source: AKy350Z8TWrcluqIRjJpPVxDt3WORPbqZMb/or9eb9AhS9joL87R8YrJ6okmowfCRh4Mr4ymM/uZ X-Received: by 2002:a17:906:3118:b0:930:d319:d947 with SMTP id 24-20020a170906311800b00930d319d947mr3353949ejx.74.1679672588030; Fri, 24 Mar 2023 08:43:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679672588; cv=none; d=google.com; s=arc-20160816; b=iOAN3XuzPkgRaAMi3AfYFWB0VlXQ1S3FgHNy2Is/+we5E04TzMQsqjgertG0VPXjod U+6Hbjo8iMTynANG76v7mqSTyBNxKOwIU6p4EKn5sQi+sxYrLg9PIDYpmTVXICA4j8vG DRb/2eBRJIxSMd3UxZjeYYmd/J+HCUDGdozuHFkZ0eS05N7gYX7miscAqvJR4G5C0fJd l7yJTwbjslC3LrqLX3meaHz6DSBbuxLfJ1jpuYWdOWdUwZLnI3rAfoQZjpn20gE8uhYM jNpIrCsVQ/yVxw7CQccbnCgQoVSqF5vN2UCqU0JvH1DF/mSZmAycG1tIDjn6bUSOA1oT 44aw== 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:date:subject:cc:to:from :dkim-signature; bh=gKYp+kOgUX/GJB2EDtARo8NYPrwRtHr03kQCkOwXG8g=; b=abMgY0FW/YxM9Dh/AcsScLSPxiP88nEqzpQTuY5FZr8vJuTrLuUixSJjmk+EggdjDc CJdxix+2j+y/cLQow56W2SYKR4lwgYAsS6tID1Ng1ifgYC35xW7FEEMg3H19vs46oTBC m2g5idhNhALHaQ0kIbC2cJLdMT0R7VVoe8fcd9EB5JW/VFcKDJIOVP/uaQNl5Dn/NMVY HdqseorqPWeh8byWDr+MnkIsSO7LoxSHrcWKs+oe4U5yMNXRicmsOMal9VDmgFTC3YD6 7Ivp0D6jip1ilk2GaesFPCxu2B3qfVZSDlho/hV3REVqsz3jlQf1ew9AfhIKHEW8sWjl F8xQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=a7d1l5vy; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id o19-20020aa7d3d3000000b005021d88ac44si2294373edr.381.2023.03.24.08.42.44; Fri, 24 Mar 2023 08:43:08 -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=@redhat.com header.s=mimecast20190719 header.b=a7d1l5vy; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232515AbjCXPlX (ORCPT + 99 others); Fri, 24 Mar 2023 11:41:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50470 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232249AbjCXPlI (ORCPT ); Fri, 24 Mar 2023 11:41:08 -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 ESMTPS id 106C892 for ; Fri, 24 Mar 2023 08:40:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1679672418; 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=gKYp+kOgUX/GJB2EDtARo8NYPrwRtHr03kQCkOwXG8g=; b=a7d1l5vyPQHUlU54EjEtuOhZkJBajSdLQMxeHJDp+RcekV3qUXYFi4bO3lFi4wFPJ6QGPY jmyQ5CKb+/1ppH9EEXEl2WbAOS0tCFIu8BTB428ob423dgMx8WV8wrFw9a2PB1AzUlrtu/ FOVef7uB7/Dx4ljptL5SHTPbH8F7lws= Received: from mail-ed1-f71.google.com (mail-ed1-f71.google.com [209.85.208.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-626-rYmB9r_DMLimDQocaKtEBA-1; Fri, 24 Mar 2023 11:40:12 -0400 X-MC-Unique: rYmB9r_DMLimDQocaKtEBA-1 Received: by mail-ed1-f71.google.com with SMTP id j21-20020a508a95000000b004fd82403c91so3762614edj.3 for ; Fri, 24 Mar 2023 08:40:11 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679672411; 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=gKYp+kOgUX/GJB2EDtARo8NYPrwRtHr03kQCkOwXG8g=; b=xY4dvlJVHU/Ik8rCXUXRjpWJp+MTkbpzNzvpyNhAyyqdp+WJlMZixHp3AcceGwB7iG OFsG1q6tieq3bnWSmcI5QhazFMLhqJ6awQWTuxWOFUagTvzc6rHbno3Y9CStEpjmdDh4 kkgDrsldKhuZ8lx9AoKxknabws4OEjBneuwKDBzGIUcY8D/ySbENT2LCl8QLJHdOgXK8 cKqhtWO2uVRF53vTRU9smlhkYtw/hf8YsctHgxTaL0hdxeYssaJ194FU+8yDlbE61Tn4 u6EpEqBh8CwovtpcOEpIegaHtOBAQ5dybpIbYxOmBYIzlQLrDPGEiZ72Wg1hTwJM5+Td niag== X-Gm-Message-State: AAQBX9fYF4m4C32H16t33EJlxBuYnGXav9QaDYv+qqf8oGDFguHfD5Wq LL3WZMGlr4hEgiQ0VY56D+gxj8byiIpSe7xOhx7r1WrVvWwbjFAvACzH0QGgjTooFLrZSPbOekh DF2S2k5PIe5X9ypx+F0fpYcq3 X-Received: by 2002:a17:907:161f:b0:932:7f5c:4bb2 with SMTP id hb31-20020a170907161f00b009327f5c4bb2mr3662670ejc.75.1679672410842; Fri, 24 Mar 2023 08:40:10 -0700 (PDT) X-Received: by 2002:a17:907:161f:b0:932:7f5c:4bb2 with SMTP id hb31-20020a170907161f00b009327f5c4bb2mr3662649ejc.75.1679672410627; Fri, 24 Mar 2023 08:40:10 -0700 (PDT) Received: from localhost.localdomain (host-82-53-134-98.retail.telecomitalia.it. [82.53.134.98]) by smtp.gmail.com with ESMTPSA id rk28-20020a170907215c00b00933b38505f9sm7857073ejb.152.2023.03.24.08.40.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Mar 2023 08:40:09 -0700 (PDT) From: Stefano Garzarella To: virtualization@lists.linux-foundation.org Cc: Andrey Zhadchenko , Jason Wang , eperezma@redhat.com, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, kvm@vger.kernel.org, stefanha@redhat.com, "Michael S. Tsirkin" , Stefano Garzarella Subject: [PATCH v4 9/9] vdpa_sim: add support for user VA Date: Fri, 24 Mar 2023 16:40:00 +0100 Message-Id: <20230324154000.47809-1-sgarzare@redhat.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230324153607.46836-1-sgarzare@redhat.com> References: <20230324153607.46836-1-sgarzare@redhat.com> MIME-Version: 1.0 Content-type: text/plain X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE autolearn=unavailable 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1761264363587882331?= X-GMAIL-MSGID: =?utf-8?q?1761264363587882331?= The new "use_va" module parameter (default: true) is used in vdpa_alloc_device() to inform the vDPA framework that the device supports VA. vringh is initialized to use VA only when "use_va" is true and the user's mm has been bound. So, only when the bus supports user VA (e.g. vhost-vdpa). vdpasim_mm_work_fn work is used to serialize the binding to a new address space when the .bind_mm callback is invoked, and unbinding when the .unbind_mm callback is invoked. Call mmget_not_zero()/kthread_use_mm() inside the worker function to pin the address space only as long as needed, following the documentation of mmget() in include/linux/sched/mm.h: * Never use this function to pin this address space for an * unbounded/indefinite amount of time. Acked-by: Jason Wang Signed-off-by: Stefano Garzarella --- Notes: v4: - checked `use_va` in vdpasim_work_fn() [Jason] - removed `va_enabled` variable now used only in the if condition v3: - called mmget_not_zero() before kthread_use_mm() [Jason] As the documentation of mmget() in include/linux/sched/mm.h says: * Never use this function to pin this address space for an * unbounded/indefinite amount of time. I moved mmget_not_zero/kthread_use_mm inside the worker function, this way we pin the address space only as long as needed. This is similar to what vfio_iommu_type1_dma_rw_chunk() does in drivers/vfio/vfio_iommu_type1.c - simplified the mm bind/unbind [Jason] - renamed vdpasim_worker_change_mm_sync() [Jason] - fix commit message (s/default: false/default: true) v2: - `use_va` set to true by default [Eugenio] - supported the new unbind_mm callback [Jason] - removed the unbind_mm call in vdpasim_do_reset() [Jason] - avoided to release the lock while call kthread_flush_work() since we are now using a mutex to protect the device state drivers/vdpa/vdpa_sim/vdpa_sim.h | 1 + drivers/vdpa/vdpa_sim/vdpa_sim.c | 97 +++++++++++++++++++++++++++++--- 2 files changed, 90 insertions(+), 8 deletions(-) diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.h b/drivers/vdpa/vdpa_sim/vdpa_sim.h index 4774292fba8c..3a42887d05d9 100644 --- a/drivers/vdpa/vdpa_sim/vdpa_sim.h +++ b/drivers/vdpa/vdpa_sim/vdpa_sim.h @@ -59,6 +59,7 @@ struct vdpasim { struct vdpasim_virtqueue *vqs; struct kthread_worker *worker; struct kthread_work work; + struct mm_struct *mm_bound; struct vdpasim_dev_attr dev_attr; /* mutex to synchronize virtqueue state */ struct mutex mutex; diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.c b/drivers/vdpa/vdpa_sim/vdpa_sim.c index 2b2e439a66f7..2c706bb18897 100644 --- a/drivers/vdpa/vdpa_sim/vdpa_sim.c +++ b/drivers/vdpa/vdpa_sim/vdpa_sim.c @@ -35,10 +35,44 @@ module_param(max_iotlb_entries, int, 0444); MODULE_PARM_DESC(max_iotlb_entries, "Maximum number of iotlb entries for each address space. 0 means unlimited. (default: 2048)"); +static bool use_va = true; +module_param(use_va, bool, 0444); +MODULE_PARM_DESC(use_va, "Enable/disable the device's ability to use VA"); + #define VDPASIM_QUEUE_ALIGN PAGE_SIZE #define VDPASIM_QUEUE_MAX 256 #define VDPASIM_VENDOR_ID 0 +struct vdpasim_mm_work { + struct kthread_work work; + struct vdpasim *vdpasim; + struct mm_struct *mm_to_bind; + int ret; +}; + +static void vdpasim_mm_work_fn(struct kthread_work *work) +{ + struct vdpasim_mm_work *mm_work = + container_of(work, struct vdpasim_mm_work, work); + struct vdpasim *vdpasim = mm_work->vdpasim; + + mm_work->ret = 0; + + //TODO: should we attach the cgroup of the mm owner? + vdpasim->mm_bound = mm_work->mm_to_bind; +} + +static void vdpasim_worker_change_mm_sync(struct vdpasim *vdpasim, + struct vdpasim_mm_work *mm_work) +{ + struct kthread_work *work = &mm_work->work; + + kthread_init_work(work, vdpasim_mm_work_fn); + kthread_queue_work(vdpasim->worker, work); + + kthread_flush_work(work); +} + static struct vdpasim *vdpa_to_sim(struct vdpa_device *vdpa) { return container_of(vdpa, struct vdpasim, vdpa); @@ -59,13 +93,20 @@ static void vdpasim_queue_ready(struct vdpasim *vdpasim, unsigned int idx) { struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx]; uint16_t last_avail_idx = vq->vring.last_avail_idx; - - vringh_init_iotlb(&vq->vring, vdpasim->features, vq->num, true, - (struct vring_desc *)(uintptr_t)vq->desc_addr, - (struct vring_avail *) - (uintptr_t)vq->driver_addr, - (struct vring_used *) - (uintptr_t)vq->device_addr); + struct vring_desc *desc = (struct vring_desc *) + (uintptr_t)vq->desc_addr; + struct vring_avail *avail = (struct vring_avail *) + (uintptr_t)vq->driver_addr; + struct vring_used *used = (struct vring_used *) + (uintptr_t)vq->device_addr; + + if (use_va && vdpasim->mm_bound) { + vringh_init_iotlb_va(&vq->vring, vdpasim->features, vq->num, + true, desc, avail, used); + } else { + vringh_init_iotlb(&vq->vring, vdpasim->features, vq->num, + true, desc, avail, used); + } vq->vring.last_avail_idx = last_avail_idx; @@ -130,8 +171,20 @@ static const struct vdpa_config_ops vdpasim_batch_config_ops; static void vdpasim_work_fn(struct kthread_work *work) { struct vdpasim *vdpasim = container_of(work, struct vdpasim, work); + struct mm_struct *mm = vdpasim->mm_bound; + + if (use_va && mm) { + if (!mmget_not_zero(mm)) + return; + kthread_use_mm(mm); + } vdpasim->dev_attr.work_fn(vdpasim); + + if (use_va && mm) { + kthread_unuse_mm(mm); + mmput(mm); + } } struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *dev_attr, @@ -162,7 +215,7 @@ struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *dev_attr, vdpa = __vdpa_alloc_device(NULL, ops, dev_attr->ngroups, dev_attr->nas, dev_attr->alloc_size, - dev_attr->name, false); + dev_attr->name, use_va); if (IS_ERR(vdpa)) { ret = PTR_ERR(vdpa); goto err_alloc; @@ -582,6 +635,30 @@ static int vdpasim_set_map(struct vdpa_device *vdpa, unsigned int asid, return ret; } +static int vdpasim_bind_mm(struct vdpa_device *vdpa, struct mm_struct *mm) +{ + struct vdpasim *vdpasim = vdpa_to_sim(vdpa); + struct vdpasim_mm_work mm_work; + + mm_work.vdpasim = vdpasim; + mm_work.mm_to_bind = mm; + + vdpasim_worker_change_mm_sync(vdpasim, &mm_work); + + return mm_work.ret; +} + +static void vdpasim_unbind_mm(struct vdpa_device *vdpa) +{ + struct vdpasim *vdpasim = vdpa_to_sim(vdpa); + struct vdpasim_mm_work mm_work; + + mm_work.vdpasim = vdpasim; + mm_work.mm_to_bind = NULL; + + vdpasim_worker_change_mm_sync(vdpasim, &mm_work); +} + static int vdpasim_dma_map(struct vdpa_device *vdpa, unsigned int asid, u64 iova, u64 size, u64 pa, u32 perm, void *opaque) @@ -678,6 +755,8 @@ static const struct vdpa_config_ops vdpasim_config_ops = { .set_group_asid = vdpasim_set_group_asid, .dma_map = vdpasim_dma_map, .dma_unmap = vdpasim_dma_unmap, + .bind_mm = vdpasim_bind_mm, + .unbind_mm = vdpasim_unbind_mm, .free = vdpasim_free, }; @@ -712,6 +791,8 @@ static const struct vdpa_config_ops vdpasim_batch_config_ops = { .get_iova_range = vdpasim_get_iova_range, .set_group_asid = vdpasim_set_group_asid, .set_map = vdpasim_set_map, + .bind_mm = vdpasim_bind_mm, + .unbind_mm = vdpasim_unbind_mm, .free = vdpasim_free, };