From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3C866EE7FF4 for ; Mon, 11 Sep 2023 14:52:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9A3A36B02A7; Mon, 11 Sep 2023 10:52:51 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9543E6B02A8; Mon, 11 Sep 2023 10:52:51 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 81B276B02A9; Mon, 11 Sep 2023 10:52:51 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 723AF6B02A7 for ; Mon, 11 Sep 2023 10:52:51 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 1733FC04C9 for ; Mon, 11 Sep 2023 14:52:50 +0000 (UTC) X-FDA: 81224608542.27.A6460B7 Received: from mail-io1-f46.google.com (mail-io1-f46.google.com [209.85.166.46]) by imf03.hostedemail.com (Postfix) with ESMTP id 3D10C20019 for ; Mon, 11 Sep 2023 14:52:49 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=zJZx7f5O; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf03.hostedemail.com: domain of glider@google.com designates 209.85.166.46 as permitted sender) smtp.mailfrom=glider@google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1694443969; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=90PlK9+vusP71rfnLS7AlLwtgErcjzc4R1LcqrGfUdo=; b=c0W3iUeGq2F+4oGx5nJ/Sv3nykh9JBX+1lPkuxQZ8BVlqa4pz1G0I+djixTZIbIKFtyQuJ +2rfVCQfWjMv1V7C767Pd4Kho9WB/f5lS8xBtARhz/OdxQ1E6Rc+LUUlpBbfaoZTTizk0/ 6Yl8lC/xeRsL3Rn3Yst3KRNXSqUaYzc= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=zJZx7f5O; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf03.hostedemail.com: domain of glider@google.com designates 209.85.166.46 as permitted sender) smtp.mailfrom=glider@google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694443969; a=rsa-sha256; cv=none; b=hr9WOhVZqYCRsP0rsSQ6m8fUIhQfQqSoclPf05jNf3QLq1G7XxrTKa5buFAWaTrDa1SWya zeyoAuuctzqhjvmd3L5JsVyj42PCQsGAVkxHynCvZPAknAiyI5Cc4LuSbAvvAsaOkBT/VV AdAUVkoY1BO33m0LJ8miPv6Ye7ez51w= Received: by mail-io1-f46.google.com with SMTP id ca18e2360f4ac-77acb04309dso154035539f.2 for ; Mon, 11 Sep 2023 07:52:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1694443968; x=1695048768; darn=kvack.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=90PlK9+vusP71rfnLS7AlLwtgErcjzc4R1LcqrGfUdo=; b=zJZx7f5O1pmpYxWE7m1sY0qypUY/mCcxVkPjykwvJHvCJNOt7WxwZNXzioYxP+48T+ PDRRJrBw77WvaoTOjpxCuqwwXxo8FgDwA4TTuHw5Sx4KUUklECOuZE+Tuq9ZFSXeIDAt WW+hdQqQYMOMQMjQ6YqTwHv1wd8feQS80gGFA1kP+jOTT1GSadsjHl224wMZuLoqJNMh NxPHUNEavf0YKojJHVZ93KzoYWBsUYOtxlGbB6tQs7uAdOMMtAFOXQhJwfqoYHZ0yDmU EFmSm1Yi26eprWIizftrXNRjMakPHA6STTrYlC0KiBhUC8W/D0zoSngh1d7x+eMwY75m osrA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694443968; x=1695048768; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=90PlK9+vusP71rfnLS7AlLwtgErcjzc4R1LcqrGfUdo=; b=PnhKc/nU/Xs/HMjIQ0SaT5v0+lEiVUFFXaYia08w5TPfiaUEbD7H4ZHRPR9K9/f577 quHs1mYMU03IEpo5PSWUsLscs5BROIAUKGd/I3g4EoO4U4JsCvt0GqLIJ/ydACwudTdQ bqliQTIe60pub64rGcDn6ieTXnWv+O0oazJXwhb3ZDc5TlCYKuUGx6CkeLHTFX6Geky7 hWp82oncGJZzldPDVeY6x1dIkmv3xf83h2gtKX/VmGQYd5c27+wcqjvUp5MvwsQvOBN7 mcHXJof1eumej0Zu4Ns5DobRk8aU4A8c/ZZ7NlChsYPjtyj9bIJz5uYGCk2yw/avJgGn /O1A== X-Gm-Message-State: AOJu0YxQjrd3Hmmgvo+BqWkue4DZ+bU45CQCAtdt18I/c9fPeZTRa63J gc7Jr5XYDclvN7JLjN3WmPPhSiLSmhdtGP7p2eXJqQ== X-Google-Smtp-Source: AGHT+IGvTbxUjFNWLezCEgG/vtDzsVCoY9+Ws6OsnQP37i0VgZfEUBdu7CtB1oUinFIFB7S3R+7jceTBpPeixY6igig= X-Received: by 2002:a05:6602:185:b0:790:aa71:b367 with SMTP id m5-20020a056602018500b00790aa71b367mr12539404ioo.4.1694443968284; Mon, 11 Sep 2023 07:52:48 -0700 (PDT) MIME-Version: 1.0 References: <20230907130642.245222-1-glider@google.com> In-Reply-To: From: Alexander Potapenko Date: Mon, 11 Sep 2023 16:52:07 +0200 Message-ID: Subject: Re: [PATCH 1/2] kmsan: simplify kmsan_internal_memmove_metadata() To: Marco Elver Cc: dvyukov@google.com, akpm@linux-foundation.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, kasan-dev@googlegroups.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 3D10C20019 X-Stat-Signature: o7363diigzurykzeur4uo9qr1f1rimz5 X-Rspam-User: X-HE-Tag: 1694443969-189728 X-HE-Meta: U2FsdGVkX1+f3mZ8T5DXHppNy/XNVNP7Qvn7MR+l5wW9R6v3DGeBSYeTEh91C2cZqCpdvwrXtRDqutjjDZEhyM8dTtQlOxWnaHwtYgyVnFPU3T+s57ynkvSGjhML1gz7wKJRed2wHNAvHwBkuWH2OIAR2m9omzC9kdsl5TF3O8GAaZSBjozfiFTiYDwPvO0QUaLL92Z00KrIBpwicuV0OHwVnlit6HqOWC6gYz7ScSzEWBwQUgv1GL+ty3VKU+OLDqiyp3SpNEekoFCUFCZhta8LVM3mJHE1cf4VNaXEcY1T32OcybcCmMzdwaOEMkqbdx7EObYil75vDHYppPtLd5rpN7CBNzHRIZZBPqdEnPRP3TGnCpHhfh/RAWL78U7yd+S8yhavgRoJiXBclvsgt4eCZpY3UG+nkVEoqG4zEeNPPBrrGOaDaLewTx2q7vojDNKpSByXNg7MrdU/j/MQ0WHkBzTOY24IW11XdVJGsXbOl/HBRYBi2zotIpTnVZQn3SJB3X9ii1NtnT0gQDP5a5R9+8++TQUZ49GU0kaCeHSAzR7K09LYLQ8dRrQnX4Mgbt0tNooeFUU50A/8ikprO4qSjCUHX4LKta9MwHhUnBtzbxhoLW49bQKlhesNyPkeVr+T2I6cyrtyzvkLRm21JpMhcWf/Jj5nSZnY+faBkVrCXDeJ+IL4yMgwo4gAL5UQdKUy66ROFnAcU9WyxBBTAzucl7DZxys/kAF3LrG2K9oFhi/WNknIFLHQbxDMxkmZAi4cSphi7+lJnWc0l0SohogKQWApwsfaQ4v0JfAzewbbCr7ahaxn8zcZm6bc/cjx2NFg5dsDL8MEaGjJRV7Czm6qsBFCj7n2GvBcnRHsKipustRTt0i+ipRdmvTaAZk/WKJm0B4Hw7oGydOVp0Yk6fOGURRpADED1VCMXQltnJjlzXWY1qNq5nKCpERZhgCyP+A5WmbPKjFo/A/hawS 1eEwszid WFPWXhiikhE+lN7/3ax20tObzfsp76rQplo+lUeK4wGrykqVXYslgOJ3RiETDY7IiByK9yPvnale6QnX58ezPYv2MLQMb2mpUColYzd5UlKB18SGMKIF2QHnJAYW8KirJ1ApbYnUKQjE5JUYh+/zllhn5DBrvKfYRuvs/pTbxr0Fvt6UVZZ/5VaLl9VX69FS6iNd0Kuj0e/2u490uUZDpjbPJZn0tird/DCLFEsAQ3xzFVRfSx7TMUu8T1bvpPbVwLNm5n750CYH9dWGRg+dYUqIgNMbsG7ApHdaq9bQITGzR8uKUWKivTonVEpufJjX3OKSPS4qTuA8BDe8= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000310, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Mon, Sep 11, 2023 at 1:44=E2=80=AFPM Marco Elver wrot= e: > > On Thu, 7 Sept 2023 at 15:06, Alexander Potapenko wro= te: > > > > kmsan_internal_memmove_metadata() is the function that implements > > copying metadata every time memcpy()/memmove() is called. > > Because shadow memory stores 1 byte per each byte of kernel memory, > > copying the shadow is trivial and can be done by a single memmove() > > call. > > Origins, on the other hand, are stored as 4-byte values corresponding > > to every aligned 4 bytes of kernel memory. Therefore, if either the > > source or the destination of kmsan_internal_memmove_metadata() is > > unaligned, the number of origin slots corresponding to the source or > > destination may differ: > > > > 1) memcpy(0xffff888080a00000, 0xffff888080900000, 4) > > copies 1 origin slot into 1 origin slot: > > > > src (0xffff888080900000): xxxx > > src origins: o111 > > dst (0xffff888080a00000): xxxx > > dst origins: o111 > > > > 2) memcpy(0xffff888080a00001, 0xffff888080900000, 4) > > copies 1 origin slot into 2 origin slots: > > > > src (0xffff888080900000): xxxx > > src origins: o111 > > dst (0xffff888080a00000): .xxx x... > > dst origins: o111 o111 > > > > 3) memcpy(0xffff888080a00000, 0xffff888080900001, 4) > > copies 2 origin slots into 1 origin slot: > > > > src (0xffff888080900000): .xxx x... > > src origins: o111 o222 > > dst (0xffff888080a00000): xxxx > > dst origins: o111 > > (or o222) > > > > Previously, kmsan_internal_memmove_metadata() tried to solve this > > problem by copying min(src_slots, dst_slots) as is and cloning the > > missing slot on one of the ends, if needed. > > This was error-prone even in the simple cases where 4 bytes were copied= , > > and did not account for situations where the total number of nonzero > > origin slots could have increased by more than one after copying: > > > > memcpy(0xffff888080a00000, 0xffff888080900002, 8) > > > > src (0xffff888080900002): ..xx .... xx.. > > src origins: o111 0000 o222 > > dst (0xffff888080a00000): xx.. ..xx > > o111 0000 > > (or 0000 o222) > > > > The new implementation simply copies the shadow byte by byte, and > > updates the corresponding origin slot, if the shadow byte is nonzero. > > This approach can handle complex cases with mixed initialized and > > uninitialized bytes. Similarly to KMSAN inline instrumentation, latter > > writes to bytes sharing the same origin slots take precedence. > > > > Signed-off-by: Alexander Potapenko > > I think this needs a Fixes tag. Thanks, will add in v2! > Also, is this corner case exercised by one of the KMSAN KUnit test cases? Ditto > Otherwise, > > Acked-by: Marco Elver