From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from out30-131.freemail.mail.aliyun.com (out30-131.freemail.mail.aliyun.com [115.124.30.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C405223EA83 for ; Thu, 25 Dec 2025 10:08:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=115.124.30.131 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766657301; cv=none; b=cKmRpA5fX9hlpzj62EqFwVY1EEQgDS0VWOWKeR4npx+FC8W36tDJYWFOuUCCL0Jun+ellvvCmjgLBBX4mcYi4hWVTx/9eQXZLDGYSYjz/06/FQRj+bl3IyOiJGDyufRzQClGEMaQpNwilqkwusIOKrxD5RRwAFXUK2GqFlRIFR4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766657301; c=relaxed/simple; bh=VkR3S8eL/ZMTYtRH6fCysmlOz65NsB8RcZXJM3AHPbY=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=bjux1bNrnXvjZJxvVy3712zDrTiiYXIqP2ilZciy039tbmKUGo4lzizV2DCbeurWIJHhKPbwOWcy2VzLGdDEqmOJQ4ulMN4sG0pnpLzB6nUEPPMJtcb932PPpI+9atWC8xk2/KqGfGYkft3T0EKzKjP4A8KJ1/rXlP4vQHCyQM0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com; spf=pass smtp.mailfrom=linux.alibaba.com; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b=mS10BvLu; arc=none smtp.client-ip=115.124.30.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b="mS10BvLu" DKIM-Signature:v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1766657289; h=Message-ID:Date:MIME-Version:Subject:To:From:Content-Type; bh=bgFMQJIl6IU/hL8mMn/JPwDrQ4dql3AhRFfIkIc6/O8=; b=mS10BvLu1eMqIGlxBGBWqv/IO6Ryf0OUWS1v2FntmsL3l86944pCTqUACIy5TvYqX0378KnTLP2VlpGYpbx/irZtXcVYtHBwU9qksr6agxWCrW3cRz734O1qzAcCR+jjPoQZdkkAbQaSJGIFk+lQDZOgWQhY+czO5OCml1mcHXE= Received: from 30.74.144.121(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0WveUWeH_1766657288 cluster:ay36) by smtp.aliyun-inc.com; Thu, 25 Dec 2025 18:08:09 +0800 Message-ID: Date: Thu, 25 Dec 2025 18:08:08 +0800 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] mm/shmem: fix uninitialized folio in shmem_symlink To: Barry Song <21cnbao@gmail.com>, Matthew Wilcox Cc: akpm@linux-foundation.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Hugh Dickins , syzbot+178fff6149127421c2cc@syzkaller.appspotmail.com References: <20251224094027.65842-1-21cnbao@gmail.com> From: Baolin Wang In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit On 2025/12/25 12:04, Barry Song wrote: > On Thu, Dec 25, 2025 at 6:01 AM Matthew Wilcox wrote: >> >> On Wed, Dec 24, 2025 at 10:40:27PM +1300, Barry Song wrote: >>> From: Barry Song >>> >>> Uninitialized folio allocated in shmem_symlink() may be accessed >>> during swap-out, causing KMSAN BUG: >> >> This would be an unfortunate way to fix it. The vast majority of >> symlinks are short, and we'll never access past the \0 in normal >> operation, so we'll be dirtying a lot of cachelines essentially to (1) >> shut up an automated tool and (2) optimise a corner case. >> >> How about this instead which delays zeroing to swapout? > > Matthew, thank you very much for your review, even during Christmas. > I would like to wish you a happy holiday! > > I am not quite sure, as shm symlinks do not seem very common. Since > allocating a folio requires a symname longer than 128 bytes (where > 128 == SHORT_SYMLINK_LEN), such cases appear even rarer. > > BTW, do we need to migrate the owner_2 flag in folio_migrate_flags()? > If so, I am not quite sure it is worth changing the hotpath to > accommodate this. +1. At least for me, using the 'PG_owner_2' flag alone to mark this uncommon case doesn't seem quite worthwhile. >> diff --git a/mm/shmem.c b/mm/shmem.c >> index ec6c01378e9d..f3b3be1b50fe 100644 >> --- a/mm/shmem.c >> +++ b/mm/shmem.c >> @@ -1636,6 +1636,13 @@ int shmem_writeout(struct folio *folio, struct swap_iocb **plug, >> folio_mark_uptodate(folio); >> } >> >> + /* Zero out symlink tails to help with compression */ >> + if (folio_test_owner_2(folio)) { >> + struct inode *inode = folio->mapping->host; >> + folio_zero_segment(folio, inode->i_size, folio_size(folio)); >> + folio_clear_owner_2(folio); >> + } >> + >> if (!folio_alloc_swap(folio)) { >> bool first_swapped = shmem_recalc_inode(inode, 0, nr_pages); >> int error; >> @@ -4133,6 +4140,7 @@ static int shmem_symlink(struct mnt_idmap *idmap, struct inode *dir, >> memcpy(folio_address(folio), symname, len); >> folio_mark_uptodate(folio); >> folio_mark_dirty(folio); >> + folio_set_owner_2(folio); >> folio_unlock(folio); >> folio_put(folio); >> } > > Thanks > Barry