From b4a7748a1841cfa2d37a93e0066c2bd0038ada39 Mon Sep 17 00:00:00 2001 From: Richard Weinberger Date: Mon, 14 May 2012 17:55:51 +0200 Subject: UBI: Kill data type hint We do not need this feature and to our shame it even was not working and there was a bug found very recently. -- Artem Bityutskiy Without the data type hint UBI2 (fastmap) will be easier to implement. Signed-off-by: Richard Weinberger Signed-off-by: Artem Bityutskiy --- include/linux/mtd/ubi.h | 27 +++------------------------ 1 file changed, 3 insertions(+), 24 deletions(-) (limited to 'include') diff --git a/include/linux/mtd/ubi.h b/include/linux/mtd/ubi.h index db4836bed514..9838dce7e235 100644 --- a/include/linux/mtd/ubi.h +++ b/include/linux/mtd/ubi.h @@ -208,12 +208,12 @@ void ubi_close_volume(struct ubi_volume_desc *desc); int ubi_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset, int len, int check); int ubi_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf, - int offset, int len, int dtype); + int offset, int len); int ubi_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf, - int len, int dtype); + int len); int ubi_leb_erase(struct ubi_volume_desc *desc, int lnum); int ubi_leb_unmap(struct ubi_volume_desc *desc, int lnum); -int ubi_leb_map(struct ubi_volume_desc *desc, int lnum, int dtype); +int ubi_leb_map(struct ubi_volume_desc *desc, int lnum); int ubi_is_mapped(struct ubi_volume_desc *desc, int lnum); int ubi_sync(int ubi_num); @@ -226,25 +226,4 @@ static inline int ubi_read(struct ubi_volume_desc *desc, int lnum, char *buf, { return ubi_leb_read(desc, lnum, buf, offset, len, 0); } - -/* - * This function is the same as the 'ubi_leb_write()' functions, but it does - * not have the data type argument. - */ -static inline int ubi_write(struct ubi_volume_desc *desc, int lnum, - const void *buf, int offset, int len) -{ - return ubi_leb_write(desc, lnum, buf, offset, len, UBI_UNKNOWN); -} - -/* - * This function is the same as the 'ubi_leb_change()' functions, but it does - * not have the data type argument. - */ -static inline int ubi_change(struct ubi_volume_desc *desc, int lnum, - const void *buf, int len) -{ - return ubi_leb_change(desc, lnum, buf, len, UBI_UNKNOWN); -} - #endif /* !__LINUX_UBI_H__ */ -- cgit v1.2.3 From 092c66b2a6a098b0e7795ee86a0d86edd53fa9ad Mon Sep 17 00:00:00 2001 From: Richard Weinberger Date: Mon, 14 May 2012 17:55:52 +0200 Subject: UBI: remove data type hint from ubi-user.h This finally removes the data type hint from the UBI ABI. >From now on the "dtype" field will be ignored and must not used anymore. Signed-off-by: Richard Weinberger Signed-off-by: Artem Bityutskiy --- include/mtd/ubi-user.h | 23 ++--------------------- 1 file changed, 2 insertions(+), 21 deletions(-) (limited to 'include') diff --git a/include/mtd/ubi-user.h b/include/mtd/ubi-user.h index 3c4109777aff..370c750115ce 100644 --- a/include/mtd/ubi-user.h +++ b/include/mtd/ubi-user.h @@ -195,23 +195,6 @@ /* Maximum amount of UBI volumes that can be re-named at one go */ #define UBI_MAX_RNVOL 32 -/* - * UBI data type hint constants. - * - * UBI_LONGTERM: long-term data - * UBI_SHORTTERM: short-term data - * UBI_UNKNOWN: data persistence is unknown - * - * These constants are used when data is written to UBI volumes in order to - * help the UBI wear-leveling unit to find more appropriate physical - * eraseblocks. - */ -enum { - UBI_LONGTERM = 1, - UBI_SHORTTERM = 2, - UBI_UNKNOWN = 3, -}; - /* * UBI volume type constants. * @@ -375,25 +358,23 @@ struct ubi_rnvol_req { * requests. * @lnum: logical eraseblock number to change * @bytes: how many bytes will be written to the logical eraseblock - * @dtype: data type (%UBI_LONGTERM, %UBI_SHORTTERM, %UBI_UNKNOWN) * @padding: reserved for future, not used, has to be zeroed */ struct ubi_leb_change_req { __s32 lnum; __s32 bytes; - __s8 dtype; + __s8 dtype; /* obsolete, do not use! */ __s8 padding[7]; } __packed; /** * struct ubi_map_req - a data structure used in map LEB requests. * @lnum: logical eraseblock number to unmap - * @dtype: data type (%UBI_LONGTERM, %UBI_SHORTTERM, %UBI_UNKNOWN) * @padding: reserved for future, not used, has to be zeroed */ struct ubi_map_req { __s32 lnum; - __s8 dtype; + __s8 dtype; /* obsolete, do not use! */ __s8 padding[3]; } __packed; -- cgit v1.2.3 From 3f0b81760a5843489ca29bbf2cd1c7999609e75a Mon Sep 17 00:00:00 2001 From: Artem Bityutskiy Date: Mon, 14 May 2012 19:49:35 +0300 Subject: UBI: amend commentaries WRT dtype Richard removed the "dtype" hint, but few commentaries were left and this patch removes them. I've also added a better description about the "dtype" field in the ubi-user.h for people who may ever wonder what was that dtype thing about. This patch also adds an important note that it is better to use value "3" for the "dtype" field. Signed-off-by: Artem Bityutskiy --- include/mtd/ubi-user.h | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'include') diff --git a/include/mtd/ubi-user.h b/include/mtd/ubi-user.h index 370c750115ce..8787349fbafe 100644 --- a/include/mtd/ubi-user.h +++ b/include/mtd/ubi-user.h @@ -358,7 +358,17 @@ struct ubi_rnvol_req { * requests. * @lnum: logical eraseblock number to change * @bytes: how many bytes will be written to the logical eraseblock + * @dtype: pass "3" for better compatibility with old kernels * @padding: reserved for future, not used, has to be zeroed + * + * The @dtype field used to inform UBI about what kind of data will be written + * to the LEB: long term (value 1), short term (value 2), unknown (value 3). + * UBI tried to pick a PEB with lower erase counter for short term data and a + * PEB with higher erase counter for long term data. But this was not really + * used because users usually do not know this and could easily mislead UBI. We + * removed this feature in May 2012. UBI currently just ignores the @dtype + * field. But for better compatibility with older kernels it is recommended to + * set @dtype to 3 (unknown). */ struct ubi_leb_change_req { __s32 lnum; @@ -369,6 +379,7 @@ struct ubi_leb_change_req { /** * struct ubi_map_req - a data structure used in map LEB requests. + * @dtype: pass "3" for better compatibility with old kernels * @lnum: logical eraseblock number to unmap * @padding: reserved for future, not used, has to be zeroed */ -- cgit v1.2.3 From cc1dd2d091b1882375f0a02b7c7d88cba128dc69 Mon Sep 17 00:00:00 2001 From: Artem Bityutskiy Date: Sun, 20 May 2012 21:14:22 +0300 Subject: UBI: introduce UBI_ALL constant Joel will use it in his 'ubi_flush()' extention to specify all eraseblocks. Also amend the comment for UBI_UNKNOWN - it is used beyond attaching info structure now. Signed-off-by: Artem Bityutskiy --- include/linux/mtd/ubi.h | 3 +++ 1 file changed, 3 insertions(+) (limited to 'include') diff --git a/include/linux/mtd/ubi.h b/include/linux/mtd/ubi.h index 9838dce7e235..f636f5df883f 100644 --- a/include/linux/mtd/ubi.h +++ b/include/linux/mtd/ubi.h @@ -25,6 +25,9 @@ #include #include +/* All voumes/LEBs */ +#define UBI_ALL -1 + /* * enum ubi_open_mode - UBI volume open mode constants. * -- cgit v1.2.3 From a9259de54e6a20c840ebdb08b0383e01bc4877df Mon Sep 17 00:00:00 2001 From: Joel Reardon Date: Sun, 20 May 2012 21:27:11 +0200 Subject: UBI: modify ubi_wl_flush function to clear work queue for a lnum This patch modifies ubi_wl_flush to force the erasure of particular volume id / logical eraseblock number pairs. Previous functionality is preserved when passing UBI_ALL for both values. The locations where ubi_wl_flush were called are appropriately changed: ubi_leb_erase only flushes for the erased LEB, and ubi_create_volume forces only flushing for its volume id. External code can call this new feature via the new function ubi_flush() added to kapi.c, which simply passes through to ubi_wl_flush(). This was tested by disabling the call to do_work in ubi thread, which results in the work queue remaining unless explicitly called to remove. UBIFS was changed to call ubifs_leb_change 50 times for four different LEBs. Then the new function was called to clear the queue: passing wrong volume ids / lnum, correct ones, and finally UBI_ALL for both to ensure it was finally all cleard. The work queue was dumped each time and the selective removal of the particular LEB numbers was observed. Extra checks were enabled and ubifs's integck was also run. Finally, the drive was repeatedly filled and emptied to ensure that the queue was cleared normally. Artem: amended the patch. Signed-off-by: Joel Reardon Signed-off-by: Artem Bityutskiy --- include/linux/mtd/ubi.h | 1 + 1 file changed, 1 insertion(+) (limited to 'include') diff --git a/include/linux/mtd/ubi.h b/include/linux/mtd/ubi.h index f636f5df883f..c3918a0684fe 100644 --- a/include/linux/mtd/ubi.h +++ b/include/linux/mtd/ubi.h @@ -219,6 +219,7 @@ int ubi_leb_unmap(struct ubi_volume_desc *desc, int lnum); int ubi_leb_map(struct ubi_volume_desc *desc, int lnum); int ubi_is_mapped(struct ubi_volume_desc *desc, int lnum); int ubi_sync(int ubi_num); +int ubi_flush(int ubi_num, int vol_id, int lnum); /* * This function is the same as the 'ubi_leb_read()' function, but it does not -- cgit v1.2.3 From 66a5b91075c983921638470c5a148d6ad550c133 Mon Sep 17 00:00:00 2001 From: Richard Genoud Date: Tue, 10 Jul 2012 18:23:39 +0200 Subject: mtd: mark mtd_is_partition argument as constant 'struct mtd_info' is not modified by 'mtd_is_partition()' so it can be marked as "const". Signed-off-by: Richard Genoud Signed-off-by: Artem Bityutskiy --- include/linux/mtd/partitions.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'include') diff --git a/include/linux/mtd/partitions.h b/include/linux/mtd/partitions.h index 3a6f0372fc96..69970692f0b2 100644 --- a/include/linux/mtd/partitions.h +++ b/include/linux/mtd/partitions.h @@ -89,7 +89,7 @@ static inline int mtd_has_cmdlinepart(void) { return 1; } static inline int mtd_has_cmdlinepart(void) { return 0; } #endif -int mtd_is_partition(struct mtd_info *mtd); +int mtd_is_partition(const struct mtd_info *mtd); int mtd_add_partition(struct mtd_info *master, char *name, long long offset, long long length); int mtd_del_partition(struct mtd_info *master, int partno); -- cgit v1.2.3 From e29b98673203102d2cd363362ff503d41c702456 Mon Sep 17 00:00:00 2001 From: Richard Genoud Date: Tue, 10 Jul 2012 18:23:40 +0200 Subject: mtd: mtdparts: introduce mtd_get_device_size 'mtd_get_device_size()' returns the size of the whole MTD device, that is the mtd_info master size. This will be used by UBI to calculate the maximum number of bad blocks (MBB) on a MTD device. Artem: amended the patch a bit. Signed-off-by: Richard Genoud Signed-off-by: Artem Bityutskiy --- include/linux/mtd/partitions.h | 1 + 1 file changed, 1 insertion(+) (limited to 'include') diff --git a/include/linux/mtd/partitions.h b/include/linux/mtd/partitions.h index 69970692f0b2..afe4fa00a29e 100644 --- a/include/linux/mtd/partitions.h +++ b/include/linux/mtd/partitions.h @@ -93,5 +93,6 @@ int mtd_is_partition(const struct mtd_info *mtd); int mtd_add_partition(struct mtd_info *master, char *name, long long offset, long long length); int mtd_del_partition(struct mtd_info *master, int partno); +uint64_t mtd_get_device_size(const struct mtd_info *mtd); #endif -- cgit v1.2.3 From 072f08afcca6644e86cb4124fc9de7539ee1bac7 Mon Sep 17 00:00:00 2001 From: Richard Genoud Date: Mon, 20 Aug 2012 18:00:15 +0200 Subject: UBI: add max_beb_per1024 to attach ioctl This patch provides a possibility to set the "maximum expected number of bad blocks per 1024 blocks" (max_beb_per1024) for each mtd device using the UBI_IOCATT ioctl. Signed-off-by: Richard Genoud Signed-off-by: Artem Bityutskiy --- include/mtd/ubi-user.h | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) (limited to 'include') diff --git a/include/mtd/ubi-user.h b/include/mtd/ubi-user.h index 8787349fbafe..53cae1e11e57 100644 --- a/include/mtd/ubi-user.h +++ b/include/mtd/ubi-user.h @@ -222,6 +222,7 @@ enum { * @ubi_num: UBI device number to create * @mtd_num: MTD device number to attach * @vid_hdr_offset: VID header offset (use defaults if %0) + * @max_beb_per1024: maximum expected number of bad PEB per 1024 PEBs * @padding: reserved for future, not used, has to be zeroed * * This data structure is used to specify MTD device UBI has to attach and the @@ -245,12 +246,25 @@ enum { * be 2KiB-64 bytes = 1984. Note, that this position is not even 512-bytes * aligned, which is OK, as UBI is clever enough to realize this is 4th * sub-page of the first page and add needed padding. + * + * The @max_beb_per1024 is the maximum amount of bad PEBs UBI expects on the + * UBI device per 1024 eraseblocks. This value is often given in an other form + * in the NAND datasheet (min NVB i.e. minimal number of valid blocks). The + * maximum expected bad eraseblocks per 1024 is then: + * 1024 * (1 - MinNVB / MaxNVB) + * Which gives 20 for most NAND devices. This limit is used in order to derive + * amount of eraseblock UBI reserves for handling new bad blocks. If the device + * has more bad eraseblocks than this limit, UBI does not reserve any physical + * eraseblocks for new bad eraseblocks, but attempts to use available + * eraseblocks (if any). The accepted range is 0-768. If 0 is given, the + * default kernel value of %CONFIG_MTD_UBI_BEB_LIMIT will be used. */ struct ubi_attach_req { __s32 ubi_num; __s32 mtd_num; __s32 vid_hdr_offset; - __s8 padding[12]; + __s16 max_beb_per1024; + __s8 padding[10]; }; /** -- cgit v1.2.3 From 9280bea5b78b6a619c78b63f8273f5ec2e9b8d62 Mon Sep 17 00:00:00 2001 From: Salman Qazi Date: Fri, 9 Mar 2012 16:41:01 -0800 Subject: sched/x86: Fix overflow in cyc2ns_offset Artem: only left the 'mult_frac' addition from this patch, as we need it in ubi. Signed-off-by: Salman Qazi Acked-by: John Stultz Acked-by: Peter Zijlstra Cc: Paul Turner Cc: john stultz Link: http://lkml.kernel.org/r/20120310004027.19291.88460.stgit@dungbeetle.mtv.corp.google.com Signed-off-by: Ingo Molnar --- include/linux/kernel.h | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'include') diff --git a/include/linux/kernel.h b/include/linux/kernel.h index 46ac9a50528d..349180f93fa0 100644 --- a/include/linux/kernel.h +++ b/include/linux/kernel.h @@ -85,6 +85,19 @@ } \ ) +/* + * Multiplies an integer by a fraction, while avoiding unnecessary + * overflow or loss of precision. + */ +#define mult_frac(x, numer, denom)( \ +{ \ + typeof(x) quot = (x) / (denom); \ + typeof(x) rem = (x) % (denom); \ + (quot * (numer)) + ((rem * (numer)) / (denom)); \ +} \ +) + + #define _RET_IP_ (unsigned long)__builtin_return_address(0) #define _THIS_IP_ ({ __label__ __here; __here: (unsigned long)&&__here; }) -- cgit v1.2.3 From 7f7b3145889da64644f04f026101f33e1702357f Mon Sep 17 00:00:00 2001 From: Akinobu Mita Date: Mon, 17 Dec 2012 16:04:23 -0800 Subject: random32: rename random32 to prandom This renames all random32 functions to have 'prandom_' prefix as follows: void prandom_seed(u32 seed); /* rename from srandom32() */ u32 prandom_u32(void); /* rename from random32() */ void prandom_seed_state(struct rnd_state *state, u64 seed); /* rename from prandom32_seed() */ u32 prandom_u32_state(struct rnd_state *state); /* rename from prandom32() */ The purpose of this renaming is to prevent some kernel developers from assuming that prandom32() and random32() might imply that only prandom32() was the one using a pseudo-random number generator by prandom32's "p", and the result may be a very embarassing security exposure. This concern was expressed by Theodore Ts'o. And furthermore, I'm going to introduce new functions for getting the requested number of pseudo-random bytes. If I continue to use both prandom32 and random32 prefixes for these functions, the confusion is getting worse. As a result of this renaming, "prandom_" is the common prefix for pseudo-random number library. Currently, srandom32() and random32() are preserved because it is difficult to rename too many users at once. Signed-off-by: Akinobu Mita Cc: "Theodore Ts'o" Cc: Robert Love Cc: Michel Lespinasse Cc: Valdis Kletnieks Cc: David Laight Cc: Adrian Hunter Cc: Artem Bityutskiy Cc: David Woodhouse Cc: Eilon Greenstein Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds --- include/linux/random.h | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) (limited to 'include') diff --git a/include/linux/random.h b/include/linux/random.h index d13059f3ea32..f262b9dddc87 100644 --- a/include/linux/random.h +++ b/include/linux/random.h @@ -64,10 +64,17 @@ extern const struct file_operations random_fops, urandom_fops; unsigned int get_random_int(void); unsigned long randomize_range(unsigned long start, unsigned long end, unsigned long len); -u32 random32(void); -void srandom32(u32 seed); +u32 prandom_u32(void); +void prandom_seed(u32 seed); -u32 prandom32(struct rnd_state *); +/* + * These macros are preserved for backward compatibility and should be + * removed as soon as a transition is finished. + */ +#define random32() prandom_u32() +#define srandom32(seed) prandom_seed(seed) + +u32 prandom_u32_state(struct rnd_state *); /* * Handle minimum values for seeds @@ -78,11 +85,11 @@ static inline u32 __seed(u32 x, u32 m) } /** - * prandom32_seed - set seed for prandom32(). + * prandom_seed_state - set seed for prandom_u32_state(). * @state: pointer to state structure to receive the seed. * @seed: arbitrary 64-bit value to use as a seed. */ -static inline void prandom32_seed(struct rnd_state *state, u64 seed) +static inline void prandom_seed_state(struct rnd_state *state, u64 seed) { u32 i = (seed >> 32) ^ (seed << 10) ^ seed; -- cgit v1.2.3 From ead71fd20b54f426ae393c0919327fcb1c7c2356 Mon Sep 17 00:00:00 2001 From: Akinobu Mita Date: Mon, 17 Dec 2012 16:04:25 -0800 Subject: prandom: introduce prandom_bytes() and prandom_bytes_state() Add functions to get the requested number of pseudo-random bytes. The difference from get_random_bytes() is that it generates pseudo-random numbers by prandom_u32(). It doesn't consume the entropy pool, and the sequence is reproducible if the same rnd_state is used. So it is suitable for generating random bytes for testing. Signed-off-by: Akinobu Mita Cc: "Theodore Ts'o" Cc: Artem Bityutskiy Cc: Adrian Hunter Cc: David Woodhouse Cc: Eilon Greenstein Cc: David Laight Cc: Michel Lespinasse Cc: Robert Love Cc: Valdis Kletnieks Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds --- include/linux/random.h | 2 ++ 1 file changed, 2 insertions(+) (limited to 'include') diff --git a/include/linux/random.h b/include/linux/random.h index f262b9dddc87..fe0e2605af5d 100644 --- a/include/linux/random.h +++ b/include/linux/random.h @@ -65,6 +65,7 @@ unsigned int get_random_int(void); unsigned long randomize_range(unsigned long start, unsigned long end, unsigned long len); u32 prandom_u32(void); +void prandom_bytes(void *buf, int nbytes); void prandom_seed(u32 seed); /* @@ -75,6 +76,7 @@ void prandom_seed(u32 seed); #define srandom32(seed) prandom_seed(seed) u32 prandom_u32_state(struct rnd_state *); +void prandom_bytes_state(struct rnd_state *state, void *buf, int nbytes); /* * Handle minimum values for seeds -- cgit v1.2.3 From b3743d58728192f99767c272371a1d80f974537a Mon Sep 17 00:00:00 2001 From: Mike Frysinger Date: Wed, 1 May 2013 23:42:47 -0400 Subject: UBI: document UBI_IOCVOLUP better in user header The current ioctl define implies that this func expects to be passed a 64bit number directly rather than a pointer to a 64bit. The code that processes this ioctl shows that it clearly expects a pointer. It'd be best if we could change the type to "__s64*", but that would change the generated ioctl number thus breaking the userland ABI. So just add a comment for intrepid developers. Signed-off-by: Mike Frysinger Signed-off-by: Artem Bityutskiy --- include/mtd/ubi-user.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'include') diff --git a/include/mtd/ubi-user.h b/include/mtd/ubi-user.h index 53cae1e11e57..723c324590c1 100644 --- a/include/mtd/ubi-user.h +++ b/include/mtd/ubi-user.h @@ -173,7 +173,10 @@ #define UBI_VOL_IOC_MAGIC 'O' -/* Start UBI volume update */ +/* Start UBI volume update + * Note: This actually takes a pointer (__s64*), but we can't change + * that without breaking the ABI on 32bit systems + */ #define UBI_IOCVOLUP _IOW(UBI_VOL_IOC_MAGIC, 0, __s64) /* LEB erasure command, used for debugging, disabled by default */ #define UBI_IOCEBER _IOW(UBI_VOL_IOC_MAGIC, 1, __s32) -- cgit v1.2.3