Received: by 2002:ac0:da4c:0:0:0:0:0 with SMTP id a12csp2537732imi; Mon, 25 Jul 2022 00:54:34 -0700 (PDT) X-Google-Smtp-Source: AGRyM1tuf3dFzH1eeVyrIYf9uQ36PrTg1kMYYYjWqWmUTFYnqzoRnxOxbOSPCWliWEpbT4w973pZ X-Received: by 2002:a17:907:a0c6:b0:72f:2293:bd04 with SMTP id hw6-20020a170907a0c600b0072f2293bd04mr9245223ejc.123.1658735674597; Mon, 25 Jul 2022 00:54:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658735674; cv=none; d=google.com; s=arc-20160816; b=qwDeNb7n9SEiWwwIacBlkznoXZrwFgxY9GnR5evtFUvXNvRPddGDCgThLtVTtLmqGZ 66+6p7G6g4tPMFkiATPP0cn8qXguM8uLCwLHxsJIqwt/AeB+AI/2oQZ+NBZlozRe68v7 GhDNDLiw9iYTMWMiiyC6FTFxkGVs78N5nmc0EnS/NWSHPx7xFJdw6lvSh3A7nWB79ltf +NtiL0C9w7Aw35MQMP6vKmR3Ua4NicjHsKbpjTyOMVaRBYtdxZF6lgVGtWCQ/meAkDTF 7dPk91DRZzMz+RiGoS6+9BBiboCgd2VNKd78tBnF5rZlonWlPwgQ7h8TkJoaVIdESoiU xPyQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=eMlcbANKfJle3FD3sp54e/O4tglNGsQHuCzPBZAyo74=; b=bGnmX1uXCy5rjNSMVxPgdQLO/SHSPnYi8DVd2ad4Tosu0CsrZCllFOivzCub74oyxs LqFXMdKa0It0cX8IYqgH3Lw1aRgbnZ0a0lGGZ0D0sXc0c1m/M7FAS7U5dvYZtbWTMK8H BaDUa9GIWVbQtC9cvfGQyITJksE9yOz4M4+dEJmdox9Q9bSPQi/yY+EZcX2l9l69rGUC gsFUU5H/eyJP7Z85bSjjeXsc6nCGo28GfUb52vS+H1csTeKObNaXR7evnbCRbthPuzbX 7epYbmoBynQKnFYwGJIDeKikN04QFM45ZHDoHDOLk2PTVcqpvhz+Y02oz+/V0H3oYnRD Kwtw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="M0b/RbMx"; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id sd23-20020a1709076e1700b0072b3ccf55e1si13786025ejc.786.2022.07.25.00.54.10; Mon, 25 Jul 2022 00:54: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="M0b/RbMx"; 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 S231594AbiGYHrH (ORCPT + 99 others); Mon, 25 Jul 2022 03:47:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40176 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230270AbiGYHqz (ORCPT ); Mon, 25 Jul 2022 03:46:55 -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 ESMTP id BE52412619 for ; Mon, 25 Jul 2022 00:46:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1658735209; 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: in-reply-to:in-reply-to:references:references; bh=eMlcbANKfJle3FD3sp54e/O4tglNGsQHuCzPBZAyo74=; b=M0b/RbMx7JydJShi0A0c/HF1o0eVs4B401m/mBRe7amfTgcdTrrrctJHzGcOI5TIuuuQzq PutUyOTTRZObZVAmLpSRdARoLFmduQ1qkqA0/mtSBVHE7BzbjdFVppEhwIkeKt41uk9bLY oSRqq925IXz4Kv4OXfpRzrmap6QOkZQ= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-589-zudWaJhpOeGcI6ka-egWfg-1; Mon, 25 Jul 2022 03:46:48 -0400 X-MC-Unique: zudWaJhpOeGcI6ka-egWfg-1 Received: by mail-wm1-f72.google.com with SMTP id n19-20020a05600c3b9300b003a314062cf4so5992351wms.0 for ; Mon, 25 Jul 2022 00:46:48 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc; bh=eMlcbANKfJle3FD3sp54e/O4tglNGsQHuCzPBZAyo74=; b=o28dyIA83t0rn8V2HKPbrJtmTcZnljh4kOoa7lxdo/pvu5YxrXqAHxOymbk6AU7bR8 cUllFEoL9Vr97xOQqUzYEcTg6fiyOQQVyrjYGV2dbyp2XYbNdyLkVEsitGdaDXaq79Bm 6XR+z28tp26eikw72ytpMM8wOwJuv/BTQ6+XcckjzTwBRNmHlMjjx0MwqloDaBlY3iKa V8Dr0QaGc/Foc9u7/iti8lk1hsBpgYmPGkfOpO01c+o8phIUBmtu439idQJ6GvWtDWIZ rIVKfZZE9sf5LQ1hZl80fvZ+Rq++yW3edukK/v9MFg/+gdvu4uMl/xivZ4ghnMcpr7jI DUNw== X-Gm-Message-State: AJIora+1WkYXv2uB8r2/QlYHRMcKMeaGiQVwPfbQ8/CEelR4uG1VB0/U oiK/RkCwe7289u/hHyilgRkHCzdKdc8/zipKhVcGW/EFiVRPL2Ekn6juNN8KzSRV8WfEGOIuT/g KIVeH9yHpJUhkxEQfDylxkacw X-Received: by 2002:a05:6000:2a8:b0:21d:8c81:7eb0 with SMTP id l8-20020a05600002a800b0021d8c817eb0mr6637326wry.460.1658735206808; Mon, 25 Jul 2022 00:46:46 -0700 (PDT) X-Received: by 2002:a05:6000:2a8:b0:21d:8c81:7eb0 with SMTP id l8-20020a05600002a800b0021d8c817eb0mr6637306wry.460.1658735206495; Mon, 25 Jul 2022 00:46:46 -0700 (PDT) Received: from redhat.com ([2.55.46.46]) by smtp.gmail.com with ESMTPSA id d5-20020adff845000000b0021dd08ad8d7sm11473081wrq.46.2022.07.25.00.46.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Jul 2022 00:46:45 -0700 (PDT) Date: Mon, 25 Jul 2022 03:46:42 -0400 From: "Michael S. Tsirkin" To: Jason Wang Cc: davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, virtualization@lists.linux-foundation.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, xuanzhuo@linux.alibaba.com Subject: Re: [PATCH V6] virtio-net: fix the race between refill work and close Message-ID: <20220725034545-mutt-send-email-mst@kernel.org> References: <20220725072159.3577-1-jasowang@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220725072159.3577-1-jasowang@redhat.com> X-Spam-Status: No, score=-3.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW, SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Jul 25, 2022 at 03:21:59PM +0800, Jason Wang wrote: > We try using cancel_delayed_work_sync() to prevent the work from > enabling NAPI. This is insufficient since we don't disable the source > of the refill work scheduling. This means an NAPI poll callback after > cancel_delayed_work_sync() can schedule the refill work then can > re-enable the NAPI that leads to use-after-free [1]. > > Since the work can enable NAPI, we can't simply disable NAPI before > calling cancel_delayed_work_sync(). So fix this by introducing a > dedicated boolean to control whether or not the work could be > scheduled from NAPI. > > [1] > ================================================================== > BUG: KASAN: use-after-free in refill_work+0x43/0xd4 > Read of size 2 at addr ffff88810562c92e by task kworker/2:1/42 > > CPU: 2 PID: 42 Comm: kworker/2:1 Not tainted 5.19.0-rc1+ #480 > Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS rel-1.16.0-0-gd239552ce722-prebuilt.qemu.org 04/01/2014 > Workqueue: events refill_work > Call Trace: > > dump_stack_lvl+0x34/0x44 > print_report.cold+0xbb/0x6ac > ? _printk+0xad/0xde > ? refill_work+0x43/0xd4 > kasan_report+0xa8/0x130 > ? refill_work+0x43/0xd4 > refill_work+0x43/0xd4 > process_one_work+0x43d/0x780 > worker_thread+0x2a0/0x6f0 > ? process_one_work+0x780/0x780 > kthread+0x167/0x1a0 > ? kthread_exit+0x50/0x50 > ret_from_fork+0x22/0x30 > > ... > > Fixes: b2baed69e605c ("virtio_net: set/cancel work on ndo_open/ndo_stop") > Signed-off-by: Jason Wang That's very clean, thanks! Acked-by: Michael S. Tsirkin > --- > drivers/net/virtio_net.c | 37 ++++++++++++++++++++++++++++++++++--- > 1 file changed, 34 insertions(+), 3 deletions(-) > > diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c > index 356cf8dd4164..ec8e1b3108c3 100644 > --- a/drivers/net/virtio_net.c > +++ b/drivers/net/virtio_net.c > @@ -242,9 +242,15 @@ struct virtnet_info { > /* Packet virtio header size */ > u8 hdr_len; > > - /* Work struct for refilling if we run low on memory. */ > + /* Work struct for delayed refilling if we run low on memory. */ > struct delayed_work refill; > > + /* Is delayed refill enabled? */ > + bool refill_enabled; > + > + /* The lock to synchronize the access to refill_enabled */ > + spinlock_t refill_lock; > + > /* Work struct for config space updates */ > struct work_struct config_work; > > @@ -348,6 +354,20 @@ static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask) > return p; > } > > +static void enable_delayed_refill(struct virtnet_info *vi) > +{ > + spin_lock_bh(&vi->refill_lock); > + vi->refill_enabled = true; > + spin_unlock_bh(&vi->refill_lock); > +} > + > +static void disable_delayed_refill(struct virtnet_info *vi) > +{ > + spin_lock_bh(&vi->refill_lock); > + vi->refill_enabled = false; > + spin_unlock_bh(&vi->refill_lock); > +} > + > static void virtqueue_napi_schedule(struct napi_struct *napi, > struct virtqueue *vq) > { > @@ -1527,8 +1547,12 @@ static int virtnet_receive(struct receive_queue *rq, int budget, > } > > if (rq->vq->num_free > min((unsigned int)budget, virtqueue_get_vring_size(rq->vq)) / 2) { > - if (!try_fill_recv(vi, rq, GFP_ATOMIC)) > - schedule_delayed_work(&vi->refill, 0); > + if (!try_fill_recv(vi, rq, GFP_ATOMIC)) { > + spin_lock(&vi->refill_lock); > + if (vi->refill_enabled) > + schedule_delayed_work(&vi->refill, 0); > + spin_unlock(&vi->refill_lock); > + } > } > > u64_stats_update_begin(&rq->stats.syncp); > @@ -1651,6 +1675,8 @@ static int virtnet_open(struct net_device *dev) > struct virtnet_info *vi = netdev_priv(dev); > int i, err; > > + enable_delayed_refill(vi); > + > for (i = 0; i < vi->max_queue_pairs; i++) { > if (i < vi->curr_queue_pairs) > /* Make sure we have some buffers: if oom use wq. */ > @@ -2033,6 +2059,8 @@ static int virtnet_close(struct net_device *dev) > struct virtnet_info *vi = netdev_priv(dev); > int i; > > + /* Make sure NAPI doesn't schedule refill work */ > + disable_delayed_refill(vi); > /* Make sure refill_work doesn't re-enable napi! */ > cancel_delayed_work_sync(&vi->refill); > > @@ -2792,6 +2820,8 @@ static int virtnet_restore_up(struct virtio_device *vdev) > > virtio_device_ready(vdev); > > + enable_delayed_refill(vi); > + > if (netif_running(vi->dev)) { > err = virtnet_open(vi->dev); > if (err) > @@ -3535,6 +3565,7 @@ static int virtnet_probe(struct virtio_device *vdev) > vdev->priv = vi; > > INIT_WORK(&vi->config_work, virtnet_config_changed_work); > + spin_lock_init(&vi->refill_lock); > > /* If we can receive ANY GSO packets, we must allocate large ones. */ > if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) || > -- > 2.25.1