From owner-freebsd-xen@freebsd.org Sun Jul 29 13:43:37 2018 Return-Path: Delivered-To: freebsd-xen@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 1222110578E4 for ; Sun, 29 Jul 2018 13:43:37 +0000 (UTC) (envelope-from pratiy0100@gmail.com) Received: from mail-it0-f53.google.com (mail-it0-f53.google.com [209.85.214.53]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client CN "smtp.gmail.com", Issuer "Google Internet Authority G2" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 826A181A98; Sun, 29 Jul 2018 13:43:36 +0000 (UTC) (envelope-from pratiy0100@gmail.com) Received: by mail-it0-f53.google.com with SMTP id s7-v6so13762957itb.4; Sun, 29 Jul 2018 06:43:36 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to:cc; bh=E35pgb/USmEMXS3r/HI0zKRgyYN0PpBTWM/lDvSXfLc=; b=OxxnoNrH2dgW9W75Y1s36LQSl5+LDJFTT7dOFsCjSGXaEDvqSakSz03X+Zm7GLn8nH kQvO0YNNiQsqBIOLAn5sqdxHvSo9puGAEFcrzOGSn/e21aFH9Vjlhw1cQyu25FB5OGHf Zs01qG2dHYwfWGsVuZuwzz13Md/4ZFTxWOU27bUYlRMvsm/SYOQbJRXOrB3g2mihK9cf 09f3bOAkNceqH4OOQ+4Jw3Cq2rv6EOZOjHC7VdRLDzqvAp3zxqq/cak0TsmKi9uKO8ie a7rr7vOihvx1DX9C+4u40fTI9iHCRGICaLj8iFQZQqSd49iC3KToyAufCoNRThtNapCE Hu7A== X-Gm-Message-State: AOUpUlFpqjssMdE0mdw9rUCf97iHfrg+MBTRljJhpDHY0o0eyrY2nLUk 987WRfh5kTleuctJzZU6DWyNLN5ZLQpaog== X-Google-Smtp-Source: AAOMgpfWfJSXWrsm8XBr0r7c5yI9gOmpnVpb3Jfs04OdR7meq7mypdZLNBtey0wZwXQH/uifD2R5sA== X-Received: by 2002:a24:2206:: with SMTP id o6-v6mr11354644ito.112.1532867972973; Sun, 29 Jul 2018 05:39:32 -0700 (PDT) Received: from mail-io0-f181.google.com (mail-io0-f181.google.com. [209.85.223.181]) by smtp.gmail.com with ESMTPSA id t4-v6sm2983034iof.55.2018.07.29.05.39.32 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 29 Jul 2018 05:39:32 -0700 (PDT) Received: by mail-io0-f181.google.com with SMTP id l7-v6so7707446ioj.1; Sun, 29 Jul 2018 05:39:32 -0700 (PDT) X-Received: by 2002:a6b:c844:: with SMTP id y65-v6mr10703672iof.187.1532867972491; Sun, 29 Jul 2018 05:39:32 -0700 (PDT) MIME-Version: 1.0 From: Pratyush Yadav Date: Sun, 29 Jul 2018 18:08:56 +0530 X-Gmail-Original-Message-ID: Message-ID: Subject: Recursion in non-recursive mutex when using the grant table free callbacks To: =?UTF-8?Q?Roger_Pau_Monn=C3=A9?= Cc: Akshay Jaggi , Edward Napierala , FreeBSD-Xen Content-Type: text/plain; charset="UTF-8" X-BeenThere: freebsd-xen@freebsd.org X-Mailman-Version: 2.1.27 Precedence: list List-Id: Discussion of the freebsd port to xen - implementation and usage List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 29 Jul 2018 13:43:37 -0000 Hi, Currently, the grant table free callbacks can not work. This is because of a recursion on a non-recursive mutex that causes a kernel panic. The cause of the recursion is: check_free_callbacks() is always called with the lock gnttab_list_lock held. So, the callback function is called with the lock held. So, when the client uses any of the grant reference allocation methods get_free_entries() is called, which tries to acquire gnttab_list_lock(grant_table.c:77 [0]), causing a recursion on the lock. I'm not sure what the correct fix would be though. One way I can think of is that check_free_callback() should be called without the lock held. But with this fix, it is possible for the callback to be called even though the grant references it needs are not available. This would happen when another thread takes those references while the current thread has completed the check if(gnttab_free_count >= callback->count) but has not yet called the callback (grant_table,c:105 [1]). I think a better way to fix this would be to have a check in get_free_entries() whether the current thread holds the lock, so it does not try to acquire the lock if the current thread already holds it. If you agree, I will send a patch. [0] https://github.com/freebsd/freebsd/blob/master/sys/dev/xen/grant_table/grant_table.c#L77 [1] https://github.com/freebsd/freebsd/blob/master/sys/dev/xen/grant_table/grant_table.c#L105 -- Regards, Pratyush Yadav