From patchwork Mon Apr 29 17:04:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Groves X-Patchwork-Id: 13647401 Received: from mail-ot1-f50.google.com (mail-ot1-f50.google.com [209.85.210.50]) (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 CB7BC86642 for ; Mon, 29 Apr 2024 17:05:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.50 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714410309; cv=none; b=ERjgffuesZGljSOc5CL5H0AYL2DMQOCcYxXRK/eBPnXNjmiOf0mvjYi1xdvPv0GZjZ51DYgplPM7SgK8iJCuK3NswUNlqAAHNVoYrdd5+PCe95U8d2opTlOYII5KjVdDetWvQAsUWQ0hNLB8Z498JxRT0cCGhHYQ5t5Ke5aZDKo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714410309; c=relaxed/simple; bh=qjg/aYIA2Aj3ETwT1bnW30oXWKlF7YzrJy/VAO7WdCo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=GNSfVgGy7QLFfgxROEbrxYP1CAi1PspfGUm1JCDcQEcCR4Fh9+aMf3r/Zru1E0vZY04vrtPEdx2hJeAr8lQgYxWc7CcCupdXbV8ztC+UX2L2P9J5EXSiEP0p0cRTby5/SL7Xgn8dw9WLDCsDi3qm7fANUPq7BNjvT0hLNZ/+s6E= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=Groves.net; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=JKjbJwOh; arc=none smtp.client-ip=209.85.210.50 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=Groves.net 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="JKjbJwOh" Received: by mail-ot1-f50.google.com with SMTP id 46e09a7af769-6ee4dcc4567so421405a34.3 for ; Mon, 29 Apr 2024 10:05:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1714410307; x=1715015107; darn=lists.linux.dev; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to; bh=rbyzPWFSpme/6Xxw3JGZg5PIHNbasBNXPhuMHV0OHPQ=; b=JKjbJwOh9MZ+1U5PJ+RutY1TvP/XbF6QnGzEsH3gdi8IcvUTxRcBdH1lw5mL92eVwz b1tiem+tZwYCJTzoAJbZEuEdaPnsFQ46cQ3OUOqPOHS5Xo4k1sOsUdEdiUUQyChoOx1U 1M5ppZ3YRQcWgS34pZOyCmgFm1eKGx5gj7R58DDlhgtk93XzUpZCP9RDqnH/r3HRjewE YiG4zPxZ9XsBk6eLjhwBkNC9rvNrv4dhVqBie5G1ZwBYMyFw/yU6ZEcjoyaZLNAHTYIX ayxhxXHZ6xGb5Lb5UhPLxzxhgX5bDgSiwu7wWj0pSpTCqZF946rro9aX15Z+ay+hYwp6 hoQQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714410307; x=1715015107; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=rbyzPWFSpme/6Xxw3JGZg5PIHNbasBNXPhuMHV0OHPQ=; b=BA8oZSMwCkzzf9Y8+qd/3icwa36t/vUVHyZYWTeFaNMkGc/ECYeLOzK1+aFaM71RdQ yXc4RC83d3yaXU2yPKQFvKMskZb36vHTosnWfG66llvo3bA7Pz1v9ApWWy8uC+XfA82r 3LtORkH6UKtVgasb1dWIx2NPYR4wWgtUPAGmL8hgnox6U1s9uVtZayHGAwPIwaPvQkYh syo/rJy+uVZW4NKaCXImIUN4r2QGOjgSSqj6XY4snemFBiD4RI/LXYEerT4hzYcBOgRD WdR5SEfzfiBxZ5oPvj2b8am9EzWBzbD+KV3FGGzlaWgcnccWXTRfl8/Wl6M05gZi+7mp 5viw== X-Forwarded-Encrypted: i=1; AJvYcCWCwhy0uzFZ9jZaK9XOB/KHaevEJhSGocTI/L8Bh/kTWi/O1jshe2Rc4leWH+tzvZ9SnfBZFUFHXREj3R8LNJQ5Ns0KL+oN X-Gm-Message-State: AOJu0YxtZkLo/8Z8Tbvg1H+Tbl+3fTpuRPSWh8VzFcRTKXv2NXkCx5/J fcvtkHeGyYVU5MsviMHvzMAhlgkrtjO3aoqdjbJh5alqKuuRfbO4 X-Google-Smtp-Source: AGHT+IGjO7mh1AyfnujisR4C9AKwUM+jghociccM9M53gOQe5r8ngqJlfGiDhTWRhS5N4AbdDyMemw== X-Received: by 2002:a05:6830:20cd:b0:6ee:32f0:ec4e with SMTP id z13-20020a05683020cd00b006ee32f0ec4emr4182582otq.31.1714410306753; Mon, 29 Apr 2024 10:05:06 -0700 (PDT) Received: from localhost.localdomain ([70.114.203.196]) by smtp.gmail.com with ESMTPSA id g1-20020a9d6201000000b006ea20712e66sm4074448otj.17.2024.04.29.10.05.04 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 29 Apr 2024 10:05:06 -0700 (PDT) Sender: John Groves From: John Groves X-Google-Original-From: John Groves To: John Groves , Jonathan Corbet , Jonathan Cameron , Dan Williams , Vishal Verma , Dave Jiang , Alexander Viro , Christian Brauner , Jan Kara , Matthew Wilcox , linux-cxl@vger.kernel.org, linux-fsdevel@vger.kernel.org, nvdimm@lists.linux.dev Cc: John Groves , john@jagalactic.com, Dave Chinner , Christoph Hellwig , dave.hansen@linux.intel.com, gregory.price@memverge.com, Randy Dunlap , Jerome Glisse , Aravind Ramesh , Ajay Joshi , Eishan Mirakhur , Ravi Shankar , Srinivasulu Thanneeru , Luis Chamberlain , Amir Goldstein , Chandan Babu R , Bagas Sanjaya , "Darrick J . Wong" , Kent Overstreet , Steve French , Nathan Lynch , Michael Ellerman , Thomas Zimmermann , Julien Panis , Stanislav Fomichev , Dongsheng Yang , John Groves Subject: [RFC PATCH v2 05/12] dev_dax_iomap: Add dax_operations for use by fs-dax on devdax Date: Mon, 29 Apr 2024 12:04:21 -0500 Message-Id: <2a8b926ce25a9ef242c933fa451b29401e62bb37.1714409084.git.john@groves.net> X-Mailer: git-send-email 2.39.3 (Apple Git-146) In-Reply-To: References: Precedence: bulk X-Mailing-List: nvdimm@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Notes about this commit: * These methods are based on pmem_dax_ops from drivers/nvdimm/pmem.c * dev_dax_direct_access() is returns the hpa, pfn and kva. The kva was newly stored as dev_dax->virt_addr by dev_dax_probe(). * The hpa/pfn are used for mmap (dax_iomap_fault()), and the kva is used for read/write (dax_iomap_rw()) * dev_dax_recovery_write() and dev_dax_zero_page_range() have not been tested yet. I'm looking for suggestions as to how to test those. Signed-off-by: John Groves --- drivers/dax/bus.c | 120 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 115 insertions(+), 5 deletions(-) diff --git a/drivers/dax/bus.c b/drivers/dax/bus.c index f894272beab8..9c57d4139b74 100644 --- a/drivers/dax/bus.c +++ b/drivers/dax/bus.c @@ -7,6 +7,10 @@ #include #include #include +#include +#include +#include +#include #include "dax-private.h" #include "bus.h" @@ -1471,6 +1475,105 @@ __weak phys_addr_t dax_pgoff_to_phys(struct dev_dax *dev_dax, pgoff_t pgoff, } EXPORT_SYMBOL_GPL(dax_pgoff_to_phys); +#if IS_ENABLED(CONFIG_DEV_DAX_IOMAP) + +static void write_dax(void *pmem_addr, struct page *page, + unsigned int off, unsigned int len) +{ + unsigned int chunk; + void *mem; + + while (len) { + mem = kmap_local_page(page); + chunk = min_t(unsigned int, len, PAGE_SIZE - off); + memcpy_flushcache(pmem_addr, mem + off, chunk); + kunmap_local(mem); + len -= chunk; + off = 0; + page++; + pmem_addr += chunk; + } +} + +static long __dev_dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff, + long nr_pages, enum dax_access_mode mode, void **kaddr, + pfn_t *pfn) +{ + struct dev_dax *dev_dax = dax_get_private(dax_dev); + size_t size = nr_pages << PAGE_SHIFT; + size_t offset = pgoff << PAGE_SHIFT; + void *virt_addr = dev_dax->virt_addr + offset; + u64 flags = PFN_DEV|PFN_MAP; + phys_addr_t phys; + pfn_t local_pfn; + size_t dax_size; + + WARN_ON(!dev_dax->virt_addr); + + if (down_read_interruptible(&dax_dev_rwsem)) + return 0; /* no valid data since we were killed */ + dax_size = dev_dax_size(dev_dax); + up_read(&dax_dev_rwsem); + + phys = dax_pgoff_to_phys(dev_dax, pgoff, nr_pages << PAGE_SHIFT); + + if (kaddr) + *kaddr = virt_addr; + + local_pfn = phys_to_pfn_t(phys, flags); /* are flags correct? */ + if (pfn) + *pfn = local_pfn; + + /* This the valid size at the specified address */ + return PHYS_PFN(min_t(size_t, size, dax_size - offset)); +} + +static int dev_dax_zero_page_range(struct dax_device *dax_dev, pgoff_t pgoff, + size_t nr_pages) +{ + long resid = nr_pages << PAGE_SHIFT; + long offset = pgoff << PAGE_SHIFT; + + /* Break into one write per dax region */ + while (resid > 0) { + void *kaddr; + pgoff_t poff = offset >> PAGE_SHIFT; + long len = __dev_dax_direct_access(dax_dev, poff, + nr_pages, DAX_ACCESS, &kaddr, NULL); + len = min_t(long, len, PAGE_SIZE); + write_dax(kaddr, ZERO_PAGE(0), offset, len); + + offset += len; + resid -= len; + } + return 0; +} + +static long dev_dax_direct_access(struct dax_device *dax_dev, + pgoff_t pgoff, long nr_pages, enum dax_access_mode mode, + void **kaddr, pfn_t *pfn) +{ + return __dev_dax_direct_access(dax_dev, pgoff, nr_pages, mode, kaddr, pfn); +} + +static size_t dev_dax_recovery_write(struct dax_device *dax_dev, pgoff_t pgoff, + void *addr, size_t bytes, struct iov_iter *i) +{ + size_t off; + + off = offset_in_page(addr); + + return _copy_from_iter_flushcache(addr, bytes, i); +} + +static const struct dax_operations dev_dax_ops = { + .direct_access = dev_dax_direct_access, + .zero_page_range = dev_dax_zero_page_range, + .recovery_write = dev_dax_recovery_write, +}; + +#endif /* IS_ENABLED(CONFIG_DEV_DAX_IOMAP) */ + static struct dev_dax *__devm_create_dev_dax(struct dev_dax_data *data) { struct dax_region *dax_region = data->dax_region; @@ -1526,11 +1629,18 @@ static struct dev_dax *__devm_create_dev_dax(struct dev_dax_data *data) } } - /* - * No dax_operations since there is no access to this device outside of - * mmap of the resulting character device. - */ - dax_dev = alloc_dax(dev_dax, NULL); + if (IS_ENABLED(CONFIG_DEV_DAX_IOMAP)) + /* holder_ops currently populated separately in a slightly + * hacky way + */ + dax_dev = alloc_dax(dev_dax, &dev_dax_ops); + else + /* + * No dax_operations since there is no access to this device + * outside of mmap of the resulting character device. + */ + dax_dev = alloc_dax(dev_dax, NULL); + if (IS_ERR(dax_dev)) { rc = PTR_ERR(dax_dev); goto err_alloc_dax;