From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 256C41FE44A for ; Sat, 8 Aug 2026 13:49:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.71 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786196990; cv=none; b=WrKzFQL+UMqdEBeu3gaP0hofdxs+kDsDkzphVQ8rsqq9HVggbnKqxSKacKMeuybA7u9MT7cURoz4Y5gD3fPsDXF9vDIOLhWAWMACjKCjNTbTHugnjAaL3OnD7CDA5/ZoE9rNXkk+v2xCT8AOe0ee2GQm0yXnMtPX40f/5YhmMfM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786196990; c=relaxed/simple; bh=edrIWypBZRq3a2caYGfI/F6Bz5WrxkyVpxlnbtIOvf0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Vi5QajzZbasiCY6BbLS3hRrTUJM8lxfzvYWRYM3azyH5QuuJc8ePHUU/GW/peCQXQMjDOAmlLufol+XETTdE//8YSYvDX9lhFbW03IjM2LY39Ye4k9rEAQyhCcdXzUhBQln6yi1M+KPB3PT+p7F0RnDcmpNxpydX3PC+ZAMo+Dk= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=U2nN/SH+; arc=none smtp.client-ip=209.85.128.71 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="U2nN/SH+" Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-4954c2d4081so4094415e9.2 for ; Sat, 08 Aug 2026 06:49:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1786196987; x=1786801787; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=mndilLUNnz/RAunCNJWp0FhZJrPCt2iyLLy5uowr7xI=; b=U2nN/SH+YQ4E3mF0S/Vj9ZJPInVcKN8L+FJywJGLqYXxCCP/N8EifyzxQV097xy3ia mpR6ytslcsTiqn2VVCH2tdHIOnc9BK7hlprKHKW4LjPtlJcm+aS39gQumTKrPGhKtMLo U8UmunfEa889gRRW4++N9gElf4KK/dMdWecdmLpVCj9uOC8qSw9vgqeCXoFFB6AO4rRu w3RV9giBx5DKt+rkkOy9EenFPF5+aYR2fKbXVuWEB9ldAy+d7aomZyMOEPYqqDeTNECl DDfbywIcCPXR+ybrZII6O5EFtonxEHBAfci8G5ue16dNX2jIPHbFH9d+TfX++pnFbdr4 IysQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1786196987; x=1786801787; h=content-type: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:content-type; bh=mndilLUNnz/RAunCNJWp0FhZJrPCt2iyLLy5uowr7xI=; b=LdLRuFUdLYTYYWSx+cxlZyzeZE50AwGHIyABXYXkgbT8DsHbIWpsfwfV+4T6FisxXf x8hkyAUf+rltqugkLqAVWl16JEYDtWV7sgVp4SM5yh6wwTaYY1jFxp7lqgoefgXBVPoa u5Oe/KxcNnCu8vB/RSWXjhmWw+9s+wqLQetBGWb1oDGnb3SQJP6SsyKN13uKkrK6S3VP ZooYcgIR4+qCmT349WB2kwb8/XDnevAkasdg5xEe5wkyVuSp8wd6AUI5cMUy2dH1vKYr qZINsgqh3BDFyThYAgX4JaKyjo8l94kRriTAQH0pRvo+f21YKZGHEw7FXGGRILLLZgxd YY9A== X-Forwarded-Encrypted: i=1; AHgh+RrB/4gLJ/nA4j4F5zTDqj7Fib0El7NPwh25CKCLmAbaoP0ESnLx7djn7DT463TLQX/we/HkSUvL0el2dfk=@vger.kernel.org X-Gm-Message-State: AOJu0YwqhB5FzdEBe+AQyXjGh3AXiCwRBRtGUHBqe8EHIDu7zOyvFxga W3VBUsRS2oUiLeLcBACeQxGKkOLFXy6AcxbAK3dCtw8mF1636a6r1BvdH2r/xe/LUUSgtfjhtWD 2J0NIRm+bgQ+s2pa0kA== X-Received: from wmdd14.prod.google.com ([2002:a05:600c:a20e:b0:493:b6e1:bb31]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:198f:b0:492:4e09:9fc1 with SMTP id 5b1f17b1804b1-4994e7cc5a4mr409940355e9.15.1786196987129; Sat, 08 Aug 2026 06:49:47 -0700 (PDT) Date: Sat, 8 Aug 2026 13:49:46 +0000 In-Reply-To: <20260806142301.2144536-1-zhangbo56@xiaomi.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260806142301.2144536-1-zhangbo56@xiaomi.com> Message-ID: Subject: Re: [RFC PATCH 0/1] binder: switch alloc->mutex back to spinlock From: Alice Ryhl To: Bo Zhang Cc: gregkh@linuxfoundation.org, cmllamas@google.com, arve@android.com, tkjos@android.com, christian@brauner.io, surenb@google.com, baohua@kernel.org, zhanghongru06@gmail.com, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" On Thu, Aug 06, 2026 at 10:23:01PM +0800, Bo Zhang wrote: > On Thu, Aug 06, 2026 at 09:01:19AM +0000, Alice Ryhl wrote: > > This will not work due to the following race condition between T1 and T2: > > > > T1 binder_alloc_free_page() removes the page from alloc->pages > > T2 binder_alloc_new_buf_locked() runs > > T2 binder_install_single_page() gets -EBUSY > > T2 binder_page_lookup() looks up the page in the vma > > T1 binder_alloc_free_page() calls zap_vma_range() > > > > In this scenario, T2 looks up a page that it expects to be "pinned" and > > not removable by the shrinker, but the shrinker zaps it anyway. > > > > Today this cannot happen because binder_alloc_new_buf_locked() runs > > under the mutex, which ensures that zap_vma_range() finishes removing > > the page before binder_install_single_page() runs. > > Hi Alice, > > Thanks for pointing out this race. You're right that simply moving > spin_unlock() before zap_vma_range() opens a window where the install > side could GUP the old page that is about to be freed. > > However, I think it could be resolved without keeping the lock across > zap_vma_range(). The solution might be: > > The install side only calls vm_insert_page() when pages[index] == NULL. > And the shrinker sets pages[index] = NULL before zapping. So if > vm_insert_page() returns -EBUSY, we can check pages[index] to determine > which scenario we're in: > > 1. pages[index] != NULL: Another installer won the race. It's safe to > GUP the page, which is the normal concurrent install case. > > 2. pages[index] == NULL: The shrinker cleared it but hasn't zapped > the PTE yet. We must NOT GUP the old page. Return -EAGAIN and > let the caller retry after the shrinker finishes. > > Case 2 is safe because: the install side only attempts installation > when pages[index] == NULL, so if EBUSY occurs with pages[index] == NULL, > it must be a PTE left over from the page that the shrinker is currently > reclaiming. After the shrinker zaps and frees, the retry will find > pages[index] still NULL, call vm_insert_page() again, and succeed since > the PTE is now clear. > > Case 1 cannot race with the shrinker because: if pages[index] != NULL > at the time of the EBUSY check, the page was just installed by another > thread and is not on the LRU freelist, so the shrinker won't touch it. > > The change would look like: > > case -EBUSY: > binder_free_page(page); > if (!binder_get_installed_page(alloc, index)) { > /* shrinker is reclaiming, retry */ > ret = -EAGAIN; > break; > } > /* normal concurrent install, look up the winner's page */ > page = binder_page_lookup(alloc, addr); > ... > > And the caller retries on -EAGAIN with a cond_resched(). > > This also should not introduce any performance regression since the > -EAGAIN retry only triggers in the rare case where EBUSY coincides with > an active shrinker reclaim on the same page index, a window that lasts > only for the duration of a single zap_vma_range() call. > > Does this approach look reasonable to you? No, this will deadlock if the thread installing the page is SCHED_FIFO. Waiting for the shrinker by repeatedly checking whether the shrinker is done in a loop does not work. As an aside, your solution also adds similar waiting for concurrent installers. If the successful installer gets preempted between binder_page_insert() and binder_set_installed_page(), then your code will also repeatedly retry until said thread gets scheduled in again. So there is a similar deadlock if the inserter that failed the race is SCHED_FIFO. Alice