From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f226.google.com (mail-pl1-f226.google.com [209.85.214.226]) (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 B748A477298 for ; Fri, 31 Jul 2026 19:09:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.226 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785524966; cv=none; b=EBZdMwlMMSMbR4BIZJoBZ8e+yF4qL44V7fv1TYtB7Z7b8wd3U4ZMNtcb+7SLSNsytxc7VFfkXgS+uv7Qs28WsSHPXQMMDcKYrqBTNMW3WoK2KrsIRW0hBASQROPvuQz9O4Q762+lAMS3l3f2wk9tKlgF8LjSbXS1DBFGlad/GMo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785524966; c=relaxed/simple; bh=voXzClVd6GoXHUqXOpaLFxIV+kMvz05cQg47euHLCew=; h=From:To:Cc:Subject:Date:Message-ID; b=lBuIEhWKURlOqPyE975BF3m13d9DBsbj9YGwTWpk4vV6QdLS42hbwGS39RAVRXqUwNtqxHyCWQWZcJp6/wbOPavyN58ISdtrvaGvOH5/+6gNqJrN/O03PI9fp/3azx9MiFg9GP6C48gs749MivqwVFRU0iu23oYo0tdbdTGGGGs= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=arista.com; spf=pass smtp.mailfrom=arista.com; dkim=pass (2048-bit key) header.d=arista.com header.i=@arista.com header.b=KLxLIXtU; arc=none smtp.client-ip=209.85.214.226 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=arista.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arista.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=arista.com header.i=@arista.com header.b="KLxLIXtU" Received: by mail-pl1-f226.google.com with SMTP id d9443c01a7336-2caed617615so18072175ad.3 for ; Fri, 31 Jul 2026 12:09:24 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785524964; x=1786129764; h=content-transfer-encoding:message-id:date:subject:cc:to:from :dkim-signature:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=HeJqJiUGhw1arV0neXDR3NmNqOCHvAXptXpu44OsOMY=; b=GWWGsj6kJWBNxu+uqw7a2J8PCwkvmhDztTFEL6sji5bfhYnlNo8535hEac+0BtIoSK B8kCWw9HTYx63HNeGO+6ChNNUd531pX20VdM9kgQFtMg4t0o9l8cZC0tc/ZwjFko+SKA dh6/6SAEDdDWdWO/oR5fkH3f3krU2jQ9IZWyAOGJT2S4UGsOUDvIE2S4t0qy9pDbmpBa 1tbEF53dp1YmznZrLVNuebmHjveNf52HJjm7o86v+rhk3GT0WnbE0XBWzRyKNkg6oK8p p2kmDCpcVCBwpzZnVSX6f4CG+G0DIJyqTPCgGiORyADCu8uTLuxQBDVl/JvC2z+PfF2i dj9Q== X-Forwarded-Encrypted: i=1; AHgh+Rqb1aT2pbR0+4LiGMi1q+6b5iNz5Hv4sclB5oBIafIG94F1OdQvuPDoX3GpM0JOpUmcFd/OTO2+XWMxY5Q=@vger.kernel.org X-Gm-Message-State: AOJu0Yzg0VlM3Caut8U8eGNtCsoYL+CgYD5oFNBTINYRRs7vB30S3hDz UAlFoGEJmPFzw1lg/j3g0KnPbZKS1ee6LsPBXUiyiAHJoamVhDE2jDxto+0jAaaFY5B2SDxJQ9C t0rTnqj2pz1bbob2FWg9f4XTdPEgXB3KHkA== X-Gm-Gg: AR+sD11PNhqAniTj0gZ2GJwkfd7QZd+6AWWSY26YXzeiI5zzY5aP7g9OMJE47HO+STp 9zaEX5aSUevYGa6RlgtaeiL2Ew61O5fB6BN69oYy3Z49HXjPgX4MI3tjJc6FYX+aaE7yTqR6t6y R10ItuBi3t9+aSbsAWSQY+puB1UqaJknrMlql/7MG4SQoApjiuz9KOvXeMtdqm0PfaGODS/ZwM3 fcD1gJ5IxEQ+s3PwKgBdxc+BIUb4YKvYn+oGusSpwNjXqLDyUH7kNqMgWUCd1QzbajdUoixu0vQ FRi6c0yZKb5V2ynHNOcDYHSNJ0pksGlBHYyxr4xWi2igAnrXlEeAEeTMM5RRnZP+wvn+natggQ4 /+b7y+D+keN5sp6b6uFTreUvXRm5aAYDepBNNLOB2tL8r11+F1SEFM68YNoYh9YFiQjQ= X-Received: by 2002:a17:902:e543:b0:2ca:329:3da1 with SMTP id d9443c01a7336-2d052215bb2mr10667575ad.16.1785524963897; Fri, 31 Jul 2026 12:09:23 -0700 (PDT) Received: from smtp.aristanetworks.com (ec2-52-52-135-65.us-west-1.compute.amazonaws.com. [52.52.135.65]) by smtp-relay.gmail.com with ESMTPS id d9443c01a7336-2d04aeb6e27sm1886745ad.20.2026.07.31.12.09.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 31 Jul 2026 12:09:23 -0700 (PDT) X-Relaying-Domain: arista.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=arista.com; s=Arista-A; t=1785524962; bh=HeJqJiUGhw1arV0neXDR3NmNqOCHvAXptXpu44OsOMY=; h=From:To:Cc:Subject:Date:From; b=KLxLIXtUuBQkEWgsiWMTEUGFGWArp153Es896sT+jo3LgpGJNvscYeMSf36hwkiF5 SA4IUh0Csh1ba1bBIBEA52qipRfD3rksJAPYJ2tQJ3SLtlPgC4RsrSIUhupeLZoWjX GXd7TmjU1u9KqCYMyPgxrslHooBNDNavuxXVcDO5ZDfEwxOqroF86ahz3ClazViRje vEeONshyBzTGL+i10r1/CK7bCAFpt7KLmFr1goF9Dy0LTjZFEuiprULdtIkdLJl8eu J9i0ij9SvAnIa1u8mwYYeNrPUDTF9Aut+ZBiFDw/9FFC9GYBVzajYegdh3c8XAMI3s Jm09/d0rUoDlA== Received: from kevmitch-g-eos-trunk-2026-07-17.sjc.aristanetworks.com (dhcp-242-248-27.sjc.aristanetworks.com [10.242.248.27]) by smtp.aristanetworks.com (Postfix) with ESMTP id 89859C0FEF; Fri, 31 Jul 2026 19:09:22 +0000 (UTC) X-SMTP-Authentication: Allow-List-permitted X-SMTP-Authentication: Allow-List-permitted From: Kevin Mitchell To: Tejun Heo , Greg Kroah-Hartman Cc: Kevin Mitchell , stable@vger.kernel.org, Chengming Zhou , driver-core@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH v3] kernfs: fix race to increment for nr_mmapped in kernfs_fop_mmap() Date: Fri, 31 Jul 2026 12:08:50 -0700 Message-ID: <20260731190850.235909-2-kevmitch@arista.com> Content-Transfer-Encoding: 8bit Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Counts of files to be released (nr_to_release) and mmapped (nr_mmapped) files were added to kernfs_open_node in commit bdb2fd7fc56e ("kernfs: Skip kernfs_drain_open_files() more aggressively") to optimize kernfs_drain_open_files(). A WARN_ON_ONCE sanity check was also added in kernfs_drain_open_files() to ensure that these counters were brought to zero once all files had been drained. Modifications to these counters were protected by kernfs_open_file_mutex everywhere except for in kernfs_fop_mmap(). This caused a race condition where some nr_mmapped increments could get overwritten even while the correct number of kernfs_open_files with mmapped == true were present in the kernfs_open_node's files list. Consequently, the iteration in kernfs_drain_open_files() would underflow nr_mmapped and the WARNING would fire. To fix this, acquire kernfs_open_file_mutex around nr_mmapped updates in kernfs_fop_mmap. The nesting of->mutex -> kernfs_open_file_mutex is safe as kernfs_open_file_mutex is acquired last avoiding the possible cycles highlighted in commit f83f3c515654 ("kernfs: fix locking around kernfs_ops->release() callback"). Fixes: bdb2fd7fc56e ("kernfs: Skip kernfs_drain_open_files() more aggressively") Cc: stable@vger.kernel.org Signed-off-by: Kevin Mitchell --- Changes in v3: - Remove stray linebreak and cc stable. - Link to v1: https://lore.kernel.org/all/20251119191758.612694-2-kevmitch@arista.com - Link to v2: https://lore.kernel.org/all/20260731010914.233067-2-kevmitch@arista.com fs/kernfs/file.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/fs/kernfs/file.c b/fs/kernfs/file.c index 8e0e90c93372..76ade0d0be04 100644 --- a/fs/kernfs/file.c +++ b/fs/kernfs/file.c @@ -495,8 +495,11 @@ static int kernfs_fop_mmap(struct file *file, struct vm_area_struct *vma) rc = 0; if (!of->mmapped) { - of->mmapped = true; + struct mutex *mutex = kernfs_open_file_mutex_lock(of->kn); + of_on(of)->nr_mmapped++; + mutex_unlock(mutex); + of->mmapped = true; of->vm_ops = vma->vm_ops; } vma->vm_ops = &kernfs_vm_ops; -- 2.51.0