From nobody Mon Jun 8 11:02:52 2026 Received: from mail-pf1-f172.google.com (mail-pf1-f172.google.com [209.85.210.172]) (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 0508430F815 for ; Fri, 29 May 2026 16:00:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780070418; cv=none; b=hoPLpNXYhZr8RPktN9hrD1as1ajuAODcOurh2BpccF5OFvlbLVUxFLJebr5upqpB82bpEoI9I7yL+XJpe1OAzguZx/vMp3DrSupV5iGPqI1DUhj6rV25ohcRYBZKF0x5VATOv5sBimPCDfReDhBPsF9RHLhk+nWjMZrN3lxaf/M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780070418; c=relaxed/simple; bh=k4lx5eyaIkFZb9LSl33QVXj3R4oz3we3nc7csf+YodI=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=GSQlf0jLEZcLTT9VnzHo5yB1HGsOBP2MUGolJ7legJbj9zsJsLHwvsVN2Wc3myca3NBBG5SJUnev/B6I66ZAsW7rD5ENLuXpWPvgYH0e5iAIwx+vkCp2gX5SbwGDXkaFZ2NDDXxI88JBTtsgjjCW0OVv1qty1VfXvocSGMcEJ+0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=G0f2cCtr; arc=none smtp.client-ip=209.85.210.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="G0f2cCtr" Received: by mail-pf1-f172.google.com with SMTP id d2e1a72fcca58-8422119a826so121907b3a.0 for ; Fri, 29 May 2026 09:00:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1780070415; x=1780675215; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=hKu7L3y2TqCk1Y4XvXgVCljpe0pimisDl4YLc2NUkAA=; b=G0f2cCtrW/Gtspr4mMEjj5TgFLptfdRTm4IA8zgw6lQbqyaTeumxYR/oxTpI50/LiL P1RiIOruSjbMM9LTfhZCBoAxUCZLoAauYweuMLW1YTe1SuL8U+YsjlakWtQQ2ZVKgHWJ sNAbwd7RuxEUqtJH6gWajiUZIRiPQyA/9PpTUuh1YsD2T8hiW9n/mshxD9TC/ddEWKXH +ijBsY33UPqqyXFdpDReFoozLC2Q65uszjr2F7WQ6lmG3Gq+MS/O0yt16+lK9rKoS+p3 PDa0PsXmmITZrrKrBEJmJccmwZ7PQJUXQcrIIa9FtVJDri4zcbfRUknARML/VwqauLBE zL3g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780070415; x=1780675215; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=hKu7L3y2TqCk1Y4XvXgVCljpe0pimisDl4YLc2NUkAA=; b=gC1T6dgR2equMnhbDC5BkCAXmZ9G3wdPt7d+tQgJPr80K3OWDJ1GrhYN95VsdlzSD0 wsJ6LgIfq3BwYqxrmhMbP6q6LE3tUNANk+ai8yZt5g6yCRmeV1mMNNC2TtoYkqO+L0r7 hWw2TRe6QoWVwNEQ7Z1dAb2j2k/aHl1XbqzfcbqqekcSdY+fL6UWzH9u6KbuQ5/3IpX4 E03IR5+MSyMUURFfoWzavxb1Pd7WcbzaGywxQEUt5ec4ft/BVBlMSego4Wt9hNI7ss1b nUvLnQM9HNWXCAKrZOOgkKvvqLWrgj3XXCVQikbRUYywlG48JesYK63WnHXJMTxbXtZs EqtQ== X-Forwarded-Encrypted: i=1; AFNElJ81IZxUuKj0Q9UbgVxCE37Pr+BmAUGO8kNmZhSDkDpr1XfohHWgrk4Jj9ou43mKuGyQGHUNuF8PHBGZBg8=@vger.kernel.org X-Gm-Message-State: AOJu0YzjurtbR97ASrFfNVVxMWa9nEk6yPHX+3qUKMri/lhrg73kB76q 55kYjZDeZBIRP5WYIitz5+6futtKFJjLzOn3LigX9YiC2zUkYhtRxkhE X-Gm-Gg: Acq92OHJdbuTYNAKINY9LkCWNO24nK+QT7AK5OH4P6F0jCIOOqH4baQ4Gn9E9w1Sml9 lmD40WGQwZSUBlFGaFRRe9L/f3fQmZ9eUjnpjE1/twNaaxlZPV7cHwMaExIU6LMvHkej4JkGO50 gv5q8+fCI5ZEdUwlyfAskcfEYqWmetxtGL5Cg427IR3bLj5lhvILjCIvIMfXcAl4itfo2R6pNVc 9eyB97lzMH8ESmDtRfw8fo+Ea1sW2pTDAbOaMHxTbfIxo1mg6fwhaRVB2uHQlFfJ3fdb8VQhBv2 Ev1zg/nPgxD5xl/VQeBis3zFPuF1EFgdzE27GEwg4B0HqghrF7SHeyk8wfpL3ARtaGSP/6d34QU 0ZtvZTf+fh+2uf1SGDsODhznRnzyVbfiblzut4pbNCiaG0/kd7BxB2tni8XdfzZvgIKw13EzOLK UhTU50UDTTCsjgfZ9j X-Received: by 2002:a05:6a00:a218:b0:82f:39df:dd54 with SMTP id d2e1a72fcca58-8421095d2d9mr3340613b3a.8.1780070415075; Fri, 29 May 2026 09:00:15 -0700 (PDT) Received: from lgs.. ([2001:250:5800:1000::f280]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-84214ce9cebsm2220088b3a.52.2026.05.29.09.00.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 29 May 2026 09:00:14 -0700 (PDT) From: Guangshuo Li To: Alasdair Kergon , Mike Snitzer , Mikulas Patocka , Benjamin Marzinski , Ming-Hung Tsai , dm-devel@lists.linux.dev, linux-kernel@vger.kernel.org Cc: Guangshuo Li Subject: [PATCH] dm cache policy smq: check allocation under invalidate lock Date: Fri, 29 May 2026 23:57:45 +0800 Message-ID: <20260529155745.1292113-1-lgs201920130244@gmail.com> X-Mailer: git-send-email 2.43.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" commit 2d1f7b65f5de ("dm cache policy smq: fix missing locks in invalidating cache blocks") added mq->lock around the destructive part of smq_invalidate_mapping(), but left the e->allocated check outside the critical section. That leaves a check-then-act race. Two concurrent invalidators can both observe e->allocated as true before either of them takes mq->lock. The first invalidator that acquires the lock removes the entry from the queues and hash table and then calls free_entry(), which clears e->allocated and puts the entry back on the free list. The second invalidator can then acquire mq->lock and continue with the stale result of the unlocked check. This can corrupt the SMQ queues or hash table by deleting an entry that is no longer on those structures. It can also hit the allocation check in free_entry() when the same entry is freed again. Move the allocation check under mq->lock so the predicate and the destructive operations are serialized by the same lock. Fixes: 2d1f7b65f5de ("dm cache policy smq: fix missing locks in invalidatin= g cache blocks") Signed-off-by: Guangshuo Li Reviewed-by: Ming-Hung Tsai --- drivers/md/dm-cache-policy-smq.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/drivers/md/dm-cache-policy-smq.c b/drivers/md/dm-cache-policy-= smq.c index dd77a93fd68d..1ae304c2f573 100644 --- a/drivers/md/dm-cache-policy-smq.c +++ b/drivers/md/dm-cache-policy-smq.c @@ -1590,18 +1590,22 @@ static int smq_invalidate_mapping(struct dm_cache_p= olicy *p, dm_cblock_t cblock) struct smq_policy *mq =3D to_smq_policy(p); struct entry *e =3D get_entry(&mq->cache_alloc, from_cblock(cblock)); unsigned long flags; - - if (!e->allocated) - return -ENODATA; + int r =3D 0; =20 spin_lock_irqsave(&mq->lock, flags); + if (!e->allocated) { + r =3D -ENODATA; + goto out; + } // FIXME: what if this block has pending background work? del_queue(mq, e); h_remove(&mq->table, e); free_entry(&mq->cache_alloc, e); + +out: spin_unlock_irqrestore(&mq->lock, flags); =20 - return 0; + return r; } =20 static uint32_t smq_get_hint(struct dm_cache_policy *p, dm_cblock_t cblock) --=20 2.43.0