Received: by 2002:ac8:6216:0:b0:41c:c224:f26f with SMTP id ks22csp77105qtb; Thu, 2 Nov 2023 12:02:17 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHrJMH1htpdr0ZjWCvS9xniMb4GZuvz83cUrDPctTyvceHOj4k8/08P0BEca5Wm741QBmKA X-Received: by 2002:a05:6a00:2d9d:b0:6b1:b5c4:a8b0 with SMTP id fb29-20020a056a002d9d00b006b1b5c4a8b0mr23866024pfb.23.1698951736948; Thu, 02 Nov 2023 12:02:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698951736; cv=none; d=google.com; s=arc-20160816; b=pAPu/1tAoS7cv0zhdqLYY71GV8KKxeYDm2Jj9ZFrs827+m5Tj3A16aaX8d43cmvbix QZFd0DFuRym0yvwvwZyZIo030niKCsHwEu0yvOF0DCuY3X37hMO4yqEmy0YMYCxkfy/l sJ8qAB2imKn2M1I9echt06C1YlTjrxrborIjp299oBFl0eTB+j+Q7lkNx5XVztdZGu1N RcgEmbluVdVCQKIAqq2wrPG+X5X05bxg5nBmg0Xf1936f/fzPkG2Yh7P4VzfCrBymI6/ ANzLe515gttz5bSGgBa8LmXoEZ39vA1reBjlQph41RKP5hZWnJU5PxZj+1unpzYeTbAn rJhQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=QB2XELWyUcHbwxMPwoh6o4DxLx3LMH175ftg3GV0qrE=; fh=BGJczsLANe/6qKtgCujR6ncTLsh0RS0r9nGoD5mB/1o=; b=IqlY8JvcnY07Na4mMst2q/qcIJCYfBI6iWbiLDADZeKcf+ki4tUneZIeqNDgKwo3Z5 Tdggd0wPc63cVRIOjdsZiNoY8ZE0ntzp0/4QsLJVLY8xgvG0QP02tPn74h8/UxpbyoBI +kC+cwntw3IrrwdDBwa5TUIjQymdKu9xVUmX8gZUXwfGCAsrqwnWd/bUjlji2d2FXCQL 7TiXCSIHRREeeLOhXwKWbXxRZi1DmevozVU/Sest+2imTuWoRHz1gy/0AJlXeOBxBg7T gdIR6Ukz9MLo75dLrY/ASMgFU4VVZksuMljCf+T7UCJRVcxPqe+4IJVNiA4bej2X+Uol nadw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=UnpDaF2X; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from groat.vger.email (groat.vger.email. [2620:137:e000::3:5]) by mx.google.com with ESMTPS id ca19-20020a056a02069300b005b909e678dfsi125983pgb.450.2023.11.02.12.02.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Nov 2023 12:02:16 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) client-ip=2620:137:e000::3:5; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=UnpDaF2X; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id 0803480EE804; Thu, 2 Nov 2023 12:02:04 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377370AbjKBTAq (ORCPT + 99 others); Thu, 2 Nov 2023 15:00:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34850 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235011AbjKBTAi (ORCPT ); Thu, 2 Nov 2023 15:00:38 -0400 Received: from mail-pg1-x54a.google.com (mail-pg1-x54a.google.com [IPv6:2607:f8b0:4864:20::54a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5615C182 for ; Thu, 2 Nov 2023 12:00:36 -0700 (PDT) Received: by mail-pg1-x54a.google.com with SMTP id 41be03b00d2f7-5bd18d54a48so1001298a12.0 for ; Thu, 02 Nov 2023 12:00:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698951636; x=1699556436; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=QB2XELWyUcHbwxMPwoh6o4DxLx3LMH175ftg3GV0qrE=; b=UnpDaF2X+wY//sVHfcElHJ+GOjFIaz6dlasDN77KPeQtZyBoBVWOJkxGDX94T2Qumv gFoIrYn/2RXxtHMV5LqiJVHIk7sVMh66ahoFQpuR3Hsbb9tDTuLYyryHcgKhVcaK42kK lgv+zZKcQRDzUnDKMvY4ddFenQLQoXPuYMX4F/H02RA4SOaqMhFo5kCLcnxm1TTn1tK5 Acxh0ofyjKbop6GCjA2pjfDGBe07X3XlEoJDZtA8d6851CoEkh5N8jTMPsM6yA7twQzL XOQu4msePWDblqcmL7wY8d2A4WnF22lXcco/YDMTC7ntm0nFmLgARQIodFzm4AZqAA3V Xt/g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698951636; x=1699556436; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=QB2XELWyUcHbwxMPwoh6o4DxLx3LMH175ftg3GV0qrE=; b=hNbDWGKPZGKqWOdkQOy93qIQma75d40RxgYFGoF5XodBAWbD2bgEvG3UbZCG1KYeE8 zsp69iTR7xiffukM8vVC+mnpE9Ee6Ft3jKoqikoDc/axT9ntiLyefj+cb+hXXoxhvZ7X JcOjP2UBArKxX+SkaHEp7U+h20/NTKOg89Aqtz3+b8MvaKbk5UUl3tUUY1jZZUZixEuL vvuHGVtMWpfrLNc0icE47ZLKuKFdmjp9bhj3TPkHXQCeSnwXSMDIWZAdYgF4tCkHe0Xg 4NFKb7jTH+DtFY4odGCFiXAP3DpZV/QikEcOvw/ci+mr86STeiX+7mv3dYH3cxYr1EzE wgww== X-Gm-Message-State: AOJu0YxTpJMwrrpvP/2BIJGIsaI0Xd/FXHfwKm+cGyrp7xnO4jdV4WHu 6OM/v7GhYreNet02oufqNpl3MB8oPx/UbQ== X-Received: from xllamas.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5070]) (user=cmllamas job=sendgmr) by 2002:a63:35c3:0:b0:5b0:c0b0:4cd3 with SMTP id c186-20020a6335c3000000b005b0c0b04cd3mr343744pga.6.1698951635832; Thu, 02 Nov 2023 12:00:35 -0700 (PDT) Date: Thu, 2 Nov 2023 18:59:04 +0000 In-Reply-To: <20231102185934.773885-1-cmllamas@google.com> Mime-Version: 1.0 References: <20231102185934.773885-1-cmllamas@google.com> X-Mailer: git-send-email 2.42.0.869.gea05f2083d-goog Message-ID: <20231102185934.773885-4-cmllamas@google.com> Subject: [PATCH 03/21] binder: fix race between mmput() and do_exit() From: Carlos Llamas To: Greg Kroah-Hartman , "=?UTF-8?q?Arve=20Hj=C3=B8nnev=C3=A5g?=" , Todd Kjos , Martijn Coenen , Joel Fernandes , Christian Brauner , Carlos Llamas , Suren Baghdasaryan , Brian Swetland Cc: linux-kernel@vger.kernel.org, kernel-team@android.com, Greg Kroah-Hartman Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Thu, 02 Nov 2023 12:02:04 -0700 (PDT) Task A calls binder_update_page_range() to allocate and insert pages on a remote address space from Task B. For this, Task A pins the remote mm via mmget_not_zero() first. This can race with Task B do_exit() and the final mmput() refcount decrement will come from Task A. Task A | Task B ------------------+------------------ mmget_not_zero() | | do_exit() | exit_mm() | mmput() mmput() | exit_mmap() | remove_vma() | fput() | In this case, the work of ____fput() from Task B is queued up in Task A as TWA_RESUME. So in theory, Task A returns to userspace and the cleanup work gets executed. However, Task A instead sleep, waiting for a reply from Task B that never comes (it's dead). This means the binder_deferred_release() is blocked until an unrelated binder event forces Task A to go back to userspace. All the associated death notifications will also be delayed until then. In order to fix this use mmput_async() that will schedule the work in the corresponding mm->async_put_work WQ instead of Task A. Fixes: 457b9a6f09f0 ("Staging: android: add binder driver") Signed-off-by: Carlos Llamas --- drivers/android/binder_alloc.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/android/binder_alloc.c b/drivers/android/binder_alloc.c index c4d60d81221b..046c3ae9eb8f 100644 --- a/drivers/android/binder_alloc.c +++ b/drivers/android/binder_alloc.c @@ -271,7 +271,7 @@ static int binder_update_page_range(struct binder_alloc *alloc, int allocate, } if (mm) { mmap_write_unlock(mm); - mmput(mm); + mmput_async(mm); } return 0; @@ -304,7 +304,7 @@ static int binder_update_page_range(struct binder_alloc *alloc, int allocate, err_no_vma: if (mm) { mmap_write_unlock(mm); - mmput(mm); + mmput_async(mm); } return vma ? -ENOMEM : -ESRCH; } -- 2.42.0.869.gea05f2083d-goog