From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E6D7AC433EF for ; Wed, 29 Jun 2022 08:45:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232885AbiF2IpK (ORCPT ); Wed, 29 Jun 2022 04:45:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59046 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232870AbiF2IpF (ORCPT ); Wed, 29 Jun 2022 04:45:05 -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 4407C3C709 for ; Wed, 29 Jun 2022 01:45:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1656492303; 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=Ce8LCIJWlyU+gKAFW1266Y8Jb/qLrB5BB1Xt2Vb9bMA=; b=T3j7ro3Lxuxv9oRuY2zUY/+z8jpvAv0NInhIuYmQYJ61Todzndwj7RhTSblZgiY0t5yA3J 9Q+9MHsmu6DQHAeGKtjG50O1/EbEITm66xdeZRaj1oMsqxwuNx3dKZAy9pxoWBpLcFFBfC PRTQHyi6tm4wl6F+4zkwB2HI1aQKe5A= Received: from mail-wr1-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-49-i5nTdZyTPrCJ76dDWvaz9Q-1; Wed, 29 Jun 2022 04:45:02 -0400 X-MC-Unique: i5nTdZyTPrCJ76dDWvaz9Q-1 Received: by mail-wr1-f70.google.com with SMTP id e5-20020adff345000000b0021b9f00e882so2178699wrp.6 for ; Wed, 29 Jun 2022 01:45:01 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=Ce8LCIJWlyU+gKAFW1266Y8Jb/qLrB5BB1Xt2Vb9bMA=; b=d9yD4Jk5/7arXvkMelwhlhGK/OdWZkaaSrYz3xMYJnjtyDoG0NmhTE/PM2gfbQp4me /Sb82MPasmkMbEjWAW0OxiWfJBeATBkfVOiseHQW/nV6CF87lIzHXRpmA/xyz7ZMuC5q JEjy04En9cxqmNNwI0IpelVmNp8RljquBhXDKuq/F4C6qBKem41jcZn15P69o841a5P6 zUAO/wEFdfnSoZ/us5ivd91eYqQwKye+MNK7UYE+j7zGTZDHQ+mQ26IF8FODaZxAetH7 Dy5c/PJ+C1wvahj9Lw5gwg5AH+bltooot0ynXVbbM5uXoOJhxK26x3fDKMy5koq4V+Hh Izrg== X-Gm-Message-State: AJIora+TlPuyXJPAYRAcJ04eZiLPoL+Gm5IoZGSFEIkl3LtJ2XEdBb7S TZeD1puLyUJjAaVt5MMlKh2FFz45AUwj+ATMfDrsa+WWH/7DJVvFbkQBmLSmnuFST2XPOBmlWUo WGJhEJeZWfts3B3yS X-Received: by 2002:a05:600c:41d1:b0:3a0:301d:f75f with SMTP id t17-20020a05600c41d100b003a0301df75fmr4234829wmh.31.1656492300134; Wed, 29 Jun 2022 01:45:00 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vq81KZkWHPP6HoDbBvjIka45K9E4a+OATXJO8gsMLATGBlbrgfVU1VS4agrmPdzgbyKsGUnQ== X-Received: by 2002:a05:600c:41d1:b0:3a0:301d:f75f with SMTP id t17-20020a05600c41d100b003a0301df75fmr4234805wmh.31.1656492299865; Wed, 29 Jun 2022 01:44:59 -0700 (PDT) Received: from redhat.com ([2.52.23.204]) by smtp.gmail.com with ESMTPSA id t11-20020adff60b000000b0021b962f4256sm15540856wrp.80.2022.06.29.01.44.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Jun 2022 01:44:59 -0700 (PDT) Date: Wed, 29 Jun 2022 04:44:55 -0400 From: "Michael S. Tsirkin" To: Jason Wang Cc: davem , Jakub Kicinski , virtualization , netdev , linux-kernel Subject: Re: [PATCH] virtio-net: fix the race between refill work and close Message-ID: <20220629044316-mutt-send-email-mst@kernel.org> References: <20220628090324.62219-1-jasowang@redhat.com> <20220629032106-mutt-send-email-mst@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org On Wed, Jun 29, 2022 at 04:36:12PM +0800, Jason Wang wrote: > On Wed, Jun 29, 2022 at 3:29 PM Michael S. Tsirkin wrote: > > > > On Tue, Jun 28, 2022 at 05:03:24PM +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 the > > > source the scheduling > > > > can't parse this sentence > > I actually meant "we don't disable the source of the refill work scheduling". > > > > > > of the refill work. This means an NAPI > > > > what do you mean "an NAPI"? a NAPI poll callback? > > Yes. pls post with a fixed log then. > > > > > 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 > > > --- > > > drivers/net/virtio_net.c | 38 ++++++++++++++++++++++++++++++++++++-- > > > 1 file changed, 36 insertions(+), 2 deletions(-) > > > > > > diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c > > > index db05b5e930be..21bf1e5c81ef 100644 > > > --- a/drivers/net/virtio_net.c > > > +++ b/drivers/net/virtio_net.c > > > @@ -251,6 +251,12 @@ struct virtnet_info { > > > /* Does the affinity hint is set for virtqueues? */ > > > bool affinity_hint_set; > > > > > > + /* Is refill work enabled? */ > > > + bool refill_work_enabled; > > > + > > > + /* The lock to synchronize the access to refill_work_enabled */ > > > + spinlock_t refill_lock; > > > + > > > /* CPU hotplug instances for online & dead */ > > > struct hlist_node node; > > > struct hlist_node node_dead; > > > @@ -348,6 +354,20 @@ static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask) > > > return p; > > > } > > > > > > +static void enable_refill_work(struct virtnet_info *vi) > > > +{ > > > + spin_lock(&vi->refill_lock); > > > + vi->refill_work_enabled = true; > > > + spin_unlock(&vi->refill_lock); > > > +} > > > + > > > +static void disable_refill_work(struct virtnet_info *vi) > > > +{ > > > + spin_lock(&vi->refill_lock); > > > + vi->refill_work_enabled = false; > > > + spin_unlock(&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_work_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_refill_work(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_refill_work(vi); > > > /* Make sure refill_work doesn't re-enable napi! */ > > > cancel_delayed_work_sync(&vi->refill); > > > > > > @@ -2776,6 +2804,9 @@ static void virtnet_freeze_down(struct virtio_device *vdev) > > > netif_tx_lock_bh(vi->dev); > > > netif_device_detach(vi->dev); > > > netif_tx_unlock_bh(vi->dev); > > > + /* Make sure NAPI doesn't schedule refill work */ > > > + disable_refill_work(vi); > > > + /* Make sure refill_work doesn't re-enable napi! */ > > > cancel_delayed_work_sync(&vi->refill); > > > > > > if (netif_running(vi->dev)) { > > > @@ -2799,6 +2830,8 @@ static int virtnet_restore_up(struct virtio_device *vdev) > > > > > > virtio_device_ready(vdev); > > > > > > + enable_refill_work(vi); > > > + > > > if (netif_running(vi->dev)) { > > > for (i = 0; i < vi->curr_queue_pairs; i++) > > > if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL)) > > > @@ -3548,6 +3581,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) || > > > > > > Can't say I love all the extra state but oh well. > > I couldn't find a better way. The tricky part is that NAPI and refill > can schedule each other so we need a third state. > > Thanks I wonder whether we can add a napi flag that says going away do not schedule, and have napi_enable/napi_disable fail then. > > > > > -- > > > 2.25.1 > >